void Replace(wstring& input, wstring find, wstring replace_with, bool replace_all, bool case_insensitive) { if (find.empty() || find == replace_with || input.length() < find.length()) return; if (!case_insensitive) { for (size_t pos = input.find(find); pos != wstring::npos; pos = input.find(find, pos)) { input.replace(pos, find.length(), replace_with); if (!replace_all) pos += replace_with.length(); } } else { for (size_t i = 0; i < input.length() - find.length() + 1; i++) { for (size_t j = 0; j < find.length(); j++) { if (input.length() < find.length()) return; if (tolower(input[i + j]) == tolower(find[j])) { if (j == find.length() - 1) { input.replace(i--, find.length(), replace_with); if (!replace_all) i += replace_with.length(); break; } } else { i += j; break; } } } } }
/// <summary>Parses all the keywords/macros in a string and replaces them recursively</summary> /// <param name="text">Source text to parse</param> /// <param name="depth">Debugging output depth</param> /// <returns>Fully parsed text</returns> /// <exception cref="Logic::FileFormatException">Macro contains wrong number of parameters</exception> /// <exception cref="Logic::Language::RegularExpressionException">RegEx error</exception> wstring DescriptionParser::Parse(wstring text, int depth) const { UINT Position; wsmatch match; wstring r; try { #ifdef PRINT_CONSOLE Console << Cons::Cyan << Indent(depth) << "Parsing: " << Cons::White << text << ENDL; #endif // Find/Replace all macros: {AAAA:bbb}, {AAAA:bbb,ccc}, {AAAA:bbb,ccc,ddd} ... for (Position = 0; regex_search(text.cbegin()+Position, text.cend(), match, MatchMacro); ) // Manually track position for in-place replacement + avoid infinite loop { // Recursively generate replacement text r = onMatchMacro(match, depth); #ifdef PRINT_CONSOLE Console << Indent(depth) << "Replacing text: " << Cons::Yellow << match[0].str() << Cons::White << " with " << Cons::Green << r << ENDL; #endif // Advance position to beyond inserted text, and insert text Position = (match[0].first - text.cbegin()) + r.length(); text.replace(match[0].first, match[0].second, r); } // Find/Replace all keywords: {AAAAA}, AAAAA for (Position = 0; regex_search(text.cbegin()+Position, text.cend(), match, MatchKeyword); ) { // Recursively generate replacement text r = onMatchKeyword(match, depth); #ifdef PRINT_CONSOLE Console << Indent(depth) << "Replacing text: " << Cons::Yellow << match[0].str() << Cons::White << " with " << Cons::Green << r << ENDL; #endif // Advance position to beyond inserted text, and insert text Position = (match[0].first - text.cbegin()) + r.length(); text.replace(match[0].first, match[0].second, r); } return text; } catch (regex_error& e) { throw RegularExpressionException(HERE, e); } }
bool FixPath(wstring& path) { // Fix unix paths std::replace( path.begin(), path.end(), L'/', L'\\' ); // Remove double slashes while(true) { size_t p = path.find(L"\\\\"); if (p == string::npos) break; path.replace(p, 2, L"\\"); } // Are we pointing at a real destination? if (DirectoryExists(path)) { if (path[path.length()-1] != L'\\') path += L'\\'; return true; } else if (path.at(path.length() - 1) == L'\\') { // It says its a directory but it's not, must be a file path = path.substr(0, path.length() - 1); } return FileExists(path); }
wstring NBTools::doReplace(wstring text, wstring find, wstring replace) { while (true) { size_t pos = text.find(find); if (0 > pos || pos > text.length()) return text; text = text.replace(pos, find.length(), replace); } }
void string_replace( wstring& strBig, const wstring& strSrc, const wstring& strDst ) { wstring::size_type pos=0; wstring::size_type srclen = strSrc.size(); wstring::size_type dstlen = strDst.size(); while( (pos=strBig.find(strSrc, pos)) != wstring::npos) { strBig.replace(pos, srclen, strDst); pos += dstlen; } }
/* ----------------------------------------------------------------------- * 関数名 : ws_replace * 機能概要: str内のbeforeをすべてafterに置換します * ----------------------------------------------------------------------- */ void ws_replace(wstring &str, const wstring &before, const wstring &after) { int sz_bef = before.size(); int sz_aft = after.size(); for(int rp_pos = 0; ; rp_pos += sz_aft) { rp_pos = str.find(before, rp_pos); if (rp_pos == -1) break; str.replace(rp_pos, sz_bef, after); } }
// & の削除 static wstring RemoveAmpersand( wstring sLavel) { size_t nPos =0; while ((nPos = sLavel.find( L"&", nPos)) != wstring::npos) { if (sLavel[nPos+1] == L'&') { // && sLavel.replace( nPos, 1, L""); } nPos ++; } return sLavel; }
// & の補完 static wstring SupplementAmpersand( wstring sLavel) { size_t nPos =0; while ((nPos = sLavel.find( L"&", nPos)) != wstring::npos) { if (sLavel[nPos+1] != L'&') { // &&でない sLavel.replace( nPos, 1, L"&&"); } nPos +=2; } return sLavel; }
void ReplaceChars(wstring& str, const wchar_t chars[], const wstring replace_with) { if (chars == replace_with) return; size_t pos = 0; do { pos = str.find_first_of(chars, pos); if (pos != wstring::npos) str.replace(pos, 1, replace_with); } while (pos != wstring::npos); }
void GraphicsInterface::Replace(wstring & wstrText, wstring wstrFind, wstring wstrReplace) { size_t pos = 0; pos = wstrText.find(wstrFind); while (pos != wstring::npos) { wstrText.replace(pos, wstrFind.length(), wstrReplace); pos = wstrText.find(wstrFind); } return; }
wstring RegistryMonitor::convertRegistryObjectNameToHiveName(wstring registryObjectName) { /* Convert /Registry/Machine etc to the actual hive name like HKLM */ std::list<ObjectPair>::iterator it; for(it = objectNameMap.begin(); it != objectNameMap.end(); it++) { size_t position = registryObjectName.rfind(it->first,0); if(position != wstring::npos) { return registryObjectName.replace(position, it->first.length(), it->second, 0, it->second.length()); } } return registryObjectName; }
wstring FileMonitor::convertFileObjectNameToDosName(wstring fileObjectName) { stdext::hash_map<wstring, wstring>::iterator it; for(it = dosNameMap.begin(); it != dosNameMap.end(); it++) { size_t position = fileObjectName.rfind(it->first,0); if(position != wstring::npos) { fileObjectName.replace(position, it->first.length(), it->second, 0, it->second.length()); break; } } //transform(fileObjectName.begin(), fileObjectName.end(), fileObjectName.begin(), tolower); return fileObjectName; }
/// <summary>Populates the parameters markers within the description source text.</summary> /// <param name="src">source text.</param> /// <param name="cmd">command syntax.</param> /// <returns></returns> wstring DescriptionParser::Populate(wstring src, CommandSyntaxRef cmd) { wsmatch match; wstring r; // Replace all {aaa,bbb} markers. Manually track position for in-place replacement + avoid infinite loop for (int Position = 0; regex_search(src.cbegin()+Position, src.cend(), match, MartchParameterMarker); ) { r = onParameterMarker(match, cmd); #ifdef PRINT_CONSOLE Console << " Replace: " << Cons::Yellow << match[0].str() << Cons::White << " with " << Cons::Green << r << ENDL; #endif // Advance position + perform replacement Position = (match[0].first - src.cbegin()) + r.length(); src.replace(match[0].first, match[0].second, r); } return src; }