Annotation of parser3/src/include/pa_int.h, revision 1.3

1.1       moko        1: /** @file
                      2:        Parser: pa_int and support functions decls.
                      3: 
                      4:        Copyright (c) 2001-2026 Art. Lebedev Studio (http://www.artlebedev.com)
                      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.3     ! moko       11: #define IDENT_PA_INT_H "$Id: pa_int.h,v 1.2 2026/01/06 16:36:39 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.2       moko       31: static 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: 
                     48: template<typename T> char* pa_itoa(T n, T base=10){
                     49:        char buf[MAX_NUMBER + 1];
                     50:        char* pos=buf + MAX_NUMBER;
                     51:        *pos='\0';
                     52: 
                     53:        bool negative=n < 0;
                     54:        if (n < 0){
                     55:                n=-n;
                     56:        }
                     57: 
                     58:        do {
                     59:                *(--pos)=(char)(n % base) + '0';
                     60:                n/=base;
                     61:        } while (n > 0);
                     62: 
                     63:        if (negative) {
                     64:                *(--pos) = '-';
                     65:        }
                     66:        return pa_strdup(pos, buf + MAX_NUMBER - pos);
                     67: }
                     68: 
                     69: static char* pa_itoa(int i, int base=10){ // custom to support INT_MIN
                     70:        char buf[MAX_NUMBER + 1];
                     71:        char* pos=buf + MAX_NUMBER;
                     72:        *pos='\0';
                     73: 
                     74:        bool negative=i < 0;
                     75:        unsigned int n= i < 0 ? -i : i;
                     76: 
                     77:        do {
                     78:                *(--pos)=(char)(n % base) + '0';
                     79:                n/=base;
                     80:        } while (n > 0);
                     81: 
                     82:        if (negative) {
                     83:                *(--pos) = '-';
                     84:        }
                     85:        return pa_strdup(pos, buf + MAX_NUMBER - pos);
                     86: }
                     87: 
                     88: template<typename T> char* pa_uitoa(T n, T base=10){
                     89:        char buf[MAX_NUMBER + 1];
                     90:        char* pos=buf + MAX_NUMBER;
                     91:        *pos='\0';
                     92: 
                     93:        do {
                     94:                *(--pos)=(char)(n % base) + '0';
                     95:                n/=base;
                     96:        } while (n > 0);
                     97: 
                     98:        return pa_strdup(pos, buf + MAX_NUMBER - pos);
                     99: }
                    100: 
                    101: // forwards
                    102: class String;
                    103: 
                    104: // sign and whitespace are allowed
                    105: double pa_atod(const char* str, const String* problem_source);
                    106: int pa_atoi(const char* str, int base=10, const String* problem_source=0);
                    107: pa_wint pa_atowi(const char* str, int base=10, const String* problem_source=0);
                    108: // no sign or whitespace
                    109: unsigned int pa_atoui(const char *str, int base=10, const String* problem_source=0);
                    110: uint64_t pa_atoul(const char *str, int base=10, const String* problem_source=0);
                    111: 
1.2       moko      112: const char* format_double(double value, const char *fmt);
                    113: 
1.1       moko      114: #endif

E-mail: