Annotation of parser3/src/main/pa_string.C, revision 1.198

1.45      paf         1: /** @file
1.174     paf         2:        Parser: string class. @see untalength_t.C.
1.46      paf         3: 
1.194     paf         4:        Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com)
1.138     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.164     paf         6: */
1.46      paf         7: 
1.198   ! paf         8: static const char * const IDENT_STRING_C="$Date: 2004/04/02 13:57:07 $";
1.4       paf         9: 
1.70      paf        10: #include "pcre.h"
                     11: 
1.12      paf        12: #include "pa_string.h"
1.22      paf        13: #include "pa_exception.h"
1.61      paf        14: #include "pa_table.h"
1.101     parser     15: #include "pa_dictionary.h"
1.132     paf        16: #include "pa_charset.h"
1.60      paf        17: 
1.185     paf        18: const String String::Empty;
                     19: 
1.193     paf        20: int pa_atoi(const char* str, const String* problem_source) {
                     21:        if(!str)
                     22:                return 0;
                     23: 
                     24:        while(*str && isspace(*str))
                     25:                str++;
                     26:        if(!*str)
                     27:                return 0;
                     28: 
                     29:        int result;
                     30:        char *error_pos;
                     31:        // 0xABC
                     32:        if(str[0]=='0')
                     33:                if(str[1]=='x' || str[1]=='X')
                     34:                        result=(int)(unsigned long)strtol(str, &error_pos, 0);
1.197     paf        35:                else {
                     36:                         // skip leading 0000, to disable octal interpretation
                     37:                        do str++; while(*str=='0');                             
                     38:                        result=(int)strtol(str, &error_pos, 0);
                     39:                }
1.193     paf        40:        else
                     41:                result=(int)strtol(str, &error_pos, 0);
                     42: 
                     43:        while(char c=*error_pos++)
                     44:                if(!isspace(c))
                     45:                        throw Exception("number.format",
                     46:                                problem_source,
                     47:                                problem_source?"invalid number (int)": "'%s' is invalid number (int)", str);
                     48: 
                     49:        return result;
                     50: }
                     51: 
                     52: double pa_atod(const char* str, const String* problem_source) {
                     53:        if(!str)
                     54:                return 0;
                     55: 
                     56:        while(*str && isspace(*str))
                     57:                str++;
                     58:        if(!*str)
                     59:                return 0;
                     60: 
                     61:        double result;
                     62:        char *error_pos;
                     63:        // 0xABC
                     64:        if(str[0]=='0')
                     65:                if(str[1]=='x' || str[1]=='X')
                     66:                        result=(double)(unsigned long)strtol(str, &error_pos, 0);
                     67:                else
                     68:                        result=(double)strtod(str+1/*skip leading 0*/, &error_pos);
                     69:        else
                     70:                result=(double)strtod(str, &error_pos);
                     71: 
                     72:        while(char c=*error_pos++)
                     73:                if(!isspace(c))
                     74:                        throw Exception("number.format",
                     75:                                problem_source,
                     76:                                problem_source?"invalid number (double)": "'%s' is invalid number (double)", str);
                     77: 
                     78:        return result;
                     79: }
                     80: 
1.176     paf        81: // cord lib extension
                     82: 
                     83: #ifndef DOXYGEN
                     84: typedef struct {
                     85:     ssize_t countdown;
                     86:     char target;       /* Character we're looking for  */
                     87: } chr_data;
                     88: #endif
                     89: static int CORD_range_contains_chr_greater_then_proc(char c, size_t size, void* client_data)
                     90: {
                     91:     register chr_data * d = (chr_data *)client_data;
                     92:     
                     93:     if (d -> countdown<=0) return(2);
                     94:     d -> countdown -= size;
                     95:     if (c > d -> target) return(1);
                     96:     return(0);
                     97: }
                     98: int CORD_range_contains_chr_greater_then(CORD x, size_t i, size_t n, int c)
                     99: {
                    100:     chr_data d;
                    101: 
                    102:     d.countdown = n;
                    103:     d.target = c;
                    104:     return(CORD_block_iter(x, i, CORD_range_contains_chr_greater_then_proc, &d) == 1/*alternatives: 0 normally ended, 2=struck 'n'*/);
                    105: }
                    106: 
1.187     paf       107: static int CORD_block_count_proc(char /*c*/, size_t /*size*/, void* client_data)
1.178     paf       108: {
                    109:     int* result=(int*)client_data;
                    110:     (*result)++;
                    111:     return(0); // 0=continue
                    112: }
                    113: size_t CORD_block_count(CORD x)
                    114: {
                    115:        size_t result=0;
                    116:        CORD_block_iter(x, 0, CORD_block_count_proc, &result);
                    117:     return result;
                    118: }
                    119: 
1.174     paf       120: // helpers
1.139     paf       121: 
1.174     paf       122: /// String::match uses this as replace & global search table columns
1.139     paf       123: 
1.174     paf       124: const int MAX_MATCH_GROUPS=100;
1.139     paf       125: 
1.174     paf       126: class String_match_table_template_columns: public ArrayString {
                    127: public:
                    128:        String_match_table_template_columns() {
                    129:                *this+=new String("prematch");
                    130:                *this+=new String("match");
                    131:                *this+=new String("postmatch");
                    132:                for(int i=0; i<MAX_MATCH_GROUPS; i++) {
1.176     paf       133:                        *this+=new String(String::Body::Format(1+i), String::L_CLEAN);
1.174     paf       134:                }
                    135:        }
                    136: };
                    137: 
                    138: Table string_match_table_template(new String_match_table_template_columns);
                    139: 
1.176     paf       140: // String::Body methods
1.140     paf       141: 
1.176     paf       142: String::Body String::Body::Format(int value) {
1.174     paf       143:        char local[MAX_NUMBER];
                    144:        size_t length=snprintf(local, MAX_NUMBER, "%d", value);
1.176     paf       145:        return String::Body(pa_strdup(local, length), length);
1.120     paf       146: }
                    147: 
1.195     paf       148: String::Body String::Body::trim(String::Trim_kind kind, const char* chars, 
                    149:                                                                size_t* out_start, size_t* out_length) const {
                    150:        size_t our_length=length();
                    151:        if(!our_length)
                    152:                return *this;
                    153:        if(!chars)
                    154:                chars=" \t\n"; // white space
                    155: 
                    156:        size_t start=0;
                    157:        size_t end=our_length;
1.196     paf       158:        // from left...
                    159:        if(kind!=TRIM_END) {
1.195     paf       160:                CORD_pos pos; set_pos(pos, 0);
                    161:                while(true) {
                    162:                        char c=CORD_pos_fetch(pos);
                    163:                        if(strchr(chars, c)) {
                    164:                                if(++start==our_length)
                    165:                                        return 0; // all chars are empty, just return empty string
                    166:                        } else
                    167:                                break;                  
                    168: 
                    169:                        CORD_next(pos);
                    170:                }
                    171:        }
1.196     paf       172:        // from right..
                    173:        if(kind!=TRIM_START) {
                    174:                CORD_pos pos; set_pos(pos, end-1);
                    175:                while(true) {
                    176:                        char c=CORD_pos_fetch(pos);
                    177:                        if(strchr(chars, c)) {
                    178:                                if(--end==0) // optimization: NO need to check for 'end>=start', that's(<) impossible
                    179:                                        return 0; // all chars are empty, just return empty string
                    180:                        } else
                    181:                                break;                  
                    182: 
                    183:                        CORD_prev(pos);
                    184:                }
                    185:        }
1.195     paf       186: 
                    187:        if(start==0 && end==our_length) // nobody moved a thing
                    188:                return *this;
                    189: 
                    190:        if(out_start)
                    191:                *out_start=start;
                    192:        size_t new_length=end-start;
                    193:        if(out_length)
                    194:                *out_length=new_length;
                    195: 
                    196:        return mid(start, new_length);
                    197: }
                    198: 
1.174     paf       199: static int CORD_batched_iter_fn_generic_hash_code(char c, void * client_data) {
                    200:        uint& result=*static_cast<uint*>(client_data);
                    201:        generic_hash_code(result, c);
                    202:        return 0;
                    203: }
                    204: static int CORD_batched_iter_fn_generic_hash_code(const char*  s, void * client_data) {
                    205:        uint& result=*static_cast<uint*>(client_data);
                    206:        generic_hash_code(result, s);
                    207:        return 0;
                    208: };
