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

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: 
        !            11: #define IDENT_PA_INT_H "$Id: pa_int.h,v 1.1 2025/08/01 16:14:44 moko Exp $"
        !            12: 
        !            13: // includes
        !            14: 
        !            15: #include "pa_memory.h"
        !            16: #include "pa_types.h"
        !            17: 
        !            18: //#define PA_WIDE_INT
        !            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;
        !            25: #else
        !            26: #define PA_WINT_MIN INT_MIN
        !            27: #define PA_WINT_MAX INT_MAX
        !            28: typedef int pa_wint;
        !            29: typedef uint pa_uwint;
        !            30: #endif
        !            31: 
        !            32: inline int clip2int(double value) {
        !            33:        return value <= INT_MIN ? INT_MIN : ( value >= INT_MAX ? INT_MAX : (int)value );
        !            34: }
        !            35: 
        !            36: inline uint clip2uint(double value) {
        !            37:        return value <= 0 ? 0 : ( value >= UINT_MAX ? UINT_MAX : (uint)value );
        !            38: }
        !            39: 
        !            40: 
        !            41: /// Commonly used, templated to work with any integer type
        !            42: 
        !            43: template<typename T> char* pa_itoa(T n, T base=10){
        !            44:        char buf[MAX_NUMBER + 1];
        !            45:        char* pos=buf + MAX_NUMBER;
        !            46:        *pos='\0';
        !            47: 
        !            48:        bool negative=n < 0;
        !            49:        if (n < 0){
        !            50:                n=-n;
        !            51:        }
        !            52: 
        !            53:        do {
        !            54:                *(--pos)=(char)(n % base) + '0';
        !            55:                n/=base;
        !            56:        } while (n > 0);
        !            57: 
        !            58:        if (negative) {
        !            59:                *(--pos) = '-';
        !            60:        }
        !            61:        return pa_strdup(pos, buf + MAX_NUMBER - pos);
        !            62: }
        !            63: 
        !            64: static char* pa_itoa(int i, int base=10){ // custom to support INT_MIN
        !            65:        char buf[MAX_NUMBER + 1];
        !            66:        char* pos=buf + MAX_NUMBER;
        !            67:        *pos='\0';
        !            68: 
        !            69:        bool negative=i < 0;
        !            70:        unsigned int n= i < 0 ? -i : i;
        !            71: 
        !            72:        do {
        !            73:                *(--pos)=(char)(n % base) + '0';
        !            74:                n/=base;
        !            75:        } while (n > 0);
        !            76: 
        !            77:        if (negative) {
        !            78:                *(--pos) = '-';
        !            79:        }
        !            80:        return pa_strdup(pos, buf + MAX_NUMBER - pos);
        !            81: }
        !            82: 
        !            83: template<typename T> char* pa_uitoa(T n, T base=10){
        !            84:        char buf[MAX_NUMBER + 1];
        !            85:        char* pos=buf + MAX_NUMBER;
        !            86:        *pos='\0';
        !            87: 
        !            88:        do {
        !            89:                *(--pos)=(char)(n % base) + '0';
        !            90:                n/=base;
        !            91:        } while (n > 0);
        !            92: 
        !            93:        return pa_strdup(pos, buf + MAX_NUMBER - pos);
        !            94: }
        !            95: 
        !            96: // forwards
        !            97: class String;
        !            98: 
        !            99: // sign and whitespace are allowed
        !           100: double pa_atod(const char* str, const String* problem_source);
        !           101: int pa_atoi(const char* str, int base=10, const String* problem_source=0);
        !           102: pa_wint pa_atowi(const char* str, int base=10, const String* problem_source=0);
        !           103: // no sign or whitespace
        !           104: unsigned int pa_atoui(const char *str, int base=10, const String* problem_source=0);
        !           105: uint64_t pa_atoul(const char *str, int base=10, const String* problem_source=0);
        !           106: 
        !           107: #endif

E-mail: