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

1.45      paf         1: /** @file
1.174     paf         2:        Parser: string class. @see untalength_t.C.
1.46      paf         3: 
1.266     moko        4:        Copyright (c) 2001-2020 Art. Lebedev Studio (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.12      paf         8: #include "pa_string.h"
1.22      paf         9: #include "pa_exception.h"
1.61      paf        10: #include "pa_table.h"
1.101     parser     11: #include "pa_dictionary.h"
1.132     paf        12: #include "pa_charset.h"
1.222     misha      13: #include "pa_vregex.h"
1.215     misha      14: 
1.268   ! moko       15: volatile const char * IDENT_PA_STRING_C="$Id: pa_string.C,v 1.267 2021/12/28 15:58:31 moko Exp $" IDENT_PA_STRING_H;
1.240     moko       16: 
1.185     paf        17: const String String::Empty;
                     18: 
1.262     moko       19: #define COMPILE_ASSERT(x) extern int assert_checker[(x) ? 1 : -1]
1.261     moko       20: COMPILE_ASSERT(sizeof(String::Languages) == sizeof(CORD));
                     21: 
1.242     moko       22: // pa_atoui is based on Manuel Novoa III _strto_l for uClibc
                     23: 
1.249     moko       24: template<typename T> inline T pa_ato_any(const char *str, int base, const String* problem_source,const T max){
                     25:        T result = 0;
1.242     moko       26:        const char *pos = str;
                     27: 
                     28:        while (isspace(*pos)) /* skip leading whitespace */
                     29:                ++pos;
                     30: 
                     31:        if (base == 16 && *pos == '0') { /* handle option prefix */
                     32:                ++pos;
                     33:                if (*pos == 'x' || *pos == 'X') {
                     34:                        ++pos;
                     35:                }
                     36:        }
                     37: 
                     38:        if (base == 0) { /* dynamic base */
                     39:                base = 10; /* default is 10 */
                     40:                if (*pos == '0') {
                     41:                        ++pos;
1.245     moko       42:                        if (*pos == 'x' || *pos == 'X'){
1.242     moko       43:                                ++pos;
                     44:                                base=16;
1.245     moko       45:                        }
1.242     moko       46:                }
                     47:        }
                     48: 
                     49:        if (base < 2 || base > 16) { /* illegal base */
                     50:                throw Exception(PARSER_RUNTIME, 0, "base to must be an integer from 2 to 16");
                     51:        }
                     52: 
1.249     moko       53:        T cutoff = max / base;
                     54:        int cutoff_digit = (int)(max - cutoff * base);
1.242     moko       55: 
                     56:        while(true) {
                     57:                int digit;
                     58:                
                     59:                if ((*pos >= '0') && (*pos <= '9')) {
                     60:                        digit = (*pos - '0');
                     61:                } else if (*pos >= 'a') {
                     62:                        digit = (*pos - 'a' + 10);
                     63:                } else if (*pos >= 'A') {
                     64:                        digit = (*pos - 'A' + 10);
                     65:                } else break;
                     66: 
                     67:                if (digit >= base) {
                     68:                        break;
                     69:                }
                     70:                
                     71:                ++pos;
                     72:                
                     73:                /* adjust number, with overflow check */
                     74:                if ((result > cutoff) || ((result == cutoff) && (digit > cutoff_digit))) {
                     75:                        throw Exception("number.format", problem_source, problem_source ? "out of range (int)" : "'%s' is out of range (int)", str);
                     76:                } else {
                     77:                        result  = result * base + digit;
                     78:                }
                     79:        }
                     80: 
                     81:        while(char c=*pos++)
                     82:                if(!isspace(c))
                     83:                        throw Exception("number.format", problem_source, problem_source ? "invalid number (int)" : "'%s' is invalid number (int)", str);
                     84: 
                     85:        return result;
                     86: }
                     87: 
1.249     moko       88: unsigned int pa_atoui(const char *str, int base, const String* problem_source){
1.263     moko       89:        if(!str)
                     90:                return 0;
                     91: 
1.254     moko       92:        return pa_ato_any<unsigned int>(str, base, problem_source, UINT_MAX);
1.249     moko       93: }
                     94: 
1.265     moko       95: uint64_t pa_atoul(const char *str, int base, const String* problem_source){
1.263     moko       96:        if(!str)
                     97:                return 0;
                     98: 
1.265     moko       99:        return pa_ato_any<uint64_t>(str, base, problem_source, ULLONG_MAX);
1.249     moko      100: }
                    101: 
1.264     moko      102: int pa_atoi(const char* str, int base, const String* problem_source) {
1.193     paf       103:        if(!str)
                    104:                return 0;
                    105: 
1.242     moko      106:        while(isspace(*str))
1.193     paf       107:                str++;
1.242     moko      108: 
1.193     paf       109:        if(!*str)
                    110:                return 0;
                    111: 
1.200     paf       112:        bool negative=false;
                    113:        if(str[0]=='-') {
                    114:                negative=true;
                    115:                str++;
                    116:        } else if(str[0]=='+') {
                    117:                str++;
                    118:        }
1.193     paf       119: 
1.264     moko      120:        unsigned int result=pa_atoui(str, base, problem_source);
1.193     paf       121: 
1.242     moko      122:        if(negative && result <= ((unsigned int)(-(1+INT_MIN)))+1)
1.243     moko      123:                return -(int)result;
1.242     moko      124:        
                    125:        if(result<=INT_MAX)
1.243     moko      126:                return (int)result;
1.242     moko      127:        
                    128:        throw Exception("number.format", problem_source, problem_source ? "out of range (int)" : "'%s' is out of range (int)", str);
1.193     paf       129: }
                    130: 
                    131: double pa_atod(const char* str, const String* problem_source) {
                    132:        if(!str)
                    133:                return 0;
                    134: 
1.242     moko      135:        while(isspace(*str))
1.193     paf       136:                str++;
1.242     moko      137: 
1.193     paf       138:        if(!*str)
                    139:                return 0;
                    140: 
1.200     paf       141:        bool negative=false;
                    142:        if(str[0]=='-') {
                    143:                negative=true;
                    144:                str++;
                    145:        } else if(str[0]=='+') {
                    146:                str++;
                    147:        }
1.242     moko      148: 
                    149:        double result;
1.247     moko      150:        if(str[0]=='0') {
                    151:                if(str[1]=='x' || str[1]=='X') {
1.242     moko      152:                        // 0xABC
1.249     moko      153:                        result=(double)pa_atoul(str, 0, problem_source);
1.242     moko      154:                        return negative ? -result : result;
                    155:                } else {
1.200     paf       156:                         // skip leading 0000, to disable octal interpretation
1.242     moko      157:                        do str++; while(*str=='0');
1.200     paf       158:                }
1.247     moko      159:        }
1.242     moko      160: 
                    161:        char *error_pos;
                    162:        result=strtod(str, &error_pos);
1.193     paf       163: 
                    164:        while(char c=*error_pos++)
1.199     paf       165:                if(!isspace((unsigned char)c))
1.242     moko      166:                        throw Exception("number.format", problem_source, problem_source ? "invalid number (double)" : "'%s' is invalid number (double)", str);
1.193     paf       167: 
1.242     moko      168:        return negative ? -result : result;
1.193     paf       169: }
                    170: 
1.176     paf       171: // cord lib extension
                    172: 
                    173: #ifndef DOXYGEN
                    174: typedef struct {
1.254     moko      175:        ssize_t countdown;
                    176:        int target;     /* Character we're looking for  */
1.176     paf       177: } chr_data;
                    178: #endif
1.248     moko      179: 
1.176     paf       180: static int CORD_range_contains_chr_greater_then_proc(char c, size_t size, void* client_data)
                    181: {
1.254     moko      182:        register chr_data * d = (chr_data *)client_data;
                    183: 
                    184:        if (d -> countdown<=0) return(2);
                    185:        d -> countdown -= size;
                    186:        if (c > d -> target) return(1);
                    187:        return(0);
1.176     paf       188: }
1.248     moko      189: 
1.176     paf       190: int CORD_range_contains_chr_greater_then(CORD x, size_t i, size_t n, int c)
                    191: {
1.254     moko      192:        chr_data d;
1.176     paf       193: 
1.254     moko      194:        d.countdown = n;
                    195:        d.target = c;
                    196:        return(CORD_block_iter(x, i, CORD_range_contains_chr_greater_then_proc, &d) == 1/*alternatives: 0 normally ended, 2=struck 'n'*/);
1.176     paf       197: }
                    198: 
1.187     paf       199: static int CORD_block_count_proc(char /*c*/, size_t /*size*/, void* client_data)
1.178     paf       200: {
1.254     moko      201:        int* result=(int*)client_data;
                    202:        (*result)++;
                    203:        return(0); // 0=continue
1.178     paf       204: }
1.248     moko      205: 
1.178     paf       206: size_t CORD_block_count(CORD x)
                    207: {
                    208:        size_t result=0;
                    209:        CORD_block_iter(x, 0, CORD_block_count_proc, &result);
1.254     moko      210:        return result;
1.178     paf       211: }
                    212: 
1.174     paf       213: // helpers
1.139     paf       214: 
1.174     paf       215: /// String::match uses this as replace & global search table columns
1.139     paf       216: 
1.174     paf       217: const int MAX_MATCH_GROUPS=100;
1.139     paf       218: 
1.174     paf       219: class String_match_table_template_columns: public ArrayString {
                    220: public:
                    221:        String_match_table_template_columns() {
                    222:                *this+=new String("prematch");
                    223:                *this+=new String("match");
                    224:                *this+=new String("postmatch");
                    225:                for(int i=0; i<MAX_MATCH_GROUPS; i++) {
1.176     paf       226:                        *this+=new String(String::Body::Format(1+i), String::L_CLEAN);
1.174     paf       227:                }
                    228:        }
                    229: };
                    230: 
                    231: Table string_match_table_template(new String_match_table_template_columns);
                    232: 
1.176     paf       233: // String::Body methods
1.140     paf       234: 
1.176     paf       235: String::Body String::Body::Format(int value) {
1.174     paf       236:        char local[MAX_NUMBER];
                    237:        size_t length=snprintf(local, MAX_NUMBER, "%d", value);
1.224     misha     238:        return String::Body(pa_strdup(local, length));
1.120     paf       239: }
                    240: 
1.254     moko      241: String::Body String::Body::trim(String::Trim_kind kind, const char* chars, size_t* out_start, size_t* out_length, Charset* source_charset) const {
1.195     paf       242:        size_t our_length=length();
                    243:        if(!our_length)
                    244:                return *this;
1.229     misha     245: 
                    246:        // check if any UTF-8 in chars
                    247:        bool fast=true;
                    248:        if(chars && source_charset && source_charset->isUTF8()){
                    249:                const char* pos=chars;
                    250:                while(unsigned char c=*pos++)
                    251:                        if(c>127){
                    252:                                fast=false;
                    253:                                break;
                    254:                        }
                    255:        }
                    256: 
                    257:        size_t start=0;
                    258:        size_t end=our_length;
1.195     paf       259:        if(!chars)
                    260:                chars=" \t\n"; // white space
                    261: 
1.229     misha     262:        if(fast){
                    263:                // from left...
                    264:                if(kind!=TRIM_END) {
                    265:                        CORD_pos pos; set_pos(pos, 0);
                    266:                        while(true) {
                    267:                                char c=CORD_pos_fetch(pos);
                    268:                                if(strchr(chars, c)) {
                    269:                                        if(++start==our_length)
                    270:                                                return 0; // all chars are empty, just return empty string
                    271:                                } else
                    272:                                        break;                  
                    273: 
                    274:                                CORD_next(pos);
                    275:                        }
                    276:                }
                    277: 
                    278:                // from right..
                    279:                if(kind!=TRIM_START) {
                    280:                        CORD_pos pos; set_pos(pos, end-1);
                    281:                        while(true) {
                    282:                                char c=CORD_pos_fetch(pos);
                    283:                                if(strchr(chars, c)) {
                    284:                                        if(--end==0) // optimization: NO need to check for 'end>=start', that's(<) impossible
                    285:                                                return 0; // all chars are empty, just return empty string
                    286:                                } else
                    287:                                        break;                  
                    288: 
                    289:                                CORD_prev(pos);
                    290:                        }
                    291:                }
                    292:        } else {
1.234     misha     293:                const XMLByte* src_begin=(const XMLByte*)cstr();
1.229     misha     294:                const XMLByte* src_end=src_begin+our_length;
                    295: 
                    296:                // from left...
                    297:                if(kind!=TRIM_END) {
                    298:                        while(src_begin<src_end){
                    299:                                uint char_length=1;
                    300:                                const XMLByte* ptr=src_begin;
1.231     misha     301:                                // searching first UTF-8 byte: http://tools.ietf.org/html/rfc3629#section-3
                    302:                                while(++src_begin<=src_end && (*src_begin>127 && *src_begin<0xC0))
1.229     misha     303:                                        char_length++;
                    304: 
                    305:                                bool found=false;
                    306:                                for(const char* chars_byte=chars; chars_byte=strchr(chars_byte, *ptr); chars_byte++)
                    307:                                        if(strncmp(chars_byte, (const char*)ptr, char_length)==0){
                    308:                                                found=true;
                    309:                                                break;
                    310:                                        }
                    311: 
                    312:                                if(found){
                    313:                                        start+=char_length;
                    314:                                        if(start==our_length)
                    315:                                                return 0; // all chars are empty, just return empty string
                    316:                                } else
                    317:                                        break;
                    318:                        }
                    319:                }
1.196     paf       320: 
1.229     misha     321:                // from right..
                    322:                if(kind!=TRIM_START) {
                    323:                        while(src_begin<src_end){
                    324:                                uint char_length=1;
1.231     misha     325:                                // searching first UTF-8 byte: http://tools.ietf.org/html/rfc3629#section-3
                    326:                                while(src_begin<=--src_end && (*src_end>127 && *src_end<0xC0))
1.229     misha     327:                                        char_length++;
                    328: 
                    329:                                bool found=false;
                    330:                                for(const char* chars_byte=chars; chars_byte=strchr(chars_byte, *src_end); chars_byte++)
                    331:                                        if(strncmp(chars_byte, (const char*)src_end, char_length)==0){
                    332:                                                found=true;
                    333:                                                break;
                    334:                                        }
                    335: 
                    336:                                if(found){
                    337:                                        end-=char_length;
                    338:                                        if(end==0)
                    339:                                                return 0; // all chars are empty, just return empty string
                    340:                                } else
                    341:                                        break;
                    342:                        }
1.196     paf       343:                }
                    344:        }
1.195     paf       345: 
                    346:        if(start==0 && end==our_length) // nobody moved a thing
                    347:                return *this;
                    348: 
                    349:        if(out_start)
                    350:                *out_start=start;
                    351:        size_t new_length=end-start;
                    352:        if(out_length)
                    353:                *out_length=new_length;
                    354: 
                    355:        return mid(start, new_length);
                    356: }
                    357: 
1.174     paf       358: static int CORD_batched_iter_fn_generic_hash_code(char c, void * client_data) {
                    359:        uint& result=*static_cast<uint*>(client_data);
                    360:        generic_hash_code(result, c);
                    361:        return 0;
                    362: }
1.248     moko      363: 
1.174     paf       364: static int CORD_batched_iter_fn_generic_hash_code(const char*  s, void * client_data) {
                    365:        uint& result=*static_cast<uint*>(client_data);
                    366:        generic_hash_code(result, s);
                    367:        return 0;
1.248     moko      368: }
                    369: 
1.225     misha     370: uint String::Body::get_hash_code() const {
                    371: #ifdef HASH_CODE_CACHING
                    372:        if(hash_code)
                    373:                return hash_code;
                    374: #else
                    375:        uint hash_code=0;
                    376: #endif
1.220     misha     377:        if (body && CORD_IS_STRING(body)){
1.250     moko      378:                generic_hash_code(hash_code, (const char *)body);
1.220     misha     379:        } else {
                    380:                CORD_iter5(body, 0,
                    381:                        CORD_batched_iter_fn_generic_hash_code, 
1.225     misha     382:                        CORD_batched_iter_fn_generic_hash_code, &hash_code);
1.220     misha     383:        }
1.225     misha     384:        return hash_code;
1.94      parser    385: }
                    386: 