1.176     paf       209: uint String::Body::hash_code() const {
1.174     paf       210:        uint result=0;
                    211:        CORD_iter5(body, 0,
                    212:                CORD_batched_iter_fn_generic_hash_code, 
                    213:                CORD_batched_iter_fn_generic_hash_code, &result);
1.120     paf       214:        return result;
1.94      parser    215: }
                    216: 
1.174     paf       217: // String methods
                    218: 
                    219: String::String(const char* cstr, size_t helper_length, bool tainted): body(CORD_EMPTY) {
                    220:        append_help_length(cstr, helper_length, tainted?L_TAINTED:L_CLEAN);
1.115     paf       221: }
1.174     paf       222: String::String(const String::C cstr, bool tainted): body(CORD_EMPTY) {
                    223:        append_know_length(cstr.str, cstr.length, tainted?L_TAINTED:L_CLEAN);
1.5       paf       224: }
1.28      paf       225: 
1.174     paf       226: String& String::append_know_length(const char* str, size_t known_length, Language lang) {
                    227:        if(!known_length)
1.9       paf       228:                return *this;
1.122     paf       229: 
1.176     paf       230:        // first: langs
                    231:        langs.append(body, lang, known_length);
                    232:        // next: letters themselves
1.174     paf       233:        body.append_know_length(str, known_length);
1.1       paf       234: 
1.174     paf       235:        ASSERT_STRING_INVARIANT(*this);
1.1       paf       236:        return *this;
                    237: }
1.174     paf       238: String& String::append_help_length(const char* str, size_t helper_length, Language lang) {
                    239:        if(!str)
                    240:                return *this;
                    241:        size_t known_length=helper_length?helper_length:strlen(str);
                    242:        if(!known_length)
                    243:                return *this;
1.1       paf       244: 
1.174     paf       245:        return append_know_length(str, known_length, lang);
1.5       paf       246: }
1.174     paf       247: String& String::append_strdup(const char* str, size_t helper_length, Language lang) {
                    248:        size_t known_length=helper_length?helper_length:strlen(str);
                    249:        if(!known_length)
                    250:                return *this;
1.5       paf       251: 
1.176     paf       252:        // first: langs
                    253:        langs.append(body, lang, known_length);
                    254:        // next: letters themselves
1.174     paf       255:        body.append_strdup_know_length(str, known_length);
1.33      paf       256: 
1.174     paf       257:        ASSERT_STRING_INVARIANT(*this);
                    258:        return *this;
1.5       paf       259: }
1.46      paf       260: 
1.174     paf       261: /// @todo check in doc: whether it documents NOW bad situation "abc".mid(-1, 3) =were?="ab"
                    262: String& String::mid(size_t substr_begin, size_t substr_end) const {
                    263:        String& result=*new String;
                    264: 
                    265:        size_t self_length=length();
                    266:        substr_begin=min(substr_begin, self_length);
                    267:        substr_end=min(max(substr_end, substr_begin), self_length);
1.176     paf       268:        size_t substr_length=substr_end-substr_begin;
                    269:        if(!substr_length)
1.107     parser    270:                return result;
1.53      paf       271: 
1.176     paf       272:        // first: their langs
                    273:        result.langs.append(result.body, langs, substr_begin, substr_length);
                    274:        // next: letters themselves
                    275:        result.body=body.mid(substr_begin, substr_length);
1.174     paf       276: 
                    277: //     SAPI::log("piece of '%s' from %d to %d is '%s'",
                    278:                //cstr(), substr_begin, substr_end, result.cstr());
                    279:        ASSERT_STRING_INVARIANT(result);
1.53      paf       280:        return result;
1.54      paf       281: }
                    282: 
1.176     paf       283: size_t String::pos(const String::Body substr, size_t this_offset, Language lang) const {
1.183     paf       284:        size_t substr_length=substr.length();
                    285:        while(true) {
                    286:                size_t substr_begin=body.pos(substr, this_offset);
                    287:                
                    288:                if(substr_begin==CORD_NOT_FOUND)
                    289:                        return STRING_NOT_FOUND;
1.174     paf       290: 
1.183     paf       291:                if(langs.check_lang(lang, substr_begin, substr_length))
                    292:                        return substr_begin;
                    293: 
                    294:                this_offset=substr_begin+substr_length;
                    295:        }
1.58      paf       296: }
                    297: 
1.174     paf       298: size_t String::pos(const String& substr, 
                    299:                                size_t this_offset, Language lang) const {
                    300:        return pos(substr.body, this_offset, lang);
1.60      paf       301: }
                    302: 
1.174     paf       303: void String::split(ArrayString& result, 
                    304:                   size_t& pos_after, 
                    305:                   const char* delim, 
                    306:                   Language lang, int limit) const {
                    307:        size_t self_length=length();
                    308:        if(size_t delim_length=strlen(delim)) {
1.186     paf       309:                size_t pos_before;
1.60      paf       310:                // while we have 'delim'...
1.174     paf       311:                for(; (pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND && limit; limit--) {
1.69      paf       312:                        result+=&mid(pos_after, pos_before);
1.174     paf       313:                        pos_after=pos_before+delim_length;
1.60      paf       314:                }
                    315:                // last piece
1.174     paf       316:                if(pos_after<self_length && limit) {
                    317:                        result+=&mid(pos_after, self_length);
                    318:                        pos_after=self_length;
1.60      paf       319:                }
                    320:        } else { // empty delim
                    321:                result+=this;
1.174     paf       322:                pos_after+=self_length;
1.60      paf       323:        }
                    324: }
                    325: 
1.174     paf       326: void String::split(ArrayString& result, 
                    327:                   size_t& pos_after, 
                    328:                   const String& delim, Language lang, 
                    329:                   int limit) const {
1.140     paf       330:        if(!delim.is_empty()) {
1.186     paf       331:                size_t pos_before;
1.60      paf       332:                // while we have 'delim'...
1.174     paf       333:                for(; (pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND && limit; limit--) {
1.69      paf       334:                        result+=&mid(pos_after, pos_before);
1.174     paf       335:                        pos_after=pos_before+delim.length();
1.60      paf       336:                }
                    337:                // last piece
1.174     paf       338:                if(pos_after<length() && limit) {
                    339:                        result+=&mid(pos_after, length());
                    340:                        pos_after=length();
1.60      paf       341:                }
                    342:        } else { // empty delim
                    343:                result+=this;
1.174     paf       344:                pos_after+=length();
1.60      paf       345:        }
1.61      paf       346: }
                    347: 
1.174     paf       348: static void regex_options(const String* options, int *result, bool& need_pre_post_match){
1.63      paf       349:     struct Regex_option {
1.174     paf       350:                const char* keyL;
                    351:                const char* keyU;
1.63      paf       352:                int clear, set;
                    353:                int *result;
1.154     paf       354:                bool *flag;
1.63      paf       355:     } regex_option[]={
1.189     paf       356:                {"i", "I", 0, PCRE_CASELESS, result, 0}, // a=A
                    357:                {"s", "S", 0, PCRE_DOTALL, result, 0}, // \n\n$ [default]
                    358:                {"x", "U", 0, PCRE_EXTENDED, result, 0}, // whitespace in regex ignored
                    359:                {"m", "M", PCRE_DOTALL, PCRE_MULTILINE, result, 0}, // ^aaa\n$^bbb\n$
                    360:                {"g", "G", 0, 1, result+1, 0}, // many rows
1.154     paf       361:                {"'", 0, 0, 0, 0, &need_pre_post_match},
1.189     paf       362:                {0, 0, 0, 0, 0, 0}
1.63      paf       363:     };
1.171     paf       364:        result[0]=PCRE_EXTRA | PCRE_DOTALL | PCRE_DOLLAR_ENDONLY;
1.63      paf       365:        result[1]=0;
                    366: 
1.174     paf       367:     if(options && !options->is_empty()) 
1.153     paf       368:                for(Regex_option *o=regex_option; o->keyL; o++) 
1.174     paf       369:                        if(options->pos(o->keyL)!=STRING_NOT_FOUND
                    370:                                || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND)) {
1.154     paf       371:                                if(o->flag)
                    372:                                        *o->flag=true;
                    373:                                else { // result
                    374:                                        *o->result &= ~o->clear;
                    375:                                        *o->result |= o->set;
                    376:                                }
1.63      paf       377:                        }
                    378: }
                    379: 
1.174     paf       380: Table* String::match(Charset& source_charset,
                    381:                     const String& regexp, 
                    382:                     const String* options,
                    383:                     Row_action row_action, void *info,
                    384:                     bool& just_matched) const { 
1.140     paf       385:        if(regexp.is_empty())
1.149     paf       386:                throw Exception(0,
1.174     paf       387:                        0,
1.73      paf       388:                        "regexp is empty");
1.154     paf       389: 
1.174     paf       390:        const char* pattern=regexp.cstr();
                    391:        const char* errptr;
1.62      paf       392:        int erroffset;
1.173     paf       393:        bool need_pre_post_match=false;
1.174     paf       394:        int option_bits[2]={0};  regex_options(options, option_bits, need_pre_post_match);
                    395:        bool global=option_bits[1]!=0;
1.63      paf       396:        pcre *code=pcre_compile(pattern, option_bits[0], 
1.62      paf       397:                &errptr, &erroffset,
1.174     paf       398:                source_charset.pcre_tables);
1.62      paf       399: 
1.67      paf       400:        if(!code)
1.149     paf       401:                throw Exception(0,
1.174     paf       402:                        &regexp.mid(erroffset, regexp.length()),
1.74      paf       403:                        "regular expression syntax error - %s", errptr);
1.62      paf       404:        
1.174     paf       405:        int subpatterns=pcre_info(code, 0, 0);
                    406:        if(subpatterns<0) {
1.100     parser    407:                pcre_free(code);
1.149     paf       408:                throw Exception(0,
1.174     paf       409:                        &regexp,
1.76      paf       410:                        "pcre_info error (%d)", 
1.174     paf       411:                                subpatterns);
1.63      paf       412:        }
                    413: 
1.174     paf       414:        const char* subject=cstr();
                    415:        size_t subject_length=strlen(subject);
                    416:        const int oveclength=(1/*match*/+MAX_MATCH_GROUPS)*3;
                    417:        int ovector[oveclength];
1.155     paf       418: 
                    419:        // create table
1.173     paf       420:        Table::Action_options table_options;
1.174     paf       421:        Table& table=*new Table(string_match_table_template, table_options);
1.63      paf       422: 
1.64      paf       423:        int exec_option_bits=0;
1.154     paf       424:        int prestart=0;
                    425:        int poststart=0;
1.174     paf       426:        int postfinish=length();
1.63      paf       427:        while(true) {
                    428:                int exec_substrings=pcre_exec(code, 0,
1.174     paf       429:                        subject, subject_length, prestart,
                    430:                        exec_option_bits, ovector, oveclength);
1.63      paf       431:                
                    432:                if(exec_substrings==PCRE_ERROR_NOMATCH) {
1.100     parser    433:                        pcre_free(code);
1.174     paf       434:                        row_action(table, 0/*last time, no raw*/, 0, 0, poststart, postfinish, info);
                    435:                        if(global || subpatterns)
                    436:                                return &table; // global or with subpatterns=true+result
                    437:                        else {
                    438:                                just_matched=false; return 0; // not global=no result
                    439:                        }
1.63      paf       440:                }
                    441: 
                    442:                if(exec_substrings<0) {
1.100     parser    443:                        pcre_free(code);
1.149     paf       444:                        throw Exception(0,
1.174     paf       445:                                &regexp,
1.76      paf       446:                                "regular expression execute error (%d)", 
1.63      paf       447:                                        exec_substrings);
                    448:                }
                    449: 
1.154     paf       450:                int prefinish=ovector[0];
                    451:                poststart=ovector[1];
1.174     paf       452:                ArrayString* row=new ArrayString;
                    453:                if(need_pre_post_match) {
                    454:                        *row+=&mid(0, prefinish); // .prematch column value
                    455:                        *row+=&mid(prefinish, poststart); // .match
                    456:                        *row+=&mid(poststart, postfinish); // .postmatch
                    457:                } else {
1.185     paf       458:                        *row+=&Empty; // .prematch column value
                    459:                        *row+=&Empty; // .match
                    460:                        *row+=&Empty; // .postmatch
1.174     paf       461:                }
1.63      paf       462:                
                    463:                for(int i=1; i<exec_substrings; i++) {
1.69      paf       464:                        // -1:-1 case handled peacefully by mid() itself
1.174     paf       465:                        *row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value
1.63      paf       466:                }
                    467:                
1.174     paf       468:                row_action(table, row, prestart, prefinish, poststart, postfinish, info);
1.63      paf       469: 
1.174     paf       470:                if(!global || prestart==poststart) { // not global | going to hang
1.100     parser    471:                        pcre_free(code);
1.174     paf       472:                        row_action(table, 0/*last time, no row*/, 0, 0, poststart, postfinish, info);
                    473:                        return &table;
1.63      paf       474:                }
1.154     paf       475:                prestart=poststart;
1.63      paf       476: 
                    477: /*
                    478:                if(option_bits[0] & PCRE_MULTILINE)
1.64      paf       479:                        exec_option_bits|=PCRE_NOTBOL; // start of subject+startoffset not BOL
1.63      paf       480: */
                    481:        }
1.82      parser    482: }
                    483: 
1.174     paf       484: String& String::change_case(Charset& source_charset, Change_case_kind kind) const {
                    485:        String& result=*new String();
                    486:        if(is_empty())
                    487:                return result;
                    488: 
                    489:        char* new_cstr=cstrm();
1.192     paf       490:        size_t new_cstr_len=length();
1.181     paf       491:        if(source_charset.isUTF8()) {
                    492:                switch(kind) {
                    493:                case CC_UPPER:
1.192     paf       494:                        change_case_UTF8((const XMLByte*)new_cstr, new_cstr_len, (XMLByte*)new_cstr, new_cstr_len, UTF8CaseToUpper);
1.181     paf       495:                        break;
                    496:                case CC_LOWER:
1.192     paf       497:                        change_case_UTF8((const XMLByte*)new_cstr, new_cstr_len, (XMLByte*)new_cstr, new_cstr_len, UTF8CaseToLower);
1.181     paf       498:                        break;
                    499:                default:
                    500:                        assert(!"unknown change case kind");
                    501:                        break; // never
                    502:                }       
                    503:                
                    504:        } else {
                    505:                const unsigned char *tables=source_charset.pcre_tables;
1.82      parser    506: 
1.181     paf       507:                const unsigned char *a;
                    508:                const unsigned char *b;
                    509:                switch(kind) {
                    510:                case CC_UPPER:
                    511:                        a=tables+lcc_offset;
                    512:                        b=tables+fcc_offset;
                    513:                        break;
                    514:                case CC_LOWER:
                    515:                        a=tables+lcc_offset;
                    516:                        b=0;
                    517:                        break;
                    518:                default:
                    519:                        assert(!"unknown change case kind");
                    520:                        a=b=0; // calm, compiler
                    521:                        break; // never
                    522:                }       
                    523: 
1.192     paf       524:                char *dest=new_cstr;
1.181     paf       525:                unsigned char index;
1.190     paf       526:                for(const char* current=new_cstr; (index=(unsigned char)*current); current++) {
1.181     paf       527:                        unsigned char c=a[index];
                    528:                        if(b)
                    529:                                c=b[c];
                    530: 
                    531:                        *dest++=(char)c;
                    532:                }
1.174     paf       533:        }
1.176     paf       534:        result.langs=langs;
1.174     paf       535:        result.body=new_cstr;
1.89      parser    536: 
1.101     parser    537:        return result;
                    538: }
                    539: 
1.174     paf       540: const String& String::replace(const Dictionary& dict) const {
                    541:        String& result=*new String();
                    542:        const char* old_cstr=cstr();
                    543:        const char* prematch_begin=old_cstr;
                    544: 
                    545:        const char* current=old_cstr;
                    546:        while(*current) {
1.184     paf       547:                if(Dictionary::Subst subst=dict.first_that_begins(current)) {
1.174     paf       548:                        // prematch
                    549:                        if(size_t prematch_length=current-prematch_begin) {
1.179     paf       550:                                result.langs.append(result.body, langs, prematch_begin-old_cstr, prematch_length);
1.174     paf       551:                                result.body.append_strdup_know_length(prematch_begin, prematch_length);
1.101     parser    552:                        }
                    553: 
1.174     paf       554:                        // match
                    555:                        // skip 'a' in 'current'; move prematch_begin
1.184     paf       556:                        current+=subst.from_length; prematch_begin=current;
1.174     paf       557: 
1.184     paf       558:                        if(const String* b=subst.to) // are there any b?
1.174     paf       559:                                result<<*b;
                    560:                } else // simply advance
                    561:                        current++; 
                    562:        }
1.156     paf       563: 
1.174     paf       564:        // postmatch
                    565:        if(size_t postmatch_length=current-prematch_begin) {
1.179     paf       566:                result.langs.append(result.body, langs, prematch_begin-old_cstr, postmatch_length);
1.174     paf       567:                result.body.append_strdup_know_length(prematch_begin, postmatch_length);
                    568:        }
1.156     paf       569: 
1.174     paf       570:        ASSERT_STRING_INVARIANT(result);
1.82      parser    571:        return result;
1.61      paf       572: }
1.113     parser    573: 
1.180     paf       574: static int serialize_body_char(char c, char** cur) {
                    575:        *((*cur)++)=c;
                    576:        return 0; // 0=continue
                    577: };
1.174     paf       578: static int serialize_body_piece(const char* s, char** cur) {
                    579:        size_t length=strlen(s);
                    580:        memcpy(*cur, s, length);  *cur+=length;
1.178     paf       581:        return 0; // 0=continue
1.174     paf       582: };
1.178     paf       583: static int serialize_lang_piece(char alang, size_t asize, char** cur) {
                    584:        // lang
1.191     paf       585:        **cur=alang; (*cur)++;
                    586:        // length [WARNING: not cast, addresses must be %4=0 on sparc]
1.178     paf       587:        memcpy(*cur, &asize, sizeof(asize));  *cur+=sizeof(asize);
                    588: 
                    589:        return 0; // 0=continue
                    590: }
1.174     paf       591: String::Cm String::serialize(size_t prolog_length) const {
1.178     paf       592:        size_t fragments_count=langs.count();
1.174     paf       593:        size_t buf_length=
1.178     paf       594:                prolog_length //1
                    595:                +sizeof(size_t) //2
                    596:                +fragments_count*(sizeof(char)+sizeof(size_t)) //3
                    597:                +body.length() //4
                    598:                +1; // for zero terminator used in deserialize
1.174     paf       599:        String::Cm result(new(PointerFreeGC) char[buf_length], buf_length);
                    600: 
                    601:        // 1: prolog
                    602:        char *cur=result.str+prolog_length;
1.191     paf       603:        // 2: langs.count [WARNING: not cast, addresses must be %4=0 on sparc]
1.174     paf       604:        memcpy(cur, &fragments_count, sizeof(fragments_count));  cur+=sizeof(fragments_count);
                    605:        // 3: lang info
1.178     paf       606:        langs.for_each(body, serialize_lang_piece, &cur);
1.174     paf       607:        // 4: letters
1.180     paf       608:        body.for_each(serialize_body_char, serialize_body_piece, &cur);
1.182     paf       609:        // 5: zero terminator
                    610:        *cur=0;
1.113     parser    611: 
1.174     paf       612:        return result;
1.113     parser    613: }
1.174     paf       614: bool String::deserialize(size_t prolog_length, void *buf, size_t buf_length) {
                    615:        if(buf_length<=prolog_length)
1.148     paf       616:                return false;
1.174     paf       617:        buf_length-=prolog_length;
1.178     paf       618:        buf_length-=1; // 5: zero terminator
1.135     paf       619: 
1.174     paf       620:        // 1: prolog
                    621:        const char* cur=(const char* )buf+prolog_length;
1.113     parser    622: 
1.176     paf       623:        // 2: langs.count
1.191     paf       624:        size_t fragments_count;
                    625:        if(buf_length<sizeof(fragments_count)) // langs.count don't fit?
1.174     paf       626:                return false;
1.191     paf       627:        // [WARNING: not cast, addresses must be %4=0 on sparc]
                    628:        memcpy(&fragments_count, cur, sizeof(fragments_count));  cur+=sizeof(fragments_count);
                    629:        buf_length-=sizeof(fragments_count);
1.174     paf       630:        
                    631:        if(fragments_count) {
                    632:                // 3: lang info
                    633:                size_t total_length=0;
                    634:                for(size_t f=0; f<fragments_count; f++) {
1.191     paf       635:                        char lang;
                    636:                        size_t fragment_length;
                    637:                        size_t piece_length=sizeof(lang)+sizeof(fragment_length);
1.174     paf       638:                        if(buf_length<piece_length) // lang+length
                    639:                                return false;
                    640: 
1.191     paf       641:                        // lang
                    642:                        lang=*cur++;
                    643:                        // length [WARNING: not cast, addresses must be %4=0 on sparc]
                    644:                        memcpy(&fragment_length, cur, sizeof(fragment_length));  cur+=sizeof(fragment_length);
                    645: 
                    646:                        // uchar needed to prevent propagating 0x80 bit to upper bytes
                    647:                        langs.append(total_length, (String::Language)(uchar)lang, fragment_length);
1.174     paf       648:                        total_length+=fragment_length;
1.148     paf       649: 
1.174     paf       650:                        buf_length-=piece_length;
                    651:                }
1.128     paf       652: 
1.174     paf       653:                // 4: letters
                    654:                if(buf_length!=total_length)
1.148     paf       655:                        return false;
                    656: 
1.178     paf       657:                // serialize wrote extra zero byte there, we can rely on that
1.176     paf       658:                body=String::Body(cur, buf_length);
1.174     paf       659:        }
1.113     parser    660: 
1.174     paf       661:        ASSERT_STRING_INVARIANT(*this);
1.148     paf       662:        return true;
1.176     paf       663: }
                    664: 
                    665: const char* String::Body::v() const {
                    666:        return CORD_to_const_char_star(body);
                    667: }
                    668: const char* String::Languages::v() const {
1.177     paf       669:        if(opt.is_not_just_lang)
1.176     paf       670:                return CORD_to_const_char_star(langs);
                    671:        else
                    672:                return (const char*)&langs;
                    673: }
                    674: const char* String::v() const {
1.198   ! paf       675:        const uint LIMIT_VIEW=20;
1.176     paf       676:        char* buf=(char*)malloc(MAX_STRING);
                    677:        const char*body_view=body.v();
                    678:        const char*langs_view=langs.v();
                    679:        snprintf(buf, MAX_STRING, 
1.178     paf       680:                "%d:%.*s%s}   "
1.176     paf       681:                "{%d:%s",
1.178     paf       682:                langs.count(), LIMIT_VIEW, langs_view, strlen(langs_view)>LIMIT_VIEW?"...":"",
1.176     paf       683:                strlen(body_view), body_view
                    684:        );
                    685: 
                    686:        return buf;
1.113     parser    687: }
1.195     paf       688: 
                    689: const String& String::trim(String::Trim_kind kind, const char* chars) const {
                    690:        if(!length())
                    691:                return *this;
                    692: 
                    693:        size_t substr_begin, substr_length;
                    694:        Body new_body=body.trim(kind, chars, &substr_begin, &substr_length);
                    695:        if(new_body==body) // we received unchanged pointer, do likewise
                    696:                return *this;
                    697:        // new_body differs from body, adjust langs along
                    698: 
                    699:        String& result=*new String;
                    700:        if(!new_body) // body.trim produced empty result
                    701:                return result;
                    702:        // body.trim produced nonempty result
                    703: 
                    704:        // first: their langs
                    705:        result.langs.append(result.body, langs, substr_begin, substr_length);
                    706:        // next: letters themselves
                    707:        result.body=new_body;
                    708: 
                    709:        ASSERT_STRING_INVARIANT(result);
                    710:        return result;
1.198   ! paf       711: }

E-mail: