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

1.45      paf         1: /** @file
1.174   ! paf         2:        Parser: string class. @see untalength_t.C.
1.46      paf         3: 
1.174   ! paf         4:        Copyright (c) 2001-2003 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.174   ! paf         8: static const char* IDENT_STRING_C="$Date: 2003/07/22 08:20:24 $";
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.174   ! paf        18: // helpers
1.139     paf        19: 
1.174   ! paf        20: /// String::match uses this as replace & global search table columns
1.139     paf        21: 
1.174   ! paf        22: const int MAX_MATCH_GROUPS=100;
1.139     paf        23: 
1.174   ! paf        24: class String_match_table_template_columns: public ArrayString {
        !            25: public:
        !            26:        String_match_table_template_columns() {
        !            27:                *this+=new String("prematch");
        !            28:                *this+=new String("match");
        !            29:                *this+=new String("postmatch");
        !            30:                for(int i=0; i<MAX_MATCH_GROUPS; i++) {
        !            31:                        *this+=new String(StringBody::Format(1+i), String::L_CLEAN);
        !            32:                }
        !            33:        }
        !            34: };
        !            35: 
        !            36: Table string_match_table_template(new String_match_table_template_columns);
        !            37: 
        !            38: // String::ArrayFragment methods
        !            39: 
        !            40: void String::ArrayFragment::append_positions(const ArrayFragment& src, 
        !            41:                                             size_t substr_begin, size_t substr_end) {
        !            42:        if(substr_begin==substr_end)
        !            43:                return;
        !            44: 
        !            45: //     FILE *err=fopen("append.log", "wt");
        !            46: 
        !            47:        size_t fragment_begin=0;
        !            48:        size_t fragment_end;
        !            49:        for(Array_iterator<element_type> i(src); ; fragment_begin=fragment_end) {
        !            50:                const Fragment fragment=i.next();
        !            51:                fragment_end=fragment_begin+fragment.length;            
        !            52:                //fprintf(err, "1end=%u\n", fragment_end);fflush(err);
        !            53: 
        !            54:                // not reached fragments which may include 'substr'?
        !            55:                if(!(substr_begin>=fragment_begin && substr_begin<fragment_end))
        !            56:                        continue;
        !            57: 
        !            58:                // found first fragment including piece of 'substr'
        !            59:                if(substr_end<=fragment_end) // fits into first fragment?
        !            60:                        *this+=Fragment(fragment.lang, substr_end-substr_begin);
        !            61:                else { // spans more then one fragment
        !            62:                        *this+=Fragment(fragment.lang, fragment_end-substr_begin);
        !            63:                        while(true) {
        !            64:                                const Fragment fragment=i.next();
        !            65:                                fragment_end=(fragment_begin=fragment_end)+fragment.length;
        !            66:                                //fprintf(err, "2end=%u\n", fragment_end);fflush(err);
        !            67: 
        !            68:                                if(substr_end>fragment_end) // are there still more?
        !            69:                                        append(Fragment(fragment.lang, fragment.length)); // appending whole fragment
        !            70:                                else { // no, it was last
        !            71:                                        append(Fragment(fragment.lang, substr_end-fragment_begin));
        !            72:                                        //fclose(err);
        !            73:                                        return;
        !            74:                                }
        !            75:                        }
        !            76:                }
        !            77: 
        !            78:                break;
        !            79:        }
        !            80:        //fclose(err);
1.1       paf        81: }
1.140     paf        82: 
1.174   ! paf        83: // StringBody methods
1.140     paf        84: 
1.174   ! paf        85: StringBody StringBody::Format(int value) {
        !            86:        char local[MAX_NUMBER];
        !            87:        size_t length=snprintf(local, MAX_NUMBER, "%d", value);
        !            88:        return StringBody(pa_strdup(local, length), length);
1.120     paf        89: }
                     90: 
1.174   ! paf        91: static int CORD_batched_iter_fn_generic_hash_code(char c, void * client_data) {
        !            92:        uint& result=*static_cast<uint*>(client_data);
        !            93:        generic_hash_code(result, c);
        !            94:        return 0;
        !            95: }
        !            96: static int CORD_batched_iter_fn_generic_hash_code(const char*  s, void * client_data) {
        !            97:        uint& result=*static_cast<uint*>(client_data);
        !            98:        generic_hash_code(result, s);
        !            99:        return 0;
        !           100: };
        !           101: uint StringBody::hash_code() const {
        !           102:        uint result=0;
        !           103:        CORD_iter5(body, 0,
        !           104:                CORD_batched_iter_fn_generic_hash_code, 
        !           105:                CORD_batched_iter_fn_generic_hash_code, &result);
1.120     paf       106:        return result;
1.94      parser    107: }
                    108: 
1.174   ! paf       109: // String methods
        !           110: 
        !           111: String::String(const char* cstr, size_t helper_length, bool tainted): body(CORD_EMPTY) {
        !           112:        append_help_length(cstr, helper_length, tainted?L_TAINTED:L_CLEAN);
1.115     paf       113: }
1.174   ! paf       114: String::String(const String::C cstr, bool tainted): body(CORD_EMPTY) {
        !           115:        append_know_length(cstr.str, cstr.length, tainted?L_TAINTED:L_CLEAN);
1.5       paf       116: }
1.28      paf       117: 
1.174   ! paf       118: String::String(const String& src): body(src.body) {
        !           119:        fragments.append(src.fragments);
        !           120:        ASSERT_STRING_INVARIANT(*this);
        !           121: }
1.139     paf       122: 
1.174   ! paf       123: String& String::append_know_length(const char* str, size_t known_length, Language lang) {
        !           124:        if(!known_length)
1.9       paf       125:                return *this;
1.122     paf       126: 
1.174   ! paf       127:        body.append_know_length(str, known_length);
        !           128:        fragments+=Fragment(lang, known_length);
1.1       paf       129: 
1.174   ! paf       130:        ASSERT_STRING_INVARIANT(*this);
1.1       paf       131:        return *this;
                    132: }
1.174   ! paf       133: String& String::append_help_length(const char* str, size_t helper_length, Language lang) {
        !           134:        if(!str)
        !           135:                return *this;
        !           136:        size_t known_length=helper_length?helper_length:strlen(str);
        !           137:        if(!known_length)
        !           138:                return *this;
1.1       paf       139: 
1.174   ! paf       140:        return append_know_length(str, known_length, lang);
1.5       paf       141: }
1.174   ! paf       142: String& String::append_strdup(const char* str, size_t helper_length, Language lang) {
        !           143:        size_t known_length=helper_length?helper_length:strlen(str);
        !           144:        if(!known_length)
        !           145:                return *this;
1.5       paf       146: 
1.174   ! paf       147:        body.append_strdup_know_length(str, known_length);
        !           148:        fragments+=Fragment(lang, known_length);
1.33      paf       149: 
1.174   ! paf       150:        ASSERT_STRING_INVARIANT(*this);
        !           151:        return *this;
1.5       paf       152: }
1.46      paf       153: 
1.174   ! paf       154: /// @todo check in doc: whether it documents NOW bad situation "abc".mid(-1, 3) =were?="ab"
        !           155: String& String::mid(size_t substr_begin, size_t substr_end) const {
        !           156:        String& result=*new String;
        !           157: 
        !           158:        size_t self_length=length();
        !           159:        substr_begin=min(substr_begin, self_length);
        !           160:        substr_end=min(max(substr_end, substr_begin), self_length);
        !           161:        if(substr_begin==substr_end)
1.107     parser    162:                return result;
1.53      paf       163: 
1.174   ! paf       164:        // first: letters themselves
        !           165:        result.body=body.mid(substr_begin, substr_end-substr_begin);
        !           166: 
        !           167:        // next: their langs
        !           168:        result.fragments.append_positions(fragments, substr_begin, substr_end);
        !           169: 
        !           170: //     SAPI::log("piece of '%s' from %d to %d is '%s'",
        !           171:                //cstr(), substr_begin, substr_end, result.cstr());
        !           172:        ASSERT_STRING_INVARIANT(result);
1.53      paf       173:        return result;
1.54      paf       174: }
                    175: 
1.174   ! paf       176: size_t String::pos(const StringBody substr, 
        !           177:                   size_t this_offset, Language lang) const {
        !           178:        // first: letters themselves
        !           179:        size_t substr_begin=body.pos(substr, this_offset);
        !           180:        if(substr_begin==CORD_NOT_FOUND)
        !           181:                return STRING_NOT_FOUND;
        !           182: 
        !           183:        // next: check the lang when specified
        !           184: 
        !           185:        if(lang==L_UNSPECIFIED) // ignore lang?
        !           186:                return substr_begin;
        !           187: 
        !           188:        // substr must be in one fragment, and fragments' lang must = lang
        !           189:        size_t substr_end=substr_begin+substr.length();
        !           190:        size_t fragment_begin=0;
        !           191:        size_t fragment_end;
        !           192:        for(Array_iterator<ArrayFragment::element_type> i(fragments); i.has_next(); fragment_begin=fragment_end) {
        !           193:                const Fragment fragment=i.next();
        !           194:                fragment_end=fragment_begin+fragment.length;
        !           195: 
        !           196:                if(substr_begin<fragment_begin) // not reached fragments which may include 'result'?
        !           197:                        continue;
        !           198:                if(substr_begin>=fragment_end) // begin of substr OUT of current fragment?
        !           199:                        continue;
        !           200:                
        !           201:                if(substr_end>fragment_end) // end of substr OUT of current fragment?
        !           202:                        throw Exception(0, // (*) see below
        !           203:                                this,
        !           204:                                "searching for '%s' starting from %ud problem: found begin in one fragment, but end in another",
        !           205:                                        substr.cstr(), this_offset);
        !           206: 
        !           207:                if(fragment.lang<=lang)
        !           208:                        return substr_begin;
        !           209:                else { // bad lang...
        !           210:                        /// WARNING: this possibly skips assert (*), but it's fast
        !           211:                        substr_begin=body.pos(substr, fragment_end/*...search AFTER for more*/);
        !           212:                        if(substr_begin==CORD_NOT_FOUND)
        !           213:                                return STRING_NOT_FOUND;
        !           214: 
        !           215:                        size_t substr_end=substr_begin+substr.length();
        !           216:                        // and continuing with next fragment
        !           217:                }
        !           218:        }       
        !           219: 
        !           220:        return STRING_NOT_FOUND;
