--- parser3/src/main/pa_string.C 2003/04/10 11:35:10 1.172.2.21.2.39 +++ parser3/src/main/pa_string.C 2003/09/24 08:02:09 1.175.2.4 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_STRING_C="$Date: 2003/04/10 11:35:10 $"; +static const char* IDENT_STRING_C="$Date: 2003/09/24 08:02:09 $"; #include "pcre.h" @@ -15,6 +15,32 @@ static const char* IDENT_STRING_C="$Date #include "pa_dictionary.h" #include "pa_charset.h" +// cord lib extension + +#ifndef DOXYGEN +typedef struct { + ssize_t countdown; + char target; /* Character we're looking for */ +} chr_data; +#endif +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; + + if (d -> countdown<=0) return(2); + d -> countdown -= size; + if (c > d -> target) return(1); + return(0); +} +int CORD_range_contains_chr_greater_then(CORD x, size_t i, size_t n, int c) +{ + chr_data d; + + d.countdown = n; + d.target = c; + return(CORD_block_iter(x, i, CORD_range_contains_chr_greater_then_proc, &d) == 1/*alternatives: 0 normally ended, 2=struck 'n'*/); +} + // helpers /// String::match uses this as replace & global search table columns @@ -35,51 +61,6 @@ public: Table string_match_table_template(new String_match_table_template_columns); -// String::ArrayFragment methods - -void String::ArrayFragment::append_positions(const ArrayFragment& src, - size_t substr_begin, size_t substr_end) { - if(substr_begin==substr_end) - return; - -// FILE *err=fopen("append.log", "wt"); - - size_t fragment_begin=0; - size_t fragment_end; - for(Array_iterator i(src); ; fragment_begin=fragment_end) { - const Fragment fragment=i.next(); - fragment_end=fragment_begin+fragment.length; - //fprintf(err, "1end=%u\n", fragment_end);fflush(err); - - // not reached fragments which may include 'substr'? - if(!(substr_begin>=fragment_begin && substr_begin<=fragment_end)) - continue; - - // found first fragment including piece of 'substr' - if(substr_end<=fragment_end) // fits into first fragment? - *this+=Fragment(fragment.lang, substr_end-substr_begin); - else { // spans more then one fragment - *this+=Fragment(fragment.lang, fragment_end-substr_begin); - while(true) { - const Fragment fragment=i.next(); - fragment_end=(fragment_begin=fragment_end)+fragment.length; - //fprintf(err, "2end=%u\n", fragment_end);fflush(err); - - if(substr_end>fragment_end) // are there still more? - append(Fragment(fragment.lang, fragment.length)); // appending whole fragment - else { // no, it was last - append(Fragment(fragment.lang, substr_end-fragment_begin)); - //fclose(err); - return; - } - } - } - - break; - } - //fclose(err); -} - // StringBody methods StringBody StringBody::Format(int value) { @@ -115,17 +96,14 @@ String::String(const String::C cstr, boo append_know_length(cstr.str, cstr.length, tainted?L_TAINTED:L_CLEAN); } -String::String(const String& src): body(src.body) { - fragments.append(src.fragments); - ASSERT_STRING_INVARIANT(*this); -} - String& String::append_know_length(const char* str, size_t known_length, Language lang) { - if(!known_length || !*str) + if(!known_length) return *this; + // first: langs + langs.append(body, lang, known_length); + // next: letters themselves body.append_know_length(str, known_length); - fragments+=Fragment(lang, known_length); ASSERT_STRING_INVARIANT(*this); return *this; @@ -144,8 +122,10 @@ String& String::append_strdup(const char if(!known_length) return *this; + // first: langs + langs.append(body, lang, known_length); + // next: letters themselves body.append_strdup_know_length(str, known_length); - fragments+=Fragment(lang, known_length); ASSERT_STRING_INVARIANT(*this); return *this; @@ -161,63 +141,23 @@ String& String::mid(size_t substr_begin, if(substr_begin==substr_end) return result; - // first: letters themselves + // first: their langs + result.langs.append(result.body, langs, substr_begin, substr_end-substr_begin); + // next: letters themselves result.body=body.mid(substr_begin, substr_end-substr_begin); - // next: their langs - result.fragments.append_positions(fragments, substr_begin, substr_end); - // SAPI::log("piece of '%s' from %d to %d is '%s'", //cstr(), substr_begin, substr_end, result.cstr()); ASSERT_STRING_INVARIANT(result); return result; } -size_t String::pos(const StringBody substr, - size_t this_offset, Language lang) const { - // first: letters themselves +size_t String::pos(const StringBody substr, size_t this_offset, Language lang) const { size_t substr_begin=body.pos(substr, this_offset); - if(substr_begin==CORD_NOT_FOUND) + if(substr_begin==CORD_NOT_FOUND || !langs.check_lang(lang, substr_begin, substr.length())) return STRING_NOT_FOUND; - // next: check the lang when specified - - if(lang==L_UNSPECIFIED) // ignore lang? - return substr_begin; - - // substr must be in one fragment, and fragments' lang must = lang - size_t substr_end=substr_begin+substr.length(); - size_t fragment_begin=0; - size_t fragment_end; - for(Array_iterator i(fragments); i.has_next(); fragment_begin=fragment_end) { - const Fragment fragment=i.next(); - fragment_end=fragment_begin+fragment.length; - - if(substr_begin=fragment_end) // begin of substr OUT of current fragment? - continue; - - if(substr_end>fragment_end) // end of substr OUT of current fragment? - throw Exception(0, // (*) see below - this, - "searching for '%s' starting from %ud problem: found begin in one fragment, but end in another", - substr.cstr(), this_offset); - - if(fragment.lang<=lang) - return substr_begin; - else { // bad lang... - /// WARNING: this possibly skips assert (*), but it's fast - substr_begin=body.pos(substr, fragment_end/*...search AFTER for more*/); - if(substr_begin==CORD_NOT_FOUND) - return STRING_NOT_FOUND; - - size_t substr_end=substr_begin+substr.length(); - // and continuing with next fragment - } - } - - return STRING_NOT_FOUND; + return substr_begin; } size_t String::pos(const String& substr, @@ -233,7 +173,7 @@ void String::split(ArrayString& result, if(size_t delim_length=strlen(delim)) { int pos_before; // while we have 'delim'... - for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) { + for(; (pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND && limit; limit--) { result+=&mid(pos_after, pos_before); pos_after=pos_before+delim_length; } @@ -255,7 +195,7 @@ void String::split(ArrayString& result, if(!delim.is_empty()) { int pos_before; // while we have 'delim'... - for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) { + for(; (pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND && limit; limit--) { result+=&mid(pos_after, pos_before); pos_after=pos_before+delim.length(); } @@ -291,8 +231,8 @@ static void regex_options(const String* if(options && !options->is_empty()) for(Regex_option *o=regex_option; o->keyL; o++) - if(options->pos(o->keyL)>=0 - || (o->keyU && options->pos(o->keyU)>=0)) { + if(options->pos(o->keyL)!=STRING_NOT_FOUND + || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND)) { if(o->flag) *o->flag=true; else { // result @@ -342,7 +282,8 @@ Table* String::match(Charset& source_cha int ovector[oveclength]; // create table - Table& table=*new Table(string_match_table_template); + Table::Action_options table_options; + Table& table=*new Table(string_match_table_template, table_options); int exec_option_bits=0; int prestart=0; @@ -442,8 +383,8 @@ String& String::change_case(Charset& sou *dest++=(char)c; } + result.langs=langs; result.body=new_cstr; - result.fragments.append(fragments); return result; } @@ -458,8 +399,8 @@ const String& String::replace(const Dict if(Table::element_type row=dict.first_that_begins(current)) { // prematch if(size_t prematch_length=current-prematch_begin) { + result.langs.append(result.body, langs, prematch_begin-old_cstr, current-old_cstr); result.body.append_strdup_know_length(prematch_begin, prematch_length); - result.fragments.append_positions(fragments, prematch_begin-old_cstr, current-old_cstr); } // match @@ -478,8 +419,8 @@ const String& String::replace(const Dict // postmatch if(size_t postmatch_length=current-prematch_begin) { + result.langs.append(result.body, langs, prematch_begin-old_cstr, current-old_cstr); result.body.append_strdup_know_length(prematch_begin, postmatch_length); - result.fragments.append_positions(fragments, prematch_begin-old_cstr, current-old_cstr); } ASSERT_STRING_INVARIANT(result); @@ -560,9 +501,12 @@ static int serialize_body_piece(const ch return 0; }; String::Cm String::serialize(size_t prolog_length) const { +#if TODO + //_asm int 3; size_t buf_length= prolog_length - +fragments.count()*(sizeof(Language)+sizeof(size_t)) + +sizeof(size_t) + +langs.count()*(sizeof(Language)+sizeof(size_t)) +length(); String::Cm result(new(PointerFreeGC) char[buf_length], buf_length); @@ -570,12 +514,12 @@ String::Cm String::serialize(size_t prol char *cur=result.str+prolog_length; - // 2: fragments.count - size_t fragments_count=fragments.count(); + // 2: langs.count + size_t fragments_count=langs.count(); memcpy(cur, &fragments_count, sizeof(fragments_count)); cur+=sizeof(fragments_count); // 3: lang info - for(Array_iterator i(fragments); i.has_next(); ) { + for(Array_iterator i(langs); i.has_next(); ) { const Fragment fragment=i.next(); // lang memcpy(cur, &fragment.lang, sizeof(fragment.lang)); cur+=sizeof(fragment.lang); @@ -587,8 +531,11 @@ String::Cm String::serialize(size_t prol body.for_each(serialize_body_piece, &cur); return result; +#endif + return String::Cm(0, 0); } bool String::deserialize(size_t prolog_length, void *buf, size_t buf_length) { +#if TODO if(buf_length<=prolog_length) return false; buf_length-=prolog_length; @@ -596,8 +543,8 @@ bool String::deserialize(size_t prolog_l // 1: prolog const char* cur=(const char* )buf+prolog_length; - // 2: fragments.count - if(buf_length(cur); cur+=sizeof(size_t); buf_length-=sizeof(size_t); @@ -612,7 +559,7 @@ bool String::deserialize(size_t prolog_l Language lang=*reinterpret_cast(cur); cur+=sizeof(Language); size_t fragment_length=*reinterpret_cast(cur); cur+=sizeof(size_t); - fragments+=Fragment(lang, fragment_length); + langs+=Fragment(lang, fragment_length); total_length+=fragment_length; buf_length-=piece_length; @@ -627,4 +574,6 @@ bool String::deserialize(size_t prolog_l ASSERT_STRING_INVARIANT(*this); return true; +#endif + return false; }