|
|
| version 1.96, 2001/07/24 15:43:56 | version 1.111, 2001/10/17 15:44:47 |
|---|---|
| Line 2 | Line 2 |
| Parser: string class. @see untasize_t.C. | Parser: string class. @see untasize_t.C. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) |
| $Id$ | |
| */ | */ |
| static const char *RCSId="$Id$"; | |
| #include "pa_config_includes.h" | #include "pa_config_includes.h" |
| Line 20 static const char *RCSId="$Id$"; | Line 20 static const char *RCSId="$Id$"; |
| #include "pa_array.h" | #include "pa_array.h" |
| #include "pa_globals.h" | #include "pa_globals.h" |
| #include "pa_table.h" | #include "pa_table.h" |
| #include "pa_dictionary.h" | |
| String::String(Pool& apool, const char *src, size_t src_size, bool tainted) : | String::String(Pool& apool, const char *src, size_t src_size, bool tainted) : |
| Pooled(apool), | Pooled(apool), |
| Line 46 String::String(const String& src) : | Line 47 String::String(const String& src) : |
| size_t src_used_rows=src.fused_rows; | size_t src_used_rows=src.fused_rows; |
| if(src_used_rows<=head.count) { | if(src_used_rows<=head.count) { |
| // all new rows fit size_to preallocated area | // all new rows fit size_to preallocated area |
| last_chunk=&head; | |
| size_t curr_chunk_rows=head.count; | size_t curr_chunk_rows=head.count; |
| memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows); | memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows); |
| append_here=&head.rows[src_used_rows]; | append_here=&head.rows[src_used_rows]; |
| Line 64 String::String(const String& src) : | Line 66 String::String(const String& src) : |
| memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count); | memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count); |
| // remaining rows size_to new_chunk | // remaining rows size_to new_chunk |
| size_t curr_chunk_rows=src_used_rows-head.count; | size_t curr_chunk_rows=src_used_rows-head.count; |
| Chunk *new_chunk=static_cast<Chunk *>( | last_chunk=static_cast<Chunk *>( |
| malloc(sizeof(size_t)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *))); | malloc(sizeof(size_t)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *))); |
| new_chunk->count=curr_chunk_rows; | last_chunk->count=curr_chunk_rows; |
| head.preallocated_link=new_chunk; | head.preallocated_link=last_chunk; |
| append_here=link_row=&new_chunk->rows[new_chunk->count]; | append_here=link_row=&last_chunk->rows[last_chunk->count]; |
| Chunk *old_chunk=src.head.preallocated_link; | Chunk *old_chunk=src.head.preallocated_link; |
| Chunk::Row *new_rows=new_chunk->rows; | Chunk::Row *new_rows=last_chunk->rows; |
| size_t rows_left_to_copy=new_chunk->count; | size_t rows_left_to_copy=last_chunk->count; |
| while(true) { | while(true) { |
| size_t old_count=old_chunk->count; | size_t old_count=old_chunk->count; |
| Chunk *next_chunk=old_chunk->rows[old_count].link; | Chunk *next_chunk=old_chunk->rows[old_count].link; |
| Line 149 String& String::real_append(STRING_APPEN | Line 151 String& String::real_append(STRING_APPEN |
| return *this; | return *this; |
| } | } |
| char String::first_char() const { | |
| if(!fused_rows) | |
| THROW(0, 0, | |
| this, | |
| "getting first char of empty string"); | |
| return *head.rows[0].item.ptr; | |
| } | |
| uint String::hash_code() const { | uint String::hash_code() const { |
| uint result=0; | uint result=0; |
| Line 346 const Origin& String::origin() const { | Line 357 const Origin& String::origin() const { |
| #endif | #endif |
| String& String::mid(size_t start, size_t finish) const { | String& String::mid(size_t start, size_t finish) const { |
| String& result=*NEW String(pool()); | |
| start=max(0, start); | start=max(0, start); |
| finish=min(size(), finish); | finish=min(size(), finish); |
| if(start==finish) | if(start==finish) |
| return *empty_string; | return result; |
| String& result=*NEW String(pool()); | |
| size_t pos=0; | size_t pos=0; |
| const Chunk *chunk=&head; | const Chunk *chunk=&head; |
| Line 517 bool String::match(const unsigned char * | Line 528 bool String::match(const unsigned char * |
| int info_substrings=pcre_info(code, 0, 0); | int info_substrings=pcre_info(code, 0, 0); |
| if(info_substrings<0) { | if(info_substrings<0) { |
| (*pcre_free)(code); | pcre_free(code); |
| THROW(0, 0, | THROW(0, 0, |
| aorigin, | aorigin, |
| "pcre_info error (%d)", | "pcre_info error (%d)", |
| Line 551 bool String::match(const unsigned char * | Line 562 bool String::match(const unsigned char * |
| exec_option_bits, ovector, ovecsize); | exec_option_bits, ovector, ovecsize); |
| if(exec_substrings==PCRE_ERROR_NOMATCH) { | if(exec_substrings==PCRE_ERROR_NOMATCH) { |
| (*pcre_free)(code); | pcre_free(code); |
| (*row_action)(**table, 0/*last time, no row*/, 0, 0, info); | row_action(**table, 0/*last time, no row*/, 0, 0, info); |
| return option_bits[1]!=0; // global=true+table, not global=false | return option_bits[1]!=0; // global=true+table, not global=false |
| } | } |
| if(exec_substrings<0) { | if(exec_substrings<0) { |
| (*pcre_free)(code); | pcre_free(code); |
| THROW(0, 0, | THROW(0, 0, |
| aorigin, | aorigin, |
| "regular expression execute error (%d)", | "regular expression execute error (%d)", |
| Line 574 bool String::match(const unsigned char * | Line 585 bool String::match(const unsigned char * |
| row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value | row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value |
| } | } |
| (*row_action)(**table, &row, startoffset, ovector[0], info); | row_action(**table, &row, startoffset, ovector[0], info); |
| if(!option_bits[1] || !(startoffset=ovector[1])) { // not global | going to hang | if(!option_bits[1] || startoffset==ovector[1]) { // not global | going to hang |
| (*pcre_free)(code); | pcre_free(code); |
| (*row_action)(**table, 0/*last time, no row*/, 0, 0, info); | row_action(**table, 0/*last time, no row*/, 0, 0, info); |
| return true; | return true; |
| } | } |
| startoffset=ovector[1]; | |
| /* | /* |
| if(option_bits[0] & PCRE_MULTILINE) | if(option_bits[0] & PCRE_MULTILINE) |
| Line 642 break2: | Line 654 break2: |
| return result; | return result; |
| } | } |
| void String::join_chain(Pool& pool, | |
| size_t& ai, const Chunk*& achunk, const Chunk::Row*& arow, | |
| Untaint_lang& joined_lang, const char *& joined_ptr, size_t& joined_size) const { | |
| joined_lang=arow->item.lang; | |
| // calc size | |
| joined_size=0; | |
| { | |
| size_t start_i=ai; | |
| const Chunk::Row *start_row=arow; | |
| const Chunk *chunk=achunk; | |
| do { | |
| const Chunk::Row *row=start_row; | |
| for(size_t i=start_i; i<chunk->count; i++, row++) { | |
| if(row==append_here) | |
| goto break21; | |
| if(row->item.lang==joined_lang) | |
| joined_size+=row->item.size; | |
| else | |
| break; | |
| } | |
| if(chunk=row->link) { | |
| start_i=0; | |
| start_row=chunk->rows; | |
| } else | |
| break; | |
| } while(true); | |
| break21:; | |
| } | |
| // if one row, return simply itself | |
| if(joined_size==arow->item.size) { | |
| joined_ptr=arow->item.ptr; | |
| ai++; arow++; | |
| if(ai==achunk->count) | |
| achunk=arow->link; | |
| } else { | |
| // join adjacent rows | |
| char *ptr=(char *)pool.malloc(joined_size); | |
| joined_ptr=ptr; | |
| size_t start_i=ai; | |
| const Chunk::Row *start_row=arow; | |
| const Chunk *chunk=achunk; | |
| size_t i; | |
| const Chunk::Row *row; | |
| do { | |
| row=start_row; | |
| for(i=start_i; i<chunk->count; i++, row++) { | |
| if(row==append_here) | |
| goto break22; | |
| if(row->item.lang==joined_lang) { | |
| memcpy(ptr, row->item.ptr, row->item.size); | |
| ptr+=row->item.size; | |
| } else | |
| break; | |
| } | |
| if(chunk=row->link) { | |
| start_i=0; | |
| start_row=chunk->rows; | |
| } else | |
| break; | |
| } while(true); | |
| break22:; | |
| // return joined rows | |
| ai=i; | |
| arow=row; | |
| achunk=chunk; | |
| } | |
| } | |
| String& String::reconstruct(Pool& pool) const { | |
| //_asm int 3; | |
| String& result=*new(pool) String(pool); | |
| const Chunk *chunk=&head; | |
| do { | |
| const Chunk::Row *row=chunk->rows; | |
| for(size_t i=0; i<chunk->count; ) { | |
| if(row==append_here) | |
| goto break2; | |
| Untaint_lang joined_lang; | |
| const char *joined_ptr; | |
| size_t joined_size; | |
| join_chain(pool, i, chunk, row, | |
| joined_lang, joined_ptr, joined_size); | |
| result.APPEND(joined_ptr, joined_size, | |
| joined_lang, | |
| row->item.origin.file, row->item.origin.line); | |
| if(!chunk) | |
| goto break2; | |
| } | |
| } while(true); | |
| break2: | |
| return result; | |
| }; | |
| String& String::replace_in_reconstructed(Pool& pool, Dictionary& dict) const { | |
| //_asm int 3; | |
| String& result=*new(pool) String(pool); | |
| const Chunk *chunk=&head; | |
| do { | |
| const Chunk::Row *row=chunk->rows; | |
| for(size_t i=0; i<chunk->count; i++, row++) { | |
| if(row==append_here) | |
| goto break2; | |
| const char *src=row->item.ptr; | |
| size_t src_size=row->item.size; | |
| char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio())); | |
| char *dest=new_cstr; | |
| while(src_size) { | |
| // there is a row where first column starts 'src' | |
| if(Table::Item *item=dict.first_that_starts(src, src_size)) { | |
| // get a=>b values | |
| const String& a=*static_cast<Array *>(item)->get_string(0); | |
| const String& b=*static_cast<Array *>(item)->get_string(1); | |
| // skip 'a' in 'src' && reduce work size | |
| src+=a.size(); src_size-=a.size(); | |
| // write 'b' to 'dest' && skip 'b' in 'dest' | |
| b.store_to(dest); dest+=b.size(); | |
| } else { | |
| // write a char to b && reduce work size | |
| *dest++=*src++; src_size--; | |
| } | |
| } | |
| result.APPEND(new_cstr, dest-new_cstr, | |
| row->item.lang, | |
| row->item.origin.file, row->item.origin.line); | |
| } | |
| chunk=row->link; | |
| } while(chunk); | |
| break2: | |
| return result; | |
| } | |
| String& String::replace(Pool& pool, Dictionary& dict) const { | |
| return reconstruct(pool).replace_in_reconstructed(pool, dict); | |
| } | |
| double String::as_double() const { | double String::as_double() const { |
| double result; | double result; |
| const char *cstr=this->cstr(); | const char *cstr=this->cstr(); |
| char *error_pos=0; | char *error_pos; |
| // 0xABC | // 0xABC |
| if(cstr[0]=='0' && (cstr[1]=='x' || cstr[1]=='X')) | if(cstr[0]=='0') |
| result=(double)(unsigned long)strtol(cstr, &error_pos, 0); | if(cstr[1]=='x' || cstr[1]=='X') |
| result=(double)(unsigned long)strtol(cstr, &error_pos, 0); | |
| else | |
| result=(double)strtod(cstr+1/*skip leading 0*/, &error_pos); | |
| else | else |
| result=strtod(cstr, &error_pos); | result=(double)strtod(cstr, &error_pos); |
| if(error_pos && *error_pos) | if(*error_pos/*not EOS*/) |
| THROW(0, 0, | THROW(0, 0, |
| this, | this, |
| "invalid number (double)"); | "invalid number (double)"); |
| Line 662 double String::as_double() const { | Line 823 double String::as_double() const { |
| int String::as_int() const { | int String::as_int() const { |
| int result; | int result; |
| const char *cstr=this->cstr(); | const char *cstr=this->cstr(); |
| char *error_pos=0; | char *error_pos; |
| // 0xABC | // 0xABC |
| if(cstr[0]=='0' && (cstr[1]=='x' || cstr[1]=='X')) | if(cstr[0]=='0') |
| result=(int)(unsigned long)strtol(cstr, &error_pos, 0); | if(cstr[1]=='x' || cstr[1]=='X') |
| result=(int)(unsigned long)strtol(cstr, &error_pos, 0); | |
| else | |
| result=(int)strtol(cstr+1/*skip leading 0*/, &error_pos, 0); | |
| else | else |
| result=(int)strtol(cstr, &error_pos, 0); | result=(int)strtol(cstr, &error_pos, 0); |
| if(error_pos && *error_pos) | if(*error_pos/*not EOS*/) |
| THROW(0, 0, | THROW(0, 0, |
| this, | this, |
| "invalid number (int)"); | "invalid number (int)"); |