aboutsummaryrefslogtreecommitdiffstats
path: root/util/string/util.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/string/util.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/string/util.cpp')
-rw-r--r--util/string/util.cpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/util/string/util.cpp b/util/string/util.cpp
new file mode 100644
index 00000000000..b14f20bf75a
--- /dev/null
+++ b/util/string/util.cpp
@@ -0,0 +1,72 @@
+#include "util.h"
+
+#include <util/generic/utility.h>
+
+#include <cstdio>
+#include <cstdarg>
+#include <cstdlib>
+
+int a2i(const TString& s) {
+ return atoi(s.c_str());
+}
+
+//============================== span =====================================
+
+void str_spn::init(const char* charset, bool extended) {
+ // chars_table_1 is necessary to avoid some unexpected
+ // multi-threading issues
+ ui8 chars_table_1[256];
+ memset(chars_table_1, 0, sizeof(chars_table_1));
+ if (extended) {
+ for (const char* cs = charset; *cs; cs++) {
+ if (cs[1] == '-' && cs[2] != 0) {
+ for (int c = (ui8)*cs; c <= (ui8)cs[2]; c++) {
+ chars_table_1[c] = 1;
+ }
+ cs += 2;
+ continue;
+ }
+ chars_table_1[(ui8)*cs] = 1;
+ }
+ } else {
+ for (; *charset; charset++) {
+ chars_table_1[(ui8)*charset] = 1;
+ }
+ }
+ memcpy(chars_table, chars_table_1, 256);
+ chars_table_1[0] = 1;
+ for (int n = 0; n < 256; n++) {
+ c_chars_table[n] = !chars_table_1[n];
+ }
+}
+
+Tr::Tr(const char* from, const char* to) {
+ for (size_t n = 0; n < 256; n++) {
+ Map[n] = (char)n;
+ }
+ for (; *from && *to; from++, to++) {
+ Map[(ui8)*from] = *to;
+ }
+}
+
+size_t Tr::FindFirstChangePosition(const TString& str) const {
+ for (auto it = str.begin(); it != str.end(); ++it) {
+ if (ConvertChar(*it) != *it) {
+ return it - str.begin();
+ }
+ }
+
+ return TString::npos;
+}
+
+void Tr::Do(TString& str) const {
+ const size_t changePosition = FindFirstChangePosition(str);
+
+ if (changePosition == TString::npos) {
+ return;
+ }
+
+ for (auto it = str.begin() + changePosition; it != str.end(); ++it) {
+ *it = ConvertChar(*it);
+ }
+}