|
|
| version 1.179, 2003/09/26 09:06:21 | version 1.197, 2004/04/02 13:57:07 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: string class. @see untalength_t.C. | Parser: string class. @see untalength_t.C. |
| Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| static const char* IDENT_STRING_C="$Date$"; | static const char * const IDENT_STRING_C="$Date$"; |
| #include "pcre.h" | #include "pcre.h" |
| Line 15 static const char* IDENT_STRING_C="$Date | Line 15 static const char* IDENT_STRING_C="$Date |
| #include "pa_dictionary.h" | #include "pa_dictionary.h" |
| #include "pa_charset.h" | #include "pa_charset.h" |
| const String String::Empty; | |
| int pa_atoi(const char* str, const String* problem_source) { | |
| if(!str) | |
| return 0; | |
| while(*str && isspace(*str)) | |
| str++; | |
| if(!*str) | |
| return 0; | |
| int result; | |
| char *error_pos; | |
| // 0xABC | |
| if(str[0]=='0') | |
| if(str[1]=='x' || str[1]=='X') | |
| result=(int)(unsigned long)strtol(str, &error_pos, 0); | |
| else { | |
| // skip leading 0000, to disable octal interpretation | |
| do str++; while(*str=='0'); | |
| result=(int)strtol(str, &error_pos, 0); | |
| } | |
| else | |
| result=(int)strtol(str, &error_pos, 0); | |
| while(char c=*error_pos++) | |
| if(!isspace(c)) | |
| throw Exception("number.format", | |
| problem_source, | |
| problem_source?"invalid number (int)": "'%s' is invalid number (int)", str); | |
| return result; | |
| } | |
| double pa_atod(const char* str, const String* problem_source) { | |
| if(!str) | |
| return 0; | |
| while(*str && isspace(*str)) | |
| str++; | |
| if(!*str) | |
| return 0; | |
| double result; | |
| char *error_pos; | |
| // 0xABC | |
| if(str[0]=='0') | |
| if(str[1]=='x' || str[1]=='X') | |
| result=(double)(unsigned long)strtol(str, &error_pos, 0); | |
| else | |
| result=(double)strtod(str+1/*skip leading 0*/, &error_pos); | |
| else | |
| result=(double)strtod(str, &error_pos); | |
| while(char c=*error_pos++) | |
| if(!isspace(c)) | |
| throw Exception("number.format", | |
| problem_source, | |
| problem_source?"invalid number (double)": "'%s' is invalid number (double)", str); | |
| return result; | |
| } | |
| // cord lib extension | // cord lib extension |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| Line 41 int CORD_range_contains_chr_greater_then | Line 104 int CORD_range_contains_chr_greater_then |
| return(CORD_block_iter(x, i, CORD_range_contains_chr_greater_then_proc, &d) == 1/*alternatives: 0 normally ended, 2=struck 'n'*/); | return(CORD_block_iter(x, i, CORD_range_contains_chr_greater_then_proc, &d) == 1/*alternatives: 0 normally ended, 2=struck 'n'*/); |
| } | } |
| static int CORD_block_count_proc(char c, size_t size, void* client_data) | static int CORD_block_count_proc(char /*c*/, size_t /*size*/, void* client_data) |
| { | { |
| int* result=(int*)client_data; | int* result=(int*)client_data; |
| (*result)++; | (*result)++; |
| Line 82 String::Body String::Body::Format(int va | Line 145 String::Body String::Body::Format(int va |
| return String::Body(pa_strdup(local, length), length); | return String::Body(pa_strdup(local, length), length); |
| } | } |
| String::Body String::Body::trim(String::Trim_kind kind, const char* chars, | |
| size_t* out_start, size_t* out_length) const { | |
| size_t our_length=length(); | |
| if(!our_length) | |
| return *this; | |
| if(!chars) | |
| chars=" \t\n"; // white space | |
| Body result=*this; | |
| size_t start=0; | |
| size_t end=our_length; | |
| // from left... | |
| if(kind!=TRIM_END) { | |
| CORD_pos pos; set_pos(pos, 0); | |
| while(true) { | |
| char c=CORD_pos_fetch(pos); | |
| if(strchr(chars, c)) { | |
| if(++start==our_length) | |
| return 0; // all chars are empty, just return empty string | |
| } else | |
| break; | |
| CORD_next(pos); | |
| } | |
| } | |
| // from right.. | |
| if(kind!=TRIM_START) { | |
| CORD_pos pos; set_pos(pos, end-1); | |
| while(true) { | |
| char c=CORD_pos_fetch(pos); | |
| if(strchr(chars, c)) { | |
| if(--end==0) // optimization: NO need to check for 'end>=start', that's(<) impossible | |
| return 0; // all chars are empty, just return empty string | |
| } else | |
| break; | |
| CORD_prev(pos); | |
| } | |
| } | |
| if(start==0 && end==our_length) // nobody moved a thing | |
| return *this; | |
| if(out_start) | |
| *out_start=start; | |
| size_t new_length=end-start; | |
| if(out_length) | |
| *out_length=new_length; | |
| return mid(start, new_length); | |
| } | |
| static int CORD_batched_iter_fn_generic_hash_code(char c, void * client_data) { | static int CORD_batched_iter_fn_generic_hash_code(char c, void * client_data) { |
| uint& result=*static_cast<uint*>(client_data); | uint& result=*static_cast<uint*>(client_data); |
| generic_hash_code(result, c); | generic_hash_code(result, c); |
| Line 167 String& String::mid(size_t substr_begin, | Line 282 String& String::mid(size_t substr_begin, |
| } | } |
| size_t String::pos(const String::Body substr, size_t this_offset, Language lang) const { | size_t String::pos(const String::Body substr, size_t this_offset, Language lang) const { |
| size_t substr_begin=body.pos(substr, this_offset); | size_t substr_length=substr.length(); |
| if(substr_begin==CORD_NOT_FOUND || !langs.check_lang(lang, substr_begin, substr.length())) | while(true) { |
| return STRING_NOT_FOUND; | size_t substr_begin=body.pos(substr, this_offset); |
| if(substr_begin==CORD_NOT_FOUND) | |
| return STRING_NOT_FOUND; | |
| if(langs.check_lang(lang, substr_begin, substr_length)) | |
| return substr_begin; | |
| return substr_begin; | this_offset=substr_begin+substr_length; |
| } | |
| } | } |
| size_t String::pos(const String& substr, | size_t String::pos(const String& substr, |
| Line 185 void String::split(ArrayString& result, | Line 307 void String::split(ArrayString& result, |
| Language lang, int limit) const { | Language lang, int limit) const { |
| size_t self_length=length(); | size_t self_length=length(); |
| if(size_t delim_length=strlen(delim)) { | if(size_t delim_length=strlen(delim)) { |
| int pos_before; | size_t pos_before; |
| // while we have 'delim'... | // while we have 'delim'... |
| for(; (pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND && limit; limit--) { | for(; (pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND && limit; limit--) { |
| result+=&mid(pos_after, pos_before); | result+=&mid(pos_after, pos_before); |
| Line 207 void String::split(ArrayString& result, | Line 329 void String::split(ArrayString& result, |
| const String& delim, Language lang, | const String& delim, Language lang, |
| int limit) const { | int limit) const { |
| if(!delim.is_empty()) { | if(!delim.is_empty()) { |
| int pos_before; | size_t pos_before; |
| // while we have 'delim'... | // while we have 'delim'... |
| for(; (pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND && limit; limit--) { | for(; (pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND && limit; limit--) { |
| result+=&mid(pos_after, pos_before); | result+=&mid(pos_after, pos_before); |
| Line 232 static void regex_options(const String* | Line 354 static void regex_options(const String* |
| int *result; | int *result; |
| bool *flag; | bool *flag; |
| } regex_option[]={ | } regex_option[]={ |
| {"i", "I", 0, PCRE_CASELESS, result}, // a=A | {"i", "I", 0, PCRE_CASELESS, result, 0}, // a=A |
| {"s", "S", 0, PCRE_DOTALL, result}, // \n\n$ [default] | {"s", "S", 0, PCRE_DOTALL, result, 0}, // \n\n$ [default] |
| {"x", "U", 0, PCRE_EXTENDED, result}, // whitespace in regex ignored | {"x", "U", 0, PCRE_EXTENDED, result, 0}, // whitespace in regex ignored |
| {"m", "M", PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$ | {"m", "M", PCRE_DOTALL, PCRE_MULTILINE, result, 0}, // ^aaa\n$^bbb\n$ |
| {"g", "G", 0, true, result+1}, // many rows | {"g", "G", 0, 1, result+1, 0}, // many rows |
| {"'", 0, 0, 0, 0, &need_pre_post_match}, | {"'", 0, 0, 0, 0, &need_pre_post_match}, |
| {0} | {0, 0, 0, 0, 0, 0} |
| }; | }; |
| result[0]=PCRE_EXTRA | PCRE_DOTALL | PCRE_DOLLAR_ENDONLY; | result[0]=PCRE_EXTRA | PCRE_DOTALL | PCRE_DOLLAR_ENDONLY; |
| result[1]=0; | result[1]=0; |
| Line 334 Table* String::match(Charset& source_cha | Line 456 Table* String::match(Charset& source_cha |
| *row+=&mid(prefinish, poststart); // .match | *row+=&mid(prefinish, poststart); // .match |
| *row+=&mid(poststart, postfinish); // .postmatch | *row+=&mid(poststart, postfinish); // .postmatch |
| } else { | } else { |
| *row+=0; // .prematch column value | *row+=&Empty; // .prematch column value |
| *row+=0; // .match | *row+=&Empty; // .match |
| *row+=0; // .postmatch | *row+=&Empty; // .postmatch |
| } | } |
| for(int i=1; i<exec_substrings; i++) { | for(int i=1; i<exec_substrings; i++) { |
| Line 365 String& String::change_case(Charset& sou | Line 487 String& String::change_case(Charset& sou |
| if(is_empty()) | if(is_empty()) |
| return result; | return result; |
| const unsigned char *tables=source_charset.pcre_tables; | |
| const unsigned char *a; | |
| const unsigned char *b; | |
| switch(kind) { | |
| case CC_UPPER: | |
| a=tables+lcc_offset; | |
| b=tables+fcc_offset; | |
| break; | |
| case CC_LOWER: | |
| a=tables+lcc_offset; | |
| b=0; | |
| break; | |
| default: | |
| throw Exception(0, | |
| this, | |
| "unknown change case kind #%d", | |
| static_cast<int>(kind)); // never | |
| a=b=0; // calm, compiler | |
| break; // never | |
| } | |
| char* new_cstr=cstrm(); | char* new_cstr=cstrm(); |
| char *dest=new_cstr; | size_t new_cstr_len=length(); |
| unsigned char index; | if(source_charset.isUTF8()) { |
| for(const char* current=new_cstr; index=(unsigned char)*current; current++) { | switch(kind) { |
| unsigned char c=a[index]; | case CC_UPPER: |
| if(b) | change_case_UTF8((const XMLByte*)new_cstr, new_cstr_len, (XMLByte*)new_cstr, new_cstr_len, UTF8CaseToUpper); |
| c=b[c]; | break; |
| case CC_LOWER: | |
| change_case_UTF8((const XMLByte*)new_cstr, new_cstr_len, (XMLByte*)new_cstr, new_cstr_len, UTF8CaseToLower); | |
| break; | |
| default: | |
| assert(!"unknown change case kind"); | |
| break; // never | |
| } | |
| } else { | |
| const unsigned char *tables=source_charset.pcre_tables; | |
| const unsigned char *a; | |
| const unsigned char *b; | |
| switch(kind) { | |
| case CC_UPPER: | |
| a=tables+lcc_offset; | |
| b=tables+fcc_offset; | |
| break; | |
| case CC_LOWER: | |
| a=tables+lcc_offset; | |
| b=0; | |
| break; | |
| default: | |
| assert(!"unknown change case kind"); | |
| a=b=0; // calm, compiler | |
| break; // never | |
| } | |
| char *dest=new_cstr; | |
| unsigned char index; | |
| for(const char* current=new_cstr; (index=(unsigned char)*current); current++) { | |
| unsigned char c=a[index]; | |
| if(b) | |
| c=b[c]; | |
| *dest++=(char)c; | *dest++=(char)c; |
| } | |
| } | } |
| result.langs=langs; | result.langs=langs; |
| result.body=new_cstr; | result.body=new_cstr; |
| Line 410 const String& String::replace(const Dict | Line 545 const String& String::replace(const Dict |
| const char* current=old_cstr; | const char* current=old_cstr; |
| while(*current) { | while(*current) { |
| if(Table::element_type row=dict.first_that_begins(current)) { | if(Dictionary::Subst subst=dict.first_that_begins(current)) { |
| // prematch | // prematch |
| if(size_t prematch_length=current-prematch_begin) { | if(size_t prematch_length=current-prematch_begin) { |
| result.langs.append(result.body, langs, prematch_begin-old_cstr, prematch_length); | result.langs.append(result.body, langs, prematch_begin-old_cstr, prematch_length); |
| Line 418 const String& String::replace(const Dict | Line 553 const String& String::replace(const Dict |
| } | } |
| // match | // match |
| const String* a=row->get(0); | |
| // skip 'a' in 'current'; move prematch_begin | // skip 'a' in 'current'; move prematch_begin |
| current+=a->length(); prematch_begin=current; | current+=subst.from_length; prematch_begin=current; |
| if(row->count()>1) { // are there any b? | if(const String* b=subst.to) // are there any b? |
| const String* b=row->get(1); | |
| result<<*b; | result<<*b; |
| } | |
| } else // simply advance | } else // simply advance |
| current++; | current++; |
| } | } |
| Line 441 const String& String::replace(const Dict | Line 572 const String& String::replace(const Dict |
| return result; | return result; |
| } | } |
| double String::as_double() const { | static int serialize_body_char(char c, char** cur) { |
| double result; | *((*cur)++)=c; |
| const char *str=cstr(); | return 0; // 0=continue |
| }; | |
| while(*str && isspace(*str)) | |
| str++; | |
| if(!*str) | |
| return 0; | |
| char *error_pos; | |
| // 0xABC | |
| if(str[0]=='0') | |
| if(str[1]=='x' || str[1]=='X') | |
| result=(double)(unsigned long)strtol(str, &error_pos, 0); | |
| else | |
| result=(double)strtod(str+1/*skip leading 0*/, &error_pos); | |
| else | |
| result=(double)strtod(str, &error_pos); | |
| while(char c=*error_pos++) | |
| if(!isspace(c)) | |
| throw Exception("number.format", | |
| this, | |
| "invalid number (double)"); | |
| return result; | |
| } | |
| int String::as_int() const { | |
| int result; | |
| const char *str=cstr(); | |
| while(*str && isspace(*str)) | |
| str++; | |
| if(!*str) | |
| return 0; | |
| char *error_pos; | |
| // 0xABC | |
| if(str[0]=='0') | |
| if(str[1]=='x' || str[1]=='X') | |
| result=(int)(unsigned long)strtol(str, &error_pos, 0); | |
| else | |
| result=(int)strtol(str+1/*skip leading 0*/, &error_pos, 0); | |
| else | |
| result=(int)strtol(str, &error_pos, 0); | |
| while(char c=*error_pos++) | |
| if(!isspace(c)) | |
| throw Exception("number.format", | |
| this, | |
| "invalid number (int)"); | |
| return result; | |
| } | |
| static int serialize_body_piece(const char* s, char** cur) { | static int serialize_body_piece(const char* s, char** cur) { |
| size_t length=strlen(s); | size_t length=strlen(s); |
| memcpy(*cur, s, length); *cur+=length; | memcpy(*cur, s, length); *cur+=length; |
| Line 503 static int serialize_body_piece(const ch | Line 583 static int serialize_body_piece(const ch |
| }; | }; |
| static int serialize_lang_piece(char alang, size_t asize, char** cur) { | static int serialize_lang_piece(char alang, size_t asize, char** cur) { |
| // lang | // lang |
| memcpy(*cur, &alang, sizeof(alang)); *cur+=sizeof(alang); | **cur=alang; (*cur)++; |
| // length | // length [WARNING: not cast, addresses must be %4=0 on sparc] |
| memcpy(*cur, &asize, sizeof(asize)); *cur+=sizeof(asize); | memcpy(*cur, &asize, sizeof(asize)); *cur+=sizeof(asize); |
| return 0; // 0=continue | return 0; // 0=continue |
| Line 521 String::Cm String::serialize(size_t prol | Line 601 String::Cm String::serialize(size_t prol |
| // 1: prolog | // 1: prolog |
| char *cur=result.str+prolog_length; | char *cur=result.str+prolog_length; |
| // 2: langs.count | // 2: langs.count [WARNING: not cast, addresses must be %4=0 on sparc] |
| memcpy(cur, &fragments_count, sizeof(fragments_count)); cur+=sizeof(fragments_count); | memcpy(cur, &fragments_count, sizeof(fragments_count)); cur+=sizeof(fragments_count); |
| // 3: lang info | // 3: lang info |
| langs.for_each(body, serialize_lang_piece, &cur); | langs.for_each(body, serialize_lang_piece, &cur); |
| // 4: letters | // 4: letters |
| body.for_each(serialize_body_piece, &cur); | body.for_each(serialize_body_char, serialize_body_piece, &cur); |
| // 5: zero terminator already there put by new(PointerFreeGC) | // 5: zero terminator |
| *cur=0; | |
| return result; | return result; |
| } | } |
| Line 541 bool String::deserialize(size_t prolog_l | Line 622 bool String::deserialize(size_t prolog_l |
| const char* cur=(const char* )buf+prolog_length; | const char* cur=(const char* )buf+prolog_length; |
| // 2: langs.count | // 2: langs.count |
| if(buf_length<sizeof(size_t)) // langs.count don't fit? | size_t fragments_count; |
| if(buf_length<sizeof(fragments_count)) // langs.count don't fit? | |
| return false; | return false; |
| size_t fragments_count=*reinterpret_cast<const size_t*>(cur); cur+=sizeof(size_t); | // [WARNING: not cast, addresses must be %4=0 on sparc] |
| buf_length-=sizeof(size_t); | memcpy(&fragments_count, cur, sizeof(fragments_count)); cur+=sizeof(fragments_count); |
| buf_length-=sizeof(fragments_count); | |
| if(fragments_count) { | if(fragments_count) { |
| // 3: lang info | // 3: lang info |
| size_t total_length=0; | size_t total_length=0; |
| for(size_t f=0; f<fragments_count; f++) { | for(size_t f=0; f<fragments_count; f++) { |
| size_t piece_length=sizeof(char)+sizeof(size_t); | char lang; |
| size_t fragment_length; | |
| size_t piece_length=sizeof(lang)+sizeof(fragment_length); | |
| if(buf_length<piece_length) // lang+length | if(buf_length<piece_length) // lang+length |
| return false; | return false; |
| Language lang=*reinterpret_cast<const Language *>(cur); cur+=sizeof(char); | // lang |
| size_t fragment_length=*reinterpret_cast<const size_t*>(cur); cur+=sizeof(size_t); | lang=*cur++; |
| langs.append(total_length, lang, fragment_length); | // length [WARNING: not cast, addresses must be %4=0 on sparc] |
| memcpy(&fragment_length, cur, sizeof(fragment_length)); cur+=sizeof(fragment_length); | |
| // uchar needed to prevent propagating 0x80 bit to upper bytes | |
| langs.append(total_length, (String::Language)(uchar)lang, fragment_length); | |
| total_length+=fragment_length; | total_length+=fragment_length; |
| buf_length-=piece_length; | buf_length-=piece_length; |
| Line 584 const char* String::Languages::v() const | Line 673 const char* String::Languages::v() const |
| return (const char*)&langs; | return (const char*)&langs; |
| } | } |
| const char* String::v() const { | const char* String::v() const { |
| #define LIMIT_VIEW 20 | const int LIMIT_VIEW=20; |
| char* buf=(char*)malloc(MAX_STRING); | char* buf=(char*)malloc(MAX_STRING); |
| const char*body_view=body.v(); | const char*body_view=body.v(); |
| const char*langs_view=langs.v(); | const char*langs_view=langs.v(); |
| Line 596 const char* String::v() const { | Line 685 const char* String::v() const { |
| ); | ); |
| return buf; | return buf; |
| #undef LIMIT_VIEW | |
| } | } |
| const String& String::trim(String::Trim_kind kind, const char* chars) const { | |
| if(!length()) | |
| return *this; | |
| size_t substr_begin, substr_length; | |
| Body new_body=body.trim(kind, chars, &substr_begin, &substr_length); | |
| if(new_body==body) // we received unchanged pointer, do likewise | |
| return *this; | |
| // new_body differs from body, adjust langs along | |
| String& result=*new String; | |
| if(!new_body) // body.trim produced empty result | |
| return result; | |
| // body.trim produced nonempty result | |
| // first: their langs | |
| result.langs.append(result.body, langs, substr_begin, substr_length); | |
| // next: letters themselves | |
| result.body=new_body; | |
| ASSERT_STRING_INVARIANT(result); | |
| return result; | |
| } | |