Annotation of parser3/src/include/pa_int.h, revision 1.6
1.1 moko 1: /** @file
2: Parser: pa_int and support functions decls.
3:
1.6 ! moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.1 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
6: */
7:
8: #ifndef PA_INT_H
9: #define PA_INT_H
10:
1.6 ! moko 11: #define IDENT_PA_INT_H "$Id: pa_int.h,v 1.5 2026/01/07 18:58:26 moko Exp $"
1.1 moko 12:
13: // includes
14:
15: #include "pa_memory.h"
16: #include "pa_types.h"
17:
1.3 moko 18: #define PA_WIDE_INT
1.1 moko 19: #ifdef PA_WIDE_INT
20: // int53, -(2^53-1) .. +(2^53-1) safe convertion from double
21: #define PA_WINT_MIN -9007199254740991LL
22: #define PA_WINT_MAX 9007199254740991LL
23: typedef int64_t pa_wint;
24: typedef uint64_t pa_uwint;
1.2 moko 25: int check4int(pa_wint avalue);
1.1 moko 26: #else
27: #define PA_WINT_MIN INT_MIN
28: #define PA_WINT_MAX INT_MAX
29: typedef int pa_wint;
30: typedef uint pa_uwint;
1.5 moko 31: inline int check4int(pa_wint avalue){ return avalue; }
1.1 moko 32: #endif
33:
34: inline int clip2int(double value) {
35: return value <= INT_MIN ? INT_MIN : ( value >= INT_MAX ? INT_MAX : (int)value );
36: }
37:
1.2 moko 38: inline pa_wint clip2wint(double value) {
39: return value <= PA_WINT_MIN ? PA_WINT_MIN : ( value >= PA_WINT_MAX ? PA_WINT_MAX : (pa_wint)value );
40: }
41:
1.1 moko 42: inline uint clip2uint(double value) {
43: return value <= 0 ? 0 : ( value >= UINT_MAX ? UINT_MAX : (uint)value );
44: }
45:
46: /// Commonly used, templated to work with any integer type
47:
1.4 moko 48: template<typename T> char* pa_itoa(T n){
49: const T base=10;
1.1 moko 50: char buf[MAX_NUMBER + 1];
51: char* pos=buf + MAX_NUMBER;
52: *pos='\0';
53:
54: bool negative=n < 0;
55: if (n < 0){
56: n=-n;
57: }
58:
59: do {
60: *(--pos)=(char)(n % base) + '0';
61: n/=base;
62: } while (n > 0);
63:
64: if (negative) {
65: *(--pos) = '-';
66: }
67: return pa_strdup(pos, buf + MAX_NUMBER - pos);
68: }
69:
1.4 moko 70: char* pa_itoa(int); // custom to support INT_MIN
1.1 moko 71:
72: template<typename T> char* pa_uitoa(T n, T base=10){
73: char buf[MAX_NUMBER + 1];
74: char* pos=buf + MAX_NUMBER;
75: *pos='\0';
76:
77: do {
78: *(--pos)=(char)(n % base) + '0';
79: n/=base;
80: } while (n > 0);
81:
82: return pa_strdup(pos, buf + MAX_NUMBER - pos);
83: }
84:
85: // forwards
86: class String;
87:
88: // sign and whitespace are allowed
89: double pa_atod(const char* str, const String* problem_source);
90: int pa_atoi(const char* str, int base=10, const String* problem_source=0);
91: pa_wint pa_atowi(const char* str, int base=10, const String* problem_source=0);
92: // no sign or whitespace
93: unsigned int pa_atoui(const char *str, int base=10, const String* problem_source=0);
94: uint64_t pa_atoul(const char *str, int base=10, const String* problem_source=0);
95:
1.2 moko 96: const char* format_double(double value, const char *fmt);
97:
1.1 moko 98: #endif
E-mail: