--- parser3/src/main/pa_string.C 2020/10/10 06:08:37 1.263 +++ parser3/src/main/pa_string.C 2025/05/25 17:44:27 1.282 @@ -1,8 +1,8 @@ /** @file Parser: string class. @see untalength_t.C. - Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #include "pa_string.h" @@ -12,7 +12,7 @@ #include "pa_charset.h" #include "pa_vregex.h" -volatile const char * IDENT_PA_STRING_C="$Id: pa_string.C,v 1.263 2020/10/10 06:08:37 moko Exp $" IDENT_PA_STRING_H; +volatile const char * IDENT_PA_STRING_C="$Id: pa_string.C,v 1.282 2025/05/25 17:44:27 moko Exp $" IDENT_PA_STRING_H; const String String::Empty; @@ -46,9 +46,10 @@ template inline T pa_ato_any } } - if (base < 2 || base > 16) { /* illegal base */ + if (base < 2 || base > 16) /* illegal base */ throw Exception(PARSER_RUNTIME, 0, "base to must be an integer from 2 to 16"); - } + if (*pos == '-') + throw Exception("number.format", problem_source, problem_source ? "out of range (negative)" : "'%s' is out if range (negative)", str); T cutoff = max / base; int cutoff_digit = (int)(max - cutoff * base); @@ -80,7 +81,7 @@ template inline T pa_ato_any while(char c=*pos++) if(!isspace(c)) - throw Exception("number.format", problem_source, problem_source ? "invalid number (int)" : "'%s' is invalid number (int)", str); + throw Exception("number.format", problem_source, problem_source ? "invalid number (int)" : "'%s' is an invalid number (int)", str); return result; } @@ -92,14 +93,14 @@ unsigned int pa_atoui(const char *str, i return pa_ato_any(str, base, problem_source, UINT_MAX); } -unsigned long long pa_atoul(const char *str, int base, const String* problem_source){ +uint64_t pa_atoul(const char *str, int base, const String* problem_source){ if(!str) return 0; - return pa_ato_any(str, base, problem_source, ULLONG_MAX); + return pa_ato_any(str, base, problem_source, ULLONG_MAX); } -int pa_atoi(const char* str, const String* problem_source) { +int pa_atoi(const char* str, int base, const String* problem_source) { if(!str) return 0; @@ -109,26 +110,31 @@ int pa_atoi(const char* str, const Strin if(!*str) return 0; + const char *str_copy=str; bool negative=false; if(str[0]=='-') { negative=true; str++; + if(!*str || isspace(*str)) + throw Exception("number.format", problem_source, problem_source ? "invalid number (int)" : "'%s' is an invalid number (int)", str_copy); } else if(str[0]=='+') { str++; + if(!*str || isspace(*str)) + throw Exception("number.format", problem_source, problem_source ? "invalid number (int)" : "'%s' is an invalid number (int)", str_copy); } - unsigned int result=pa_atoui(str, 0, problem_source); + unsigned int result=pa_atoui(str, base, problem_source); if(negative && result <= ((unsigned int)(-(1+INT_MIN)))+1) return -(int)result; if(result<=INT_MAX) return (int)result; - - throw Exception("number.format", problem_source, problem_source ? "out of range (int)" : "'%s' is out of range (int)", str); + + throw Exception("number.format", problem_source, problem_source ? "out of range (int)" : "'%s' is out of range (int)", str_copy); } -double pa_atod(const char* str, const String* problem_source) { +double pa_atod(const char* str, const String* problem_source /* never null */) { if(!str) return 0; @@ -142,8 +148,12 @@ double pa_atod(const char* str, const St if(str[0]=='-') { negative=true; str++; + if(!*str || isspace(*str)) + throw Exception("number.format", problem_source, "invalid number (double)"); } else if(str[0]=='+') { str++; + if(!*str || isspace(*str)) + throw Exception("number.format", problem_source, "invalid number (double)"); } double result; @@ -161,9 +171,9 @@ double pa_atod(const char* str, const St char *error_pos; result=strtod(str, &error_pos); - while(char c=*error_pos++) - if(!isspace((unsigned char)c)) - throw Exception("number.format", problem_source, problem_source ? "invalid number (double)" : "'%s' is invalid number (double)", str); + while(const char c=*error_pos++) + if(!isspace(c)) + throw Exception("number.format", problem_source, "invalid number (double)"); return negative ? -result : result; } @@ -179,7 +189,7 @@ typedef struct { static int CORD_range_contains_chr_greater_then_proc(char c, size_t size, void* client_data) { - register chr_data * d = (chr_data *)client_data; + chr_data * d = (chr_data *)client_data; if (d -> countdown<=0) return(2); d -> countdown -= size; @@ -223,19 +233,27 @@ public: *this+=new String("match"); *this+=new String("postmatch"); for(int i=0; i=start', that's(<) impossible return 0; // all chars are empty, just return empty string } else - break; + break; CORD_prev(pos); } @@ -652,15 +670,16 @@ Table* String::match(VRegex* vregex, Row const char* subject=cstr(); size_t subject_length=length(); - const int ovector_size=(1/*match*/+MAX_MATCH_GROUPS)*3; + const int ovector_size=(1/*match*/+MAX_MATCH_GROUPS)*3; /* 1/3 is used as workspace by pcre_exec() */ int ovector[ovector_size]; Table::Action_options table_options; - Table& table=*new Table(string_match_table_template, table_options); + Table& table=*new Table(string_match_table_template(), table_options); int prestart=0; int poststart=0; int postfinish=length(); + int action_was_executed=-1; while(true) { int exec_result=vregex->exec(subject, subject_length, ovector, ovector_size, prestart); @@ -670,8 +689,9 @@ Table* String::match(VRegex* vregex, Row int prefinish=ovector[0]; poststart=ovector[1]; - if (prestart==poststart && subject[poststart]=='\n'){ + if (prestart==poststart && action_was_executed==1){ prestart++; + action_was_executed=0; continue; } @@ -692,12 +712,13 @@ Table* String::match(VRegex* vregex, Row } matches_count++; - row_action(table, row, prestart, prefinish, poststart, postfinish, info); + row_action(table, row, prestart - !action_was_executed, prefinish, poststart, postfinish, info); - if(!global || prestart==poststart) // last step + if(!global || (size_t)poststart>=subject_length) // last step, avoid prestart++ after last char break; prestart=poststart; + action_was_executed=1; } row_action(table, 0/*last time, no raw*/, 0, 0, poststart, postfinish, info);