MCPcopy Create free account
hub / github.com/catboost/catboost / SubstGlobalImpl

Function SubstGlobalImpl

util/string/subst.cpp:47–98  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45 **/
46template <class TStringType, typename TStringViewType = TBasicStringBuf<typename TStringType::value_type>>
47static inline size_t SubstGlobalImpl(TStringType& s, const TStringViewType from, const TStringViewType to, size_t fromPos = 0) {
48 if (from.empty()) {
49 return 0;
50 }
51
52 Y_ASSERT(!IsIntersect(s, from));
53 Y_ASSERT(!IsIntersect(s, to));
54
55 const size_t fromSize = from.size();
56 const size_t toSize = to.size();
57 size_t replacementsCount = 0;
58 size_t off = fromPos;
59 size_t srcPos = 0;
60
61 if (toSize > fromSize) {
62 // string will grow: append to another string
63 TStringType result;
64 for (; (off = TStringViewType(s).find(from, off)) != TStringType::npos; off += fromSize) {
65 if (!replacementsCount) {
66 // first replacement occured, we can prepare result string
67 result.reserve(s.size() + s.size() / 3);
68 }
69 result.append(s.begin() + srcPos, s.begin() + off);
70 result.append(to.data(), to.size());
71 srcPos = off + fromSize;
72 ++replacementsCount;
73 }
74 if (replacementsCount) {
75 // append tail
76 result.append(s.begin() + srcPos, s.end());
77 s = std::move(result);
78 }
79 return replacementsCount;
80 }
81
82 // string will not grow: use inplace algo
83 size_t dstPos = 0;
84 typename TStringType::value_type* ptr = &*s.begin();
85 for (; (off = TStringViewType(s).find(from, off)) != TStringType::npos; off += fromSize) {
86 Y_ASSERT(dstPos <= srcPos);
87 MoveBlock<TStringType, TStringViewType, true>(ptr, srcPos, dstPos, off, to, toSize);
88 srcPos = off + fromSize;
89 ++replacementsCount;
90 }
91
92 if (replacementsCount) {
93 // append tail
94 MoveBlock<TStringType, TStringViewType, false>(ptr, srcPos, dstPos, s.size(), to, toSize);
95 s.resize(dstPos);
96 }
97 return replacementsCount;
98}
99
100/// Replaces all occurences of the 'from' symbol in a string to the 'to' symbol.
101template <class TStringType>

Callers 1

SubstGlobalFunction · 0.70

Calls 11

IsIntersectFunction · 0.70
moveFunction · 0.50
emptyMethod · 0.45
sizeMethod · 0.45
findMethod · 0.45
reserveMethod · 0.45
appendMethod · 0.45
beginMethod · 0.45
dataMethod · 0.45
endMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected