diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2023-12-02 01:45:21 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2023-12-02 02:42:50 +0300 |
commit | 9c43d58f75cf086b744cf4fe2ae180e8f37e4a0c (patch) | |
tree | 9f88a486917d371d099cd712efd91b4c122d209d /library/cpp/string_utils | |
parent | 32fb6dda1feb24f9ab69ece5df0cb9ec238ca5e6 (diff) | |
download | ydb-9c43d58f75cf086b744cf4fe2ae180e8f37e4a0c.tar.gz |
Intermediate changes
Diffstat (limited to 'library/cpp/string_utils')
-rw-r--r-- | library/cpp/string_utils/subst_buf/substbuf.cpp | 1 | ||||
-rw-r--r-- | library/cpp/string_utils/subst_buf/substbuf.h | 63 | ||||
-rw-r--r-- | library/cpp/string_utils/subst_buf/ya.make | 7 |
3 files changed, 71 insertions, 0 deletions
diff --git a/library/cpp/string_utils/subst_buf/substbuf.cpp b/library/cpp/string_utils/subst_buf/substbuf.cpp new file mode 100644 index 0000000000..f23cb24b19 --- /dev/null +++ b/library/cpp/string_utils/subst_buf/substbuf.cpp @@ -0,0 +1 @@ +#include "substbuf.h" diff --git a/library/cpp/string_utils/subst_buf/substbuf.h b/library/cpp/string_utils/subst_buf/substbuf.h new file mode 100644 index 0000000000..357ee68ae3 --- /dev/null +++ b/library/cpp/string_utils/subst_buf/substbuf.h @@ -0,0 +1,63 @@ +#pragma once + +#include <util/generic/vector.h> +#include <util/generic/strbuf.h> +#include <util/string/subst.h> + +/// Заменяет в строке одни подстроки на другие. +template <class TBuf, class TPool> +size_t SubstGlobal(TBuf& s, const TBuf& from, const TBuf& to, TPool& pool) { + if (from.empty()) + return 0; + + TVector<size_t> offs; + for (size_t off = 0; (off = s.find(from, off)) != TBuf::npos; off += from.length()) + offs.push_back(off); + if (offs.empty()) + return 0; + + size_t dstSize = s.size() + ssize_t(offs.size()) * ssize_t(to.size() - from.size()); + const size_t charTypeSz = sizeof(typename TBuf::char_type); + typename TBuf::char_type* dst = (typename TBuf::char_type*)pool.Allocate((dstSize + 1) * charTypeSz); + dst[dstSize] = 0; + + typename TBuf::char_type* p = dst; + size_t lastSrc = 0; + for (auto off : offs) { + memcpy(p, s.data() + lastSrc, (off - lastSrc) * charTypeSz); + p += off - lastSrc; + lastSrc = off + from.size(); + memcpy(p, to.data(), to.size() * charTypeSz); + p += to.size(); + } + memcpy(p, s.data() + lastSrc, (s.size() - lastSrc) * charTypeSz); + p += s.size() - lastSrc; + Y_ASSERT(p - dst == (ssize_t)dstSize); + + s = TBuf(dst, dstSize); + return offs.size(); +} + +template <class TPool> +size_t SubstGlobal(TStringBuf& s, const TStringBuf& from, const TStringBuf& to, TPool& pool) { + return SubstGlobal<TStringBuf, TPool>(s, from, to, pool); +} + +/// Заменяет в строке одни подстроки на другие. +template <class TBuf, class TPool> +inline size_t SubstGlobal(TBuf& s, typename TBuf::char_type from, typename TBuf::char_type to, TPool& pool) { + size_t result = 0; + size_t off = s.find(from); + if (off == TBuf::npos) + return 0; + + s = TBuf(pool.Append(s), s.size()); + + for (typename TBuf::char_type* it = const_cast<typename TBuf::char_type*>(s.begin()) + off; it != s.end(); ++it) { + if (*it == from) { + *it = to; + ++result; + } + } + return result; +} diff --git a/library/cpp/string_utils/subst_buf/ya.make b/library/cpp/string_utils/subst_buf/ya.make new file mode 100644 index 0000000000..8b8793f5b3 --- /dev/null +++ b/library/cpp/string_utils/subst_buf/ya.make @@ -0,0 +1,7 @@ +LIBRARY() + +SRCS( + substbuf.cpp +) + +END() |