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

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

E-mail: