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

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

E-mail: