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