--- parser3/src/main/pa_string.C 2002/02/28 09:35:41 1.147 +++ parser3/src/main/pa_string.C 2002/04/04 13:42:51 1.150 @@ -4,7 +4,7 @@ Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - $Id: pa_string.C,v 1.147 2002/02/28 09:35:41 paf Exp $ + $Id: pa_string.C,v 1.150 2002/04/04 13:42:51 paf Exp $ */ #include "pcre.h" @@ -81,7 +81,7 @@ void String::expand() { String& String::real_append(STRING_APPEND_PARAMS) { if(!last_chunk) // growth stopped [we're appended as string to somebody] - throw Exception(0, 0, + throw Exception(0, this, "string growth stopped (append cstr)"); @@ -131,7 +131,7 @@ String& String::real_append(STRING_APPEN char String::first_char() const { if(is_empty()) - throw Exception(0, 0, + throw Exception(0, this, "getting first char of empty string"); @@ -473,7 +473,7 @@ bool String::match( bool *was_global) const { if(regexp.is_empty()) - throw Exception(0, 0, + throw Exception(0, aorigin, "regexp is empty"); const char *pattern=regexp.cstr(); @@ -487,14 +487,14 @@ bool String::match( pool().get_source_charset().pcre_tables); if(!code) - throw Exception(0, 0, + throw Exception(0, ®exp.mid(erroffset, regexp.size()), "regular expression syntax error - %s", errptr); int info_substrings=pcre_info(code, 0, 0); if(info_substrings<0) { pcre_free(code); - throw Exception(0, 0, + throw Exception(0, aorigin, "pcre_info error (%d)", info_substrings); @@ -534,7 +534,7 @@ bool String::match( if(exec_substrings<0) { pcre_free(code); - throw Exception(0, 0, + throw Exception(0, aorigin, "regular expression execute error (%d)", exec_substrings); @@ -583,7 +583,7 @@ String& String::change_case(Pool& pool, b=0; break; default: - throw Exception(0, 0, + throw Exception(0, this, "unknown change case kind #%d", static_cast(kind)); // never @@ -655,9 +655,11 @@ void String::join_chain(Pool& pool, } } -String& String::reconstruct(Pool& pool) const { - //_asm int 3; +/// @test if in some piece were found no dict words, append it, not it's duplicate +String& String::replace(Pool& pool, Dictionary& dict) const { +// return reconstruct(pool).replace_in_reconstructed(pool, dict); String& result=*new(pool) String(pool); + STRING_FOREACH_ROW( uchar joined_lang; const char *joined_ptr; @@ -668,20 +670,9 @@ IFNDEF_NO_STRING_ORIGIN( ); join_chain(pool, chunk, row, countdown, joined_lang, joined_ptr, joined_size); - - result.APPEND(joined_ptr, joined_size, joined_lang, - joined_origin_file, joined_origin_line); - ); - - return result; -}; - -String& String::replace_in_reconstructed(Pool& pool, Dictionary& dict) const { - //_asm int 3; - String& result=*new(pool) String(pool); - STRING_FOREACH_ROW( - const char *src=row->item.ptr; - size_t src_size=row->item.size; + + const char *src=joined_ptr; + size_t src_size=joined_size; char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio()), 14); char *dest=new_cstr; while(src_size) { @@ -700,17 +691,12 @@ String& String::replace_in_reconstructed } } - result.APPEND(new_cstr, dest-new_cstr, - row->item.lang, - row->item.origin.file, row->item.origin.line); + result.APPEND(new_cstr, dest-new_cstr, joined_lang, + joined_origin_file, joined_origin_line); ); return result; } -String& String::replace(Pool& pool, Dictionary& dict) const { - return reconstruct(pool).replace_in_reconstructed(pool, dict); -} - double String::as_double() const { double result; const char *cstr; @@ -733,7 +719,7 @@ double String::as_double() const { result=(double)strtod(cstr, &error_pos); if(*error_pos/*not EOS*/) - throw Exception(0, 0, + throw Exception("number.format", this, "invalid number (double)"); @@ -761,7 +747,7 @@ int String::as_int() const { result=(int)strtol(cstr, &error_pos, 0); if(*error_pos/*not EOS*/) - throw Exception(0, 0, + throw Exception("number.format", this, "invalid number (int)"); @@ -802,26 +788,32 @@ void String::serialize(size_t prolog_siz cur+=row->item.size; ); } -void String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file) { +bool String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file) { if(buf_size<=prolog_size) - return; + return false; char *cur=(char *)buf+prolog_size; buf_size-=prolog_size; while(buf_size) { - uchar lang=*(uchar *)(cur); - + if(sizeof(uchar)+sizeof(ushort)>buf_size) // lang+size + return false; + + uchar lang=*(uchar *)(cur); ushort size=uchars2ushort( *(uchar*)(cur+sizeof(uchar)*1), *(uchar*)(cur+sizeof(uchar)*2) ); + size_t piece_size=sizeof(uchar)+sizeof(ushort)+size; + if(piece_size>buf_size) // buffer overrun, can be on incomplete cache files + return false; + const char *ptr=(const char*)(cur+sizeof(uchar)*3); APPEND(ptr, size, lang, file, 0); - size_t piece_size=sizeof(uchar)+sizeof(ushort)+size; cur+=piece_size; buf_size-=piece_size; } + return true; }