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