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