--- parser3/src/main/pa_string.C 2010/12/29 12:53:33 1.236 +++ parser3/src/main/pa_string.C 2012/03/12 22:29:41 1.239 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_STRING_C="$Date: 2010/12/29 12:53:33 $"; +static const char * const IDENT_STRING_C="$Date: 2012/03/12 22:29:41 $"; #include "pa_string.h" #include "pa_exception.h" @@ -483,8 +483,10 @@ void String::split(ArrayString& result, size_t& pos_after, const char* delim, Language lang, int limit) const { + if(is_empty()) + return; size_t self_length=length(); - if(size_t delim_length=strlen(delim) && !is_empty()) { + if(size_t delim_length=strlen(delim)) { size_t pos_before; // while we have 'delim'... for(; (pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND && limit; limit--) { @@ -506,7 +508,9 @@ void String::split(ArrayString& result, size_t& pos_after, const String& delim, Language lang, int limit) const { - if(!delim.is_empty() && !is_empty()) { + if(is_empty()) + return; + if(!delim.is_empty()) { size_t pos_before; // while we have 'delim'... for(; (pos_before=pos(delim, pos_after, lang))!=STRING_NOT_FOUND && limit; limit--) { @@ -651,34 +655,63 @@ const String& String::escape(Charset& so return Charset::escape(*this, source_charset); } +#define STRING_APPEND(result, from_cstr, langs, langs_offset, length) \ + result.langs.append(result.body, langs, langs_offset, length); \ + result.body.append_strdup_know_length(from_cstr, length); + const String& String::replace(const Dictionary& dict) const { + if(!dict.count() || is_empty()) + return *this; + String& result=*new String(); const char* old_cstr=cstr(); const char* prematch_begin=old_cstr; - const char* current=old_cstr; - while(*current) { - if(Dictionary::Subst subst=dict.first_that_begins(current)) { + if(dict.count()==1) { + // optimized simple case + + Dictionary::Subst subst=dict.get(0); + while(const char* p=strstr(prematch_begin, subst.from)) { // prematch - if(size_t prematch_length=current-prematch_begin) { - result.langs.append(result.body, langs, prematch_begin-old_cstr, prematch_length); - result.body.append_strdup_know_length(prematch_begin, prematch_length); + if(size_t prematch_length=p-prematch_begin) { + STRING_APPEND(result, prematch_begin, langs, prematch_begin-old_cstr, prematch_length) } // match - // skip 'a' in 'current'; move prematch_begin - current+=subst.from_length; prematch_begin=current; + prematch_begin=p+subst.from_length; if(const String* b=subst.to) // are there any b? result<<*b; - } else // simply advance - current++; + } + + } else { + + const char* current=old_cstr; + while(*current) { + if(Dictionary::Subst subst=dict.first_that_begins(current)) { + // prematch + if(size_t prematch_length=current-prematch_begin) { + STRING_APPEND(result, prematch_begin, langs, prematch_begin-old_cstr, prematch_length) + } + + // match + // skip 'a' in 'current'; move prematch_begin + current+=subst.from_length; prematch_begin=current; + + if(const String* b=subst.to) // are there any b? + result<<*b; + } else // simply advance + current++; + } + } + if(prematch_begin==old_cstr) // not modified + return *this; + // postmatch - if(size_t postmatch_length=current-prematch_begin) { - result.langs.append(result.body, langs, prematch_begin-old_cstr, postmatch_length); - result.body.append_strdup_know_length(prematch_begin, postmatch_length); + if(size_t postmatch_length=old_cstr+length()-prematch_begin) { + STRING_APPEND(result, prematch_begin, langs, prematch_begin-old_cstr, postmatch_length) } ASSERT_STRING_INVARIANT(result);