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

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

E-mail: