|
|
| version 1.110, 2001/10/16 14:49:16 | version 1.116, 2001/10/29 16:29:08 |
|---|---|
| Line 30 String::String(Pool& apool, const char * | Line 30 String::String(Pool& apool, const char * |
| append_here=head.rows; | append_here=head.rows; |
| head.preallocated_link=0; | head.preallocated_link=0; |
| link_row=&head.rows[head.count]; | link_row=&head.rows[head.count]; |
| fused_rows=fsize=0; | fsize=0; |
| if(src) | if(src) |
| if(tainted) | if(tainted) |
| Line 44 String::String(const String& src) : | Line 44 String::String(const String& src) : |
| forigins_mode(false) { | forigins_mode(false) { |
| head.count=CR_PREALLOCATED_COUNT; | head.count=CR_PREALLOCATED_COUNT; |
| size_t src_used_rows=src.fused_rows; | uint src_used_rows=src.used_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; | last_chunk=&head; |
| size_t curr_chunk_rows=head.count; | uint 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]; |
| link_row=&head.rows[curr_chunk_rows]; | link_row=&head.rows[curr_chunk_rows]; |
| Line 65 String::String(const String& src) : | Line 65 String::String(const String& src) : |
| // preallocated chunk src to constructing head | // preallocated chunk src to constructing head |
| 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; | uint curr_chunk_rows=src_used_rows-head.count; |
| last_chunk=static_cast<Chunk *>( | last_chunk=static_cast<Chunk *>( |
| malloc(sizeof(size_t)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *))); | malloc(sizeof(uint)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *), 9)); |
| last_chunk->count=curr_chunk_rows; | last_chunk->count=curr_chunk_rows; |
| head.preallocated_link=last_chunk; | head.preallocated_link=last_chunk; |
| append_here=link_row=&last_chunk->rows[last_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=last_chunk->rows; | Chunk::Row *new_rows=last_chunk->rows; |
| size_t rows_left_to_copy=last_chunk->count; | uint rows_left_to_copy=last_chunk->count; |
| while(true) { | while(true) { |
| size_t old_count=old_chunk->count; | uint old_count=old_chunk->count; |
| Chunk *next_chunk=old_chunk->rows[old_count].link; | Chunk *next_chunk=old_chunk->rows[old_count].link; |
| if(next_chunk) { | if(next_chunk) { |
| // not last source chunk | // not last source chunk |
| Line 95 String::String(const String& src) : | Line 95 String::String(const String& src) : |
| } | } |
| } | } |
| link_row->link=0; | link_row->link=0; |
| fused_rows=src_used_rows; | src_used_rows; |
| fsize=src.fsize; | fsize=src.fsize; |
| } | } |
| /// @todo not very optimal | |
| uint String::used_rows() const { | |
| uint result=0; | |
| const Chunk *chunk=&head; | |
| do { | |
| const Chunk::Row *row=chunk->rows; | |
| for(uint i=0; i<chunk->count; i++, row++) { | |
| if(row==append_here) | |
| goto break2; | |
| result++; | |
| } | |
| chunk=row->link; | |
| } while(chunk); | |
| break2: | |
| return result; | |
| } | |
| void String::expand() { | void String::expand() { |
| size_t new_chunk_count=last_chunk->count+CR_GROW_COUNT; | uint new_chunk_count=last_chunk->count+CR_GROW_COUNT; |
| last_chunk=static_cast<Chunk *>( | last_chunk=static_cast<Chunk *>( |
| malloc(sizeof(size_t)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *))); | malloc(sizeof(uint)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *), 10)); |
| last_chunk->count=new_chunk_count; | last_chunk->count=new_chunk_count; |
| link_row->link=last_chunk; | link_row->link=last_chunk; |
| append_here=last_chunk->rows; | append_here=last_chunk->rows; |
| Line 114 String& String::append(const String& src | Line 132 String& String::append(const String& src |
| const Chunk *chunk=&src.head; | const Chunk *chunk=&src.head; |
| do { | do { |
| const Chunk::Row *row=chunk->rows; | const Chunk::Row *row=chunk->rows; |
| for(size_t i=0; i<chunk->count; i++, row++) { | for(uint i=0; i<chunk->count; i++, row++) { |
| if(row==src.append_here) | if(row==src.append_here) |
| goto break2; | goto break2; |
| Line 146 String& String::real_append(STRING_APPEN | Line 164 String& String::real_append(STRING_APPEN |
| append_here->item.origin.file=file; | append_here->item.origin.file=file; |
| append_here->item.origin.line=line; | append_here->item.origin.line=line; |
| #endif | #endif |
| append_here++; fused_rows++; | append_here++; |
| return *this; | return *this; |
| } | } |
| char String::first_char() const { | char String::first_char() const { |
| if(!fused_rows) | if(!fsize) |
| THROW(0, 0, | throw Exception(0, 0, |
| this, | this, |
| "getting first char of empty string"); | "getting first char of empty string"); |
| Line 166 uint String::hash_code() const { | Line 184 uint String::hash_code() const { |
| const Chunk *chunk=&head; | const Chunk *chunk=&head; |
| do { | do { |
| const Chunk::Row *row=chunk->rows; | const Chunk::Row *row=chunk->rows; |
| for(size_t i=0; i<chunk->count; i++) { | for(uint i=0; i<chunk->count; i++) { |
| if(row==append_here) | if(row==append_here) |
| goto break2; | goto break2; |
| Line 193 int String::cmp(int& partial, const Stri | Line 211 int String::cmp(int& partial, const Stri |
| size_t b_offset=0; | size_t b_offset=0; |
| Chunk::Row *a_end=append_here; | Chunk::Row *a_end=append_here; |
| Chunk::Row *b_end=src.append_here; | Chunk::Row *b_end=src.append_here; |
| size_t a_countdown=a_chunk->count; | uint a_countdown=a_chunk->count; |
| size_t b_countdown=b_chunk->count; | uint b_countdown=b_chunk->count; |
| size_t result; | int result; |
| size_t pos=0; | size_t pos=0; |
| bool a_break=size()==0; | bool a_break=size()==0; |
| Line 279 int String::cmp(int& partial, const char | Line 297 int String::cmp(int& partial, const char |
| size_t a_offset=this_offset; | size_t a_offset=this_offset; |
| size_t b_offset=0; | size_t b_offset=0; |
| Chunk::Row *a_end=append_here; | Chunk::Row *a_end=append_here; |
| size_t a_countdown=a_chunk->count; | uint a_countdown=a_chunk->count; |
| size_t pos=0; | size_t pos=0; |
| bool a_break=size()==0; | bool a_break=size()==0; |
| Line 294 int String::cmp(int& partial, const char | Line 312 int String::cmp(int& partial, const char |
| (b_size-b_offset); | (b_size-b_offset); |
| if(size_diff==0) { // a has same size as b | if(size_diff==0) { // a has same size as b |
| if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, | if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, |
| a_row->item.size-a_offset)!=0) | a_row->item.size-a_offset)!=0) |
| return result; | return result; |
| pos+=a_row->item.size; | pos+=a_row->item.size; |
| a_row++; a_countdown--; a_offset=0; | a_row++; a_countdown--; a_offset=0; |
| b_break=true; | b_break=true; |
| } else if (size_diff>0) { // a longer | } else if (size_diff>0) { // a longer |
| if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, | if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, |
| b_size-b_offset)!=0) | b_size-b_offset)!=0) |
| return result; | return result; |
| a_offset+=b_size-b_offset; | a_offset+=b_size-b_offset; |
| b_break=true; | b_break=true; |
| } else { // b longer | } else { // b longer |
| if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, | if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, |
| a_row->item.size-a_offset)!=0) | a_row->item.size-a_offset)!=0) |
| return result; | return result; |
| b_offset+=a_row->item.size-a_offset; | b_offset+=a_row->item.size-a_offset; |
| Line 340 int String::cmp(int& partial, const char | Line 358 int String::cmp(int& partial, const char |
| #ifndef NO_STRING_ORIGIN | #ifndef NO_STRING_ORIGIN |
| const Origin& String::origin() const { | const Origin& String::origin() const { |
| if(!fused_rows) { | if(!fsize) { |
| static const Origin empty_origin={"empty string"}; | static const Origin empty_origin={"empty string"}; |
| return empty_origin; | return empty_origin; |
| } | } |
| Line 360 String& String::mid(size_t start, size_t | Line 378 String& String::mid(size_t start, size_t |
| String& result=*NEW String(pool()); | String& result=*NEW String(pool()); |
| start=max(0, start); | start=max(0, start); |
| if(finish) | finish=min(size(), finish); |
| finish=min(size(), finish); | |
| else | |
| finish=size(); | |
| if(start==finish) | if(start==finish) |
| return result; | return result; |
| Line 371 String& String::mid(size_t start, size_t | Line 386 String& String::mid(size_t start, size_t |
| const Chunk *chunk=&head; | const Chunk *chunk=&head; |
| do { | do { |
| const Chunk::Row *row=chunk->rows; | const Chunk::Row *row=chunk->rows; |
| for(size_t i=0; i<chunk->count; pos+=row->item.size, i++, row++) { | for(uint i=0; i<chunk->count; pos+=row->item.size, i++, row++) { |
| if(row==append_here) | if(row==append_here) |
| goto break2; | goto break2; |
| Line 398 break2: | Line 413 break2: |
| } | } |
| int String::pos(const String& substr, | int String::pos(const String& substr, |
| size_t result, Untaint_lang lang) const { | int result, Untaint_lang lang) const { |
| for(; result<size(); result++) { | for(; result<size(); result++) { |
| int partial; cmp(partial, substr, result, lang); | int partial; cmp(partial, substr, result, lang); |
| if( | if( |
| Line 411 int String::pos(const String& substr, | Line 426 int String::pos(const String& substr, |
| } | } |
| int String::pos(const char *substr, size_t substr_size, | int String::pos(const char *substr, size_t substr_size, |
| size_t result, Untaint_lang lang) const { | int result, Untaint_lang lang) const { |
| for(; result<size(); result++) { | for(; result<size(); result++) { |
| int partial; cmp(partial, substr, substr_size, result, lang); | int partial; cmp(partial, substr, substr_size, result, lang); |
| if( | if( |
| Line 511 bool String::match(const unsigned char * | Line 526 bool String::match(const unsigned char * |
| bool *was_global) const { | bool *was_global) const { |
| if(!regexp.size()) | if(!regexp.size()) |
| THROW(0, 0, | throw Exception(0, 0, |
| aorigin, | aorigin, |
| "regexp is empty"); | "regexp is empty"); |
| const char *pattern=regexp.cstr(UL_AS_IS); | const char *pattern=regexp.cstr(UL_AS_IS); |
| Line 525 bool String::match(const unsigned char * | Line 540 bool String::match(const unsigned char * |
| pcre_tables); | pcre_tables); |
| if(!code) | if(!code) |
| THROW(0, 0, | throw Exception(0, 0, |
| ®exp.mid(erroffset, regexp.size()), | ®exp.mid(erroffset, regexp.size()), |
| "regular expression syntax error - %s", errptr); | "regular expression syntax error - %s", errptr); |
| 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 Exception(0, 0, |
| aorigin, | aorigin, |
| "pcre_info error (%d)", | "pcre_info error (%d)", |
| info_substrings); | info_substrings); |
| Line 543 bool String::match(const unsigned char * | Line 558 bool String::match(const unsigned char * |
| int length=strlen(subject); | int length=strlen(subject); |
| int ovecsize; | int ovecsize; |
| int *ovector=(int *)malloc(sizeof(int)* | int *ovector=(int *)malloc(sizeof(int)* |
| (ovecsize=(1/*match*/+info_substrings)*3)); | (ovecsize=(1/*match*/+info_substrings)*3), 11); |
| { // create table | { // create table |
| Array& columns=*NEW Array(pool()); | Array& columns=*NEW Array(pool()); |
| Line 572 bool String::match(const unsigned char * | Line 587 bool String::match(const unsigned char * |
| if(exec_substrings<0) { | if(exec_substrings<0) { |
| pcre_free(code); | pcre_free(code); |
| THROW(0, 0, | throw Exception(0, 0, |
| aorigin, | aorigin, |
| "regular expression execute error (%d)", | "regular expression execute error (%d)", |
| exec_substrings); | exec_substrings); |
| Line 620 String& String::change_case(Pool& pool, | Line 635 String& String::change_case(Pool& pool, |
| b=0; | b=0; |
| break; | break; |
| default: | default: |
| PTHROW(0, 0, | throw Exception(0, 0, |
| this, | this, |
| "unknown change case kind #%d", | "unknown change case kind #%d", |
| static_cast<int>(kind)); // never | static_cast<int>(kind)); // never |
| Line 631 String& String::change_case(Pool& pool, | Line 646 String& String::change_case(Pool& pool, |
| const Chunk *chunk=&head; | const Chunk *chunk=&head; |
| do { | do { |
| const Chunk::Row *row=chunk->rows; | const Chunk::Row *row=chunk->rows; |
| for(size_t i=0; i<chunk->count; i++, row++) { | for(uint i=0; i<chunk->count; i++, row++) { |
| if(row==append_here) | if(row==append_here) |
| goto break2; | goto break2; |
| char *new_cstr=(char *)pool.malloc(row->item.size); | char *new_cstr=(char *)pool.malloc(row->item.size, 12); |
| char *dest=new_cstr; | char *dest=new_cstr; |
| const char *src=row->item.ptr; | const char *src=row->item.ptr; |
| for(int size=row->item.size; size--; src++) { | for(int size=row->item.size; size--; src++) { |
| Line 658 break2: | Line 673 break2: |
| } | } |
| void String::join_chain(Pool& pool, | void String::join_chain(Pool& pool, |
| size_t& ai, const Chunk*& achunk, const Chunk::Row*& arow, | uint& ai, const Chunk*& achunk, const Chunk::Row*& arow, |
| Untaint_lang& joined_lang, const char *& joined_ptr, size_t& joined_size) const { | Untaint_lang& joined_lang, const char *& joined_ptr, size_t& joined_size) const { |
| joined_lang=arow->item.lang; | joined_lang=arow->item.lang; |
| // calc size | // calc size |
| joined_size=0; | joined_size=0; |
| { | { |
| size_t start_i=ai; | uint start_i=ai; |
| const Chunk::Row *start_row=arow; | const Chunk::Row *start_row=arow; |
| const Chunk *chunk=achunk; | const Chunk *chunk=achunk; |
| do { | do { |
| const Chunk::Row *row=start_row; | const Chunk::Row *row=start_row; |
| for(size_t i=start_i; i<chunk->count; i++, row++) { | for(uint i=start_i; i<chunk->count; i++, row++) { |
| if(row==append_here) | if(row==append_here) |
| goto break21; | goto break21; |
| Line 696 break21:; | Line 711 break21:; |
| achunk=arow->link; | achunk=arow->link; |
| } else { | } else { |
| // join adjacent rows | // join adjacent rows |
| char *ptr=(char *)pool.malloc(joined_size); | char *ptr=(char *)pool.malloc(joined_size,13); |
| joined_ptr=ptr; | joined_ptr=ptr; |
| size_t start_i=ai; | uint start_i=ai; |
| const Chunk::Row *start_row=arow; | const Chunk::Row *start_row=arow; |
| const Chunk *chunk=achunk; | const Chunk *chunk=achunk; |
| size_t i; | uint i; |
| const Chunk::Row *row; | const Chunk::Row *row; |
| do { | do { |
| row=start_row; | row=start_row; |
| Line 736 String& String::reconstruct(Pool& pool) | Line 751 String& String::reconstruct(Pool& pool) |
| const Chunk *chunk=&head; | const Chunk *chunk=&head; |
| do { | do { |
| const Chunk::Row *row=chunk->rows; | const Chunk::Row *row=chunk->rows; |
| for(size_t i=0; i<chunk->count; ) { | for(uint i=0; i<chunk->count; ) { |
| if(row==append_here) | if(row==append_here) |
| goto break2; | goto break2; |
| Line 764 String& String::replace_in_reconstructed | Line 779 String& String::replace_in_reconstructed |
| const Chunk *chunk=&head; | const Chunk *chunk=&head; |
| do { | do { |
| const Chunk::Row *row=chunk->rows; | const Chunk::Row *row=chunk->rows; |
| for(size_t i=0; i<chunk->count; i++, row++) { | for(uint i=0; i<chunk->count; i++, row++) { |
| if(row==append_here) | if(row==append_here) |
| goto break2; | goto break2; |
| const char *src=row->item.ptr; | const char *src=row->item.ptr; |
| size_t src_size=row->item.size; | size_t src_size=row->item.size; |
| char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio())); | char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio()), 14); |
| char *dest=new_cstr; | char *dest=new_cstr; |
| while(src_size) { | while(src_size) { |
| // there is a row where first column starts 'src' | // there is a row where first column starts 'src' |
| Line 805 String& String::replace(Pool& pool, Dict | Line 820 String& String::replace(Pool& pool, Dict |
| double String::as_double() const { | double String::as_double() const { |
| double result; | double result; |
| const char *cstr=this->cstr(); | const char *cstr; |
| char buf[MAX_NUMBER]; | |
| if(head.rows+1==append_here) { | |
| int size=min(head.rows[0].item.size, MAX_NUMBER-1); | |
| memcpy(buf, head.rows[0].item.ptr, size); | |
| buf[size]=0; | |
| cstr=buf; | |
| } else | |
| cstr=this->cstr(); | |
| char *error_pos; | char *error_pos; |
| // 0xABC | // 0xABC |
| if(cstr[0]=='0') | if(cstr[0]=='0') |
| Line 817 double String::as_double() const { | Line 840 double String::as_double() const { |
| result=(double)strtod(cstr, &error_pos); | result=(double)strtod(cstr, &error_pos); |
| if(*error_pos/*not EOS*/) | if(*error_pos/*not EOS*/) |
| THROW(0, 0, | throw Exception(0, 0, |
| this, | this, |
| "invalid number (double)"); | "invalid number (double)"); |
| Line 825 double String::as_double() const { | Line 848 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; |
| char buf[MAX_NUMBER]; | |
| if(head.rows+1==append_here) { | |
| int size=min(head.rows[0].item.size, MAX_NUMBER-1); | |
| memcpy(buf, head.rows[0].item.ptr, size); | |
| buf[size]=0; | |
| cstr=buf; | |
| } else | |
| cstr=this->cstr(); | |
| char *error_pos; | char *error_pos; |
| // 0xABC | // 0xABC |
| if(cstr[0]=='0') | if(cstr[0]=='0') |
| Line 837 int String::as_int() const { | Line 868 int String::as_int() const { |
| result=(int)strtol(cstr, &error_pos, 0); | result=(int)strtol(cstr, &error_pos, 0); |
| if(*error_pos/*not EOS*/) | if(*error_pos/*not EOS*/) |
| THROW(0, 0, | throw Exception(0, 0, |
| this, | this, |
| "invalid number (int)"); | "invalid number (int)"); |
| return result; | return result; |
| } | } |
| /* @todo maybe network order worth spending some effort? | |
| don't bothering myself with network byte order, | |
| am not planning to be able to move resulting file across platforms | |
| for now | |
| */ | |
| void String::serialize(size_t prolog_size, void *& buf, size_t& buf_size) const { | |
| buf_size= | |
| prolog_size | |
| +used_rows()*(sizeof(Untaint_lang)+sizeof(size_t)) | |
| +size(); | |
| buf=malloc(buf_size,15); | |
| char *cur=(char *)buf+prolog_size; | |
| const Chunk *chunk=&head; | |
| do { | |
| const Chunk::Row *row=chunk->rows; | |
| for(uint i=0; i<chunk->count; i++) { | |
| if(row==append_here) | |
| goto break2; | |
| // lang | |
| memcpy(cur, &row->item.lang, sizeof(Untaint_lang)); | |
| cur+=sizeof(Untaint_lang); | |
| // size | |
| memcpy(cur, &row->item.size, sizeof(size_t)); | |
| cur+=sizeof(size_t); | |
| // bytes | |
| memcpy(cur, row->item.ptr, row->item.size); | |
| cur+=row->item.size; | |
| row++; | |
| } | |
| chunk=row->link; | |
| } while(chunk); | |
| break2: | |
| ; | |
| } | |
| /* @todo maybe network order worth spending some effort? | |
| don't bothering myself with network byte order, | |
| am not planning to be able to move resulting file across platforms | |
| for now | |
| */ | |
| #ifndef DOXYGEN | |
| struct Serialized_piece { | |
| String::Untaint_lang lang; | |
| size_t size; | |
| char ptr[1]; | |
| }; | |
| #endif | |
| void String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file) { | |
| char *cur=((char *)buf)+prolog_size; | |
| buf_size-=prolog_size; | |
| while(buf_size) { | |
| Serialized_piece& p=*(Serialized_piece *)cur; | |
| APPEND(p.ptr, p.size, p.lang, file, 0); | |
| size_t piece_size=sizeof(p.lang)+sizeof(p.size)+p.size; | |
| cur+=piece_size; | |
| buf_size-=piece_size; | |
| } | |
| } | |