1.58      paf       221: }
                    222: 
1.174   ! paf       223: size_t String::pos(const String& substr, 
        !           224:                                size_t this_offset, Language lang) const {
        !           225:        return pos(substr.body, this_offset, lang);
1.60      paf       226: }
                    227: 
1.174   ! paf       228: void String::split(ArrayString& result, 
        !           229:                   size_t& pos_after, 
        !           230:                   const char* delim, 
        !           231:                   Language lang, int limit) const {
        !           232:        size_t self_length=length();
        !           233:        if(size_t delim_length=strlen(delim)) {
1.60      paf       234:                int pos_before;
                    235:                // while we have 'delim'...
1.174   ! paf       236:                for(; (pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND && limit; limit--) {
1.69      paf       237:                        result+=&mid(pos_after, pos_before);
1.174   ! paf       238:                        pos_after=pos_before+delim_length;
1.60      paf       239:                }
                    240:                // last piece
1.174   ! paf       241:                if(pos_after<self_length && limit) {
        !           242:                        result+=&mid(pos_after, self_length);
        !           243:                        pos_after=self_length;
1.60      paf       244:                }
                    245:        } else { // empty delim
                    246:                result+=this;
1.174   ! paf       247:                pos_after+=self_length;
1.60      paf       248:        }
                    249: }
                    250: 
1.174   ! paf       251: void String::split(ArrayString& result, 
        !           252:                   size_t& pos_after, 
        !           253:                   const String& delim, Language lang, 
        !           254:                   int limit) const {
1.140     paf       255:        if(!delim.is_empty()) {
1.60      paf       256:                int pos_before;
                    257:                // while we have 'delim'...
1.174   ! paf       258:                for(; (pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND && limit; limit--) {
1.69      paf       259:                        result+=&mid(pos_after, pos_before);
1.174   ! paf       260:                        pos_after=pos_before+delim.length();
1.60      paf       261:                }
                    262:                // last piece
1.174   ! paf       263:                if(pos_after<length() && limit) {
        !           264:                        result+=&mid(pos_after, length());
        !           265:                        pos_after=length();
1.60      paf       266:                }
                    267:        } else { // empty delim
                    268:                result+=this;
1.174   ! paf       269:                pos_after+=length();
1.60      paf       270:        }
1.61      paf       271: }
                    272: 
1.174   ! paf       273: static void regex_options(const String* options, int *result, bool& need_pre_post_match){
1.63      paf       274:     struct Regex_option {
1.174   ! paf       275:                const char* keyL;
        !           276:                const char* keyU;
1.63      paf       277:                int clear, set;
                    278:                int *result;
1.154     paf       279:                bool *flag;
1.63      paf       280:     } regex_option[]={
1.153     paf       281:                {"i", "I", 0, PCRE_CASELESS, result}, // a=A
                    282:                {"s", "S", 0, PCRE_DOTALL, result}, // \n\n$ [default]
                    283:                {"x", "U", 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
                    284:                {"m", "M", PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
                    285:                {"g", "G", 0, true, result+1}, // many rows
1.154     paf       286:                {"'", 0, 0, 0, 0, &need_pre_post_match},
                    287:                {0}
1.63      paf       288:     };
1.171     paf       289:        result[0]=PCRE_EXTRA | PCRE_DOTALL | PCRE_DOLLAR_ENDONLY;
1.63      paf       290:        result[1]=0;
                    291: 
1.174   ! paf       292:     if(options && !options->is_empty()) 
1.153     paf       293:                for(Regex_option *o=regex_option; o->keyL; o++) 
1.174   ! paf       294:                        if(options->pos(o->keyL)!=STRING_NOT_FOUND
        !           295:                                || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND)) {
1.154     paf       296:                                if(o->flag)
                    297:                                        *o->flag=true;
                    298:                                else { // result
                    299:                                        *o->result &= ~o->clear;
                    300:                                        *o->result |= o->set;
                    301:                                }
1.63      paf       302:                        }
                    303: }
                    304: 
1.174   ! paf       305: Table* String::match(Charset& source_charset,
        !           306:                     const String& regexp, 
        !           307:                     const String* options,
        !           308:                     Row_action row_action, void *info,
        !           309:                     bool& just_matched) const { 
1.140     paf       310:        if(regexp.is_empty())
1.149     paf       311:                throw Exception(0,
1.174   ! paf       312:                        0,
1.73      paf       313:                        "regexp is empty");
1.154     paf       314: 
1.174   ! paf       315:        const char* pattern=regexp.cstr();
        !           316:        const char* errptr;
1.62      paf       317:        int erroffset;
1.173     paf       318:        bool need_pre_post_match=false;
1.174   ! paf       319:        int option_bits[2]={0};  regex_options(options, option_bits, need_pre_post_match);
        !           320:        bool global=option_bits[1]!=0;
1.63      paf       321:        pcre *code=pcre_compile(pattern, option_bits[0], 
1.62      paf       322:                &errptr, &erroffset,
1.174   ! paf       323:                source_charset.pcre_tables);
1.62      paf       324: 
1.67      paf       325:        if(!code)
1.149     paf       326:                throw Exception(0,
1.174   ! paf       327:                        &regexp.mid(erroffset, regexp.length()),
1.74      paf       328:                        "regular expression syntax error - %s", errptr);
1.62      paf       329:        
1.174   ! paf       330:        int subpatterns=pcre_info(code, 0, 0);
        !           331:        if(subpatterns<0) {
1.100     parser    332:                pcre_free(code);
1.149     paf       333:                throw Exception(0,
1.174   ! paf       334:                        &regexp,
1.76      paf       335:                        "pcre_info error (%d)", 
1.174   ! paf       336:                                subpatterns);
1.63      paf       337:        }
                    338: 
1.174   ! paf       339:        const char* subject=cstr();
        !           340:        size_t subject_length=strlen(subject);
        !           341:        const int oveclength=(1/*match*/+MAX_MATCH_GROUPS)*3;
        !           342:        int ovector[oveclength];
1.155     paf       343: 
                    344:        // create table
1.173     paf       345:        Table::Action_options table_options;
1.174   ! paf       346:        Table& table=*new Table(string_match_table_template, table_options);
1.63      paf       347: 
1.64      paf       348:        int exec_option_bits=0;
1.154     paf       349:        int prestart=0;
                    350:        int poststart=0;
1.174   ! paf       351:        int postfinish=length();
1.63      paf       352:        while(true) {
                    353:                int exec_substrings=pcre_exec(code, 0,
1.174   ! paf       354:                        subject, subject_length, prestart,
        !           355:                        exec_option_bits, ovector, oveclength);
1.63      paf       356:                
                    357:                if(exec_substrings==PCRE_ERROR_NOMATCH) {
1.100     parser    358:                        pcre_free(code);
1.174   ! paf       359:                        row_action(table, 0/*last time, no raw*/, 0, 0, poststart, postfinish, info);
        !           360:                        if(global || subpatterns)
        !           361:                                return &table; // global or with subpatterns=true+result
        !           362:                        else {
        !           363:                                just_matched=false; return 0; // not global=no result
        !           364:                        }
1.63      paf       365:                }
                    366: 
                    367:                if(exec_substrings<0) {
1.100     parser    368:                        pcre_free(code);
1.149     paf       369:                        throw Exception(0,
1.174   ! paf       370:                                &regexp,
1.76      paf       371:                                "regular expression execute error (%d)", 
1.63      paf       372:                                        exec_substrings);
                    373:                }
                    374: 
1.154     paf       375:                int prefinish=ovector[0];
                    376:                poststart=ovector[1];
1.174   ! paf       377:                ArrayString* row=new ArrayString;
        !           378:                if(need_pre_post_match) {
        !           379:                        *row+=&mid(0, prefinish); // .prematch column value
        !           380:                        *row+=&mid(prefinish, poststart); // .match
        !           381:                        *row+=&mid(poststart, postfinish); // .postmatch
        !           382:                } else {
        !           383:                        *row+=0; // .prematch column value
        !           384:                        *row+=0; // .match
        !           385:                        *row+=0; // .postmatch
        !           386:                }
1.63      paf       387:                
                    388:                for(int i=1; i<exec_substrings; i++) {
1.69      paf       389:                        // -1:-1 case handled peacefully by mid() itself
1.174   ! paf       390:                        *row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value
1.63      paf       391:                }
                    392:                
1.174   ! paf       393:                row_action(table, row, prestart, prefinish, poststart, postfinish, info);
1.63      paf       394: 
1.174   ! paf       395:                if(!global || prestart==poststart) { // not global | going to hang
1.100     parser    396:                        pcre_free(code);
1.174   ! paf       397:                        row_action(table, 0/*last time, no row*/, 0, 0, poststart, postfinish, info);
        !           398:                        return &table;
1.63      paf       399:                }
1.154     paf       400:                prestart=poststart;
1.63      paf       401: 
                    402: /*
                    403:                if(option_bits[0] & PCRE_MULTILINE)
1.64      paf       404:                        exec_option_bits|=PCRE_NOTBOL; // start of subject+startoffset not BOL
1.63      paf       405: */
                    406:        }
1.82      parser    407: }
                    408: 
1.174   ! paf       409: String& String::change_case(Charset& source_charset, Change_case_kind kind) const {
        !           410:        String& result=*new String();
        !           411:        if(is_empty())
        !           412:                return result;
        !           413: 
        !           414:        const unsigned char *tables=source_charset.pcre_tables;
1.82      parser    415: 
                    416:        const unsigned char *a;
                    417:        const unsigned char *b;
                    418:        switch(kind) {
                    419:        case CC_UPPER:
                    420:                a=tables+lcc_offset;
                    421:                b=tables+fcc_offset;
                    422:                break;
                    423:        case CC_LOWER:
                    424:                a=tables+lcc_offset;
                    425:                b=0;
                    426:                break;
                    427:        default:
1.149     paf       428:                throw Exception(0, 
1.82      parser    429:                        this, 
                    430:                        "unknown change case kind #%d", 
                    431:                                static_cast<int>(kind)); // never
                    432:                a=b=0; // calm, compiler
                    433:                break; // never
                    434:        }       
                    435: 
1.174   ! paf       436:        char* new_cstr=cstrm();
        !           437:        char *dest=new_cstr;
        !           438:        unsigned char index;
        !           439:        for(const char* current=new_cstr; index=(unsigned char)*current; current++) {
        !           440:                unsigned char c=a[index];
        !           441:                if(b)
        !           442:                        c=b[c];
1.82      parser    443: 
1.174   ! paf       444:                *dest++=(char)c;
        !           445:        }
        !           446:        result.body=new_cstr;
        !           447:        result.fragments.append(fragments);
1.89      parser    448: 
1.101     parser    449:        return result;
                    450: }
                    451: 
1.174   ! paf       452: const String& String::replace(const Dictionary& dict) const {
        !           453:        String& result=*new String();
        !           454:        const char* old_cstr=cstr();
        !           455:        const char* prematch_begin=old_cstr;
        !           456: 
        !           457:        const char* current=old_cstr;
        !           458:        while(*current) {
        !           459:                if(Table::element_type row=dict.first_that_begins(current)) {
        !           460:                        // prematch
        !           461:                        if(size_t prematch_length=current-prematch_begin) {
        !           462:                                result.body.append_strdup_know_length(prematch_begin, prematch_length);
        !           463:                                result.fragments.append_positions(fragments, prematch_begin-old_cstr, current-old_cstr);
1.101     parser    464:                        }
                    465: 
1.174   ! paf       466:                        // match
1.170     paf       467: 
1.174   ! paf       468:                        const String* a=row->get(0);
        !           469:                        // skip 'a' in 'current'; move prematch_begin
        !           470:                        current+=a->length(); prematch_begin=current;
        !           471: 
        !           472:                        if(row->count()>1) { // are there any b?
        !           473:                                const String* b=row->get(1);
        !           474:                                result<<*b;
        !           475:                        }
        !           476:                } else // simply advance
        !           477:                        current++; 
        !           478:        }
1.156     paf       479: 
1.174   ! paf       480:        // postmatch
        !           481:        if(size_t postmatch_length=current-prematch_begin) {
        !           482:                result.body.append_strdup_know_length(prematch_begin, postmatch_length);
        !           483:                result.fragments.append_positions(fragments, prematch_begin-old_cstr, current-old_cstr);
        !           484:        }
1.156     paf       485: 
1.174   ! paf       486:        ASSERT_STRING_INVARIANT(result);
1.89      parser    487:        return result;
                    488: }
                    489: 
1.90      parser    490: double String::as_double() const { 
1.89      parser    491:        double result;
1.174   ! paf       492:        const char *str=cstr();
        !           493: 
        !           494:        while(*str && isspace(*str))
        !           495:                str++;
        !           496:        if(!*str)
1.162     paf       497:                return 0;
1.161     paf       498: 
1.102     parser    499:        char *error_pos;
1.89      parser    500:        // 0xABC
1.174   ! paf       501:        if(str[0]=='0')
        !           502:                if(str[1]=='x' || str[1]=='X')
        !           503:                        result=(double)(unsigned long)strtol(str, &error_pos, 0);
1.99      parser    504:                else
1.174   ! paf       505:                        result=(double)strtod(str+1/*skip leading 0*/, &error_pos);
1.89      parser    506:        else
1.174   ! paf       507:                result=(double)strtod(str, &error_pos);
1.89      parser    508: 
1.159     paf       509:        while(char c=*error_pos++)
                    510:                if(!isspace(c))
                    511:                        throw Exception("number.format",
                    512:                                this,
                    513:                                "invalid number (double)");
1.89      parser    514: 
                    515:        return result;
                    516: }
1.90      parser    517: int String::as_int() const { 
1.89      parser    518:        int result;
1.174   ! paf       519:        const char *str=cstr();
        !           520: 
        !           521:        while(*str && isspace(*str))
        !           522:                str++;
        !           523:        if(!*str)
1.162     paf       524:                return 0;
1.161     paf       525: 
1.102     parser    526:        char *error_pos;
1.89      parser    527:        // 0xABC
1.174   ! paf       528:        if(str[0]=='0')
        !           529:                if(str[1]=='x' || str[1]=='X')
        !           530:                        result=(int)(unsigned long)strtol(str, &error_pos, 0);
1.99      parser    531:                else
1.174   ! paf       532:                        result=(int)strtol(str+1/*skip leading 0*/, &error_pos, 0);
1.89      parser    533:        else
1.174   ! paf       534:                result=(int)strtol(str, &error_pos, 0);
1.89      parser    535: 
1.159     paf       536:        while(char c=*error_pos++)
                    537:                if(!isspace(c))
                    538:                        throw Exception("number.format",
                    539:                                this,
                    540:                                "invalid number (int)");
1.82      parser    541: 
                    542:        return result;
1.61      paf       543: }
1.113     parser    544: 
1.174   ! paf       545: inline void uint2uchars(uint word, uchar *bytes) {
        !           546:        bytes[0]=word&0xFF;
        !           547:        bytes[1]=(word>>8)&0xFF;
        !           548:        bytes[2]=(word>>16)&0xFF;
        !           549:        bytes[3]=(word>>24)&0xFF;
        !           550: }
        !           551: inline uint uchars2uint(uchar *bytes) {
        !           552:        return bytes[3]<<24
        !           553:                | bytes[2]<<16
        !           554:                | bytes[1]<<8
        !           555:                | bytes[0];
        !           556: }
        !           557: 
        !           558: static int serialize_body_piece(const char* s, char** cur) {
        !           559:        size_t length=strlen(s);
        !           560:        memcpy(*cur, s, length);  *cur+=length;
        !           561:        return 0;
        !           562: };
        !           563: String::Cm String::serialize(size_t prolog_length) const {
        !           564:        size_t buf_length=
        !           565:                prolog_length
        !           566:                +fragments.count()*(sizeof(Language)+sizeof(size_t))
        !           567:                +length();
        !           568:        String::Cm result(new(PointerFreeGC) char[buf_length], buf_length);
        !           569: 
        !           570:        // 1: prolog
        !           571:        char *cur=result.str+prolog_length;
        !           572: 
        !           573: 
        !           574:        // 2: fragments.count
        !           575:        size_t fragments_count=fragments.count();
        !           576:        memcpy(cur, &fragments_count, sizeof(fragments_count));  cur+=sizeof(fragments_count);
        !           577: 
        !           578:        // 3: lang info
        !           579:        for(Array_iterator<ArrayFragment::element_type> i(fragments); i.has_next(); ) {
        !           580:                const Fragment fragment=i.next();
        !           581:                // lang
        !           582:                memcpy(cur, &fragment.lang, sizeof(fragment.lang));  cur+=sizeof(fragment.lang);
        !           583:                // length
        !           584:                memcpy(cur, &fragment.length, sizeof(fragment.length));  cur+=sizeof(fragment.length);
        !           585:        }
        !           586: 
        !           587:        // 4: letters
        !           588:        body.for_each(serialize_body_piece, &cur);
1.113     parser    589: 
1.174   ! paf       590:        return result;
1.113     parser    591: }
1.174   ! paf       592: bool String::deserialize(size_t prolog_length, void *buf, size_t buf_length) {
        !           593:        if(buf_length<=prolog_length)
1.148     paf       594:                return false;
1.174   ! paf       595:        buf_length-=prolog_length;
1.135     paf       596: 
1.174   ! paf       597:        // 1: prolog
        !           598:        const char* cur=(const char* )buf+prolog_length;
1.113     parser    599: 
1.174   ! paf       600:        // 2: fragments.count
        !           601:        if(buf_length<sizeof(size_t)) // fragments.count don't fit?
        !           602:                return false;
        !           603:        size_t fragments_count=*reinterpret_cast<const size_t*>(cur);  cur+=sizeof(size_t);
        !           604:        buf_length-=sizeof(size_t);
        !           605:        
        !           606:        if(fragments_count) {
        !           607:                // 3: lang info
        !           608:                size_t total_length=0;
        !           609:                for(size_t f=0; f<fragments_count; f++) {
        !           610:                        size_t piece_length=sizeof(Language)+sizeof(size_t);
        !           611:                        if(buf_length<piece_length) // lang+length
        !           612:                                return false;
        !           613: 
        !           614:                        Language lang=*reinterpret_cast<const Language *>(cur);  cur+=sizeof(Language);
        !           615:                        size_t fragment_length=*reinterpret_cast<const size_t*>(cur);  cur+=sizeof(size_t);
        !           616:                        fragments+=Fragment(lang, fragment_length);
        !           617:                        total_length+=fragment_length;
1.148     paf       618: 
1.174   ! paf       619:                        buf_length-=piece_length;
        !           620:                }
1.128     paf       621: 
1.174   ! paf       622:                // 4: letters
        !           623:                if(buf_length!=total_length)
1.148     paf       624:                        return false;
                    625: 
1.174   ! paf       626:                body=StringBody(cur, buf_length);
        !           627:        }
1.113     parser    628: 
1.174   ! paf       629:        ASSERT_STRING_INVARIANT(*this);
1.148     paf       630:        return true;
1.113     parser    631: }

E-mail: