aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/string_utils
diff options
context:
space:
mode:
authorvitalyisaev <vitalyisaev@ydb.tech>2023-11-30 13:26:22 +0300
committervitalyisaev <vitalyisaev@ydb.tech>2023-11-30 15:44:45 +0300
commit0a98fece5a9b54f16afeb3a94b3eb3105e9c3962 (patch)
tree291d72dbd7e9865399f668c84d11ed86fb190bbf /library/cpp/string_utils
parentcb2c8d75065e5b3c47094067cb4aa407d4813298 (diff)
downloadydb-0a98fece5a9b54f16afeb3a94b3eb3105e9c3962.tar.gz
YQ Connector:Use docker-compose in integrational tests
Diffstat (limited to 'library/cpp/string_utils')
-rw-r--r--library/cpp/string_utils/subst_buf/substbuf.cpp1
-rw-r--r--library/cpp/string_utils/subst_buf/substbuf.h63
-rw-r--r--library/cpp/string_utils/subst_buf/ya.make7
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()