diff options
| author | aneporada <[email protected]> | 2026-02-07 00:14:51 +0300 |
|---|---|---|
| committer | aneporada <[email protected]> | 2026-02-07 00:47:28 +0300 |
| commit | 6e9efe623fc1105836d6c2114c5ddc9fbf57bee9 (patch) | |
| tree | a5308933adc4d2e0379d19da098c24851e2d8cdd /library/cpp/type_info | |
| parent | eee21ee8f272da8f55cdccf80dba6a5157d51ad9 (diff) | |
Add fast timezone id validation function
commit_hash:a2c20ffc1acc83385a3195d76eb95818b3ad6a67
Diffstat (limited to 'library/cpp/type_info')
| -rw-r--r-- | library/cpp/type_info/tz/is_valid_gen.h | 2 | ||||
| -rw-r--r-- | library/cpp/type_info/tz/tz.cpp | 2 | ||||
| -rw-r--r-- | library/cpp/type_info/tz/tz.h | 7 | ||||
| -rw-r--r-- | library/cpp/type_info/tz/tz_gen.h (renamed from library/cpp/type_info/tz/tz.gen) | 0 | ||||
| -rw-r--r-- | library/cpp/type_info/tz/tz_ut.cpp | 8 | ||||
| -rwxr-xr-x | library/cpp/type_info/tz/update.py | 13 |
6 files changed, 29 insertions, 3 deletions
diff --git a/library/cpp/type_info/tz/is_valid_gen.h b/library/cpp/type_info/tz/is_valid_gen.h new file mode 100644 index 00000000000..70f1029deff --- /dev/null +++ b/library/cpp/type_info/tz/is_valid_gen.h @@ -0,0 +1,2 @@ +// this file is generated, do not edit +return idx < 600 && idx != 586 && idx != 593; diff --git a/library/cpp/type_info/tz/tz.cpp b/library/cpp/type_info/tz/tz.cpp index b2c3fc9d7f0..409519dc976 100644 --- a/library/cpp/type_info/tz/tz.cpp +++ b/library/cpp/type_info/tz/tz.cpp @@ -5,7 +5,7 @@ namespace NTi { namespace { static constexpr std::initializer_list<const std::string_view> TimezonesInit = { -#include "tz.gen" +#include "tz_gen.h" }; static constexpr TArrayRef<const std::string_view> Timezones(TimezonesInit); diff --git a/library/cpp/type_info/tz/tz.h b/library/cpp/type_info/tz/tz.h index b683269af29..51ccd15276a 100644 --- a/library/cpp/type_info/tz/tz.h +++ b/library/cpp/type_info/tz/tz.h @@ -6,4 +6,11 @@ namespace NTi { TArrayRef<const std::string_view> GetTimezones(); +inline bool IsValidTimezoneIndex(size_t idx) { + // this function is equivalent to: + // const auto zones = GetTimezones(); + // return idx < zones.size() && !zones[idx].empty(); + #include "is_valid_gen.h" +} + } diff --git a/library/cpp/type_info/tz/tz.gen b/library/cpp/type_info/tz/tz_gen.h index e71c9cc81e7..e71c9cc81e7 100644 --- a/library/cpp/type_info/tz/tz.gen +++ b/library/cpp/type_info/tz/tz_gen.h diff --git a/library/cpp/type_info/tz/tz_ut.cpp b/library/cpp/type_info/tz/tz_ut.cpp index 65e8340cdc5..781c80fa226 100644 --- a/library/cpp/type_info/tz/tz_ut.cpp +++ b/library/cpp/type_info/tz/tz_ut.cpp @@ -13,4 +13,12 @@ Y_UNIT_TEST_SUITE(TTz) { UNIT_ASSERT(GetTimezones().size() > 0); UNIT_ASSERT_VALUES_EQUAL(GetTimezones()[0], "GMT"); } + + Y_UNIT_TEST(IsValidTimezoneIndex) { + auto timezones = GetTimezones(); + for (size_t i = 0; i < timezones.size(); ++i) { + UNIT_ASSERT(IsValidTimezoneIndex(i) == !timezones[i].empty()); + } + UNIT_ASSERT(!IsValidTimezoneIndex(timezones.size())); + } } diff --git a/library/cpp/type_info/tz/update.py b/library/cpp/type_info/tz/update.py index d1d0f71decf..f6ca4f1b904 100755 --- a/library/cpp/type_info/tz/update.py +++ b/library/cpp/type_info/tz/update.py @@ -5,12 +5,13 @@ import re def main(): REGEXP = """^("(.+)",|"",//(.+))$""" - OUTPUT = "../tz.gen" + OUTPUT = "../tz_gen.h" + OUTPUT_VALID = "../is_valid_gen.h" INPUT = OUTPUT CONTRIB_ZONES = "../../../../../contrib/libs/cctz/tzdata/ya.make.resources" RES_PREFIX = "/cctz/tzdata/" - print("process %s into %s" % (INPUT, OUTPUT)) + print("process %s into %s and %s" % (INPUT, OUTPUT, OUTPUT_VALID)) with open(INPUT, "r") as inp: lines = inp.readlines() @@ -43,15 +44,23 @@ def main(): zones.append(zone) lines = [] + idx = 0 + pred = "" for zone in zones: if zone in scontrib: lines.append('"%s",\n' % zone) else: lines.append('"",//%s\n' % zone) + pred += " && idx != %s" % idx + idx += 1 with open(OUTPUT, "w") as outp: outp.writelines(lines) + with open(OUTPUT_VALID, "w") as outp: + outp.write("// this file is generated, do not edit\n") + outp.write("return idx < %s%s;\n" % (len(zones), pred)) + print("saved %s zones" % len(zones)) |
