bool wxFFile::Write(const wxString& s, const wxMBConv& conv) { // Writing nothing always succeeds -- and simplifies the check for // conversion failure below. if ( s.empty() ) return true; const wxWX2MBbuf buf = s.mb_str(conv); #if wxUSE_UNICODE const size_t size = buf.length(); if ( !size ) { // This means that the conversion failed as the original string wasn't // empty (we explicitly checked for this above) and in this case we // must fail too to indicate that we can't save the data. return false; } #else const size_t size = s.length(); #endif return Write(buf, size) == size; }
bool wxFile::Write(const wxString& s, const wxMBConv& conv) { const wxWX2MBbuf buf = s.mb_str(conv); if ( !buf ) return false; #if wxUSE_UNICODE const size_t size = buf.length(); #else const size_t size = s.length(); #endif return Write(buf, size) == size; }
int wxRegExImpl::Replace(wxString *text, const wxString& replacement, size_t maxMatches) const { wxCHECK_MSG( text, wxNOT_FOUND, wxT("NULL text in wxRegEx::Replace") ); wxCHECK_MSG( IsValid(), wxNOT_FOUND, wxT("must successfully Compile() first") ); // the input string #ifndef WXREGEX_CONVERT_TO_MB const wxChar *textstr = text->c_str(); size_t textlen = text->length(); #else const wxWX2MBbuf textstr = WXREGEX_CHAR(*text); if (!textstr) { wxLogError(_("Failed to find match for regular expression: %s"), GetErrorMsg(0, true).c_str()); return 0; } size_t textlen = strlen(textstr); text->clear(); #endif // the replacement text wxString textNew; // the result, allow 25% extra wxString result; result.reserve(5 * textlen / 4); // attempt at optimization: don't iterate over the string if it doesn't // contain back references at all bool mayHaveBackrefs = replacement.find_first_of(wxT("\\&")) != wxString::npos; if ( !mayHaveBackrefs ) { textNew = replacement; } // the position where we start looking for the match size_t matchStart = 0; // number of replacement made: we won't make more than maxMatches of them // (unless maxMatches is 0 which doesn't limit the number of replacements) size_t countRepl = 0; // note that "^" shouldn't match after the first call to Matches() so we // use wxRE_NOTBOL to prevent it from happening while ( (!maxMatches || countRepl < maxMatches) && Matches( #ifndef WXREGEX_CONVERT_TO_MB textstr + matchStart, #else textstr.data() + matchStart, #endif countRepl ? wxRE_NOTBOL : 0 WXREGEX_IF_NEED_LEN(textlen - matchStart)) ) { // the string possibly contains back references: we need to calculate // the replacement text anew after each match if ( mayHaveBackrefs ) { mayHaveBackrefs = false; textNew.clear(); textNew.reserve(replacement.length()); for ( const wxChar *p = replacement.c_str(); *p; p++ ) { size_t index = (size_t)-1; if ( *p == wxT('\\') ) { if ( wxIsdigit(*++p) ) { // back reference wxChar *end; index = (size_t)wxStrtoul(p, &end, 10); p = end - 1; // -1 to compensate for p++ in the loop } //else: backslash used as escape character } else if ( *p == wxT('&') ) { // treat this as "\0" for compatbility with ed and such index = 0; } // do we have a back reference? if ( index != (size_t)-1 ) { // yes, get its text size_t start, len; if ( !GetMatch(&start, &len, index) ) { wxFAIL_MSG( wxT("invalid back reference") ); // just eat it... } else { textNew += wxString( #ifndef WXREGEX_CONVERT_TO_MB textstr #else textstr.data() #endif + matchStart + start, *wxConvCurrent, len); mayHaveBackrefs = true; } } else // ordinary character { textNew += *p; } } } size_t start, len; if ( !GetMatch(&start, &len) ) { // we did have match as Matches() returned true above! wxFAIL_MSG( wxT("internal logic error in wxRegEx::Replace") ); return wxNOT_FOUND; } // an insurance against implementations that don't grow exponentially // to ensure building the result takes linear time if (result.capacity() < result.length() + start + textNew.length()) result.reserve(2 * result.length()); #ifndef WXREGEX_CONVERT_TO_MB result.append(*text, matchStart, start); #else result.append(wxString(textstr.data() + matchStart, *wxConvCurrent, start)); #endif matchStart += start; result.append(textNew); countRepl++; matchStart += len; } #ifndef WXREGEX_CONVERT_TO_MB result.append(*text, matchStart, wxString::npos); #else result.append(wxString(textstr.data() + matchStart, *wxConvCurrent)); #endif *text = result; return countRepl; }