MCPcopy Create free account
hub / github.com/chen3feng/toft / ReplaceAll

Function ReplaceAll

base/string/algorithm.cpp:49–70  ·  view source on GitHub ↗

Replace all the "old" pattern with the "new" pattern in a string

Source from the content-addressed store, hash-verified

47
48// Replace all the "old" pattern with the "new" pattern in a string
49std::string ReplaceAll(const StringPiece& s, const StringPiece& oldsub,
50 const StringPiece& newsub)
51{
52 if (oldsub.empty())
53 return s.as_string();
54
55 std::string res;
56 std::string::size_type start_pos = 0;
57 std::string::size_type pos;
58 do {
59 pos = s.find(oldsub, start_pos);
60 if (pos == std::string::npos)
61 {
62 break;
63 }
64 res.append(s.data() + start_pos, pos - start_pos);
65 res.append(newsub.data(), newsub.size());
66 start_pos = pos + oldsub.size();
67 } while (true);
68 res.append(s.data() + start_pos, s.length() - start_pos);
69 return res;
70}
71
72void ReplaceAll(std::string* s, const StringPiece& from, const StringPiece& to)
73{

Callers 3

GetNewWriterMethod · 0.85
RemoveAllFunction · 0.85
TESTFunction · 0.85

Calls 6

as_stringMethod · 0.80
findMethod · 0.80
dataMethod · 0.80
emptyMethod · 0.45
sizeMethod · 0.45
lengthMethod · 0.45

Tested by 1

TESTFunction · 0.68