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