1.241     misha     387: struct CORD_pos_info {
                    388:        const char* chars;
                    389:        size_t left;
                    390:        size_t pos;
                    391: };
                    392: 
                    393: // can be called only for IS_FUNCTION(CORD) which is used in String::Body::strrpbrk
                    394: static int CORD_iter_fn_rpos(char c, CORD_pos_info* info) {
                    395:        if(info->pos < info->left){
                    396:                info->pos=STRING_NOT_FOUND;
                    397:                return 1;
                    398:        }
                    399:        if(strchr(info->chars, c))
                    400:                return 1;
                    401:        --(info->pos);
                    402:        return 0;
                    403: }
                    404: 
                    405: size_t String::Body::strrpbrk(const char* chars, size_t left, size_t right) const {
                    406:        if(is_empty() || !chars || !strlen(chars))
                    407:                return STRING_NOT_FOUND;
                    408:        CORD_pos_info info={chars, left, right};
                    409:        if(CORD_riter4(body, right, (CORD_iter_fn)CORD_iter_fn_rpos, &info))
                    410:                return info.pos;
                    411:        else
                    412:                return STRING_NOT_FOUND;
                    413: }
                    414: 
                    415: 
                    416: // can be called only for IS_FUNCTION(CORD) which is used in String::Body::rskipchars
                    417: static int CORD_iter_fn_rskip(char c, CORD_pos_info* info) {
                    418:        if(info->pos < info->left) {
                    419:                info->pos=STRING_NOT_FOUND;
                    420:                return 1;
                    421:        }
                    422:        if(!strchr(info->chars, c))
                    423:                return 1;
                    424:        --(info->pos);
                    425:        return 0;
                    426: }
                    427: 
                    428: size_t String::Body::rskipchars(const char* chars, size_t left, size_t right) const {
                    429:        if(is_empty() || !chars || !strlen(chars))
                    430:                return STRING_NOT_FOUND;
                    431:        CORD_pos_info info={chars, left, right};
                    432:        if(CORD_riter4(body, right, (CORD_iter_fn)CORD_iter_fn_rskip, &info))
                    433:                return info.pos;
                    434:        else
                    435:                return STRING_NOT_FOUND;
                    436: }
                    437: 
1.174     paf       438: // String methods
                    439: 
                    440: String& String::append_know_length(const char* str, size_t known_length, Language lang) {
                    441:        if(!known_length)
1.9       paf       442:                return *this;
1.122     paf       443: 
1.176     paf       444:        // first: langs
                    445:        langs.append(body, lang, known_length);
                    446:        // next: letters themselves
1.174     paf       447:        body.append_know_length(str, known_length);
1.1       paf       448: 
1.174     paf       449:        ASSERT_STRING_INVARIANT(*this);
1.1       paf       450:        return *this;
                    451: }
1.248     moko      452: 
1.174     paf       453: String& String::append_help_length(const char* str, size_t helper_length, Language lang) {
                    454:        if(!str)
                    455:                return *this;
                    456:        size_t known_length=helper_length?helper_length:strlen(str);
                    457:        if(!known_length)
                    458:                return *this;
1.1       paf       459: 
1.174     paf       460:        return append_know_length(str, known_length, lang);
1.5       paf       461: }
1.248     moko      462: 
1.244     moko      463: String::String(int value, const char *format) : langs(L_CLEAN){
1.226     misha     464:        char buf[MAX_NUMBER];
                    465:        body.append_strdup_know_length(buf, snprintf(buf, MAX_NUMBER, format, value));
                    466: }
1.248     moko      467: 
1.174     paf       468: String& String::append_strdup(const char* str, size_t helper_length, Language lang) {
                    469:        size_t known_length=helper_length?helper_length:strlen(str);
                    470:        if(!known_length)
                    471:                return *this;
1.5       paf       472: 
1.176     paf       473:        // first: langs
                    474:        langs.append(body, lang, known_length);
                    475:        // next: letters themselves
1.174     paf       476:        body.append_strdup_know_length(str, known_length);
1.33      paf       477: 
1.174     paf       478:        ASSERT_STRING_INVARIANT(*this);
                    479:        return *this;
1.5       paf       480: }
1.46      paf       481: 
1.234     misha     482: struct CORD_length_info {
                    483:        size_t len;
                    484:        size_t skip;
                    485: };
                    486: 
                    487: int CORD_batched_len(const char* s, CORD_length_info* info){
1.255     moko      488:        info->len += lengthUTF8( (const XMLByte *)s, (const XMLByte *)s+strlen(s));
                    489:        return 0;
1.221     misha     490: }
                    491: 
1.234     misha     492: // can be called only for IS_FUNCTION(CORD) which are used in large String::Body::mid
                    493: int CORD_batched_len(const char c, CORD_length_info* info){
                    494:        if (info->skip==0){
                    495:                info->len++;
                    496:                info->skip = lengthUTF8Char(c)-1;
                    497:        } else {
                    498:                info->skip--;
                    499:        }
1.220     misha     500:        return 0;
                    501: }
                    502: 
1.210     misha     503: size_t String::length(Charset& charset) const {
                    504:        if(charset.isUTF8()){
1.234     misha     505:                CORD_length_info info = {0, 0};
                    506:                body.for_each<CORD_length_info *>(CORD_batched_len, CORD_batched_len, &info);
                    507:                return info.len;
1.210     misha     508:        } else
                    509:                return body.length();
                    510: }
                    511: 
1.174     paf       512: /// @todo check in doc: whether it documents NOW bad situation "abc".mid(-1, 3) =were?="ab"
                    513: String& String::mid(size_t substr_begin, size_t substr_end) const {
                    514:        String& result=*new String;
                    515: 
                    516:        size_t self_length=length();
                    517:        substr_begin=min(substr_begin, self_length);
                    518:        substr_end=min(max(substr_end, substr_begin), self_length);
1.176     paf       519:        size_t substr_length=substr_end-substr_begin;
                    520:        if(!substr_length)
1.107     parser    521:                return result;
1.53      paf       522: 
1.176     paf       523:        // first: their langs
                    524:        result.langs.append(result.body, langs, substr_begin, substr_length);
                    525:        // next: letters themselves
                    526:        result.body=body.mid(substr_begin, substr_length);
1.174     paf       527: 
                    528:        ASSERT_STRING_INVARIANT(result);
1.53      paf       529:        return result;
1.54      paf       530: }
                    531: 
1.211     misha     532: // from, to and helper_length in characters, not in bytes (it's important for utf-8)
                    533: String& String::mid(Charset& charset, size_t from, size_t to, size_t helper_length) const {
1.210     misha     534:        String& result=*new String;
                    535: 
1.255     moko      536:        size_t self_length=helper_length ? helper_length : length(charset);
1.211     misha     537: 
                    538:        if(!self_length)
                    539:                return result;
                    540: 
                    541:        from=min(min(to, from), self_length);
1.210     misha     542:        to=min(max(to, from), self_length);
1.211     misha     543: 
1.210     misha     544:        size_t substr_length=to-from;
1.211     misha     545: 
1.210     misha     546:        if(!substr_length)
                    547:                return result;
                    548: 
                    549:        if(charset.isUTF8()){
1.234     misha     550:                const XMLByte* src_begin=(const XMLByte*)cstr();
1.230     misha     551:                const XMLByte* src_end=src_begin+body.length();
1.210     misha     552: 
1.212     misha     553:                // convert 'from' and 'substr_length' from 'characters' to 'bytes'
1.230     misha     554:                from=getUTF8BytePos(src_begin, src_end, from);
                    555:                substr_length=getUTF8BytePos(src_begin+from, src_end, substr_length);
1.210     misha     556:                if(!substr_length)
                    557:                        return result;
                    558:        }
                    559: 
                    560:        // first: their langs
                    561:        result.langs.append(result.body, langs, from, substr_length);
                    562:        // next: letters themselves
                    563:        result.body=body.mid(from, substr_length);
                    564: 
                    565:        ASSERT_STRING_INVARIANT(result);
                    566:        return result;
                    567: }
                    568: 
1.176     paf       569: size_t String::pos(const String::Body substr, size_t this_offset, Language lang) const {
1.183     paf       570:        size_t substr_length=substr.length();
                    571:        while(true) {
                    572:                size_t substr_begin=body.pos(substr, this_offset);
                    573:                
                    574:                if(substr_begin==CORD_NOT_FOUND)
                    575:                        return STRING_NOT_FOUND;
1.174     paf       576: 
1.183     paf       577:                if(langs.check_lang(lang, substr_begin, substr_length))
                    578:                        return substr_begin;
                    579: 
                    580:                this_offset=substr_begin+substr_length;
                    581:        }
1.58      paf       582: }
                    583: 
1.254     moko      584: size_t String::pos(const String& substr, size_t this_offset, Language lang) const {
1.174     paf       585:        return pos(substr.body, this_offset, lang);
1.60      paf       586: }
                    587: 
1.254     moko      588: size_t String::pos(Charset& charset, const String& substr, size_t this_offset, Language lang) const {
1.210     misha     589: 
                    590:        if(charset.isUTF8()){
1.234     misha     591:                const XMLByte* srcPtr=(const XMLByte*)cstr();
1.212     misha     592:                const XMLByte* srcEnd=srcPtr+body.length();
                    593: 
                    594:                // convert 'this_offset' from 'characters' to 'bytes'
                    595:                this_offset=getUTF8BytePos(srcPtr, srcEnd, this_offset);
                    596: 
1.229     misha     597:                size_t result=pos(substr.body, this_offset, lang);
                    598:                return (result==CORD_NOT_FOUND)
                    599:                        ? STRING_NOT_FOUND
                    600:                        : getUTF8CharPos(srcPtr, srcEnd, result); // convert 'result' from 'bytes' to 'characters'
1.212     misha     601:        } else {
1.229     misha     602:                size_t result=pos(substr.body, this_offset, lang);
                    603:                return (result==CORD_NOT_FOUND)
                    604:                        ? STRING_NOT_FOUND
                    605:                        : result;
1.210     misha     606:        }
                    607: }
                    608: 
