bool changenth(std::string &str, int n, const char *with, bool insert = false, bool nonzero = false) { std::string::size_type s, e; if (!findnth(str, n, s, e)) return false; if (nonzero && str.substr(s, e-s) == "0") return true; // not an error if (!insert) str.replace(s, e-s, with); else str.insert(s, with); return true; }
// This is used by several of the FileIO and /dev/fs functions std::string HLE_IPC_BuildFilename(std::string path_wii) { std::string path_full = File::GetUserPath(D_SESSION_WIIROOT_IDX); // Replaces chars that FAT32 can't support with strings defined in /sys/replace for (auto& replacement : replacements) { for (size_t j = 0; (j = path_wii.find(replacement.first, j)) != path_wii.npos; ++j) path_wii.replace(j, 1, replacement.second); } path_full += path_wii; return path_full; }
bool replaceString(std::string& text, const std::string& key, const std::string& value) { if(text.find(key) == std::string::npos) return false; std::string::size_type start = 0, pos = 0; while((start = text.find(key, pos)) != std::string::npos) { text.replace(start, key.size(), value); //text = text.substr(0, start) + value + text.substr(start + key.size()); pos = start + value.size(); } return true; }
void findandreplace( std::string& tInput, std::string tFind, std::string tReplace ) { size_t uPos = 0; size_t uFindLen = tFind.length(); size_t uReplaceLen = tReplace.length(); if( uFindLen == 0 ){ return; } for( ;(uPos = tInput.find( tFind, uPos )) != std::string::npos; ){ tInput.replace( uPos, uFindLen, tReplace ); uPos += uReplaceLen; } }
bool replaceString(std::string& text, const std::string key, const std::string value) { std::string::size_type start = text.find(key); if(start == std::string::npos) //skip if there's no key in text return false; std::string::size_type pos = 0; for(; start != std::string::npos; start = text.find(key, pos)) { text.replace(start, key.size(), value); pos = start + key.size(); } return true; }
BOOL URLParser::Replace(std::string& strSource, std::string& strToFind, std::string& ptrToReplace) { BOOL bRetVal = FALSE; while(TRUE) { int iPos = strSource.find(strToFind, 0); if(std::string::npos == iPos) { break; } strSource.replace(iPos, strToFind.length(), ptrToReplace); bRetVal = TRUE; } return bRetVal; }
// This is used by several of the FileIO and /dev/fs functions std::string HLE_IPC_BuildFilename(std::string path_wii, int _size) { std::string path_full = File::GetUserPath(D_WIIROOT_IDX); // Replaces chars that FAT32 can't support with strings defined in /sys/replace for (auto i = replacements.begin(); i != replacements.end(); ++i) { for (size_t j = 0; (j = path_wii.find(i->first, j)) != path_wii.npos; ++j) path_wii.replace(j, 1, i->second); } path_full += path_wii; return path_full; }
std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) { size_t pos = 0; if (src == dest) return result; while ((pos = result.find(src, pos)) != std::string::npos) { result.replace(pos, src.size(), dest); pos += dest.length(); } return result; }
static void replaceDefines(const std::string& compileTimeDefines, std::string& out) { // Replace semicolons with '#define ... \n' if (compileTimeDefines.size() > 0) { size_t pos; out = compileTimeDefines; out.insert(0, "#define "); while ((pos = out.find(';')) != std::string::npos) { out.replace(pos, 1, "\n#define "); } out += "\n"; } }
// static void CMIRIAMResourceObject::unescapeId(std::string & id) { // We have to convert all %[0-9a-fA-F][0-9a-fA-F] character sequences to utf8 characters. std::string::size_type pos; for (pos = 0; pos < id.length(); pos++) if (id[pos] == '%' && id.find_first_not_of("0123456789abcdefABCDEF", pos + 1) > pos + 2) { char ascii[2]; ascii[0] = (unsigned char) strtol(id.substr(pos + 1 , 2).c_str(), NULL, 16); ascii[1] = 0x0; id.replace(pos, 3, CCopasiXMLInterface::utf8(ascii)); } }
std::size_t StringExtend::ReplaceAll (std::string& source, const std::string& prototype, const std::string& copy) { if(prototype.empty()) { return 0; } std::size_t startpos = 0, counter = 0; while((startpos = source.find(prototype, startpos)) != std::string::npos) { source.replace(startpos, prototype.length(), copy); startpos += copy.length(); counter ++; } return counter; }
//actual filtering void FilterBadWords(std::string& s) { //preconstruct our array, we love speed gain by paying startup time static auto const& Mapping_CP852 = [] { std::cout << "PRECONSTRUCT: " << __PRETTY_FUNCTION__ << "\n"; static uint8_t mappings[0x100]; for(unsigned i = 0; i < sizeof(mappings); ++i) mappings[i] = static_cast<uint8_t>(i); mappings[0x88 /* ł */] = 'l'; mappings[0xa5 /* ą */] = 'a'; mappings[0xa9 /* ę */] = 'e'; mappings[0x86 /* ć */] = 'c'; mappings[0xbe /* ż */] = 'z'; mappings[0xab /* ź */] = 'z'; mappings[0xa2 /* ó */] = 'o'; mappings[0x98 /* ś */] = 's'; mappings[0xe4 /* ń */] = 'n'; mappings[0x9d /* Ł */] = 'L'; mappings[0xa4 /* Ą */] = 'A'; mappings[0xa8 /* Ę */] = 'E'; mappings[0x8f /* Ć */] = 'C'; mappings[0xbd /* Ż */] = 'Z'; mappings[0x8d /* Ź */] = 'Z'; mappings[0xe0 /* Ó */] = 'O'; mappings[0x97 /* Ś */] = 'S'; mappings[0xe3 /* Ń */] = 'N'; return mappings; }(); std::string sc(s); // bit redundant work here for (auto& character : sc) character = Mapping_CP852[(uint8_t)character]; for (auto &badword : badwords) { size_t pos = sc.find(badword); size_t size = badword.size(); while (pos != std::string::npos) { s.replace ( s.begin() + pos, s.begin() + pos + size, "*"); sc.replace(sc.begin() + pos, sc.begin() + pos + size, "*"); pos = sc.find(badword); } } }
bool FixPathCase(std::string& basePath, std::string &path, FixPathCaseBehavior behavior) { size_t len = path.size(); if (len == 0) return true; if (path[len - 1] == '/') { len--; if (len == 0) return true; } std::string fullPath; fullPath.reserve(basePath.size() + len + 1); fullPath.append(basePath); size_t start = 0; while (start < len) { size_t i = path.find('/', start); if (i == std::string::npos) i = len; if (i > start) { std::string component = path.substr(start, i - start); // Fix case and stop on nonexistant path component if (FixFilenameCase(fullPath, component) == false) { // Still counts as success if partial matches allowed or if this // is the last component and only the ones before it are required return (behavior == FPC_PARTIAL_ALLOWED || (behavior == FPC_PATH_MUST_EXIST && i >= len)); } path.replace(start, i - start, component); fullPath.append(component); fullPath.append(1, '/'); } start = i + 1; } return true; }
std::string AptCacheFile::debParser(std::string descr) { // Policy page on package descriptions // http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Description unsigned int i; string::size_type nlpos=0; nlpos = descr.find('\n'); // delete first line if (nlpos != string::npos) { descr.erase(0, nlpos + 2); // del "\n " too } // avoid replacing '\n' for a ' ' after a '.\n' is found bool removedFullStop = false; while (nlpos < descr.length()) { // find the new line position nlpos = descr.find('\n', nlpos); if (nlpos == string::npos) { // if it could not find the new line // get out of the loop break; } i = nlpos; // erase the char after '\n' which is always " " descr.erase(++i, 1); // remove lines likes this: " .", making it a \n if (descr[i] == '.') { descr.erase(i, 1); nlpos = i; // don't permit the next round to replace a '\n' to a ' ' removedFullStop = true; continue; } else if (descr[i] != ' ' && removedFullStop == false) { // it's not a line to be verbatim displayed // So it's a paragraph let's replace '\n' with a ' ' // replace new line with " " descr.replace(nlpos, 1, " "); } removedFullStop = false; nlpos++; } return descr; }
void escapeXMLChars(std::string &s) { std::string bad = "<>&"; const char *replace[] = { "<", ">", "&" }; for (unsigned i = 0; i < s.size(); i++) { unsigned n = bad.find(s[i]); if (n != std::string::npos) { s.replace(i, 1, replace[n]); } } }
static void replaceAll(std::string& str, const std::string& from, const std::string& to) { size_t start_pos = 0; while((start_pos = str.find(from, start_pos)) != std::string::npos) { //size_t end_pos = start_pos + from.length(); while((start_pos = str.find(from, start_pos)) != std::string::npos) { str.replace(start_pos, from.length(), to); start_pos += to.length(); } start_pos += to.length(); } }
NS_LEMON_FILESYSTEM_BEGIN static void replace(std::string& str, const char* from, const char* to) { unsigned size = strlen(from); if( size == 0 ) return; unsigned size_to = strlen(to); size_t start_pos = 0; while( (start_pos = str.find(from, start_pos)) != std::string::npos ) { str.replace(start_pos, size, to); start_pos += size_to; } }
std::string StringUtils::replace(std::string str, const char* what, const char* by) { const std::string what_tmp(what); const std::string by_tmp(by); size_t idx = str.find(what); const size_t what_len = what_tmp.length(); if (what_len > 0) { const size_t by_len = by_tmp.length(); while (idx != std::string::npos) { str = str.replace(idx, what_len, by); idx = str.find(what, idx + by_len); } } return str; }
/*! * @if jp * @brief 文字列を置き換える * @else * @brief Replace string * @endif */ unsigned int replaceString(std::string& str, const std::string from, const std::string to) { std::string::size_type pos(0); unsigned int cnt(0); while (pos != std::string::npos) { pos = str.find(from, pos); if (pos == std::string::npos) break; str.replace(pos, from.size(), to); pos += to.size(); ++cnt; } return cnt; }
//============================================================================ // FUNCTION : SPELLutils::replace //============================================================================ void SPELLutils::replace( std::string& str, std::string original, std::string newstr ) { std::string::size_type pos; while(true) { pos = str.find(original); if (pos != std::string::npos) { str.replace(pos, original.size(), newstr.c_str()); } else { break; } } }
// 字符串替换 int kbe_replace(std::string& str, const std::string& pattern, const std::string& newpat) { int count = 0; const size_t nsize = newpat.size(); const size_t psize = pattern.size(); for(size_t pos = str.find(pattern, 0); pos != std::string::npos; pos = str.find(pattern,pos + nsize)) { str.replace(pos, psize, newpat); count++; } return count; }
// adjusts a POSIX path to system-specific conventions // -> changes '/' to DIR_DELIM // -> absolute paths start with "C:\\" on windows std::string p(std::string path) { for (size_t i = 0; i < path.size(); ++i) { if (path[i] == '/') { path.replace(i, 1, DIR_DELIM); i += std::string(DIR_DELIM).size() - 1; // generally a no-op } } #ifdef _WIN32 if (path[0] == '\\') path = "C:" + path; #endif return path; }
bool ChangeTokNth(std::string& str, uint32 n, char const* with, bool insert = false, bool allowZero = false) { std::string::size_type s = 0, e = 0; if (!FindTokNth(str, n, s, e)) return false; if (allowZero && str.substr(s, e - s) == "0") return true; // not an error if (!insert) str.replace(s, e-s, with); else str.insert(s, with); return true; }
std::string string_replace_first_instance( std::string src, std::string const& target, std::string const& repl){ // handle error situations/trivial cases if (target.length() == 0 || src.length() == 0) { return src; } size_t idx = 0; idx = src.find( target, idx); src.replace( idx, target.length(), repl); idx += repl.length(); return src; }
std::string ReplaceString(std::string subject, const std::string& search, const std::string& replace, int & counter) { // http://stackoverflow.com/questions/4643512/replace-substring-with-another-substring-c if( subject.find_first_not_of(" \t") != -1 && subject.at( subject.find_first_not_of(" \t") ) == '#' ){ return subject; } else{ size_t pos = 0; while((pos = subject.find(search, pos)) != std::string::npos) { subject.replace(pos, search.length(), replace); counter++; pos += replace.length(); } return subject; } }
static void ReplaceInString(std::string& str, const std::string& what, const std::string& replacement) { size_t lst = 0; size_t l1 = what.size(); size_t l2 = replacement.size(); if (l1 == 0 && l2 == 0) return; // Nothing to do do { size_t t = str.find(what, lst); if(t == std::string::npos) return; str.replace(t, l1, replacement); t += l2; } while(true); }
// ------------------------------------------------------------------------------------------------ void XFileParser::ParseDataObjectTextureFilename( std::string& pName) { readHeadOfDataObject(); GetNextTokenAsString( pName); CheckForClosingBrace(); // FIX: some files (e.g. AnimationTest.x) have "" as texture file name if (!pName.length()) { DefaultLogger::get()->warn("Length of texture file name is zero. Skipping this texture."); } // some exporters write double backslash paths out. We simply replace them if we find them while ( pName.find( "\\\\") != std::string::npos) pName.replace( pName.find( "\\\\"), 2, "\\"); }
void escape(std::string &str) { std::map<char, std::string> escapes; escapes['\a'] = "\\a"; escapes['\b'] = "\\b"; escapes['\f'] = "\\f"; escapes['\n'] = "\\n"; escapes['\t'] = "\\t"; escapes['\v'] = "\\v"; escapes['\r'] = "\\r"; for (std::map<char, std::string>::iterator it = escapes.begin(); it != escapes.end(); ++it) for (size_t pos=0; (pos=str.find(it->first, pos)) != std::string::npos; pos+=it->second.size()) str.replace(pos, 1, it->second); }
/** Remplace the newline characters by a space * * It is used to strip newline characters in strings comming from the * admin site. * * \param s The string to modify * * \return The modified string * */ std::string RainbruRPG::Core::StringConv:: xmlStripNewLine(std::string s){ int i=0; char c=13; std::string search(1, c); do { i=s.find(search, i); if (i!=-1){ s.replace(i, 1, " "); } } while (i!=-1); return s; }
std::string simplify_white_space( std::string str ) { char const* separator = " \t\r\n"; std::string::size_type pos = str.find_first_not_of(separator); str.erase(0,pos); pos = str.find_last_not_of(separator); str.erase(pos+1); pos = 0; while( ( pos = str.find( " ", pos ) ) != std::string::npos ) str.replace( pos, 2, " " ); return str; }