1.256     moko      609: void String::split(ArrayString& result, size_t pos_after, const char* delim, Language lang) const {
1.237     misha     610:        if(is_empty())
                    611:                return;
1.174     paf       612:        size_t self_length=length();
1.237     misha     613:        if(size_t delim_length=strlen(delim)) {
1.186     paf       614:                size_t pos_before;
1.60      paf       615:                // while we have 'delim'...
1.256     moko      616:                while((pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND) {
1.69      paf       617:                        result+=&mid(pos_after, pos_before);
1.174     paf       618:                        pos_after=pos_before+delim_length;
1.60      paf       619:                }
                    620:                // last piece
1.256     moko      621:                if(pos_after<self_length)
1.174     paf       622:                        result+=&mid(pos_after, self_length);
1.60      paf       623:        } else { // empty delim
                    624:                result+=this;
                    625:        }
                    626: }
                    627: 
1.256     moko      628: void String::split(ArrayString& result, size_t pos_after, const String& delim, Language lang) const {
1.237     misha     629:        if(is_empty())
                    630:                return;
                    631:        if(!delim.is_empty()) {
1.186     paf       632:                size_t pos_before;
1.60      paf       633:                // while we have 'delim'...
1.256     moko      634:                while((pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND) {
1.69      paf       635:                        result+=&mid(pos_after, pos_before);
1.174     paf       636:                        pos_after=pos_before+delim.length();
1.60      paf       637:                }
                    638:                // last piece
1.256     moko      639:                if(pos_after<length())
1.174     paf       640:                        result+=&mid(pos_after, length());
1.60      paf       641:        } else { // empty delim
                    642:                result+=this;
                    643:        }
1.61      paf       644: }
                    645: 
1.254     moko      646: Table* String::match(VRegex* vregex, Row_action row_action, void *info, int& matches_count) const {
1.209     misha     647: 
1.222     misha     648:        // vregex->info(); // I have no idea what does it for?
1.63      paf       649: 
1.222     misha     650:        bool need_pre_post_match=vregex->is_pre_post_match_needed();
                    651:        bool global=vregex->is_global_search();
1.63      paf       652: 
1.174     paf       653:        const char* subject=cstr();
1.234     misha     654:        size_t subject_length=length();
1.222     misha     655:        const int ovector_size=(1/*match*/+MAX_MATCH_GROUPS)*3;
                    656:        int ovector[ovector_size];
1.155     paf       657: 
1.173     paf       658:        Table::Action_options table_options;
1.174     paf       659:        Table& table=*new Table(string_match_table_template, table_options);
1.63      paf       660: 
1.154     paf       661:        int prestart=0;
                    662:        int poststart=0;
1.174     paf       663:        int postfinish=length();
1.267     moko      664:        int action_was_executed=-1;
1.63      paf       665:        while(true) {
1.222     misha     666:                int exec_result=vregex->exec(subject, subject_length, ovector, ovector_size, prestart);
1.63      paf       667: 
1.222     misha     668:                if(exec_result<0) // only PCRE_ERROR_NOMATCH might be here, other negative results cause an exception
                    669:                        break;
1.63      paf       670: 
1.154     paf       671:                int prefinish=ovector[0];
                    672:                poststart=ovector[1];
1.236     moko      673: 
1.267     moko      674:                if (prestart==poststart && action_was_executed==1){
1.236     moko      675:                        prestart++;
1.267     moko      676:                        action_was_executed=0;
1.236     moko      677:                        continue;
                    678:                }
                    679: 
1.232     misha     680:                ArrayString* row=new ArrayString(3);
1.174     paf       681:                if(need_pre_post_match) {
                    682:                        *row+=&mid(0, prefinish); // .prematch column value
                    683:                        *row+=&mid(prefinish, poststart); // .match
                    684:                        *row+=&mid(poststart, postfinish); // .postmatch
                    685:                } else {
1.185     paf       686:                        *row+=&Empty; // .prematch column value
                    687:                        *row+=&Empty; // .match
                    688:                        *row+=&Empty; // .postmatch
1.174     paf       689:                }
1.63      paf       690:                
1.222     misha     691:                for(int i=1; i<exec_result; i++) {
1.69      paf       692:                        // -1:-1 case handled peacefully by mid() itself
1.232     misha     693:                        *row+=(ovector[i*2+0]>=0 && ovector[i*2+1]>0)?&mid(ovector[i*2+0], ovector[i*2+1]):new String; // .i column value
1.63      paf       694:                }
                    695:                
1.209     misha     696:                matches_count++;
1.267     moko      697:                row_action(table, row, prestart - !action_was_executed, prefinish, poststart, postfinish, info);
1.63      paf       698: 
1.268   ! moko      699:                if(!global || poststart>=subject_length) // last step, avoid prestart++ after last char
1.222     misha     700:                        break;
                    701: 
1.154     paf       702:                prestart=poststart;
1.267     moko      703:                action_was_executed=1;
1.222     misha     704:        }
1.63      paf       705: 
1.222     misha     706:        row_action(table, 0/*last time, no raw*/, 0, 0, poststart, postfinish, info);
                    707:        return vregex->is_just_count() ? 0 : &table;
1.82      parser    708: }
                    709: 
1.174     paf       710: String& String::change_case(Charset& source_charset, Change_case_kind kind) const {
                    711:        String& result=*new String();
                    712:        if(is_empty())
                    713:                return result;
                    714: 
                    715:        char* new_cstr=cstrm();
1.216     misha     716: 
1.181     paf       717:        if(source_charset.isUTF8()) {
1.216     misha     718:                size_t new_cstr_len=length();
1.181     paf       719:                switch(kind) {
                    720:                case CC_UPPER:
1.192     paf       721:                        change_case_UTF8((const XMLByte*)new_cstr, new_cstr_len, (XMLByte*)new_cstr, new_cstr_len, UTF8CaseToUpper);
1.181     paf       722:                        break;
                    723:                case CC_LOWER:
1.192     paf       724:                        change_case_UTF8((const XMLByte*)new_cstr, new_cstr_len, (XMLByte*)new_cstr, new_cstr_len, UTF8CaseToLower);
1.181     paf       725:                        break;
                    726:                default:
                    727:                        assert(!"unknown change case kind");
                    728:                        break; // never
                    729:                }       
                    730:                
                    731:        } else {
                    732:                const unsigned char *tables=source_charset.pcre_tables;
1.82      parser    733: 
1.181     paf       734:                const unsigned char *a;
                    735:                const unsigned char *b;
                    736:                switch(kind) {
                    737:                case CC_UPPER:
                    738:                        a=tables+lcc_offset;
                    739:                        b=tables+fcc_offset;
                    740:                        break;
                    741:                case CC_LOWER:
                    742:                        a=tables+lcc_offset;
                    743:                        b=0;
                    744:                        break;
                    745:                default:
                    746:                        assert(!"unknown change case kind");
                    747:                        a=b=0; // calm, compiler
                    748:                        break; // never
                    749:                }       
                    750: 
1.192     paf       751:                char *dest=new_cstr;
1.181     paf       752:                unsigned char index;
1.190     paf       753:                for(const char* current=new_cstr; (index=(unsigned char)*current); current++) {
1.181     paf       754:                        unsigned char c=a[index];
                    755:                        if(b)
                    756:                                c=b[c];
                    757: 
                    758:                        *dest++=(char)c;
                    759:                }
1.174     paf       760:        }
1.176     paf       761:        result.langs=langs;
1.174     paf       762:        result.body=new_cstr;
1.89      parser    763: 
1.101     parser    764:        return result;
                    765: }
                    766: 
1.213     misha     767: const String& String::escape(Charset& source_charset) const {
                    768:        if(is_empty())
                    769:                return *this;
                    770: 
                    771:        return Charset::escape(*this, source_charset);
                    772: }
                    773: 
1.238     misha     774: #define STRING_APPEND(result, from_cstr, langs, langs_offset, length) \
                    775:                        result.langs.append(result.body, langs, langs_offset, length); \
                    776:                        result.body.append_strdup_know_length(from_cstr, length);
                    777: 
1.174     paf       778: const String& String::replace(const Dictionary& dict) const {
1.238     misha     779:        if(!dict.count() || is_empty())
                    780:                return *this;
                    781: 
1.174     paf       782:        String& result=*new String();
                    783:        const char* old_cstr=cstr();
                    784:        const char* prematch_begin=old_cstr;
                    785: 
1.238     misha     786:        if(dict.count()==1) {
                    787:                // optimized simple case
                    788: 
                    789:                Dictionary::Subst subst=dict.get(0);
1.239     moko      790:                while(const char* p=strstr(prematch_begin, subst.from)) {
1.174     paf       791:                        // prematch
1.238     misha     792:                        if(size_t prematch_length=p-prematch_begin) {
                    793:                                STRING_APPEND(result, prematch_begin, langs, prematch_begin-old_cstr, prematch_length)
1.101     parser    794:                        }
                    795: 
1.174     paf       796:                        // match
1.238     misha     797:                        prematch_begin=p+subst.from_length;
1.174     paf       798: 
1.184     paf       799:                        if(const String* b=subst.to) // are there any b?
1.174     paf       800:                                result<<*b;
1.238     misha     801:                }
                    802: 
                    803:        } else {
                    804: 
                    805:                const char* current=old_cstr;
                    806:                while(*current) {
                    807:                        if(Dictionary::Subst subst=dict.first_that_begins(current)) {
                    808:                                // prematch
                    809:                                if(size_t prematch_length=current-prematch_begin) {
                    810:                                        STRING_APPEND(result, prematch_begin, langs, prematch_begin-old_cstr, prematch_length)
                    811:                                }
                    812: 
                    813:                                // match
                    814:                                // skip 'a' in 'current'; move prematch_begin
                    815:                                current+=subst.from_length; prematch_begin=current;
                    816: 
                    817:                                if(const String* b=subst.to) // are there any b?
                    818:                                        result<<*b;
                    819:                        } else // simply advance
                    820:                                current++; 
                    821:                }
                    822: 
1.174     paf       823:        }
1.156     paf       824: 
1.238     misha     825:        if(prematch_begin==old_cstr) // not modified
                    826:                return *this;
                    827: 
1.174     paf       828:        // postmatch
1.238     misha     829:        if(size_t postmatch_length=old_cstr+length()-prematch_begin) {
                    830:                STRING_APPEND(result, prematch_begin, langs, prematch_begin-old_cstr, postmatch_length)
1.174     paf       831:        }
1.156     paf       832: 
1.174     paf       833:        ASSERT_STRING_INVARIANT(result);
1.82      parser    834:        return result;
1.61      paf       835: }
1.113     parser    836: 
1.180     paf       837: static int serialize_body_char(char c, char** cur) {
                    838:        *((*cur)++)=c;
                    839:        return 0; // 0=continue
1.248     moko      840: }
                    841: 
1.174     paf       842: static int serialize_body_piece(const char* s, char** cur) {
                    843:        size_t length=strlen(s);
                    844:        memcpy(*cur, s, length);  *cur+=length;
1.178     paf       845:        return 0; // 0=continue
1.248     moko      846: }
                    847: 
1.178     paf       848: static int serialize_lang_piece(char alang, size_t asize, char** cur) {
                    849:        // lang
1.191     paf       850:        **cur=alang; (*cur)++;
                    851:        // length [WARNING: not cast, addresses must be %4=0 on sparc]
1.178     paf       852:        memcpy(*cur, &asize, sizeof(asize));  *cur+=sizeof(asize);
                    853: 
                    854:        return 0; // 0=continue
                    855: }
1.248     moko      856: 
1.174     paf       857: String::Cm String::serialize(size_t prolog_length) const {
1.178     paf       858:        size_t fragments_count=langs.count();
1.202     paf       859:        size_t body_length=body.length();
1.174     paf       860:        size_t buf_length=
1.178     paf       861:                prolog_length //1
                    862:                +sizeof(size_t) //2
1.202     paf       863:                +body_length //3
                    864:                +1 // 4 for zero terminator used in deserialize
                    865:                +sizeof(size_t) //5
                    866:                +fragments_count*(sizeof(char)+sizeof(size_t)); //6
                    867: 
1.174     paf       868:        String::Cm result(new(PointerFreeGC) char[buf_length], buf_length);
                    869: 
                    870:        // 1: prolog
                    871:        char *cur=result.str+prolog_length;
1.202     paf       872:        // 2: chars.count [WARNING: not cast, addresses must be %4=0 on sparc]
                    873:        memcpy(cur, &body_length, sizeof(body_length));  cur+=sizeof(body_length);
                    874:        // 3: letters
                    875:        body.for_each(serialize_body_char, serialize_body_piece, &cur);
                    876:        // 4: zero terminator
                    877:        *cur++=0;
                    878:        // 5: langs.count [WARNING: not cast, addresses must be %4=0 on sparc]
1.174     paf       879:        memcpy(cur, &fragments_count, sizeof(fragments_count));  cur+=sizeof(fragments_count);
1.202     paf       880:        // 6: lang info
1.178     paf       881:        langs.for_each(body, serialize_lang_piece, &cur);
1.113     parser    882: 
1.174     paf       883:        return result;
1.113     parser    884: }
1.248     moko      885: 
1.202     paf       886: bool String::deserialize(size_t prolog_size, void *buf, size_t buf_size) {
                    887:        size_t in_buf=buf_size;
                    888:        if(in_buf<=prolog_size)
1.148     paf       889:                return false;
1.202     paf       890:        in_buf-=prolog_size;
1.135     paf       891: 
1.174     paf       892:        // 1: prolog
1.202     paf       893:        const char* cur=(const char* )buf+prolog_size;
1.113     parser    894: 
1.207     paf       895:        // 2: chars.count
1.202     paf       896:        size_t body_length;
                    897:        if(in_buf<sizeof(body_length)) // body.length don't fit?
                    898:                return false;
                    899:        // [WARNING: not cast, addresses must be %4=0 on sparc]
                    900:        memcpy(&body_length, cur, sizeof(body_length));  cur+=sizeof(body_length);
                    901:        in_buf-=sizeof(body_length);
                    902: 
                    903:        if(in_buf<body_length+1) // letters+terminator don't fit?
                    904:                return false;
                    905:        // 4: zero terminator
                    906:        if(cur[body_length] != 0) // in place?
                    907:                return false;
                    908:        // 3: letters
1.251     moko      909:        body=String::Body(String::C(cur, body_length));
1.202     paf       910:        cur+=body_length+1;
                    911:        in_buf-=body_length+1;
                    912: 
                    913:        // 5: langs.count
1.191     paf       914:        size_t fragments_count;
1.202     paf       915:        if(in_buf<sizeof(fragments_count)) // langs.count don't fit?
1.174     paf       916:                return false;
1.191     paf       917:        // [WARNING: not cast, addresses must be %4=0 on sparc]
                    918:        memcpy(&fragments_count, cur, sizeof(fragments_count));  cur+=sizeof(fragments_count);
1.202     paf       919:        in_buf-=sizeof(fragments_count);
1.174     paf       920:        
                    921:        if(fragments_count) {
1.202     paf       922:                // 6: lang info
1.174     paf       923:                size_t total_length=0;
                    924:                for(size_t f=0; f<fragments_count; f++) {
1.191     paf       925:                        char lang;
                    926:                        size_t fragment_length;
                    927:                        size_t piece_length=sizeof(lang)+sizeof(fragment_length);
1.202     paf       928:                        if(in_buf<piece_length) // lang+length
1.174     paf       929:                                return false;
                    930: 
1.191     paf       931:                        // lang
                    932:                        lang=*cur++;
                    933:                        // length [WARNING: not cast, addresses must be %4=0 on sparc]
                    934:                        memcpy(&fragment_length, cur, sizeof(fragment_length));  cur+=sizeof(fragment_length);
                    935: 
1.206     paf       936:                        size_t combined_length=total_length+fragment_length;
                    937:                        if(combined_length>body_length)
                    938:                                return false; // file curruption
1.191     paf       939:                        // uchar needed to prevent propagating 0x80 bit to upper bytes
                    940:                        langs.append(total_length, (String::Language)(uchar)lang, fragment_length);
1.206     paf       941:                        total_length=combined_length;
1.202     paf       942:                        in_buf-=piece_length;
1.174     paf       943:                }
1.128     paf       944: 
1.202     paf       945:                if(total_length!=body_length) // length(all language fragments) vs length(letters)
1.148     paf       946:                        return false;
1.174     paf       947:        }
1.202     paf       948:        if(in_buf!=0) // some strange extra bytes
                    949:                return false;
1.113     parser    950: 
1.174     paf       951:        ASSERT_STRING_INVARIANT(*this);
1.148     paf       952:        return true;
1.176     paf       953: }
                    954: 
1.201     paf       955: void String::Body::dump() const {
                    956:        CORD_dump(body);
                    957: }
                    958: 
1.257     moko      959: const char* String::Languages::visualize() const {
1.177     paf       960:        if(opt.is_not_just_lang)
1.233     misha     961:                return CORD_to_const_char_star(langs, 0);
1.176     paf       962:        else
1.257     moko      963:                return 0;
1.176     paf       964: }
1.248     moko      965: 
1.201     paf       966: void String::Languages::dump() const {
                    967:        if(opt.is_not_just_lang)
                    968:                CORD_dump(langs);
                    969:        else
                    970:                puts((const char*)&langs);
                    971: }
1.248     moko      972: 
1.257     moko      973: void String::dump() const {
                    974:        body.dump();
                    975:        langs.dump();
                    976: }
1.176     paf       977: 
1.257     moko      978: static char *n_chars(char c, size_t length){
                    979:        char *result=(char *)pa_malloc_atomic(length+1);
                    980:        memset(result, c, length);
                    981:        result[length] = '\0';
                    982:        return result;
1.113     parser    983: }
1.229     misha     984: 
1.257     moko      985: char* String::visualize_langs() const {
1.258     moko      986:        return is_not_just_lang() ? pa_strdup(langs.visualize()) : n_chars((char)just_lang(), length());
1.201     paf       987: }
1.229     misha     988: 
                    989: const String& String::trim(String::Trim_kind kind, const char* chars, Charset* source_charset) const {
1.223     misha     990:        if(is_empty())
1.195     paf       991:                return *this;
                    992: 
                    993:        size_t substr_begin, substr_length;
1.229     misha     994:        Body new_body=body.trim(kind, chars, &substr_begin, &substr_length, source_charset);
1.195     paf       995:        if(new_body==body) // we received unchanged pointer, do likewise
                    996:                return *this;
                    997:        // new_body differs from body, adjust langs along
                    998: 
                    999:        String& result=*new String;
                   1000:        if(!new_body) // body.trim produced empty result
                   1001:                return result;
                   1002:        // body.trim produced nonempty result
                   1003: 
                   1004:        // first: their langs
                   1005:        result.langs.append(result.body, langs, substr_begin, substr_length);
                   1006:        // next: letters themselves
                   1007:        result.body=new_body;
                   1008: 
                   1009:        ASSERT_STRING_INVARIANT(result);
                   1010:        return result;
1.198     paf      1011: }

E-mail: