From 178e11aaf9234fc096b7a7008a3182786af549ab Mon Sep 17 00:00:00 2001 From: Bulat Date: Thu, 7 Mar 2024 13:17:23 +0300 Subject: [PATCH] Removed TString, TStringBuf, TVector, TMap, TList, TMutex, TCondVar from util, refactor string utils (#82) --- client/iam/common/iam.h | 39 +- .../db_driver_state/endpoint_pool.cpp | 6 +- .../grpc_connections/grpc_connections.h | 6 +- .../kqp_session_common/kqp_session_common.cpp | 34 +- .../impl/ydb_internal/logger/CMakeLists.txt | 1 - client/impl/ydb_internal/logger/log.cpp | 4 +- .../ydb_internal/plain_status/CMakeLists.txt | 1 - .../impl/ydb_internal/plain_status/status.cpp | 4 +- .../impl/ydb_internal/plain_status/status.h | 4 +- .../ydb_internal/value_helpers/CMakeLists.txt | 1 - .../ydb_internal/value_helpers/helpers.cpp | 6 +- client/impl/ydb_stats/CMakeLists.txt | 1 - client/impl/ydb_stats/stats.h | 8 +- client/resources/ydb_ca.h | 2 +- client/resources/ydb_resources.h | 2 +- client/ydb_coordination/coordination.cpp | 362 ++--- client/ydb_federated_topic/federated_topic.h | 18 +- .../impl/federated_read_session.cpp | 47 +- .../impl/federated_read_session.h | 2 +- .../impl/federated_read_session_event.cpp | 18 +- .../impl/federated_topic_impl.cpp | 9 +- .../impl/federated_topic_impl.h | 12 +- .../impl/federated_write_session.cpp | 53 +- .../impl/federation_observer.cpp | 34 +- client/ydb_federated_topic/ut/fds_mock.h | 12 +- client/ydb_operation/CMakeLists.txt | 1 - client/ydb_operation/operation.h | 4 +- client/ydb_params/CMakeLists.txt | 1 - client/ydb_params/params.cpp | 10 +- client/ydb_persqueue_core/CMakeLists.txt | 1 - client/ydb_persqueue_core/impl/CMakeLists.txt | 1 - client/ydb_persqueue_core/impl/common.cpp | 4 +- .../ydb_persqueue_core/impl/counters_logger.h | 36 +- .../impl/persqueue_impl.cpp | 42 +- .../ydb_persqueue_core/impl/read_session.cpp | 146 +- client/ydb_persqueue_core/impl/read_session.h | 6 +- .../ydb_persqueue_core/impl/read_session.ipp | 470 +++--- .../impl/read_session_messages.cpp | 14 +- .../impl/write_session_impl.cpp | 86 +- .../impl/write_session_impl.h | 2 +- client/ydb_persqueue_core/persqueue.h | 17 +- .../ydb_persqueue_core/ut/read_session_ut.cpp | 72 +- .../ydb_persqueue_core/ut/ut_utils/ut_utils.h | 29 +- .../codecs/CMakeLists.txt | 3 +- client/ydb_persqueue_public/codecs/codecs.cpp | 2 +- client/ydb_result/CMakeLists.txt | 4 +- client/ydb_result/result.cpp | 19 +- client/ydb_scheme/CMakeLists.txt | 3 +- client/ydb_scheme/scheme.cpp | 3 +- client/ydb_table/CMakeLists.txt | 3 +- client/ydb_table/impl/client_session.cpp | 36 +- client/ydb_table/impl/request_migrator.h | 2 +- client/ydb_table/table.cpp | 10 +- client/ydb_topic/CMakeLists.txt | 1 - client/ydb_topic/codecs/codecs.cpp | 2 +- client/ydb_topic/impl/CMakeLists.txt | 1 - client/ydb_topic/impl/deferred_commit.cpp | 4 +- client/ydb_topic/impl/event_handlers.cpp | 55 +- client/ydb_topic/impl/read_session.cpp | 38 +- client/ydb_topic/impl/read_session.h | 2 +- client/ydb_topic/impl/read_session_event.cpp | 22 +- client/ydb_topic/impl/topic_impl.cpp | 29 +- client/ydb_topic/impl/write_session_impl.cpp | 93 +- client/ydb_topic/impl/write_session_impl.h | 2 +- client/ydb_topic/topic.h | 43 +- client/ydb_types/credentials/login/login.cpp | 2 +- .../ydb_types/fatal_error_handlers/handlers.h | 2 +- client/ydb_types/operation/CMakeLists.txt | 3 +- client/ydb_types/operation/operation.cpp | 3 +- client/ydb_value/CMakeLists.txt | 1 - client/ydb_value/value.cpp | 77 +- contrib/libs/lzmasdk/LzmaEnc.c | 2 +- contrib/libs/snappy/snappy.cc | 28 +- contrib/libs/snappy/snappy.h | 4 - examples/secondary_index/secondary_index.h | 2 +- .../secondary_index_generate.cpp | 25 +- .../secondary_index_builtin/secondary_index.h | 2 +- examples/topic_reader/transaction/options.h | 2 +- library/cpp/CMakeLists.txt | 1 - .../blockcodecs/codecs/brotli/CMakeLists.txt | 2 - .../cpp/blockcodecs/codecs/brotli/brotli.cpp | 4 +- .../codecs/legacy_zstd06/CMakeLists.txt | 2 - .../codecs/legacy_zstd06/legacy_zstd06.cpp | 4 +- .../cpp/blockcodecs/codecs/lz4/CMakeLists.txt | 2 - library/cpp/blockcodecs/codecs/lz4/lz4.cpp | 6 +- .../blockcodecs/codecs/zstd/CMakeLists.txt | 2 - library/cpp/blockcodecs/codecs/zstd/zstd.cpp | 4 +- library/cpp/blockcodecs/core/codecs.h | 4 +- library/cpp/build_info/sandbox.cpp.in | 3 +- library/cpp/cache/thread_safe_cache.h | 1 + .../case_insensitive_string.cpp | 10 - .../case_insensitive_string.h | 8 +- library/cpp/cgiparam/cgiparam.h | 4 +- library/cpp/charset/ci_string.cpp | 12 +- library/cpp/charset/ci_string.h | 8 +- library/cpp/charset/codepage.cpp | 6 +- library/cpp/charset/codepage.h | 6 +- library/cpp/charset/recyr_int.hh | 2 +- library/cpp/charset/wide_ut.cpp | 6 +- library/cpp/colorizer/colors.cpp | 2 +- library/cpp/colorizer/colors.h | 6 +- .../containers/paged_vector/paged_vector.h | 18 +- library/cpp/coroutine/engine/helper.h | 2 +- library/cpp/coroutine/engine/sockpool.h | 11 +- .../cpp/coroutine/engine/stack/stack_guards.h | 2 +- library/cpp/cppparser/parser.h | 2 +- library/cpp/dbg_output/CMakeLists.txt | 1 - library/cpp/dbg_output/dumpers.h | 10 +- library/cpp/dbg_output/engine.cpp | 3 +- library/cpp/dbg_output/engine.h | 1 + library/cpp/diff/diff.cpp | 4 +- library/cpp/diff/diff.h | 4 +- library/cpp/digest/lower_case/hash_ops.h | 2 +- library/cpp/digest/lower_case/lchash.h | 2 +- library/cpp/digest/md5/md5.h | 2 +- library/cpp/getopt/small/CMakeLists.txt | 4 +- library/cpp/getopt/small/completer.cpp | 10 +- library/cpp/getopt/small/completer.h | 2 + .../cpp/getopt/small/completion_generator.cpp | 8 +- library/cpp/getopt/small/formatted_output.cpp | 4 +- library/cpp/getopt/small/formatted_output.h | 6 +- library/cpp/getopt/small/last_getopt.h | 1 + library/cpp/getopt/small/last_getopt_opts.cpp | 2 +- .../cpp/getopt/small/last_getopt_support.h | 4 +- library/cpp/getopt/small/modchooser.cpp | 3 +- library/cpp/getopt/small/wrap.cpp | 4 +- library/cpp/http/fetch/http_socket.cpp | 11 +- library/cpp/http/fetch/httpheader.h | 4 +- library/cpp/http/fetch/sockhandler.h | 2 - library/cpp/http/io/CMakeLists.txt | 1 - library/cpp/http/io/compression.cpp | 4 +- library/cpp/http/io/headers.cpp | 2 +- library/cpp/http/io/headers.h | 8 +- library/cpp/http/io/headers_ut.cpp | 4 +- library/cpp/http/io/stream.h | 4 +- library/cpp/http/misc/CMakeLists.txt | 1 - library/cpp/http/misc/httpreqdata.cpp | 8 +- library/cpp/http/misc/parsed_request.cpp | 5 +- library/cpp/http/server/http.cpp | 20 +- library/cpp/http/server/http_ut.cpp | 29 +- library/cpp/http/simple/http_client.cpp | 16 +- library/cpp/json/CMakeLists.txt | 1 - library/cpp/json/common/defs.h | 2 +- .../json/fast_sax/CMakeLists.darwin-arm64.txt | 1 - .../fast_sax/CMakeLists.darwin-x86_64.txt | 1 - .../fast_sax/CMakeLists.linux-aarch64.txt | 1 - .../json/fast_sax/CMakeLists.linux-x86_64.txt | 1 - .../fast_sax/CMakeLists.windows-x86_64.txt | 1 - library/cpp/json/fast_sax/parser.rl6 | 6 +- library/cpp/json/json_reader.cpp | 4 +- library/cpp/json/json_writer.cpp | 4 +- library/cpp/json/writer/CMakeLists.txt | 1 - library/cpp/json/writer/json.cpp | 4 +- library/cpp/json/writer/json_value.cpp | 6 +- library/cpp/logger/CMakeLists.txt | 1 - library/cpp/logger/backend.cpp | 11 +- library/cpp/logger/composite.h | 2 +- library/cpp/logger/element_ut.cpp | 2 +- library/cpp/logger/global/common.cpp | 1 + library/cpp/logger/log_ut.cpp | 2 +- library/cpp/logger/record.h | 2 +- library/cpp/logger/reopen_ut.cpp | 2 +- library/cpp/logger/rotating_file.cpp | 6 +- library/cpp/logger/rotating_file_ut.cpp | 2 +- library/cpp/logger/stream.cpp | 4 +- library/cpp/logger/stream.h | 2 +- library/cpp/logger/sync_page_cache_file.cpp | 9 +- library/cpp/mime/types/mime.cpp | 2 +- library/cpp/monlib/counters/counters.h | 6 +- .../monlib/dynamic_counters/CMakeLists.txt | 1 - .../cpp/monlib/dynamic_counters/counters.cpp | 4 +- .../cpp/monlib/encode/buffered/CMakeLists.txt | 1 - .../encode/buffered/buffered_encoder_base.cpp | 6 +- .../monlib/encode/buffered/string_pool.cpp | 2 + library/cpp/monlib/encode/format.h | 2 +- library/cpp/monlib/encode/format_ut.cpp | 2 +- library/cpp/monlib/encode/json/CMakeLists.txt | 1 - library/cpp/monlib/encode/json/fuzz/main.cpp | 2 +- .../cpp/monlib/encode/json/json_decoder.cpp | 6 +- .../monlib/encode/prometheus/CMakeLists.txt | 1 - .../encode/prometheus/prometheus_decoder.cpp | 4 +- .../cpp/monlib/metrics/histogram_snapshot.h | 1 + library/cpp/monlib/metrics/labels.h | 4 - library/cpp/monlib/metrics/metric_type.cpp | 2 +- library/cpp/monlib/service/monservice.cpp | 1 - .../monlib/service/pages/index_mon_page.cpp | 14 +- .../cpp/monlib/service/pages/index_mon_page.h | 3 +- library/cpp/monlib/service/pages/mon_page.h | 2 +- library/cpp/object_factory/object_factory.h | 1 + library/cpp/openssl/init/init.cpp | 10 +- library/cpp/resource/registry.cpp | 5 +- library/cpp/resource/registry.h | 4 +- library/cpp/resource/resource.h | 4 +- library/cpp/string_builder/CMakeLists.txt | 17 - library/cpp/string_builder/string_builder.cpp | 6 - library/cpp/string_builder/string_builder.h | 40 - library/cpp/string_utils/CMakeLists.txt | 3 +- .../cpp/string_utils/escape/CMakeLists.txt | 15 - library/cpp/string_utils/escape/escape.cpp | 193 --- library/cpp/string_utils/escape/escape.h | 40 - library/cpp/string_utils/escape/escape_ut.cpp | 148 -- .../cpp/string_utils/helpers/CMakeLists.txt | 15 + .../{misc/misc.cpp => helpers/helpers.cpp} | 49 +- library/cpp/string_utils/helpers/helpers.h | 64 + library/cpp/string_utils/misc/CMakeLists.txt | 14 +- library/cpp/string_utils/misc/misc.h | 37 +- .../relaxed_escaper/relaxed_escaper.h | 2 +- library/cpp/string_utils/scan/scan.h | 3 +- .../string_utils/string_output/CMakeLists.txt | 16 - .../string_output/string_output.cpp | 24 - .../string_output/string_output.h | 26 - library/cpp/string_utils/url/CMakeLists.txt | 1 - library/cpp/string_utils/url/url.cpp | 8 +- library/cpp/string_utils/url/url.h | 6 +- library/cpp/svnversion/svnversion.cpp | 2 +- .../terminate_handler/terminate_handler.cpp | 2 + library/cpp/testing/common/env.cpp | 4 +- library/cpp/testing/common/env.h | 4 +- library/cpp/testing/common/scope.h | 2 +- .../cpp/testing/gmock_in_unittest/events.cpp | 4 +- .../example_ut/example_ut.cpp | 2 +- library/cpp/testing/gtest/matchers.h | 2 +- .../cpp/testing/gtest_extensions/assertions.h | 2 +- .../cpp/testing/gtest_extensions/matchers.h | 30 +- .../gtest_extensions/pretty_printers.h | 12 +- .../ut/gtest_extensions_ut.cpp | 2 +- library/cpp/testing/unittest/checks.cpp | 2 +- library/cpp/testing/unittest/registar.cpp | 12 +- library/cpp/testing/unittest/tests_data.cpp | 9 +- library/cpp/testing/unittest/tests_data.h | 2 +- library/cpp/testing/unittest/utmain.cpp | 2 +- library/cpp/threading/chunk_queue/queue.h | 16 +- library/cpp/threading/equeue/fast/equeue.h | 151 -- library/cpp/threading/equeue/fast/ya.make | 12 - .../future/CMakeLists.darwin-arm64.txt | 25 - .../future/CMakeLists.darwin-x86_64.txt | 25 - .../future/CMakeLists.linux-aarch64.txt | 26 - .../future/CMakeLists.linux-x86_64.txt | 26 - library/cpp/threading/future/CMakeLists.txt | 34 +- .../future/CMakeLists.windows-x86_64.txt | 25 - .../cpp/threading/future/async_semaphore.cpp | 31 +- .../cpp/threading/future/core/future-inl.h | 56 +- library/cpp/threading/future/core/future.h | 2 + library/cpp/threading/future/perf/main.cpp | 2 +- .../threading/future/wait/wait_group-inl.h | 4 +- .../threading/poor_man_openmp/thread_helper.h | 27 +- .../poor_man_openmp/thread_helper_ut.cpp | 2 +- library/cpp/uri/CMakeLists.darwin-arm64.txt | 3 +- library/cpp/uri/CMakeLists.darwin-x86_64.txt | 3 +- library/cpp/uri/CMakeLists.linux-aarch64.txt | 3 +- library/cpp/uri/CMakeLists.linux-x86_64.txt | 3 +- library/cpp/uri/CMakeLists.windows-x86_64.txt | 3 +- library/cpp/uri/common.h | 4 +- library/cpp/uri/location.h | 2 +- library/cpp/uri/other.cpp | 3 + library/cpp/uri/other.h | 2 +- library/cpp/uri/parse.h | 2 +- library/cpp/uri/qargs.cpp | 2 + library/cpp/uri/uri.h | 5 +- library/cpp/yson/json/CMakeLists.txt | 1 - library/cpp/yson/json/json_writer.cpp | 4 +- library/cpp/yson/varint.cpp | 1 + library/cpp/yt/assert/assert.h | 2 +- library/cpp/yt/misc/CMakeLists.txt | 1 - library/cpp/yt/misc/cast-inl.h | 6 +- library/cpp/yt/misc/enum-inl.h | 5 +- library/cpp/yt/misc/enum.h | 2 +- library/cpp/yt/misc/guid.cpp | 2 +- library/cpp/yt/misc/guid.h | 3 +- library/cpp/yt/misc/strong_typedef-inl.h | 5 +- library/cpp/yt/misc/strong_typedef.h | 2 +- library/cpp/yt/misc/thread_name.cpp | 5 +- library/cpp/yt/misc/thread_name.h | 2 +- library/cpp/yt/misc/variant.h | 2 +- library/cpp/yt/string/guid.h | 2 + library/cpp/yt/string/raw_formatter.h | 2 +- library/cpp/yt/string/string.h | 2 +- library/cpp/yt/string/string_builder.h | 4 +- library/cpp/yt/yson/consumer.h | 2 +- library/cpp/yt/yson_string/convert.h | 2 +- tools/enum_parser/enum_parser/main.cpp | 12 +- .../enum_runtime.cpp | 6 +- .../serialized_enum.h | 4 +- .../enum_parser/parse_enum/benchmark/main.cpp | 6 +- tools/enum_parser/parse_enum/parse_enum.cpp | 5 +- tools/enum_parser/parse_enum/ut/enums.cpp | 8 +- tools/rescompiler/main.cpp | 16 +- util/CMakeLists.darwin-arm64.txt | 10 +- util/CMakeLists.darwin-x86_64.txt | 10 +- util/CMakeLists.linux-aarch64.txt | 10 +- util/CMakeLists.linux-x86_64.txt | 11 +- util/CMakeLists.windows-x86_64.txt | 10 +- util/charset/utf8.cpp | 40 +- util/charset/utf8.h | 32 +- util/charset/utf8_ut.cpp | 44 +- util/charset/wide.cpp | 114 +- util/charset/wide.h | 140 +- util/charset/wide_ut.cpp | 244 ++-- util/datetime/base.cpp | 43 +- util/datetime/base.h | 70 +- util/datetime/base_ut.cpp | 38 +- util/datetime/cputimer.cpp | 8 +- util/datetime/cputimer.h | 9 +- util/datetime/parser.rl6 | 38 +- util/datetime/parser_deprecated_ut.cpp | 16 +- util/datetime/parser_ut.cpp | 20 +- util/datetime/systime.cpp | 2 +- util/datetime/systime.h | 4 +- util/datetime/uptime.cpp | 2 +- util/digest/city.h | 16 +- util/digest/city_ut.cpp | 2 +- util/digest/fnv_ut.cpp | 8 +- util/digest/multi_ut.cpp | 4 +- util/digest/multi_ut.pyx | 5 +- util/digest/sequence_ut.cpp | 6 +- util/draft/date.cpp | 8 +- util/draft/date.h | 10 +- util/draft/datetime.cpp | 2 +- util/draft/datetime.h | 9 +- util/draft/datetime_ut.cpp | 12 +- util/draft/enum.h | 15 +- util/draft/holder_vector.h | 1 - util/draft/holder_vector_ut.cpp | 4 +- util/draft/ip.h | 6 +- util/folder/dirut.cpp | 78 +- util/folder/dirut.h | 37 +- util/folder/dirut_ut.cpp | 19 +- util/folder/filelist.cpp | 6 +- util/folder/filelist.h | 14 +- util/folder/filelist_ut.cpp | 11 +- util/folder/fts_ut.cpp | 3 +- util/folder/iterator.h | 7 +- util/folder/iterator_ut.cpp | 54 +- util/folder/path.cpp | 54 +- util/folder/path.h | 40 +- util/folder/path.pxd | 28 +- util/folder/path_ut.cpp | 78 +- util/folder/path_ut.pyx | 12 +- util/folder/pathsplit.cpp | 66 +- util/folder/pathsplit.h | 49 +- util/folder/tempdir.cpp | 4 +- util/folder/tempdir.h | 9 +- util/generic/algorithm_ut.cpp | 123 +- util/generic/bitops_ut.cpp | 2 +- util/generic/buffer.cpp | 2 +- util/generic/buffer.h | 2 +- util/generic/buffer_ut.cpp | 26 +- util/generic/function_ref_ut.cpp | 2 +- util/generic/fwd.h | 14 - util/generic/guid.cpp | 18 +- util/generic/guid.h | 16 +- util/generic/guid_ut.cpp | 4 +- util/generic/hash_set_ut.pyx | 12 +- util/generic/hash_table.cpp | 22 +- util/generic/hash_table.h | 34 +- util/generic/hash_ut.cpp | 80 +- util/generic/hash_ut.pyx | 31 +- util/generic/is_in_ut.cpp | 49 +- util/generic/iterator_ut.cpp | 10 +- util/generic/join.h | 0 util/generic/lazy_value_ut.cpp | 6 +- util/generic/list.cpp | 1 - util/generic/list.h | 22 - util/generic/list.pxd | 65 - util/generic/list_ut.pyx | 158 -- util/generic/map.cpp | 1 - util/generic/map.h | 36 - util/generic/map.pxd | 46 - util/generic/mapfindptr_ut.cpp | 16 +- util/generic/overloaded.h | 4 +- util/generic/overloaded_ut.cpp | 2 +- util/generic/ptr_ut.cpp | 6 +- util/generic/ptr_ut.pyx | 11 +- util/generic/queue.h | 1 - util/generic/singleton_ut.cpp | 4 +- util/generic/strbase.h | 554 ------- util/generic/strbuf.cpp | 8 - util/generic/strbuf.h | 543 ------- util/generic/strbuf_ut.cpp | 391 ----- util/generic/string.cpp | 155 -- util/generic/string.h | 1294 ----------------- util/generic/string.pxd | 118 -- util/generic/string_transparent_hash_ut.cpp | 7 +- util/generic/string_ut.cpp | 1246 ---------------- util/generic/string_ut.h | 1162 --------------- util/generic/string_ut.pyx | 225 --- util/generic/typetraits.h | 1 + util/generic/typetraits_ut.cpp | 5 +- util/generic/utility_ut.cpp | 2 +- util/generic/vector.cpp | 1 - util/generic/vector.h | 131 -- util/generic/vector.pxd | 83 -- util/generic/vector_ut.cpp | 587 -------- util/generic/vector_ut.pyx | 221 --- util/generic/xrange_ut.cpp | 2 +- util/generic/yexception.cpp | 23 +- util/generic/yexception.h | 17 +- util/generic/yexception_ut.cpp | 20 +- util/memory/blob.cpp | 39 +- util/memory/blob.h | 43 +- util/memory/blob.pxd | 7 +- util/memory/blob_ut.cpp | 14 +- util/memory/blob_ut.pyx | 3 +- util/memory/pool.h | 9 +- util/memory/pool_ut.cpp | 4 +- util/memory/segmented_string_pool.h | 5 +- util/memory/tempbuf_ut.cpp | 2 +- util/network/address.cpp | 10 +- util/network/address.h | 7 +- util/network/address_ut.cpp | 8 +- util/network/endpoint.cpp | 4 +- util/network/endpoint.h | 2 +- util/network/endpoint_ut.cpp | 9 +- util/network/interface.h | 4 +- util/network/ip.h | 11 +- util/network/poller.cpp | 4 +- util/network/pollerimpl.h | 14 +- util/network/sock.h | 24 +- util/network/sock_ut.cpp | 4 +- util/network/socket.cpp | 8 +- util/network/socket.h | 8 +- util/network/socket_ut.cpp | 16 +- util/random/entropy.cpp | 2 +- util/random/shuffle_ut.cpp | 6 +- util/str_stl.h | 75 +- util/stream/aligned.cpp | 2 +- util/stream/aligned.h | 2 +- util/stream/buffer.cpp | 2 + util/stream/buffer_ut.cpp | 4 +- util/stream/buffered.cpp | 8 +- util/stream/buffered.h | 2 +- util/stream/buffered_ut.cpp | 27 +- util/stream/direct_io.cpp | 2 +- util/stream/direct_io.h | 2 +- util/stream/direct_io_ut.cpp | 3 +- util/stream/file.cpp | 6 +- util/stream/file.h | 6 +- util/stream/file_ut.cpp | 12 +- util/stream/format.h | 25 +- util/stream/format_std_ut.cpp | 2 +- util/stream/format_ut.cpp | 10 +- util/stream/hex.cpp | 2 +- util/stream/hex_ut.cpp | 8 +- util/stream/input.cpp | 37 +- util/stream/input.h | 14 +- util/stream/input_ut.cpp | 18 +- util/stream/ios_ut.cpp | 68 +- util/stream/length.cpp | 2 +- util/stream/length.h | 2 +- util/stream/length_ut.cpp | 7 +- util/stream/mem.cpp | 8 +- util/stream/mem.h | 11 +- util/stream/mem_ut.cpp | 4 +- util/stream/multi.cpp | 4 +- util/stream/multi.h | 2 +- util/stream/multi_ut.cpp | 10 +- util/stream/output.cpp | 64 +- util/stream/output.h | 7 +- util/stream/output.pxd | 4 +- util/stream/pipe.cpp | 11 +- util/stream/pipe.h | 7 +- util/stream/printf_ut.cpp | 3 +- util/stream/str.cpp | 4 +- util/stream/str.h | 25 +- util/stream/str.pxd | 4 +- util/stream/str_ut.cpp | 40 +- util/stream/str_ut.pyx | 18 +- util/stream/tokenizer.h | 5 +- util/stream/tokenizer_ut.cpp | 47 +- util/stream/walk.cpp | 1 - util/stream/walk_ut.cpp | 14 +- util/stream/zerocopy.cpp | 2 +- util/stream/zerocopy.h | 2 +- util/stream/zerocopy_output_ut.cpp | 9 +- util/stream/zlib.cpp | 10 +- util/stream/zlib.h | 8 +- util/stream/zlib_ut.cpp | 14 +- util/string/ascii.cpp | 2 +- util/string/ascii.h | 29 +- util/string/ascii_ut.cpp | 22 +- util/string/builder.cpp | 2 +- util/string/builder.h | 5 +- util/string/builder_ut.cpp | 12 +- util/string/cast.cpp | 58 +- util/string/cast.h | 106 +- util/string/cast.pxd | 8 +- util/string/cast_ut.cpp | 90 +- util/string/escape.cpp | 88 +- util/string/escape.h | 60 +- util/string/escape_ut.cpp | 55 +- util/string/hex.cpp | 14 +- util/string/hex.h | 9 +- util/string/join.h | 105 +- util/string/join_ut.cpp | 18 +- util/string/printf.cpp | 12 +- util/string/printf.h | 8 +- util/string/printf_ut.cpp | 12 +- util/string/reverse.cpp | 9 +- util/string/reverse.h | 8 +- util/string/split.cpp | 6 +- util/string/split.h | 55 +- util/string/split_ut.cpp | 276 ++-- util/string/strip.cpp | 8 +- util/string/strip.h | 18 +- util/string/strip_ut.cpp | 60 +- util/string/strspn_ut.cpp | 16 +- util/string/subst.cpp | 31 +- util/string/subst.h | 16 +- util/string/subst_ut.cpp | 132 +- util/string/type.cpp | 18 +- util/string/type.h | 15 +- util/string/type_ut.cpp | 4 +- util/string/util.cpp | 10 +- util/string/util.h | 18 +- util/string/util_ut.cpp | 2 +- util/string/vector.cpp | 32 +- util/string/vector.h | 42 +- util/string/vector_ut.cpp | 12 +- util/system/atexit.cpp | 21 +- util/system/backtrace.cpp | 9 +- util/system/backtrace.h | 2 +- util/system/compat.cpp | 1 - util/system/compiler.h | 2 +- util/system/condvar.cpp | 145 -- util/system/condvar.h | 71 - util/system/condvar_ut.cpp | 191 --- util/system/context.cpp | 4 +- util/system/demangle_impl.h | 1 - util/system/direct_io.cpp | 12 +- util/system/direct_io.h | 2 +- util/system/direct_io_ut.cpp | 4 +- util/system/dynlib.cpp | 13 +- util/system/dynlib.h | 15 +- util/system/env.cpp | 11 +- util/system/env.h | 5 +- util/system/env_ut.cpp | 23 +- util/system/event.cpp | 34 +- util/system/execpath.cpp | 46 +- util/system/execpath.h | 4 +- util/system/execpath_ut.cpp | 4 +- util/system/fake_mutex.cpp | 1 + util/system/fake_mutex.h | 28 + util/system/fasttime.cpp | 3 +- util/system/file.cpp | 86 +- util/system/file.h | 14 +- util/system/file_lock.cpp | 2 +- util/system/file_lock.h | 2 +- util/system/file_ut.cpp | 18 +- util/system/filemap.cpp | 42 +- util/system/filemap.h | 32 +- util/system/filemap_ut.cpp | 8 +- util/system/flock_ut.cpp | 12 +- util/system/fs.cpp | 35 +- util/system/fs.h | 39 +- util/system/fs_ut.cpp | 12 +- util/system/fs_win.cpp | 58 +- util/system/fs_win.h | 22 +- util/system/fs_win_ut.cpp | 11 +- util/system/fstat.cpp | 4 +- util/system/fstat.h | 4 +- util/system/fstat_ut.cpp | 4 +- util/system/guard.h | 6 +- util/system/hostname.cpp | 18 +- util/system/hostname.h | 6 +- util/system/info.cpp | 12 +- util/system/madvise.cpp | 6 +- util/system/mem_info.cpp | 21 +- util/system/mktemp.cpp | 4 +- util/system/mutex.cpp | 146 -- util/system/mutex.h | 64 - util/system/mutex_ut.cpp | 128 -- util/system/progname.cpp | 4 +- util/system/progname.h | 4 +- util/system/progname_ut.cpp | 4 +- util/system/protect.cpp | 5 +- util/system/rwlock.cpp | 90 +- util/system/sem.cpp | 4 +- util/system/shellcommand.cpp | 88 +- util/system/shellcommand.h | 31 +- util/system/shellcommand_ut.cpp | 65 +- util/system/spinlock.h | 2 +- util/system/src_location.cpp | 2 +- util/system/src_location.h | 10 +- util/system/src_location_ut.cpp | 2 +- util/system/src_root_ut.cpp | 4 +- util/system/tempfile.cpp | 6 +- util/system/tempfile.h | 15 +- util/system/tempfile_ut.cpp | 24 +- util/system/thread.cpp | 8 +- util/system/thread.h | 9 +- util/system/thread_ut.cpp | 4 +- util/system/tls.cpp | 10 +- util/system/type_name.cpp | 10 +- util/system/type_name.h | 11 +- util/system/type_name_ut.cpp | 6 +- util/system/user.cpp | 14 +- util/system/user.h | 2 +- util/system/yassert.cpp | 16 +- util/thread/fwd.h | 11 - util/thread/lfqueue.cpp | 1 - util/thread/lfqueue.h | 399 ----- util/thread/lfqueue_ut.cpp | 331 ----- util/thread/lfstack.cpp | 1 - util/thread/lfstack.h | 192 --- util/thread/lfstack_ut.cpp | 347 ----- util/thread/pool.cpp | 128 +- util/thread/pool.h | 12 +- util/thread/pool_ut.cpp | 40 +- util/ysaveload.cpp | 2 +- util/ysaveload.h | 14 +- util/ysaveload_ut.cpp | 46 +- ydb/library/grpc/client/CMakeLists.txt | 1 - ydb/library/grpc/client/grpc_client_low.h | 6 +- ydb/library/grpc/client/grpc_common.h | 2 +- ydb/library/login/CMakeLists.txt | 1 - ydb/library/login/login.cpp | 6 +- ydb/library/login/login.h | 2 +- .../topic_parser_public/CMakeLists.txt | 1 - .../topic_parser_public/topic_parser.cpp | 16 +- ydb/library/yql/public/decimal/yql_decimal.h | 2 +- ydb/library/yql/public/issue/yql_issue.h | 6 +- ydb/library/yql/utils/utf8.cpp | 2 +- ydb/public/lib/json_value/ydb_json_value.cpp | 46 +- ydb/public/lib/operation_id/operation_id.h | 4 +- ydb/public/lib/yson_value/CMakeLists.txt | 3 +- ydb/public/lib/yson_value/ydb_yson_value.cpp | 6 +- 626 files changed, 4652 insertions(+), 14663 deletions(-) delete mode 100644 library/cpp/string_builder/CMakeLists.txt delete mode 100644 library/cpp/string_builder/string_builder.cpp delete mode 100644 library/cpp/string_builder/string_builder.h delete mode 100644 library/cpp/string_utils/escape/CMakeLists.txt delete mode 100644 library/cpp/string_utils/escape/escape.cpp delete mode 100644 library/cpp/string_utils/escape/escape.h delete mode 100644 library/cpp/string_utils/escape/escape_ut.cpp create mode 100644 library/cpp/string_utils/helpers/CMakeLists.txt rename library/cpp/string_utils/{misc/misc.cpp => helpers/helpers.cpp} (84%) create mode 100644 library/cpp/string_utils/helpers/helpers.h delete mode 100644 library/cpp/string_utils/string_output/CMakeLists.txt delete mode 100644 library/cpp/string_utils/string_output/string_output.cpp delete mode 100644 library/cpp/string_utils/string_output/string_output.h delete mode 100644 library/cpp/threading/equeue/fast/equeue.h delete mode 100644 library/cpp/threading/equeue/fast/ya.make delete mode 100644 library/cpp/threading/future/CMakeLists.darwin-arm64.txt delete mode 100644 library/cpp/threading/future/CMakeLists.darwin-x86_64.txt delete mode 100644 library/cpp/threading/future/CMakeLists.linux-aarch64.txt delete mode 100644 library/cpp/threading/future/CMakeLists.linux-x86_64.txt delete mode 100644 library/cpp/threading/future/CMakeLists.windows-x86_64.txt create mode 100644 util/generic/join.h delete mode 100644 util/generic/list.cpp delete mode 100644 util/generic/list.h delete mode 100644 util/generic/list.pxd delete mode 100644 util/generic/list_ut.pyx delete mode 100644 util/generic/map.cpp delete mode 100644 util/generic/map.h delete mode 100644 util/generic/map.pxd delete mode 100644 util/generic/strbase.h delete mode 100644 util/generic/strbuf.cpp delete mode 100644 util/generic/strbuf.h delete mode 100644 util/generic/strbuf_ut.cpp delete mode 100644 util/generic/string.cpp delete mode 100644 util/generic/string.h delete mode 100644 util/generic/string.pxd delete mode 100644 util/generic/string_ut.cpp delete mode 100644 util/generic/string_ut.h delete mode 100644 util/generic/string_ut.pyx delete mode 100644 util/generic/vector.cpp delete mode 100644 util/generic/vector.h delete mode 100644 util/generic/vector.pxd delete mode 100644 util/generic/vector_ut.cpp delete mode 100644 util/generic/vector_ut.pyx delete mode 100644 util/system/condvar.cpp delete mode 100644 util/system/condvar.h delete mode 100644 util/system/condvar_ut.cpp create mode 100644 util/system/fake_mutex.cpp create mode 100644 util/system/fake_mutex.h delete mode 100644 util/system/mutex.cpp delete mode 100644 util/system/mutex.h delete mode 100644 util/system/mutex_ut.cpp delete mode 100644 util/thread/lfqueue.cpp delete mode 100644 util/thread/lfqueue.h delete mode 100644 util/thread/lfqueue_ut.cpp delete mode 100644 util/thread/lfstack.cpp delete mode 100644 util/thread/lfstack.h delete mode 100644 util/thread/lfstack_ut.cpp diff --git a/client/iam/common/iam.h b/client/iam/common/iam.h index 85bae17c5b1..adba5b52d59 100644 --- a/client/iam/common/iam.h +++ b/client/iam/common/iam.h @@ -93,7 +93,8 @@ class TGrpcIamCredentialsProvider : public ICredentialsProvider { } void UpdateTicket(bool sync = false) { - with_lock(Lock_) { + { + std::lock_guard guard(Lock_); if (NeedStop_ || RequestInflight_) { return; } @@ -129,7 +130,8 @@ class TGrpcIamCredentialsProvider : public ICredentialsProvider { std::string GetTicket() { TInstant nextTicketUpdate; std::string ticket; - with_lock(Lock_) { + { + std::lock_guard guard(Lock_); ticket = Ticket_; nextTicketUpdate = NextTicketUpdate_; if (ticket.empty()) @@ -142,7 +144,8 @@ class TGrpcIamCredentialsProvider : public ICredentialsProvider { } void Stop() { - with_lock(Lock_) { + { + std::lock_guard guard(Lock_); if (NeedStop_) { return; } @@ -156,8 +159,9 @@ class TGrpcIamCredentialsProvider : public ICredentialsProvider { void ProcessIamResponse(NYdbGrpc::TGrpcStatus&& status, TResponse&& result, bool sync) { if (!status.Ok()) { TDuration sleepDuration; - with_lock(Lock_) { - LastRequestError_ = NUtils::TYdbStringBuilder() + { + std::lock_guard guard(Lock_); + LastRequestError_ = TStringBuilder() << "Last request error was at " << TInstant::Now() << ". GrpcStatusCode: " << status.GRpcStatusCode << " Message: \"" << status.Msg @@ -173,19 +177,18 @@ class TGrpcIamCredentialsProvider : public ICredentialsProvider { UpdateTicket(sync); } else { - with_lock(Lock_) { - LastRequestError_ = ""; - Ticket_ = result.iam_token(); - RequestInflight_ = false; - BackoffTimeout_ = BACKOFF_START; - - const auto now = Now(); - NextTicketUpdate_ = std::min( - now + IamEndpoint_.RefreshPeriod, - TInstant::Seconds(result.expires_at().seconds()) - ) - IamEndpoint_.RequestTimeout; - NextTicketUpdate_ = std::max(NextTicketUpdate_, now + TDuration::MilliSeconds(100)); - } + std::lock_guard guard(Lock_); + LastRequestError_ = ""; + Ticket_ = result.iam_token(); + RequestInflight_ = false; + BackoffTimeout_ = BACKOFF_START; + + const auto now = Now(); + NextTicketUpdate_ = std::min( + now + IamEndpoint_.RefreshPeriod, + TInstant::Seconds(result.expires_at().seconds()) + ) - IamEndpoint_.RequestTimeout; + NextTicketUpdate_ = std::max(NextTicketUpdate_, now + TDuration::MilliSeconds(100)); } } diff --git a/client/impl/ydb_internal/db_driver_state/endpoint_pool.cpp b/client/impl/ydb_internal/db_driver_state/endpoint_pool.cpp index 9ebc2bdd079..49946489be2 100644 --- a/client/impl/ydb_internal/db_driver_state/endpoint_pool.cpp +++ b/client/impl/ydb_internal/db_driver_state/endpoint_pool.cpp @@ -75,7 +75,7 @@ std::pair, bool> TEndpointPool::Updat if (addr.empty()) { continue; } - NUtils::TYdbStringBuilder endpointBuilder; + TStringBuilder endpointBuilder; endpointBuilder << "ipv6:"; if (addr[0] != '[') { endpointBuilder << "["; @@ -94,7 +94,7 @@ std::pair, bool> TEndpointPool::Updat continue; } std::string endpointString = - NUtils::TYdbStringBuilder() + TStringBuilder() << "ipv4:" << addr << ":" @@ -104,7 +104,7 @@ std::pair, bool> TEndpointPool::Updat } if (addDefault) { std::string endpointString = - NUtils::TYdbStringBuilder() + TStringBuilder() << endpoint.address() << ":" << endpoint.port(); diff --git a/client/impl/ydb_internal/grpc_connections/grpc_connections.h b/client/impl/ydb_internal/grpc_connections/grpc_connections.h index d4f163336ec..df6a0db755b 100644 --- a/client/impl/ydb_internal/grpc_connections/grpc_connections.h +++ b/client/impl/ydb_internal/grpc_connections/grpc_connections.h @@ -188,7 +188,7 @@ class TGRpcConnectionsImpl nullptr, TPlainStatus( EStatus::CLIENT_UNAUTHENTICATED, - NUtils::TYdbStringBuilder() << "Can't get Authentication info from CredentialsProvider. " << e.what() + TStringBuilder() << "Can't get Authentication info from CredentialsProvider. " << e.what() ) ); return; @@ -421,7 +421,7 @@ class TGRpcConnectionsImpl responseCb( TPlainStatus( EStatus::CLIENT_UNAUTHENTICATED, - NUtils::TYdbStringBuilder() << "Can't get Authentication info from CredentialsProvider. " << e.what() + TStringBuilder() << "Can't get Authentication info from CredentialsProvider. " << e.what() ), nullptr ); @@ -517,7 +517,7 @@ class TGRpcConnectionsImpl connectedCallback( TPlainStatus( EStatus::CLIENT_UNAUTHENTICATED, - NUtils::TYdbStringBuilder() << "Can't get Authentication info from CredentialsProvider. " << e.what() + TStringBuilder() << "Can't get Authentication info from CredentialsProvider. " << e.what() ), nullptr ); diff --git a/client/impl/ydb_internal/kqp_session_common/kqp_session_common.cpp b/client/impl/ydb_internal/kqp_session_common/kqp_session_common.cpp index 39cb1ed9f41..e1ad800deb0 100644 --- a/client/impl/ydb_internal/kqp_session_common/kqp_session_common.cpp +++ b/client/impl/ydb_internal/kqp_session_common/kqp_session_common.cpp @@ -57,22 +57,20 @@ const TEndpointKey& TKqpSessionCommon::GetEndpointKey() const { // Can be called from interceptor, need lock void TKqpSessionCommon::MarkBroken() { - with_lock(Lock_) { - if (State_ == EState::S_ACTIVE) { - NeedUpdateActiveCounter_ = true; - } - State_ = EState::S_BROKEN; + std::lock_guard guard(Lock_); + if (State_ == EState::S_ACTIVE) { + NeedUpdateActiveCounter_ = true; } + State_ = EState::S_BROKEN; } void TKqpSessionCommon::MarkAsClosing() { - with_lock(Lock_) { - if (State_ == EState::S_ACTIVE) { - NeedUpdateActiveCounter_ = true; - } - - State_ = EState::S_CLOSING; + std::lock_guard guard(Lock_); + if (State_ == EState::S_ACTIVE) { + NeedUpdateActiveCounter_ = true; } + + State_ = EState::S_CLOSING; } void TKqpSessionCommon::MarkActive() { @@ -91,9 +89,8 @@ bool TKqpSessionCommon::IsOwnedBySessionPool() const { TKqpSessionCommon::EState TKqpSessionCommon::GetState() const { // See comments in InjectSessionStatusInterception about lock - with_lock(Lock_) { - return State_; - } + std::lock_guard guard(const_cast(Lock_)); + return State_; } void TKqpSessionCommon::SetNeedUpdateActiveCounter(bool flag) { @@ -111,12 +108,11 @@ void TKqpSessionCommon::ScheduleTimeToTouch(TDuration interval, bool updateTimeInPast) { auto now = TInstant::Now(); - with_lock(Lock_) { - if (updateTimeInPast) { - TimeInPast_ = now; - } - TimeToTouch_ = now + interval; + std::lock_guard guard(Lock_); + if (updateTimeInPast) { + TimeInPast_ = now; } + TimeToTouch_ = now + interval; } void TKqpSessionCommon::ScheduleTimeToTouchFast(TDuration interval, diff --git a/client/impl/ydb_internal/logger/CMakeLists.txt b/client/impl/ydb_internal/logger/CMakeLists.txt index e892463338c..dcb40f964ac 100644 --- a/client/impl/ydb_internal/logger/CMakeLists.txt +++ b/client/impl/ydb_internal/logger/CMakeLists.txt @@ -4,7 +4,6 @@ target_link_libraries(impl-ydb_internal-logger PUBLIC contrib-libs-cxxsupp yutil library-cpp-logger - cpp-string_builder ) target_sources(impl-ydb_internal-logger PRIVATE diff --git a/client/impl/ydb_internal/logger/log.cpp b/client/impl/ydb_internal/logger/log.cpp index 6fd102d1282..1b9ae0c7bf9 100644 --- a/client/impl/ydb_internal/logger/log.cpp +++ b/client/impl/ydb_internal/logger/log.cpp @@ -1,7 +1,7 @@ #define INCLUDE_YDB_INTERNAL_H #include "log.h" -#include +#include #include #include @@ -32,7 +32,7 @@ TLogFormatter GetPrefixLogFormatter(const std::string& prefix) { constexpr size_t endlLen = 1; const std::string_view priorityString = LogPriorityToString(priority); - NUtils::TYdbStringBuilder result; + TStringBuilder result; const size_t toReserve = prefix.size() + message.size() + timeLen + endlLen + priorityString.size(); result.reserve(toReserve); diff --git a/client/impl/ydb_internal/plain_status/CMakeLists.txt b/client/impl/ydb_internal/plain_status/CMakeLists.txt index 377f6b01b37..655774ccbbc 100644 --- a/client/impl/ydb_internal/plain_status/CMakeLists.txt +++ b/client/impl/ydb_internal/plain_status/CMakeLists.txt @@ -6,7 +6,6 @@ target_link_libraries(impl-ydb_internal-plain_status PUBLIC protobuf::libprotobuf library-grpc-client yql-public-issue - cpp-string_builder ) target_sources(impl-ydb_internal-plain_status PRIVATE diff --git a/client/impl/ydb_internal/plain_status/status.cpp b/client/impl/ydb_internal/plain_status/status.cpp index 09a99f0aa83..effbd99b832 100644 --- a/client/impl/ydb_internal/plain_status/status.cpp +++ b/client/impl/ydb_internal/plain_status/status.cpp @@ -18,7 +18,7 @@ TPlainStatus::TPlainStatus( if (grpcStatus.InternalError) { Status = EStatus::CLIENT_INTERNAL_ERROR; if (!grpcStatus.Msg.empty()) { - msg = NUtils::TYdbStringBuilder() << "Internal client error: " << grpcStatus.Msg; + msg = TStringBuilder() << "Internal client error: " << grpcStatus.Msg; } else { msg = "Unknown internal client error"; } @@ -49,7 +49,7 @@ TPlainStatus::TPlainStatus( Status = EStatus::CLIENT_INTERNAL_ERROR; break; } - msg = NUtils::TYdbStringBuilder() << "GRpc error: (" << grpcStatus.GRpcStatusCode << "): " << grpcStatus.Msg; + msg = TStringBuilder() << "GRpc error: (" << grpcStatus.GRpcStatusCode << "): " << grpcStatus.Msg; } else { Status = EStatus::SUCCESS; } diff --git a/client/impl/ydb_internal/plain_status/status.h b/client/impl/ydb_internal/plain_status/status.h index 4cd881c9748..aa4c86ecd36 100644 --- a/client/impl/ydb_internal/plain_status/status.h +++ b/client/impl/ydb_internal/plain_status/status.h @@ -64,8 +64,8 @@ struct TPlainStatus { return TRANSPORT_STATUSES_FIRST <= status && status <= TRANSPORT_STATUSES_LAST; } - NUtils::TYdbStringBuilder ToDebugString() const { - NUtils::TYdbStringBuilder ret; + TStringBuilder ToDebugString() const { + TStringBuilder ret; ret << "Status: " << Status; if(!Ok()) ret << ", Description: " << SubstGlobalCopy(Issues.ToString(), '\n', ' '); diff --git a/client/impl/ydb_internal/value_helpers/CMakeLists.txt b/client/impl/ydb_internal/value_helpers/CMakeLists.txt index d158b9f231e..aa7833cd102 100644 --- a/client/impl/ydb_internal/value_helpers/CMakeLists.txt +++ b/client/impl/ydb_internal/value_helpers/CMakeLists.txt @@ -5,7 +5,6 @@ target_link_libraries(impl-ydb_internal-value_helpers PUBLIC yutil api-protos client-ydb_types-fatal_error_handlers - cpp-string_builder ) target_sources(impl-ydb_internal-value_helpers PRIVATE diff --git a/client/impl/ydb_internal/value_helpers/helpers.cpp b/client/impl/ydb_internal/value_helpers/helpers.cpp index bef4fca9161..1a90f0d3ac2 100644 --- a/client/impl/ydb_internal/value_helpers/helpers.cpp +++ b/client/impl/ydb_internal/value_helpers/helpers.cpp @@ -2,7 +2,7 @@ #include "helpers.h" #include -#include +#include #include @@ -92,7 +92,7 @@ bool TypesEqual(const Ydb::Type& t1, const Ydb::Type& t2) { } return true; default: - ThrowFatalError(NUtils::TYdbStringBuilder() << "Unexpected Variant type case " << static_cast(t1.type_case())); + ThrowFatalError(TStringBuilder() << "Unexpected Variant type case " << static_cast(t1.type_case())); return false; } return false; @@ -106,7 +106,7 @@ bool TypesEqual(const Ydb::Type& t1, const Ydb::Type& t2) { case Ydb::Type::kEmptyDictType: return true; default: - ThrowFatalError(NUtils::TYdbStringBuilder() << "Unexpected type case " << static_cast(t1.type_case())); + ThrowFatalError(TStringBuilder() << "Unexpected type case " << static_cast(t1.type_case())); return false; } } diff --git a/client/impl/ydb_stats/CMakeLists.txt b/client/impl/ydb_stats/CMakeLists.txt index 95434db883a..84f876a3d1c 100644 --- a/client/impl/ydb_stats/CMakeLists.txt +++ b/client/impl/ydb_stats/CMakeLists.txt @@ -5,7 +5,6 @@ target_link_libraries(client-impl-ydb_stats PUBLIC yutil library-grpc-client cpp-monlib-metrics - cpp-string_builder ) target_sources(client-impl-ydb_stats PRIVATE diff --git a/client/impl/ydb_stats/stats.h b/client/impl/ydb_stats/stats.h index ec7d4699e1d..8504b7b172f 100644 --- a/client/impl/ydb_stats/stats.h +++ b/client/impl/ydb_stats/stats.h @@ -204,16 +204,16 @@ struct TStatCollector { void IncSyncRetryOperation(const EStatus& status) { if (auto registry = MetricRegistry_.Get()) { - std::string statusName = NUtils::TYdbStringBuilder() << status; - std::string sensor = NUtils::TYdbStringBuilder() << "RetryOperation/" << UnderscoreToUpperCamel(statusName); + std::string statusName = TStringBuilder() << status; + std::string sensor = TStringBuilder() << "RetryOperation/" << UnderscoreToUpperCamel(statusName); registry->Rate({ {"database", Database_}, {"sensor", sensor} })->Inc(); } } void IncAsyncRetryOperation(const EStatus& status) { if (auto registry = MetricRegistry_.Get()) { - std::string statusName = NUtils::TYdbStringBuilder() << status; - std::string sensor = NUtils::TYdbStringBuilder() << "RetryOperation/" << UnderscoreToUpperCamel(statusName); + std::string statusName = TStringBuilder() << status; + std::string sensor = TStringBuilder() << "RetryOperation/" << UnderscoreToUpperCamel(statusName); registry->Rate({ {"database", Database_}, {"sensor", sensor} })->Inc(); } } diff --git a/client/resources/ydb_ca.h b/client/resources/ydb_ca.h index 1e1b4031efb..05add5c68b1 100644 --- a/client/resources/ydb_ca.h +++ b/client/resources/ydb_ca.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace NYdb { diff --git a/client/resources/ydb_resources.h b/client/resources/ydb_resources.h index e3ba98f649b..d8cccf970c0 100644 --- a/client/resources/ydb_resources.h +++ b/client/resources/ydb_resources.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace NYdb { diff --git a/client/ydb_coordination/coordination.cpp b/client/ydb_coordination/coordination.cpp index 2232e492453..ad416152907 100644 --- a/client/ydb_coordination/coordination.cpp +++ b/client/ydb_coordination/coordination.cpp @@ -218,7 +218,8 @@ class TSessionContext : public TThrRefBase { promise.SetValue(TSessionResult(std::move(status))); return; } - with_lock (Lock) { + { + std::lock_guard guard(Lock); LocalContext = context; ConnectionState = EConnectionState::CONNECTING; } @@ -229,18 +230,17 @@ class TSessionContext : public TThrRefBase { } void Stop() { - with_lock (Lock) { - IsStopping = true; - if (!CurrentFailure) { - SetCurrentFailure( - TPlainStatus(EStatus::CLIENT_CANCELLED, "Client is stopped")); - } - if (Processor) { - Processor->Cancel(); - } - if (SleepContext) { - SleepContext->Cancel(); - } + std::lock_guard guard(Lock); + IsStopping = true; + if (!CurrentFailure) { + SetCurrentFailure( + TPlainStatus(EStatus::CLIENT_CANCELLED, "Client is stopped")); + } + if (Processor) { + Processor->Cancel(); + } + if (SleepContext) { + SleepContext->Cancel(); } } @@ -445,165 +445,154 @@ class TSessionContext : public TThrRefBase { private: ESessionState DoGetSessionState() { - with_lock (Lock) { - return SessionState; - } + std::lock_guard guard(Lock); + return SessionState; } EConnectionState DoGetConnectionState() { - with_lock (Lock) { - return ConnectionState; - } + std::lock_guard guard(Lock); + return ConnectionState; } TAsyncResult DoClose(bool isAbort) { - with_lock (Lock) { - if (!ClosedPromise.Initialized()) { - ClosedPromise = NewResultPromise(); - if (IsWriteAllowed()) { - SendCloseRequest(); - } + std::lock_guard guard(Lock); + if (!ClosedPromise.Initialized()) { + ClosedPromise = NewResultPromise(); + if (IsWriteAllowed()) { + SendCloseRequest(); } - if (isAbort) { - // Move to the stopping state, but without cancelling the context - IsStopping = true; - if (!CurrentFailure) { - SetCurrentFailure( - TPlainStatus(EStatus::UNAVAILABLE, "Session is aborted")); - } - if (SleepContext) { - SleepContext->Cancel(); - } + } + if (isAbort) { + // Move to the stopping state, but without cancelling the context + IsStopping = true; + if (!CurrentFailure) { + SetCurrentFailure( + TPlainStatus(EStatus::UNAVAILABLE, "Session is aborted")); + } + if (SleepContext) { + SleepContext->Cancel(); } - return ClosedPromise; } + return ClosedPromise; } TAsyncResult DoPing() { - with_lock (Lock) { - if (IsClosed()) { - return MakeClosedResult(); - } - auto op = MakeHolder(); - auto future = op->Promise.GetFuture(); - if (IsWriteAllowed()) { - DoSendSimpleOp(std::move(op)); - } else { - PendingRequests.emplace_back(std::move(op)); - } - return future; + std::lock_guard guard(Lock); + if (IsClosed()) { + return MakeClosedResult(); + } + auto op = MakeHolder(); + auto future = op->Promise.GetFuture(); + if (IsWriteAllowed()) { + DoSendSimpleOp(std::move(op)); + } else { + PendingRequests.emplace_back(std::move(op)); } + return future; } TAsyncResult DoReconnect() { - with_lock (Lock) { - SessionReconnectDelay = TDuration::Zero(); - if (Processor) { - Processor->Cancel(); - } - if (SleepContext) { - SleepContext->Cancel(); - } - if (IsClosed()) { - return MakeClosedResult(); - } - if (!ReconnectPromise.Initialized()) { - ReconnectPromise = NewResultPromise(); - } - return ReconnectPromise; + std::lock_guard guard(Lock); + SessionReconnectDelay = TDuration::Zero(); + if (Processor) { + Processor->Cancel(); + } + if (SleepContext) { + SleepContext->Cancel(); } + if (IsClosed()) { + return MakeClosedResult(); + } + if (!ReconnectPromise.Initialized()) { + ReconnectPromise = NewResultPromise(); + } + return ReconnectPromise; } TAsyncResult DoAcquireSemaphore( const std::string& name, const TAcquireSemaphoreSettings& settings) { - with_lock (Lock) { - if (IsClosed()) { - return MakeClosedResult(); - } - auto op = MakeIntrusive(settings); - DoSemaphoreEnqueueOp(name, op); - return op->Promise; + std::lock_guard guard(Lock); + if (IsClosed()) { + return MakeClosedResult(); } + auto op = MakeIntrusive(settings); + DoSemaphoreEnqueueOp(name, op); + return op->Promise; } TAsyncResult DoReleaseSemaphore(const std::string& name) { - with_lock (Lock) { - if (IsClosed()) { - return MakeClosedResult(); - } - auto op = MakeIntrusive(); - DoSemaphoreEnqueueOp(name, op); - return op->Promise; + std::lock_guard guard(Lock); + if (IsClosed()) { + return MakeClosedResult(); } + auto op = MakeIntrusive(); + DoSemaphoreEnqueueOp(name, op); + return op->Promise; } TAsyncDescribeSemaphoreResult DoDescribeSemaphore( const std::string& name, const TDescribeSemaphoreSettings& settings) { - with_lock (Lock) { - if (IsClosed()) { - return MakeClosedResult(); - } - auto op = MakeHolder(name, settings); - auto future = op->Promise.GetFuture(); - if (IsWriteAllowed()) { - DoSendSimpleOp(std::move(op)); - } else { - PendingRequests.emplace_back(std::move(op)); - } - return future; + std::lock_guard guard(Lock); + if (IsClosed()) { + return MakeClosedResult(); } + auto op = MakeHolder(name, settings); + auto future = op->Promise.GetFuture(); + if (IsWriteAllowed()) { + DoSendSimpleOp(std::move(op)); + } else { + PendingRequests.emplace_back(std::move(op)); + } + return future; } TAsyncResult DoCreateSemaphore(const std::string& name, ui64 limit, const std::string& data) { - with_lock (Lock) { - if (IsClosed()) { - return MakeClosedResult(); - } - auto op = MakeHolder(name, limit, data); - auto future = op->Promise.GetFuture(); - if (IsWriteAllowed()) { - DoSendSimpleOp(std::move(op)); - } else { - PendingRequests.emplace_back(std::move(op)); - } - return future; + std::lock_guard guard(Lock); + if (IsClosed()) { + return MakeClosedResult(); + } + auto op = MakeHolder(name, limit, data); + auto future = op->Promise.GetFuture(); + if (IsWriteAllowed()) { + DoSendSimpleOp(std::move(op)); + } else { + PendingRequests.emplace_back(std::move(op)); } + return future; } TAsyncResult DoUpdateSemaphore(const std::string& name, const std::string& data) { - with_lock (Lock) { - if (IsClosed()) { - return MakeClosedResult(); - } - auto op = MakeHolder(name, data); - auto future = op->Promise.GetFuture(); - if (IsWriteAllowed()) { - DoSendSimpleOp(std::move(op)); - } else { - PendingRequests.emplace_back(std::move(op)); - } - return future; + std::lock_guard guard(Lock); + if (IsClosed()) { + return MakeClosedResult(); } + auto op = MakeHolder(name, data); + auto future = op->Promise.GetFuture(); + if (IsWriteAllowed()) { + DoSendSimpleOp(std::move(op)); + } else { + PendingRequests.emplace_back(std::move(op)); + } + return future; } TAsyncResult DoDeleteSemaphore(const std::string& name, bool force) { - with_lock (Lock) { - if (IsClosed()) { - return MakeClosedResult(); - } - auto op = MakeHolder(name, force); - auto future = op->Promise.GetFuture(); - if (IsWriteAllowed()) { - DoSendSimpleOp(std::move(op)); - } else { - PendingRequests.emplace_back(std::move(op)); - } - return future; + std::lock_guard guard(Lock); + if (IsClosed()) { + return MakeClosedResult(); } + auto op = MakeHolder(name, force); + auto future = op->Promise.GetFuture(); + if (IsWriteAllowed()) { + DoSendSimpleOp(std::move(op)); + } else { + PendingRequests.emplace_back(std::move(op)); + } + return future; } TDuration GetConnectTimeout() { @@ -627,7 +616,8 @@ class TSessionContext : public TThrRefBase { IQueueClientContextPtr prevConnectContext; IQueueClientContextPtr prevConnectTimeoutContext; - with_lock (Lock) { + { + std::lock_guard guard(Lock); prevConnectContext = std::exchange(ConnectContext, connectContext); prevConnectTimeoutContext = std::exchange(ConnectTimeoutContext, connectTimeoutContext); } @@ -889,7 +879,8 @@ class TSessionContext : public TThrRefBase { TDeque> failedSimpleOps; TResultPromise closePromise; - with_lock (Lock) { + { + std::lock_guard guard(Lock); Processor.Reset(); Y_ABORT_UNLESS(LocalContext, "Processing event without a valid context"); @@ -1051,7 +1042,8 @@ class TSessionContext : public TThrRefBase { TDuration sleepDuration; IQueueClientContextPtr sleepContext; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (!IsStopping) { // Session object still active, calculate sleep duration sleepDuration = Min(SessionReconnectDelay, *SessionExpireDeadline - TInstant::Now()); @@ -1086,31 +1078,31 @@ class TSessionContext : public TThrRefBase { void OnConnectTimeout(IQueueClientContextPtr context) { Y_ABORT_UNLESS(context, "Connection timeout context is unexpectedly empty"); - with_lock (Lock) { - if (ConnectTimeoutContext != context) { - // Context changed or dropped, ignore - return; - } + std::lock_guard guard(Lock); + if (ConnectTimeoutContext != context) { + // Context changed or dropped, ignore + return; + } - ConnectTimeoutContext.reset(); + ConnectTimeoutContext.reset(); - // Timer succeeded and still current, cancel connection attempt - Y_ABORT_UNLESS(ConnectContext, "Connection context is unexpectedly empty"); - ConnectContext->Cancel(); - ConnectContext.reset(); + // Timer succeeded and still current, cancel connection attempt + Y_ABORT_UNLESS(ConnectContext, "Connection context is unexpectedly empty"); + ConnectContext->Cancel(); + ConnectContext.reset(); - SessionTimeout = true; - if (!CurrentFailure) { - SetCurrentFailure( - TPlainStatus(EStatus::TIMEOUT, "Connection request timed out")); - } + SessionTimeout = true; + if (!CurrentFailure) { + SetCurrentFailure( + TPlainStatus(EStatus::TIMEOUT, "Connection request timed out")); } } void OnConnect(TPlainStatus plain, IProcessor::TPtr processor) { bool hadTimeout = false; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (SessionTimeout) { // OnConnectTimeout was called first hadTimeout = true; @@ -1143,7 +1135,8 @@ class TSessionContext : public TThrRefBase { IQueueClientContextPtr timeoutContext; TResultPromise reconnectPromise; - with_lock (Lock) { + { + std::lock_guard guard(Lock); Processor = processor; ConnectionState = EConnectionState::ATTACHING; if (ReconnectPromise.Initialized()) { @@ -1228,7 +1221,8 @@ class TSessionContext : public TThrRefBase { IQueueClientContextPtr sessionSelfPingContext; bool detached = false; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (SessionState == ESessionState::ATTACHED) { SessionState = ESessionState::DETACHED; detached = true; @@ -1276,7 +1270,8 @@ class TSessionContext : public TThrRefBase { } IProcessor::TPtr processor; - with_lock (Lock) { + { + std::lock_guard guard(Lock); processor = Processor; } Y_ABORT_UNLESS(processor, "Processor is missing for some reason"); @@ -1313,7 +1308,8 @@ class TSessionContext : public TThrRefBase { bool stopping; std::shared_ptr context; - with_lock (Lock) { + { + std::lock_guard guard(Lock); sleepContext.swap(SleepContext); Y_ABORT_UNLESS(sleepContext, "Unexpected missing SleepContext in OnSleepFinished"); if (!(stopping = IsStopping)) { @@ -1341,7 +1337,8 @@ class TSessionContext : public TThrRefBase { void OnSelfPingTimer(IQueueClientContextPtr context) { TInstant nextTimerTimestamp; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (SessionSelfPingContext != context) { // Context changed or dropped, ignore return; @@ -1414,7 +1411,8 @@ class TSessionContext : public TThrRefBase { const auto& source = Response->pong(); const ui64 reqId = source.opaque(); TResultPromise replyPromise; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (auto* op = FindSentRequest(reqId)) { UpdateLastKnownGoodTimestampLocked(op->SendTimestamp); replyPromise.Swap(op->Promise); @@ -1431,7 +1429,8 @@ class TSessionContext : public TThrRefBase { auto plain = MakePlainStatus(source.status(), source.issues()); bool expired = false; bool detached = false; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (plain.Status == EStatus::SESSION_EXPIRED || plain.Status == EStatus::UNAUTHORIZED || plain.Status == EStatus::NOT_FOUND) @@ -1465,7 +1464,8 @@ class TSessionContext : public TThrRefBase { IQueueClientContextPtr sessionStartTimeoutContext; IQueueClientContextPtr sessionSelfPingContext; TInstant selfPingFirstTimestamp; - with_lock (Lock) { + { + std::lock_guard guard(Lock); // Remember session id for future reconnects SessionId = source.session_id(); if (SessionPromise.Initialized()) { @@ -1511,7 +1511,8 @@ class TSessionContext : public TThrRefBase { } case TResponse::kSessionStopped: { bool expired = false; - with_lock (Lock) { + { + std::lock_guard guard(Lock); IsStopping = true; if (!ClosedPromise.Initialized()) { ClosedPromise = NewResultPromise(); @@ -1535,7 +1536,8 @@ class TSessionContext : public TThrRefBase { ui64 reqId = source.req_id(); TResultPromise supersededPromise; std::function acceptedCallback; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (auto* state = SemaphoreByReqId.Value(reqId, nullptr)) { auto op = state->LastSentOp; Y_ABORT_UNLESS(op && op->ReqId == reqId && op->OpType == SEM_OP_ACQUIRE, @@ -1571,7 +1573,8 @@ class TSessionContext : public TThrRefBase { auto plain = MakePlainStatus(source.status(), source.issues()); TResultPromise supersededPromise; TResultPromise resultPromise; - with_lock (Lock) { + { + std::lock_guard guard(Lock); DoSemaphoreProcessResult(reqId, SEM_OP_ACQUIRE, plain.Status, &supersededPromise, &resultPromise); } if (supersededPromise.Initialized()) { @@ -1589,7 +1592,8 @@ class TSessionContext : public TThrRefBase { auto plain = MakePlainStatus(source.status(), source.issues()); TResultPromise supersededPromise; TResultPromise resultPromise; - with_lock (Lock) { + { + std::lock_guard guard(Lock); DoSemaphoreProcessResult(reqId, SEM_OP_RELEASE, plain.Status, &supersededPromise, &resultPromise); } if (supersededPromise.Initialized()) { @@ -1606,7 +1610,8 @@ class TSessionContext : public TThrRefBase { const ui64 reqId = source.req_id(); auto plain = MakePlainStatus(source.status(), source.issues()); TPromise resultPromise; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (auto* op = FindSentRequest(reqId)) { UpdateLastKnownGoodTimestampLocked(op->SendTimestamp); resultPromise.Swap(op->Promise); @@ -1628,7 +1633,8 @@ class TSessionContext : public TThrRefBase { const ui64 reqId = source.req_id(); std::function callback; bool triggered = false; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (auto* op = FindSentRequest(reqId)) { UpdateLastKnownGoodTimestampLocked(op->SendTimestamp); callback.swap(op->Settings.OnChanged_); @@ -1647,7 +1653,8 @@ class TSessionContext : public TThrRefBase { const ui64 reqId = source.req_id(); auto plain = MakePlainStatus(source.status(), source.issues()); TResultPromise resultPromise; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (auto* op = FindSentRequest(reqId)) { UpdateLastKnownGoodTimestampLocked(op->SendTimestamp); resultPromise.Swap(op->Promise); @@ -1664,7 +1671,8 @@ class TSessionContext : public TThrRefBase { const ui64 reqId = source.req_id(); auto plain = MakePlainStatus(source.status(), source.issues()); TResultPromise resultPromise; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (auto* op = FindSentRequest(reqId)) { UpdateLastKnownGoodTimestampLocked(op->SendTimestamp); resultPromise.Swap(op->Promise); @@ -1681,7 +1689,8 @@ class TSessionContext : public TThrRefBase { const ui64 reqId = source.req_id(); auto plain = MakePlainStatus(source.status(), source.issues()); TResultPromise resultPromise; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (auto* op = FindSentRequest(reqId)) { UpdateLastKnownGoodTimestampLocked(op->SendTimestamp); resultPromise.Swap(op->Promise); @@ -1707,25 +1716,24 @@ class TSessionContext : public TThrRefBase { void OnSessionStartTimeout(IQueueClientContextPtr context) { Y_ABORT_UNLESS(context, "Unexpected empty timeout context"); - with_lock (Lock) { - if (SessionStartTimeoutContext != context) { - // Context changed or dropped, ignore - return; - } + std::lock_guard guard(Lock); + if (SessionStartTimeoutContext != context) { + // Context changed or dropped, ignore + return; + } - SessionStartTimeoutContext.reset(); + SessionStartTimeoutContext.reset(); - // Mark session as timed out - SessionTimeout = true; - if (!CurrentFailure) { - SetCurrentFailure( - TPlainStatus(EStatus::TIMEOUT, "Session start request timed out")); - } + // Mark session as timed out + SessionTimeout = true; + if (!CurrentFailure) { + SetCurrentFailure( + TPlainStatus(EStatus::TIMEOUT, "Session start request timed out")); + } - // We cannot report timeout until OnRead finishes with an error - if (Processor) { - Processor->Cancel(); - } + // We cannot report timeout until OnRead finishes with an error + if (Processor) { + Processor->Cancel(); } } diff --git a/client/ydb_federated_topic/federated_topic.h b/client/ydb_federated_topic/federated_topic.h index 350cbcff474..1e04ca1b7b7 100644 --- a/client/ydb_federated_topic/federated_topic.h +++ b/client/ydb_federated_topic/federated_topic.h @@ -466,22 +466,22 @@ namespace NYdb::NTopic { using namespace NFederatedTopic; template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& res, bool) const; +void TPrintable::DebugString(TStringBuilder& res, bool) const; template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& res, bool) const; +void TPrintable::DebugString(TStringBuilder& res, bool) const; template<> -void TPrintable>::DebugString(NUtils::TYdbStringBuilder& res, bool) const; +void TPrintable>::DebugString(TStringBuilder& res, bool) const; template<> -void TPrintable>::DebugString(NUtils::TYdbStringBuilder& res, bool) const; +void TPrintable>::DebugString(TStringBuilder& res, bool) const; template<> -void TPrintable>::DebugString(NUtils::TYdbStringBuilder& res, bool) const; +void TPrintable>::DebugString(TStringBuilder& res, bool) const; template<> -void TPrintable>::DebugString(NUtils::TYdbStringBuilder& res, bool) const; +void TPrintable>::DebugString(TStringBuilder& res, bool) const; template<> -void TPrintable>::DebugString(NUtils::TYdbStringBuilder& res, bool) const; +void TPrintable>::DebugString(TStringBuilder& res, bool) const; template<> -void TPrintable>::DebugString(NUtils::TYdbStringBuilder& res, bool) const; +void TPrintable>::DebugString(TStringBuilder& res, bool) const; template<> -void TPrintable>::DebugString(NUtils::TYdbStringBuilder& res, bool) const; +void TPrintable>::DebugString(TStringBuilder& res, bool) const; } diff --git a/client/ydb_federated_topic/impl/federated_read_session.cpp b/client/ydb_federated_topic/impl/federated_read_session.cpp index ca7d5600173..65588a57d07 100644 --- a/client/ydb_federated_topic/impl/federated_read_session.cpp +++ b/client/ydb_federated_topic/impl/federated_read_session.cpp @@ -72,13 +72,12 @@ void TFederatedReadSessionImpl::Start() { AsyncInit.Subscribe([selfCtx = SelfContext](const auto& f){ Y_UNUSED(f); if (auto self = selfCtx->LockShared()) { - with_lock(self->Lock) { - if (self->Closing) { - return; - } - self->FederationState = self->Observer->GetState(); - self->OnFederatedStateUpdateImpl(); + std::lock_guard guard(self->Lock); + if (self->Closing) { + return; } + self->FederationState = self->Observer->GetState(); + self->OnFederatedStateUpdateImpl(); } }); } @@ -114,14 +113,13 @@ NThreading::TFuture TFederatedReadSessionImpl::WaitEvent() { return AsyncInit.Apply([selfCtx = SelfContext](const NThreading::TFuture) { if (auto self = selfCtx->LockShared()) { std::vector> waiters; - with_lock(self->Lock) { - if (self->Closing) { - return NThreading::MakeFuture(); - } - Y_ABORT_UNLESS(!self->SubSessions.empty(), "SubSessions empty in discovered state"); - for (const auto& sub : self->SubSessions) { - waiters.emplace_back(sub.Session->WaitEvent()); - } + std::lock_guard guard(self->Lock); + if (self->Closing) { + return NThreading::MakeFuture(); + } + Y_ABORT_UNLESS(!self->SubSessions.empty(), "SubSessions empty in discovered state"); + for (const auto& sub : self->SubSessions) { + waiters.emplace_back(sub.Session->WaitEvent()); } return NThreading::WaitAny(std::move(waiters)); } @@ -133,7 +131,8 @@ std::vector TFederatedReadSessionImpl::GetEvents(bool if (block) { WaitEvent().Wait(); } - with_lock(Lock) { + { + std::lock_guard guard(Lock); if (Closing) { // TODO correct conversion return {NTopic::TSessionClosedEvent(FederationState->Status.GetStatus(), {})}; @@ -147,7 +146,8 @@ std::vector TFederatedReadSessionImpl::GetEvents(bool } } std::vector result; - with_lock(Lock) { + { + std::lock_guard guard(Lock); do { auto sub = SubSessions[SubsessionIndex]; for (auto&& ev : sub.Session->GetEvents(false, maxEventsCount, maxByteSize)) { @@ -165,16 +165,15 @@ void TFederatedReadSessionImpl::CloseImpl() { } bool TFederatedReadSessionImpl::Close(TDuration timeout) { - with_lock(Lock) { - Closing = true; + std::lock_guard guard(Lock); + Closing = true; - bool result = true; - for (const auto& sub : SubSessions) { - // TODO substract from user timeout - result = sub.Session->Close(timeout); - } - return result; + bool result = true; + for (const auto& sub : SubSessions) { + // TODO substract from user timeout + result = sub.Session->Close(timeout); } + return result; } } // namespace NYdb::NFederatedTopic diff --git a/client/ydb_federated_topic/impl/federated_read_session.h b/client/ydb_federated_topic/impl/federated_read_session.h index e087621baba..35830597a77 100644 --- a/client/ydb_federated_topic/impl/federated_read_session.h +++ b/client/ydb_federated_topic/impl/federated_read_session.h @@ -47,7 +47,7 @@ class TFederatedReadSessionImpl : public NPersQueue::TEnableSelfContext -void TPrintable::DebugString(NUtils::TYdbStringBuilder& res, bool) const { +void TPrintable::DebugString(TStringBuilder& res, bool) const { const auto* self = static_cast(this); res << " Partition session id: " << self->GetPartitionSessionId() << " Topic: \"" << self->GetTopicPath() << "\"" @@ -66,7 +66,7 @@ void TPrintable::DebugString(NUtils::TYdbStringBuild } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const { +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const { const auto* self = static_cast(this); ret << "Message {"; auto ptr = dynamic_cast(self); @@ -77,7 +77,7 @@ void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool prin } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const { +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const { const auto* self = static_cast(this); ret << "CompressedMessage {"; static_cast(self)->DebugString(ret, printData); @@ -88,7 +88,7 @@ void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool) const { +void TPrintable::DebugString(TStringBuilder& ret, bool) const { const auto* self = static_cast(this); ret << "CommitAcknowledgement {"; self->GetFederatedPartitionSession()->DebugString(ret); @@ -97,7 +97,7 @@ void TPrintable::DebugString(NUtils::TYdbStri } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool) const { +void TPrintable::DebugString(TStringBuilder& ret, bool) const { const auto* self = static_cast(this); ret << "StartPartitionSession {"; self->GetFederatedPartitionSession()->DebugString(ret); @@ -107,7 +107,7 @@ void TPrintable::DebugString(NUtils::TYdbStringBuil } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool) const { +void TPrintable::DebugString(TStringBuilder& ret, bool) const { const auto* self = static_cast(this); ret << "StopPartitionSession {"; self->GetFederatedPartitionSession()->DebugString(ret); @@ -116,7 +116,7 @@ void TPrintable::DebugString(NUtils::TYdbStringBuild } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool) const { +void TPrintable::DebugString(TStringBuilder& ret, bool) const { const auto* self = static_cast(this); ret << "PartitionSessionStatus {"; self->GetFederatedPartitionSession()->DebugString(ret); @@ -128,7 +128,7 @@ void TPrintable::DebugString(NUtils::TYdbStringBui } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool) const { +void TPrintable::DebugString(TStringBuilder& ret, bool) const { const auto* self = static_cast(this); ret << "PartitionSessionClosed {"; self->GetFederatedPartitionSession()->DebugString(ret); @@ -137,7 +137,7 @@ void TPrintable::DebugString(NUtils::TYdbStringBui } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const { +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const { const auto* self = static_cast(this); ret << "DataReceived {"; self->GetFederatedPartitionSession()->DebugString(ret); diff --git a/client/ydb_federated_topic/impl/federated_topic_impl.cpp b/client/ydb_federated_topic/impl/federated_topic_impl.cpp index 593ba3bf759..3bc88b9dc04 100644 --- a/client/ydb_federated_topic/impl/federated_topic_impl.cpp +++ b/client/ydb_federated_topic/impl/federated_topic_impl.cpp @@ -40,11 +40,10 @@ TFederatedTopicClient::TImpl::CreateWriteSession(const TFederatedWriteSessionSet } void TFederatedTopicClient::TImpl::InitObserver() { - with_lock(Lock) { - if (!Observer || Observer->IsStale()) { - Observer = std::make_shared(Connections, ClientSettings); - Observer->Start(); - } + std::lock_guard guard(Lock); + if (!Observer || Observer->IsStale()) { + Observer = std::make_shared(Connections, ClientSettings); + Observer->Start(); } } diff --git a/client/ydb_federated_topic/impl/federated_topic_impl.h b/client/ydb_federated_topic/impl/federated_topic_impl.h index ff8bf4cd1a4..939d585af12 100644 --- a/client/ydb_federated_topic/impl/federated_topic_impl.h +++ b/client/ydb_federated_topic/impl/federated_topic_impl.h @@ -25,10 +25,9 @@ class TFederatedTopicClient::TImpl { } ~TImpl() { - with_lock(Lock) { - if (Observer) { - Observer->Stop(); - } + std::lock_guard guard(Lock); + if (Observer) { + Observer->Stop(); } } @@ -39,9 +38,8 @@ class TFederatedTopicClient::TImpl { std::shared_ptr CreateWriteSession(const TFederatedWriteSessionSettings& settings); std::shared_ptr GetObserver() { - with_lock(Lock) { - return Observer; - } + std::lock_guard guard(Lock); + return Observer; } void InitObserver(); diff --git a/client/ydb_federated_topic/impl/federated_write_session.cpp b/client/ydb_federated_topic/impl/federated_write_session.cpp index 62702dfc43a..f92209f5bb5 100644 --- a/client/ydb_federated_topic/impl/federated_write_session.cpp +++ b/client/ydb_federated_topic/impl/federated_write_session.cpp @@ -37,17 +37,17 @@ TFederatedWriteSession::TFederatedWriteSession(const TFederatedWriteSessionSetti void TFederatedWriteSession::Start() { // TODO validate settings? - with_lock(Lock) { + { + std::lock_guard guard(Lock); ClientEventsQueue->PushEvent(NTopic::TWriteSessionEvent::TReadyToAcceptEvent{IssueContinuationToken()}); ClientHasToken = true; } AsyncInit.Subscribe([self = shared_from_this()](const auto& f){ Y_UNUSED(f); - with_lock(self->Lock) { - self->FederationState = self->Observer->GetState(); - self->OnFederatedStateUpdateImpl(); - } + std::lock_guard guard(self->Lock); + self->FederationState = self->Observer->GetState(); + self->OnFederatedStateUpdateImpl(); }); } @@ -66,7 +66,8 @@ void TFederatedWriteSession::OpenSubSessionImpl(std::shared_ptr db) { .HandlersExecutor(Settings.EventHandlers_.HandlersExecutor_) .ReadyToAcceptHander([self = shared_from_this()](NTopic::TWriteSessionEvent::TReadyToAcceptEvent& ev){ TDeferredWrite deferred(self->Subsession); - with_lock(self->Lock) { + { + std::lock_guard guard(self->Lock); Y_ABORT_UNLESS(!self->PendingToken.has_value()); self->PendingToken = std::move(ev.ContinuationToken); self->PrepareDeferredWrite(deferred); @@ -74,23 +75,21 @@ void TFederatedWriteSession::OpenSubSessionImpl(std::shared_ptr db) { deferred.DoWrite(); }) .AcksHandler([self = shared_from_this()](NTopic::TWriteSessionEvent::TAcksEvent& ev){ - with_lock(self->Lock) { - Y_ABORT_UNLESS(ev.Acks.size() <= self->OriginalMessagesToGetAck.size()); - for (size_t i = 0; i < ev.Acks.size(); ++i) { - self->BufferFreeSpace += self->OriginalMessagesToGetAck.front().Data.size(); - self->OriginalMessagesToGetAck.pop_front(); - } - self->ClientEventsQueue->PushEvent(std::move(ev)); - if (self->BufferFreeSpace > 0 && !self->ClientHasToken) { - self->ClientEventsQueue->PushEvent(NTopic::TWriteSessionEvent::TReadyToAcceptEvent{IssueContinuationToken()}); - self->ClientHasToken = true; - } + std::lock_guard guard(self->Lock); + Y_ABORT_UNLESS(ev.Acks.size() <= self->OriginalMessagesToGetAck.size()); + for (size_t i = 0; i < ev.Acks.size(); ++i) { + self->BufferFreeSpace += self->OriginalMessagesToGetAck.front().Data.size(); + self->OriginalMessagesToGetAck.pop_front(); + } + self->ClientEventsQueue->PushEvent(std::move(ev)); + if (self->BufferFreeSpace > 0 && !self->ClientHasToken) { + self->ClientEventsQueue->PushEvent(NTopic::TWriteSessionEvent::TReadyToAcceptEvent{IssueContinuationToken()}); + self->ClientHasToken = true; } }) .SessionClosedHandler([self = shared_from_this()](const NTopic::TSessionClosedEvent & ev){ - with_lock(self->Lock) { - self->ClientEventsQueue->PushEvent(ev); - } + std::lock_guard guard(self->Lock); + self->ClientEventsQueue->PushEvent(ev); }); NTopic::TWriteSessionSettings wsSettings = Settings; @@ -173,13 +172,12 @@ void TFederatedWriteSession::ScheduleFederatedStateUpdateImpl(TDuration delay) { Y_ABORT_UNLESS(Lock.IsLocked()); auto cb = [self = shared_from_this()](bool ok) { if (ok) { - with_lock(self->Lock) { - if (self->Closing) { - return; - } - self->FederationState = self->Observer->GetState(); - self->OnFederatedStateUpdateImpl(); + std::lock_guard guard(self->Lock); + if (self->Closing) { + return; } + self->FederationState = self->Observer->GetState(); + self->OnFederatedStateUpdateImpl(); } }; @@ -247,7 +245,8 @@ void TFederatedWriteSession::WriteInternal(NTopic::TContinuationToken&&, NTopic: { TDeferredWrite deferred(Subsession); - with_lock(Lock) { + { + std::lock_guard guard(Lock); BufferFreeSpace -= message.Data.size(); OriginalMessagesToPassDown.emplace_back(std::move(message)); diff --git a/client/ydb_federated_topic/impl/federation_observer.cpp b/client/ydb_federated_topic/impl/federation_observer.cpp index f21ccd00c91..6e42b679004 100644 --- a/client/ydb_federated_topic/impl/federation_observer.cpp +++ b/client/ydb_federated_topic/impl/federation_observer.cpp @@ -22,9 +22,8 @@ TFederatedDbObserverImpl::~TFederatedDbObserverImpl() { } std::shared_ptr TFederatedDbObserverImpl::GetState() { - with_lock(Lock) { - return FederatedDbState; - } + std::lock_guard guard(Lock); + return FederatedDbState; } NThreading::TFuture TFederatedDbObserverImpl::WaitForFirstState() { @@ -32,17 +31,17 @@ NThreading::TFuture TFederatedDbObserverImpl::WaitForFirstState() { } void TFederatedDbObserverImpl::Start() { - with_lock(Lock) { - if (Stopping) { - return; - } - ScheduleFederationDiscoveryImpl(TDuration::Zero()); + std::lock_guard guard(Lock); + if (Stopping) { + return; } + ScheduleFederationDiscoveryImpl(TDuration::Zero()); } void TFederatedDbObserverImpl::Stop() { NYdbGrpc::IQueueClientContextPtr ctx; - with_lock(Lock) { + { + std::lock_guard guard(Lock); Stopping = true; ctx = std::exchange(FederationDiscoveryDelayContext, nullptr); } @@ -53,9 +52,8 @@ void TFederatedDbObserverImpl::Stop() { // If observer is stale it will never update state again because of client retry policy bool TFederatedDbObserverImpl::IsStale() const { - with_lock(Lock) { - return PromiseToInitState.HasValue() && !FederatedDbState->Status.IsSuccess(); - } + std::lock_guard guard(const_cast(Lock)); + return PromiseToInitState.HasValue() && !FederatedDbState->Status.IsSuccess(); } Ydb::FederationDiscovery::ListFederationDatabasesRequest TFederatedDbObserverImpl::ComposeRequest() const { @@ -100,12 +98,11 @@ void TFederatedDbObserverImpl::ScheduleFederationDiscoveryImpl(TDuration delay) auto cb = [selfCtx = SelfContext](bool ok) { if (ok) { if (auto self = selfCtx->LockShared()) { - with_lock(self->Lock) { - if (self->Stopping) { - return; - } - self->RunFederationDiscoveryImpl(); + std::lock_guard guard(self->Lock); + if (self->Stopping) { + return; } + self->RunFederationDiscoveryImpl(); } } }; @@ -123,7 +120,8 @@ void TFederatedDbObserverImpl::ScheduleFederationDiscoveryImpl(TDuration delay) } void TFederatedDbObserverImpl::OnFederationDiscovery(TStatus&& status, Ydb::FederationDiscovery::ListFederationDatabasesResult&& result) { - with_lock(Lock) { + { + std::lock_guard guard(Lock); if (Stopping) { // TODO log something return; diff --git a/client/ydb_federated_topic/ut/fds_mock.h b/client/ydb_federated_topic/ut/fds_mock.h index 1f39ab0c441..1ed05a80f11 100644 --- a/client/ydb_federated_topic/ut/fds_mock.h +++ b/client/ydb_federated_topic/ut/fds_mock.h @@ -37,11 +37,10 @@ class TFederationDiscoveryServiceMock: public Ydb::FederationDiscovery::V1::Fede std::optional GetNextPendingRequest() { std::optional result; - with_lock(Lock) { - if (!PendingRequests.empty()) { - result = PendingRequests.front(); - PendingRequests.pop_front(); - } + std::lock_guard guard(Lock); + if (!PendingRequests.empty()) { + result = PendingRequests.front(); + PendingRequests.pop_front(); } return result; } @@ -54,7 +53,8 @@ class TFederationDiscoveryServiceMock: public Ydb::FederationDiscovery::V1::Fede auto p = NThreading::NewPromise(); auto f = p.GetFuture(); - with_lock(Lock) { + { + std::lock_guard guard(Lock); PendingRequests.push_back({request, std::move(p)}); } diff --git a/client/ydb_operation/CMakeLists.txt b/client/ydb_operation/CMakeLists.txt index 5257c4b2abc..5f00c609ad3 100644 --- a/client/ydb_operation/CMakeLists.txt +++ b/client/ydb_operation/CMakeLists.txt @@ -11,7 +11,6 @@ target_link_libraries(cpp-client-ydb_operation PUBLIC cpp-client-ydb_export cpp-client-ydb_import client-ydb_types-operation - cpp-string_builder ) target_sources(cpp-client-ydb_operation PRIVATE diff --git a/client/ydb_operation/operation.h b/client/ydb_operation/operation.h index dc76a272779..bc73e5e8189 100644 --- a/client/ydb_operation/operation.h +++ b/client/ydb_operation/operation.h @@ -3,7 +3,7 @@ #include #include -#include +#include namespace NYdb { namespace NOperation { @@ -27,7 +27,7 @@ class TOperationsList : public TStatus { const std::string& NextPageToken() const { return NextPageToken_; } std::string ToJsonString() const { - NUtils::TYdbStringBuilder json; + TStringBuilder json; json << "{\"operations\":["; bool first = true; diff --git a/client/ydb_params/CMakeLists.txt b/client/ydb_params/CMakeLists.txt index f437c9600cf..c64290cc231 100644 --- a/client/ydb_params/CMakeLists.txt +++ b/client/ydb_params/CMakeLists.txt @@ -8,7 +8,6 @@ target_link_libraries(cpp-client-ydb_params PUBLIC api-protos client-ydb_types-fatal_error_handlers cpp-client-ydb_value - cpp-string_builder ) target_sources(cpp-client-ydb_params PRIVATE diff --git a/client/ydb_params/params.cpp b/client/ydb_params/params.cpp index 26b8a7522aa..8a1f57e07d0 100644 --- a/client/ydb_params/params.cpp +++ b/client/ydb_params/params.cpp @@ -5,7 +5,7 @@ #include -#include +#include namespace NYdb { @@ -76,7 +76,7 @@ class TParamsBuilder::TImpl { if (HasTypeInfo()) { if (!TypesEqual(param->type(), value.GetType().GetProto())) { - FatalError(NUtils::TYdbStringBuilder() << "Type mismatch for parameter: " << name << ", expected: " + FatalError(TStringBuilder() << "Type mismatch for parameter: " << name << ", expected: " << FormatType(TType(param->type())) << ", actual: " << FormatType(value.GetType())); } } else { @@ -89,7 +89,7 @@ class TParamsBuilder::TImpl { TParams Build() { for (auto& pair : ValueBuildersMap_) { if (!pair.second.Finished()) { - FatalError(NUtils::TYdbStringBuilder() << "Incomplete value for parameter: " << pair.first + FatalError(TStringBuilder() << "Incomplete value for parameter: " << pair.first << ", call Build() on parameter value builder"); } } @@ -106,7 +106,7 @@ class TParamsBuilder::TImpl { if (HasTypeInfo()) { auto it = ParamsMap_.find(name); if (it == ParamsMap_.end()) { - FatalError(NUtils::TYdbStringBuilder() << "Parameter not found: " << name); + FatalError(TStringBuilder() << "Parameter not found: " << name); return nullptr; } @@ -117,7 +117,7 @@ class TParamsBuilder::TImpl { } void FatalError(const std::string& msg) const { - ThrowFatalError(NUtils::TYdbStringBuilder() << "TParamsBuilder: " << msg); + ThrowFatalError(TStringBuilder() << "TParamsBuilder: " << msg); } private: diff --git a/client/ydb_persqueue_core/CMakeLists.txt b/client/ydb_persqueue_core/CMakeLists.txt index 85af3053ff8..db6a3d27a18 100644 --- a/client/ydb_persqueue_core/CMakeLists.txt +++ b/client/ydb_persqueue_core/CMakeLists.txt @@ -21,7 +21,6 @@ target_link_libraries(cpp-client-ydb_persqueue_core PUBLIC api-grpc api-grpc-draft api-protos - cpp-string_builder ) generate_enum_serilization(cpp-client-ydb_persqueue_core diff --git a/client/ydb_persqueue_core/impl/CMakeLists.txt b/client/ydb_persqueue_core/impl/CMakeLists.txt index 91a2b9542e1..e7c41fe2691 100644 --- a/client/ydb_persqueue_core/impl/CMakeLists.txt +++ b/client/ydb_persqueue_core/impl/CMakeLists.txt @@ -13,7 +13,6 @@ target_link_libraries(client-ydb_persqueue_core-impl PUBLIC impl-ydb_internal-make_request client-ydb_common_client-impl cpp-client-ydb_driver - cpp-string_builder cpp-string_utils-misc ) diff --git a/client/ydb_persqueue_core/impl/common.cpp b/client/ydb_persqueue_core/impl/common.cpp index eb9994909fe..da5e40fc6f9 100644 --- a/client/ydb_persqueue_core/impl/common.cpp +++ b/client/ydb_persqueue_core/impl/common.cpp @@ -100,9 +100,9 @@ std::string ApplyClusterEndpoint(std::string_view driverEndpoint, const std::str const bool hasColon = clusterDiscoveryEndpoint.find(':') != std::string::npos; if (hasColon) { - return NUtils::TYdbStringBuilder() << '[' << clusterDiscoveryEndpoint << "]:" << driverPort; + return TStringBuilder() << '[' << clusterDiscoveryEndpoint << "]:" << driverPort; } else { - return NUtils::TYdbStringBuilder() << clusterDiscoveryEndpoint << ':' << driverPort; + return TStringBuilder() << clusterDiscoveryEndpoint << ':' << driverPort; } } diff --git a/client/ydb_persqueue_core/impl/counters_logger.h b/client/ydb_persqueue_core/impl/counters_logger.h index 6c91c2f0c08..1ea34be46d0 100644 --- a/client/ydb_persqueue_core/impl/counters_logger.h +++ b/client/ydb_persqueue_core/impl/counters_logger.h @@ -52,7 +52,8 @@ class TCountersLogger : public std::enable_shared_from_thisCreateContext(); - if (DumpCountersContext) { - auto callback = [ctx = SelfContext, timeNumber](bool ok) { - if (ok) { - if (auto borrowedSelf = ctx->LockShared()) { - borrowedSelf->DumpCountersToLog(timeNumber); - } + DumpCountersContext = Connections->CreateContext(); + if (DumpCountersContext) { + auto callback = [ctx = SelfContext, timeNumber](bool ok) { + if (ok) { + if (auto borrowedSelf = ctx->LockShared()) { + borrowedSelf->DumpCountersToLog(timeNumber); } - }; - Connections->ScheduleCallback(UPDATE_PERIOD, - std::move(callback), - DumpCountersContext); - } + } + }; + Connections->ScheduleCallback(UPDATE_PERIOD, + std::move(callback), + DumpCountersContext); } } @@ -115,7 +115,7 @@ class TCountersLogger : public std::enable_shared_from_this TPersQueueClient::TImpl::CreateReadSession(const T std::optional maybeSettings; if (!settings.DecompressionExecutor_ || !settings.EventHandlers_.HandlersExecutor_) { maybeSettings = settings; - with_lock (Lock) { - if (!settings.DecompressionExecutor_) { - maybeSettings->DecompressionExecutor(Settings.DefaultCompressionExecutor_); - } - if (!settings.EventHandlers_.HandlersExecutor_) { - maybeSettings->EventHandlers_.HandlersExecutor(Settings.DefaultHandlersExecutor_); - } + std::lock_guard guard(Lock); + if (!settings.DecompressionExecutor_) { + maybeSettings->DecompressionExecutor(Settings.DefaultCompressionExecutor_); + } + if (!settings.EventHandlers_.HandlersExecutor_) { + maybeSettings->EventHandlers_.HandlersExecutor(Settings.DefaultHandlersExecutor_); } } auto session = std::make_shared(maybeSettings.value_or(settings), shared_from_this(), Connections_, DbDriverState_); @@ -28,13 +27,12 @@ std::shared_ptr TPersQueueClient::TImpl::CreateWriteSession( std::optional maybeSettings; if (!settings.CompressionExecutor_ || !settings.EventHandlers_.HandlersExecutor_) { maybeSettings = settings; - with_lock (Lock) { - if (!settings.CompressionExecutor_) { - maybeSettings->CompressionExecutor(Settings.DefaultCompressionExecutor_); - } - if (!settings.EventHandlers_.HandlersExecutor_) { - maybeSettings->EventHandlers_.HandlersExecutor(Settings.DefaultHandlersExecutor_); - } + std::lock_guard guard(Lock); + if (!settings.CompressionExecutor_) { + maybeSettings->CompressionExecutor(Settings.DefaultCompressionExecutor_); + } + if (!settings.EventHandlers_.HandlersExecutor_) { + maybeSettings->EventHandlers_.HandlersExecutor(Settings.DefaultHandlersExecutor_); } } auto session = std::make_shared( @@ -48,7 +46,8 @@ std::shared_ptr TPersQueueClient::TImpl::CreateSimp const TWriteSessionSettings& settings ) { auto alteredSettings = settings; - with_lock (Lock) { + { + std::lock_guard guard(Lock); alteredSettings.EventHandlers_.HandlersExecutor(Settings.DefaultHandlersExecutor_); if (!settings.CompressionExecutor_) { alteredSettings.CompressionExecutor(Settings.DefaultCompressionExecutor_); @@ -62,14 +61,13 @@ std::shared_ptr TPersQueueClient::TImpl::CreateSimp } std::shared_ptr TPersQueueClient::TImpl::GetClientForEndpoint(const std::string& clusterEndoint) { - with_lock (Lock) { - Y_ABORT_UNLESS(CustomEndpoint.empty()); - std::shared_ptr& client = Subclients[clusterEndoint]; - if (!client) { - client = std::make_shared(clusterEndoint, Connections_, Settings); - } - return client; + std::lock_guard guard(Lock); + Y_ABORT_UNLESS(CustomEndpoint.empty()); + std::shared_ptr& client = Subclients[clusterEndoint]; + if (!client) { + client = std::make_shared(clusterEndoint, Connections_, Settings); } + return client; } std::shared_ptr TPersQueueClient::TImpl::CreateReadSessionConnectionProcessorFactory() { diff --git a/client/ydb_persqueue_core/impl/read_session.cpp b/client/ydb_persqueue_core/impl/read_session.cpp index 8a0ffbfb8fe..6a8be895619 100644 --- a/client/ydb_persqueue_core/impl/read_session.cpp +++ b/client/ydb_persqueue_core/impl/read_session.cpp @@ -34,8 +34,8 @@ std::pair GetMessageOffsetRange(const TReadSessionEvent::TDataReceiv //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // TReadSession -NUtils::TYdbStringBuilder TReadSession::GetLogPrefix() const { - return NUtils::TYdbStringBuilder() << GetDatabaseLogPrefix(DbDriverState->Database) << "[" << SessionId << "] "; +TStringBuilder TReadSession::GetLogPrefix() const { + return TStringBuilder() << GetDatabaseLogPrefix(DbDriverState->Database) << "[" << SessionId << "] "; } TReadSession::TReadSession(const TReadSessionSettings& settings, @@ -127,7 +127,8 @@ bool TReadSession::ValidateSettings() { } void TReadSession::StartClusterDiscovery() { - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (Aborting) { return; } @@ -167,7 +168,8 @@ void TReadSession::StartClusterDiscovery() { void TReadSession::ProceedWithoutClusterDiscovery() { TDeferredActions deferred; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (Aborting) { return; } @@ -226,7 +228,8 @@ void TReadSession::CreateClusterSessionsImpl(TDeferredActions& deferred) { void TReadSession::OnClusterDiscovery(const TStatus& status, const Ydb::PersQueue::ClusterDiscovery::DiscoverClustersResult& result) { TDeferredActions deferred; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (Aborting) { return; } @@ -256,7 +259,7 @@ void TReadSession::OnClusterDiscovery(const TStatus& status, const Ydb::PersQueu // Init ClusterSessions. if (static_cast(result.read_sessions_clusters_size()) != Settings.Topics_.size()) { ++*Settings.Counters_->Errors; - AbortImpl(EStatus::INTERNAL_ERROR, NUtils::TYdbStringBuilder() << "Unexpected reply from cluster discovery. Sizes of topics arrays don't match: " + AbortImpl(EStatus::INTERNAL_ERROR, TStringBuilder() << "Unexpected reply from cluster discovery. Sizes of topics arrays don't match: " << result.read_sessions_clusters_size() << " vs " << Settings.Topics_.size(), deferred); return; } @@ -289,12 +292,12 @@ void TReadSession::OnClusterDiscovery(const TStatus& status, const Ydb::PersQueu } TClusterSessionInfo& clusterSessionInfo = clusterSessionInfoIter->second; if (cluster.endpoint().empty()) { - issues.AddIssue(NUtils::TYdbStringBuilder() << "Unexpected reply from cluster discovery. Empty endpoint for cluster " + issues.AddIssue(TStringBuilder() << "Unexpected reply from cluster discovery. Empty endpoint for cluster " << normalizedName); } auto fullEndpoint = ApplyClusterEndpoint(DbDriverState->DiscoveryEndpoint, cluster.endpoint()); if (!clusterSessionInfo.ClusterEndpoint.empty() && clusterSessionInfo.ClusterEndpoint != fullEndpoint) { - issues.AddIssue(NUtils::TYdbStringBuilder() << "Unexpected reply from cluster discovery. Different endpoints for one cluster name. Cluster: " + issues.AddIssue(TStringBuilder() << "Unexpected reply from cluster discovery. Different endpoints for one cluster name. Cluster: " << normalizedName << ". \"" << clusterSessionInfo.ClusterEndpoint << "\" vs \"" << fullEndpoint << "\""); } @@ -310,7 +313,7 @@ void TReadSession::OnClusterDiscovery(const TStatus& status, const Ydb::PersQueu for (const auto& [cluster, clusterInfo] : ClusterSessions) { if (clusterInfo.Topics.empty()) { // If specified explicitly by user. errorStatus = EStatus::BAD_REQUEST; - issues.AddIssue(NUtils::TYdbStringBuilder() << "Unsupported cluster: " << cluster); + issues.AddIssue(TStringBuilder() << "Unsupported cluster: " << cluster); } } @@ -369,7 +372,8 @@ bool TReadSession::Close(TDuration timeout) { }; TDeferredActions deferred; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (Closing || Aborting) { return false; } @@ -423,13 +427,12 @@ bool TReadSession::Close(TDuration timeout) { } NYql::TIssues issues; - issues.AddIssue(NUtils::TYdbStringBuilder() << "Session was closed after waiting " << timeout); + issues.AddIssue(TStringBuilder() << "Session was closed after waiting " << timeout); EventsQueue->Close(TSessionClosedEvent(EStatus::TIMEOUT, std::move(issues)), deferred); } - with_lock (Lock) { - Aborting = true; // Set abort flag for doing nothing on destructor. - } + std::lock_guard guard(Lock); + Aborting = true; // Set abort flag for doing nothing on destructor. return result; } @@ -476,9 +479,8 @@ void TReadSession::AbortImpl(EStatus statusCode, const std::string& message, TDe void TReadSession::Abort() { TDeferredActions deferred; - with_lock (Lock) { - AbortImpl(EStatus::ABORTED, "Aborted", deferred); - } + std::lock_guard guard(Lock); + AbortImpl(EStatus::ABORTED, "Aborted", deferred); } void TReadSession::ClearAllEvents() { @@ -507,14 +509,13 @@ std::optional TReadSession::GetEvent(bool block, size void TReadSession::StopReadingData() { LOG_LAZY(Log, TLOG_INFO, GetLogPrefix() << "Stop reading data"); - with_lock (Lock) { - if (!DataReadingSuspended) { - DataReadingSuspended = true; + std::lock_guard guard(Lock); + if (!DataReadingSuspended) { + DataReadingSuspended = true; - for (auto& [cluster, sessionInfo] : ClusterSessions) { - if (sessionInfo.Session) { - sessionInfo.Session->StopReadingData(); - } + for (auto& [cluster, sessionInfo] : ClusterSessions) { + if (sessionInfo.Session) { + sessionInfo.Session->StopReadingData(); } } } @@ -522,14 +523,13 @@ void TReadSession::StopReadingData() { void TReadSession::ResumeReadingData() { LOG_LAZY(Log, TLOG_INFO, GetLogPrefix() << "Resume reading data"); - with_lock (Lock) { - if (DataReadingSuspended) { - DataReadingSuspended = false; + std::lock_guard guard(Lock); + if (DataReadingSuspended) { + DataReadingSuspended = false; - for (auto& [cluster, sessionInfo] : ClusterSessions) { - if (sessionInfo.Session) { - sessionInfo.Session->ResumeReadingData(); - } + for (auto& [cluster, sessionInfo] : ClusterSessions) { + if (sessionInfo.Session) { + sessionInfo.Session->ResumeReadingData(); } } } @@ -547,12 +547,11 @@ void TReadSession::MakeCountersIfNeeded() { } void TReadSession::SetupCountersLogger() { - with_lock(Lock) { - CountersLogger = std::make_shared>(Connections, CbContexts, Settings.Counters_, Log, - GetLogPrefix(), StartSessionTime); - DumpCountersContext = CountersLogger->MakeCallbackContext(); - CountersLogger->Start(); - } + std::lock_guard guard(Lock); + CountersLogger = std::make_shared>(Connections, CbContexts, Settings.Counters_, Log, + GetLogPrefix(), StartSessionTime); + DumpCountersContext = CountersLogger->MakeCallbackContext(); + CountersLogger->Start(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -623,7 +622,7 @@ std::string DebugString(const TReadSessionEvent::TEvent& event) { } std::string TReadSessionEvent::TDataReceivedEvent::DebugString(bool printData) const { - NUtils::TYdbStringBuilder ret; + TStringBuilder ret; ret << "DataReceived { PartitionStreamId: " << GetPartitionStream()->GetPartitionStreamId() << " PartitionId: " << GetPartitionStream()->GetPartitionId(); for (const auto& message : Messages) { @@ -639,14 +638,14 @@ std::string TReadSessionEvent::TDataReceivedEvent::DebugString(bool printData) c } std::string TReadSessionEvent::TCommitAcknowledgementEvent::DebugString() const { - return NUtils::TYdbStringBuilder() << "CommitAcknowledgement { PartitionStreamId: " << GetPartitionStream()->GetPartitionStreamId() + return TStringBuilder() << "CommitAcknowledgement { PartitionStreamId: " << GetPartitionStream()->GetPartitionStreamId() << " PartitionId: " << GetPartitionStream()->GetPartitionId() << " CommittedOffset: " << GetCommittedOffset() << " }"; } std::string TReadSessionEvent::TCreatePartitionStreamEvent::DebugString() const { - return NUtils::TYdbStringBuilder() << "CreatePartitionStream { PartitionStreamId: " << GetPartitionStream()->GetPartitionStreamId() + return TStringBuilder() << "CreatePartitionStream { PartitionStreamId: " << GetPartitionStream()->GetPartitionStreamId() << " TopicPath: " << GetPartitionStream()->GetTopicPath() << " Cluster: " << GetPartitionStream()->GetCluster() << " PartitionId: " << GetPartitionStream()->GetPartitionId() @@ -656,14 +655,14 @@ std::string TReadSessionEvent::TCreatePartitionStreamEvent::DebugString() const } std::string TReadSessionEvent::TDestroyPartitionStreamEvent::DebugString() const { - return NUtils::TYdbStringBuilder() << "DestroyPartitionStream { PartitionStreamId: " << GetPartitionStream()->GetPartitionStreamId() + return TStringBuilder() << "DestroyPartitionStream { PartitionStreamId: " << GetPartitionStream()->GetPartitionStreamId() << " PartitionId: " << GetPartitionStream()->GetPartitionId() << " CommittedOffset: " << GetCommittedOffset() << " }"; } std::string TReadSessionEvent::TPartitionStreamStatusEvent::DebugString() const { - return NUtils::TYdbStringBuilder() << "PartitionStreamStatus { PartitionStreamId: " << GetPartitionStream()->GetPartitionStreamId() + return TStringBuilder() << "PartitionStreamStatus { PartitionStreamId: " << GetPartitionStream()->GetPartitionStreamId() << " PartitionId: " << GetPartitionStream()->GetPartitionId() << " CommittedOffset: " << GetCommittedOffset() << " ReadOffset: " << GetReadOffset() @@ -673,7 +672,7 @@ std::string TReadSessionEvent::TPartitionStreamStatusEvent::DebugString() const } std::string TReadSessionEvent::TPartitionStreamClosedEvent::DebugString() const { - return NUtils::TYdbStringBuilder() << "PartitionStreamClosed { PartitionStreamId: " << GetPartitionStream()->GetPartitionStreamId() + return TStringBuilder() << "PartitionStreamClosed { PartitionStreamId: " << GetPartitionStream()->GetPartitionStreamId() << " PartitionId: " << GetPartitionStream()->GetPartitionId() << " Reason: " << GetReason() << " }"; @@ -681,7 +680,7 @@ std::string TReadSessionEvent::TPartitionStreamClosedEvent::DebugString() const std::string TSessionClosedEvent::DebugString() const { return - NUtils::TYdbStringBuilder() << "SessionClosed { Status: " << GetStatus() + TStringBuilder() << "SessionClosed { Status: " << GetStatus() << " Issues: \"" << IssuesSingleLineString(GetIssues()) << "\" }"; } @@ -706,7 +705,8 @@ class TGracefulReleasingSimpleDataHandlers : public TThrRefBase { void OnDataReceived(TReadSessionEvent::TDataReceivedEvent& event) { Y_ASSERT(event.GetMessagesCount()); TDeferredCommit deferredCommit; - with_lock (Lock) { + { + std::lock_guard guard(Lock); auto& offsetSet = PartitionStreamToUncommittedOffsets[event.GetPartitionStream()->GetPartitionStreamId()]; // Messages could contain holes in offset, but later commit ack will tell us right border. // So we can easily insert the whole interval with holes included. @@ -725,49 +725,47 @@ class TGracefulReleasingSimpleDataHandlers : public TThrRefBase { } void OnCommitAcknowledgement(TReadSessionEvent::TCommitAcknowledgementEvent& event) { - with_lock (Lock) { - const ui64 partitionStreamId = event.GetPartitionStream()->GetPartitionStreamId(); - auto& offsetSet = PartitionStreamToUncommittedOffsets[partitionStreamId]; - if (offsetSet.EraseInterval(0, event.GetCommittedOffset() + 1)) { // Remove some offsets. - if (offsetSet.Empty()) { // No offsets left. - auto unconfirmedDestroyIt = UnconfirmedDestroys.find(partitionStreamId); - if (unconfirmedDestroyIt != UnconfirmedDestroys.end()) { - // Confirm and forget about this partition stream. - unconfirmedDestroyIt->second.Confirm(); - UnconfirmedDestroys.erase(unconfirmedDestroyIt); - PartitionStreamToUncommittedOffsets.erase(partitionStreamId); - } + std::lock_guard guard(Lock); + const ui64 partitionStreamId = event.GetPartitionStream()->GetPartitionStreamId(); + auto& offsetSet = PartitionStreamToUncommittedOffsets[partitionStreamId]; + if (offsetSet.EraseInterval(0, event.GetCommittedOffset() + 1)) { // Remove some offsets. + if (offsetSet.Empty()) { // No offsets left. + auto unconfirmedDestroyIt = UnconfirmedDestroys.find(partitionStreamId); + if (unconfirmedDestroyIt != UnconfirmedDestroys.end()) { + // Confirm and forget about this partition stream. + unconfirmedDestroyIt->second.Confirm(); + UnconfirmedDestroys.erase(unconfirmedDestroyIt); + PartitionStreamToUncommittedOffsets.erase(partitionStreamId); } } } } void OnCreatePartitionStream(TReadSessionEvent::TCreatePartitionStreamEvent& event) { - with_lock (Lock) { + { + std::lock_guard guard(Lock); Y_ABORT_UNLESS(PartitionStreamToUncommittedOffsets[event.GetPartitionStream()->GetPartitionStreamId()].Empty()); } event.Confirm(); } void OnDestroyPartitionStream(TReadSessionEvent::TDestroyPartitionStreamEvent& event) { - with_lock (Lock) { - const ui64 partitionStreamId = event.GetPartitionStream()->GetPartitionStreamId(); - Y_ABORT_UNLESS(UnconfirmedDestroys.find(partitionStreamId) == UnconfirmedDestroys.end()); - if (PartitionStreamToUncommittedOffsets[partitionStreamId].Empty()) { - PartitionStreamToUncommittedOffsets.erase(partitionStreamId); - event.Confirm(); - } else { - UnconfirmedDestroys.emplace(partitionStreamId, std::move(event)); - } + std::lock_guard guard(Lock); + const ui64 partitionStreamId = event.GetPartitionStream()->GetPartitionStreamId(); + Y_ABORT_UNLESS(UnconfirmedDestroys.find(partitionStreamId) == UnconfirmedDestroys.end()); + if (PartitionStreamToUncommittedOffsets[partitionStreamId].Empty()) { + PartitionStreamToUncommittedOffsets.erase(partitionStreamId); + event.Confirm(); + } else { + UnconfirmedDestroys.emplace(partitionStreamId, std::move(event)); } } void OnPartitionStreamClosed(TReadSessionEvent::TPartitionStreamClosedEvent& event) { - with_lock (Lock) { - const ui64 partitionStreamId = event.GetPartitionStream()->GetPartitionStreamId(); - PartitionStreamToUncommittedOffsets.erase(partitionStreamId); - UnconfirmedDestroys.erase(partitionStreamId); - } + std::lock_guard guard(Lock); + const ui64 partitionStreamId = event.GetPartitionStream()->GetPartitionStreamId(); + PartitionStreamToUncommittedOffsets.erase(partitionStreamId); + UnconfirmedDestroys.erase(partitionStreamId); } private: @@ -893,7 +891,7 @@ void TDeferredCommit::TImpl::Add(const TReadSessionEvent::TDataReceivedEvent::TM void TDeferredCommit::TImpl::Add(const TPartitionStream::TPtr& partitionStream, TDisjointIntervalTree& offsetSet, ui64 startOffset, ui64 endOffset) { if (offsetSet.Intersects(startOffset, endOffset)) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Commit set already has some offsets from half-interval [" + ThrowFatalError(TStringBuilder() << "Commit set already has some offsets from half-interval [" << startOffset << "; " << endOffset << ") for partition stream with id " << partitionStream->GetPartitionStreamId()); } else { @@ -910,7 +908,7 @@ void TDeferredCommit::TImpl::Add(const TPartitionStream::TPtr& partitionStream, Y_ASSERT(partitionStream); auto& offsetSet = Offsets[partitionStream]; if (offsetSet.Has(offset)) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Commit set already has offset " << offset + ThrowFatalError(TStringBuilder() << "Commit set already has offset " << offset << " for partition stream with id " << partitionStream->GetPartitionStreamId()); } else { offsetSet.Insert(offset); diff --git a/client/ydb_persqueue_core/impl/read_session.h b/client/ydb_persqueue_core/impl/read_session.h index 02aaaf42367..3e6f5c4413b 100644 --- a/client/ydb_persqueue_core/impl/read_session.h +++ b/client/ydb_persqueue_core/impl/read_session.h @@ -704,7 +704,7 @@ class TPartitionStreamImpl : public TAPartitionStream { return this->PartitionSessionId; } }(); - ThrowFatalError(NUtils::TYdbStringBuilder() << "Invalid offset range [" << startOffset << ", " << endOffset << ") : range must start from " + ThrowFatalError(TStringBuilder() << "Invalid offset range [" << startOffset << ", " << endOffset << ") : range must start from " << MaxCommittedOffset << " or has some offsets that are committed already. Partition stream id: -" << id << Endl); return false; } @@ -1003,7 +1003,7 @@ class TSingleClusterReadSessionImpl : public NPersQueue::TEnableSelfContext::Commit(ui64 startOffset, ui64 e std::vector> toCommit; if (auto sessionShared = CbContext->LockShared()) { Y_ABORT_UNLESS(endOffset > startOffset); - with_lock(sessionShared->Lock) { + { + std::lock_guard guard(sessionShared->Lock); if (!AddToCommitRanges(startOffset, endOffset, true)) // Add range for real commit always. return; @@ -226,8 +227,8 @@ void TRawPartitionStreamEventQueue::DeleteNotReadyTail(TDe // TSingleClusterReadSessionImpl template -NUtils::TYdbStringBuilder TSingleClusterReadSessionImpl::GetLogPrefix() const { - return NUtils::TYdbStringBuilder() << GetDatabaseLogPrefix(Database) << "[" << SessionId << "] [" << ClusterName << "] "; +TStringBuilder TSingleClusterReadSessionImpl::GetLogPrefix() const { + return TStringBuilder() << GetDatabaseLogPrefix(Database) << "[" << SessionId << "] [" << ClusterName << "] "; } template @@ -263,7 +264,8 @@ bool TSingleClusterReadSessionImpl::Reconnect(const TPlain NYdbGrpc::IQueueClientContextPtr connectTimeoutContext = nullptr; TDeferredActions deferred; - with_lock (Lock) { + { + std::lock_guard guard(Lock); connectContext = ClientContext->CreateContext(); connectTimeoutContext = ClientContext->CreateContext(); if (!connectContext || !connectTimeoutContext) { @@ -378,7 +380,8 @@ void TSingleClusterReadSessionImpl::BreakConnectionAndReco template void TSingleClusterReadSessionImpl::OnConnectTimeout(const NYdbGrpc::IQueueClientContextPtr& connectTimeoutContext) { - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (ConnectTimeoutContext == connectTimeoutContext) { Cancel(ConnectContext); ConnectContext = nullptr; @@ -395,7 +398,7 @@ void TSingleClusterReadSessionImpl::OnConnectTimeout(const } ++*Settings.Counters_->Errors; - NUtils::TYdbStringBuilder description; + TStringBuilder description; description << "Failed to establish connection to server. Attempts done: " << ConnectionAttemptsDone; if (!Reconnect(TPlainStatus(EStatus::TIMEOUT, description))) { AbortSession(EStatus::TIMEOUT, description); @@ -406,7 +409,8 @@ template void TSingleClusterReadSessionImpl::OnConnect( TPlainStatus&& st, typename IProcessor::TPtr&& processor, const NYdbGrpc::IQueueClientContextPtr& connectContext) { TDeferredActions deferred; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (ConnectContext == connectContext) { Cancel(ConnectTimeoutContext); ConnectContext = nullptr; @@ -433,7 +437,7 @@ void TSingleClusterReadSessionImpl::OnConnect( ++*Settings.Counters_->Errors; if (!Reconnect(st)) { AbortSession( - st.Status, MakeIssueWithSubIssues(NUtils::TYdbStringBuilder() << "Failed to establish connection to server \"" + st.Status, MakeIssueWithSubIssues(TStringBuilder() << "Failed to establish connection to server \"" << st.Endpoint << "\" ( cluster " << ClusterName << "). Attempts done: " << ConnectionAttemptsDone, st.Issues)); @@ -573,7 +577,7 @@ bool TSingleClusterReadSessionImpl::IsActualPartitionStrea template void TSingleClusterReadSessionImpl::ConfirmPartitionStreamCreate(const TPartitionStreamImpl* partitionStream, std::optional readOffset, std::optional commitOffset) { - NUtils::TYdbStringBuilder commitOffsetLogStr; + TStringBuilder commitOffsetLogStr; if (commitOffset) { commitOffsetLogStr << ". Commit offset: " << *commitOffset; } @@ -585,43 +589,42 @@ void TSingleClusterReadSessionImpl::ConfirmPartitionStream << ". Read offset: " << readOffset << commitOffsetLogStr ); - with_lock (Lock) { - if (Aborting || Closing || !IsActualPartitionStreamImpl(partitionStream)) { // Got previous incarnation. - LOG_LAZY(Log, - TLOG_DEBUG, - GetLogPrefix() << "Skip partition stream create confirm. Partition stream id: " - << GetPartitionStreamId(partitionStream) - ); - return; - } + std::lock_guard guard(Lock); + if (Aborting || Closing || !IsActualPartitionStreamImpl(partitionStream)) { // Got previous incarnation. + LOG_LAZY(Log, + TLOG_DEBUG, + GetLogPrefix() << "Skip partition stream create confirm. Partition stream id: " + << GetPartitionStreamId(partitionStream) + ); + return; + } - TClientMessage req; + TClientMessage req; - if constexpr (UseMigrationProtocol) { - auto& startRead = *req.mutable_start_read(); - startRead.mutable_topic()->set_path(partitionStream->GetTopicPath()); - startRead.set_cluster(partitionStream->GetCluster()); - startRead.set_partition(partitionStream->GetPartitionId()); - startRead.set_assign_id(partitionStream->GetAssignId()); - if (readOffset) { - startRead.set_read_offset(*readOffset); - } - if (commitOffset) { - startRead.set_commit_offset(*commitOffset); - } - } else { - auto& startRead = *req.mutable_start_partition_session_response(); - startRead.set_partition_session_id(partitionStream->GetAssignId()); - if (readOffset) { - startRead.set_read_offset(*readOffset); - } - if (commitOffset) { - startRead.set_commit_offset(*commitOffset); - } + if constexpr (UseMigrationProtocol) { + auto& startRead = *req.mutable_start_read(); + startRead.mutable_topic()->set_path(partitionStream->GetTopicPath()); + startRead.set_cluster(partitionStream->GetCluster()); + startRead.set_partition(partitionStream->GetPartitionId()); + startRead.set_assign_id(partitionStream->GetAssignId()); + if (readOffset) { + startRead.set_read_offset(*readOffset); + } + if (commitOffset) { + startRead.set_commit_offset(*commitOffset); + } + } else { + auto& startRead = *req.mutable_start_partition_session_response(); + startRead.set_partition_session_id(partitionStream->GetAssignId()); + if (readOffset) { + startRead.set_read_offset(*readOffset); + } + if (commitOffset) { + startRead.set_commit_offset(*commitOffset); } - - WriteToProcessorImpl(std::move(req)); } + + WriteToProcessorImpl(std::move(req)); } template @@ -635,54 +638,53 @@ void TSingleClusterReadSessionImpl::ConfirmPartitionStream ); TDeferredActions deferred; - with_lock (Lock) { - if (Aborting || Closing || !IsActualPartitionStreamImpl(partitionStream)) { // Got previous incarnation. - LOG_LAZY(Log, - TLOG_DEBUG, - GetLogPrefix() << "Skip partition stream destroy confirm. Partition stream id: " - << GetPartitionStreamId(partitionStream) - ); - return; - } + std::lock_guard guard(Lock); + if (Aborting || Closing || !IsActualPartitionStreamImpl(partitionStream)) { // Got previous incarnation. + LOG_LAZY(Log, + TLOG_DEBUG, + GetLogPrefix() << "Skip partition stream destroy confirm. Partition stream id: " + << GetPartitionStreamId(partitionStream) + ); + return; + } - using TClosedEvent = std::conditional_t< - UseMigrationProtocol, - NPersQueue::TReadSessionEvent::TPartitionStreamClosedEvent, - NTopic::TReadSessionEvent::TPartitionSessionClosedEvent - >; + using TClosedEvent = std::conditional_t< + UseMigrationProtocol, + NPersQueue::TReadSessionEvent::TPartitionStreamClosedEvent, + NTopic::TReadSessionEvent::TPartitionSessionClosedEvent + >; - CookieMapping.RemoveMapping(GetPartitionStreamId(partitionStream)); - PartitionStreams.erase(partitionStream->GetAssignId()); + CookieMapping.RemoveMapping(GetPartitionStreamId(partitionStream)); + PartitionStreams.erase(partitionStream->GetAssignId()); - bool pushRes = true; - if constexpr (UseMigrationProtocol) { - pushRes = EventsQueue->PushEvent(partitionStream, - TClosedEvent(partitionStream, TClosedEvent::EReason::DestroyConfirmedByUser), - deferred); - } else { - pushRes = EventsQueue->PushEvent(partitionStream, - TClosedEvent(partitionStream, TClosedEvent::EReason::StopConfirmedByUser), - deferred); - } - if (!pushRes) { - AbortImpl(); - return; - } - TClientMessage req; - - if constexpr (UseMigrationProtocol) { - auto& released = *req.mutable_released(); - released.mutable_topic()->set_path(partitionStream->GetTopicPath()); - released.set_cluster(partitionStream->GetCluster()); - released.set_partition(partitionStream->GetPartitionId()); - released.set_assign_id(partitionStream->GetAssignId()); - } else { - auto& released = *req.mutable_stop_partition_session_response(); - released.set_partition_session_id(partitionStream->GetAssignId()); - } + bool pushRes = true; + if constexpr (UseMigrationProtocol) { + pushRes = EventsQueue->PushEvent(partitionStream, + TClosedEvent(partitionStream, TClosedEvent::EReason::DestroyConfirmedByUser), + deferred); + } else { + pushRes = EventsQueue->PushEvent(partitionStream, + TClosedEvent(partitionStream, TClosedEvent::EReason::StopConfirmedByUser), + deferred); + } + if (!pushRes) { + AbortImpl(); + return; + } + TClientMessage req; - WriteToProcessorImpl(std::move(req)); + if constexpr (UseMigrationProtocol) { + auto& released = *req.mutable_released(); + released.mutable_topic()->set_path(partitionStream->GetTopicPath()); + released.set_cluster(partitionStream->GetCluster()); + released.set_partition(partitionStream->GetPartitionId()); + released.set_assign_id(partitionStream->GetAssignId()); + } else { + auto& released = *req.mutable_stop_partition_session_response(); + released.set_partition_session_id(partitionStream->GetAssignId()); } + + WriteToProcessorImpl(std::move(req)); } template @@ -692,43 +694,42 @@ void TSingleClusterReadSessionImpl::Commit(const TPartitio GetLogPrefix() << "Commit offsets [" << startOffset << ", " << endOffset << "). Partition stream id: " << GetPartitionStreamId(partitionStream) ); - with_lock (Lock) { - if (Aborting || Closing || !IsActualPartitionStreamImpl(partitionStream)) { // Got previous incarnation. - return; - } - TClientMessage req; - bool hasSomethingToCommit = false; + std::lock_guard guard(Lock); + if (Aborting || Closing || !IsActualPartitionStreamImpl(partitionStream)) { // Got previous incarnation. + return; + } + TClientMessage req; + bool hasSomethingToCommit = false; - if constexpr (UseMigrationProtocol) { - if (GetRangesMode()) { - hasSomethingToCommit = true; - auto* range = req.mutable_commit()->add_offset_ranges(); - range->set_assign_id(partitionStream->GetAssignId()); - range->set_start_offset(startOffset); - range->set_end_offset(endOffset); - } else { - for (ui64 offset = startOffset; offset < endOffset; ++offset) { - typename TPartitionCookieMapping::TCookie::TPtr cookie = CookieMapping.CommitOffset(GetPartitionStreamId(partitionStream), offset); - if (cookie) { - hasSomethingToCommit = true; - auto* cookieInfo = req.mutable_commit()->add_cookies(); - cookieInfo->set_assign_id(partitionStream->GetAssignId()); - cookieInfo->set_partition_cookie(cookie->Cookie); - } + if constexpr (UseMigrationProtocol) { + if (GetRangesMode()) { + hasSomethingToCommit = true; + auto* range = req.mutable_commit()->add_offset_ranges(); + range->set_assign_id(partitionStream->GetAssignId()); + range->set_start_offset(startOffset); + range->set_end_offset(endOffset); + } else { + for (ui64 offset = startOffset; offset < endOffset; ++offset) { + typename TPartitionCookieMapping::TCookie::TPtr cookie = CookieMapping.CommitOffset(GetPartitionStreamId(partitionStream), offset); + if (cookie) { + hasSomethingToCommit = true; + auto* cookieInfo = req.mutable_commit()->add_cookies(); + cookieInfo->set_assign_id(partitionStream->GetAssignId()); + cookieInfo->set_partition_cookie(cookie->Cookie); } } - } else { - hasSomethingToCommit = true; - auto* part_commit = req.mutable_commit_offset_request()->add_commit_offsets(); - part_commit->set_partition_session_id(partitionStream->GetAssignId()); - auto* range = part_commit->add_offsets(); - range->set_start(startOffset); - range->set_end(endOffset); } + } else { + hasSomethingToCommit = true; + auto* part_commit = req.mutable_commit_offset_request()->add_commit_offsets(); + part_commit->set_partition_session_id(partitionStream->GetAssignId()); + auto* range = part_commit->add_offsets(); + range->set_start(startOffset); + range->set_end(endOffset); + } - if (hasSomethingToCommit) { - WriteToProcessorImpl(std::move(req)); - } + if (hasSomethingToCommit) { + WriteToProcessorImpl(std::move(req)); } } @@ -738,26 +739,25 @@ void TSingleClusterReadSessionImpl::RequestPartitionStream TLOG_DEBUG, GetLogPrefix() << "Requesting status for partition stream id: " << GetPartitionStreamId(partitionStream) ); - with_lock (Lock) { - if (Aborting || Closing || !IsActualPartitionStreamImpl(partitionStream)) { // Got previous incarnation. - return; - } - - TClientMessage req; + std::lock_guard guard(Lock); + if (Aborting || Closing || !IsActualPartitionStreamImpl(partitionStream)) { // Got previous incarnation. + return; + } - if constexpr (UseMigrationProtocol) { - auto& status = *req.mutable_status(); - status.mutable_topic()->set_path(partitionStream->GetTopicPath()); - status.set_cluster(partitionStream->GetCluster()); - status.set_partition(partitionStream->GetPartitionId()); - status.set_assign_id(partitionStream->GetAssignId()); - } else { - auto& status = *req.mutable_partition_session_status_request(); - status.set_partition_session_id(partitionStream->GetAssignId()); - } + TClientMessage req; - WriteToProcessorImpl(std::move(req)); + if constexpr (UseMigrationProtocol) { + auto& status = *req.mutable_status(); + status.mutable_topic()->set_path(partitionStream->GetTopicPath()); + status.set_cluster(partitionStream->GetCluster()); + status.set_partition(partitionStream->GetPartitionId()); + status.set_assign_id(partitionStream->GetAssignId()); + } else { + auto& status = *req.mutable_partition_session_status_request(); + status.set_partition_session_id(partitionStream->GetAssignId()); } + + WriteToProcessorImpl(std::move(req)); } template @@ -775,15 +775,14 @@ void TSingleClusterReadSessionImpl::OnUserRetrievedEvent(i *Settings.Counters_->BytesInflightUncompressed -= decompressedSize; TDeferredActions deferred; - with_lock (Lock) { - UpdateMemoryUsageStatisticsImpl(); + std::lock_guard guard(Lock); + UpdateMemoryUsageStatisticsImpl(); - Y_ABORT_UNLESS(decompressedSize <= DecompressedDataSize); - DecompressedDataSize -= decompressedSize; + Y_ABORT_UNLESS(decompressedSize <= DecompressedDataSize); + DecompressedDataSize -= decompressedSize; - ContinueReadingDataImpl(); - StartDecompressionTasksImpl(deferred); - } + ContinueReadingDataImpl(); + StartDecompressionTasksImpl(deferred); } template @@ -846,7 +845,8 @@ void TSingleClusterReadSessionImpl::OnReadDone(NYdbGrpc::T } TDeferredActions deferred; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (Aborting) { return; } @@ -965,7 +965,7 @@ inline void TSingleClusterReadSessionImpl::OnReadDoneImpl( if (partitionStreamIt == PartitionStreams.end()) { ++*Settings.Counters_->Errors; BreakConnectionAndReconnectImpl(EStatus::INTERNAL_ERROR, - NUtils::TYdbStringBuilder() + TStringBuilder() << "Got unexpected partition stream data message. Topic: " << partitionData.topic() << ". Partition: " << partitionData.partition() << " AssignId: " << partitionData.cookie().assign_id(), @@ -1003,7 +1003,7 @@ inline void TSingleClusterReadSessionImpl::OnReadDoneImpl( } if (firstOffset == std::numeric_limits::max()) { BreakConnectionAndReconnectImpl(EStatus::INTERNAL_ERROR, - NUtils::TYdbStringBuilder() << "Got empty data message. Topic: " + TStringBuilder() << "Got empty data message. Topic: " << partitionData.topic() << ". Partition: " << partitionData.partition() << " message: " << msg, @@ -1014,7 +1014,7 @@ inline void TSingleClusterReadSessionImpl::OnReadDoneImpl( partitionStream->SetFirstNotReadOffset(desiredOffset); if (!CookieMapping.AddMapping(cookie)) { BreakConnectionAndReconnectImpl(EStatus::INTERNAL_ERROR, - NUtils::TYdbStringBuilder() << "Got unexpected data message. Topic: " + TStringBuilder() << "Got unexpected data message. Topic: " << partitionData.topic() << ". Partition: " << partitionData.partition() << ". Cookie mapping already has such cookie", @@ -1224,7 +1224,7 @@ inline void TSingleClusterReadSessionImpl::OnReadDoneImpl( if (partitionStreamIt == PartitionStreams.end()) { ++*Settings.Counters_->Errors; BreakConnectionAndReconnectImpl(EStatus::INTERNAL_ERROR, - NUtils::TYdbStringBuilder() << "Got unexpected partition stream data message. " + TStringBuilder() << "Got unexpected partition stream data message. " << "PartitionSessionId: " << partitionData.partition_session_id(), deferred); return; @@ -1258,7 +1258,7 @@ inline void TSingleClusterReadSessionImpl::OnReadDoneImpl( } if (firstOffset == std::numeric_limits::max()) { BreakConnectionAndReconnectImpl(EStatus::INTERNAL_ERROR, - NUtils::TYdbStringBuilder() << "Got empty data message. " + TStringBuilder() << "Got empty data message. " << "PartitionSessionId: " << partitionData.partition_session_id() << " message: " << msg, deferred); @@ -1499,20 +1499,19 @@ void TSingleClusterReadSessionImpl::OnDecompressionInfoDes *Settings.Counters_->BytesInflightTotal -= (compressedSize + decompressedSize); TDeferredActions deferred; - with_lock (Lock) { - UpdateMemoryUsageStatisticsImpl(); - - CompressedDataSize -= compressedSize; - DecompressedDataSize -= decompressedSize; + std::lock_guard guard(Lock); + UpdateMemoryUsageStatisticsImpl(); - if constexpr (!UseMigrationProtocol) { - LOG_LAZY(Log, TLOG_DEBUG, GetLogPrefix() << "Returning serverBytesSize = " << serverBytesSize << " to budget"); - ReadSizeBudget += serverBytesSize; - } + CompressedDataSize -= compressedSize; + DecompressedDataSize -= decompressedSize; - ContinueReadingDataImpl(); - StartDecompressionTasksImpl(deferred); + if constexpr (!UseMigrationProtocol) { + LOG_LAZY(Log, TLOG_DEBUG, GetLogPrefix() << "Returning serverBytesSize = " << serverBytesSize << " to budget"); + ReadSizeBudget += serverBytesSize; } + + ContinueReadingDataImpl(); + StartDecompressionTasksImpl(deferred); } template @@ -1529,33 +1528,31 @@ void TSingleClusterReadSessionImpl::OnDataDecompressed(i64 *Settings.Counters_->BytesInflightCompressed -= sourceSize; *Settings.Counters_->BytesInflightTotal += (decompressedSize - sourceSize); - with_lock (Lock) { - UpdateMemoryUsageStatisticsImpl(); - CompressedDataSize -= sourceSize; - DecompressedDataSize += decompressedSize - estimatedDecompressedSize; - constexpr double weight = 0.6; - if (sourceSize > 0) { - AverageCompressionRatio = weight * static_cast(decompressedSize) / static_cast(sourceSize) + (1 - weight) * AverageCompressionRatio; - } - if (Aborting) { - return; - } - if constexpr (!UseMigrationProtocol) { - LOG_LAZY(Log, TLOG_DEBUG, GetLogPrefix() << "Returning serverBytesSize = " << serverBytesSize << " to budget"); - ReadSizeBudget += serverBytesSize; - } - ContinueReadingDataImpl(); - StartDecompressionTasksImpl(deferred); + std::lock_guard guard(Lock); + UpdateMemoryUsageStatisticsImpl(); + CompressedDataSize -= sourceSize; + DecompressedDataSize += decompressedSize - estimatedDecompressedSize; + constexpr double weight = 0.6; + if (sourceSize > 0) { + AverageCompressionRatio = weight * static_cast(decompressedSize) / static_cast(sourceSize) + (1 - weight) * AverageCompressionRatio; + } + if (Aborting) { + return; + } + if constexpr (!UseMigrationProtocol) { + LOG_LAZY(Log, TLOG_DEBUG, GetLogPrefix() << "Returning serverBytesSize = " << serverBytesSize << " to budget"); + ReadSizeBudget += serverBytesSize; } + ContinueReadingDataImpl(); + StartDecompressionTasksImpl(deferred); } template void TSingleClusterReadSessionImpl::Abort() { LOG_LAZY(Log, TLOG_DEBUG, GetLogPrefix() << "Abort session to cluster"); - with_lock (Lock) { - AbortImpl(); - } + std::lock_guard guard(Lock); + AbortImpl(); } template @@ -1588,32 +1585,31 @@ void TSingleClusterReadSessionImpl::AbortImpl() { template void TSingleClusterReadSessionImpl::Close(std::function callback) { - with_lock (Lock) { - if (Aborting) { - callback(); - } + std::lock_guard guard(Lock); + if (Aborting) { + callback(); + } - if (!Closing) { - Closing = true; + if (!Closing) { + Closing = true; - CloseCallback = std::move(callback); + CloseCallback = std::move(callback); - Cancel(ConnectContext); - Cancel(ConnectTimeoutContext); - Cancel(ConnectDelayContext); + Cancel(ConnectContext); + Cancel(ConnectTimeoutContext); + Cancel(ConnectDelayContext); - if (!Processor) { + if (!Processor) { + CallCloseCallbackImpl(); + } else { + if (!HasCommitsInflightImpl()) { + Processor->Cancel(); CallCloseCallbackImpl(); - } else { - if (!HasCommitsInflightImpl()) { - Processor->Cancel(); - CallCloseCallbackImpl(); - } } } - - AbortImpl(); } + + AbortImpl(); } template @@ -1629,43 +1625,40 @@ void TSingleClusterReadSessionImpl::CallCloseCallbackImpl( template void TSingleClusterReadSessionImpl::StopReadingData() { - with_lock (Lock) { - DataReadingSuspended = true; - } + std::lock_guard guard(Lock); + DataReadingSuspended = true; } template void TSingleClusterReadSessionImpl::ResumeReadingData() { - with_lock (Lock) { - if (DataReadingSuspended) { - DataReadingSuspended = false; - ContinueReadingDataImpl(); - } + std::lock_guard guard(Lock); + if (DataReadingSuspended) { + DataReadingSuspended = false; + ContinueReadingDataImpl(); } } template void TSingleClusterReadSessionImpl::DumpStatisticsToLog(TLogElement& log) { - with_lock (Lock) { - // cluster:topic:partition:stream-id:read-offset:committed-offset - for (auto&& [key, partitionStream] : PartitionStreams) { - if constexpr (UseMigrationProtocol) { - log << " " - << ClusterName - << ':' << partitionStream->GetTopicPath() - << ':' << partitionStream->GetPartitionId() - << ':' << partitionStream->GetPartitionStreamId() - << ':' << partitionStream->GetMaxReadOffset() - << ':' << partitionStream->GetMaxCommittedOffset(); - } else { - log << " " - << "-" - << ':' << partitionStream->GetTopicPath() - << ':' << partitionStream->GetPartitionId() - << ':' << partitionStream->GetPartitionSessionId() - << ':' << partitionStream->GetMaxReadOffset() - << ':' << partitionStream->GetMaxCommittedOffset(); - } + std::lock_guard guard(Lock); + // cluster:topic:partition:stream-id:read-offset:committed-offset + for (auto&& [key, partitionStream] : PartitionStreams) { + if constexpr (UseMigrationProtocol) { + log << " " + << ClusterName + << ':' << partitionStream->GetTopicPath() + << ':' << partitionStream->GetPartitionId() + << ':' << partitionStream->GetPartitionStreamId() + << ':' << partitionStream->GetMaxReadOffset() + << ':' << partitionStream->GetMaxCommittedOffset(); + } else { + log << " " + << "-" + << ':' << partitionStream->GetTopicPath() + << ':' << partitionStream->GetPartitionId() + << ':' << partitionStream->GetPartitionSessionId() + << ':' << partitionStream->GetMaxReadOffset() + << ':' << partitionStream->GetMaxCommittedOffset(); } } } @@ -1686,9 +1679,8 @@ void TSingleClusterReadSessionImpl::UpdateMemoryUsageStati template void TSingleClusterReadSessionImpl::UpdateMemoryUsageStatistics() { - with_lock (Lock) { - UpdateMemoryUsageStatisticsImpl(); - } + std::lock_guard guard(Lock); + UpdateMemoryUsageStatisticsImpl(); } template @@ -1726,7 +1718,7 @@ typename TSingleClusterReadSessionImpl::TPartitionCookieMa UncommittedOffsetToCookie.erase(cookieIt); return cookie; } else { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Invalid offset " << offset << ". Partition stream id: " << partitionStreamId << Endl); + ThrowFatalError(TStringBuilder() << "Invalid offset " << offset << ". Partition stream id: " << partitionStreamId << Endl); } // If offset wasn't found, there might be already hard released partition. // This situation is OK. @@ -2278,15 +2270,14 @@ void TDataDecompressionInfo::BuildBatchesMeta() { template void TDataDecompressionInfo::PutDecompressionError(std::exception_ptr error, size_t batch, size_t message) { if (!DecompressionErrorsStructCreated) { - with_lock (DecompressionErrorsStructLock) { - DecompressionErrors.resize(ServerMessage.batches_size()); - for (size_t batch = 0; batch < static_cast(ServerMessage.batches_size()); ++batch) { - DecompressionErrors[batch].resize(static_cast(ServerMessage.batches(batch).message_data_size())); - } - - // Set barrier. - DecompressionErrorsStructCreated = true; + std::lock_guard guard(DecompressionErrorsStructLock); + DecompressionErrors.resize(ServerMessage.batches_size()); + for (size_t batch = 0; batch < static_cast(ServerMessage.batches_size()); ++batch) { + DecompressionErrors[batch].resize(static_cast(ServerMessage.batches(batch).message_data_size())); } + + // Set barrier. + DecompressionErrorsStructCreated = true; } Y_ASSERT(batch < DecompressionErrors.size()); Y_ASSERT(message < DecompressionErrors[batch].size()); @@ -2360,10 +2351,9 @@ void TDataDecompressionInfo::PlanDecompressionTasks(double TDataDecompressionInfo::shared_from_this(), ReadyThresholds.back().Ready); if (!pushRes) { - // with_lock(session->Lock) { + // std::lock_guard guard(session->Lock); session->Abort(); return; - // } } } @@ -2472,7 +2462,7 @@ void TDataDecompressionEvent::TakeData(TIntrusivePtrGetLog(), TLOG_DEBUG, NUtils::TYdbStringBuilder() + LOG_LAZY(partitionStream->GetLog(), TLOG_DEBUG, TStringBuilder() << "Take Data. Partition " << partitionStream->GetPartitionId() << ". Read: {" << Batch << ", " << Message << "} (" << minOffset << "-" << maxOffset << ")"); @@ -2590,7 +2580,7 @@ void TDataDecompressionInfo::TDecompressionTask::operator( } } if (auto session = Parent->CbContext->LockShared()) { - LOG_LAZY(session->GetLog(), TLOG_DEBUG, NUtils::TYdbStringBuilder() << "Decompression task done. Partition/PartitionSessionId: " + LOG_LAZY(session->GetLog(), TLOG_DEBUG, TStringBuilder() << "Decompression task done. Partition/PartitionSessionId: " << partition_id << " (" << minOffset << "-" << maxOffset << ")"); } diff --git a/client/ydb_persqueue_core/impl/read_session_messages.cpp b/client/ydb_persqueue_core/impl/read_session_messages.cpp index 9d3931bdc4c..b1c7180871d 100644 --- a/client/ydb_persqueue_core/impl/read_session_messages.cpp +++ b/client/ydb_persqueue_core/impl/read_session_messages.cpp @@ -24,7 +24,7 @@ TReadSessionEvent::TDataReceivedEvent::TMessageInformation::TMessageInformation( , UncompressedSize(uncompressedSize) {} -static void DebugStringImpl(const TReadSessionEvent::TDataReceivedEvent::TMessageInformation& info, NUtils::TYdbStringBuilder& ret) { +static void DebugStringImpl(const TReadSessionEvent::TDataReceivedEvent::TMessageInformation& info, TStringBuilder& ret) { ret << " Information: {" << " Offset: " << info.Offset << " SeqNo: " << info.SeqNo @@ -43,7 +43,7 @@ static void DebugStringImpl(const TReadSessionEvent::TDataReceivedEvent::TMessag } template -static void DebugStringImpl(NUtils::TYdbStringBuilder& ret, +static void DebugStringImpl(TStringBuilder& ret, const std::string& name, const TReadSessionEvent::TDataReceivedEvent::IMessage& msg, bool printData, @@ -90,7 +90,7 @@ const std::string TReadSessionEvent::TDataReceivedEvent::IMessage::GetExplicitHa } std::string TReadSessionEvent::TDataReceivedEvent::IMessage::DebugString(bool printData) const { - NUtils::TYdbStringBuilder ret; + TStringBuilder ret; DebugString(ret, printData); return std::move(ret); } @@ -144,8 +144,8 @@ const TWriteSessionMeta::TPtr& TReadSessionEvent::TDataReceivedEvent::TMessage:: return Information.Meta; } -void TReadSessionEvent::TDataReceivedEvent::TMessage::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const { - DebugStringImpl(ret, "Message", *this, printData, [this](NUtils::TYdbStringBuilder& ret) { +void TReadSessionEvent::TDataReceivedEvent::TMessage::DebugString(TStringBuilder& ret, bool printData) const { + DebugStringImpl(ret, "Message", *this, printData, [this](TStringBuilder& ret) { DebugStringImpl(this->Information, ret); }); } @@ -206,13 +206,13 @@ ui64 TReadSessionEvent::TDataReceivedEvent::TCompressedMessage::GetUncompressedS return Information.at(index).UncompressedSize; } -void TReadSessionEvent::TDataReceivedEvent::TCompressedMessage::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const { +void TReadSessionEvent::TDataReceivedEvent::TCompressedMessage::DebugString(TStringBuilder& ret, bool printData) const { DebugStringImpl( ret, "CompressedMessage", *this, printData, - [this](NUtils::TYdbStringBuilder& ret) { + [this](TStringBuilder& ret) { for (auto& info : this->Information) { DebugStringImpl(info, ret); } diff --git a/client/ydb_persqueue_core/impl/write_session_impl.cpp b/client/ydb_persqueue_core/impl/write_session_impl.cpp index 92cee33d4cf..65abba46471 100644 --- a/client/ydb_persqueue_core/impl/write_session_impl.cpp +++ b/client/ydb_persqueue_core/impl/write_session_impl.cpp @@ -97,7 +97,8 @@ void TWriteSessionImpl::Start(const TDuration& delay) { ++ConnectionAttemptsDone; if (!Started) { - with_lock(Lock) { + { + std::lock_guard guard(Lock); HandleWakeUpImpl(); } InitWriter(); @@ -150,7 +151,8 @@ bool IsFederation(const std::string& endpoint) { void TWriteSessionImpl::DoCdsRequest(TDuration delay) { bool cdsRequestIsUnnecessary; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (AtomicGet(Aborting)) { return; } @@ -183,7 +185,7 @@ void TWriteSessionImpl::DoCdsRequest(TDuration delay) { LOG_LAZY(DbDriverState->Log, TLOG_INFO, LogPrefix() << "Do schedule cds request after " << delay.MilliSeconds() << " ms\n"); auto cdsRequestCall = [req_=std::move(req), extr=std::move(extractor), connections = std::shared_ptr(Connections), dbState=DbDriverState, settings=Settings]() mutable { - LOG_LAZY(dbState->Log, TLOG_INFO, NUtils::TYdbStringBuilder() << "MessageGroupId [" << settings.MessageGroupId_ << "] Running cds request ms\n"); + LOG_LAZY(dbState->Log, TLOG_INFO, TStringBuilder() << "MessageGroupId [" << settings.MessageGroupId_ << "] Running cds request ms\n"); connections->RunDeferred( @@ -212,7 +214,8 @@ void TWriteSessionImpl::OnCdsResponse( std::string endpoint, name; THandleResult handleResult; if (!status.IsSuccess()) { - with_lock (Lock) { + { + std::lock_guard guard(Lock); handleResult = OnErrorImpl({ status.GetStatus(), MakeIssueWithSubIssues("Failed to discover clusters", status.GetIssues()) @@ -224,7 +227,8 @@ void TWriteSessionImpl::OnCdsResponse( NYql::TIssues issues; EStatus errorStatus = EStatus::INTERNAL_ERROR; - with_lock (Lock) { + { + std::lock_guard guard(Lock); const Ydb::PersQueue::ClusterDiscovery::WriteSessionClusters& wsClusters = result.write_sessions_clusters(0); bool isFirst = true; @@ -240,14 +244,14 @@ void TWriteSessionImpl::OnCdsResponse( if (!clusterInfo.available()) { if (!TargetCluster.empty() && TargetCluster == normalizedName) { errorStatus = EStatus::UNAVAILABLE; - issues.AddIssue(NUtils::TYdbStringBuilder() << "Selected destination cluster: " << normalizedName + issues.AddIssue(TStringBuilder() << "Selected destination cluster: " << normalizedName << " is currently disabled"); break; } continue; } if (clusterInfo.endpoint().empty()) { - issues.AddIssue(NUtils::TYdbStringBuilder() << "Unexpected reply from cluster discovery. Empty endpoint for cluster " + issues.AddIssue(TStringBuilder() << "Unexpected reply from cluster discovery. Empty endpoint for cluster " << normalizedName); } else { name = clusterInfo.name(); @@ -257,17 +261,19 @@ void TWriteSessionImpl::OnCdsResponse( } if (endpoint.empty()) { errorStatus = EStatus::GENERIC_ERROR; - issues.AddIssue(NUtils::TYdbStringBuilder() << "Could not get valid endpoint from cluster discovery"); + issues.AddIssue(TStringBuilder() << "Could not get valid endpoint from cluster discovery"); } } if (issues) { - with_lock(Lock) { + { + std::lock_guard guard(Lock); handleResult = OnErrorImpl({errorStatus, std::move(issues)}); } ProcessHandleResult(handleResult); - return; + return; } - with_lock(Lock) { + { + std::lock_guard guard(Lock); if (InitialCluster.empty()) { InitialCluster = name; } @@ -385,7 +391,8 @@ void TWriteSessionImpl::WriteInternal( TInstant createdAtValue = createTimestamp.value_or(TInstant::Now()); bool readyToAccept = false; size_t bufferSize = data.size(); - with_lock(Lock) { + { + std::lock_guard guard(Lock); CurrentBatch.Add(GetNextIdImpl(seqNo), createdAtValue, data, codec, originalSize); FlushWriteIfRequiredImpl(); @@ -438,7 +445,8 @@ void TWriteSessionImpl::DoConnect(const TDuration& delay, const std::string& end std::function connectCallback; std::function connectTimeoutCallback; - with_lock(Lock) { + { + std::lock_guard guard(Lock); if (Aborting) { return; } @@ -512,7 +520,8 @@ void TWriteSessionImpl::DoConnect(const TDuration& delay, const std::string& end void TWriteSessionImpl::OnConnectTimeout(const NYdbGrpc::IQueueClientContextPtr& connectTimeoutContext) { LOG_LAZY(DbDriverState->Log, TLOG_ERR, LogPrefix() << "Write session: connect timeout"); THandleResult handleResult; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (ConnectTimeoutContext == connectTimeoutContext) { Cancel(ConnectContext); ConnectContext = nullptr; @@ -521,7 +530,7 @@ void TWriteSessionImpl::OnConnectTimeout(const NYdbGrpc::IQueueClientContextPtr& } else { return; } - NUtils::TYdbStringBuilder description; + TStringBuilder description; description << "Failed to establish connection to server. Attempts done: " << ConnectionAttemptsDone; handleResult = RestartImpl(TPlainStatus(EStatus::TIMEOUT, description)); if (handleResult.DoStop) { @@ -539,7 +548,8 @@ void TWriteSessionImpl::OnConnect( TPlainStatus&& st, typename IProcessor::TPtr&& processor, const NYdbGrpc::IQueueClientContextPtr& connectContext ) { THandleResult handleResult; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (ConnectContext == connectContext) { Cancel(ConnectTimeoutContext); ConnectContext = nullptr; @@ -560,7 +570,7 @@ void TWriteSessionImpl::OnConnect( CloseImpl( st.Status, MakeIssueWithSubIssues( - NUtils::TYdbStringBuilder() << "Failed to establish connection to server \"" << st.Endpoint + TStringBuilder() << "Failed to establish connection to server \"" << st.Endpoint << "\". Attempts done: " << ConnectionAttemptsDone, st.Issues ) @@ -617,7 +627,8 @@ void TWriteSessionImpl::ReadFromProcessor() { IProcessor::TPtr prc; ui64 generation; std::function callback; - with_lock(Lock) { + { + std::lock_guard guard(Lock); if (Aborting) { return; } @@ -638,7 +649,8 @@ void TWriteSessionImpl::ReadFromProcessor() { void TWriteSessionImpl::OnWriteDone(NYdbGrpc::TGrpcStatus&& status, size_t connectionGeneration) { THandleResult handleResult; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (connectionGeneration != ConnectionGeneration) { return; // Message from previous connection. Ignore. } @@ -660,7 +672,8 @@ void TWriteSessionImpl::OnReadDone(NYdbGrpc::TGrpcStatus&& grpcStatus, size_t co errorStatus = TPlainStatus(std::move(grpcStatus)); } bool doRead = false; - with_lock (Lock) { + { + std::lock_guard guard(Lock); UpdateTimedCountersImpl(); if (connectionGeneration != ConnectionGeneration) { return; // Message from previous connection. Ignore. @@ -680,7 +693,8 @@ void TWriteSessionImpl::OnReadDone(NYdbGrpc::TGrpcStatus&& grpcStatus, size_t co if (doRead) ReadFromProcessor(); - with_lock(Lock) { + { + std::lock_guard guard(Lock); if (!errorStatus.Ok()) { if (processResult.Ok) { // Otherwise, OnError was already called processResult.HandleResult = RestartImpl(errorStatus); @@ -700,12 +714,12 @@ void TWriteSessionImpl::OnReadDone(NYdbGrpc::TGrpcStatus&& grpcStatus, size_t co ProcessHandleResult(processResult.HandleResult); } -NUtils::TYdbStringBuilder TWriteSessionImpl::LogPrefix() const { - return NUtils::TYdbStringBuilder() << "MessageGroupId [" << Settings.MessageGroupId_ << "] SessionId [" << SessionId << "] "; +TStringBuilder TWriteSessionImpl::LogPrefix() const { + return TStringBuilder() << "MessageGroupId [" << Settings.MessageGroupId_ << "] SessionId [" << SessionId << "] "; } std::string TWriteSessionEvent::TAcksEvent::DebugString() const { - NUtils::TYdbStringBuilder res; + TStringBuilder res; res << "AcksEvent:"; for (auto& ack : Acks) { res << " { seqNo : " << ack.SeqNo << ", State : " << ack.State; @@ -936,9 +950,8 @@ void TWriteSessionImpl::CompressImpl(TBlock&& block_) { void TWriteSessionImpl::OnCompressed(TBlock&& block, bool isSyncCompression) { TMemoryUsageChange memoryUsage; if (!isSyncCompression) { - with_lock(Lock) { - memoryUsage = OnCompressedImpl(std::move(block)); - } + std::lock_guard guard(Lock); + memoryUsage = OnCompressedImpl(std::move(block)); } else { memoryUsage = OnCompressedImpl(std::move(block)); } @@ -1095,7 +1108,7 @@ bool TWriteSessionImpl::IsReadyToSendNextImpl() { } void TWriteSessionImpl::DumpState() { - NUtils::TYdbStringBuilder s; + TStringBuilder s; s << "STATE:\n"; auto omts = OriginalMessagesToSend; @@ -1246,7 +1259,8 @@ bool TWriteSessionImpl::Close(TDuration closeTimeout) { bool ready = false; bool needSetSeqNoValue = false; while (remaining > TDuration::Zero()) { - with_lock(Lock) { + { + std::lock_guard guard(Lock); if (OriginalMessagesToSend.empty() && SentOriginalMessages.empty()) { ready = true; } @@ -1259,10 +1273,12 @@ bool TWriteSessionImpl::Close(TDuration closeTimeout) { remaining = closeTimeout - (TInstant::Now() - startTime); Sleep(Min(TDuration::MilliSeconds(100), remaining)); } - with_lock(Lock) { + { + std::lock_guard guard(Lock); ready = (OriginalMessagesToSend.empty() && SentOriginalMessages.empty()) && !AtomicGet(Aborting); } - with_lock(Lock) { + { + std::lock_guard guard(Lock); CloseImpl(EStatus::SUCCESS, NYql::TIssues{}); needSetSeqNoValue = !InitSeqNoSetDone && (InitSeqNoSetDone = true); } @@ -1295,9 +1311,8 @@ void TWriteSessionImpl::HandleWakeUpImpl() { } if (auto self = cbContext->LockShared()) { - with_lock(self->Lock) { - self->HandleWakeUpImpl(); - } + std::lock_guard guard(self->Lock); + self->HandleWakeUpImpl(); } }; @@ -1395,7 +1410,8 @@ void TWriteSessionImpl::CloseImpl(TPlainStatus&& status) { TWriteSessionImpl::~TWriteSessionImpl() { LOG_LAZY(DbDriverState->Log, TLOG_DEBUG, LogPrefix() << "Write session: destroy"); bool needClose = false; - with_lock(Lock) { + { + std::lock_guard guard(Lock); if (!AtomicGet(Aborting)) { CloseImpl(EStatus::SUCCESS, NYql::TIssues{}); diff --git a/client/ydb_persqueue_core/impl/write_session_impl.h b/client/ydb_persqueue_core/impl/write_session_impl.h index bf9107183c5..4612fddffb9 100644 --- a/client/ydb_persqueue_core/impl/write_session_impl.h +++ b/client/ydb_persqueue_core/impl/write_session_impl.h @@ -334,7 +334,7 @@ class TWriteSessionImpl : public TContinuationTokenIssuer, private: - NUtils::TYdbStringBuilder LogPrefix() const; + TStringBuilder LogPrefix() const; void UpdateTokenIfNeededImpl(); diff --git a/client/ydb_persqueue_core/persqueue.h b/client/ydb_persqueue_core/persqueue.h index 83f4c56ea57..4ff40c3ced7 100644 --- a/client/ydb_persqueue_core/persqueue.h +++ b/client/ydb_persqueue_core/persqueue.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include @@ -547,7 +547,7 @@ struct TReadSessionEvent { virtual void Commit() = 0; std::string DebugString(bool printData = false) const; - virtual void DebugString(NUtils::TYdbStringBuilder& ret, bool printData = false) const = 0; + virtual void DebugString(TStringBuilder& ret, bool printData = false) const = 0; IMessage(const std::string& data, TPartitionStream::TPtr partitionStream, @@ -603,7 +603,7 @@ struct TReadSessionEvent { void Commit() override; using IMessage::DebugString; - void DebugString(NUtils::TYdbStringBuilder& ret, bool printData = false) const override; + void DebugString(TStringBuilder& ret, bool printData = false) const override; private: std::exception_ptr DecompressionException; @@ -653,7 +653,7 @@ struct TReadSessionEvent { void Commit() override; using IMessage::DebugString; - void DebugString(NUtils::TYdbStringBuilder& ret, bool printData = false) const override; + void DebugString(TStringBuilder& ret, bool printData = false) const override; private: ECodec Codec; @@ -966,11 +966,10 @@ class IExecutor : public TThrRefBase { // This method is idempotent. // It can be called many times. Only the first one has effect. void Start() { - with_lock (StartLock) { - if (!Started) { - DoStart(); - Started = true; - } + std::lock_guard guard(StartLock); + if (!Started) { + DoStart(); + Started = true; } } diff --git a/client/ydb_persqueue_core/ut/read_session_ut.cpp b/client/ydb_persqueue_core/ut/read_session_ut.cpp index 8e2add87ccd..0d681aa6b51 100644 --- a/client/ydb_persqueue_core/ut/read_session_ut.cpp +++ b/client/ydb_persqueue_core/ut/read_session_ut.cpp @@ -102,9 +102,8 @@ struct TMockProcessorFactory : public ISessionConnectionProcessorFactory> futuresQueue; - with_lock (Lock) { + { + std::lock_guard guard(Lock); CallbackFutures.swap(futuresQueue); } while (!futuresQueue.empty()) { @@ -347,13 +342,12 @@ struct TMockReadSessionProcessor : public TMockProcessorFactory> callbackFutures; - with_lock (Lock) { + { + std::lock_guard guard(Lock); CallbackFutures.swap(callbackFutures); } @@ -404,19 +399,19 @@ struct TMockReadSessionProcessor : public TMockProcessorFactory(GetDriver()); - } - return *PersQueueClient; + std::lock_guard guard(Lock); + if (!PersQueueClient) { + PersQueueClient = MakeHolder(GetDriver()); } + return *PersQueueClient; } NYdb::NPersQueue::TReadSessionSettings GetReadSessionSettings() { @@ -255,18 +254,16 @@ struct TYdbPqTestRetryPolicy : IRetryPolicy { AtomicAdd(CurrentRetries, 1); auto expected = AtomicGet(RetriesExpected); if (expected > 0 && AtomicGet(CurrentRetries) >= expected) { - with_lock(Lock) { - RetryPromise.SetValue(); - } + std::lock_guard guard(Lock); + RetryPromise.SetValue(); AtomicSet(RetriesExpected, 0); } } void StateDestroyed() const { auto expected = AtomicSwap(&RepairExpected, 0); if (expected) { - with_lock(Lock) { - RepairPromise.SetValue(); - } + std::lock_guard guard(Lock); + RepairPromise.SetValue(); } } void ExpectBreakDown() { @@ -287,9 +284,8 @@ struct TYdbPqTestRetryPolicy : IRetryPolicy { void WaitForRetries(ui64 retryCount, NThreading::TPromise& promise) { AtomicSet(RetriesExpected, retryCount); - with_lock(Lock) { - RetryPromise = promise; - } + std::lock_guard guard(Lock); + RetryPromise = promise; } void WaitForRetriesSync(ui64 retryCount) { NThreading::TPromise retriesPromise = NThreading::NewPromise(); @@ -300,9 +296,8 @@ struct TYdbPqTestRetryPolicy : IRetryPolicy { void WaitForRepair(NThreading::TPromise& promise) { AtomicSet(RepairExpected, 1 ); - with_lock(Lock) { - RepairPromise = promise; - } + std::lock_guard guard(Lock); + RepairPromise = promise; } void WaitForRepairSync() { diff --git a/client/ydb_persqueue_public/codecs/CMakeLists.txt b/client/ydb_persqueue_public/codecs/CMakeLists.txt index 2d6b809e512..25d69466044 100644 --- a/client/ydb_persqueue_public/codecs/CMakeLists.txt +++ b/client/ydb_persqueue_public/codecs/CMakeLists.txt @@ -9,8 +9,7 @@ target_link_libraries(client-ydb_persqueue_public-codecs PUBLIC api-grpc-draft api-grpc api-protos - cpp-string_utils-string_output -) + ) target_sources(client-ydb_persqueue_public-codecs PRIVATE ${CMAKE_SOURCE_DIR}/client/ydb_persqueue_public/codecs/codecs.cpp diff --git a/client/ydb_persqueue_public/codecs/codecs.cpp b/client/ydb_persqueue_public/codecs/codecs.cpp index 388b5b0dcd8..94b1005eaf6 100644 --- a/client/ydb_persqueue_public/codecs/codecs.cpp +++ b/client/ydb_persqueue_public/codecs/codecs.cpp @@ -28,7 +28,7 @@ IInputStream* CreateDecompressorStream(TInputStreamVariant& inputStreamStorage, std::string Decompress(const Ydb::PersQueue::V1::MigrationStreamingReadServerMessage::DataBatch::MessageData& data) { TMemoryInput input(data.data().data(), data.data().size()); std::string result; - NUtils::TStringOutput resultOutput(result); + TStringOutput resultOutput(result); TInputStreamVariant inputStreamStorage; TransferData(CreateDecompressorStream(inputStreamStorage, data.codec(), &input), &resultOutput); return result; diff --git a/client/ydb_result/CMakeLists.txt b/client/ydb_result/CMakeLists.txt index 5c59ca6c40b..5e07d317f42 100644 --- a/client/ydb_result/CMakeLists.txt +++ b/client/ydb_result/CMakeLists.txt @@ -9,9 +9,7 @@ target_link_libraries(cpp-client-ydb_result PUBLIC client-ydb_types-fatal_error_handlers cpp-client-ydb_value cpp-client-ydb_proto - cpp-string_utils-string_output - cpp-string_builder -) + ) target_sources(cpp-client-ydb_result PRIVATE ${CMAKE_SOURCE_DIR}/client/ydb_result/proto_accessor.cpp ${CMAKE_SOURCE_DIR}/client/ydb_result/result.cpp diff --git a/client/ydb_result/result.cpp b/client/ydb_result/result.cpp index 17178064c05..93ab1ad0556 100644 --- a/client/ydb_result/result.cpp +++ b/client/ydb_result/result.cpp @@ -5,8 +5,7 @@ #include #include -#include -#include +#include #include @@ -16,7 +15,7 @@ namespace NYdb { std::string TColumn::ToString() const { std::string result; - NUtils::TStringOutput out(result); + TStringOutput out(result); Out(out); return result; } @@ -122,7 +121,7 @@ class TResultSetParser::TImpl { auto& row = ResultSet_.GetProto().rows()[RowIndex_]; if (static_cast(row.items_size()) != ColumnsCount()) { - FatalError(NUtils::TYdbStringBuilder() << "Corrupted data: row " << RowIndex_ << " contains " << row.items_size() << " column(s), but metadata contains " << ColumnsCount() << " column(s)"); + FatalError(TStringBuilder() << "Corrupted data: row " << RowIndex_ << " contains " << row.items_size() << " column(s), but metadata contains " << ColumnsCount() << " column(s)"); } for (size_t i = 0; i < ColumnsCount(); ++i) { @@ -140,7 +139,7 @@ class TResultSetParser::TImpl { TValueParser& ColumnParser(size_t columnIndex) { if (columnIndex >= ColumnParsers.size()) { - FatalError(NUtils::TYdbStringBuilder() << "Column index out of bounds: " << columnIndex); + FatalError(TStringBuilder() << "Column index out of bounds: " << columnIndex); } return ColumnParsers[columnIndex]; @@ -149,7 +148,7 @@ class TResultSetParser::TImpl { TValueParser& ColumnParser(const std::string& columnName) { auto idx = MapFindPtr(ColumnIndexMap, columnName); if (!idx) { - FatalError(NUtils::TYdbStringBuilder() << "Unknown column: " << columnName); + FatalError(TStringBuilder() << "Unknown column: " << columnName); } return ColumnParser(*idx); @@ -157,11 +156,11 @@ class TResultSetParser::TImpl { TValue GetValue(size_t columnIndex) const { if (columnIndex >= ColumnParsers.size()) { - FatalError(NUtils::TYdbStringBuilder() << "Column index out of bounds: " << columnIndex); + FatalError(TStringBuilder() << "Column index out of bounds: " << columnIndex); } if (RowIndex_ == 0) { - FatalError(NUtils::TYdbStringBuilder() << "Row position is undefined"); + FatalError(TStringBuilder() << "Row position is undefined"); } const auto& row = ResultSet_.GetProto().rows()[RowIndex_ - 1]; @@ -173,7 +172,7 @@ class TResultSetParser::TImpl { TValue GetValue(const std::string& columnName) const { auto idx = MapFindPtr(ColumnIndexMap, columnName); if (!idx) { - FatalError(NUtils::TYdbStringBuilder() << "Unknown column: " << columnName); + FatalError(TStringBuilder() << "Unknown column: " << columnName); } return GetValue(*idx); @@ -181,7 +180,7 @@ class TResultSetParser::TImpl { private: void FatalError(const std::string& msg) const { - ThrowFatalError(NUtils::TYdbStringBuilder() << "TResultSetParser: " << msg); + ThrowFatalError(TStringBuilder() << "TResultSetParser: " << msg); } private: diff --git a/client/ydb_scheme/CMakeLists.txt b/client/ydb_scheme/CMakeLists.txt index ec69cf1da44..f026e1e6bfb 100644 --- a/client/ydb_scheme/CMakeLists.txt +++ b/client/ydb_scheme/CMakeLists.txt @@ -14,8 +14,7 @@ target_link_libraries(cpp-client-ydb_scheme PUBLIC impl-ydb_internal-make_request client-ydb_common_client-impl cpp-client-ydb_driver - cpp-string_utils-string_output -) + ) target_sources(cpp-client-ydb_scheme PRIVATE ${CMAKE_SOURCE_DIR}/client/ydb_scheme/scheme.cpp diff --git a/client/ydb_scheme/scheme.cpp b/client/ydb_scheme/scheme.cpp index 53621f6e23f..b6fe989cafd 100644 --- a/client/ydb_scheme/scheme.cpp +++ b/client/ydb_scheme/scheme.cpp @@ -9,7 +9,6 @@ #include #include -#include namespace NYdb { namespace NScheme { @@ -28,7 +27,7 @@ TVirtualTimestamp::TVirtualTimestamp(const ::Ydb::VirtualTimestamp& proto) std::string TVirtualTimestamp::ToString() const { std::string result; - NUtils::TStringOutput out(result); + TStringOutput out(result); Out(out); return result; } diff --git a/client/ydb_table/CMakeLists.txt b/client/ydb_table/CMakeLists.txt index b3204202c39..313850cf152 100644 --- a/client/ydb_table/CMakeLists.txt +++ b/client/ydb_table/CMakeLists.txt @@ -28,8 +28,7 @@ target_link_libraries(cpp-client-ydb_table PUBLIC client-ydb_types-operation cpp-client-ydb_value cpp-string_utils-misc - cpp-string_utils-string_output -) + ) target_sources(cpp-client-ydb_table PRIVATE ${CMAKE_SOURCE_DIR}/client/ydb_table/table.cpp diff --git a/client/ydb_table/impl/client_session.cpp b/client/ydb_table/impl/client_session.cpp index 66b3114ec50..4e5ccc42b90 100644 --- a/client/ydb_table/impl/client_session.cpp +++ b/client/ydb_table/impl/client_session.cpp @@ -17,11 +17,10 @@ void TSession::TImpl::InvalidateQueryInCache(const std::string& key) { return; } - with_lock(Lock_) { - auto it = QueryCache_.Find(key); - if (it != QueryCache_.End()) { - QueryCache_.Erase(it); - } + std::lock_guard guard(Lock_); + auto it = QueryCache_.Find(key); + if (it != QueryCache_.End()) { + QueryCache_.Erase(it); } } @@ -30,9 +29,8 @@ void TSession::TImpl::InvalidateQueryCache() { return; } - with_lock(Lock_) { - QueryCache_.Clear(); - } + std::lock_guard guard(Lock_); + QueryCache_.Clear(); } std::optional TSession::TImpl::GetQueryFromCache(const std::string& query, bool allowMigration) { @@ -42,11 +40,10 @@ std::optional TSession::TImpl::GetQueryFromCach auto key = EncodeQuery(query, allowMigration); - with_lock(Lock_) { - auto it = QueryCache_.Find(key); - if (it != QueryCache_.End()) { - return *it; - } + std::lock_guard guard(Lock_); + auto it = QueryCache_.Find(key); + if (it != QueryCache_.End()) { + return *it; } return std::nullopt; @@ -65,13 +62,12 @@ void TSession::TImpl::AddQueryToCache(const TDataQuery& query) { auto key = query.Impl_->GetTextHash(); TDataQueryInfo queryInfo(id, query.Impl_->GetParameterTypes()); - with_lock(Lock_) { - auto it = QueryCache_.Find(key); - if (it != QueryCache_.End()) { - *it = queryInfo; - } else { - QueryCache_.Insert(key, queryInfo); - } + std::lock_guard guard(Lock_); + auto it = QueryCache_.Find(key); + if (it != QueryCache_.End()) { + *it = queryInfo; + } else { + QueryCache_.Insert(key, queryInfo); } } diff --git a/client/ydb_table/impl/request_migrator.h b/client/ydb_table/impl/request_migrator.h index 1f75e18c831..26c6cedd8df 100644 --- a/client/ydb_table/impl/request_migrator.h +++ b/client/ydb_table/impl/request_migrator.h @@ -6,7 +6,7 @@ #include -#include +#include #include #include diff --git a/client/ydb_table/table.cpp b/client/ydb_table/table.cpp index 850688fd198..204c24d0245 100644 --- a/client/ydb_table/table.cpp +++ b/client/ydb_table/table.cpp @@ -24,13 +24,11 @@ #include #include -#include -#include #include #include -#include +#include namespace NYdb { namespace NTable { @@ -2295,7 +2293,7 @@ void TIndexDescription::SerializeTo(Ydb::Table::TableIndex& proto) const { std::string TIndexDescription::ToString() const { std::string result; - NUtils::TStringOutput out(result); + TStringOutput out(result); Out(out); return result; } @@ -2546,7 +2544,7 @@ void TChangefeedDescription::SerializeTo(Ydb::Table::Changefeed& proto) const { std::string TChangefeedDescription::ToString() const { std::string result; - NUtils::TStringOutput out(result); + TStringOutput out(result); Out(out); return result; } @@ -2648,7 +2646,7 @@ void TValueSinceUnixEpochModeSettings::Out(IOutputStream& out, EUnit unit) { std::string TValueSinceUnixEpochModeSettings::ToString(EUnit unit) { std::string result; - NUtils::TStringOutput out(result); + TStringOutput out(result); Out(out, unit); return result; } diff --git a/client/ydb_topic/CMakeLists.txt b/client/ydb_topic/CMakeLists.txt index 4162429ed3d..0dca5a0bfa5 100644 --- a/client/ydb_topic/CMakeLists.txt +++ b/client/ydb_topic/CMakeLists.txt @@ -23,7 +23,6 @@ target_link_libraries(cpp-client-ydb_topic PUBLIC api-grpc-draft api-protos cpp-client-ydb_table - cpp-string_builder ) target_sources(cpp-client-ydb_topic PRIVATE diff --git a/client/ydb_topic/codecs/codecs.cpp b/client/ydb_topic/codecs/codecs.cpp index ff6c0dda5d2..8d16511f1cf 100644 --- a/client/ydb_topic/codecs/codecs.cpp +++ b/client/ydb_topic/codecs/codecs.cpp @@ -28,7 +28,7 @@ IInputStream* CreateDecompressorStream(TInputStreamVariant& inputStreamStorage, std::string Decompress(const Ydb::Topic::StreamReadMessage::ReadResponse::MessageData& data, Ydb::Topic::Codec codec) { TMemoryInput input(data.data().data(), data.data().size()); std::string result; - NUtils::TStringOutput resultOutput(result); + TStringOutput resultOutput(result); TInputStreamVariant inputStreamStorage; TransferData(CreateDecompressorStream(inputStreamStorage, codec, &input), &resultOutput); return result; diff --git a/client/ydb_topic/impl/CMakeLists.txt b/client/ydb_topic/impl/CMakeLists.txt index 37ef6fc3196..87501a4f43c 100644 --- a/client/ydb_topic/impl/CMakeLists.txt +++ b/client/ydb_topic/impl/CMakeLists.txt @@ -15,7 +15,6 @@ target_link_libraries(client-ydb_topic-impl PUBLIC cpp-client-ydb_driver client-ydb_persqueue_core-impl cpp-client-ydb_proto - cpp-string_builder ) target_sources(client-ydb_topic-impl PRIVATE diff --git a/client/ydb_topic/impl/deferred_commit.cpp b/client/ydb_topic/impl/deferred_commit.cpp index fbb82c0e936..fa9dcd56288 100644 --- a/client/ydb_topic/impl/deferred_commit.cpp +++ b/client/ydb_topic/impl/deferred_commit.cpp @@ -77,7 +77,7 @@ void TDeferredCommit::TImpl::Add(const TReadSessionEvent::TDataReceivedEvent::TM void TDeferredCommit::TImpl::Add(const TPartitionSession::TPtr& partitionStream, TDisjointIntervalTree& offsetSet, ui64 startOffset, ui64 endOffset) { if (offsetSet.Intersects(startOffset, endOffset)) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Commit set already has some offsets from half-interval [" + ThrowFatalError(TStringBuilder() << "Commit set already has some offsets from half-interval [" << startOffset << "; " << endOffset << ") for partition stream with id " << partitionStream->GetPartitionSessionId()); } else { @@ -94,7 +94,7 @@ void TDeferredCommit::TImpl::Add(const TPartitionSession::TPtr& partitionStream, Y_ASSERT(partitionStream); auto& offsetSet = Offsets[partitionStream]; if (offsetSet.Has(offset)) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Commit set already has offset " << offset + ThrowFatalError(TStringBuilder() << "Commit set already has offset " << offset << " for partition stream with id " << partitionStream->GetPartitionSessionId()); } else { offsetSet.Insert(offset); diff --git a/client/ydb_topic/impl/event_handlers.cpp b/client/ydb_topic/impl/event_handlers.cpp index 2dd75aec601..c78fe7b1372 100644 --- a/client/ydb_topic/impl/event_handlers.cpp +++ b/client/ydb_topic/impl/event_handlers.cpp @@ -20,7 +20,8 @@ class TGracefulReleasingSimpleDataHandlers : public TThrRefBase { void OnDataReceived(TReadSessionEvent::TDataReceivedEvent& event) { Y_ASSERT(event.GetMessagesCount()); TDeferredCommit deferredCommit; - with_lock (Lock) { + { + std::lock_guard guard(Lock); auto& offsetSet = PartitionStreamToUncommittedOffsets[event.GetPartitionSession()->GetPartitionSessionId()]; // Messages could contain holes in offset, but later commit ack will tell us right border. // So we can easily insert the whole interval with holes included. @@ -39,49 +40,47 @@ class TGracefulReleasingSimpleDataHandlers : public TThrRefBase { } void OnCommitAcknowledgement(TReadSessionEvent::TCommitOffsetAcknowledgementEvent& event) { - with_lock (Lock) { - const ui64 partitionStreamId = event.GetPartitionSession()->GetPartitionSessionId(); - auto& offsetSet = PartitionStreamToUncommittedOffsets[partitionStreamId]; - if (offsetSet.EraseInterval(0, event.GetCommittedOffset() + 1)) { // Remove some offsets. - if (offsetSet.Empty()) { // No offsets left. - auto unconfirmedDestroyIt = UnconfirmedDestroys.find(partitionStreamId); - if (unconfirmedDestroyIt != UnconfirmedDestroys.end()) { - // Confirm and forget about this partition stream. - unconfirmedDestroyIt->second.Confirm(); - UnconfirmedDestroys.erase(unconfirmedDestroyIt); - PartitionStreamToUncommittedOffsets.erase(partitionStreamId); - } + std::lock_guard guard(Lock); + const ui64 partitionStreamId = event.GetPartitionSession()->GetPartitionSessionId(); + auto& offsetSet = PartitionStreamToUncommittedOffsets[partitionStreamId]; + if (offsetSet.EraseInterval(0, event.GetCommittedOffset() + 1)) { // Remove some offsets. + if (offsetSet.Empty()) { // No offsets left. + auto unconfirmedDestroyIt = UnconfirmedDestroys.find(partitionStreamId); + if (unconfirmedDestroyIt != UnconfirmedDestroys.end()) { + // Confirm and forget about this partition stream. + unconfirmedDestroyIt->second.Confirm(); + UnconfirmedDestroys.erase(unconfirmedDestroyIt); + PartitionStreamToUncommittedOffsets.erase(partitionStreamId); } } } } void OnCreatePartitionStream(TReadSessionEvent::TStartPartitionSessionEvent& event) { - with_lock (Lock) { + { + std::lock_guard guard(Lock); Y_ABORT_UNLESS(PartitionStreamToUncommittedOffsets[event.GetPartitionSession()->GetPartitionSessionId()].Empty()); } event.Confirm(); } void OnDestroyPartitionStream(TReadSessionEvent::TStopPartitionSessionEvent& event) { - with_lock (Lock) { - const ui64 partitionStreamId = event.GetPartitionSession()->GetPartitionSessionId(); - Y_ABORT_UNLESS(UnconfirmedDestroys.find(partitionStreamId) == UnconfirmedDestroys.end()); - if (PartitionStreamToUncommittedOffsets[partitionStreamId].Empty()) { - PartitionStreamToUncommittedOffsets.erase(partitionStreamId); - event.Confirm(); - } else { - UnconfirmedDestroys.emplace(partitionStreamId, std::move(event)); - } + std::lock_guard guard(Lock); + const ui64 partitionStreamId = event.GetPartitionSession()->GetPartitionSessionId(); + Y_ABORT_UNLESS(UnconfirmedDestroys.find(partitionStreamId) == UnconfirmedDestroys.end()); + if (PartitionStreamToUncommittedOffsets[partitionStreamId].Empty()) { + PartitionStreamToUncommittedOffsets.erase(partitionStreamId); + event.Confirm(); + } else { + UnconfirmedDestroys.emplace(partitionStreamId, std::move(event)); } } void OnPartitionStreamClosed(TReadSessionEvent::TPartitionSessionClosedEvent& event) { - with_lock (Lock) { - const ui64 partitionStreamId = event.GetPartitionSession()->GetPartitionSessionId(); - PartitionStreamToUncommittedOffsets.erase(partitionStreamId); - UnconfirmedDestroys.erase(partitionStreamId); - } + std::lock_guard guard(Lock); + const ui64 partitionStreamId = event.GetPartitionSession()->GetPartitionSessionId(); + PartitionStreamToUncommittedOffsets.erase(partitionStreamId); + UnconfirmedDestroys.erase(partitionStreamId); } private: diff --git a/client/ydb_topic/impl/read_session.cpp b/client/ydb_topic/impl/read_session.cpp index 7675b32efc2..0bdee4d40c5 100644 --- a/client/ydb_topic/impl/read_session.cpp +++ b/client/ydb_topic/impl/read_session.cpp @@ -55,7 +55,8 @@ void TReadSession::Start() { LOG_LAZY(Log, TLOG_INFO, GetLogPrefix() << "Starting read session"); NPersQueue::TDeferredActions deferred; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (Aborting) { return; } @@ -248,7 +249,8 @@ bool TReadSession::Close(TDuration timeout) { LOG_LAZY(Log, TLOG_INFO, GetLogPrefix() << "Closing read session. Close timeout: " << timeout); // Log final counters. CountersLogger->Stop(); - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (DumpCountersContext) { DumpCountersContext->Cancel(); } @@ -261,7 +263,8 @@ bool TReadSession::Close(TDuration timeout) { }; NPersQueue::TDeferredActions deferred; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (Closing || Aborting) { return false; } @@ -305,13 +308,12 @@ bool TReadSession::Close(TDuration timeout) { session->Abort(); NYql::TIssues issues; - issues.AddIssue(NUtils::TYdbStringBuilder() << "Session was closed after waiting " << timeout); + issues.AddIssue(TStringBuilder() << "Session was closed after waiting " << timeout); EventsQueue->Close(TSessionClosedEvent(EStatus::TIMEOUT, std::move(issues)), deferred); } - with_lock (Lock) { - Aborting = true; // Set abort flag for doing nothing on destructor. - } + std::lock_guard guard(Lock); + Aborting = true; // Set abort flag for doing nothing on destructor. return result; } @@ -319,8 +321,8 @@ void TReadSession::ClearAllEvents() { EventsQueue->ClearAllEvents(); } -NUtils::TYdbStringBuilder TReadSession::GetLogPrefix() const { - return NUtils::TYdbStringBuilder() << GetDatabaseLogPrefix(DbDriverState->Database) << "[" << SessionId << "] "; +TStringBuilder TReadSession::GetLogPrefix() const { + return TStringBuilder() << GetDatabaseLogPrefix(DbDriverState->Database) << "[" << SessionId << "] "; } void TReadSession::MakeCountersIfNeeded() { @@ -335,14 +337,13 @@ void TReadSession::MakeCountersIfNeeded() { } void TReadSession::SetupCountersLogger() { - with_lock(Lock) { - std::vector> sessions{CbContext}; + std::lock_guard guard(Lock); + std::vector> sessions{CbContext}; - CountersLogger = std::make_shared>(Connections, sessions, Settings.Counters_, Log, - GetLogPrefix(), StartSessionTime); - DumpCountersContext = CountersLogger->MakeCallbackContext(); - CountersLogger->Start(); - } + CountersLogger = std::make_shared>(Connections, sessions, Settings.Counters_, Log, + GetLogPrefix(), StartSessionTime); + DumpCountersContext = CountersLogger->MakeCallbackContext(); + CountersLogger->Start(); } void TReadSession::AbortImpl(NPersQueue::TDeferredActions&) { @@ -390,9 +391,8 @@ void TReadSession::Abort(EStatus statusCode, const std::string& message) { void TReadSession::Abort(TSessionClosedEvent&& closeEvent) { NPersQueue::TDeferredActions deferred; - with_lock (Lock) { - AbortImpl(std::move(closeEvent), deferred); - } + std::lock_guard guard(Lock); + AbortImpl(std::move(closeEvent), deferred); } } diff --git a/client/ydb_topic/impl/read_session.h b/client/ydb_topic/impl/read_session.h index c48a6023ef4..45aa4f7c182 100644 --- a/client/ydb_topic/impl/read_session.h +++ b/client/ydb_topic/impl/read_session.h @@ -43,7 +43,7 @@ class TReadSession : public IReadSession { void ClearAllEvents(); private: - NUtils::TYdbStringBuilder GetLogPrefix() const; + TStringBuilder GetLogPrefix() const; // Start bool ValidateSettings(); diff --git a/client/ydb_topic/impl/read_session_event.cpp b/client/ydb_topic/impl/read_session_event.cpp index 852103a4422..f4c37312dc9 100644 --- a/client/ydb_topic/impl/read_session_event.cpp +++ b/client/ydb_topic/impl/read_session_event.cpp @@ -66,7 +66,7 @@ const TPartitionSession::TPtr& TReadSessionEvent::TPartitionSessionAccessor::Get } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& res, bool) const { +void TPrintable::DebugString(TStringBuilder& res, bool) const { const auto* self = static_cast(this); res << " Partition session id: " << self->GetPartitionSessionId() << " Topic: \"" << self->GetTopicPath() << "\"" @@ -118,7 +118,7 @@ const TMessageMeta::TPtr& TMessageBase::GetMessageMeta() const { } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const { +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const { const auto* self = static_cast(this); try { const std::string& data = self->GetData(); @@ -182,7 +182,7 @@ void TMessage::Commit() { } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const { +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const { const auto* self = static_cast(this); ret << "Message {"; static_cast(self)->DebugString(ret, printData); @@ -216,7 +216,7 @@ void TCompressedMessage::Commit() { } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const { +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const { const auto* self = static_cast(this); ret << "CompressedMessage {"; static_cast(self)->DebugString(ret, printData); @@ -251,7 +251,7 @@ void TDataReceivedEvent::Commit() { } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const { +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const { const auto* self = static_cast(this); ret << "DataReceived {"; self->GetPartitionSession()->DebugString(ret); @@ -279,7 +279,7 @@ TCommitOffsetAcknowledgementEvent::TCommitOffsetAcknowledgementEvent(TPartitionS } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool) const { +void TPrintable::DebugString(TStringBuilder& ret, bool) const { const auto* self = static_cast(this); ret << "CommitAcknowledgement {"; self->GetPartitionSession()->DebugString(ret); @@ -305,7 +305,7 @@ void TStartPartitionSessionEvent::Confirm(std::optional readOffset, std::o } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool) const { +void TPrintable::DebugString(TStringBuilder& ret, bool) const { const auto* self = static_cast(this); ret << "StartPartitionSession {"; self->GetPartitionSession()->DebugString(ret); @@ -329,7 +329,7 @@ void TStopPartitionSessionEvent::Confirm() { } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool) const { +void TPrintable::DebugString(TStringBuilder& ret, bool) const { const auto* self = static_cast(this); ret << "StopPartitionSession {"; self->GetPartitionSession()->DebugString(ret); @@ -351,7 +351,7 @@ TPartitionSessionStatusEvent::TPartitionSessionStatusEvent(TPartitionSession::TP } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool) const { +void TPrintable::DebugString(TStringBuilder& ret, bool) const { const auto* self = static_cast(this); ret << "PartitionSessionStatus {"; self->GetPartitionSession()->DebugString(ret); @@ -372,7 +372,7 @@ TPartitionSessionClosedEvent::TPartitionSessionClosedEvent(TPartitionSession::TP } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool) const { +void TPrintable::DebugString(TStringBuilder& ret, bool) const { const auto* self = static_cast(this); ret << "PartitionSessionClosed {"; self->GetPartitionSession()->DebugString(ret); @@ -384,7 +384,7 @@ void TPrintable::DebugString(NUtils::TYdbStringBui // NTopic::TSessionClosedEvent template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool) const { +void TPrintable::DebugString(TStringBuilder& ret, bool) const { const auto* self = static_cast(this); ret << "SessionClosed { Status: " << self->GetStatus() << " Issues: \"" << NPersQueue::IssuesSingleLineString(self->GetIssues()) diff --git a/client/ydb_topic/impl/topic_impl.cpp b/client/ydb_topic/impl/topic_impl.cpp index cbbea0fd7fd..00e8cfe17b6 100644 --- a/client/ydb_topic/impl/topic_impl.cpp +++ b/client/ydb_topic/impl/topic_impl.cpp @@ -12,13 +12,12 @@ std::shared_ptr TTopicClient::TImpl::CreateReadSession(const TRead std::optional maybeSettings; if (!settings.DecompressionExecutor_ || !settings.EventHandlers_.HandlersExecutor_) { maybeSettings = settings; - with_lock (Lock) { - if (!settings.DecompressionExecutor_) { - maybeSettings->DecompressionExecutor(Settings.DefaultCompressionExecutor_); - } - if (!settings.EventHandlers_.HandlersExecutor_) { - maybeSettings->EventHandlers_.HandlersExecutor(Settings.DefaultHandlersExecutor_); - } + std::lock_guard guard(Lock); + if (!settings.DecompressionExecutor_) { + maybeSettings->DecompressionExecutor(Settings.DefaultCompressionExecutor_); + } + if (!settings.EventHandlers_.HandlersExecutor_) { + maybeSettings->EventHandlers_.HandlersExecutor(Settings.DefaultHandlersExecutor_); } } auto session = std::make_shared(maybeSettings.value_or(settings), shared_from_this(), Connections_, DbDriverState_); @@ -32,13 +31,12 @@ std::shared_ptr TTopicClient::TImpl::CreateWriteSession( std::optional maybeSettings; if (!settings.CompressionExecutor_ || !settings.EventHandlers_.HandlersExecutor_) { maybeSettings = settings; - with_lock (Lock) { - if (!settings.CompressionExecutor_) { - maybeSettings->CompressionExecutor(Settings.DefaultCompressionExecutor_); - } - if (!settings.EventHandlers_.HandlersExecutor_) { - maybeSettings->EventHandlers_.HandlersExecutor(Settings.DefaultHandlersExecutor_); - } + std::lock_guard guard(Lock); + if (!settings.CompressionExecutor_) { + maybeSettings->CompressionExecutor(Settings.DefaultCompressionExecutor_); + } + if (!settings.EventHandlers_.HandlersExecutor_) { + maybeSettings->EventHandlers_.HandlersExecutor(Settings.DefaultHandlersExecutor_); } } auto session = std::make_shared( @@ -52,7 +50,8 @@ std::shared_ptr TTopicClient::TImpl::CreateSimpleWr const TWriteSessionSettings& settings ) { auto alteredSettings = settings; - with_lock (Lock) { + { + std::lock_guard guard(Lock); alteredSettings.EventHandlers_.HandlersExecutor(Settings.DefaultHandlersExecutor_); if (!settings.CompressionExecutor_) { alteredSettings.CompressionExecutor(Settings.DefaultCompressionExecutor_); diff --git a/client/ydb_topic/impl/write_session_impl.cpp b/client/ydb_topic/impl/write_session_impl.cpp index a7ee75de83b..81c8d5c0ef1 100644 --- a/client/ydb_topic/impl/write_session_impl.cpp +++ b/client/ydb_topic/impl/write_session_impl.cpp @@ -97,23 +97,20 @@ void TWriteSessionImpl::Start(const TDuration& delay) { ++ConnectionAttemptsDone; if (!Started) { - with_lock(Lock) { + { + std::lock_guard guard(Lock); HandleWakeUpImpl(); } InitWriter(); } Started = true; - if (Settings.PartitionId_.has_value() && Settings.DirectWriteToPartition_) - { - with_lock (Lock) { - PreferredPartitionLocation = {}; + if (Settings.PartitionId_.has_value() && Settings.DirectWriteToPartition_) { + std::lock_guard guard(Lock); + PreferredPartitionLocation = {}; - return ConnectToPreferredPartitionLocation(delay); - } - } - else - { + return ConnectToPreferredPartitionLocation(delay); + } else { return Connect(delay); } } @@ -210,7 +207,8 @@ void TWriteSessionImpl::OnDescribePartition(const TStatus& status, const Ydb::To std::string endpoint, name; THandleResult handleResult; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (DescribePartitionContext == describePartitionContext) { DescribePartitionContext = nullptr; } else { @@ -219,7 +217,8 @@ void TWriteSessionImpl::OnDescribePartition(const TStatus& status, const Ydb::To } if (!status.IsSuccess()) { - with_lock (Lock) { + { + std::lock_guard guard(Lock); handleResult = OnErrorImpl({status.GetStatus(), NPersQueue::MakeIssueWithSubIssues("Failed to get partition location", status.GetIssues())}); } ProcessHandleResult(handleResult); @@ -228,7 +227,8 @@ void TWriteSessionImpl::OnDescribePartition(const TStatus& status, const Ydb::To const Ydb::Topic::DescribeTopicResult_PartitionInfo& partition = proto.partition(); if (partition.partition_id() != Settings.PartitionId_ || !partition.has_partition_location() || partition.partition_location().node_id() == 0 || partition.partition_location().generation() == 0) { - with_lock (Lock) { + { + std::lock_guard guard(Lock); handleResult = OnErrorImpl({EStatus::INTERNAL_ERROR, "Wrong partition location"}); } ProcessHandleResult(handleResult); @@ -236,19 +236,22 @@ void TWriteSessionImpl::OnDescribePartition(const TStatus& status, const Ydb::To } std::optional preferredEndpoint; - with_lock (Lock) { + { + std::lock_guard guard(Lock); preferredEndpoint = GetPreferredEndpointImpl(*Settings.PartitionId_, partition.partition_location().node_id()); } if (!preferredEndpoint.has_value()) { - with_lock (Lock) { + { + std::lock_guard guard(Lock); handleResult = OnErrorImpl({EStatus::UNAVAILABLE, "Partition preferred endpoint is not found"}); } ProcessHandleResult(handleResult); return; } - with_lock (Lock) { + { + std::lock_guard guard(Lock); PreferredPartitionLocation = {*preferredEndpoint, partition.partition_location().generation()}; } @@ -404,7 +407,8 @@ void TWriteSessionImpl::WriteInternal(TContinuationToken&&, TWriteMessage&& mess TInstant createdAtValue = message.CreateTimestamp_.value_or(TInstant::Now()); bool readyToAccept = false; size_t bufferSize = message.Data.size(); - with_lock(Lock) { + { + std::lock_guard guard(Lock); CurrentBatch.Add( GetNextIdImpl(message.SeqNo_), createdAtValue, message.Data, message.Codec, message.OriginalSize, message.MessageMeta_, @@ -456,7 +460,8 @@ void TWriteSessionImpl::Connect(const TDuration& delay) { std::function connectCallback; std::function connectTimeoutCallback; - with_lock(Lock) { + { + std::lock_guard guard(Lock); if (Aborting) { return; } @@ -532,7 +537,8 @@ void TWriteSessionImpl::Connect(const TDuration& delay) { void TWriteSessionImpl::OnConnectTimeout(const NYdbGrpc::IQueueClientContextPtr& connectTimeoutContext) { LOG_LAZY(DbDriverState->Log, TLOG_ERR, LogPrefix() << "Write session: connect timeout"); THandleResult handleResult; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (ConnectTimeoutContext == connectTimeoutContext) { NPersQueue::Cancel(ConnectContext); ConnectContext = nullptr; @@ -541,7 +547,7 @@ void TWriteSessionImpl::OnConnectTimeout(const NYdbGrpc::IQueueClientContextPtr& } else { return; } - NUtils::TYdbStringBuilder description; + TStringBuilder description; description << "Failed to establish connection to server. Attempts done: " << ConnectionAttemptsDone; handleResult = RestartImpl(TPlainStatus(EStatus::TIMEOUT, description)); if (handleResult.DoStop) { @@ -559,7 +565,8 @@ void TWriteSessionImpl::OnConnect( TPlainStatus&& st, typename IProcessor::TPtr&& processor, const NYdbGrpc::IQueueClientContextPtr& connectContext ) { THandleResult handleResult; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (ConnectContext == connectContext) { NPersQueue::Cancel(ConnectTimeoutContext); ConnectContext = nullptr; @@ -580,7 +587,7 @@ void TWriteSessionImpl::OnConnect( CloseImpl( st.Status, NPersQueue::MakeIssueWithSubIssues( - NUtils::TYdbStringBuilder() << "Failed to establish connection to server \"" << st.Endpoint + TStringBuilder() << "Failed to establish connection to server \"" << st.Endpoint << "\". Attempts done: " << ConnectionAttemptsDone, st.Issues ) @@ -650,7 +657,8 @@ void TWriteSessionImpl::ReadFromProcessor() { IProcessor::TPtr prc; ui64 generation; std::function callback; - with_lock(Lock) { + { + std::lock_guard guard(Lock); if (Aborting) { return; } @@ -673,7 +681,8 @@ void TWriteSessionImpl::OnWriteDone(NYdbGrpc::TGrpcStatus&& status, size_t conne LOG_LAZY(DbDriverState->Log, TLOG_DEBUG, LogPrefix() << "Write session: OnWriteDone " << status.ToDebugString()); THandleResult handleResult; - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (connectionGeneration != ConnectionGeneration) { return; // Message from previous connection. Ignore. } @@ -697,7 +706,8 @@ void TWriteSessionImpl::OnReadDone(NYdbGrpc::TGrpcStatus&& grpcStatus, size_t co errorStatus = TPlainStatus(std::move(grpcStatus)); } bool doRead = false; - with_lock (Lock) { + { + std::lock_guard guard(Lock); UpdateTimedCountersImpl(); if (connectionGeneration != ConnectionGeneration) { return; // Message from previous connection. Ignore. @@ -717,7 +727,8 @@ void TWriteSessionImpl::OnReadDone(NYdbGrpc::TGrpcStatus&& grpcStatus, size_t co if (doRead) ReadFromProcessor(); - with_lock(Lock) { + { + std::lock_guard guard(Lock); if (!errorStatus.Ok()) { if (processResult.Ok) { // Otherwise, OnError was already called processResult.HandleResult = RestartImpl(errorStatus); @@ -737,8 +748,8 @@ void TWriteSessionImpl::OnReadDone(NYdbGrpc::TGrpcStatus&& grpcStatus, size_t co ProcessHandleResult(processResult.HandleResult); } -NUtils::TYdbStringBuilder TWriteSessionImpl::LogPrefix() const { - NUtils::TYdbStringBuilder ret; +TStringBuilder TWriteSessionImpl::LogPrefix() const { + TStringBuilder ret; ret << " SessionId [" << SessionId << "] "; if (Settings.PartitionId_.has_value()) { @@ -753,7 +764,7 @@ NUtils::TYdbStringBuilder TWriteSessionImpl::LogPrefix() const { } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& res, bool) const { +void TPrintable::DebugString(TStringBuilder& res, bool) const { const auto* self = static_cast(this); res << "AcksEvent:"; for (auto& ack : self->Acks) { @@ -774,7 +785,7 @@ void TPrintable::DebugString(NUtils::TYdbStringB } template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& res, bool) const { +void TPrintable::DebugString(TStringBuilder& res, bool) const { res << "ReadyToAcceptEvent"; } @@ -997,9 +1008,8 @@ void TWriteSessionImpl::CompressImpl(TBlock&& block_) { void TWriteSessionImpl::OnCompressed(TBlock&& block, bool isSyncCompression) { TMemoryUsageChange memoryUsage; if (!isSyncCompression) { - with_lock(Lock) { - memoryUsage = OnCompressedImpl(std::move(block)); - } + std::lock_guard guard(Lock); + memoryUsage = OnCompressedImpl(std::move(block)); } else { memoryUsage = OnCompressedImpl(std::move(block)); } @@ -1254,7 +1264,8 @@ bool TWriteSessionImpl::Close(TDuration closeTimeout) { bool ready = false; bool needSetSeqNoValue = false; while (remaining > TDuration::Zero()) { - with_lock(Lock) { + { + std::lock_guard guard(Lock); if (OriginalMessagesToSend.empty() && SentOriginalMessages.empty()) { ready = true; } @@ -1267,10 +1278,12 @@ bool TWriteSessionImpl::Close(TDuration closeTimeout) { remaining = closeTimeout - (TInstant::Now() - startTime); Sleep(Min(TDuration::MilliSeconds(100), remaining)); } - with_lock(Lock) { + { + std::lock_guard guard(Lock); ready = (OriginalMessagesToSend.empty() && SentOriginalMessages.empty()) && !AtomicGet(Aborting); } - with_lock(Lock) { + { + std::lock_guard guard(Lock); CloseImpl(EStatus::SUCCESS, NYql::TIssues{}); needSetSeqNoValue = !InitSeqNoSetDone && (InitSeqNoSetDone = true); } @@ -1298,9 +1311,8 @@ void TWriteSessionImpl::HandleWakeUpImpl() { return; } if (auto self = cbContext->LockShared()) { - with_lock(self->Lock) { - self->HandleWakeUpImpl(); - } + std::lock_guard guard(self->Lock); + self->HandleWakeUpImpl(); } }; if (TInstant::Now() - LastTokenUpdate > UPDATE_TOKEN_PERIOD) { @@ -1398,7 +1410,8 @@ void TWriteSessionImpl::CloseImpl(TPlainStatus&& status) { TWriteSessionImpl::~TWriteSessionImpl() { LOG_LAZY(DbDriverState->Log, TLOG_DEBUG, LogPrefix() << "Write session: destroy"); bool needClose = false; - with_lock(Lock) { + { + std::lock_guard guard(Lock); if (!AtomicGet(Aborting)) { CloseImpl(EStatus::SUCCESS, NYql::TIssues{}); diff --git a/client/ydb_topic/impl/write_session_impl.h b/client/ydb_topic/impl/write_session_impl.h index 8cc230e01cd..3799da75311 100644 --- a/client/ydb_topic/impl/write_session_impl.h +++ b/client/ydb_topic/impl/write_session_impl.h @@ -367,7 +367,7 @@ class TWriteSessionImpl : public TContinuationTokenIssuer, private: - NUtils::TYdbStringBuilder LogPrefix() const; + TStringBuilder LogPrefix() const; void UpdateTokenIfNeededImpl(); diff --git a/client/ydb_topic/topic.h b/client/ydb_topic/topic.h index d61503fb049..603a7bcbe27 100644 --- a/client/ydb_topic/topic.h +++ b/client/ydb_topic/topic.h @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include @@ -614,13 +614,13 @@ template class TPrintable { public: std::string DebugString(bool printData = false) const { - NUtils::TYdbStringBuilder b; + TStringBuilder b; static_cast(this)->DebugString(b, printData); return b; } // implemented in template specializations - void DebugString(NUtils::TYdbStringBuilder& ret, bool printData = false) const = delete; + void DebugString(TStringBuilder& ret, bool printData = false) const = delete; }; //! Session metainformation. @@ -644,7 +644,7 @@ struct TSessionClosedEvent: public TStatus, public TPrintable -void TPrintable::DebugString(NUtils::TYdbStringBuilder& res, bool) const; +void TPrintable::DebugString(TStringBuilder& res, bool) const; struct TWriteStat : public TThrRefBase { TDuration WriteTime; @@ -785,7 +785,7 @@ struct TPartitionSession: public TThrRefBase, public TPrintable -void TPrintable::DebugString(NUtils::TYdbStringBuilder& res, bool) const; +void TPrintable::DebugString(TStringBuilder& res, bool) const; //! Events for read session. struct TReadSessionEvent { @@ -1125,25 +1125,25 @@ class TDeferredCommit { //! Events debug strings. template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const; +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const; template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const; +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const; template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const; +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const; template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const; +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const; template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const; +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const; template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const; +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const; template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const; +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const; template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const; +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const; template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const; +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const; template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const; +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const; std::string DebugString(const TReadSessionEvent::TEvent& event); @@ -1194,11 +1194,10 @@ class IExecutor: public TThrRefBase { // This method is idempotent. // It can be called many times. Only the first one has effect. void Start() { - with_lock(StartLock) { - if (!Started) { - DoStart(); - Started = true; - } + std::lock_guard guard(StartLock); + if (!Started) { + DoStart(); + Started = true; } } @@ -1269,9 +1268,9 @@ struct TWriteSessionEvent { //! Events debug strings. template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const; +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const; template<> -void TPrintable::DebugString(NUtils::TYdbStringBuilder& ret, bool printData) const; +void TPrintable::DebugString(TStringBuilder& ret, bool printData) const; std::string DebugString(const TWriteSessionEvent::TEvent& event); diff --git a/client/ydb_types/credentials/login/login.cpp b/client/ydb_types/credentials/login/login.cpp index 2c964fd95c7..4f2565d60a6 100644 --- a/client/ydb_types/credentials/login/login.cpp +++ b/client/ydb_types/credentials/login/login.cpp @@ -175,7 +175,7 @@ std::string TLoginCredentialsProvider::GetError() const { return Ydb::StatusIds_StatusCode_Name(Response_.operation().status()); } } else { - NUtils::TYdbStringBuilder str; + TStringBuilder str; str << "Couldn't get token for provided credentials from " << Status_.Endpoint << " with status " << Status_.Status << "."; for (const auto& issue : Status_.Issues) { diff --git a/client/ydb_types/fatal_error_handlers/handlers.h b/client/ydb_types/fatal_error_handlers/handlers.h index 88266afdcda..bd2a89c1995 100644 --- a/client/ydb_types/fatal_error_handlers/handlers.h +++ b/client/ydb_types/fatal_error_handlers/handlers.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/client/ydb_types/operation/CMakeLists.txt b/client/ydb_types/operation/CMakeLists.txt index e34dfa61542..743de04c626 100644 --- a/client/ydb_types/operation/CMakeLists.txt +++ b/client/ydb_types/operation/CMakeLists.txt @@ -7,8 +7,7 @@ target_link_libraries(client-ydb_types-operation PUBLIC cpp-threading-future public-lib-operation_id cpp-client-ydb_types - cpp-string_utils-string_output -) + ) target_sources(client-ydb_types-operation PRIVATE ${CMAKE_SOURCE_DIR}/client/ydb_types/operation/operation.cpp diff --git a/client/ydb_types/operation/operation.cpp b/client/ydb_types/operation/operation.cpp index dbb9cda55dc..80d69c27219 100644 --- a/client/ydb_types/operation/operation.cpp +++ b/client/ydb_types/operation/operation.cpp @@ -1,6 +1,5 @@ #include "operation.h" -#include #include #include @@ -68,7 +67,7 @@ const TStatus& TOperation::Status() const { std::string TOperation::ToString() const { std::string result; - NUtils::TStringOutput out(result); + TStringOutput out(result); Out(out); return result; } diff --git a/client/ydb_value/CMakeLists.txt b/client/ydb_value/CMakeLists.txt index 8ddcda60111..708a1d2e4e3 100644 --- a/client/ydb_value/CMakeLists.txt +++ b/client/ydb_value/CMakeLists.txt @@ -19,7 +19,6 @@ target_link_libraries(cpp-client-ydb_value PUBLIC client-ydb_types-fatal_error_handlers yql-public-decimal ydb-library-uuid - cpp-string_builder ) target_sources(cpp-client-ydb_value PRIVATE diff --git a/client/ydb_value/value.cpp b/client/ydb_value/value.cpp index af52fba4ece..b30af9b478f 100644 --- a/client/ydb_value/value.cpp +++ b/client/ydb_value/value.cpp @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include @@ -24,7 +24,7 @@ namespace NYdb { static void CheckKind(TTypeParser::ETypeKind actual, TTypeParser::ETypeKind expected, const std::string& method) { if (expected != actual) { - ThrowFatalError(NUtils::TYdbStringBuilder() << method << "(): invalid state, expected type: " + ThrowFatalError(TStringBuilder() << method << "(): invalid state, expected type: " << expected << ", actual: " << actual); } } @@ -65,7 +65,7 @@ static TTypeParser::ETypeKind GetKind(const Ydb::Type& type) { break; } - ThrowFatalError(NUtils::TYdbStringBuilder() << "Unexpected proto type kind: " << (ui32) type.type_case()); + ThrowFatalError(TStringBuilder() << "Unexpected proto type kind: " << (ui32) type.type_case()); return ETypeKind::Void; } @@ -296,7 +296,7 @@ class TTypeParser::TImpl { break; } default: { - FatalError(NUtils::TYdbStringBuilder() << "Unexpected variant type kind: " << variantType); + FatalError(TStringBuilder() << "Unexpected variant type kind: " << variantType); break; } } @@ -311,7 +311,7 @@ class TTypeParser::TImpl { } default: - FatalError(NUtils::TYdbStringBuilder() << "Unexpected type kind: " << GetKind()); + FatalError(TStringBuilder() << "Unexpected type kind: " << GetKind()); break; } @@ -342,7 +342,7 @@ class TTypeParser::TImpl { } void FatalError(const std::string& msg) const { - ThrowFatalError(NUtils::TYdbStringBuilder() << "TTypeParser: " << msg); + ThrowFatalError(TStringBuilder() << "TTypeParser: " << msg); } private: @@ -468,6 +468,7 @@ void TTypeParser::CloseTagged() { //////////////////////////////////////////////////////////////////////////////// void FormatTypeInternal(TTypeParser& parser, IOutputStream& out) { + using namespace std::literals; switch (parser.GetKind()) { case TTypeParser::ETypeKind::Primitive: out << parser.GetPrimitive(); @@ -567,7 +568,7 @@ void FormatTypeInternal(TTypeParser& parser, IOutputStream& out) { break; default: - ThrowFatalError(NUtils::TYdbStringBuilder() + ThrowFatalError(TStringBuilder() << "Unexpected type kind: " << parser.GetKind()); } } @@ -768,7 +769,7 @@ class TTypeBuilder::TImpl { } void FatalError(const std::string& msg) const { - ThrowFatalError(NUtils::TYdbStringBuilder() << "TTypeBuilder: " << msg); + ThrowFatalError(TStringBuilder() << "TTypeBuilder: " << msg); } void CheckKind(ETypeKind kind, const std::string& method) { @@ -1019,7 +1020,7 @@ TUuidValue::TUuidValue(const Ydb::Value& valueProto) { TUuidValue::TUuidValue(const std::string& uuidString) { ui16 dw[8]; if (!NKikimr::NUuid::ParseUuidToArray(uuidString, dw, false)) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Unable to parse string as uuid"); + ThrowFatalError(TStringBuilder() << "Unable to parse string as uuid"); } static_assert(sizeof(dw) == sizeof(Buf_.Bytes)); // TODO: check output on big-endian machines here and everywhere. @@ -1301,7 +1302,7 @@ class TValueParser::TImpl { return true; } - FatalError(NUtils::TYdbStringBuilder() << "Missing struct 'items' value at index: " << GetPathBack().Idx); + FatalError(TStringBuilder() << "Missing struct 'items' value at index: " << GetPathBack().Idx); } return false; @@ -1327,7 +1328,7 @@ class TValueParser::TImpl { return true; } - FatalError(NUtils::TYdbStringBuilder() << "Missing tuple 'items' value at index: " << GetPathBack().Idx); + FatalError(TStringBuilder() << "Missing tuple 'items' value at index: " << GetPathBack().Idx); } return false; @@ -1378,7 +1379,7 @@ class TValueParser::TImpl { if (GetProto().value_case() == Ydb::Value::kNestedValue) { AddPath(EParseKind::Value, &GetProto().nested_value()); } else { - FatalError(NUtils::TYdbStringBuilder() << "No nested value for variant type."); + FatalError(TStringBuilder() << "No nested value for variant type."); } } @@ -1402,7 +1403,7 @@ class TValueParser::TImpl { private: const TProtoPosition& GetPathBack() const { if (Path_.empty()) { - FatalError(NUtils::TYdbStringBuilder() << "Bad parser state, no open value."); + FatalError(TStringBuilder() << "Bad parser state, no open value."); } return Path_.back(); @@ -1410,7 +1411,7 @@ class TValueParser::TImpl { void PopPath() { if (Path_.empty()) { - FatalError(NUtils::TYdbStringBuilder() << "Bad parser state, no open value."); + FatalError(TStringBuilder() << "Bad parser state, no open value."); } Path_.pop_back(); @@ -1419,7 +1420,7 @@ class TValueParser::TImpl { void AddPath(EParseKind kind, const google::protobuf::Message* message, i32 idx = -1) { if (!Path_.empty()) { if (Path_.back().Kind == EParseKind::Null) { - FatalError(NUtils::TYdbStringBuilder() << "Can't parse inside NULL value"); + FatalError(TStringBuilder() << "Can't parse inside NULL value"); return; } @@ -1429,13 +1430,13 @@ class TValueParser::TImpl { case EParseKind::Pair: if (kind != EParseKind::Value) { - FatalError(NUtils::TYdbStringBuilder() << "Bad parser state, expected dict pair."); + FatalError(TStringBuilder() << "Bad parser state, expected dict pair."); return; } break; default: - FatalError(NUtils::TYdbStringBuilder() << "Bad parser state, no value to parse."); + FatalError(TStringBuilder() << "Bad parser state, no value to parse."); return; } } @@ -1513,7 +1514,7 @@ class TValueParser::TImpl { void CheckTransportKind(Ydb::Value::ValueCase expectedCase) const { if (expectedCase != GetProto().value_case()) { - FatalError(NUtils::TYdbStringBuilder() << "Transport value case mismatch, requested: " << (ui32)expectedCase + FatalError(TStringBuilder() << "Transport value case mismatch, requested: " << (ui32)expectedCase << ", actual: " << (ui32)GetProto().value_case()); } } @@ -1522,7 +1523,7 @@ class TValueParser::TImpl { CheckKind(ETypeKind::Primitive, "Get"); if (primitiveType != TypeParser_.GetPrimitive()) { - FatalError(NUtils::TYdbStringBuilder() << "Type mismatch, requested: " << primitiveType + FatalError(TStringBuilder() << "Type mismatch, requested: " << primitiveType << ", actual: " << TypeParser_.GetPrimitive()); } @@ -1544,7 +1545,7 @@ class TValueParser::TImpl { const Ydb::ValuePair& GetProtoPair() const { if (GetPathBack().Kind != EParseKind::Pair) { - FatalError(NUtils::TYdbStringBuilder() << "Bad parser state, expected dict pair"); + FatalError(TStringBuilder() << "Bad parser state, expected dict pair"); } return *static_cast(GetPathBack().Ptr); @@ -1598,13 +1599,13 @@ class TValueParser::TImpl { case NYdb::EPrimitiveType::Uuid: return Ydb::Value::kLow128; default: - FatalError(NUtils::TYdbStringBuilder() << "Unexpected primitive type: " << primitiveTypeId); + FatalError(TStringBuilder() << "Unexpected primitive type: " << primitiveTypeId); return Ydb::Value::kBytesValue; } } void FatalError(const std::string& msg) const { - ThrowFatalError(NUtils::TYdbStringBuilder() << "TValueParser: " << msg); + ThrowFatalError(TStringBuilder() << "TValueParser: " << msg); } private: @@ -2174,7 +2175,7 @@ class TValueBuilderImpl { TypeBuilder_.EndOptional(); if (!PathTop().OptLevel) { - FatalError(NUtils::TYdbStringBuilder() << "No opened optional"); + FatalError(TStringBuilder() << "No opened optional"); } --PathTop().OptLevel; @@ -2207,7 +2208,7 @@ class TValueBuilderImpl { void EmptyOptional() { BeginOptional(); if (!CheckType()) { - FatalError(NUtils::TYdbStringBuilder() << "EmptyOptional: unknown item type"); + FatalError(TStringBuilder() << "EmptyOptional: unknown item type"); return; } NestEmptyOptional(); @@ -2272,7 +2273,7 @@ class TValueBuilderImpl { void EmptyList() { BeginList(); if (!CheckType()) { - FatalError(NUtils::TYdbStringBuilder() << "EmptyList: unknown item type"); + FatalError(TStringBuilder() << "EmptyList: unknown item type"); return; } EndList(); @@ -2305,13 +2306,13 @@ class TValueBuilderImpl { } else { auto membersMap = StructsPathTop().MembersMap; if (!membersMap) { - FatalError(NUtils::TYdbStringBuilder() << "Missing struct members info."); + FatalError(TStringBuilder() << "Missing struct members info."); return; } auto memberIndex = MapFindPtr(*membersMap, memberName); if (!memberIndex) { - FatalError(NUtils::TYdbStringBuilder() << "Struct member not found: " << memberName); + FatalError(TStringBuilder() << "Struct member not found: " << memberName); return; } @@ -2358,7 +2359,7 @@ class TValueBuilderImpl { auto it = std::find_if(membersMap.begin(), membersMap.end(), [index](const auto& pair) { return pair.second == index; }); - FatalError(NUtils::TYdbStringBuilder() << "No value given for struct member: " << it->first); + FatalError(TStringBuilder() << "No value given for struct member: " << it->first); } PopStructsPath(); @@ -2382,7 +2383,7 @@ class TValueBuilderImpl { auto index = GetValue().items_size(); auto tuple_size = GetType(1).tuple_type().elements_size(); if (index >= tuple_size) { - FatalError(NUtils::TYdbStringBuilder() << "Tuple elements count mismatch, expected: " + FatalError(TStringBuilder() << "Tuple elements count mismatch, expected: " << tuple_size << ", actual: " << index + 1); return; } @@ -2412,7 +2413,7 @@ class TValueBuilderImpl { auto expectedElements = GetType().tuple_type().elements_size(); auto actualIElements = GetValue().items_size(); if (expectedElements != actualIElements) { - FatalError(NUtils::TYdbStringBuilder() << "Tuple elements count mismatch, expected: " << expectedElements + FatalError(TStringBuilder() << "Tuple elements count mismatch, expected: " << expectedElements << ", actual: " << actualIElements); } } @@ -2502,13 +2503,13 @@ class TValueBuilderImpl { TypeBuilder_.DictKey(); if (!CheckType()) { - FatalError(NUtils::TYdbStringBuilder() << "EmptyDict: unknown key type"); + FatalError(TStringBuilder() << "EmptyDict: unknown key type"); return; } TypeBuilder_.DictPayload(); if (!CheckType()) { - FatalError(NUtils::TYdbStringBuilder() << "EmptyDict: unknown payload type"); + FatalError(TStringBuilder() << "EmptyDict: unknown payload type"); return; } @@ -2586,7 +2587,7 @@ class TValueBuilderImpl { auto expectedKind = GetKind(GetType()); if (expectedKind != kind) { - FatalError(NUtils::TYdbStringBuilder() << "Type mismatch, expected: " << expectedKind + FatalError(TStringBuilder() << "Type mismatch, expected: " << expectedKind << ", actual: " << kind); return false; } @@ -2600,7 +2601,7 @@ class TValueBuilderImpl { } if (!TypesEqual(GetType(), type.GetProto())) { - FatalError(NUtils::TYdbStringBuilder() << "Type mismatch, expected: " << FormatType(GetType()) + FatalError(TStringBuilder() << "Type mismatch, expected: " << FormatType(GetType()) << ", actual: " << FormatType(type)); return false; } @@ -2615,7 +2616,7 @@ class TValueBuilderImpl { auto expectedType = EPrimitiveType(GetType().type_id()); if (expectedType != type) { - FatalError(NUtils::TYdbStringBuilder() << "Primitive type mismatch, expected: " << expectedType + FatalError(TStringBuilder() << "Primitive type mismatch, expected: " << expectedType << ", actual: " << type); return false; } @@ -2633,12 +2634,12 @@ class TValueBuilderImpl { void CheckContainerKind(ETypeKind kind) { if (Path_.size() < 2) { - FatalError(NUtils::TYdbStringBuilder() << "No opened container"); + FatalError(TStringBuilder() << "No opened container"); } auto actualKind = GetKind(GetType(1)); if (actualKind != kind) { - FatalError(NUtils::TYdbStringBuilder() << "Container type mismatch, expected: " << kind + FatalError(TStringBuilder() << "Container type mismatch, expected: " << kind << ", actual: " << actualKind); } } @@ -2695,7 +2696,7 @@ class TValueBuilderImpl { } void FatalError(const std::string& msg) const { - ThrowFatalError(NUtils::TYdbStringBuilder() << "TValueBuilder: " << msg); + ThrowFatalError(TStringBuilder() << "TValueBuilder: " << msg); } private: diff --git a/contrib/libs/lzmasdk/LzmaEnc.c b/contrib/libs/lzmasdk/LzmaEnc.c index a5a087f556f..6a8a2989347 100644 --- a/contrib/libs/lzmasdk/LzmaEnc.c +++ b/contrib/libs/lzmasdk/LzmaEnc.c @@ -2981,4 +2981,4 @@ BoolInt LzmaEnc_IsFinished(CLzmaEncHandle pp) CLzmaEnc *p = (CLzmaEnc *)pp; return p->finished; } - \ No newline at end of file + diff --git a/contrib/libs/snappy/snappy.cc b/contrib/libs/snappy/snappy.cc index 075e0d7dbff..3467c26d2b7 100644 --- a/contrib/libs/snappy/snappy.cc +++ b/contrib/libs/snappy/snappy.cc @@ -83,7 +83,7 @@ #include #include #include -#include +#include namespace snappy { @@ -2053,20 +2053,6 @@ bool Uncompress(const char* compressed, size_t compressed_length, string_as_array(uncompressed)); } -bool Uncompress(const char* compressed, size_t n, TString* uncompressed) { - size_t ulength; - if (!GetUncompressedLength(compressed, n, &ulength)) { - return false; - } - // On 32-bit builds: max_size() < kuint32max. Check for that instead - // of crashing (e.g., consider externally specified compressed data). - if (ulength > uncompressed->max_size()) { - return false; - } - uncompressed->ReserveAndResize(ulength); - return RawUncompress(compressed, n, uncompressed->begin()); -} - // A Writer that drops everything on the floor and just does validation class SnappyDecompressionValidator { private: @@ -2172,18 +2158,6 @@ size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt, return compressed_length; } -size_t Compress(const char* input, size_t input_length, - TString* compressed) { - // Pre-grow the buffer to the max length of the compressed output - compressed->ReserveAndResize(MaxCompressedLength(input_length)); - - size_t compressed_length; - RawCompress(input, input_length, compressed->begin(), - &compressed_length); - compressed->resize(compressed_length); - return compressed_length; -} - // ----------------------------------------------------------------------- // Sink interface // ----------------------------------------------------------------------- diff --git a/contrib/libs/snappy/snappy.h b/contrib/libs/snappy/snappy.h index 3fe79b0b58a..c7412fb26f2 100644 --- a/contrib/libs/snappy/snappy.h +++ b/contrib/libs/snappy/snappy.h @@ -78,8 +78,6 @@ namespace snappy { // REQUIRES: "input[]" is not an alias of "*compressed". size_t Compress(const char* input, size_t input_length, std::string* compressed); - size_t Compress(const char* input, size_t input_length, - TString* compressed); // Same as `Compress` above but taking an `iovec` array as input. Note that // this function preprocesses the inputs to compute the sum of @@ -96,8 +94,6 @@ namespace snappy { // returns false if the message is corrupted and could not be decompressed bool Uncompress(const char* compressed, size_t compressed_length, std::string* uncompressed); - bool Uncompress(const char* compressed, size_t compressed_length, - TString* uncompressed); // Decompresses "compressed" to "*uncompressed". // diff --git a/examples/secondary_index/secondary_index.h b/examples/secondary_index/secondary_index.h index ce1a59481ed..5def34344ec 100644 --- a/examples/secondary_index/secondary_index.h +++ b/examples/secondary_index/secondary_index.h @@ -5,7 +5,7 @@ #include -#include +#include #include #include #include diff --git a/examples/secondary_index/secondary_index_generate.cpp b/examples/secondary_index/secondary_index_generate.cpp index a3631018575..64173bb9ce9 100644 --- a/examples/secondary_index/secondary_index_generate.cpp +++ b/examples/secondary_index/secondary_index_generate.cpp @@ -26,7 +26,8 @@ class TExecutor { } void Stop(bool rethrow = true) { - with_lock (Lock) { + { + std::lock_guard guard(Lock); Stopped = true; } Wait(rethrow); @@ -35,16 +36,16 @@ class TExecutor { void Wait(bool rethrow = true) { Queue.Stop(); if (rethrow) { - with_lock (Lock) { - if (Exception) { - std::rethrow_exception(Exception); - } + std::lock_guard guard(Lock); + if (Exception) { + std::rethrow_exception(Exception); } } } bool Execute(std::function callback) { - with_lock (Lock) { + { + std::lock_guard guard(Lock); if (Stopped) { if (Exception) { std::rethrow_exception(Exception); @@ -75,7 +76,8 @@ class TExecutor { void Process(void*) override { THolder self(this); - with_lock (Owner->Lock) { + { + std::lock_guard guard(Owner->Lock); if (Owner->Stopped) { return; } @@ -84,11 +86,10 @@ class TExecutor { auto callback = std::move(Callback); callback(); } catch (...) { - with_lock (Owner->Lock) { - if (!Owner->Stopped && !Owner->Exception) { - Owner->Stopped = true; - Owner->Exception = std::current_exception(); - } + std::lock_guard guard(Owner->Lock); + if (!Owner->Stopped && !Owner->Exception) { + Owner->Stopped = true; + Owner->Exception = std::current_exception(); } } } diff --git a/examples/secondary_index_builtin/secondary_index.h b/examples/secondary_index_builtin/secondary_index.h index 3ebb24c0bd7..aa77d9b85ca 100644 --- a/examples/secondary_index_builtin/secondary_index.h +++ b/examples/secondary_index_builtin/secondary_index.h @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include #include diff --git a/examples/topic_reader/transaction/options.h b/examples/topic_reader/transaction/options.h index b40330e2dcd..2a9f01d6402 100644 --- a/examples/topic_reader/transaction/options.h +++ b/examples/topic_reader/transaction/options.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include struct TOptions { diff --git a/library/cpp/CMakeLists.txt b/library/cpp/CMakeLists.txt index 1a19c66b622..2e8819f9576 100644 --- a/library/cpp/CMakeLists.txt +++ b/library/cpp/CMakeLists.txt @@ -26,7 +26,6 @@ add_subdirectory(resource) add_subdirectory(retry) add_subdirectory(sanitizer) add_subdirectory(streams) -add_subdirectory(string_builder) add_subdirectory(string_utils) add_subdirectory(svnversion) add_subdirectory(terminate_handler) diff --git a/library/cpp/blockcodecs/codecs/brotli/CMakeLists.txt b/library/cpp/blockcodecs/codecs/brotli/CMakeLists.txt index 523c0b1f930..f13f79ea390 100644 --- a/library/cpp/blockcodecs/codecs/brotli/CMakeLists.txt +++ b/library/cpp/blockcodecs/codecs/brotli/CMakeLists.txt @@ -6,7 +6,6 @@ target_link_libraries(blockcodecs-codecs-brotli INTERFACE libs-brotli-enc libs-brotli-dec cpp-blockcodecs-core - cpp-string_builder ) add_global_library_for(blockcodecs-codecs-brotli.global blockcodecs-codecs-brotli) @@ -17,7 +16,6 @@ target_link_libraries(blockcodecs-codecs-brotli.global PUBLIC libs-brotli-enc libs-brotli-dec cpp-blockcodecs-core - cpp-string_builder ) target_sources(blockcodecs-codecs-brotli.global PRIVATE diff --git a/library/cpp/blockcodecs/codecs/brotli/brotli.cpp b/library/cpp/blockcodecs/codecs/brotli/brotli.cpp index 42ef48f0f1f..285ec070ffd 100644 --- a/library/cpp/blockcodecs/codecs/brotli/brotli.cpp +++ b/library/cpp/blockcodecs/codecs/brotli/brotli.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -17,7 +17,7 @@ namespace { inline TBrotliCodec(ui32 level) : Quality(level) - , MyName(NUtils::TYdbStringBuilder() << std::string_view("brotli_") << std::to_string(level)) + , MyName(TStringBuilder() << std::string_view("brotli_") << std::to_string(level)) { } diff --git a/library/cpp/blockcodecs/codecs/legacy_zstd06/CMakeLists.txt b/library/cpp/blockcodecs/codecs/legacy_zstd06/CMakeLists.txt index de9bd58c498..ba0e88088f1 100644 --- a/library/cpp/blockcodecs/codecs/legacy_zstd06/CMakeLists.txt +++ b/library/cpp/blockcodecs/codecs/legacy_zstd06/CMakeLists.txt @@ -5,7 +5,6 @@ target_link_libraries(blockcodecs-codecs-legacy_zstd06 INTERFACE yutil contrib-libs-zstd06 cpp-blockcodecs-core - cpp-string_builder ) add_global_library_for(blockcodecs-codecs-legacy_zstd06.global blockcodecs-codecs-legacy_zstd06) @@ -15,7 +14,6 @@ target_link_libraries(blockcodecs-codecs-legacy_zstd06.global PUBLIC yutil contrib-libs-zstd06 cpp-blockcodecs-core - cpp-string_builder ) target_sources(blockcodecs-codecs-legacy_zstd06.global PRIVATE diff --git a/library/cpp/blockcodecs/codecs/legacy_zstd06/legacy_zstd06.cpp b/library/cpp/blockcodecs/codecs/legacy_zstd06/legacy_zstd06.cpp index 2eb676c2e7e..7fdc8e75bef 100644 --- a/library/cpp/blockcodecs/codecs/legacy_zstd06/legacy_zstd06.cpp +++ b/library/cpp/blockcodecs/codecs/legacy_zstd06/legacy_zstd06.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -13,7 +13,7 @@ namespace { struct TZStd06Codec: public TAddLengthCodec { inline TZStd06Codec(unsigned level) : Level(level) - , MyName(NUtils::TYdbStringBuilder() << std::string_view("zstd06_") << std::to_string(Level)) + , MyName(TStringBuilder() << std::string_view("zstd06_") << std::to_string(Level)) { } diff --git a/library/cpp/blockcodecs/codecs/lz4/CMakeLists.txt b/library/cpp/blockcodecs/codecs/lz4/CMakeLists.txt index 16d27d84674..66538a034b6 100644 --- a/library/cpp/blockcodecs/codecs/lz4/CMakeLists.txt +++ b/library/cpp/blockcodecs/codecs/lz4/CMakeLists.txt @@ -5,7 +5,6 @@ target_link_libraries(blockcodecs-codecs-lz4 INTERFACE yutil contrib-libs-lz4 cpp-blockcodecs-core - cpp-string_builder ) add_global_library_for(blockcodecs-codecs-lz4.global blockcodecs-codecs-lz4) @@ -15,7 +14,6 @@ target_link_libraries(blockcodecs-codecs-lz4.global PUBLIC yutil contrib-libs-lz4 cpp-blockcodecs-core - cpp-string_builder ) target_sources(blockcodecs-codecs-lz4.global PRIVATE diff --git a/library/cpp/blockcodecs/codecs/lz4/lz4.cpp b/library/cpp/blockcodecs/codecs/lz4/lz4.cpp index 4789c12e356..080b00cc1c0 100644 --- a/library/cpp/blockcodecs/codecs/lz4/lz4.cpp +++ b/library/cpp/blockcodecs/codecs/lz4/lz4.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -72,14 +72,14 @@ namespace { template struct TLz4Codec: public TAddLengthCodec>, public TLz4Base, public TC, public TD { inline TLz4Codec() - : MyName(NUtils::TYdbStringBuilder() << "lz4-" << TC::CPrefix() << "-" << TD::DPrefix()) + : MyName(TStringBuilder() << "lz4-" << TC::CPrefix() << "-" << TD::DPrefix()) { } template inline TLz4Codec(const T& t) : TC(t) - , MyName(NUtils::TYdbStringBuilder() << "lz4-" << TC::CPrefix() << "-" << TD::DPrefix()) + , MyName(TStringBuilder() << "lz4-" << TC::CPrefix() << "-" << TD::DPrefix()) { } diff --git a/library/cpp/blockcodecs/codecs/zstd/CMakeLists.txt b/library/cpp/blockcodecs/codecs/zstd/CMakeLists.txt index bebbd871295..b98b8b9ca0c 100644 --- a/library/cpp/blockcodecs/codecs/zstd/CMakeLists.txt +++ b/library/cpp/blockcodecs/codecs/zstd/CMakeLists.txt @@ -5,7 +5,6 @@ target_link_libraries(blockcodecs-codecs-zstd INTERFACE yutil contrib-libs-zstd cpp-blockcodecs-core - cpp-string_builder ) add_global_library_for(blockcodecs-codecs-zstd.global blockcodecs-codecs-zstd) @@ -15,7 +14,6 @@ target_link_libraries(blockcodecs-codecs-zstd.global PUBLIC yutil contrib-libs-zstd cpp-blockcodecs-core - cpp-string_builder ) target_sources(blockcodecs-codecs-zstd.global PRIVATE diff --git a/library/cpp/blockcodecs/codecs/zstd/zstd.cpp b/library/cpp/blockcodecs/codecs/zstd/zstd.cpp index c25921941d4..ad3db845fc3 100644 --- a/library/cpp/blockcodecs/codecs/zstd/zstd.cpp +++ b/library/cpp/blockcodecs/codecs/zstd/zstd.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #define ZSTD_STATIC_LINKING_ONLY #include @@ -13,7 +13,7 @@ namespace { struct TZStd08Codec: public TAddLengthCodec { inline TZStd08Codec(unsigned level) : Level(level) - , MyName(NUtils::TYdbStringBuilder() << std::string_view("zstd08_") << std::to_string(Level)) + , MyName(TStringBuilder() << std::string_view("zstd08_") << std::to_string(Level)) { } diff --git a/library/cpp/blockcodecs/core/codecs.h b/library/cpp/blockcodecs/core/codecs.h index 17e4d1efa2d..5bf36f7eb10 100644 --- a/library/cpp/blockcodecs/core/codecs.h +++ b/library/cpp/blockcodecs/core/codecs.h @@ -1,8 +1,8 @@ #pragma once #include -#include -#include +#include +#include #include #include diff --git a/library/cpp/build_info/sandbox.cpp.in b/library/cpp/build_info/sandbox.cpp.in index bb8f5a6dbb9..44b115881e1 100644 --- a/library/cpp/build_info/sandbox.cpp.in +++ b/library/cpp/build_info/sandbox.cpp.in @@ -1,7 +1,8 @@ #include #include -#include +#include #include +#include extern "C" const char* GetSandboxTaskId() { return "@SANDBOX_TASK_ID@"; diff --git a/library/cpp/cache/thread_safe_cache.h b/library/cpp/cache/thread_safe_cache.h index 71e14427173..fc7165b5298 100644 --- a/library/cpp/cache/thread_safe_cache.h +++ b/library/cpp/cache/thread_safe_cache.h @@ -3,6 +3,7 @@ #include "cache.h" #include +#include #include namespace NPrivate { diff --git a/library/cpp/case_insensitive_string/case_insensitive_string.cpp b/library/cpp/case_insensitive_string/case_insensitive_string.cpp index 16c0f5ff7a2..1b7e719e5b8 100644 --- a/library/cpp/case_insensitive_string/case_insensitive_string.cpp +++ b/library/cpp/case_insensitive_string/case_insensitive_string.cpp @@ -10,13 +10,3 @@ size_t THash::operator()(TCaseInsensitiveStringBuf st } return hash.Value(); } - -template <> -void Out(IOutputStream& o, const TCaseInsensitiveString& p) { - o.Write(p.data(), p.size()); -} - -template <> -void Out(IOutputStream& o, const TCaseInsensitiveStringBuf& p) { - o.Write(p.data(), p.size()); -} diff --git a/library/cpp/case_insensitive_string/case_insensitive_string.h b/library/cpp/case_insensitive_string/case_insensitive_string.h index 443de3e5f94..c0dd6ad867f 100644 --- a/library/cpp/case_insensitive_string/case_insensitive_string.h +++ b/library/cpp/case_insensitive_string/case_insensitive_string.h @@ -2,13 +2,13 @@ #include "case_insensitive_char_traits.h" -#include -#include +#include +#include #include #include -using TCaseInsensitiveString = TBasicString; -using TCaseInsensitiveStringBuf = TBasicStringBuf; +using TCaseInsensitiveString = std::basic_string; +using TCaseInsensitiveStringBuf = std::basic_string_view; template <> struct THash { diff --git a/library/cpp/cgiparam/cgiparam.h b/library/cpp/cgiparam/cgiparam.h index f148daa3da9..b3cc9258975 100644 --- a/library/cpp/cgiparam/cgiparam.h +++ b/library/cpp/cgiparam/cgiparam.h @@ -3,8 +3,8 @@ #include #include -#include -#include +#include +#include #include #include diff --git a/library/cpp/charset/ci_string.cpp b/library/cpp/charset/ci_string.cpp index c2a9d103067..6008c98ccbc 100644 --- a/library/cpp/charset/ci_string.cpp +++ b/library/cpp/charset/ci_string.cpp @@ -1,31 +1,31 @@ #include "ci_string.h" int TCiString::compare(const TCiString& s1, const TCiString& s2, const CodePage& cp) { - return cp.stricmp(s1.data(), s2.data()); + return cp.Stricmp(s1.data(), s2.data()); } int TCiString::compare(const char* p, const TCiString& s2, const CodePage& cp) { - return cp.stricmp(p, s2.data()); + return cp.Stricmp(p, s2.data()); } int TCiString::compare(const TCiString& s1, const char* p, const CodePage& cp) { - return cp.stricmp(s1.data(), p); + return cp.Stricmp(s1.data(), p); } int TCiString::compare(const std::string_view& p1, const std::string_view& p2, const CodePage& cp) { - int rv = cp.strnicmp(p1.data(), p2.data(), Min(p1.size(), p2.size())); + int rv = cp.Strnicmp(p1.data(), p2.data(), Min(p1.size(), p2.size())); return rv ? rv : p1.size() < p2.size() ? -1 : p1.size() == p2.size() ? 0 : 1; } bool TCiString::is_prefix(const std::string_view& what, const std::string_view& of, const CodePage& cp) { size_t len = what.size(); - return len <= of.size() && cp.strnicmp(what.data(), of.data(), len) == 0; + return len <= of.size() && cp.Strnicmp(what.data(), of.data(), len) == 0; } bool TCiString::is_suffix(const std::string_view& what, const std::string_view& of, const CodePage& cp) { size_t len = what.size(); size_t slen = of.size(); - return (len <= slen) && (0 == cp.strnicmp(what.data(), of.data() + slen - len, len)); + return (len <= slen) && (0 == cp.Strnicmp(what.data(), of.data() + slen - len, len)); } size_t TCiString::hashVal(const char* s, size_t len, const CodePage& cp) { diff --git a/library/cpp/charset/ci_string.h b/library/cpp/charset/ci_string.h index a9a6dd85a56..93a8399e234 100644 --- a/library/cpp/charset/ci_string.h +++ b/library/cpp/charset/ci_string.h @@ -2,7 +2,7 @@ #include "codepage.h" -#include +#include #include // Same as std::string but uses CASE INSENSITIVE comparator and hash. Use with care. @@ -256,17 +256,17 @@ struct TCIHash { struct ci_less { inline bool operator()(const char* x, const char* y) const { - return csYandex.stricmp(x, y) < 0; + return csYandex.Stricmp(x, y) < 0; } }; struct ci_equal_to { inline bool operator()(const char* x, const char* y) const { - return csYandex.stricmp(x, y) == 0; + return csYandex.Stricmp(x, y) == 0; } // this implementation is not suitable for strings with zero characters inside, sorry bool operator()(const std::string_view& x, const std::string_view& y) const { - return x.size() == y.size() && csYandex.strnicmp(x.data(), y.data(), y.size()) == 0; + return x.size() == y.size() && csYandex.Strnicmp(x.data(), y.data(), y.size()) == 0; } }; diff --git a/library/cpp/charset/codepage.cpp b/library/cpp/charset/codepage.cpp index f13c68da9dd..3098e0aec6a 100644 --- a/library/cpp/charset/codepage.cpp +++ b/library/cpp/charset/codepage.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include @@ -61,7 +61,7 @@ char* CodePage::ToUpper(const char* b, char* to) const { return Apply(b, to, TCodePageData::rcdr_to_upper[CPEnum]); } -int CodePage::stricmp(const char* dst, const char* src) const { +int CodePage::Stricmp(const char* dst, const char* src) const { unsigned char f, l; do { f = ToLower(*dst++); @@ -70,7 +70,7 @@ int CodePage::stricmp(const char* dst, const char* src) const { return f - l; } -int CodePage::strnicmp(const char* dst, const char* src, size_t len) const { +int CodePage::Strnicmp(const char* dst, const char* src, size_t len) const { unsigned char f, l; if (len) { do { diff --git a/library/cpp/charset/codepage.h b/library/cpp/charset/codepage.h index 91fe9443f32..8d66d6c7d3a 100644 --- a/library/cpp/charset/codepage.h +++ b/library/cpp/charset/codepage.h @@ -7,7 +7,7 @@ #include #include // all wchar32 functions #include -#include +#include #include #include #include @@ -71,8 +71,8 @@ struct CodePage { char* ToUpper(const char* begin, const char* end, char* to) const; char* ToUpper(const char* begin, char* to) const; - int stricmp(const char* s1, const char* s2) const; - int strnicmp(const char* s1, const char* s2, size_t len) const; + int Stricmp(const char* s1, const char* s2) const; + int Strnicmp(const char* s1, const char* s2, size_t len) const; inline unsigned char ToUpper(unsigned char ch) const; inline unsigned char ToLower(unsigned char ch) const; diff --git a/library/cpp/charset/recyr_int.hh b/library/cpp/charset/recyr_int.hh index 353af53305e..14ac04e1226 100644 --- a/library/cpp/charset/recyr_int.hh +++ b/library/cpp/charset/recyr_int.hh @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include "codepage.h" diff --git a/library/cpp/charset/wide_ut.cpp b/library/cpp/charset/wide_ut.cpp index 979e350f69e..954e9020a82 100644 --- a/library/cpp/charset/wide_ut.cpp +++ b/library/cpp/charset/wide_ut.cpp @@ -244,7 +244,7 @@ void TConversionTest::TestRecodeIntoString() { TUtf16String sUnicode; sUnicode.reserve(YandexText.size() * 4); const wchar16* wdata = sUnicode.data(); - TWtringBuf wres = NDetail::Recode(YandexText, sUnicode, CODES_YANDEX); + std::u16string_view wres = NDetail::Recode(YandexText, sUnicode, CODES_YANDEX); UNIT_ASSERT(sUnicode == UnicodeText); // same content UNIT_ASSERT(sUnicode.data() == wdata); // reserved buffer reused UNIT_ASSERT(sUnicode.data() == wres.data()); // same buffer @@ -402,13 +402,13 @@ void TConversionTest::TestUnicodeLimit() { } static void TestCanEncodeEmpty() { - TWtringBuf empty; + std::u16string_view empty; UNIT_ASSERT(CanBeEncoded(empty, CODES_WIN)); UNIT_ASSERT(CanBeEncoded(empty, CODES_YANDEX)); UNIT_ASSERT(CanBeEncoded(empty, CODES_UTF8)); } -static void TestCanEncodeEach(const TWtringBuf& text, ECharset encoding, bool expectedResult) { +static void TestCanEncodeEach(const std::u16string_view& text, ECharset encoding, bool expectedResult) { // char by char for (size_t i = 0; i < text.size(); ++i) { if (CanBeEncoded(text.SubStr(i, 1), encoding) != expectedResult) diff --git a/library/cpp/colorizer/colors.cpp b/library/cpp/colorizer/colors.cpp index 9fcfef7b90a..98686b22b69 100644 --- a/library/cpp/colorizer/colors.cpp +++ b/library/cpp/colorizer/colors.cpp @@ -152,7 +152,7 @@ void Out(IOutputStream& os, TTypeTraits -#include +#include + +#include +#include #include diff --git a/library/cpp/containers/paged_vector/paged_vector.h b/library/cpp/containers/paged_vector/paged_vector.h index e92dc7a7953..7613fb76ea1 100644 --- a/library/cpp/containers/paged_vector/paged_vector.h +++ b/library/cpp/containers/paged_vector/paged_vector.h @@ -1,10 +1,11 @@ #pragma once +#include #include -#include #include #include +#include namespace NPagedVector { template > @@ -146,12 +147,15 @@ namespace std { namespace NPagedVector { //2-level radix tree + template + using TPagedVectorBase = std::vector>; + template - class TPagedVector: TVector>, A> { + class TPagedVector: private TPagedVectorBase>, A> { static_assert(PageSize, "expect PageSize"); - typedef TVector TPage; - typedef TVector, A> TPages; + typedef TPagedVectorBase TPage; + typedef TPagedVectorBase, A> TPages; typedef TPagedVector TSelf; public: @@ -236,7 +240,7 @@ namespace NPagedVector { } void AllocateNewPage() { - TPages::push_back(new TPage()); + TPages::push_back(MakeSimpleShared()); CurrentPage().reserve(PageSize); } @@ -355,11 +359,11 @@ namespace NPagedVector { } reference front() { - return TPages::front()->front(); + return *((*TPages::begin())->begin()); } const_reference front() const { - return TPages::front()->front(); + return *((*TPages::cbegin())->cbegin()); } reference back() { diff --git a/library/cpp/coroutine/engine/helper.h b/library/cpp/coroutine/engine/helper.h index 70f3dd35eb0..cf2c3e0fefd 100644 --- a/library/cpp/coroutine/engine/helper.h +++ b/library/cpp/coroutine/engine/helper.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include namespace NCoro { diff --git a/library/cpp/coroutine/engine/sockpool.h b/library/cpp/coroutine/engine/sockpool.h index 1ebb7e7b382..67c588f25b3 100644 --- a/library/cpp/coroutine/engine/sockpool.h +++ b/library/cpp/coroutine/engine/sockpool.h @@ -5,7 +5,8 @@ #include #include -#include + +#include extern void SetCommonSockOpts(SOCKET sock, const struct sockaddr* sa = nullptr); @@ -158,7 +159,7 @@ class TSocketPool { TSockets toDelete; { - TGuard guard(Mutex_); + std::lock_guard guard(Mutex_); for (TSockets::TIterator it = Pool_.Begin(); it != Pool_.End();) { if (it->LastTouch() < maxAge) { @@ -195,7 +196,7 @@ class TSocketPool { private: TPooledSocket::TImpl* GetImpl() { - TGuard guard(Mutex_); + std::lock_guard guard(Mutex_); while (!Pool_.Empty()) { THolder ret(Pool_.PopFront()); @@ -208,7 +209,7 @@ class TSocketPool { } void Release(TPooledSocket::TImpl* impl) noexcept { - TGuard guard(Mutex_); + std::lock_guard guard(Mutex_); Pool_.PushFront(impl); } @@ -219,7 +220,7 @@ class TSocketPool { TAddrRef Addr_; using TSockets = TIntrusiveListWithAutoDelete; TSockets Pool_; - TMutex Mutex_; + std::mutex Mutex_; }; inline void TPooledSocket::TImpl::ReturnToPool() noexcept { diff --git a/library/cpp/coroutine/engine/stack/stack_guards.h b/library/cpp/coroutine/engine/stack/stack_guards.h index 49a174c8e0e..496d417ce92 100644 --- a/library/cpp/coroutine/engine/stack/stack_guards.h +++ b/library/cpp/coroutine/engine/stack/stack_guards.h @@ -3,7 +3,7 @@ #include "stack_common.h" #include -#include +#include #include diff --git a/library/cpp/cppparser/parser.h b/library/cpp/cppparser/parser.h index e6345f88647..9c90dc1b5fe 100644 --- a/library/cpp/cppparser/parser.h +++ b/library/cpp/cppparser/parser.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include class TCppSaxParser: public IOutputStream { diff --git a/library/cpp/dbg_output/CMakeLists.txt b/library/cpp/dbg_output/CMakeLists.txt index ac793636005..fd61af67fd3 100644 --- a/library/cpp/dbg_output/CMakeLists.txt +++ b/library/cpp/dbg_output/CMakeLists.txt @@ -5,7 +5,6 @@ target_link_libraries(library-cpp-dbg_output PUBLIC yutil library-cpp-colorizer cpp-string_utils-misc - cpp-string_utils-escape ) target_sources(library-cpp-dbg_output PRIVATE diff --git a/library/cpp/dbg_output/dumpers.h b/library/cpp/dbg_output/dumpers.h index f7a04299b44..4e4c935be45 100644 --- a/library/cpp/dbg_output/dumpers.h +++ b/library/cpp/dbg_output/dumpers.h @@ -3,8 +3,8 @@ #include "engine.h" #include -#include -#include +#include +#include #include @@ -154,10 +154,6 @@ template <> struct TDumper: public TStrDumper { }; -template <> -struct TDumper: public TStrDumper { -}; - template <> struct TDumper: public TStrDumper { }; @@ -167,5 +163,5 @@ struct TDumper>: public TStrDumper { }; template -struct TDumper>: public TStrDumper { +struct TDumper>: public TStrDumper { }; diff --git a/library/cpp/dbg_output/engine.cpp b/library/cpp/dbg_output/engine.cpp index e1ea704cbe8..55de5ec04d8 100644 --- a/library/cpp/dbg_output/engine.cpp +++ b/library/cpp/dbg_output/engine.cpp @@ -1,8 +1,7 @@ #include "engine.h" #include -#include -#include +#include #if !defined(DBGDUMP_INLINE_IF_INCLUDED) #define DBGDUMP_INLINE_IF_INCLUDED diff --git a/library/cpp/dbg_output/engine.h b/library/cpp/dbg_output/engine.h index 828ff1d3a57..066f4abc41c 100644 --- a/library/cpp/dbg_output/engine.h +++ b/library/cpp/dbg_output/engine.h @@ -2,6 +2,7 @@ #include +#include #include template diff --git a/library/cpp/diff/diff.cpp b/library/cpp/diff/diff.cpp index e4ab9678eb4..58da705fcbc 100644 --- a/library/cpp/diff/diff.cpp +++ b/library/cpp/diff/diff.cpp @@ -51,7 +51,7 @@ struct TCollection: public TCollectionImpl { template <> struct TCollection: public TCollectionImpl { - TCollection(const TWtringBuf& str, const TUtf16String& delims) { + TCollection(const std::u16string_view& str, const std::u16string& delims) { TSetDelimiter set(delims.data()); TKeepDelimiters> c(this); SplitString(str.begin(), str.end(), set, c); @@ -72,7 +72,7 @@ size_t NDiff::InlineDiff(std::vector>& chunks, const std::string_vi return dist; } -size_t NDiff::InlineDiff(std::vector>& chunks, const TWtringBuf& left, const TWtringBuf& right, const TUtf16String& delims) { +size_t NDiff::InlineDiff(std::vector>& chunks, const std::u16string_view& left, const std::u16string_view& right, const std::u16string& delims) { if (delims.empty()) { return InlineDiff(chunks, TConstArrayRef(left.data(), left.size()), TConstArrayRef(right.data(), right.size())); } diff --git a/library/cpp/diff/diff.h b/library/cpp/diff/diff.h index 117954e1a04..c1e59c2ef45 100644 --- a/library/cpp/diff/diff.h +++ b/library/cpp/diff/diff.h @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include @@ -107,6 +107,6 @@ namespace NDiff { // Without delimiters calculates character-wise diff // With delimiters calculates token-wise diff size_t InlineDiff(std::vector>& chunks, const std::string_view& left, const std::string_view& right, const std::string& delims = std::string()); - size_t InlineDiff(std::vector>& chunks, const TWtringBuf& left, const TWtringBuf& right, const TUtf16String& delims = TUtf16String()); + size_t InlineDiff(std::vector>& chunks, const std::u16string_view& left, const std::u16string_view& right, const std::u16string& delims = std::u16string()); } diff --git a/library/cpp/digest/lower_case/hash_ops.h b/library/cpp/digest/lower_case/hash_ops.h index 5babbfb1f91..5c126b1c861 100644 --- a/library/cpp/digest/lower_case/hash_ops.h +++ b/library/cpp/digest/lower_case/hash_ops.h @@ -1,6 +1,6 @@ #pragma once -#include +#include // can be used for caseless hashes like: THashMap diff --git a/library/cpp/digest/lower_case/lchash.h b/library/cpp/digest/lower_case/lchash.h index 6323b49bc0a..f30c60680bc 100644 --- a/library/cpp/digest/lower_case/lchash.h +++ b/library/cpp/digest/lower_case/lchash.h @@ -3,7 +3,7 @@ #include "lciter.h" #include -#include +#include template static inline T FnvCaseLess(const char* b, size_t l, T t = 0) noexcept { diff --git a/library/cpp/digest/md5/md5.h b/library/cpp/digest/md5/md5.h index 0818c1dce72..2636a73f03b 100644 --- a/library/cpp/digest/md5/md5.h +++ b/library/cpp/digest/md5/md5.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include class IInputStream; diff --git a/library/cpp/getopt/small/CMakeLists.txt b/library/cpp/getopt/small/CMakeLists.txt index 1fa3873903c..4918160b274 100644 --- a/library/cpp/getopt/small/CMakeLists.txt +++ b/library/cpp/getopt/small/CMakeLists.txt @@ -5,9 +5,7 @@ target_link_libraries(cpp-getopt-small PUBLIC yutil library-cpp-colorizer cpp-string_utils-misc - cpp-string_builder - cpp-string_utils-string_output -) + ) target_sources(cpp-getopt-small PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/getopt/small/completer.cpp diff --git a/library/cpp/getopt/small/completer.cpp b/library/cpp/getopt/small/completer.cpp index 601b5031a1a..045c4146af7 100644 --- a/library/cpp/getopt/small/completer.cpp +++ b/library/cpp/getopt/small/completer.cpp @@ -2,7 +2,7 @@ #include "completion_generator.h" -#include +#include #include using NLastGetopt::NEscaping::Q; @@ -25,7 +25,7 @@ namespace NLastGetopt::NComp { } std::string_view TCompleterManager::GetCompleterID(const ICompleter* completer) { - return Queue_.emplace_back(NUtils::TYdbStringBuilder() << "_" << Command_ << "__completer_" << ++Id_, completer).first; + return Queue_.emplace_back(TStringBuilder() << "_" << Command_ << "__completer_" << ++Id_, completer).first; } void TCompleterManager::GenerateZsh(TFormattedOutput& out) { @@ -131,7 +131,7 @@ namespace NLastGetopt::NComp { }; ICompleterPtr Choice(std::vector choices) { - NUtils::TYdbStringBuilder bash; + TStringBuilder bash; bash << "COMPREPLY+=( $(compgen -W '"; std::string_view sep = ""; for (auto& choice : choices) { @@ -140,7 +140,7 @@ namespace NLastGetopt::NComp { } bash << "' -- ${cur}) )"; - NUtils::TYdbStringBuilder action; + TStringBuilder action; action << "(("; for (auto& choice: choices) { action << " " << SS(choice.Choice); @@ -153,7 +153,7 @@ namespace NLastGetopt::NComp { } std::string Compgen(std::string_view flags) { - return NUtils::TYdbStringBuilder() << "COMPREPLY+=( $(compgen " << flags << " -- ${cur}) )"; + return TStringBuilder() << "COMPREPLY+=( $(compgen " << flags << " -- ${cur}) )"; } ICompleterPtr Default() { diff --git a/library/cpp/getopt/small/completer.h b/library/cpp/getopt/small/completer.h index bb3347bc46c..a633342f5c4 100644 --- a/library/cpp/getopt/small/completer.h +++ b/library/cpp/getopt/small/completer.h @@ -2,6 +2,8 @@ #include "formatted_output.h" +#include + namespace NLastGetopt::NComp { class ICompleter; diff --git a/library/cpp/getopt/small/completion_generator.cpp b/library/cpp/getopt/small/completion_generator.cpp index 4945fe9fb63..3e418500471 100644 --- a/library/cpp/getopt/small/completion_generator.cpp +++ b/library/cpp/getopt/small/completion_generator.cpp @@ -643,7 +643,7 @@ namespace NLastGetopt { #undef L std::string NEscaping::Q(std::string_view string) { - NUtils::TYdbStringBuilder out; + TStringBuilder out; // out.reserve(string.size()); for (auto c: string) { switch (c) { @@ -694,7 +694,7 @@ namespace NLastGetopt { } std::string NEscaping::C(std::string_view string) { - NUtils::TYdbStringBuilder out; + TStringBuilder out; // out.reserve(string.size() + 1); for (auto c: string) { switch (c) { @@ -727,7 +727,7 @@ namespace NLastGetopt { } std::string NEscaping::S(std::string_view string) { - NUtils::TYdbStringBuilder out; + TStringBuilder out; // out.reserve(string.size() + 1); for (auto c: string) { switch (c) { @@ -757,7 +757,7 @@ namespace NLastGetopt { } std::string NEscaping::B(std::string_view string) { - NUtils::TYdbStringBuilder out; + TStringBuilder out; // out.reserve(string.size() + 1); for (auto c: string) { switch (c) { diff --git a/library/cpp/getopt/small/formatted_output.cpp b/library/cpp/getopt/small/formatted_output.cpp index 28af5fe8eca..16b8c7681ce 100644 --- a/library/cpp/getopt/small/formatted_output.cpp +++ b/library/cpp/getopt/small/formatted_output.cpp @@ -19,8 +19,8 @@ namespace NLastGetopt { return IndentGuard(this); } - NUtils::TYdbStringBuilder& TFormattedOutput::Line() { - return Lines_.emplace_back(IndentLevel_, NUtils::TYdbStringBuilder()).second; + TStringBuilder& TFormattedOutput::Line() { + return Lines_.emplace_back(IndentLevel_, TStringBuilder()).second; } void TFormattedOutput::Print(IOutputStream& out) { diff --git a/library/cpp/getopt/small/formatted_output.h b/library/cpp/getopt/small/formatted_output.h index db72c620eac..afabbbaac71 100644 --- a/library/cpp/getopt/small/formatted_output.h +++ b/library/cpp/getopt/small/formatted_output.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include @@ -19,13 +19,13 @@ namespace NLastGetopt { IndentGuard Indent(); /// Append a new indented line to the stream. - NUtils::TYdbStringBuilder& Line(); + TStringBuilder& Line(); /// Collect all lines into a stream. void Print(IOutputStream& out); private: int IndentLevel_ = 0; - std::vector> Lines_; + std::vector> Lines_; }; } diff --git a/library/cpp/getopt/small/last_getopt.h b/library/cpp/getopt/small/last_getopt.h index a4fef5cae32..980bf00706b 100644 --- a/library/cpp/getopt/small/last_getopt.h +++ b/library/cpp/getopt/small/last_getopt.h @@ -5,6 +5,7 @@ #include "last_getopt_parse_result.h" #include +#include #include /// see some documentation in diff --git a/library/cpp/getopt/small/last_getopt_opts.cpp b/library/cpp/getopt/small/last_getopt_opts.cpp index 4e0fac64d22..466e9289212 100644 --- a/library/cpp/getopt/small/last_getopt_opts.cpp +++ b/library/cpp/getopt/small/last_getopt_opts.cpp @@ -437,7 +437,7 @@ namespace NLastGetopt { size_t lastLineLength = 0; bool helpHasParagraphs = false; if (!help.empty()) { - os << Wrap(Wrap_, help, NUtils::TYdbStringBuilder() << SPad << leftPadding << " ", &lastLineLength, &helpHasParagraphs); + os << Wrap(Wrap_, help, TStringBuilder() << SPad << leftPadding << " ", &lastLineLength, &helpHasParagraphs); } auto choicesHelp = opt->GetChoicesHelp(); diff --git a/library/cpp/getopt/small/last_getopt_support.h b/library/cpp/getopt/small/last_getopt_support.h index 279a1651525..2f0a2d2e619 100644 --- a/library/cpp/getopt/small/last_getopt_support.h +++ b/library/cpp/getopt/small/last_getopt_support.h @@ -3,11 +3,13 @@ #include #include -#include +#include #include #include +#include + namespace NLastGetopt { class TOpt; class TOpts; diff --git a/library/cpp/getopt/small/modchooser.cpp b/library/cpp/getopt/small/modchooser.cpp index a56185070bd..03a6e31484b 100644 --- a/library/cpp/getopt/small/modchooser.cpp +++ b/library/cpp/getopt/small/modchooser.cpp @@ -6,6 +6,7 @@ #include #include +#include class PtrWrapper: public TMainClass { public: @@ -249,7 +250,7 @@ size_t TModChooser::TMode::CalculateFullNameLen() const { } std::string TModChooser::TMode::FormatFullName(size_t pad) const { - NUtils::TYdbStringBuilder name; + TStringBuilder name; if (!Aliases.empty()) { name << "{"; } diff --git a/library/cpp/getopt/small/wrap.cpp b/library/cpp/getopt/small/wrap.cpp index 25386109ce8..4794793e35e 100644 --- a/library/cpp/getopt/small/wrap.cpp +++ b/library/cpp/getopt/small/wrap.cpp @@ -1,8 +1,8 @@ #include "wrap.h" #include -#include +#include #include namespace NLastGetopt { @@ -20,7 +20,7 @@ namespace NLastGetopt { } std::string res; - auto os = NUtils::TStringOutput(res); + auto os = TStringOutput(res); const char* spaceBegin = text.begin(); const char* wordBegin = text.begin(); diff --git a/library/cpp/http/fetch/http_socket.cpp b/library/cpp/http/fetch/http_socket.cpp index 1524ef04a80..64af871d219 100644 --- a/library/cpp/http/fetch/http_socket.cpp +++ b/library/cpp/http/fetch/http_socket.cpp @@ -9,7 +9,8 @@ #include #include #include -#include + +#include /********************************************************/ // HTTPS handler is used as implementation of @@ -40,7 +41,7 @@ static int gcry_pthread_mutex_init(void** priv) { int err = 0; try { - TMutex* lock = new TMutex; + std::mutex* lock = new std::mutex; *priv = lock; } catch (...) { err = -1; @@ -50,19 +51,19 @@ static int gcry_pthread_mutex_init(void** priv) { } static int gcry_pthread_mutex_destroy(void** lock) { - delete static_cast(*lock); + delete static_cast(*lock); return 0; } static int gcry_pthread_mutex_lock(void** lock) { - static_cast(*lock)->Acquire(); + static_cast(*lock)->lock(); return 0; } static int gcry_pthread_mutex_unlock(void** lock) { - static_cast(*lock)->Release(); + static_cast(*lock)->unlock(); return 0; } diff --git a/library/cpp/http/fetch/httpheader.h b/library/cpp/http/fetch/httpheader.h index 63c001206fa..f291fbb8615 100644 --- a/library/cpp/http/fetch/httpheader.h +++ b/library/cpp/http/fetch/httpheader.h @@ -2,11 +2,13 @@ #include "exthttpcodes.h" +#include + #include #include #include -#include +#include #include #include diff --git a/library/cpp/http/fetch/sockhandler.h b/library/cpp/http/fetch/sockhandler.h index 6f4fe909894..94f97ff6d0e 100644 --- a/library/cpp/http/fetch/sockhandler.h +++ b/library/cpp/http/fetch/sockhandler.h @@ -3,12 +3,10 @@ #include #include -#include #include #include #include -#include #include #include diff --git a/library/cpp/http/io/CMakeLists.txt b/library/cpp/http/io/CMakeLists.txt index 2d4e1c8471e..7a66a4ebd46 100644 --- a/library/cpp/http/io/CMakeLists.txt +++ b/library/cpp/http/io/CMakeLists.txt @@ -9,7 +9,6 @@ target_link_libraries(cpp-http-io PUBLIC cpp-streams-lzma cpp-string_utils-stream cpp-string_utils-misc - cpp-string_builder ) target_sources(cpp-http-io PRIVATE diff --git a/library/cpp/http/io/compression.cpp b/library/cpp/http/io/compression.cpp index eabc7e07d1f..beb76ec9bda 100644 --- a/library/cpp/http/io/compression.cpp +++ b/library/cpp/http/io/compression.cpp @@ -4,7 +4,7 @@ #include #endif -#include +#include #include #include #include @@ -56,7 +56,7 @@ TCompressionCodecFactory::TCompressionCodecFactory() { auto dec = [codec](auto s) { return MakeHolder(s, codec); }; - std::string fullName = NUtils::TYdbStringBuilder() << "z-" << codecName; + std::string fullName = TStringBuilder() << "z-" << codecName; Add(fullName, dec, enc); } } diff --git a/library/cpp/http/io/headers.cpp b/library/cpp/http/io/headers.cpp index 9dbeaa7e2a9..a4907da1df4 100644 --- a/library/cpp/http/io/headers.cpp +++ b/library/cpp/http/io/headers.cpp @@ -1,9 +1,9 @@ #include "headers.h" #include "stream.h" -#include #include #include +#include static inline std::string_view Trim(const char* b, const char* e) noexcept { return StripString(std::string_view(b, e)); diff --git a/library/cpp/http/io/headers.h b/library/cpp/http/io/headers.h index 746b7fa5a31..fa99e3a00d9 100644 --- a/library/cpp/http/io/headers.h +++ b/library/cpp/http/io/headers.h @@ -1,9 +1,9 @@ #pragma once -#include +#include -#include -#include +#include +#include #include #include @@ -43,7 +43,7 @@ class THttpInputHeader { /// Возвращает строку "имя параметра: значение". inline std::string ToString() const { - return NUtils::TYdbStringBuilder() << Name_ << ": " << Value_; + return TStringBuilder() << Name_ << ": " << Value_; } private: diff --git a/library/cpp/http/io/headers_ut.cpp b/library/cpp/http/io/headers_ut.cpp index 13e4817bdeb..3e205266256 100644 --- a/library/cpp/http/io/headers_ut.cpp +++ b/library/cpp/http/io/headers_ut.cpp @@ -1,6 +1,6 @@ #include -#include -#include +#include +#include #include #include diff --git a/library/cpp/http/io/stream.h b/library/cpp/http/io/stream.h index eab39978427..04200e0c7f0 100644 --- a/library/cpp/http/io/stream.h +++ b/library/cpp/http/io/stream.h @@ -4,8 +4,8 @@ #include #include -#include -#include +#include +#include #include #include diff --git a/library/cpp/http/misc/CMakeLists.txt b/library/cpp/http/misc/CMakeLists.txt index 151ae7433f8..e88e1b0a249 100644 --- a/library/cpp/http/misc/CMakeLists.txt +++ b/library/cpp/http/misc/CMakeLists.txt @@ -15,7 +15,6 @@ target_link_libraries(cpp-http-misc PUBLIC library-cpp-cgiparam cpp-digest-lower_case cpp-string_utils-misc - cpp-string_builder ) target_sources(cpp-http-misc PRIVATE diff --git a/library/cpp/http/misc/httpreqdata.cpp b/library/cpp/http/misc/httpreqdata.cpp index 3826a4f8b0b..27823ea7d41 100644 --- a/library/cpp/http/misc/httpreqdata.cpp +++ b/library/cpp/http/misc/httpreqdata.cpp @@ -1,7 +1,7 @@ #include "httpreqdata.h" #include -#include +#include #include #include @@ -28,9 +28,10 @@ TBaseServerRequestData::TBaseServerRequestData(std::string_view qs, SOCKET s) } void TBaseServerRequestData::AppendQueryString(std::string_view str) { + using namespace std::literals; if (Y_UNLIKELY(!Query_.empty())) { std::string_view separator = !Query_.ends_with('&') && !str.starts_with('&') ? "&"sv : ""sv; - ModifiedQueryString_ = NUtils::TYdbStringBuilder() << Query_ << separator << str; + ModifiedQueryString_ = TStringBuilder() << Query_ << separator << str; } else { ModifiedQueryString_ = str; } @@ -73,7 +74,7 @@ std::string TBaseServerRequestData::HeaderByIndex(size_t n) const noexcept { const auto& [key, value] = *std::next(HeadersIn_.begin(), n); - return NUtils::TYdbStringBuilder() << key << ": " << value; + return TStringBuilder() << key << ": " << value; } std::string_view TBaseServerRequestData::Environment(std::string_view key) const { @@ -107,6 +108,7 @@ std::string_view TBaseServerRequestData::Environment(std::string_view key) const } const std::string& TBaseServerRequestData::GetCurPage() const { + using namespace std::literals; if (CurPage_.empty() && !Host_.empty()) { std::array fragments; auto fragmentIt = fragments.begin(); diff --git a/library/cpp/http/misc/parsed_request.cpp b/library/cpp/http/misc/parsed_request.cpp index c06c88c989a..38df18302ac 100644 --- a/library/cpp/http/misc/parsed_request.cpp +++ b/library/cpp/http/misc/parsed_request.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -19,11 +20,11 @@ TParsedHttpRequest::TParsedHttpRequest(const std::string_view& str) { std::string_view tmp; if (!NUtils::TrySplit(StripLeft(str), Method, tmp, ' ')) { - ythrow yexception() << "bad request(" << ToString(str).Quote() << ")"; + ythrow yexception() << "bad request(" << NUtils::Quote(str) << ")"; } if (!NUtils::TrySplit(StripLeft(tmp), Request, Proto, ' ')) { - ythrow yexception() << "bad request(" << ToString(str).Quote() << ")"; + ythrow yexception() << "bad request(" << NUtils::Quote(str) << ")"; } Proto = StripLeft(Proto); diff --git a/library/cpp/http/server/http.cpp b/library/cpp/http/server/http.cpp index 563fd58e8be..d36d2980c2e 100644 --- a/library/cpp/http/server/http.cpp +++ b/library/cpp/http/server/http.cpp @@ -13,13 +13,13 @@ #include // stricmp, strnicmp, strlwr, strupr, stpcpy #include #include -#include #include #include #include #include #include +#include using namespace NAddr; @@ -86,7 +86,7 @@ class THttpServer::TImpl { } inline void Add(TClientConnection* c) noexcept { - TGuard g(Mutex_); + std::lock_guard g(Mutex_); Conns_.PushBack(c); if (Options.OneShotPoll) { @@ -104,7 +104,7 @@ class THttpServer::TImpl { TIntrusiveListWithAutoDelete toDelete; { - TGuard g(Mutex_); + std::lock_guard g(Mutex_); PendingDelete_.ForEach([&toDelete, threadNum](TClientConnection * conn) { if (!(conn->CleanupState_.ThreadMask &= ~((ui64)1 << threadNum))) { @@ -116,7 +116,7 @@ class THttpServer::TImpl { inline void Erase(TClientConnection* c, TInstant now) noexcept { - TGuard g(Mutex_); + std::lock_guard g(Mutex_); EraseUnsafe(c, /*removeFromPoller*/!Options.OneShotPoll); if (Options.ExpirationTimeout > TDuration::Zero()) { TryRemovingUnsafe(now - Options.ExpirationTimeout); @@ -124,13 +124,13 @@ class THttpServer::TImpl { } inline void Clear() noexcept { - TGuard g(Mutex_); + std::lock_guard g(Mutex_); Conns_.Clear(); } inline bool RemoveOld(TInstant border) noexcept { - TGuard g(Mutex_); + std::lock_guard g(Mutex_); return TryRemovingUnsafe(border); } @@ -162,7 +162,7 @@ class THttpServer::TImpl { } public: - TMutex Mutex_; + std::mutex Mutex_; TIntrusiveListWithAutoDelete Conns_; TIntrusiveListWithAutoDelete PendingDelete_; TSocketPoller* Poller_ = nullptr; @@ -263,14 +263,14 @@ class THttpServer::TImpl { void Wait() { Cb_->OnWait(); - TGuard g(StopMutex); + std::lock_guard g(StopMutex); JoinListenerThreads(); } void Stop() { Shutdown(); - TGuard g(StopMutex); + std::lock_guard g(StopMutex); JoinListenerThreads(); while (ConnectionCount) { @@ -498,7 +498,7 @@ class THttpServer::TImpl { ICallBack* Cb_ = nullptr; THttpServer* Parent_ = nullptr; TWakeupPollAble WakeupPollAble; - TMutex StopMutex; + std::mutex StopMutex; private: template diff --git a/library/cpp/http/server/http_ut.cpp b/library/cpp/http/server/http_ut.cpp index 7bc13281f82..e4f4b2b4a7e 100644 --- a/library/cpp/http/server/http_ut.cpp +++ b/library/cpp/http/server/http_ut.cpp @@ -8,9 +8,10 @@ #include #include #include -#include #include +#include + Y_UNIT_TEST_SUITE(THttpServerTest) { class TEchoServer: public THttpServer::ICallBack { class TRequest: public THttpClientRequestEx { @@ -73,10 +74,9 @@ Y_UNIT_TEST_SUITE(THttpServerTest) { bool DoReply(const TReplyParams& params) override { ++Server->Replies; - with_lock (Server->Lock) { - params.Output.Write("HTTP/1.0 201 Created\nX-Server: sleeping server\n\nZoooo"); - params.Output.Finish(); - } + std::lock_guard guard(Server->Lock); + params.Output.Write("HTTP/1.0 201 Created\nX-Server: sleeping server\n\nZoooo"); + params.Output.Finish(); return true; } @@ -99,7 +99,7 @@ Y_UNIT_TEST_SUITE(THttpServerTest) { ++MaxConns; } public: - TMutex Lock; + std::mutex Lock; std::atomic Replies; std::atomic MaxConns; @@ -708,7 +708,7 @@ Y_UNIT_TEST_SUITE(THttpServerTest) { THttpServer srv(&server, options); UNIT_ASSERT(srv.Start()); - UNIT_ASSERT(server.Lock.TryAcquire()); + UNIT_ASSERT(server.Lock.try_lock()); std::atomic threadsFinished = 0; std::vector> threads; @@ -737,7 +737,7 @@ Y_UNIT_TEST_SUITE(THttpServerTest) { } } - server.Lock.Release(); + server.Lock.unlock(); for (auto&& thread : threads) { thread->Join(); @@ -983,7 +983,7 @@ Y_UNIT_TEST_SUITE(THttpServerTest) { Y_UNIT_TEST(TestTTLExceed) { // Checks that one of request returns "TTL Exceed" - // First request waits for server.Lock.Release() for one threaded TSleepingServer + // First request waits for server.Lock.unlock() for one threaded TSleepingServer // So second request in queue should fail with TTL Exceed, because fist one lock thread pool for (ttl + 1) ms TPortManager portManager; const ui16 port = portManager.GetPort(); @@ -996,17 +996,16 @@ Y_UNIT_TEST_SUITE(THttpServerTest) { THttpServer srv(&server, options); UNIT_ASSERT(srv.Start()); - UNIT_ASSERT(server.Lock.TryAcquire()); + UNIT_ASSERT(server.Lock.try_lock()); THashSet results; - TMutex resultLock; + std::mutex resultLock; auto func = [port, &resultLock, &results]() { try { TTestRequest r(port); std::string result = r.Execute(); - with_lock(resultLock) { - results.insert(result); - } + std::lock_guard guard(resultLock); + results.insert(result); } catch (...) { } }; @@ -1014,7 +1013,7 @@ Y_UNIT_TEST_SUITE(THttpServerTest) { auto t1 = SystemThreadFactory()->Run(func); auto t2 = SystemThreadFactory()->Run(func); Sleep(TDuration::MilliSeconds(ttl + 1)); - server.Lock.Release(); + server.Lock.unlock(); t1->Join(); t2->Join(); UNIT_ASSERT_EQUAL_C(results, (THashSet({"Zoooo", "TTL Exceed"})), "Results is {" + ToString(results) + "}"); diff --git a/library/cpp/http/simple/http_client.cpp b/library/cpp/http/simple/http_client.cpp index 49be079c961..afb4cb1c8a0 100644 --- a/library/cpp/http/simple/http_client.cpp +++ b/library/cpp/http/simple/http_client.cpp @@ -83,16 +83,16 @@ std::vector TKeepAliveHttpClient::FormRequest(std::string_ std::vector parts; parts.reserve(16 + 4 * headers.size()); - parts.push_back(TStringBuf(method)); - parts.push_back(TStringBuf(" ")); - parts.push_back(TStringBuf(relativeUrl)); - parts.push_back(TStringBuf(" HTTP/1.1")); + parts.push_back(method); + parts.push_back(std::string_view(" ")); + parts.push_back(relativeUrl); + parts.push_back(std::string_view(" HTTP/1.1")); parts.push_back(IOutputStream::TPart::CrLf()); - parts.push_back(TStringBuf("Host: ")); - parts.push_back(TStringBuf(Host)); + parts.push_back(std::string_view("Host: ")); + parts.push_back(std::string_view(Host)); parts.push_back(IOutputStream::TPart::CrLf()); - parts.push_back(TStringBuf("Content-Length: ")); - parts.push_back(TStringBuf(contentLength)); + parts.push_back(std::string_view("Content-Length: ")); + parts.push_back(contentLength); parts.push_back(IOutputStream::TPart::CrLf()); for (const auto& entry : headers) { diff --git a/library/cpp/json/CMakeLists.txt b/library/cpp/json/CMakeLists.txt index b5e31eecc6c..d72523c4bf0 100644 --- a/library/cpp/json/CMakeLists.txt +++ b/library/cpp/json/CMakeLists.txt @@ -12,7 +12,6 @@ target_link_libraries(library-cpp-json PUBLIC cpp-json-fast_sax cpp-json-writer cpp-string_utils-relaxed_escaper - cpp-string_builder ) target_sources(library-cpp-json PRIVATE diff --git a/library/cpp/json/common/defs.h b/library/cpp/json/common/defs.h index 434661ee527..249e4f8883d 100644 --- a/library/cpp/json/common/defs.h +++ b/library/cpp/json/common/defs.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include namespace NJson { diff --git a/library/cpp/json/fast_sax/CMakeLists.darwin-arm64.txt b/library/cpp/json/fast_sax/CMakeLists.darwin-arm64.txt index c2bbfeb86ed..441d602ca7a 100644 --- a/library/cpp/json/fast_sax/CMakeLists.darwin-arm64.txt +++ b/library/cpp/json/fast_sax/CMakeLists.darwin-arm64.txt @@ -12,7 +12,6 @@ target_link_libraries(cpp-json-fast_sax PUBLIC contrib-libs-cxxsupp yutil cpp-json-common - cpp-string_builder ) target_sources(cpp-json-fast_sax PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/json/fast_sax/unescape.cpp diff --git a/library/cpp/json/fast_sax/CMakeLists.darwin-x86_64.txt b/library/cpp/json/fast_sax/CMakeLists.darwin-x86_64.txt index c2bbfeb86ed..441d602ca7a 100644 --- a/library/cpp/json/fast_sax/CMakeLists.darwin-x86_64.txt +++ b/library/cpp/json/fast_sax/CMakeLists.darwin-x86_64.txt @@ -12,7 +12,6 @@ target_link_libraries(cpp-json-fast_sax PUBLIC contrib-libs-cxxsupp yutil cpp-json-common - cpp-string_builder ) target_sources(cpp-json-fast_sax PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/json/fast_sax/unescape.cpp diff --git a/library/cpp/json/fast_sax/CMakeLists.linux-aarch64.txt b/library/cpp/json/fast_sax/CMakeLists.linux-aarch64.txt index 0f6e814fcb9..5a75c7a88f3 100644 --- a/library/cpp/json/fast_sax/CMakeLists.linux-aarch64.txt +++ b/library/cpp/json/fast_sax/CMakeLists.linux-aarch64.txt @@ -13,7 +13,6 @@ target_link_libraries(cpp-json-fast_sax PUBLIC contrib-libs-cxxsupp yutil cpp-json-common - cpp-string_builder ) target_sources(cpp-json-fast_sax PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/json/fast_sax/unescape.cpp diff --git a/library/cpp/json/fast_sax/CMakeLists.linux-x86_64.txt b/library/cpp/json/fast_sax/CMakeLists.linux-x86_64.txt index 0f6e814fcb9..5a75c7a88f3 100644 --- a/library/cpp/json/fast_sax/CMakeLists.linux-x86_64.txt +++ b/library/cpp/json/fast_sax/CMakeLists.linux-x86_64.txt @@ -13,7 +13,6 @@ target_link_libraries(cpp-json-fast_sax PUBLIC contrib-libs-cxxsupp yutil cpp-json-common - cpp-string_builder ) target_sources(cpp-json-fast_sax PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/json/fast_sax/unescape.cpp diff --git a/library/cpp/json/fast_sax/CMakeLists.windows-x86_64.txt b/library/cpp/json/fast_sax/CMakeLists.windows-x86_64.txt index 50dc8421b13..11c78db37a7 100644 --- a/library/cpp/json/fast_sax/CMakeLists.windows-x86_64.txt +++ b/library/cpp/json/fast_sax/CMakeLists.windows-x86_64.txt @@ -12,7 +12,6 @@ target_link_libraries(cpp-json-fast_sax PUBLIC contrib-libs-cxxsupp yutil cpp-json-common - cpp-string_builder ) target_sources(cpp-json-fast_sax PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/json/fast_sax/unescape.cpp diff --git a/library/cpp/json/fast_sax/parser.rl6 b/library/cpp/json/fast_sax/parser.rl6 index d11ccab7b51..4bdc33ad05b 100644 --- a/library/cpp/json/fast_sax/parser.rl6 +++ b/library/cpp/json/fast_sax/parser.rl6 @@ -1,10 +1,10 @@ #include #include -#include +#include #include #include -#include +#include #include namespace NJson { @@ -59,7 +59,7 @@ struct TParserCtx { if (token.empty()) { Hndl.OnError(off, reason); } else { - Hndl.OnError(off, NUtils::TYdbStringBuilder() << reason << " at token: '" << token << "'"); + Hndl.OnError(off, TStringBuilder() << reason << " at token: '" << token << "'"); } return false; diff --git a/library/cpp/json/json_reader.cpp b/library/cpp/json/json_reader.cpp index 8420a9f9761..d3bd05a94bb 100644 --- a/library/cpp/json/json_reader.cpp +++ b/library/cpp/json/json_reader.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include @@ -14,7 +14,7 @@ namespace NJson { namespace { std::string PrintError(const rapidjson::ParseResult& result) { - return NUtils::TYdbStringBuilder() << "Offset: " << result.Offset() + return TStringBuilder() << "Offset: " << result.Offset() << ", Code: " << (int)result.Code() << ", Error: " << GetParseError_En(result.Code()); } diff --git a/library/cpp/json/json_writer.cpp b/library/cpp/json/json_writer.cpp index dd18816b2a4..fbfb57117b1 100644 --- a/library/cpp/json/json_writer.cpp +++ b/library/cpp/json/json_writer.cpp @@ -1,6 +1,6 @@ #include "json_writer.h" -#include +#include #include @@ -74,7 +74,7 @@ namespace NJson { Buf.WriteKey(value); } else { if (DontEscapeStrings) { - Buf.UnsafeWriteValue(NUtils::TYdbStringBuilder() << "\"" << value << '"'); + Buf.UnsafeWriteValue(TStringBuilder() << "\"" << value << '"'); } else { Buf.WriteString(value); } diff --git a/library/cpp/json/writer/CMakeLists.txt b/library/cpp/json/writer/CMakeLists.txt index 92bfd168705..f39b120423e 100644 --- a/library/cpp/json/writer/CMakeLists.txt +++ b/library/cpp/json/writer/CMakeLists.txt @@ -13,7 +13,6 @@ target_link_libraries(cpp-json-writer PUBLIC yutil cpp-json-common tools-enum_parser-enum_serialization_runtime - cpp-string_builder cpp-string_utils-misc ) diff --git a/library/cpp/json/writer/json.cpp b/library/cpp/json/writer/json.cpp index e8dc0f20c73..bd1ed26233a 100644 --- a/library/cpp/json/writer/json.cpp +++ b/library/cpp/json/writer/json.cpp @@ -1,7 +1,7 @@ #include "json.h" #include -#include +#include #include #include @@ -493,7 +493,7 @@ namespace NJsonWriter { if (callback.empty()) { return buf.Str(); } else { - return NUtils::TYdbStringBuilder() << callback << "(" << buf.Str() << ")"; + return TStringBuilder() << callback << "(" << buf.Str() << ")"; } } diff --git a/library/cpp/json/writer/json_value.cpp b/library/cpp/json/writer/json_value.cpp index 9a96cdcd704..89ba1e6bee4 100644 --- a/library/cpp/json/writer/json_value.cpp +++ b/library/cpp/json/writer/json_value.cpp @@ -1,7 +1,7 @@ #include "json_value.h" #include "json.h" -#include +#include #include #include @@ -892,11 +892,11 @@ namespace NJson { if (Type == JSON_MAP) { for (auto&& i : *Value.Map) { - i.second.DoScan(!path.empty() ? NUtils::TYdbStringBuilder() << path << "." << i.first : i.first, this, callback); + i.second.DoScan(!path.empty() ? TStringBuilder() << path << "." << i.first : i.first, this, callback); } } else if (Type == JSON_ARRAY) { for (ui32 i = 0; i < Value.Array->size(); ++i) { - (*Value.Array)[i].DoScan(NUtils::TYdbStringBuilder() << path << "[" << ToString(i) << "]", this, callback); + (*Value.Array)[i].DoScan(TStringBuilder() << path << "[" << ToString(i) << "]", this, callback); } } } diff --git a/library/cpp/logger/CMakeLists.txt b/library/cpp/logger/CMakeLists.txt index 2157accf57d..d02e87be226 100644 --- a/library/cpp/logger/CMakeLists.txt +++ b/library/cpp/logger/CMakeLists.txt @@ -13,7 +13,6 @@ target_link_libraries(library-cpp-logger PUBLIC tools-enum_parser-enum_serialization_runtime library-cpp-json cpp-deprecated-atomic - cpp-string_builder ) target_sources(library-cpp-logger PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/logger/backend.cpp diff --git a/library/cpp/logger/backend.cpp b/library/cpp/logger/backend.cpp index cf090e5dd9a..4e3a69826b3 100644 --- a/library/cpp/logger/backend.cpp +++ b/library/cpp/logger/backend.cpp @@ -1,23 +1,24 @@ #include "backend.h" -#include #include #include +#include + namespace { class TGlobalLogsStorage { private: std::vector Backends; - TMutex Mutex; + std::mutex Mutex; public: void Register(TLogBackend* backend) { - TGuard g(Mutex); + std::lock_guard g(Mutex); Backends.push_back(backend); } void UnRegister(TLogBackend* backend) { - TGuard g(Mutex); + std::lock_guard g(Mutex); for (ui32 i = 0; i < Backends.size(); ++i) { if (Backends[i] == backend) { Backends.erase(Backends.begin() + i); @@ -28,7 +29,7 @@ namespace { } void Reopen(bool flush) { - TGuard g(Mutex); + std::lock_guard g(Mutex); for (auto& b : Backends) { if (typeid(*b) == typeid(TLogBackend)) { continue; diff --git a/library/cpp/logger/composite.h b/library/cpp/logger/composite.h index e6b680cb10b..4fd534ca0c5 100644 --- a/library/cpp/logger/composite.h +++ b/library/cpp/logger/composite.h @@ -1,7 +1,7 @@ #pragma once #include "backend.h" -#include +#include class TCompositeLogBackend: public TLogBackend { diff --git a/library/cpp/logger/element_ut.cpp b/library/cpp/logger/element_ut.cpp index 13e43622663..58bd7c8872e 100644 --- a/library/cpp/logger/element_ut.cpp +++ b/library/cpp/logger/element_ut.cpp @@ -2,7 +2,7 @@ #include "element.h" #include "stream.h" -#include +#include #include #include #include diff --git a/library/cpp/logger/global/common.cpp b/library/cpp/logger/global/common.cpp index 32e0ddd6d2c..34add878918 100644 --- a/library/cpp/logger/global/common.cpp +++ b/library/cpp/logger/global/common.cpp @@ -9,6 +9,7 @@ namespace NLoggingImpl { } std::string PrepareToOpenLog(std::string logType, const int logLevel, const bool rotation, const bool startAsDaemon) { + using namespace std::literals; Y_ENSURE(logLevel >= 0 && logLevel <= (int)LOG_MAX_PRIORITY, "Incorrect log level"); if (rotation && TFsPath(logType).Exists()) { diff --git a/library/cpp/logger/log_ut.cpp b/library/cpp/logger/log_ut.cpp index 9d879a7141b..25ebadd2b00 100644 --- a/library/cpp/logger/log_ut.cpp +++ b/library/cpp/logger/log_ut.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include class TLogTest: public TTestBase { diff --git a/library/cpp/logger/record.h b/library/cpp/logger/record.h index ccda04a25ad..c8ef6ca058f 100644 --- a/library/cpp/logger/record.h +++ b/library/cpp/logger/record.h @@ -2,7 +2,7 @@ #include "priority.h" -#include +#include #include #include diff --git a/library/cpp/logger/reopen_ut.cpp b/library/cpp/logger/reopen_ut.cpp index e40f3fcb380..aa08de0da39 100644 --- a/library/cpp/logger/reopen_ut.cpp +++ b/library/cpp/logger/reopen_ut.cpp @@ -2,7 +2,7 @@ #include "reopen.h" #include -#include +#include #include #include diff --git a/library/cpp/logger/rotating_file.cpp b/library/cpp/logger/rotating_file.cpp index f0b96c6886f..2c80cf30cc3 100644 --- a/library/cpp/logger/rotating_file.cpp +++ b/library/cpp/logger/rotating_file.cpp @@ -2,7 +2,7 @@ #include "file.h" #include "record.h" -#include +#include #include #include @@ -35,9 +35,9 @@ class TRotatingFileLogBackend::TImpl { if (static_cast(AtomicGet(Size_)) > MaxSizeBytes_) { TWriteGuard guard(Lock_); if (static_cast(AtomicGet(Size_)) > MaxSizeBytes_) { - std::string newLogPath(NUtils::TYdbStringBuilder() << Path_ << "." << RotatedFilesCount_); + std::string newLogPath(TStringBuilder() << Path_ << "." << RotatedFilesCount_); for (size_t fileId = RotatedFilesCount_ - 1; fileId; --fileId) { - std::string oldLogPath(NUtils::TYdbStringBuilder() << Path_ << "." << fileId); + std::string oldLogPath(TStringBuilder() << Path_ << "." << fileId); NFs::Rename(oldLogPath.c_str(), newLogPath.c_str()); newLogPath = oldLogPath; } diff --git a/library/cpp/logger/rotating_file_ut.cpp b/library/cpp/logger/rotating_file_ut.cpp index e656be569a3..d5b2017e550 100644 --- a/library/cpp/logger/rotating_file_ut.cpp +++ b/library/cpp/logger/rotating_file_ut.cpp @@ -1,7 +1,7 @@ #include "rotating_file.h" #include "record.h" -#include +#include #include #include diff --git a/library/cpp/logger/stream.cpp b/library/cpp/logger/stream.cpp index c0290e9d3d3..456e05f1c1c 100644 --- a/library/cpp/logger/stream.cpp +++ b/library/cpp/logger/stream.cpp @@ -1,7 +1,7 @@ #include "stream.h" #include "record.h" -#include +#include #include @@ -33,7 +33,7 @@ void TStreamWithContextLogBackend::WriteData(const TLogRecord& rec) { Slave_->Write(rec.Data, rec.Len); Slave_->Write(DELIMITER); for (const auto& [key, value] : rec.MetaFlags) { - Slave_->Write(NUtils::TYdbStringBuilder() << key << "=" << value); + Slave_->Write(TStringBuilder() << key << "=" << value); Slave_->Write(DELIMITER); } } diff --git a/library/cpp/logger/stream.h b/library/cpp/logger/stream.h index cba4504fbaa..dfbee134ac0 100644 --- a/library/cpp/logger/stream.h +++ b/library/cpp/logger/stream.h @@ -2,7 +2,7 @@ #include "backend.h" -#include +#include class IOutputStream; diff --git a/library/cpp/logger/sync_page_cache_file.cpp b/library/cpp/logger/sync_page_cache_file.cpp index 928bb9f2d36..4f87896afb2 100644 --- a/library/cpp/logger/sync_page_cache_file.cpp +++ b/library/cpp/logger/sync_page_cache_file.cpp @@ -5,9 +5,10 @@ #include #include #include -#include #include +#include + class TSyncPageCacheFileLogBackend::TImpl: public TNonCopyable { public: TImpl(const std::string& path, size_t maxBufferSize, size_t maxPendingCacheSize) @@ -28,7 +29,7 @@ class TSyncPageCacheFileLogBackend::TImpl: public TNonCopyable { } void WriteData(const TLogRecord& rec) { - TGuard guard{Lock_}; + std::lock_guard guard{Lock_}; Buffer_.Append(rec.Data, rec.Len); if (Buffer_.size() >= MaxBufferSize_) { @@ -47,7 +48,7 @@ class TSyncPageCacheFileLogBackend::TImpl: public TNonCopyable { } void ReopenLog() { - TGuard guard{Lock_}; + std::lock_guard guard{Lock_}; Write(); FlushSync(GuaranteedWrittenPtr_, WrittenPtr_); @@ -102,7 +103,7 @@ class TSyncPageCacheFileLogBackend::TImpl: public TNonCopyable { } private: - TMutex Lock_; + std::mutex Lock_; TFile File_; const size_t MaxBufferSize_ = 0; diff --git a/library/cpp/mime/types/mime.cpp b/library/cpp/mime/types/mime.cpp index 89c8a6616cd..e82ebb0cd20 100644 --- a/library/cpp/mime/types/mime.cpp +++ b/library/cpp/mime/types/mime.cpp @@ -155,7 +155,7 @@ MimeTypes TMimeTypes::MimeByStr(const char* str) const { } MimeTypes TMimeTypes::MimeByStr(const std::string_view& str) const { - TRecordHash::const_iterator it = ContentTypes.find(str); + TRecordHash::const_iterator it = ContentTypes.find(str.data()); if (it == ContentTypes.end()) return MIME_UNKNOWN; return Records[it->second].Mime; diff --git a/library/cpp/monlib/counters/counters.h b/library/cpp/monlib/counters/counters.h index d14bca8af11..b0c74f81d26 100644 --- a/library/cpp/monlib/counters/counters.h +++ b/library/cpp/monlib/counters/counters.h @@ -4,7 +4,6 @@ #include #include -#include #include #include @@ -18,6 +17,7 @@ #include #include +#include #include namespace NMonitoring { @@ -260,7 +260,7 @@ namespace NMonitoring { private: G* Add(const T& name) { - TGuard guard(AddMutex); + std::lock_guard guard(AddMutex); G* result = Groups->Find(name); if (result == nullptr) { T* newName = new T(name); @@ -294,7 +294,7 @@ namespace NMonitoring { } virtual ~TDeprecatedCounterGroups() { - TGuard guard(AddMutex); + std::lock_guard guard(AddMutex); Groups->Free(); delete Groups; Groups = nullptr; diff --git a/library/cpp/monlib/dynamic_counters/CMakeLists.txt b/library/cpp/monlib/dynamic_counters/CMakeLists.txt index 2762777f01e..a195f8e1fa0 100644 --- a/library/cpp/monlib/dynamic_counters/CMakeLists.txt +++ b/library/cpp/monlib/dynamic_counters/CMakeLists.txt @@ -12,7 +12,6 @@ target_link_libraries(cpp-monlib-dynamic_counters PUBLIC monlib-service-pages cpp-string_utils-quote cpp-threading-light_rw_lock - cpp-string_builder ) target_sources(cpp-monlib-dynamic_counters PRIVATE diff --git a/library/cpp/monlib/dynamic_counters/counters.cpp b/library/cpp/monlib/dynamic_counters/counters.cpp index 1a61fef2fbd..72e3544e062 100644 --- a/library/cpp/monlib/dynamic_counters/counters.cpp +++ b/library/cpp/monlib/dynamic_counters/counters.cpp @@ -1,7 +1,7 @@ #include "counters.h" #include -#include +#include #include @@ -254,7 +254,7 @@ void TDynamicCounters::OutputPlainText(IOutputStream& os, const std::string& ind if (const auto subgroup = AsDynamicCounters(value)) { os << "\n"; os << indent << key.LabelName << "=" << key.LabelValue << ":\n"; - subgroup->OutputPlainText(os, NUtils::TYdbStringBuilder() << indent << INDENT); + subgroup->OutputPlainText(os, TStringBuilder() << indent << INDENT); } } } diff --git a/library/cpp/monlib/encode/buffered/CMakeLists.txt b/library/cpp/monlib/encode/buffered/CMakeLists.txt index 1a9e81a5edf..d7e3dc9080c 100644 --- a/library/cpp/monlib/encode/buffered/CMakeLists.txt +++ b/library/cpp/monlib/encode/buffered/CMakeLists.txt @@ -5,7 +5,6 @@ target_link_libraries(monlib-encode-buffered PUBLIC yutil cpp-monlib-encode cpp-monlib-metrics - cpp-string_builder ) target_sources(monlib-encode-buffered PRIVATE diff --git a/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp b/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp index 72c14de0142..93bffaf150a 100644 --- a/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp +++ b/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp @@ -1,6 +1,6 @@ #include "buffered_encoder_base.h" -#include +#include #include #include @@ -149,7 +149,7 @@ void TBufferedEncoderBase::OnLogHistogram(TInstant time, TLogHistogramSnapshotPt std::string TBufferedEncoderBase::FormatLabels(const TPooledLabels& labels) const { auto formattedLabels = std::vector(labels.size() + CommonLabels_.size()); auto addLabel = [&](const TPooledLabel& l) { - auto formattedLabel = NUtils::TYdbStringBuilder() << LabelNamesPool_.Get(l.Key) << '=' << LabelValuesPool_.Get(l.Value); + auto formattedLabel = TStringBuilder() << LabelNamesPool_.Get(l.Key) << '=' << LabelValuesPool_.Get(l.Value); formattedLabels.push_back(std::move(formattedLabel)); }; @@ -166,7 +166,7 @@ std::string TBufferedEncoderBase::FormatLabels(const TPooledLabels& labels) cons } Sort(formattedLabels); - return NUtils::TYdbStringBuilder() << "{" << JoinSeq(", ", formattedLabels) << "}"; + return TStringBuilder() << "{" << JoinSeq(", ", formattedLabels) << "}"; } } // namespace NMonitoring diff --git a/library/cpp/monlib/encode/buffered/string_pool.cpp b/library/cpp/monlib/encode/buffered/string_pool.cpp index 63857bb983e..a603aa1f4fe 100644 --- a/library/cpp/monlib/encode/buffered/string_pool.cpp +++ b/library/cpp/monlib/encode/buffered/string_pool.cpp @@ -1,5 +1,7 @@ #include "string_pool.h" +#include + namespace NMonitoring { //////////////////////////////////////////////////////////////////////////////// // TStringPoolBuilder diff --git a/library/cpp/monlib/encode/format.h b/library/cpp/monlib/encode/format.h index bad44b40f79..e8c9d178b2b 100644 --- a/library/cpp/monlib/encode/format.h +++ b/library/cpp/monlib/encode/format.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace NMonitoring { namespace NFormatContenType { diff --git a/library/cpp/monlib/encode/format_ut.cpp b/library/cpp/monlib/encode/format_ut.cpp index 229c77c7688..266eeea11b5 100644 --- a/library/cpp/monlib/encode/format_ut.cpp +++ b/library/cpp/monlib/encode/format_ut.cpp @@ -2,7 +2,7 @@ #include -#include +#include #include #include diff --git a/library/cpp/monlib/encode/json/CMakeLists.txt b/library/cpp/monlib/encode/json/CMakeLists.txt index cac3fe15203..f8e549579df 100644 --- a/library/cpp/monlib/encode/json/CMakeLists.txt +++ b/library/cpp/monlib/encode/json/CMakeLists.txt @@ -8,7 +8,6 @@ target_link_libraries(monlib-encode-json PUBLIC cpp-monlib-exception library-cpp-json cpp-json-writer - cpp-string_builder ) target_sources(monlib-encode-json PRIVATE diff --git a/library/cpp/monlib/encode/json/fuzz/main.cpp b/library/cpp/monlib/encode/json/fuzz/main.cpp index 4f40310e068..dcff9805d62 100644 --- a/library/cpp/monlib/encode/json/fuzz/main.cpp +++ b/library/cpp/monlib/encode/json/fuzz/main.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include extern "C" int LLVMFuzzerTestOneInput(const ui8* data, size_t size) { diff --git a/library/cpp/monlib/encode/json/json_decoder.cpp b/library/cpp/monlib/encode/json/json_decoder.cpp index a845989a728..f1aa205e775 100644 --- a/library/cpp/monlib/encode/json/json_decoder.cpp +++ b/library/cpp/monlib/encode/json/json_decoder.cpp @@ -8,7 +8,7 @@ #include -#include +#include #include #include @@ -494,7 +494,7 @@ class TDecoderJson final: public NJson::TJsonCallbacks { #define PARSE_ENSURE(CONDITION, ...) \ do { \ if (Y_UNLIKELY(!(CONDITION))) { \ - ErrorMsg_ = NUtils::TYdbStringBuilder() << __VA_ARGS__; \ + ErrorMsg_ = TStringBuilder() << __VA_ARGS__; \ return false; \ } \ } while (false) @@ -862,7 +862,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ } else if (key == std::string_view("memOnly")) { // deprecated. Skip it without errors for backward compatibility } else { - ErrorMsg_ = NUtils::TYdbStringBuilder() << "unexpected key \"" << key << "\" in a metric schema"; + ErrorMsg_ = TStringBuilder() << "unexpected key \"" << key << "\" in a metric schema"; return false; } break; diff --git a/library/cpp/monlib/encode/prometheus/CMakeLists.txt b/library/cpp/monlib/encode/prometheus/CMakeLists.txt index 1863ba0c7aa..e94adb46ead 100644 --- a/library/cpp/monlib/encode/prometheus/CMakeLists.txt +++ b/library/cpp/monlib/encode/prometheus/CMakeLists.txt @@ -5,7 +5,6 @@ target_link_libraries(monlib-encode-prometheus PUBLIC yutil cpp-monlib-encode monlib-encode-buffered - cpp-string_builder ) target_sources(monlib-encode-prometheus PRIVATE diff --git a/library/cpp/monlib/encode/prometheus/prometheus_decoder.cpp b/library/cpp/monlib/encode/prometheus/prometheus_decoder.cpp index 5472cd905af..00b1e4d6e6b 100644 --- a/library/cpp/monlib/encode/prometheus/prometheus_decoder.cpp +++ b/library/cpp/monlib/encode/prometheus/prometheus_decoder.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include @@ -27,7 +27,7 @@ namespace NMonitoring { using TLabelsMap = THashMap; std::string LabelsToStr(const TLabelsMap& labels) { - NUtils::TYdbStringBuilder sb; + TStringBuilder sb; auto it = labels.begin(); auto end = labels.end(); diff --git a/library/cpp/monlib/metrics/histogram_snapshot.h b/library/cpp/monlib/metrics/histogram_snapshot.h index a7f9411eabb..1f8923ba365 100644 --- a/library/cpp/monlib/metrics/histogram_snapshot.h +++ b/library/cpp/monlib/metrics/histogram_snapshot.h @@ -4,6 +4,7 @@ #include #include +#include #include #include diff --git a/library/cpp/monlib/metrics/labels.h b/library/cpp/monlib/metrics/labels.h index 1d406156d69..c6fc4d801b1 100644 --- a/library/cpp/monlib/metrics/labels.h +++ b/library/cpp/monlib/metrics/labels.h @@ -198,10 +198,6 @@ namespace NMonitoring { TLabelsImpl() = default; - explicit TLabelsImpl(::NDetail::TReserveTag rt) - : Labels_(std::move(rt)) - {} - explicit TLabelsImpl(size_t count) : Labels_(count) {} diff --git a/library/cpp/monlib/metrics/metric_type.cpp b/library/cpp/monlib/metrics/metric_type.cpp index 2982b0f2370..c6d9b3260d9 100644 --- a/library/cpp/monlib/metrics/metric_type.cpp +++ b/library/cpp/monlib/metrics/metric_type.cpp @@ -1,6 +1,6 @@ #include "metric_type.h" -#include +#include #include #include diff --git a/library/cpp/monlib/service/monservice.cpp b/library/cpp/monlib/service/monservice.cpp index 42a64533b77..a268343c55d 100644 --- a/library/cpp/monlib/service/monservice.cpp +++ b/library/cpp/monlib/service/monservice.cpp @@ -3,7 +3,6 @@ #include #include -#include #include #include diff --git a/library/cpp/monlib/service/pages/index_mon_page.cpp b/library/cpp/monlib/service/pages/index_mon_page.cpp index 5089573176e..2db1bddcd01 100644 --- a/library/cpp/monlib/service/pages/index_mon_page.cpp +++ b/library/cpp/monlib/service/pages/index_mon_page.cpp @@ -25,7 +25,7 @@ void TIndexMonPage::Output(IMonHttpRequest& request) { TMonPagePtr found; // analogous to CGI PATH_INFO { - TGuard g(Mtx); + std::lock_guard g(Mtx); std::string_view pathTmp = request.GetPathInfo(); for (;;) { if (TPagesByPath::iterator i = PagesByPath.find(pathTmp); i != PagesByPath.end()) { @@ -51,7 +51,7 @@ void TIndexMonPage::Output(IMonHttpRequest& request) { } void TIndexMonPage::OutputIndex(IOutputStream& out, bool pathEndsWithSlash) { - TGuard g(Mtx); + std::lock_guard g(Mtx); for (auto& Page : Pages) { IMonPage* page = Page.Get(); if (page->IsInIndex()) { @@ -65,7 +65,7 @@ void TIndexMonPage::OutputIndex(IOutputStream& out, bool pathEndsWithSlash) { } void TIndexMonPage::Register(TMonPagePtr page) { - TGuard g(Mtx); + std::lock_guard g(Mtx); if (auto [it, inserted] = PagesByPath.try_emplace("/" + page->GetPath()); inserted) { // new unique page just inserted, insert it to the end it->second = Pages.insert(Pages.end(), page); @@ -77,7 +77,7 @@ void TIndexMonPage::Register(TMonPagePtr page) { } TIndexMonPage* TIndexMonPage::RegisterIndexPage(const std::string& path, const std::string& title) { - TGuard g(Mtx); + std::lock_guard g(Mtx); TIndexMonPage* page = VerifyDynamicCast(FindPage(path)); if (page) { return page; @@ -88,7 +88,7 @@ TIndexMonPage* TIndexMonPage::RegisterIndexPage(const std::string& path, const s } IMonPage* TIndexMonPage::FindPage(const std::string& relativePath) { - TGuard g(Mtx); + std::lock_guard g(Mtx); TPagesByPath::iterator i = PagesByPath.find("/" + relativePath); if (i == PagesByPath.end()) { @@ -163,14 +163,14 @@ void TIndexMonPage::OutputBody(IMonHttpRequest& req) { } void TIndexMonPage::SortPages() { - TGuard g(Mtx); + std::lock_guard g(Mtx); Pages.sort([](const TMonPagePtr& a, const TMonPagePtr& b) { return AsciiCompareIgnoreCase(a->GetTitle(), b->GetTitle()) < 0; }); } void TIndexMonPage::ClearPages() { - TGuard g(Mtx); + std::lock_guard g(Mtx); Pages.clear(); PagesByPath.clear(); } diff --git a/library/cpp/monlib/service/pages/index_mon_page.h b/library/cpp/monlib/service/pages/index_mon_page.h index 5122fa88242..5dede23cac3 100644 --- a/library/cpp/monlib/service/pages/index_mon_page.h +++ b/library/cpp/monlib/service/pages/index_mon_page.h @@ -3,10 +3,11 @@ #include "mon_page.h" #include +#include namespace NMonitoring { struct TIndexMonPage: public IMonPage { - TMutex Mtx; + std::mutex Mtx; using TPages = std::list; TPages Pages; // a list of pages to maintain specific order using TPagesByPath = THashMap; diff --git a/library/cpp/monlib/service/pages/mon_page.h b/library/cpp/monlib/service/pages/mon_page.h index c4b3ef3e45f..8912a42f6d7 100644 --- a/library/cpp/monlib/service/pages/mon_page.h +++ b/library/cpp/monlib/service/pages/mon_page.h @@ -3,7 +3,7 @@ #include #include -#include +#include #include namespace NMonitoring { diff --git a/library/cpp/object_factory/object_factory.h b/library/cpp/object_factory/object_factory.h index d30a2f17518..3fa2cdafc79 100644 --- a/library/cpp/object_factory/object_factory.h +++ b/library/cpp/object_factory/object_factory.h @@ -6,6 +6,7 @@ #include #include +#include #include namespace NObjectFactory { diff --git a/library/cpp/openssl/init/init.cpp b/library/cpp/openssl/init/init.cpp index 14ed21cbc89..aa9e545047d 100644 --- a/library/cpp/openssl/init/init.cpp +++ b/library/cpp/openssl/init/init.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include @@ -18,6 +17,7 @@ #include #include #include +#include #include namespace { @@ -27,7 +27,7 @@ namespace { : Mutexes(CRYPTO_num_locks()) { for (auto& mpref : Mutexes) { - mpref.Reset(new TMutex()); + mpref.Reset(new std::mutex()); } } @@ -35,13 +35,13 @@ namespace { auto& mutex = *Mutexes.at(n); if (mode & CRYPTO_LOCK) { - mutex.Acquire(); + mutex.lock(); } else { - mutex.Release(); + mutex.unlock(); } } - std::vector> Mutexes; + std::vector> Mutexes; }; inline TInitSsl() { diff --git a/library/cpp/resource/registry.cpp b/library/cpp/resource/registry.cpp index b86da1202bb..9d910409e1c 100644 --- a/library/cpp/resource/registry.cpp +++ b/library/cpp/resource/registry.cpp @@ -8,6 +8,7 @@ #include #include #include +#include using namespace NResource; using namespace NBlockCodecs; @@ -61,7 +62,7 @@ namespace { try { *out = Decompress((*res)->second); } catch (const yexception& e) { - if (GetEnv("RESOURCE_DECOMPRESS_DIAG")) { + if (!GetEnv("RESOURCE_DECOMPRESS_DIAG").empty()) { Cerr << "Can't decompress resource " << key << Endl << e.what() << Endl; } throw e; @@ -83,7 +84,7 @@ namespace { it.first, Decompress(it.second->second)}; cb.OnMatch(res); } catch (const yexception& e) { - if (GetEnv("RESOURCE_DECOMPRESS_DIAG")) { + if (!GetEnv("RESOURCE_DECOMPRESS_DIAG").empty()) { Cerr << "Can't decompress resource " << it.first << Endl << e.what() << Endl; } throw e; diff --git a/library/cpp/resource/registry.h b/library/cpp/resource/registry.h index dee7f59198d..3730a892c28 100644 --- a/library/cpp/resource/registry.h +++ b/library/cpp/resource/registry.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include "resource.h" diff --git a/library/cpp/resource/resource.h b/library/cpp/resource/resource.h index d0088d2a177..e49064caf0f 100644 --- a/library/cpp/resource/resource.h +++ b/library/cpp/resource/resource.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include namespace NResource { diff --git a/library/cpp/string_builder/CMakeLists.txt b/library/cpp/string_builder/CMakeLists.txt deleted file mode 100644 index ffb695375d5..00000000000 --- a/library/cpp/string_builder/CMakeLists.txt +++ /dev/null @@ -1,17 +0,0 @@ -add_library(cpp-string_builder) - -target_link_libraries(cpp-string_builder PUBLIC - contrib-libs-cxxsupp - yutil - cpp-string_utils-string_output -) - -target_sources(cpp-string_builder PRIVATE - ${CMAKE_SOURCE_DIR}/library/cpp/string_builder/string_builder.cpp -) - -if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT HAVE_CUDA) - target_link_libraries(cpp-string_builder PUBLIC - contrib-libs-linux-headers - ) -endif() \ No newline at end of file diff --git a/library/cpp/string_builder/string_builder.cpp b/library/cpp/string_builder/string_builder.cpp deleted file mode 100644 index 8833a6a9673..00000000000 --- a/library/cpp/string_builder/string_builder.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "string_builder.h" - -template <> -void Out(IOutputStream& os, const NUtils::TYdbStringBuilder& sb) { - os << static_cast(sb); -} \ No newline at end of file diff --git a/library/cpp/string_builder/string_builder.h b/library/cpp/string_builder/string_builder.h deleted file mode 100644 index 7f49ad696c3..00000000000 --- a/library/cpp/string_builder/string_builder.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include - -#include - -namespace NUtils { - -class TYdbStringBuilder: public std::string { -public: - inline TYdbStringBuilder() - : Out(*this) - { - } - - TYdbStringBuilder(TYdbStringBuilder&& rhs) noexcept - : std::string(std::move(rhs)) - , Out(*this) - { - } - - TStringOutput Out; -}; - -template -static inline TYdbStringBuilder& operator<<(TYdbStringBuilder& builder Y_LIFETIME_BOUND, const T& t) { - builder.Out << t; - - return builder; -} - -template -static inline TYdbStringBuilder&& operator<<(TYdbStringBuilder&& builder Y_LIFETIME_BOUND, const T& t) { - builder.Out << t; - - return std::move(builder); -} - -} - diff --git a/library/cpp/string_utils/CMakeLists.txt b/library/cpp/string_utils/CMakeLists.txt index 9958df23b27..c87a4aeccb3 100644 --- a/library/cpp/string_utils/CMakeLists.txt +++ b/library/cpp/string_utils/CMakeLists.txt @@ -7,11 +7,10 @@ add_subdirectory(base64) -add_subdirectory(escape) add_subdirectory(misc) add_subdirectory(quote) add_subdirectory(relaxed_escaper) add_subdirectory(scan) add_subdirectory(stream) -add_subdirectory(string_output) +add_subdirectory(helpers) add_subdirectory(url) diff --git a/library/cpp/string_utils/escape/CMakeLists.txt b/library/cpp/string_utils/escape/CMakeLists.txt deleted file mode 100644 index 59ccea17a3b..00000000000 --- a/library/cpp/string_utils/escape/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -add_library(cpp-string_utils-escape) - -target_link_libraries(cpp-string_utils-escape PUBLIC - contrib-libs-cxxsupp -) - -if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT HAVE_CUDA) - target_link_libraries(cpp-string_utils-escape PUBLIC - contrib-libs-linux-headers - ) -endif() - -target_sources(cpp-string_utils-escape PRIVATE - ${CMAKE_SOURCE_DIR}/library/cpp/string_utils/escape/escape.cpp -) diff --git a/library/cpp/string_utils/escape/escape.cpp b/library/cpp/string_utils/escape/escape.cpp deleted file mode 100644 index 5576968e608..00000000000 --- a/library/cpp/string_utils/escape/escape.cpp +++ /dev/null @@ -1,193 +0,0 @@ -#include "escape.h" - -#include -#include - -/// @todo: escape trigraphs (eg "??/" is "\") - -/* REFEREBCES FOR ESCAPE SEQUENCE INTERPRETATION: - * C99 p. 6.4.3 Universal character names. - * C99 p. 6.4.4.4 Character constants. - * - * ::= { - * \' , \" , \? , \\ , - * \a , \b , \f , \n , \r , \t , \v - * } - * - * ::= \ {1, 3} - * ::= \x + - * ::= \u {4} - * || \U {8} - * - * NOTE (6.4.4.4.7): - * Each octal or hexadecimal escape sequence is the longest sequence of characters that can - * constitute the escape sequence. - * - * THEREFORE: - * - Octal escape sequence spans until rightmost non-octal-digit character. - * - Octal escape sequence always terminates after three octal digits. - * - Hexadecimal escape sequence spans until rightmost non-hexadecimal-digit character. - * - Universal character name consists of exactly 4 or 8 hexadecimal digit. - * - * by kerzum@ - * It is also required to escape trigraphs that are enabled in compilers by default and - * are also processed inside string literals - * The nine trigraphs and their replacements are - * - * Trigraph: ??( ??) ??< ??> ??= ??/ ??' ??! ??- - * Replacement: [ ] { } # \ ^ | ~ - * - */ -namespace { - template - static inline char HexDigit(TChar value) { - Y_ASSERT(value < 16); - if (value < 10) { - return '0' + value; - } else { - return 'A' + value - 10; - } - } - - template - static inline char OctDigit(TChar value) { - Y_ASSERT(value < 8); - return '0' + value; - } - - template - static inline bool IsPrintable(TChar c) { - return c >= 32 && c <= 126; - } - - template - static inline bool IsHexDigit(TChar c) { - return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); - } - - template - static inline bool IsOctDigit(TChar c) { - return c >= '0' && c <= '7'; - } - - template - struct TEscapeUtil; - - template <> - struct TEscapeUtil { - static const size_t ESCAPE_C_BUFFER_SIZE = 4; - - template - static inline size_t EscapeC(unsigned char c, TNextChar next, TBufferChar r[ESCAPE_C_BUFFER_SIZE]) { - // (1) Printable characters go as-is, except backslash and double quote. - // (2) Characters \r, \n, \t and \0 ... \7 replaced by their simple escape characters (if possible). - // (3) Otherwise, character is encoded using hexadecimal escape sequence (if possible), or octal. - if (c == '\"') { - r[0] = '\\'; - r[1] = '\"'; - return 2; - } else if (c == '\\') { - r[0] = '\\'; - r[1] = '\\'; - return 2; - } else if (IsPrintable(c) && (!(c == '?' && next == '?'))) { - r[0] = c; - return 1; - } else if (c == '\r') { - r[0] = '\\'; - r[1] = 'r'; - return 2; - } else if (c == '\n') { - r[0] = '\\'; - r[1] = 'n'; - return 2; - } else if (c == '\t') { - r[0] = '\\'; - r[1] = 't'; - return 2; - } else if (c < 8 && !IsOctDigit(next)) { - r[0] = '\\'; - r[1] = OctDigit(c); - return 2; - } else if (!IsHexDigit(next)) { - r[0] = '\\'; - r[1] = 'x'; - r[2] = HexDigit((c & 0xF0) >> 4); - r[3] = HexDigit((c & 0x0F) >> 0); - return 4; - } else { - r[0] = '\\'; - r[1] = OctDigit((c & 0700) >> 6); - r[2] = OctDigit((c & 0070) >> 3); - r[3] = OctDigit((c & 0007) >> 0); - return 4; - } - } - }; - - template <> - struct TEscapeUtil { - static const size_t ESCAPE_C_BUFFER_SIZE = 6; - - template - static inline size_t EscapeC(wchar16 c, TNextChar next, TBufferChar r[ESCAPE_C_BUFFER_SIZE]) { - if (c < 0x100) { - return TEscapeUtil::EscapeC(char(c), next, r); - } else { - r[0] = '\\'; - r[1] = 'u'; - r[2] = HexDigit((c & 0xF000) >> 12); - r[3] = HexDigit((c & 0x0F00) >> 8); - r[4] = HexDigit((c & 0x00F0) >> 4); - r[5] = HexDigit((c & 0x000F) >> 0); - return 6; - } - } - }; -} - -template -std::basic_string& EscapeCImpl(const TChar* str, size_t len, std::basic_string& r) { - using TEscapeUtil = ::TEscapeUtil; - - TChar buffer[TEscapeUtil::ESCAPE_C_BUFFER_SIZE]; - - size_t i, j; - for (i = 0, j = 0; i < len; ++i) { - size_t rlen = TEscapeUtil::EscapeC(str[i], (i + 1 < len ? str[i + 1] : 0), buffer); - - if (rlen > 1) { - r.append(str + j, i - j); - j = i + 1; - r.append(buffer, rlen); - } - } - - if (j > 0) { - r.append(str + j, len - j); - } else { - r.append(str, len); - } - - return r; -} - -template std::string& EscapeCImpl(const std::string::value_type* str, size_t len, std::string& r); -template std::u16string& EscapeCImpl(const std::u16string::value_type* str, size_t len, std::u16string& r); - -std::string& EscapeC(const std::string_view str, std::string& s) { - return EscapeC(str.data(), str.size(), s); -} - -std::u16string& EscapeC(const std::u16string_view str, std::u16string& w) { - return EscapeC(str.data(), str.size(), w); -} - -std::string EscapeC(const std::string& str) { - return EscapeC(str.data(), str.size()); -} - -std::u16string EscapeC(const std::u16string& str) { - return EscapeC(str.data(), str.size()); -} - diff --git a/library/cpp/string_utils/escape/escape.h b/library/cpp/string_utils/escape/escape.h deleted file mode 100644 index 1fa45d841c9..00000000000 --- a/library/cpp/string_utils/escape/escape.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include - -template -std::basic_string& EscapeCImpl(const TChar* str, size_t len, std::basic_string&); - -template -static inline std::basic_string& EscapeC(const TChar* str, size_t len, std::basic_string& s) { - return EscapeCImpl(str, len, s); -} - -template -static inline std::basic_string EscapeC(const TChar* str, size_t len) { - std::basic_string s; - return EscapeC(str, len, s); -} - -template -static inline std::basic_string EscapeC(const std::basic_string_view& str) { - return EscapeC(str.data(), str.size()); -} - -template -static inline std::basic_string EscapeC(TChar ch) { - return EscapeC(&ch, 1); -} - -template -static inline std::basic_string EscapeC(const TChar* str) { - return EscapeC(str, std::char_traits::length(str)); -} - -std::string& EscapeC(const std::string_view str, std::string& res); -std::u16string& EscapeC(const std::u16string_view str, std::u16string& res); - -// these two need to be methods, because of TBasicString::Quote implementation -std::string EscapeC(const std::string& str); -std::u16string EscapeC(const std::u16string& str); - diff --git a/library/cpp/string_utils/escape/escape_ut.cpp b/library/cpp/string_utils/escape/escape_ut.cpp deleted file mode 100644 index cd38ecffd34..00000000000 --- a/library/cpp/string_utils/escape/escape_ut.cpp +++ /dev/null @@ -1,148 +0,0 @@ -#include "escape.h" - -#include - -#include -#include - -using namespace std::string_view_literals; - -namespace { - struct TExample { - TString Expected; - TString Source; - - TExample(const TStringBuf expected, const TStringBuf source) - : Expected{expected} - , Source{source} - { - } - }; -} - -static const TExample CommonTestData[] = { - // Should be valid UTF-8. - {"http://ya.ru/", "http://ya.ru/"}, - {"http://ya.ru/\\x17\\n", "http://ya.ru/\x17\n"}, - - {"http://ya.ru/\\0", "http://ya.ru/\0"sv}, - {"http://ya.ru/\\0\\0", "http://ya.ru/\0\0"sv}, - {"http://ya.ru/\\0\\0000", "http://ya.ru/\0\0" - "0"sv}, - {"http://ya.ru/\\0\\0001", "http://ya.ru/\0\x00" - "1"sv}, - - {R"(\2\4\00678)", "\2\4\6" - "78"sv}, // \6 -> \006 because next char '7' is "octal" - {R"(\2\4\689)", "\2\4\6" - "89"sv}, // \6 -> \6 because next char '8' is not "octal" - - {R"(\"Hello\", Alice said.)", "\"Hello\", Alice said."}, - {"Slash\\\\dash!", "Slash\\dash!"}, - {R"(There\nare\r\nnewlines.)", "There\nare\r\nnewlines."}, - {"There\\tare\\ttabs.", "There\tare\ttabs."}, - - {"There are questions \\x3F\\x3F?", "There are questions ???"}, - {"There are questions \\x3F?", "There are questions ??"}, -}; - -Y_UNIT_TEST_SUITE(TEscapeCTest) { - Y_UNIT_TEST(TestStrokaEscapeC) { - for (const auto& e : CommonTestData) { - TString expected(e.Expected); - TString source(e.Source); - TString actual(EscapeC(e.Source)); - TString actual2(UnescapeC(e.Expected)); - - UNIT_ASSERT_VALUES_EQUAL(e.Expected, actual); - UNIT_ASSERT_VALUES_EQUAL(e.Source, actual2); - } - - UNIT_ASSERT_VALUES_EQUAL("http://ya.ru/\\x17\\n\\xAB", EscapeC(TString("http://ya.ru/\x17\n\xab"))); - UNIT_ASSERT_VALUES_EQUAL("http://ya.ru/\x17\n\xab", UnescapeC(TString("http://ya.ru/\\x17\\n\\xAB"))); - UNIT_ASSERT_VALUES_EQUAL("h", EscapeC('h')); - UNIT_ASSERT_VALUES_EQUAL("h", UnescapeC(TString("h"))); - UNIT_ASSERT_VALUES_EQUAL("\\xFF", EscapeC('\xFF')); - UNIT_ASSERT_VALUES_EQUAL("\xFF", UnescapeC(TString("\\xFF"))); - - UNIT_ASSERT_VALUES_EQUAL("\\377f", EscapeC(TString("\xff" - "f"))); - UNIT_ASSERT_VALUES_EQUAL("\xff" - "f", - UnescapeC(TString("\\377f"))); - UNIT_ASSERT_VALUES_EQUAL("\\xFFg", EscapeC(TString("\xff" - "g"))); - UNIT_ASSERT_VALUES_EQUAL("\xff" - "g", - UnescapeC(TString("\\xFFg"))); - UNIT_ASSERT_VALUES_EQUAL("\xEA\x9A\x96", UnescapeC(TString("\\uA696"))); - UNIT_ASSERT_VALUES_EQUAL("Странный компроматтест", UnescapeC(TString("\\u0421\\u0442\\u0440\\u0430\\u043d\\u043d\\u044b\\u0439 \\u043a\\u043e\\u043c\\u043f\\u0440\\u043e\\u043c\\u0430\\u0442тест"))); - } - - Y_UNIT_TEST(TestWtrokaEscapeC) { - for (const auto& e : CommonTestData) { - TUtf16String expected(UTF8ToWide(e.Expected)); - TUtf16String source(UTF8ToWide(e.Source)); - TUtf16String actual(EscapeC(source)); - TUtf16String actual2(UnescapeC(expected)); - - UNIT_ASSERT_VALUES_EQUAL(expected, actual); - UNIT_ASSERT_VALUES_EQUAL(source, actual2); - } - - UNIT_ASSERT_VALUES_EQUAL(u"http://ya.ru/\\x17\\n\\u1234", EscapeC(u"http://ya.ru/\x17\n\u1234")); - UNIT_ASSERT_VALUES_EQUAL(u"h", EscapeC(u'h')); - UNIT_ASSERT_VALUES_EQUAL(u"\\xFF", EscapeC(wchar16(255))); - } - - Y_UNIT_TEST(TestEscapeTrigraphs) { - UNIT_ASSERT_VALUES_EQUAL("?", EscapeC(TString("?"))); - UNIT_ASSERT_VALUES_EQUAL("\\x3F?", EscapeC(TString("??"))); - UNIT_ASSERT_VALUES_EQUAL("\\x3F\\x3F?", EscapeC(TString("???"))); - // ok but may cause warning about trigraphs - // UNIT_ASSERT_VALUES_EQUAL("[x]?z", EscapeC(TString("??(x??)?z"))); - UNIT_ASSERT_VALUES_EQUAL("\\x3F?x\\x3F\\x3F?z", EscapeC(TString("??x???z"))); - } - - Y_UNIT_TEST(TestUnescapeCCharLen) { - auto test = [](const char* str, size_t len) { - UNIT_ASSERT_EQUAL(UnescapeCCharLen(str, str + strlen(str)), len); - }; - - test("", 0); - test("abc", 1); - test("\\", 1); - test("\\\\", 2); - test("\\#", 2); - test("\\n10", 2); - test("\\r\\n", 2); - test("\\x05abc", 4); - test("\\u11117777", 6); - test("\\u123yyy", 2); - test("\\U11117777cccc", 10); - test("\\U111yyy", 2); - test("\\0\\1", 2); - test("\\01\\1", 3); - test("\\012\\1", 4); - test("\\0123\\1", 4); - test("\\4\\1", 2); - test("\\40\\1", 3); - test("\\400\\1", 3); - test("\\4xxx", 2); - } - - Y_UNIT_TEST(TestUnbounded) { - char buf[100000]; - - for (const auto& x : CommonTestData) { - char* end = UnescapeC(x.Expected.data(), x.Expected.size(), buf); - - UNIT_ASSERT_VALUES_EQUAL(x.Source, TStringBuf(buf, end)); - } - } - - Y_UNIT_TEST(TestCapitalUEscapes) { - UNIT_ASSERT_VALUES_EQUAL(UnescapeC("\\U00000020"), " "); - UNIT_ASSERT_VALUES_EQUAL(UnescapeC("\\Uxxx"), "Uxxx"); - } -} diff --git a/library/cpp/string_utils/helpers/CMakeLists.txt b/library/cpp/string_utils/helpers/CMakeLists.txt new file mode 100644 index 00000000000..8beb9865850 --- /dev/null +++ b/library/cpp/string_utils/helpers/CMakeLists.txt @@ -0,0 +1,15 @@ +add_library(cpp-string_utils-helpers) + +target_link_libraries(cpp-string_utils-helpers PUBLIC + contrib-libs-cxxsupp +) + +if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT HAVE_CUDA) + target_link_libraries(cpp-string_utils-helpers PUBLIC + contrib-libs-linux-headers + ) +endif() + +target_sources(cpp-string_utils-helpers PRIVATE + ${CMAKE_SOURCE_DIR}/library/cpp/string_utils/helpers/helpers.cpp +) diff --git a/library/cpp/string_utils/misc/misc.cpp b/library/cpp/string_utils/helpers/helpers.cpp similarity index 84% rename from library/cpp/string_utils/misc/misc.cpp rename to library/cpp/string_utils/helpers/helpers.cpp index e3184545ed9..bca1872e03b 100644 --- a/library/cpp/string_utils/misc/misc.cpp +++ b/library/cpp/string_utils/helpers/helpers.cpp @@ -1,11 +1,7 @@ -#include "misc.h" - -#include -#include +#include "helpers.h" namespace NUtils { - -namespace { + namespace { void DoSplit(std::string_view src, std::string_view& l, std::string_view& r, size_t pos, size_t len) { const auto right = src.substr(pos + len); // in case if (&l == &src) l = src.substr(0, pos); @@ -33,10 +29,6 @@ std::string ToTitle(const std::string& s) { return res; } -std::string Quote(std::string_view s) { - return TYdbStringBuilder() << "\"" << EscapeC(s) << "\""; -} - void RemoveAll(std::string& str, char ch) { size_t pos = str.find(ch); // 'find' to avoid cloning of string in 'TString.begin()' if (pos == std::string::npos) @@ -150,4 +142,39 @@ std::string_view Before(std::string_view src, char c) { return TrySplit(src, l, r, c) ? l : src; } -} \ No newline at end of file +size_t SumLength() noexcept { + return 0; +} + +void CopyAll(char*) noexcept { +} + +template <> +std::u16string FromAscii(const ::std::string_view& s) { + std::u16string res; + res.resize(s.size()); + + auto dst = res.begin(); + + for (const char* src = s.data(); dst != res.end(); ++dst, ++src) { + *dst = static_cast(*src); + } + + return res; +} + +template <> +std::u32string FromAscii(const ::std::string_view& s) { + std::u32string res; + res.resize(s.size()); + + auto dst = res.begin(); + + for (const char* src = s.data(); dst != res.end(); ++dst, ++src) { + *dst = static_cast(*src); + } + + return res; +} + +} diff --git a/library/cpp/string_utils/helpers/helpers.h b/library/cpp/string_utils/helpers/helpers.h new file mode 100644 index 00000000000..92bea0ef476 --- /dev/null +++ b/library/cpp/string_utils/helpers/helpers.h @@ -0,0 +1,64 @@ +#pragma once + +#include + +namespace NUtils { + +std::string ToLower(const std::string& str); +void ToLower(std::string& str); + +std::string ToTitle(const std::string& s); + +void RemoveAll(std::string& str, char ch); + +bool TrySplitOn(std::string_view src, std::string_view& l, std::string_view& r, size_t pos, size_t len); + +bool TrySplit(std::string_view src, std::string_view& l, std::string_view& r, char delim); +bool TrySplit(std::string_view src, std::string_view& l, std::string_view& r, std::string_view delim); +bool TryRSplit(std::string_view src, std::string_view& l, std::string_view& r, char delim); +bool TryRSplit(std::string_view src, std::string_view& l, std::string_view& r, std::string_view delim); + +void Split(std::string_view src, std::string_view& l, std::string_view& r, char delim); +void Split(std::string_view src, std::string_view& l, std::string_view& r, std::string_view delim); +void RSplit(std::string_view src, std::string_view& l, std::string_view& r, char delim); +void RSplit(std::string_view src, std::string_view& l, std::string_view& r, std::string_view delim); + +std::string_view NextTok(std::string_view& src, char delim); +std::string_view NextTok(std::string_view& src, std::string_view delim); +bool NextTok(std::string_view& src, std::string_view& tok, char delim); +bool NextTok(std::string_view& src, std::string_view& tok, std::string_view delim); + +std::string_view RNextTok(std::string_view& src, char delim); +std::string_view RNextTok(std::string_view& src, std::string_view delim); + +std::string_view After(std::string_view src, char c); +std::string_view Before(std::string_view src, char c); + +size_t SumLength() noexcept; + +template +size_t SumLength(const std::string_view s1, const R&... r) noexcept { + return s1.size() + SumLength(r...); +} + +void CopyAll(char*) noexcept; + +template +void CopyAll(char* p, const std::string_view s, const R&... r) { + std::string::traits_type::copy(p, s.data(), s.size()); + CopyAll(p + s.size(), r...); +} + +template +std::string Join(const R&... r) { + std::string s(SumLength(r...), '\0'); + + CopyAll((char*)s.data(), r...); + + return s; +} + +template +std::basic_string FromAscii(const std::string_view& s); + +} \ No newline at end of file diff --git a/library/cpp/string_utils/misc/CMakeLists.txt b/library/cpp/string_utils/misc/CMakeLists.txt index f1b7c3ddca6..e216f93ebf9 100644 --- a/library/cpp/string_utils/misc/CMakeLists.txt +++ b/library/cpp/string_utils/misc/CMakeLists.txt @@ -1,19 +1,13 @@ -add_library(cpp-string_utils-misc) +add_library(cpp-string_utils-misc INTERFACE) -target_link_libraries(cpp-string_utils-misc PUBLIC +target_link_libraries(cpp-string_utils-misc INTERFACE contrib-libs-cxxsupp yutil - cpp-string_utils-escape - cpp-string_builder - yutil + cpp-string_utils-helpers ) if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT HAVE_CUDA) - target_link_libraries(cpp-string_utils-misc PUBLIC + target_link_libraries(cpp-string_utils-misc INTERFACE contrib-libs-linux-headers ) endif() - -target_sources(cpp-string_utils-misc PRIVATE - ${CMAKE_SOURCE_DIR}/library/cpp/string_utils/misc/misc.cpp -) diff --git a/library/cpp/string_utils/misc/misc.h b/library/cpp/string_utils/misc/misc.h index 0d0b6c187cc..6ec6a3774a6 100644 --- a/library/cpp/string_utils/misc/misc.h +++ b/library/cpp/string_utils/misc/misc.h @@ -1,44 +1,13 @@ #pragma once -#include -#include +#include #include #include -namespace NUtils { - -std::string ToLower(const std::string& str); -void ToLower(std::string& str); - -std::string ToTitle(const std::string& s); - -void RemoveAll(std::string& str, char ch); - -std::string Quote(std::string_view s); - -bool TrySplitOn(std::string_view src, std::string_view& l, std::string_view& r, size_t pos, size_t len); - -bool TrySplit(std::string_view src, std::string_view& l, std::string_view& r, char delim); -bool TrySplit(std::string_view src, std::string_view& l, std::string_view& r, std::string_view delim); -bool TryRSplit(std::string_view src, std::string_view& l, std::string_view& r, char delim); -bool TryRSplit(std::string_view src, std::string_view& l, std::string_view& r, std::string_view delim); - -void Split(std::string_view src, std::string_view& l, std::string_view& r, char delim); -void Split(std::string_view src, std::string_view& l, std::string_view& r, std::string_view delim); -void RSplit(std::string_view src, std::string_view& l, std::string_view& r, char delim); -void RSplit(std::string_view src, std::string_view& l, std::string_view& r, std::string_view delim); - -std::string_view NextTok(std::string_view& src, char delim); -std::string_view NextTok(std::string_view& src, std::string_view delim); -bool NextTok(std::string_view& src, std::string_view& tok, char delim); -bool NextTok(std::string_view& src, std::string_view& tok, std::string_view delim); - -std::string_view RNextTok(std::string_view& src, char delim); -std::string_view RNextTok(std::string_view& src, std::string_view delim); +#include -std::string_view After(std::string_view src, char c); -std::string_view Before(std::string_view src, char c); +namespace NUtils { template bool ContainerTransform(TContainer& str, T&& f, size_t pos = 0, size_t n = TContainer::npos) { diff --git a/library/cpp/string_utils/relaxed_escaper/relaxed_escaper.h b/library/cpp/string_utils/relaxed_escaper/relaxed_escaper.h index f474ae8e52d..a4d4252f857 100644 --- a/library/cpp/string_utils/relaxed_escaper/relaxed_escaper.h +++ b/library/cpp/string_utils/relaxed_escaper/relaxed_escaper.h @@ -3,7 +3,7 @@ #include #include #include -#include +#include namespace NEscJ { // almost copypaste from util/string/escape.h diff --git a/library/cpp/string_utils/scan/scan.h b/library/cpp/string_utils/scan/scan.h index 65448fee5e1..6f7db43ef46 100644 --- a/library/cpp/string_utils/scan/scan.h +++ b/library/cpp/string_utils/scan/scan.h @@ -1,11 +1,10 @@ #pragma once -#include +#include template static inline void ScanKeyValue(std::string_view s, F&& f) { std::string_view key, val; - TStringBuf().NextTok(' '); while (!s.empty()) { auto splitPos = s.find(sep); diff --git a/library/cpp/string_utils/string_output/CMakeLists.txt b/library/cpp/string_utils/string_output/CMakeLists.txt deleted file mode 100644 index 7b668ac4727..00000000000 --- a/library/cpp/string_utils/string_output/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -add_library(cpp-string_utils-string_output) - -target_link_libraries(cpp-string_utils-string_output PUBLIC - contrib-libs-cxxsupp - yutil -) - -target_sources(cpp-string_utils-string_output PRIVATE - ${CMAKE_SOURCE_DIR}/library/cpp/string_utils/string_output/string_output.cpp -) - -if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT HAVE_CUDA) - target_link_libraries(cpp-string_utils-string_output PUBLIC - contrib-libs-linux-headers - ) -endif() \ No newline at end of file diff --git a/library/cpp/string_utils/string_output/string_output.cpp b/library/cpp/string_utils/string_output/string_output.cpp deleted file mode 100644 index 0c88a157051..00000000000 --- a/library/cpp/string_utils/string_output/string_output.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "string_output.h" - -#include - -namespace NUtils { - -inline void TStringOutput::Reserve(size_t size) { - S_->reserve(S_->size() + size); -} - -inline void TStringOutput::Undo(size_t len) { - Y_ABORT_UNLESS(len <= S_->size(), "trying to undo more bytes than actually written"); - S_->resize(S_->size() - len); -} - -void TStringOutput::DoWrite(const void* buf, size_t len) { - S_->append((const char*)buf, len); -} - -void TStringOutput::DoWriteC(char c) { - S_->push_back(c); -} - -} \ No newline at end of file diff --git a/library/cpp/string_utils/string_output/string_output.h b/library/cpp/string_utils/string_output/string_output.h deleted file mode 100644 index 5be5aabf127..00000000000 --- a/library/cpp/string_utils/string_output/string_output.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include - -#include - -namespace NUtils { - -class TStringOutput: public IOutputStream { -public: - inline TStringOutput(std::string& s) noexcept - : S_(&s) - { - } - - inline void Reserve(size_t size); - inline void Undo(size_t len); - -private: - void DoWrite(const void* buf, size_t len) override; - void DoWriteC(char c) override; - - std::string* S_; -}; - -} \ No newline at end of file diff --git a/library/cpp/string_utils/url/CMakeLists.txt b/library/cpp/string_utils/url/CMakeLists.txt index 6339dfb0838..02d44cc4fcc 100644 --- a/library/cpp/string_utils/url/CMakeLists.txt +++ b/library/cpp/string_utils/url/CMakeLists.txt @@ -4,7 +4,6 @@ target_link_libraries(cpp-string_utils-url PUBLIC contrib-libs-cxxsupp yutil cpp-string_utils-misc - cpp-string_builder ) target_sources(cpp-string_utils-url PRIVATE diff --git a/library/cpp/string_utils/url/url.cpp b/library/cpp/string_utils/url/url.cpp index 7249c6e299e..e293e3193f5 100644 --- a/library/cpp/string_utils/url/url.cpp +++ b/library/cpp/string_utils/url/url.cpp @@ -1,6 +1,6 @@ #include "url.h" -#include +#include #include #include @@ -90,7 +90,7 @@ size_t GetHttpPrefixSize(const std::string_view url, bool ignorehttps) noexcept return GetHttpPrefixSizeImpl(url.data(), TKnownSize(url.size()), ignorehttps); } -size_t GetHttpPrefixSize(const TWtringBuf url, bool ignorehttps) noexcept { +size_t GetHttpPrefixSize(const std::u16string_view url, bool ignorehttps) noexcept { return GetHttpPrefixSizeImpl(url.data(), TKnownSize(url.size()), ignorehttps); } @@ -98,7 +98,7 @@ std::string_view CutHttpPrefix(const std::string_view url, bool ignorehttps) noe return CutHttpPrefixImpl(url, ignorehttps); } -TWtringBuf CutHttpPrefix(const TWtringBuf url, bool ignorehttps) noexcept { +std::u16string_view CutHttpPrefix(const std::u16string_view url, bool ignorehttps) noexcept { return CutHttpPrefixImpl(url, ignorehttps); } @@ -349,7 +349,7 @@ std::string AddSchemePrefix(const std::string& url, std::string_view scheme) { return url; } - return NUtils::TYdbStringBuilder() << scheme << std::string_view("://") << url; + return TStringBuilder() << scheme << std::string_view("://") << url; } #define X(c) (c >= 'A' ? ((c & 0xdf) - 'A') + 10 : (c - '0')) diff --git a/library/cpp/string_utils/url/url.h b/library/cpp/string_utils/url/url.h index 6707b623b4d..71e144a8856 100644 --- a/library/cpp/string_utils/url/url.h +++ b/library/cpp/string_utils/url/url.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include namespace NUrl { @@ -32,7 +32,7 @@ Y_PURE_FUNCTION size_t GetHttpPrefixSize(const std::string_view url, bool ignorehttps = false) noexcept; Y_PURE_FUNCTION -size_t GetHttpPrefixSize(const TWtringBuf url, bool ignorehttps = false) noexcept; +size_t GetHttpPrefixSize(const std::u16string_view url, bool ignorehttps = false) noexcept; /** BEWARE of std::string_view! You can not use operator ~ or c_str() like in std::string !!!!!!!!!!!! */ @@ -51,7 +51,7 @@ Y_PURE_FUNCTION std::string_view CutHttpPrefix(const std::string_view url, bool ignorehttps = false) noexcept; Y_PURE_FUNCTION -TWtringBuf CutHttpPrefix(const TWtringBuf url, bool ignorehttps = false) noexcept; +std::u16string_view CutHttpPrefix(const std::u16string_view url, bool ignorehttps = false) noexcept; Y_PURE_FUNCTION std::string_view CutSchemePrefix(const std::string_view url) noexcept; diff --git a/library/cpp/svnversion/svnversion.cpp b/library/cpp/svnversion/svnversion.cpp index 84a6156f569..f0f70538183 100644 --- a/library/cpp/svnversion/svnversion.cpp +++ b/library/cpp/svnversion/svnversion.cpp @@ -6,7 +6,7 @@ #include "svnversion.h" #include -#include +#include extern "C" void PrintProgramSvnVersion() { puts(GetProgramSvnVersion()); diff --git a/library/cpp/terminate_handler/terminate_handler.cpp b/library/cpp/terminate_handler/terminate_handler.cpp index d7e8fbed95f..d159c9a5ca2 100644 --- a/library/cpp/terminate_handler/terminate_handler.cpp +++ b/library/cpp/terminate_handler/terminate_handler.cpp @@ -5,6 +5,8 @@ #include #include +#include + namespace { // Avoid infinite recursion if std::terminate is triggered anew by the // FancyTerminateHandler. diff --git a/library/cpp/testing/common/env.cpp b/library/cpp/testing/common/env.cpp index 9912f97d604..038ad3ec644 100644 --- a/library/cpp/testing/common/env.cpp +++ b/library/cpp/testing/common/env.cpp @@ -16,6 +16,8 @@ #include #include +#include + std::string ArcadiaSourceRoot() { if (const auto& sourceRoot = NPrivate::GetTestEnv().SourceRoot) { return sourceRoot; @@ -116,7 +118,7 @@ const std::string& GetGlobalResource(std::string_view name) { void AddEntryToCoreSearchFile(const std::string& filename, std::string_view cmd, int pid, const TFsPath& binaryPath = TFsPath(), const TFsPath& cwd = TFsPath()) { auto lock = TFileLock(filename); - TGuard guard(lock); + std::lock_guard guard(lock); TOFStream output(TFile(filename, WrOnly | ForAppend | OpenAlways)); diff --git a/library/cpp/testing/common/env.h b/library/cpp/testing/common/env.h index c43f9c2d7a9..f357d7aaf6e 100644 --- a/library/cpp/testing/common/env.h +++ b/library/cpp/testing/common/env.h @@ -3,8 +3,8 @@ #include #include -#include -#include +#include +#include #include // @brief return full path to arcadia root diff --git a/library/cpp/testing/common/scope.h b/library/cpp/testing/common/scope.h index 1611caca4e3..94237884572 100644 --- a/library/cpp/testing/common/scope.h +++ b/library/cpp/testing/common/scope.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/library/cpp/testing/gmock_in_unittest/events.cpp b/library/cpp/testing/gmock_in_unittest/events.cpp index e5789fb3954..fccd975fdb0 100644 --- a/library/cpp/testing/gmock_in_unittest/events.cpp +++ b/library/cpp/testing/gmock_in_unittest/events.cpp @@ -2,8 +2,8 @@ #include -#include -#include +#include +#include #include void TGMockTestEventListener::OnTestPartResult(const testing::TestPartResult& result) { diff --git a/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp b/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp index d901b7e5e27..87c8df9f6e8 100644 --- a/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp +++ b/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp @@ -2,7 +2,7 @@ #include -#include +#include // Set this variable to true if you want to see failures ///////////////////////////////////////////////////////// diff --git a/library/cpp/testing/gtest/matchers.h b/library/cpp/testing/gtest/matchers.h index 4c25a8dd9ab..d42714c33ac 100644 --- a/library/cpp/testing/gtest/matchers.h +++ b/library/cpp/testing/gtest/matchers.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/library/cpp/testing/gtest_extensions/assertions.h b/library/cpp/testing/gtest_extensions/assertions.h index 27934125ca0..8fbcd888b72 100644 --- a/library/cpp/testing/gtest_extensions/assertions.h +++ b/library/cpp/testing/gtest_extensions/assertions.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include diff --git a/library/cpp/testing/gtest_extensions/matchers.h b/library/cpp/testing/gtest_extensions/matchers.h index d727485827c..61a3a3963b8 100644 --- a/library/cpp/testing/gtest_extensions/matchers.h +++ b/library/cpp/testing/gtest_extensions/matchers.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -10,29 +10,29 @@ namespace testing { * When matching `const std::string_view&`, implicitly convert other strings and string views to `Eq` matchers. */ template - class Matcher&>: public internal::MatcherBase&> { + class Matcher&>: public internal::MatcherBase&> { public: Matcher() { } - explicit Matcher(const MatcherInterface&>* impl) - : internal::MatcherBase&>(impl) { + explicit Matcher(const MatcherInterface&>* impl) + : internal::MatcherBase&>(impl) { } template ::type::is_gtest_matcher> Matcher(M&& m) - : internal::MatcherBase&>(std::forward(m)) { + : internal::MatcherBase&>(std::forward(m)) { } Matcher(const TBasicString& s) { - *this = Eq(TBasicStringBuf(s)); + *this = Eq(std::basic_string_view(s)); } Matcher(const T* s) { - *this = Eq(TBasicStringBuf(s)); + *this = Eq(std::basic_string_view(s)); } - Matcher(TBasicStringBuf s) { + Matcher(std::basic_string_view s) { *this = Eq(s); } }; @@ -41,22 +41,22 @@ namespace testing { * When matching `TBasicBuf`, implicitly convert other strings and string views to `Eq` matchers. */ template - class Matcher>: public internal::MatcherBase> { + class Matcher>: public internal::MatcherBase> { public: Matcher() { } - explicit Matcher(const MatcherInterface >* impl) - : internal::MatcherBase>(impl) { + explicit Matcher(const MatcherInterface >* impl) + : internal::MatcherBase>(impl) { } - explicit Matcher(const MatcherInterface&>* impl) - : internal::MatcherBase>(impl) { + explicit Matcher(const MatcherInterface&>* impl) + : internal::MatcherBase>(impl) { } template ::type::is_gtest_matcher> Matcher(M&& m) - : internal::MatcherBase>(std::forward(m)) { + : internal::MatcherBase>(std::forward(m)) { } Matcher(const TBasicString& s) { @@ -67,7 +67,7 @@ namespace testing { *this = Eq(TBasicString(s)); } - Matcher(TBasicStringBuf s) { + Matcher(std::basic_string_view s) { *this = Eq(s); } }; diff --git a/library/cpp/testing/gtest_extensions/pretty_printers.h b/library/cpp/testing/gtest_extensions/pretty_printers.h index 29608ee6248..2ccfc8bcc9d 100644 --- a/library/cpp/testing/gtest_extensions/pretty_printers.h +++ b/library/cpp/testing/gtest_extensions/pretty_printers.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include #include @@ -52,10 +52,10 @@ void PrintTo(const TBasicString& value, std::ostream* st *stream << value.Quote().c_str(); } -template -void PrintTo(TBasicStringBuf value, std::ostream* stream) { - *stream << TBasicString{value}.Quote().c_str(); -} +// template +// void PrintTo(TBasicStringBuf value, std::ostream* stream) { +// *stream << TBasicString{value}.Quote().c_str(); +// } template void PrintTo(const std::optional& value, std::ostream* stream) { diff --git a/library/cpp/testing/gtest_extensions/ut/gtest_extensions_ut.cpp b/library/cpp/testing/gtest_extensions/ut/gtest_extensions_ut.cpp index 06c0bd2ecf3..2d0f9c5213c 100644 --- a/library/cpp/testing/gtest_extensions/ut/gtest_extensions_ut.cpp +++ b/library/cpp/testing/gtest_extensions/ut/gtest_extensions_ut.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include #include diff --git a/library/cpp/testing/unittest/checks.cpp b/library/cpp/testing/unittest/checks.cpp index 3cf59a6cb83..4f38e3c0b40 100644 --- a/library/cpp/testing/unittest/checks.cpp +++ b/library/cpp/testing/unittest/checks.cpp @@ -1,4 +1,4 @@ -#include +#include #include bool CheckExceptionMessage(const char* msg, std::string& err) { diff --git a/library/cpp/testing/unittest/registar.cpp b/library/cpp/testing/unittest/registar.cpp index 8f362427787..b0e933f87e0 100644 --- a/library/cpp/testing/unittest/registar.cpp +++ b/library/cpp/testing/unittest/registar.cpp @@ -12,6 +12,8 @@ #include #include +#include + bool NUnitTest::ShouldColorizeDiff = true; std::string NUnitTest::RandomString(size_t len, ui32 seed) { @@ -143,7 +145,7 @@ std::string NUnitTest::GetResetTag() { } std::string NUnitTest::ColoredDiff(std::string_view s1, std::string_view s2, const std::string& delims, bool reverse) { - std::stringStream res; + TStringStream res; std::vector> chunks; NDiff::InlineDiff(chunks, s1, s2, delims); if (NUnitTest::ShouldColorizeDiff) { @@ -326,9 +328,8 @@ void NUnitTest::TTestBase::AddError(const char* msg, TTestContext* context) { } void NUnitTest::TTestBase::RunAfterTest(std::function f) { - with_lock (AfterTestFunctionsLock_) { - AfterTestFunctions_.emplace_back(std::move(f)); - } + std::lock_guard guard(AfterTestFunctionsLock_); + AfterTestFunctions_.emplace_back(std::move(f)); } bool NUnitTest::TTestBase::CheckAccessTest(const char* test) { @@ -382,7 +383,8 @@ void NUnitTest::TTestBase::AfterTest() { TearDown(); std::vector> afterTestFunctions; - with_lock (AfterTestFunctionsLock_) { + { + std::lock_guard guard(AfterTestFunctionsLock_); afterTestFunctions.swap(AfterTestFunctions_); } diff --git a/library/cpp/testing/unittest/tests_data.cpp b/library/cpp/testing/unittest/tests_data.cpp index 43cd211dcdb..f58e069f4ac 100644 --- a/library/cpp/testing/unittest/tests_data.cpp +++ b/library/cpp/testing/unittest/tests_data.cpp @@ -4,7 +4,8 @@ #include #include -#include + +#include class TPortManager::TPortManagerImpl { public: @@ -22,7 +23,7 @@ class TPortManager::TPortManagerImpl { TAtomicSharedPtr holder(NTesting::GetFreePort().Release()); ReservePortForCurrentTest(holder); - TGuard g(Lock); + std::lock_guard g(Lock); ReservedPorts.push_back(holder); return holder->Get(); } @@ -43,7 +44,7 @@ class TPortManager::TPortManagerImpl { Y_UNUSED(startPort); auto ports = NTesting::NLegacy::GetFreePortsRange(range); ui16 first = ports[0]; - TGuard g(Lock); + std::lock_guard g(Lock); for (auto& port : ports) { ReservedPorts.emplace_back(port.Release()); ReservePortForCurrentTest(ReservedPorts.back()); @@ -64,7 +65,7 @@ class TPortManager::TPortManagerImpl { } private: - TMutex Lock; + std::mutex Lock; std::vector> ReservedPorts; const bool EnableReservePortsForCurrentTest; const bool DisableRandomPorts; diff --git a/library/cpp/testing/unittest/tests_data.h b/library/cpp/testing/unittest/tests_data.h index 6536bc1ae69..206186333ea 100644 --- a/library/cpp/testing/unittest/tests_data.h +++ b/library/cpp/testing/unittest/tests_data.h @@ -4,7 +4,7 @@ #include #include -#include +#include #include class TInet6StreamSocket; diff --git a/library/cpp/testing/unittest/utmain.cpp b/library/cpp/testing/unittest/utmain.cpp index 0443db5941e..e4f13fa2530 100644 --- a/library/cpp/testing/unittest/utmain.cpp +++ b/library/cpp/testing/unittest/utmain.cpp @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include diff --git a/library/cpp/threading/chunk_queue/queue.h b/library/cpp/threading/chunk_queue/queue.h index 5b5a1ae2047..d5d60ce4f83 100644 --- a/library/cpp/threading/chunk_queue/queue.h +++ b/library/cpp/threading/chunk_queue/queue.h @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -358,21 +359,18 @@ namespace NThreading { template void Enqueue(TT&& value) { - with_lock (WriteLock) { - Queue.Enqueue(std::forward(value)); - } + std::lock_guard guard(WriteLock); + Queue.Enqueue(std::forward(value)); } bool Dequeue(T& value) { - with_lock (ReadLock) { - return Queue.Dequeue(value); - } + std::lock_guard guard(ReadLock); + return Queue.Dequeue(value); } bool IsEmpty() { - with_lock (ReadLock) { - return Queue.IsEmpty(); - } + std::lock_guard guard(ReadLock); + return Queue.IsEmpty(); } }; diff --git a/library/cpp/threading/equeue/fast/equeue.h b/library/cpp/threading/equeue/fast/equeue.h deleted file mode 100644 index 195628d6416..00000000000 --- a/library/cpp/threading/equeue/fast/equeue.h +++ /dev/null @@ -1,151 +0,0 @@ -#pragma once - -#include - -#include -#include -#include - -#include -#include - -#include -#include - -class TFastElasticQueue - : public TThreadPoolBase - , private IThreadFactory::IThreadAble -{ -public: - explicit TFastElasticQueue(const TParams& params = {}) - : TThreadPoolBase(params) - { - Y_ENSURE(!params.Blocking_); - } - - ~TFastElasticQueue() { - Stop(); - } - - void Start(size_t threadCount, size_t maxQueueSize) override { - Y_ENSURE(Threads_.empty()); - Y_ENSURE(maxQueueSize > 0); - - Queue_.Reset(new NThreading::TBoundedQueue(FastClp2(maxQueueSize + threadCount))); //threadCount is for stop events - MaxQueueSize_ = maxQueueSize; - - try { - for (size_t i = 0; i < threadCount; ++i) { - Threads_.push_back(Pool()->Run(this)); - } - } catch (...) { - Stop(); - throw; - } - - Stopped_ = false; - } - - size_t ObjectCount() const { - //GuardCount_ can be temporary incremented above real object count in queue - return Min(GuardCount_.load(), MaxQueueSize_); - } - - bool Add(IObjectInQueue* obj) override Y_WARN_UNUSED_RESULT { - if (Stopped_ || !obj) { - return false; - } - - if (GuardCount_.fetch_add(1) >= MaxQueueSize_) { - GuardCount_.fetch_sub(1); - return false; - } - - QueueSize_.fetch_add(1); - - if (!Queue_->Enqueue(obj)) { - //Simultaneous Dequeue calls can return not in exact fifo order of items, - //so there can be GuardCount_ < MaxQueueSize_ but Enqueue will fail because of - //the oldest enqueued item is not actually dequeued and ring buffer can't proceed. - GuardCount_.fetch_sub(1); - QueueSize_.fetch_sub(1); - return false; - } - - - Event_.NotifyOne(); - - return true; - } - - size_t Size() const noexcept override { - return QueueSize_.load(); - } - - void Stop() noexcept override { - Stopped_ = true; - - for (size_t i = 0; i < Threads_.size(); ++i) { - while (!Queue_->Enqueue(nullptr)) { - Sleep(TDuration::MilliSeconds(1)); - } - - Event_.NotifyOne(); - } - - while (!Threads_.empty()) { - Threads_.back()->Join(); - Threads_.pop_back(); - } - - Queue_.Reset(); - } - - void DoExecute() override { - TThread::SetCurrentThreadName(Params.ThreadName_.c_str()); - - while (true) { - IObjectInQueue* job = nullptr; - - Event_.Await([&]() { - return Queue_->Dequeue(job); - }); - - if (!job) { - break; - } - - QueueSize_.fetch_sub(1); - - Y_DEFER { - GuardCount_.fetch_sub(1); - }; - - if (Params.Catching_) { - try { - try { - job->Process(nullptr); - } catch (...) { - Cdbg << "[mtp queue] " << CurrentExceptionMessage() << Endl; - } - } catch (...) { - ; - } - } else { - job->Process(nullptr); - } - } - } -private: - std::atomic Stopped_ = false; - size_t MaxQueueSize_ = 0; - - alignas(64) std::atomic GuardCount_ = 0; - alignas(64) std::atomic QueueSize_ = 0; - - std::vector> Threads_; - - THolder> Queue_; - NYT::NThreading::TEventCount Event_; -}; - diff --git a/library/cpp/threading/equeue/fast/ya.make b/library/cpp/threading/equeue/fast/ya.make deleted file mode 100644 index 4a93a6b5ba5..00000000000 --- a/library/cpp/threading/equeue/fast/ya.make +++ /dev/null @@ -1,12 +0,0 @@ -LIBRARY() - -SRCS( - equeue.h -) - -PEERDIR( - library/cpp/threading/bounded_queue - library/cpp/yt/threading -) - -END() diff --git a/library/cpp/threading/future/CMakeLists.darwin-arm64.txt b/library/cpp/threading/future/CMakeLists.darwin-arm64.txt deleted file mode 100644 index 66d221dbbb4..00000000000 --- a/library/cpp/threading/future/CMakeLists.darwin-arm64.txt +++ /dev/null @@ -1,25 +0,0 @@ - -# This file was generated by the build system used internally in the Yandex monorepo. -# Only simple modifications are allowed (adding source-files to targets, adding simple properties -# like target_include_directories). These modifications will be ported to original -# ya.make files by maintainers. Any complex modifications which can't be ported back to the -# original buildsystem will not be accepted. - - - -add_library(cpp-threading-future) -target_link_libraries(cpp-threading-future PUBLIC - contrib-libs-cxxsupp - yutil -) -target_sources(cpp-threading-future PRIVATE - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/async_semaphore.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/async.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/core/future.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/core/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait_group.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait_policy.cpp -) diff --git a/library/cpp/threading/future/CMakeLists.darwin-x86_64.txt b/library/cpp/threading/future/CMakeLists.darwin-x86_64.txt deleted file mode 100644 index 66d221dbbb4..00000000000 --- a/library/cpp/threading/future/CMakeLists.darwin-x86_64.txt +++ /dev/null @@ -1,25 +0,0 @@ - -# This file was generated by the build system used internally in the Yandex monorepo. -# Only simple modifications are allowed (adding source-files to targets, adding simple properties -# like target_include_directories). These modifications will be ported to original -# ya.make files by maintainers. Any complex modifications which can't be ported back to the -# original buildsystem will not be accepted. - - - -add_library(cpp-threading-future) -target_link_libraries(cpp-threading-future PUBLIC - contrib-libs-cxxsupp - yutil -) -target_sources(cpp-threading-future PRIVATE - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/async_semaphore.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/async.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/core/future.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/core/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait_group.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait_policy.cpp -) diff --git a/library/cpp/threading/future/CMakeLists.linux-aarch64.txt b/library/cpp/threading/future/CMakeLists.linux-aarch64.txt deleted file mode 100644 index ae643a1bc46..00000000000 --- a/library/cpp/threading/future/CMakeLists.linux-aarch64.txt +++ /dev/null @@ -1,26 +0,0 @@ - -# This file was generated by the build system used internally in the Yandex monorepo. -# Only simple modifications are allowed (adding source-files to targets, adding simple properties -# like target_include_directories). These modifications will be ported to original -# ya.make files by maintainers. Any complex modifications which can't be ported back to the -# original buildsystem will not be accepted. - - - -add_library(cpp-threading-future) -target_link_libraries(cpp-threading-future PUBLIC - contrib-libs-linux-headers - contrib-libs-cxxsupp - yutil -) -target_sources(cpp-threading-future PRIVATE - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/async_semaphore.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/async.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/core/future.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/core/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait_group.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait_policy.cpp -) diff --git a/library/cpp/threading/future/CMakeLists.linux-x86_64.txt b/library/cpp/threading/future/CMakeLists.linux-x86_64.txt deleted file mode 100644 index ae643a1bc46..00000000000 --- a/library/cpp/threading/future/CMakeLists.linux-x86_64.txt +++ /dev/null @@ -1,26 +0,0 @@ - -# This file was generated by the build system used internally in the Yandex monorepo. -# Only simple modifications are allowed (adding source-files to targets, adding simple properties -# like target_include_directories). These modifications will be ported to original -# ya.make files by maintainers. Any complex modifications which can't be ported back to the -# original buildsystem will not be accepted. - - - -add_library(cpp-threading-future) -target_link_libraries(cpp-threading-future PUBLIC - contrib-libs-linux-headers - contrib-libs-cxxsupp - yutil -) -target_sources(cpp-threading-future PRIVATE - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/async_semaphore.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/async.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/core/future.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/core/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait_group.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait_policy.cpp -) diff --git a/library/cpp/threading/future/CMakeLists.txt b/library/cpp/threading/future/CMakeLists.txt index d863ebd1806..9529745fb74 100644 --- a/library/cpp/threading/future/CMakeLists.txt +++ b/library/cpp/threading/future/CMakeLists.txt @@ -1,19 +1,23 @@ +add_library(cpp-threading-future) -# This file was generated by the build system used internally in the Yandex monorepo. -# Only simple modifications are allowed (adding source-files to targets, adding simple properties -# like target_include_directories). These modifications will be ported to original -# ya.make files by maintainers. Any complex modifications which can't be ported back to the -# original buildsystem will not be accepted. +target_link_libraries(cpp-threading-future PUBLIC + contrib-libs-cxxsupp +) +target_sources(cpp-threading-future PRIVATE + ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/async_semaphore.cpp + ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/async.cpp + ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/core/future.cpp + ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/core/fwd.cpp + ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/fwd.cpp + ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/fwd.cpp + ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait.cpp + ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait_group.cpp + ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait_policy.cpp +) -if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND NOT HAVE_CUDA) - include(CMakeLists.linux-x86_64.txt) -elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" AND NOT HAVE_CUDA) - include(CMakeLists.linux-aarch64.txt) -elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64") - include(CMakeLists.darwin-x86_64.txt) -elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") - include(CMakeLists.darwin-arm64.txt) -elseif (WIN32 AND CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64" AND NOT HAVE_CUDA) - include(CMakeLists.windows-x86_64.txt) +if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT HAVE_CUDA) + target_link_libraries(cpp-threading-future PUBLIC + contrib-libs-linux-headers + ) endif() diff --git a/library/cpp/threading/future/CMakeLists.windows-x86_64.txt b/library/cpp/threading/future/CMakeLists.windows-x86_64.txt deleted file mode 100644 index 66d221dbbb4..00000000000 --- a/library/cpp/threading/future/CMakeLists.windows-x86_64.txt +++ /dev/null @@ -1,25 +0,0 @@ - -# This file was generated by the build system used internally in the Yandex monorepo. -# Only simple modifications are allowed (adding source-files to targets, adding simple properties -# like target_include_directories). These modifications will be ported to original -# ya.make files by maintainers. Any complex modifications which can't be ported back to the -# original buildsystem will not be accepted. - - - -add_library(cpp-threading-future) -target_link_libraries(cpp-threading-future PUBLIC - contrib-libs-cxxsupp - yutil -) -target_sources(cpp-threading-future PRIVATE - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/async_semaphore.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/async.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/core/future.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/core/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/fwd.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait_group.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/threading/future/wait/wait_policy.cpp -) diff --git a/library/cpp/threading/future/async_semaphore.cpp b/library/cpp/threading/future/async_semaphore.cpp index b73b7f3b667..ff856d92799 100644 --- a/library/cpp/threading/future/async_semaphore.cpp +++ b/library/cpp/threading/future/async_semaphore.cpp @@ -5,6 +5,8 @@ #include +#include + namespace NThreading { TAsyncSemaphore::TAsyncSemaphore(size_t count) @@ -18,24 +20,24 @@ TAsyncSemaphore::TPtr TAsyncSemaphore::Make(size_t count) { } TFuture TAsyncSemaphore::AcquireAsync() { - with_lock(Lock_) { - if (Cancelled_) { - return MakeErrorFuture( - std::make_exception_ptr(TOperationCancelledException())); - } - if (Count_) { - --Count_; - return MakeFuture(this); - } - auto promise = NewPromise(); - Promises_.push_back(promise); - return promise.GetFuture(); + std::lock_guard guard(Lock_); + if (Cancelled_) { + return MakeErrorFuture( + std::make_exception_ptr(TOperationCancelledException())); + } + if (Count_) { + --Count_; + return MakeFuture(this); } + auto promise = NewPromise(); + Promises_.push_back(promise); + return promise.GetFuture(); } void TAsyncSemaphore::Release() { TPromise promise; - with_lock(Lock_) { + { + std::lock_guard guard(Lock_); if (Cancelled_) { return; } @@ -52,7 +54,8 @@ void TAsyncSemaphore::Release() { void TAsyncSemaphore::Cancel() { std::list> promises; - with_lock(Lock_) { + { + std::lock_guard guard(Lock_); Cancelled_ = true; std::swap(Promises_, promises); } diff --git a/library/cpp/threading/future/core/future-inl.h b/library/cpp/threading/future/core/future-inl.h index b2fee154471..5dbb6dcdbdd 100644 --- a/library/cpp/threading/future/core/future-inl.h +++ b/library/cpp/threading/future/core/future-inl.h @@ -4,6 +4,8 @@ #error "you should never include future-inl.h directly" #endif // INCLUDE_FUTURE_INL_H +#include + namespace NThreading { namespace NImpl { //////////////////////////////////////////////////////////////////////////////// @@ -47,6 +49,7 @@ namespace NThreading { }; void AccessValue(TDuration timeout, int acquireState) const { + using namespace std::literals; TAtomicBase state = AtomicGet(State); if (Y_UNLIKELY(state == NotReady)) { if (timeout == TDuration::Zero()) { @@ -129,6 +132,7 @@ namespace NThreading { template void SetValue(TT&& value) { + using namespace std::literals; bool success = TrySetValue(std::forward(value)); if (Y_UNLIKELY(!success)) { ::NThreading::NImpl::ThrowFutureException("value already set"sv, __LOCATION__); @@ -140,7 +144,8 @@ namespace NThreading { TSystemEvent* readyEvent = nullptr; TCallbackList callbacks; - with_lock (StateLock) { + { + std::lock_guard guard(StateLock); TAtomicBase state = AtomicGet(State); if (Y_UNLIKELY(state != NotReady)) { return false; @@ -169,6 +174,7 @@ namespace NThreading { } void SetException(std::exception_ptr e) { + using namespace std::literals; bool success = TrySetException(std::move(e)); if (Y_UNLIKELY(!success)) { ::NThreading::NImpl::ThrowFutureException("value already set"sv, __LOCATION__); @@ -179,7 +185,8 @@ namespace NThreading { TSystemEvent* readyEvent; TCallbackList callbacks; - with_lock (StateLock) { + { + std::lock_guard guard(StateLock); TAtomicBase state = AtomicGet(State); if (Y_UNLIKELY(state != NotReady)) { return false; @@ -209,12 +216,11 @@ namespace NThreading { template bool Subscribe(F&& func) { - with_lock (StateLock) { - TAtomicBase state = AtomicGet(State); - if (state == NotReady) { - Callbacks.emplace_back(std::forward(func)); - return true; - } + std::lock_guard guard(StateLock); + TAtomicBase state = AtomicGet(State); + if (state == NotReady) { + Callbacks.emplace_back(std::forward(func)); + return true; } return false; } @@ -230,7 +236,8 @@ namespace NThreading { bool Wait(TInstant deadline) const { TSystemEvent* readyEvent = nullptr; - with_lock (StateLock) { + { + std::lock_guard guard(const_cast(StateLock)); TAtomicBase state = AtomicGet(State); if (state != NotReady) { return true; @@ -299,6 +306,7 @@ namespace NThreading { } void GetValue(TDuration timeout = TDuration::Zero()) const { + using namespace std::literals; TAtomicBase state = AtomicGet(State); if (Y_UNLIKELY(state == NotReady)) { if (timeout == TDuration::Zero()) { @@ -318,6 +326,7 @@ namespace NThreading { } void SetValue() { + using namespace std::literals; bool success = TrySetValue(); if (Y_UNLIKELY(!success)) { ::NThreading::NImpl::ThrowFutureException("value already set"sv, __LOCATION__); @@ -328,7 +337,8 @@ namespace NThreading { TSystemEvent* readyEvent = nullptr; TCallbackList callbacks; - with_lock (StateLock) { + { + std::lock_guard guard(StateLock); TAtomicBase state = AtomicGet(State); if (Y_UNLIKELY(state != NotReady)) { return false; @@ -355,6 +365,7 @@ namespace NThreading { } void SetException(std::exception_ptr e) { + using namespace std::literals; bool success = TrySetException(std::move(e)); if (Y_UNLIKELY(!success)) { ::NThreading::NImpl::ThrowFutureException("value already set"sv, __LOCATION__); @@ -365,7 +376,8 @@ namespace NThreading { TSystemEvent* readyEvent = nullptr; TCallbackList callbacks; - with_lock (StateLock) { + { + std::lock_guard guard(StateLock); TAtomicBase state = AtomicGet(State); if (Y_UNLIKELY(state != NotReady)) { return false; @@ -395,12 +407,11 @@ namespace NThreading { template bool Subscribe(F&& func) { - with_lock (StateLock) { - TAtomicBase state = AtomicGet(State); - if (state == NotReady) { - Callbacks.emplace_back(std::forward(func)); - return true; - } + std::lock_guard guard(StateLock); + TAtomicBase state = AtomicGet(State); + if (state == NotReady) { + Callbacks.emplace_back(std::forward(func)); + return true; } return false; } @@ -416,7 +427,8 @@ namespace NThreading { bool Wait(TInstant deadline) const { TSystemEvent* readyEvent = nullptr; - with_lock (StateLock) { + { + std::lock_guard guard(const_cast(StateLock)); TAtomicBase state = AtomicGet(State); if (state != NotReady) { return true; @@ -645,11 +657,12 @@ namespace NThreading { template inline std::optional TFuture::StateId() const noexcept { - return State != nullptr ? MakeMaybe(*State) : std::nullopt; + return State != nullptr ? std::make_optional(*State) : std::nullopt; } template inline void TFuture::EnsureInitialized() const { + using namespace std::literals; if (!State) { ::NThreading::NImpl::ThrowFutureException("state not initialized"sv, __LOCATION__); } @@ -748,10 +761,11 @@ namespace NThreading { } inline std::optional TFuture::StateId() const noexcept { - return State != nullptr ? std::optional(*State) : std::nullopt; + return State != nullptr ? std::make_optional(*State) : std::nullopt; } inline void TFuture::EnsureInitialized() const { + using namespace std::literals; if (!State) { ::NThreading::NImpl::ThrowFutureException("state not initialized"sv, __LOCATION__); } @@ -859,6 +873,7 @@ namespace NThreading { template inline void TPromise::EnsureInitialized() const { + using namespace std::literals; if (!State) { ::NThreading::NImpl::ThrowFutureException("state not initialized"sv, __LOCATION__); } @@ -933,6 +948,7 @@ namespace NThreading { } inline void TPromise::EnsureInitialized() const { + using namespace std::literals; if (!State) { ::NThreading::NImpl::ThrowFutureException("state not initialized"sv, __LOCATION__); } diff --git a/library/cpp/threading/future/core/future.h b/library/cpp/threading/future/core/future.h index 6fd37e8a72e..d59ed2a0b5f 100644 --- a/library/cpp/threading/future/core/future.h +++ b/library/cpp/threading/future/core/future.h @@ -12,6 +12,8 @@ #include #include +#include + namespace NThreading { //////////////////////////////////////////////////////////////////////////////// diff --git a/library/cpp/threading/future/perf/main.cpp b/library/cpp/threading/future/perf/main.cpp index 7e0ce7d9967..303589efcd7 100644 --- a/library/cpp/threading/future/perf/main.cpp +++ b/library/cpp/threading/future/perf/main.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include #include using namespace NThreading; diff --git a/library/cpp/threading/future/wait/wait_group-inl.h b/library/cpp/threading/future/wait/wait_group-inl.h index c5e83aef8e3..664c02b2951 100644 --- a/library/cpp/threading/future/wait/wait_group-inl.h +++ b/library/cpp/threading/future/wait/wait_group-inl.h @@ -52,14 +52,14 @@ namespace NThreading { std::exception_ptr ExceptionInFlight; void TrySetException(std::exception_ptr eptr) noexcept { - TGuard lock{Mut}; + std::lock_guard lock{Mut}; if (!ExceptionInFlight) { ExceptionInFlight = std::move(eptr); } } std::exception_ptr GetExceptionInFlight() const noexcept { - TGuard lock{Mut}; + std::lock_guard lock{Mut}; return ExceptionInFlight; } }; diff --git a/library/cpp/threading/poor_man_openmp/thread_helper.h b/library/cpp/threading/poor_man_openmp/thread_helper.h index bd5c428a143..f4c4c2759a5 100644 --- a/library/cpp/threading/poor_man_openmp/thread_helper.h +++ b/library/cpp/threading/poor_man_openmp/thread_helper.h @@ -5,12 +5,12 @@ #include #include #include -#include -#include #include #include #include +#include +#include class TMtpQueueHelper { public: @@ -50,8 +50,8 @@ namespace NYmp { size_t threadCount = TMtpQueueHelper::Instance().GetThreadCount(); IThreadPool* queue = TMtpQueueHelper::Instance().Get(); - TCondVar cv; - TMutex mutex; + std::condition_variable cv; + std::mutex mutex; TAtomic counter = threadCount; std::exception_ptr err; @@ -70,23 +70,22 @@ namespace NYmp { currentChunkStart += chunkSize * threadCount; } } catch (...) { - with_lock (mutex) { - err = std::current_exception(); - } + std::lock_guard guard(mutex); + err = std::current_exception(); } - with_lock (mutex) { - if (AtomicDecrement(counter) == 0) { - //last one - cv.Signal(); - } + std::lock_guard guard(mutex); + if (AtomicDecrement(counter) == 0) { + //last one + cv.notify_one(); } }); } - with_lock (mutex) { + { + std::unique_lock lock(mutex); while (AtomicGet(counter) > 0) { - cv.WaitI(mutex); + cv.wait(lock); } } diff --git a/library/cpp/threading/poor_man_openmp/thread_helper_ut.cpp b/library/cpp/threading/poor_man_openmp/thread_helper_ut.cpp index c0f754c2bbd..ef348e289d9 100644 --- a/library/cpp/threading/poor_man_openmp/thread_helper_ut.cpp +++ b/library/cpp/threading/poor_man_openmp/thread_helper_ut.cpp @@ -2,7 +2,7 @@ #include -#include +#include #include Y_UNIT_TEST_SUITE(TestMP) { diff --git a/library/cpp/uri/CMakeLists.darwin-arm64.txt b/library/cpp/uri/CMakeLists.darwin-arm64.txt index e3b66339095..a625455e0bc 100644 --- a/library/cpp/uri/CMakeLists.darwin-arm64.txt +++ b/library/cpp/uri/CMakeLists.darwin-arm64.txt @@ -12,8 +12,7 @@ target_link_libraries(library-cpp-uri PUBLIC yutil IDN::IDN library-cpp-charset - cpp-string_utils-string_output -) + ) target_sources(library-cpp-uri PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/uri/assign.cpp ${CMAKE_SOURCE_DIR}/library/cpp/uri/common.cpp diff --git a/library/cpp/uri/CMakeLists.darwin-x86_64.txt b/library/cpp/uri/CMakeLists.darwin-x86_64.txt index e3b66339095..a625455e0bc 100644 --- a/library/cpp/uri/CMakeLists.darwin-x86_64.txt +++ b/library/cpp/uri/CMakeLists.darwin-x86_64.txt @@ -12,8 +12,7 @@ target_link_libraries(library-cpp-uri PUBLIC yutil IDN::IDN library-cpp-charset - cpp-string_utils-string_output -) + ) target_sources(library-cpp-uri PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/uri/assign.cpp ${CMAKE_SOURCE_DIR}/library/cpp/uri/common.cpp diff --git a/library/cpp/uri/CMakeLists.linux-aarch64.txt b/library/cpp/uri/CMakeLists.linux-aarch64.txt index 127d24596e4..ce708241b5a 100644 --- a/library/cpp/uri/CMakeLists.linux-aarch64.txt +++ b/library/cpp/uri/CMakeLists.linux-aarch64.txt @@ -13,8 +13,7 @@ target_link_libraries(library-cpp-uri PUBLIC yutil IDN::IDN library-cpp-charset - cpp-string_utils-string_output -) + ) target_sources(library-cpp-uri PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/uri/assign.cpp ${CMAKE_SOURCE_DIR}/library/cpp/uri/common.cpp diff --git a/library/cpp/uri/CMakeLists.linux-x86_64.txt b/library/cpp/uri/CMakeLists.linux-x86_64.txt index 127d24596e4..ce708241b5a 100644 --- a/library/cpp/uri/CMakeLists.linux-x86_64.txt +++ b/library/cpp/uri/CMakeLists.linux-x86_64.txt @@ -13,8 +13,7 @@ target_link_libraries(library-cpp-uri PUBLIC yutil IDN::IDN library-cpp-charset - cpp-string_utils-string_output -) + ) target_sources(library-cpp-uri PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/uri/assign.cpp ${CMAKE_SOURCE_DIR}/library/cpp/uri/common.cpp diff --git a/library/cpp/uri/CMakeLists.windows-x86_64.txt b/library/cpp/uri/CMakeLists.windows-x86_64.txt index 9c4cbe66986..66aead6c7cb 100644 --- a/library/cpp/uri/CMakeLists.windows-x86_64.txt +++ b/library/cpp/uri/CMakeLists.windows-x86_64.txt @@ -12,8 +12,7 @@ target_link_libraries(library-cpp-uri PUBLIC yutil IDN::IDN library-cpp-charset - cpp-string_utils-string_output -) + ) target_sources(library-cpp-uri PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/uri/assign.cpp ${CMAKE_SOURCE_DIR}/library/cpp/uri/common.cpp diff --git a/library/cpp/uri/common.h b/library/cpp/uri/common.h index c55a8f11410..bf0ff5f01b6 100644 --- a/library/cpp/uri/common.h +++ b/library/cpp/uri/common.h @@ -1,8 +1,10 @@ #pragma once +#include + #include #include -#include +#include namespace NUri { namespace NEncode { diff --git a/library/cpp/uri/location.h b/library/cpp/uri/location.h index 64e0da11ae9..fdf852f803c 100644 --- a/library/cpp/uri/location.h +++ b/library/cpp/uri/location.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace NUri { /** diff --git a/library/cpp/uri/other.cpp b/library/cpp/uri/other.cpp index 3ac28e9a0e5..d3bfd35b92d 100644 --- a/library/cpp/uri/other.cpp +++ b/library/cpp/uri/other.cpp @@ -1,5 +1,8 @@ #include "other.h" +#include + +#include #include #include diff --git a/library/cpp/uri/other.h b/library/cpp/uri/other.h index fac807b58af..d71b84a6dd0 100644 --- a/library/cpp/uri/other.h +++ b/library/cpp/uri/other.h @@ -1,6 +1,6 @@ #pragma once -#include +#include // Some functions for inverted url representation // No scheme cut-off, no 80th port normalization diff --git a/library/cpp/uri/parse.h b/library/cpp/uri/parse.h index 53412f060b1..9ea53566773 100644 --- a/library/cpp/uri/parse.h +++ b/library/cpp/uri/parse.h @@ -7,7 +7,7 @@ #include "common.h" #include -#include +#include #include #include #include diff --git a/library/cpp/uri/qargs.cpp b/library/cpp/uri/qargs.cpp index c93133cd49d..16541231b18 100644 --- a/library/cpp/uri/qargs.cpp +++ b/library/cpp/uri/qargs.cpp @@ -1,4 +1,6 @@ #include "qargs.h" + +#include #include #include diff --git a/library/cpp/uri/uri.h b/library/cpp/uri/uri.h index 8e509921892..f7857f18c63 100644 --- a/library/cpp/uri/uri.h +++ b/library/cpp/uri/uri.h @@ -4,7 +4,8 @@ #include "encode.h" #include -#include + +#include #include #include #include @@ -408,7 +409,7 @@ namespace NUri { void Print(std::string& str, int flags = FlagUrlFields) const { flags = PrintFlags(flags); str.reserve(str.length() + PrintSize(flags)); - NUtils::TStringOutput out(str); + TStringOutput out(str); PrintImpl(out, flags); } diff --git a/library/cpp/yson/json/CMakeLists.txt b/library/cpp/yson/json/CMakeLists.txt index 1f4f156a194..bbf2d40a7c8 100644 --- a/library/cpp/yson/json/CMakeLists.txt +++ b/library/cpp/yson/json/CMakeLists.txt @@ -4,7 +4,6 @@ target_link_libraries(cpp-yson-json PUBLIC contrib-libs-cxxsupp yutil library-cpp-json - cpp-string_builder ) target_sources(cpp-yson-json PRIVATE diff --git a/library/cpp/yson/json/json_writer.cpp b/library/cpp/yson/json/json_writer.cpp index bfee908e425..c08881e19df 100644 --- a/library/cpp/yson/json/json_writer.cpp +++ b/library/cpp/yson/json/json_writer.cpp @@ -1,7 +1,7 @@ #include "json_writer.h" #include -#include +#include namespace NYT { //////////////////////////////////////////////////////////////////////////////// @@ -177,7 +177,7 @@ namespace NYT { void TJsonWriter::OnKeyedItem(std::string_view name) { if (IsWriteAllowed()) { if (IsSpecialJsonKey(name)) { - WriteStringScalar(NUtils::TYdbStringBuilder() << "$" << name); + WriteStringScalar(TStringBuilder() << "$" << name); } else { WriteStringScalar(name); } diff --git a/library/cpp/yson/varint.cpp b/library/cpp/yson/varint.cpp index deed1fdbd6f..ff451990f9a 100644 --- a/library/cpp/yson/varint.cpp +++ b/library/cpp/yson/varint.cpp @@ -3,6 +3,7 @@ #include "zigzag.h" #include +#include namespace NYson { //////////////////////////////////////////////////////////////////////////////// diff --git a/library/cpp/yt/assert/assert.h b/library/cpp/yt/assert/assert.h index 7598a29b9ca..6704647098a 100644 --- a/library/cpp/yt/assert/assert.h +++ b/library/cpp/yt/assert/assert.h @@ -3,7 +3,7 @@ #include #include -#include +#include namespace NYT { diff --git a/library/cpp/yt/misc/CMakeLists.txt b/library/cpp/yt/misc/CMakeLists.txt index 559cc741d7b..b606ed1d000 100644 --- a/library/cpp/yt/misc/CMakeLists.txt +++ b/library/cpp/yt/misc/CMakeLists.txt @@ -9,7 +9,6 @@ target_link_libraries(cpp-yt-misc PUBLIC yutil cpp-yt-exception tools-enum_parser-enum_serialization_runtime - cpp-string_builder cpp-string_utils-misc ) diff --git a/library/cpp/yt/misc/cast-inl.h b/library/cpp/yt/misc/cast-inl.h index fdffae119d8..d481b0694e7 100644 --- a/library/cpp/yt/misc/cast-inl.h +++ b/library/cpp/yt/misc/cast-inl.h @@ -6,7 +6,7 @@ #include "enum.h" -#include +#include #include #include @@ -63,12 +63,12 @@ std::string FormatInvalidCastValue(T value) inline std::string FormatInvalidCastValue(signed char value) { - return NUtils::TYdbStringBuilder() << "'" << value << "'"; + return ::TStringBuilder() << "'" << value << "'"; } inline std::string FormatInvalidCastValue(unsigned char value) { - return NUtils::TYdbStringBuilder() << "'" << value << "'"; + return ::TStringBuilder() << "'" << value << "'"; } #ifdef __cpp_char8_t diff --git a/library/cpp/yt/misc/enum-inl.h b/library/cpp/yt/misc/enum-inl.h index adcc16119ef..e56c092564e 100644 --- a/library/cpp/yt/misc/enum-inl.h +++ b/library/cpp/yt/misc/enum-inl.h @@ -5,8 +5,7 @@ #include "enum.h" #endif -#include - +#include #include #include @@ -297,7 +296,7 @@ T TEnumTraits::FromString(std::string_view literal) if (!optionalValue) { throw ::NYT::TSimpleException(Sprintf("Error parsing %s value %s", GetTypeName().data(), - NUtils::Quote(literal).c_str()).c_str()); + NUtils::Quote(literal))); } return *optionalValue; } diff --git a/library/cpp/yt/misc/enum.h b/library/cpp/yt/misc/enum.h index 68206d0bd93..29f4de2fc1e 100644 --- a/library/cpp/yt/misc/enum.h +++ b/library/cpp/yt/misc/enum.h @@ -2,7 +2,7 @@ #include "preprocessor.h" -#include +#include #include #include diff --git a/library/cpp/yt/misc/guid.cpp b/library/cpp/yt/misc/guid.cpp index b0d348c8823..18d6a884731 100644 --- a/library/cpp/yt/misc/guid.cpp +++ b/library/cpp/yt/misc/guid.cpp @@ -1,7 +1,7 @@ #include "guid.h" +#include #include - #include namespace NYT { diff --git a/library/cpp/yt/misc/guid.h b/library/cpp/yt/misc/guid.h index b5debd3b2d9..4a2a279256c 100644 --- a/library/cpp/yt/misc/guid.h +++ b/library/cpp/yt/misc/guid.h @@ -1,11 +1,12 @@ #pragma once -#include +#include #include #include #include +#include namespace NYT { diff --git a/library/cpp/yt/misc/strong_typedef-inl.h b/library/cpp/yt/misc/strong_typedef-inl.h index 021cbabf037..7963a60b216 100644 --- a/library/cpp/yt/misc/strong_typedef-inl.h +++ b/library/cpp/yt/misc/strong_typedef-inl.h @@ -4,9 +4,12 @@ #include "strong_typedef.h" #endif +#include +#include + #include "wrapper_traits.h" -#include +#include #include diff --git a/library/cpp/yt/misc/strong_typedef.h b/library/cpp/yt/misc/strong_typedef.h index c59d0762507..2ee791be248 100644 --- a/library/cpp/yt/misc/strong_typedef.h +++ b/library/cpp/yt/misc/strong_typedef.h @@ -2,7 +2,7 @@ #include -#include +#include namespace NYT { diff --git a/library/cpp/yt/misc/thread_name.cpp b/library/cpp/yt/misc/thread_name.cpp index f497915fcd8..4bf4ae08e65 100644 --- a/library/cpp/yt/misc/thread_name.cpp +++ b/library/cpp/yt/misc/thread_name.cpp @@ -2,7 +2,7 @@ #include -#include +#include #include #include @@ -35,7 +35,8 @@ TThreadName GetCurrentThreadName() auto& threadName = GetTlsRef(ThreadName); if (threadName.Length == 0) { - if (auto name = TThread::CurrentThreadName()) { + auto name = TThread::CurrentThreadName(); + if (!name.empty()) { auto length = std::min(TThreadName::BufferCapacity - 1, name.length()); threadName.Length = length; ::memcpy(threadName.Buffer.data(), name.data(), length); diff --git a/library/cpp/yt/misc/thread_name.h b/library/cpp/yt/misc/thread_name.h index 1e8dc0e50e7..b19b7b1e689 100644 --- a/library/cpp/yt/misc/thread_name.h +++ b/library/cpp/yt/misc/thread_name.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/library/cpp/yt/misc/variant.h b/library/cpp/yt/misc/variant.h index 9562e3073e6..97b79eb9b5b 100644 --- a/library/cpp/yt/misc/variant.h +++ b/library/cpp/yt/misc/variant.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include namespace NYT { diff --git a/library/cpp/yt/string/guid.h b/library/cpp/yt/string/guid.h index 1aebb24d968..ad7f5acee88 100644 --- a/library/cpp/yt/string/guid.h +++ b/library/cpp/yt/string/guid.h @@ -1,3 +1,5 @@ +#include + #include #include "format.h" diff --git a/library/cpp/yt/string/raw_formatter.h b/library/cpp/yt/string/raw_formatter.h index 2af2270dde1..d9164d07b21 100644 --- a/library/cpp/yt/string/raw_formatter.h +++ b/library/cpp/yt/string/raw_formatter.h @@ -5,7 +5,7 @@ #include #include -#include +#include namespace NYT { diff --git a/library/cpp/yt/string/string.h b/library/cpp/yt/string/string.h index 11a514f5932..772a7d26c03 100644 --- a/library/cpp/yt/string/string.h +++ b/library/cpp/yt/string/string.h @@ -6,7 +6,7 @@ #include -#include +#include #include diff --git a/library/cpp/yt/string/string_builder.h b/library/cpp/yt/string/string_builder.h index 0332132baac..541d4c86aba 100644 --- a/library/cpp/yt/string/string_builder.h +++ b/library/cpp/yt/string/string_builder.h @@ -1,6 +1,8 @@ #pragma once -#include +#include + +#include namespace NYT { diff --git a/library/cpp/yt/yson/consumer.h b/library/cpp/yt/yson/consumer.h index f97b0754cd8..3d2d222aee7 100644 --- a/library/cpp/yt/yson/consumer.h +++ b/library/cpp/yt/yson/consumer.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/library/cpp/yt/yson_string/convert.h b/library/cpp/yt/yson_string/convert.h index d05dc527d41..8cd53cd475b 100644 --- a/library/cpp/yt/yson_string/convert.h +++ b/library/cpp/yt/yson_string/convert.h @@ -6,7 +6,7 @@ #include -#include +#include #include diff --git a/tools/enum_parser/enum_parser/main.cpp b/tools/enum_parser/enum_parser/main.cpp index 41141343565..7c5180c07f7 100644 --- a/tools/enum_parser/enum_parser/main.cpp +++ b/tools/enum_parser/enum_parser/main.cpp @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include @@ -30,9 +30,9 @@ void WriteHeader(const std::string& headerName, IOutputStream& out, IOutputStrea out << "#include \n\n"; out << "#include \n"; out << "#include \n"; - out << "#include \n"; - out << "#include \n"; - out << "#include \n"; + out << "#include \n"; + out << "#include \n"; + out << "#include \n"; out << "#include \n"; out << "#include \n\n"; @@ -126,7 +126,7 @@ static inline void CloseArray(TStringStream& out) { } static std::string WrapStringBuf(const std::string_view str) { - return NUtils::TYdbStringBuilder() << "\"" << str << "\"sv"; + return TStringBuilder() << "\"" << str << "\"sv"; } void GenerateEnum( @@ -168,7 +168,7 @@ void GenerateEnum( std::string nsName = "N" + cName + "Private"; - out << "namespace { namespace " << nsName << " {\n"; + out << "namespace {\n" "using namespace std::literals;\n" "namespace " << nsName << " {\n"; std::vector nameInitializerPairs; std::vector> valueInitializerPairsUnsorted; // data, sort_key diff --git a/tools/enum_parser/enum_serialization_runtime/enum_runtime.cpp b/tools/enum_parser/enum_serialization_runtime/enum_runtime.cpp index bd7f31f8ee5..e365cdd14fd 100644 --- a/tools/enum_parser/enum_serialization_runtime/enum_runtime.cpp +++ b/tools/enum_parser/enum_serialization_runtime/enum_runtime.cpp @@ -1,6 +1,6 @@ #include "enum_runtime.h" -#include +#include #include @@ -187,13 +187,13 @@ namespace NEnumSerializationRuntime { if (!AllNames.empty()) { AllNames += ", "; } - AllNames += NUtils::TYdbStringBuilder() << '\'' << it.second << '\''; + AllNames += TStringBuilder() << '\'' << it.second << '\''; AllValues.push_back(it.first); } AllCppNames.reserve(cppNamesInitializer.size()); for (const auto& cn : cppNamesInitializer) { - AllCppNames.push_back(NUtils::TYdbStringBuilder() << enumInitData.CppNamesPrefix << cn); + AllCppNames.push_back(TStringBuilder() << enumInitData.CppNamesPrefix << cn); } } diff --git a/tools/enum_parser/enum_serialization_runtime/serialized_enum.h b/tools/enum_parser/enum_serialization_runtime/serialized_enum.h index 23d20d6035a..80f7f307157 100644 --- a/tools/enum_parser/enum_serialization_runtime/serialized_enum.h +++ b/tools/enum_parser/enum_serialization_runtime/serialized_enum.h @@ -1,9 +1,11 @@ #pragma once #include -#include #include +#include +#include +#include #include /* diff --git a/tools/enum_parser/parse_enum/benchmark/main.cpp b/tools/enum_parser/parse_enum/benchmark/main.cpp index fa70bd0f290..d462d8e6e69 100644 --- a/tools/enum_parser/parse_enum/benchmark/main.cpp +++ b/tools/enum_parser/parse_enum/benchmark/main.cpp @@ -17,13 +17,13 @@ namespace { } template - std::vector SelectStrings(size_t count) { - std::vector strings; + std::vector SelectStrings(size_t count) { + std::vector strings; strings.reserve(GetEnumItemsCount()); for (const auto& [_, s] : GetEnumNames()) { strings.push_back(s); } - SortBy(strings, [](const TStringBuf& s) { return THash()(s); }); + SortBy(strings, [](const std::string_view& s) { return THash()(s); }); strings.crop(count); return strings; } diff --git a/tools/enum_parser/parse_enum/parse_enum.cpp b/tools/enum_parser/parse_enum/parse_enum.cpp index 9acac953b38..c93f73d3720 100644 --- a/tools/enum_parser/parse_enum/parse_enum.cpp +++ b/tools/enum_parser/parse_enum/parse_enum.cpp @@ -1,9 +1,10 @@ #include "parse_enum.h" #include -#include +#include #include +#include /** * Parse C-style strings inside multiline comments @@ -390,7 +391,7 @@ TEnumParser::TEnumParser(IInputStream& in) { void TEnumParser::Parse(const char* dataIn, size_t lengthIn) { TMemoryInput mi(dataIn, lengthIn); - TString line; + std::string line; std::string result; while (mi.ReadLine(line)) { diff --git a/tools/enum_parser/parse_enum/ut/enums.cpp b/tools/enum_parser/parse_enum/ut/enums.cpp index 92e54706800..05795c104ae 100644 --- a/tools/enum_parser/parse_enum/ut/enums.cpp +++ b/tools/enum_parser/parse_enum/ut/enums.cpp @@ -78,25 +78,25 @@ Y_UNIT_TEST_SUITE(TEnumGeneratorTest) { template void CheckFromString(const TString& strValue, const T& value) { - UNIT_ASSERT_VALUES_EQUAL(static_cast(FromString(TStringBuf(strValue))), static_cast(value)); + UNIT_ASSERT_VALUES_EQUAL(static_cast(FromString(std::string_view(strValue))), static_cast(value)); } template void CheckFromStringFail(const TString& strValue) { - UNIT_ASSERT_EXCEPTION(FromString(TStringBuf(strValue)), yexception); + UNIT_ASSERT_EXCEPTION(FromString(std::string_view(strValue)), yexception); } template void CheckTryFromString(const TString& strValue, const T& value) { T x; - UNIT_ASSERT_VALUES_EQUAL(TryFromString(TStringBuf(strValue), x), true); + UNIT_ASSERT_VALUES_EQUAL(TryFromString(std::string_view(strValue), x), true); UNIT_ASSERT_VALUES_EQUAL(x, value); } template void CheckTryFromStringFail(const TString& strValue) { T x = T(-666); - UNIT_ASSERT_VALUES_EQUAL(TryFromString(TStringBuf(strValue), x), false); + UNIT_ASSERT_VALUES_EQUAL(TryFromString(std::string_view(strValue), x), false); UNIT_ASSERT_VALUES_EQUAL(int(x), -666); } diff --git a/tools/rescompiler/main.cpp b/tools/rescompiler/main.cpp index 3d0fd22b825..b43158d7626 100644 --- a/tools/rescompiler/main.cpp +++ b/tools/rescompiler/main.cpp @@ -10,12 +10,12 @@ using namespace NResource; -static inline void GenOne(const TString& data, const TString& key, IOutputStream& out) { - const TString name = "name" + ToString(CityHash64(key.data(), key.size())); +static inline void GenOne(const std::string& data, const std::string& key, IOutputStream& out) { + const std::string name = "name" + ToString(CityHash64(key.data(), key.size())); out << "static const unsigned char " << name << "[] = {"; - const TString c = Compress(data); + const std::string c = Compress(data); char buf[16]; for (size_t i = 0; i < c.size(); ++i) { @@ -25,10 +25,10 @@ static inline void GenOne(const TString& data, const TString& key, IOutputStream const char ch = c[i]; - out << "0x" << TStringBuf(buf, HexEncode(&ch, 1, buf)) << ", "; + out << "0x" << std::string_view(buf, HexEncode(&ch, 1, buf)) << ", "; } - out << "\n};\n\nstatic const NResource::TRegHelper REG_" << name << "(\"" << key << "\", TStringBuf((const char*)" << name << ", sizeof(" << name << ")));\n"; + out << "\n};\n\nstatic const NResource::TRegHelper REG_" << name << "(\"" << key << "\", std::string_view((const char*)" << name << ", sizeof(" << name << ")));\n"; } int main(int argc, char** argv) { @@ -45,9 +45,9 @@ int main(int argc, char** argv) { out << "#include \n\n"; while (*argv) { - if ("-"sv == *argv) { - std::vector items = StringSplitter(TString(*(argv + 1))).Split('=').Limit(2).ToList(); - GenOne(TString(items[1]), TString(items[0]), out); + if (std::string_view{"-"} == *argv) { + std::vector items = StringSplitter(std::string(*(argv + 1))).Split('=').Limit(2).ToList(); + GenOne(items[1], items[0], out); } else { const char* key = *(argv + 1); if (*key == '-') { diff --git a/util/CMakeLists.darwin-arm64.txt b/util/CMakeLists.darwin-arm64.txt index 64062d810ca..66729a4adfc 100644 --- a/util/CMakeLists.darwin-arm64.txt +++ b/util/CMakeLists.darwin-arm64.txt @@ -90,8 +90,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/iterator.cpp ${CMAKE_SOURCE_DIR}/util/generic/iterator_range.cpp ${CMAKE_SOURCE_DIR}/util/generic/lazy_value.cpp - ${CMAKE_SOURCE_DIR}/util/generic/list.cpp - ${CMAKE_SOURCE_DIR}/util/generic/map.cpp ${CMAKE_SOURCE_DIR}/util/generic/mapfindptr.cpp ${CMAKE_SOURCE_DIR}/util/generic/mem_copy.cpp ${CMAKE_SOURCE_DIR}/util/generic/noncopyable.cpp @@ -106,15 +104,12 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/size_literals.cpp ${CMAKE_SOURCE_DIR}/util/generic/stack.cpp ${CMAKE_SOURCE_DIR}/util/generic/store_policy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/strbuf.cpp ${CMAKE_SOURCE_DIR}/util/generic/strfcpy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/string.cpp ${CMAKE_SOURCE_DIR}/util/generic/typelist.cpp ${CMAKE_SOURCE_DIR}/util/generic/typetraits.cpp ${CMAKE_SOURCE_DIR}/util/generic/utility.cpp ${CMAKE_SOURCE_DIR}/util/generic/va_args.cpp ${CMAKE_SOURCE_DIR}/util/generic/variant.cpp - ${CMAKE_SOURCE_DIR}/util/generic/vector.cpp ${CMAKE_SOURCE_DIR}/util/generic/xrange.cpp ${CMAKE_SOURCE_DIR}/util/generic/yexception.cpp ${CMAKE_SOURCE_DIR}/util/generic/ylimits.cpp @@ -216,7 +211,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/atexit.cpp ${CMAKE_SOURCE_DIR}/util/system/backtrace.cpp ${CMAKE_SOURCE_DIR}/util/system/compat.cpp - ${CMAKE_SOURCE_DIR}/util/system/condvar.cpp ${CMAKE_SOURCE_DIR}/util/system/context.cpp ${CMAKE_SOURCE_DIR}/util/system/daemon.cpp ${CMAKE_SOURCE_DIR}/util/system/datetime.cpp @@ -254,7 +248,7 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/mincore.cpp ${CMAKE_SOURCE_DIR}/util/system/mktemp.cpp ${CMAKE_SOURCE_DIR}/util/system/mlock.cpp - ${CMAKE_SOURCE_DIR}/util/system/mutex.cpp + ${CMAKE_SOURCE_DIR}/util/system/fake_mutex.cpp ${CMAKE_SOURCE_DIR}/util/system/nice.cpp ${CMAKE_SOURCE_DIR}/util/system/pipe.cpp ${CMAKE_SOURCE_DIR}/util/system/platform.cpp @@ -287,8 +281,6 @@ target_joined_source(yutil all_thread.cpp ${CMAKE_SOURCE_DIR}/util/thread/factory.cpp ${CMAKE_SOURCE_DIR}/util/thread/fwd.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfqueue.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfstack.cpp ${CMAKE_SOURCE_DIR}/util/thread/pool.cpp ${CMAKE_SOURCE_DIR}/util/thread/singleton.cpp ) diff --git a/util/CMakeLists.darwin-x86_64.txt b/util/CMakeLists.darwin-x86_64.txt index a7e01b304af..9ee14eb6532 100644 --- a/util/CMakeLists.darwin-x86_64.txt +++ b/util/CMakeLists.darwin-x86_64.txt @@ -89,8 +89,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/iterator.cpp ${CMAKE_SOURCE_DIR}/util/generic/iterator_range.cpp ${CMAKE_SOURCE_DIR}/util/generic/lazy_value.cpp - ${CMAKE_SOURCE_DIR}/util/generic/list.cpp - ${CMAKE_SOURCE_DIR}/util/generic/map.cpp ${CMAKE_SOURCE_DIR}/util/generic/mapfindptr.cpp ${CMAKE_SOURCE_DIR}/util/generic/mem_copy.cpp ${CMAKE_SOURCE_DIR}/util/generic/noncopyable.cpp @@ -105,15 +103,12 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/size_literals.cpp ${CMAKE_SOURCE_DIR}/util/generic/stack.cpp ${CMAKE_SOURCE_DIR}/util/generic/store_policy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/strbuf.cpp ${CMAKE_SOURCE_DIR}/util/generic/strfcpy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/string.cpp ${CMAKE_SOURCE_DIR}/util/generic/typelist.cpp ${CMAKE_SOURCE_DIR}/util/generic/typetraits.cpp ${CMAKE_SOURCE_DIR}/util/generic/utility.cpp ${CMAKE_SOURCE_DIR}/util/generic/va_args.cpp ${CMAKE_SOURCE_DIR}/util/generic/variant.cpp - ${CMAKE_SOURCE_DIR}/util/generic/vector.cpp ${CMAKE_SOURCE_DIR}/util/generic/xrange.cpp ${CMAKE_SOURCE_DIR}/util/generic/yexception.cpp ${CMAKE_SOURCE_DIR}/util/generic/ylimits.cpp @@ -215,7 +210,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/atexit.cpp ${CMAKE_SOURCE_DIR}/util/system/backtrace.cpp ${CMAKE_SOURCE_DIR}/util/system/compat.cpp - ${CMAKE_SOURCE_DIR}/util/system/condvar.cpp ${CMAKE_SOURCE_DIR}/util/system/context.cpp ${CMAKE_SOURCE_DIR}/util/system/daemon.cpp ${CMAKE_SOURCE_DIR}/util/system/datetime.cpp @@ -253,7 +247,7 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/mincore.cpp ${CMAKE_SOURCE_DIR}/util/system/mktemp.cpp ${CMAKE_SOURCE_DIR}/util/system/mlock.cpp - ${CMAKE_SOURCE_DIR}/util/system/mutex.cpp + ${CMAKE_SOURCE_DIR}/util/system/fake_mutex.cpp ${CMAKE_SOURCE_DIR}/util/system/nice.cpp ${CMAKE_SOURCE_DIR}/util/system/pipe.cpp ${CMAKE_SOURCE_DIR}/util/system/platform.cpp @@ -286,8 +280,6 @@ target_joined_source(yutil all_thread.cpp ${CMAKE_SOURCE_DIR}/util/thread/factory.cpp ${CMAKE_SOURCE_DIR}/util/thread/fwd.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfqueue.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfstack.cpp ${CMAKE_SOURCE_DIR}/util/thread/pool.cpp ${CMAKE_SOURCE_DIR}/util/thread/singleton.cpp ) diff --git a/util/CMakeLists.linux-aarch64.txt b/util/CMakeLists.linux-aarch64.txt index a9d2830cafb..ca3217531fe 100644 --- a/util/CMakeLists.linux-aarch64.txt +++ b/util/CMakeLists.linux-aarch64.txt @@ -93,8 +93,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/iterator.cpp ${CMAKE_SOURCE_DIR}/util/generic/iterator_range.cpp ${CMAKE_SOURCE_DIR}/util/generic/lazy_value.cpp - ${CMAKE_SOURCE_DIR}/util/generic/list.cpp - ${CMAKE_SOURCE_DIR}/util/generic/map.cpp ${CMAKE_SOURCE_DIR}/util/generic/mapfindptr.cpp ${CMAKE_SOURCE_DIR}/util/generic/mem_copy.cpp ${CMAKE_SOURCE_DIR}/util/generic/noncopyable.cpp @@ -109,15 +107,12 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/size_literals.cpp ${CMAKE_SOURCE_DIR}/util/generic/stack.cpp ${CMAKE_SOURCE_DIR}/util/generic/store_policy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/strbuf.cpp ${CMAKE_SOURCE_DIR}/util/generic/strfcpy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/string.cpp ${CMAKE_SOURCE_DIR}/util/generic/typelist.cpp ${CMAKE_SOURCE_DIR}/util/generic/typetraits.cpp ${CMAKE_SOURCE_DIR}/util/generic/utility.cpp ${CMAKE_SOURCE_DIR}/util/generic/va_args.cpp ${CMAKE_SOURCE_DIR}/util/generic/variant.cpp - ${CMAKE_SOURCE_DIR}/util/generic/vector.cpp ${CMAKE_SOURCE_DIR}/util/generic/xrange.cpp ${CMAKE_SOURCE_DIR}/util/generic/yexception.cpp ${CMAKE_SOURCE_DIR}/util/generic/ylimits.cpp @@ -219,7 +214,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/atexit.cpp ${CMAKE_SOURCE_DIR}/util/system/backtrace.cpp ${CMAKE_SOURCE_DIR}/util/system/compat.cpp - ${CMAKE_SOURCE_DIR}/util/system/condvar.cpp ${CMAKE_SOURCE_DIR}/util/system/context.cpp ${CMAKE_SOURCE_DIR}/util/system/daemon.cpp ${CMAKE_SOURCE_DIR}/util/system/datetime.cpp @@ -257,7 +251,7 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/mincore.cpp ${CMAKE_SOURCE_DIR}/util/system/mktemp.cpp ${CMAKE_SOURCE_DIR}/util/system/mlock.cpp - ${CMAKE_SOURCE_DIR}/util/system/mutex.cpp + ${CMAKE_SOURCE_DIR}/util/system/fake_mutex.cpp ${CMAKE_SOURCE_DIR}/util/system/nice.cpp ${CMAKE_SOURCE_DIR}/util/system/pipe.cpp ${CMAKE_SOURCE_DIR}/util/system/platform.cpp @@ -290,8 +284,6 @@ target_joined_source(yutil all_thread.cpp ${CMAKE_SOURCE_DIR}/util/thread/factory.cpp ${CMAKE_SOURCE_DIR}/util/thread/fwd.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfqueue.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfstack.cpp ${CMAKE_SOURCE_DIR}/util/thread/pool.cpp ${CMAKE_SOURCE_DIR}/util/thread/singleton.cpp ) diff --git a/util/CMakeLists.linux-x86_64.txt b/util/CMakeLists.linux-x86_64.txt index 874c7c2b329..2c64eeba6db 100644 --- a/util/CMakeLists.linux-x86_64.txt +++ b/util/CMakeLists.linux-x86_64.txt @@ -20,6 +20,7 @@ target_link_libraries(yutil PUBLIC contrib-libs-zlib contrib-libs-double-conversion contrib-libs-libc_compat + cpp-string_utils-helpers ) target_sources(yutil PRIVATE ${CMAKE_SOURCE_DIR}/util/system/compiler.cpp @@ -92,8 +93,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/iterator.cpp ${CMAKE_SOURCE_DIR}/util/generic/iterator_range.cpp ${CMAKE_SOURCE_DIR}/util/generic/lazy_value.cpp - ${CMAKE_SOURCE_DIR}/util/generic/list.cpp - ${CMAKE_SOURCE_DIR}/util/generic/map.cpp ${CMAKE_SOURCE_DIR}/util/generic/mapfindptr.cpp ${CMAKE_SOURCE_DIR}/util/generic/mem_copy.cpp ${CMAKE_SOURCE_DIR}/util/generic/noncopyable.cpp @@ -108,15 +107,12 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/size_literals.cpp ${CMAKE_SOURCE_DIR}/util/generic/stack.cpp ${CMAKE_SOURCE_DIR}/util/generic/store_policy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/strbuf.cpp ${CMAKE_SOURCE_DIR}/util/generic/strfcpy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/string.cpp ${CMAKE_SOURCE_DIR}/util/generic/typelist.cpp ${CMAKE_SOURCE_DIR}/util/generic/typetraits.cpp ${CMAKE_SOURCE_DIR}/util/generic/utility.cpp ${CMAKE_SOURCE_DIR}/util/generic/va_args.cpp ${CMAKE_SOURCE_DIR}/util/generic/variant.cpp - ${CMAKE_SOURCE_DIR}/util/generic/vector.cpp ${CMAKE_SOURCE_DIR}/util/generic/xrange.cpp ${CMAKE_SOURCE_DIR}/util/generic/yexception.cpp ${CMAKE_SOURCE_DIR}/util/generic/ylimits.cpp @@ -218,7 +214,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/atexit.cpp ${CMAKE_SOURCE_DIR}/util/system/backtrace.cpp ${CMAKE_SOURCE_DIR}/util/system/compat.cpp - ${CMAKE_SOURCE_DIR}/util/system/condvar.cpp ${CMAKE_SOURCE_DIR}/util/system/context.cpp ${CMAKE_SOURCE_DIR}/util/system/daemon.cpp ${CMAKE_SOURCE_DIR}/util/system/datetime.cpp @@ -256,7 +251,7 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/mincore.cpp ${CMAKE_SOURCE_DIR}/util/system/mktemp.cpp ${CMAKE_SOURCE_DIR}/util/system/mlock.cpp - ${CMAKE_SOURCE_DIR}/util/system/mutex.cpp + ${CMAKE_SOURCE_DIR}/util/system/fake_mutex.cpp ${CMAKE_SOURCE_DIR}/util/system/nice.cpp ${CMAKE_SOURCE_DIR}/util/system/pipe.cpp ${CMAKE_SOURCE_DIR}/util/system/platform.cpp @@ -289,8 +284,6 @@ target_joined_source(yutil all_thread.cpp ${CMAKE_SOURCE_DIR}/util/thread/factory.cpp ${CMAKE_SOURCE_DIR}/util/thread/fwd.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfqueue.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfstack.cpp ${CMAKE_SOURCE_DIR}/util/thread/pool.cpp ${CMAKE_SOURCE_DIR}/util/thread/singleton.cpp ) diff --git a/util/CMakeLists.windows-x86_64.txt b/util/CMakeLists.windows-x86_64.txt index e99aababc4b..8946eadaf7a 100644 --- a/util/CMakeLists.windows-x86_64.txt +++ b/util/CMakeLists.windows-x86_64.txt @@ -96,8 +96,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/iterator.cpp ${CMAKE_SOURCE_DIR}/util/generic/iterator_range.cpp ${CMAKE_SOURCE_DIR}/util/generic/lazy_value.cpp - ${CMAKE_SOURCE_DIR}/util/generic/list.cpp - ${CMAKE_SOURCE_DIR}/util/generic/map.cpp ${CMAKE_SOURCE_DIR}/util/generic/mapfindptr.cpp ${CMAKE_SOURCE_DIR}/util/generic/mem_copy.cpp ${CMAKE_SOURCE_DIR}/util/generic/noncopyable.cpp @@ -112,15 +110,12 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/size_literals.cpp ${CMAKE_SOURCE_DIR}/util/generic/stack.cpp ${CMAKE_SOURCE_DIR}/util/generic/store_policy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/strbuf.cpp ${CMAKE_SOURCE_DIR}/util/generic/strfcpy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/string.cpp ${CMAKE_SOURCE_DIR}/util/generic/typelist.cpp ${CMAKE_SOURCE_DIR}/util/generic/typetraits.cpp ${CMAKE_SOURCE_DIR}/util/generic/utility.cpp ${CMAKE_SOURCE_DIR}/util/generic/va_args.cpp ${CMAKE_SOURCE_DIR}/util/generic/variant.cpp - ${CMAKE_SOURCE_DIR}/util/generic/vector.cpp ${CMAKE_SOURCE_DIR}/util/generic/xrange.cpp ${CMAKE_SOURCE_DIR}/util/generic/yexception.cpp ${CMAKE_SOURCE_DIR}/util/generic/ylimits.cpp @@ -222,7 +217,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/atexit.cpp ${CMAKE_SOURCE_DIR}/util/system/backtrace.cpp ${CMAKE_SOURCE_DIR}/util/system/compat.cpp - ${CMAKE_SOURCE_DIR}/util/system/condvar.cpp ${CMAKE_SOURCE_DIR}/util/system/context.cpp ${CMAKE_SOURCE_DIR}/util/system/daemon.cpp ${CMAKE_SOURCE_DIR}/util/system/datetime.cpp @@ -260,7 +254,7 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/mincore.cpp ${CMAKE_SOURCE_DIR}/util/system/mktemp.cpp ${CMAKE_SOURCE_DIR}/util/system/mlock.cpp - ${CMAKE_SOURCE_DIR}/util/system/mutex.cpp + ${CMAKE_SOURCE_DIR}/util/system/fake_mutex.cpp ${CMAKE_SOURCE_DIR}/util/system/nice.cpp ${CMAKE_SOURCE_DIR}/util/system/pipe.cpp ${CMAKE_SOURCE_DIR}/util/system/platform.cpp @@ -293,8 +287,6 @@ target_joined_source(yutil all_thread.cpp ${CMAKE_SOURCE_DIR}/util/thread/factory.cpp ${CMAKE_SOURCE_DIR}/util/thread/fwd.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfqueue.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfstack.cpp ${CMAKE_SOURCE_DIR}/util/thread/pool.cpp ${CMAKE_SOURCE_DIR}/util/thread/singleton.cpp ) diff --git a/util/charset/utf8.cpp b/util/charset/utf8.cpp index efe3a52f615..18b58394c31 100644 --- a/util/charset/utf8.cpp +++ b/util/charset/utf8.cpp @@ -19,7 +19,7 @@ namespace { } bool ConvertCaseUTF8Impl(ECaseConversion conversion, const char* beg, size_t n, - TString& newString) { + std::string& newString) { const unsigned char* p = (const unsigned char*)beg; const unsigned char* const end = p + n; @@ -99,10 +99,10 @@ static const char* SkipUTF8Chars(const char* begin, const char* end, size_t numC return begin; } -TStringBuf SubstrUTF8(const TStringBuf str, size_t pos, size_t len) { +std::string_view SubstrUTF8(const std::string_view str, size_t pos, size_t len) { const char* start = SkipUTF8Chars(str.begin(), str.end(), pos); const char* end = SkipUTF8Chars(start, str.end(), len); - return TStringBuf(start, end - start); + return std::string_view(start, end - start); } EUTF8Detect UTF8Detect(const char* s, size_t len) { @@ -129,42 +129,36 @@ EUTF8Detect UTF8Detect(const char* s, size_t len) { return res; } -bool ToLowerUTF8Impl(const char* beg, size_t n, TString& newString) { +bool ToLowerUTF8Impl(const char* beg, size_t n, std::string& newString) { return ConvertCaseUTF8Impl(ECaseConversion::ToLower, beg, n, newString); } -TString ToLowerUTF8(const TString& s) { - TString newString; +std::string ToLowerUTF8(std::string_view s) { + std::string newString; bool changed = ToLowerUTF8Impl(s.data(), s.size(), newString); - return changed ? newString : s; -} - -TString ToLowerUTF8(TStringBuf s) { - TString newString; - bool changed = ToLowerUTF8Impl(s.data(), s.size(), newString); - return changed ? newString : TString(s.data(), s.size()); + return changed ? newString : std::string(s.data(), s.size()); } -TString ToLowerUTF8(const char* s) { - return ToLowerUTF8(TStringBuf(s)); +std::string ToLowerUTF8(const char* s) { + return ToLowerUTF8(std::string_view(s)); } -bool ToUpperUTF8Impl(const char* beg, size_t n, TString& newString) { +bool ToUpperUTF8Impl(const char* beg, size_t n, std::string& newString) { return ConvertCaseUTF8Impl(ECaseConversion::ToUpper, beg, n, newString); } -TString ToUpperUTF8(const TString& s) { - TString newString; +std::string ToUpperUTF8(const std::string& s) { + std::string newString; bool changed = ToUpperUTF8Impl(s.data(), s.size(), newString); return changed ? newString : s; } -TString ToUpperUTF8(TStringBuf s) { - TString newString; +std::string ToUpperUTF8(std::string_view s) { + std::string newString; bool changed = ToUpperUTF8Impl(s.data(), s.size(), newString); - return changed ? newString : TString(s.data(), s.size()); + return changed ? newString : std::string(s.data(), s.size()); } -TString ToUpperUTF8(const char* s) { - return ToUpperUTF8(TStringBuf(s)); +std::string ToUpperUTF8(const char* s) { + return ToUpperUTF8(std::string_view(s)); } diff --git a/util/charset/utf8.h b/util/charset/utf8.h index 76c1f94078a..26aa9c7fe58 100644 --- a/util/charset/utf8.h +++ b/util/charset/utf8.h @@ -2,9 +2,8 @@ #include "recode_result.h" -#include -#include #include +#include #include #include @@ -131,10 +130,10 @@ inline bool GetNumberOfUTF8Chars(const char* text, size_t len, size_t& number) { return res; } -inline size_t GetNumberOfUTF8Chars(TStringBuf text) { +inline size_t GetNumberOfUTF8Chars(std::string_view text) { size_t number; if (!GetNumberOfUTF8Chars(text.data(), text.size(), number)) { - ythrow yexception() << "GetNumberOfUTF8Chars failed on invalid utf-8 " << TString(text.substr(0, 50)).Quote(); + ythrow yexception() << "GetNumberOfUTF8Chars failed on invalid utf-8 \"" << EscapeC(std::string(text.substr(0, 50))) << "\""; } return number; } @@ -388,7 +387,7 @@ inline void WriteUTF8Char(wchar32 rune, size_t& rune_len, unsigned char* s) { } } -TStringBuf SubstrUTF8(const TStringBuf str, size_t pos, size_t len); +std::string_view SubstrUTF8(const std::string_view str, size_t pos, size_t len); enum EUTF8Detect { NotUTF8, @@ -398,7 +397,7 @@ enum EUTF8Detect { EUTF8Detect UTF8Detect(const char* s, size_t len); -inline EUTF8Detect UTF8Detect(const TStringBuf input) { +inline EUTF8Detect UTF8Detect(const std::string_view input) { return UTF8Detect(input.data(), input.size()); } @@ -406,26 +405,25 @@ inline bool IsUtf(const char* input, size_t len) { return UTF8Detect(input, len) != NotUTF8; } -inline bool IsUtf(const TStringBuf input) { +inline bool IsUtf(const std::string_view input) { return IsUtf(input.data(), input.size()); } //! returns true, if result is not the same as input, and put it in newString //! returns false, if result is unmodified -bool ToLowerUTF8Impl(const char* beg, size_t n, TString& newString); +bool ToLowerUTF8Impl(const char* beg, size_t n, std::string& newString); -TString ToLowerUTF8(const TString& s); -TString ToLowerUTF8(TStringBuf s); -TString ToLowerUTF8(const char* s); +std::string ToLowerUTF8(std::string_view s); +std::string ToLowerUTF8(const char* s); -inline TString ToLowerUTF8(const std::string& s) { - return ToLowerUTF8(TStringBuf(s)); +inline std::string ToLowerUTF8(const std::string& s) { + return ToLowerUTF8(std::string_view(s)); } //! returns true, if result is not the same as input, and put it in newString //! returns false, if result is unmodified -bool ToUpperUTF8Impl(const char* beg, size_t n, TString& newString); +bool ToUpperUTF8Impl(const char* beg, size_t n, std::string& newString); -TString ToUpperUTF8(const TString& s); -TString ToUpperUTF8(TStringBuf s); -TString ToUpperUTF8(const char* s); +std::string ToUpperUTF8(const std::string& s); +std::string ToUpperUTF8(std::string_view s); +std::string ToUpperUTF8(const char* s); diff --git a/util/charset/utf8_ut.cpp b/util/charset/utf8_ut.cpp index 9b781577889..7f3002501b6 100644 --- a/util/charset/utf8_ut.cpp +++ b/util/charset/utf8_ut.cpp @@ -15,12 +15,12 @@ Y_UNIT_TEST_SUITE(TUtfUtilTest) { Y_UNIT_TEST(TestToLowerUtfString) { UNIT_ASSERT_VALUES_EQUAL(ToLowerUTF8("xyz XYZ ПРИВЕТ!"), "xyz xyz привет!"); - UNIT_ASSERT_VALUES_EQUAL(ToLowerUTF8(TStringBuf("xyz")), "xyz"); + UNIT_ASSERT_VALUES_EQUAL(ToLowerUTF8(std::string_view("xyz")), "xyz"); { - TString s = "привет!"; - TString q = "ПРИВЕТ!"; - TString tmp; + std::string s = "привет!"; + std::string q = "ПРИВЕТ!"; + std::string tmp; UNIT_ASSERT(ToLowerUTF8Impl(s.data(), s.size(), tmp) == false); UNIT_ASSERT(ToLowerUTF8Impl(q.data(), q.size(), tmp) == true); } @@ -28,13 +28,13 @@ Y_UNIT_TEST_SUITE(TUtfUtilTest) { { const char* weird = "\xC8\xBE"; // 'Ⱦ', U+023E. strlen(weird)==2, strlen(tolower_utf8(weird)) is 3 const char* turkI = "İ"; //strlen("İ") == 2, strlen(tolower_utf8("İ") == 1 - TStringBuf chars[] = {"f", "F", "Б", "б", weird, turkI}; + std::string_view chars[] = {"f", "F", "Б", "б", weird, turkI}; const int N = Y_ARRAY_SIZE(chars); //try all combinations of these letters. int numberOfVariants = 1; for (int len = 0; len <= 4; ++len) { for (int i = 0; i < numberOfVariants; ++i) { - TString s; + std::string s; int k = i; for (int j = 0; j < len; ++j) { //Treat 'i' like number in base-N system with digits from 'chars'-array @@ -42,7 +42,7 @@ Y_UNIT_TEST_SUITE(TUtfUtilTest) { k /= N; } - TUtf16String tmp = UTF8ToWide(s); + std::u16string tmp = UTF8ToWide(s); tmp.to_lower(); UNIT_ASSERT_VALUES_EQUAL(ToLowerUTF8(s), WideToUTF8(tmp)); @@ -55,12 +55,12 @@ Y_UNIT_TEST_SUITE(TUtfUtilTest) { Y_UNIT_TEST(TestToUpperUtfString) { UNIT_ASSERT_VALUES_EQUAL(ToUpperUTF8("xyz XYZ привет!"), "XYZ XYZ ПРИВЕТ!"); - UNIT_ASSERT_VALUES_EQUAL(ToUpperUTF8(TStringBuf("XYZ")), "XYZ"); + UNIT_ASSERT_VALUES_EQUAL(ToUpperUTF8(std::string_view("XYZ")), "XYZ"); { - TString s = "ПРИВЕТ!"; - TString q = "привет!"; - TString tmp; + std::string s = "ПРИВЕТ!"; + std::string q = "привет!"; + std::string tmp; UNIT_ASSERT(ToUpperUTF8Impl(s.data(), s.size(), tmp) == false); UNIT_ASSERT(ToUpperUTF8Impl(q.data(), q.size(), tmp) == true); } @@ -68,13 +68,13 @@ Y_UNIT_TEST_SUITE(TUtfUtilTest) { { const char* weird = "\xC8\xBE"; // 'Ⱦ', U+023E. strlen(weird)==2, strlen(ToUpper_utf8(weird)) is 3 const char* turkI = "İ"; //strlen("İ") == 2, strlen(ToUpper_utf8("İ") == 1 - TStringBuf chars[] = {"F", "f", "б", "Б", turkI, weird}; + std::string_view chars[] = {"F", "f", "б", "Б", turkI, weird}; const int N = Y_ARRAY_SIZE(chars); //try all combinations of these letters. int numberOfVariants = 1; for (int len = 0; len <= 4; ++len) { for (int i = 0; i < numberOfVariants; ++i) { - TString s; + std::string s; int k = i; for (int j = 0; j < len; ++j) { //Treat 'i' like number in base-N system with digits from 'chars'-array @@ -82,7 +82,7 @@ Y_UNIT_TEST_SUITE(TUtfUtilTest) { k /= N; } - TUtf16String tmp = UTF8ToWide(s); + std::u16string tmp = UTF8ToWide(s); tmp.to_upper(); UNIT_ASSERT_VALUES_EQUAL(ToUpperUTF8(s), WideToUTF8(tmp)); @@ -93,15 +93,15 @@ Y_UNIT_TEST_SUITE(TUtfUtilTest) { } Y_UNIT_TEST(TestUTF8ToWide) { - TFileInput in(ArcadiaSourceRoot() + TStringBuf("/util/charset/ut/utf8/test1.txt")); + TFileInput in(ArcadiaSourceRoot() + std::string_view("/util/charset/ut/utf8/test1.txt")); - TString text = in.ReadAll(); + std::string text = in.ReadAll(); UNIT_ASSERT(WideToUTF8(UTF8ToWide(text)) == text); } Y_UNIT_TEST(TestInvalidUTF8) { - std::vector testData; - TFileInput input(ArcadiaSourceRoot() + TStringBuf("/util/charset/ut/utf8/invalid_UTF8.bin")); + std::vector testData; + TFileInput input(ArcadiaSourceRoot() + std::string_view("/util/charset/ut/utf8/invalid_UTF8.bin")); Load(&input, testData); for (const auto& text : testData) { @@ -110,11 +110,11 @@ Y_UNIT_TEST_SUITE(TUtfUtilTest) { } Y_UNIT_TEST(TestUTF8ToWideScalar) { - TFileInput in(ArcadiaSourceRoot() + TStringBuf("/util/charset/ut/utf8/test1.txt")); + TFileInput in(ArcadiaSourceRoot() + std::string_view("/util/charset/ut/utf8/test1.txt")); - TString text = in.ReadAll(); - TUtf16String wtextSSE = UTF8ToWide(text); - TUtf16String wtextScalar = TUtf16String::Uninitialized(text.size()); + std::string text = in.ReadAll(); + std::u16string wtextSSE = UTF8ToWide(text); + std::u16string wtextScalar = std::u16string::Uninitialized(text.size()); const unsigned char* textBegin = reinterpret_cast(text.c_str()); wchar16* wtextBegin = wtextScalar.begin(); ::NDetail::UTF8ToWideImplScalar(textBegin, textBegin + text.size(), wtextBegin); diff --git a/util/charset/wide.cpp b/util/charset/wide.cpp index a287438ddde..dacb527f5dc 100644 --- a/util/charset/wide.cpp +++ b/util/charset/wide.cpp @@ -31,7 +31,7 @@ namespace { } } -void Collapse(TUtf16String& w) { +void Collapse(std::u16string& w) { CollapseImpl(w, w, 0, IsWhitespace); } @@ -39,7 +39,7 @@ size_t Collapse(wchar16* s, size_t n) { return CollapseImpl(s, n, IsWhitespace); } -TWtringBuf StripLeft(const TWtringBuf text) noexcept { +std::u16string_view StripLeft(const std::u16string_view text) noexcept { const auto* p = text.data(); const auto* const pe = text.data() + text.size(); @@ -49,8 +49,8 @@ TWtringBuf StripLeft(const TWtringBuf text) noexcept { return {p, pe}; } -void StripLeft(TUtf16String& text) { - const auto stripped = StripLeft(TWtringBuf(text)); +void StripLeft(std::u16string& text) { + const auto stripped = StripLeft(std::u16string_view(text)); if (stripped.size() == text.size()) { return; } @@ -58,8 +58,8 @@ void StripLeft(TUtf16String& text) { text = stripped; } -TWtringBuf StripRight(const TWtringBuf text) noexcept { - if (!text) { +std::u16string_view StripRight(const std::u16string_view text) noexcept { + if (text.empty()) { return {}; } @@ -72,8 +72,8 @@ TWtringBuf StripRight(const TWtringBuf text) noexcept { return {pe + 1, p + 1}; } -void StripRight(TUtf16String& text) { - const auto stripped = StripRight(TWtringBuf(text)); +void StripRight(std::u16string& text) { + const auto stripped = StripRight(std::u16string_view(text)); if (stripped.size() == text.size()) { return; } @@ -81,17 +81,17 @@ void StripRight(TUtf16String& text) { text.resize(stripped.size()); } -TWtringBuf Strip(const TWtringBuf text) noexcept { +std::u16string_view Strip(const std::u16string_view text) noexcept { return StripRight(StripLeft(text)); } -void Strip(TUtf16String& text) { +void Strip(std::u16string& text) { StripLeft(text); StripRight(text); } template -static bool IsReductionOnSymbolsTrue(const TWtringBuf text, T&& f) { +static bool IsReductionOnSymbolsTrue(const std::u16string_view text, T&& f) { const auto* p = text.data(); const auto* const pe = text.data() + text.length(); while (p != pe) { @@ -104,15 +104,15 @@ static bool IsReductionOnSymbolsTrue(const TWtringBuf text, T&& f) { return true; } -bool IsLowerWord(const TWtringBuf text) noexcept { +bool IsLowerWord(const std::u16string_view text) noexcept { return IsReductionOnSymbolsTrue(text, [](const wchar32 s) { return IsLower(s); }); } -bool IsUpperWord(const TWtringBuf text) noexcept { +bool IsUpperWord(const std::u16string_view text) noexcept { return IsReductionOnSymbolsTrue(text, [](const wchar32 s) { return IsUpper(s); }); } -bool IsLower(const TWtringBuf text) noexcept { +bool IsLower(const std::u16string_view text) noexcept { return IsReductionOnSymbolsTrue(text, [](const wchar32 s) { if (IsAlpha(s)) { return IsLower(s); @@ -121,7 +121,7 @@ bool IsLower(const TWtringBuf text) noexcept { }); } -bool IsUpper(const TWtringBuf text) noexcept { +bool IsUpper(const std::u16string_view text) noexcept { return IsReductionOnSymbolsTrue(text, [](const wchar32 s) { if (IsAlpha(s)) { return IsUpper(s); @@ -130,8 +130,8 @@ bool IsUpper(const TWtringBuf text) noexcept { }); } -bool IsTitleWord(const TWtringBuf text) noexcept { - if (!text) { +bool IsTitleWord(const std::u16string_view text) noexcept { + if (text.empty()) { return false; } @@ -189,18 +189,18 @@ template static void DetachAndFixPointers(TStringType& text, typename TStringType::value_type*& p, const typename TStringType::value_type*& pe) { const auto pos = p - text.data(); const auto count = pe - p; - p = text.Detach() + pos; + p = text.data() + pos; pe = p + count; } template static bool ModifyStringSymbolwise(TStringType& text, size_t pos, size_t count, F&& f) { - // TODO(yazevnul): this is done for consistency with `TUtf16String::to_lower` and friends + // TODO(yazevnul): this is done for consistency with `std::u16string::to_lower` and friends // at r2914050, maybe worth replacing them with asserts. Also see the same code in `ToTitle`. pos = pos < text.size() ? pos : text.size(); count = count < text.size() - pos ? count : text.size() - pos; - // TUtf16String is refcounted and it's `data` method return pointer to the constant memory. + // std::u16string is refcounted and it's `data` method return pointer to the constant memory. // To simplify the code we do a `const_cast`, though first write to the memory will be done only // after we call `Detach()` and get pointer to a writable piece of memory. auto* p = const_cast(text.data() + pos); @@ -215,28 +215,28 @@ static bool ModifyStringSymbolwise(TStringType& text, size_t pos, size_t count, return false; } -bool ToLower(TUtf16String& text, size_t pos, size_t count) { +bool ToLower(std::u16string& text, size_t pos, size_t count) { const auto f = [](const wchar32 s) { return ToLower(s); }; return ModifyStringSymbolwise(text, pos, count, f); } -bool ToUpper(TUtf16String& text, size_t pos, size_t count) { +bool ToUpper(std::u16string& text, size_t pos, size_t count) { const auto f = [](const wchar32 s) { return ToUpper(s); }; return ModifyStringSymbolwise(text, pos, count, f); } -bool ToLower(TUtf32String& text, size_t pos, size_t count) { +bool ToLower(std::u32string& text, size_t pos, size_t count) { const auto f = [](const wchar32 s) { return ToLower(s); }; return ModifyStringSymbolwise(text, pos, count, f); } -bool ToUpper(TUtf32String& text, size_t pos, size_t count) { +bool ToUpper(std::u32string& text, size_t pos, size_t count) { const auto f = [](const wchar32 s) { return ToUpper(s); }; return ModifyStringSymbolwise(text, pos, count, f); } -bool ToTitle(TUtf16String& text, size_t pos, size_t count) { - if (!text) { +bool ToTitle(std::u16string& text, size_t pos, size_t count) { + if (text.empty()) { return false; } @@ -266,8 +266,8 @@ bool ToTitle(TUtf16String& text, size_t pos, size_t count) { return false; } -bool ToTitle(TUtf32String& text, size_t pos, size_t count) { - if (!text) { +bool ToTitle(std::u32string& text, size_t pos, size_t count) { + if (text.empty()) { return false; } @@ -297,32 +297,32 @@ bool ToTitle(TUtf32String& text, size_t pos, size_t count) { return false; } -TUtf16String ToLowerRet(TUtf16String text, size_t pos, size_t count) { +std::u16string ToLowerRet(std::u16string text, size_t pos, size_t count) { ToLower(text, pos, count); return text; } -TUtf16String ToUpperRet(TUtf16String text, size_t pos, size_t count) { +std::u16string ToUpperRet(std::u16string text, size_t pos, size_t count) { ToUpper(text, pos, count); return text; } -TUtf16String ToTitleRet(TUtf16String text, size_t pos, size_t count) { +std::u16string ToTitleRet(std::u16string text, size_t pos, size_t count) { ToTitle(text, pos, count); return text; } -TUtf32String ToLowerRet(TUtf32String text, size_t pos, size_t count) { +std::u32string ToLowerRet(std::u32string text, size_t pos, size_t count) { ToLower(text, pos, count); return text; } -TUtf32String ToUpperRet(TUtf32String text, size_t pos, size_t count) { +std::u32string ToUpperRet(std::u32string text, size_t pos, size_t count) { ToUpper(text, pos, count); return text; } -TUtf32String ToTitleRet(TUtf32String text, size_t pos, size_t count) { +std::u32string ToTitleRet(std::u32string text, size_t pos, size_t count) { ToTitle(text, pos, count); return text; } @@ -490,12 +490,12 @@ bool ToTitle(wchar32* text, size_t length) noexcept { } template -static TUtf16String ToSmthRet(const TWtringBuf text, size_t pos, size_t count, F&& f) { +static std::u16string ToSmthRet(const std::u16string_view text, size_t pos, size_t count, F&& f) { pos = pos < text.size() ? pos : text.size(); count = count < text.size() - pos ? count : text.size() - pos; - auto res = TUtf16String::Uninitialized(text.size()); - auto* const resBegin = res.Detach(); + std::u16string res(text.size(), '\0'); + auto* const resBegin = res.data(); if (pos) { MemCopy(resBegin, text.data(), pos); @@ -511,12 +511,12 @@ static TUtf16String ToSmthRet(const TWtringBuf text, size_t pos, size_t count, F } template -static TUtf32String ToSmthRet(const TUtf32StringBuf text, size_t pos, size_t count, F&& f) { +static std::u32string ToSmthRet(const std::u32string_view text, size_t pos, size_t count, F&& f) { pos = pos < text.size() ? pos : text.size(); count = count < text.size() - pos ? count : text.size() - pos; - auto res = TUtf32String::Uninitialized(text.size()); - auto* const resBegin = res.Detach(); + std::u32string res(text.size(), '\0'); + auto* const resBegin = res.data(); if (pos) { MemCopy(resBegin, text.data(), pos); @@ -531,53 +531,53 @@ static TUtf32String ToSmthRet(const TUtf32StringBuf text, size_t pos, size_t cou return res; } -TUtf16String ToLowerRet(const TWtringBuf text, size_t pos, size_t count) { +std::u16string ToLowerRet(const std::u16string_view text, size_t pos, size_t count) { return ToSmthRet(text, pos, count, [](const wchar16* theText, size_t length, wchar16* out) { ToLower(theText, length, out); }); } -TUtf16String ToUpperRet(const TWtringBuf text, size_t pos, size_t count) { +std::u16string ToUpperRet(const std::u16string_view text, size_t pos, size_t count) { return ToSmthRet(text, pos, count, [](const wchar16* theText, size_t length, wchar16* out) { ToUpper(theText, length, out); }); } -TUtf16String ToTitleRet(const TWtringBuf text, size_t pos, size_t count) { +std::u16string ToTitleRet(const std::u16string_view text, size_t pos, size_t count) { return ToSmthRet(text, pos, count, [](const wchar16* theText, size_t length, wchar16* out) { ToTitle(theText, length, out); }); } -TUtf32String ToLowerRet(const TUtf32StringBuf text, size_t pos, size_t count) { +std::u32string ToLowerRet(const std::u32string_view text, size_t pos, size_t count) { return ToSmthRet(text, pos, count, [](const wchar32* theText, size_t length, wchar32* out) { ToLower(theText, length, out); }); } -TUtf32String ToUpperRet(const TUtf32StringBuf text, size_t pos, size_t count) { +std::u32string ToUpperRet(const std::u32string_view text, size_t pos, size_t count) { return ToSmthRet(text, pos, count, [](const wchar32* theText, size_t length, wchar32* out) { ToUpper(theText, length, out); }); } -TUtf32String ToTitleRet(const TUtf32StringBuf text, size_t pos, size_t count) { +std::u32string ToTitleRet(const std::u32string_view text, size_t pos, size_t count) { return ToSmthRet(text, pos, count, [](const wchar32* theText, size_t length, wchar32* out) { ToTitle(theText, length, out); }); } template -void EscapeHtmlChars(TUtf16String& str) { - static const TUtf16String lt(LT, Y_ARRAY_SIZE(LT)); - static const TUtf16String gt(GT, Y_ARRAY_SIZE(GT)); - static const TUtf16String amp(AMP, Y_ARRAY_SIZE(AMP)); - static const TUtf16String br(BR, Y_ARRAY_SIZE(BR)); - static const TUtf16String quot(QUOT, Y_ARRAY_SIZE(QUOT)); +void EscapeHtmlChars(std::u16string& str) { + static const std::u16string lt(LT, Y_ARRAY_SIZE(LT)); + static const std::u16string gt(GT, Y_ARRAY_SIZE(GT)); + static const std::u16string amp(AMP, Y_ARRAY_SIZE(AMP)); + static const std::u16string br(BR, Y_ARRAY_SIZE(BR)); + static const std::u16string quot(QUOT, Y_ARRAY_SIZE(QUOT)); size_t escapedLen = 0; - const TUtf16String& cs = str; + const std::u16string& cs = str; for (size_t i = 0; i < cs.size(); ++i) escapedLen += EscapedLen(cs[i]); @@ -585,13 +585,13 @@ void EscapeHtmlChars(TUtf16String& str) { if (escapedLen == cs.size()) return; - TUtf16String res; + std::u16string res; res.reserve(escapedLen); size_t start = 0; for (size_t i = 0; i < cs.size(); ++i) { - const TUtf16String* ent = nullptr; + const std::u16string* ent = nullptr; switch (cs[i]) { case '<': ent = < @@ -622,5 +622,5 @@ void EscapeHtmlChars(TUtf16String& str) { res.swap(str); } -template void EscapeHtmlChars(TUtf16String& str); -template void EscapeHtmlChars(TUtf16String& str); +template void EscapeHtmlChars(std::u16string& str); +template void EscapeHtmlChars(std::u16string& str); diff --git a/util/charset/wide.h b/util/charset/wide.h index 6da3c76252c..6bc2d3f7f28 100644 --- a/util/charset/wide.h +++ b/util/charset/wide.h @@ -6,9 +6,9 @@ #include "wide_specific.h" #include -#include #include #include +#include #include #include #include @@ -24,8 +24,8 @@ class TTempArray; using TCharTemp = TTempArray; namespace NDetail { - inline TString InStringMsg(const char* s, size_t len) { - return (len <= 50) ? " in string " + TString(s, len).Quote() : TString(); + inline std::string InStringMsg(const char* s, size_t len) { + return (len <= 50) ? " in string \"" + EscapeC(std::string(s, len)) + "\"" : std::string(); } template @@ -321,14 +321,14 @@ inline size_t UTF8ToWideImpl(const char* text, size_t len, TCharType* dest, size } template -inline TUtf16String UTF8ToWide(const char* text, size_t len) { - TUtf16String w = TUtf16String::Uninitialized(len); +inline std::u16string UTF8ToWide(const char* text, size_t len) { + std::u16string w(len, '\0'); size_t written; size_t pos = UTF8ToWideImpl(text, len, w.begin(), written); if (pos != len) ythrow yexception() << "failed to decode UTF-8 string at pos " << pos << ::NDetail::InStringMsg(text, len); Y_ASSERT(w.size() >= written); - w.remove(written); + w.erase(written); return w; } @@ -347,8 +347,8 @@ inline bool UTF8ToWide(const char* text, size_t len, TCharType* dest, size_t& wr } template -inline TWtringBuf UTF8ToWide(const TStringBuf src, TUtf16String& dst) { - dst.ReserveAndResize(src.size()); +inline std::u16string_view UTF8ToWide(const std::string_view src, std::u16string& dst) { + dst.resize(src.size()); size_t written = 0; UTF8ToWideImpl(src.data(), src.size(), dst.begin(), written); dst.resize(written); @@ -357,35 +357,35 @@ inline TWtringBuf UTF8ToWide(const TStringBuf src, TUtf16String& dst) { //! if not robust will stop at first error position template -inline TUtf32StringBuf UTF8ToUTF32(const TStringBuf src, TUtf32String& dst) { - dst.ReserveAndResize(src.size()); +inline std::u32string_view UTF8ToUTF32(const std::string_view src, std::u32string& dst) { + dst.resize(src.size()); size_t written = 0; UTF8ToWideImpl(src.data(), src.size(), dst.begin(), written); dst.resize(written); return dst; } -inline TWtringBuf UTF8ToWide(const TStringBuf src, TUtf16String& dst) { +inline std::u16string_view UTF8ToWide(const std::string_view src, std::u16string& dst) { return UTF8ToWide(src, dst); } -inline TUtf16String UTF8ToWide(const char* text, size_t len) { +inline std::u16string UTF8ToWide(const char* text, size_t len) { return UTF8ToWide(text, len); } template -inline TUtf16String UTF8ToWide(const TStringBuf s) { +inline std::u16string UTF8ToWide(const std::string_view s) { return UTF8ToWide(s.data(), s.size()); } template -inline TUtf32String UTF8ToUTF32(const TStringBuf s) { - TUtf32String r; +inline std::u32string UTF8ToUTF32(const std::string_view s) { + std::u32string r; UTF8ToUTF32(s, r); return r; } -inline TUtf16String UTF8ToWide(const TStringBuf s) { +inline std::u16string UTF8ToWide(const std::string_view s) { return UTF8ToWide(s.data(), s.size()); } @@ -410,43 +410,43 @@ constexpr size_t WideToUTF8BufferSize(const size_t inputStringSize) noexcept { return inputStringSize * 4; // * 4 because the conversion functions can convert unicode character into maximum 4 bytes of UTF8 } -inline TStringBuf WideToUTF8(const TWtringBuf src, TString& dst) { - dst.ReserveAndResize(WideToUTF8BufferSize(src.size())); +inline std::string_view WideToUTF8(const std::u16string_view src, std::string& dst) { + dst.resize(WideToUTF8BufferSize(src.size())); size_t written = 0; WideToUTF8(src.data(), src.size(), dst.begin(), written); Y_ASSERT(dst.size() >= written); - dst.remove(written); + dst.erase(written); return dst; } -inline TString WideToUTF8(const wchar16* text, size_t len) { - TString s = TString::Uninitialized(WideToUTF8BufferSize(len)); +inline std::string WideToUTF8(const wchar16* text, size_t len) { + std::string s(WideToUTF8BufferSize(len), '\0'); size_t written = 0; WideToUTF8(text, len, s.begin(), written); Y_ASSERT(s.size() >= written); - s.remove(written); + s.erase(written); return s; } -inline TString WideToUTF8(const wchar32* text, size_t len) { - TString s = TString::Uninitialized(WideToUTF8BufferSize(len)); +inline std::string WideToUTF8(const wchar32* text, size_t len) { + std::string s(WideToUTF8BufferSize(len), '\0'); size_t written = 0; WideToUTF8(text, len, s.begin(), written); Y_ASSERT(s.size() >= written); - s.remove(written); + s.erase(written); return s; } -inline TString WideToUTF8(const TWtringBuf w) { +inline std::string WideToUTF8(const std::u16string_view w) { return WideToUTF8(w.data(), w.size()); } -inline TString WideToUTF8(const TUtf32StringBuf w) { +inline std::string WideToUTF8(const std::u32string_view w) { return WideToUTF8(w.data(), w.size()); } -inline TUtf16String UTF32ToWide(const wchar32* begin, size_t len) { - TUtf16String res; +inline std::u16string UTF32ToWide(const wchar32* begin, size_t len) { + std::u16string res; res.reserve(len); const wchar32* end = begin + len; @@ -631,7 +631,7 @@ inline void Copy(const TChar1* first, size_t len, TChar2* result) { template inline TStringType CopyTo(const TChar* first, const TChar* last) { Y_ASSERT(first <= last); - TStringType str = TStringType::Uninitialized(last - first); + TStringType str(last - first, '\0'); Copy(first, last, str.begin()); return str; } @@ -643,19 +643,19 @@ inline TStringType CopyTo(const TChar* s, size_t n) { return str; } -inline TString WideToASCII(const TWtringBuf w) { +inline std::string WideToASCII(const std::u16string_view w) { Y_ASSERT(IsStringASCII(w.begin(), w.end())); - return CopyTo(w.begin(), w.end()); + return CopyTo(w.begin(), w.end()); } -inline TUtf16String ASCIIToWide(const TStringBuf s) { +inline std::u16string ASCIIToWide(const std::string_view s) { Y_ASSERT(IsStringASCII(s.begin(), s.end())); - return CopyTo(s.begin(), s.end()); + return CopyTo(s.begin(), s.end()); } -inline TUtf32String ASCIIToUTF32(const TStringBuf s) { +inline std::u32string ASCIIToUTF32(const std::string_view s) { Y_ASSERT(IsStringASCII(s.begin(), s.end())); - return CopyTo(s.begin(), s.end()); + return CopyTo(s.begin(), s.end()); } //! returns @c true if string contains whitespace characters only @@ -674,27 +674,27 @@ inline bool IsSpace(const wchar16* s, size_t n) { } //! returns @c true if string contains whitespace characters only -inline bool IsSpace(const TWtringBuf s) { +inline bool IsSpace(const std::u16string_view s) { return IsSpace(s.data(), s.length()); } //! replaces multiple sequential whitespace characters with a single space character -void Collapse(TUtf16String& w); +void Collapse(std::u16string& w); //! @return new length size_t Collapse(wchar16* s, size_t n); //! Removes leading whitespace characters -TWtringBuf StripLeft(const TWtringBuf text) noexcept Y_WARN_UNUSED_RESULT; -void StripLeft(TUtf16String& text); +std::u16string_view StripLeft(const std::u16string_view text) noexcept Y_WARN_UNUSED_RESULT; +void StripLeft(std::u16string& text); //! Removes trailing whitespace characters -TWtringBuf StripRight(const TWtringBuf text) noexcept Y_WARN_UNUSED_RESULT; -void StripRight(TUtf16String& text); +std::u16string_view StripRight(const std::u16string_view text) noexcept Y_WARN_UNUSED_RESULT; +void StripRight(std::u16string& text); //! Removes leading and trailing whitespace characters -TWtringBuf Strip(const TWtringBuf text) noexcept Y_WARN_UNUSED_RESULT; -void Strip(TUtf16String& text); +std::u16string_view Strip(const std::u16string_view text) noexcept Y_WARN_UNUSED_RESULT; +void Strip(std::u16string& text); /* Check if given word is lowercase/uppercase. Will return false if string contains any * non-alphabetical symbols. It is expected that `text` is a correct UTF-16 string. @@ -703,13 +703,13 @@ void Strip(TUtf16String& text); * return false because of the space in the middle of the string. Empty string is also considered * lowercase. */ -bool IsLowerWord(const TWtringBuf text) noexcept; -bool IsUpperWord(const TWtringBuf text) noexcept; +bool IsLowerWord(const std::u16string_view text) noexcept; +bool IsUpperWord(const std::u16string_view text) noexcept; /* Will check if given word starts with capital letter and the rest of the word is lowercase. Will * return `false` for empty string. See also `IsLowerWord`. */ -bool IsTitleWord(const TWtringBuf text) noexcept; +bool IsTitleWord(const std::u16string_view text) noexcept; /* Check if given string is lowercase/uppercase. Will return `true` if all alphabetic symbols are * in proper case, all other symbols are ignored. It is expected that `text` is a correct UTF-16 @@ -720,8 +720,8 @@ bool IsTitleWord(const TWtringBuf text) noexcept; * * NOTE: for any case where `IsLowerWord` returns `true` `IsLower` will also return `true`. */ -bool IsLower(const TWtringBuf text) noexcept; -bool IsUpper(const TWtringBuf text) noexcept; +bool IsLower(const std::u16string_view text) noexcept; +bool IsUpper(const std::u16string_view text) noexcept; /* Lowercase/uppercase given string inplace. Any alphabetic symbol will be converted to a proper * case, the rest of the symbols will be kept the same. It is expected that `text` is a correct @@ -736,8 +736,8 @@ bool IsUpper(const TWtringBuf text) noexcept; * * NOTE: `pos` and `count` are measured in `wchar16`, not in codepoints. */ -bool ToLower(TUtf16String& text, size_t pos = 0, size_t count = TUtf16String::npos); -bool ToUpper(TUtf16String& text, size_t pos = 0, size_t count = TUtf16String::npos); +bool ToLower(std::u16string& text, size_t pos = 0, size_t count = std::u16string::npos); +bool ToUpper(std::u16string& text, size_t pos = 0, size_t count = std::u16string::npos); /* Lowercase/uppercase given string inplace. Any alphabetic symbol will be converted to a proper * case, the rest of the symbols will be kept the same. It is expected that `text` is a correct @@ -752,16 +752,16 @@ bool ToUpper(TUtf16String& text, size_t pos = 0, size_t count = TUtf16String::np * * NOTE: `pos` and `count` are measured in `wchar16`, not in codepoints. */ -bool ToLower(TUtf32String& /*text*/, size_t /*pos*/ = 0, size_t /*count*/ = TUtf16String::npos); -bool ToUpper(TUtf32String& /*text*/, size_t /*pos*/ = 0, size_t /*count*/ = TUtf16String::npos); +bool ToLower(std::u32string& /*text*/, size_t /*pos*/ = 0, size_t /*count*/ = std::u16string::npos); +bool ToUpper(std::u32string& /*text*/, size_t /*pos*/ = 0, size_t /*count*/ = std::u16string::npos); /* Titlecase first symbol and lowercase the rest, see `ToLower` for more details. */ -bool ToTitle(TUtf16String& text, size_t pos = 0, size_t count = TUtf16String::npos); +bool ToTitle(std::u16string& text, size_t pos = 0, size_t count = std::u16string::npos); /* Titlecase first symbol and lowercase the rest, see `ToLower` for more details. */ -bool ToTitle(TUtf32String& /*text*/, size_t /*pos*/ = 0, size_t /*count*/ = TUtf16String::npos); +bool ToTitle(std::u32string& /*text*/, size_t /*pos*/ = 0, size_t /*count*/ = std::u16string::npos); /* @param text Pointer to the string to modify * @param length Length of the string to modify @@ -769,7 +769,7 @@ bool ToTitle(TUtf32String& /*text*/, size_t /*pos*/ = 0, size_t /*count*/ = TUtf * * NOTE: [text, text+length) and [out, out+length) should not interleave. * - * TODO(yazevnul): replace these functions with `bool(const TWtringBuf, const TArrayRef)` + * TODO(yazevnul): replace these functions with `bool(const std::u16string_view, const TArrayRef)` * overload. */ bool ToLower(const wchar16* text, size_t length, wchar16* out) noexcept; @@ -795,27 +795,27 @@ bool ToTitle(wchar32* text, size_t length) noexcept; /* Convenience wrappers for `ToLower`, `ToUpper` and `ToTitle`. */ -TUtf16String ToLowerRet(TUtf16String text, size_t pos = 0, size_t count = TUtf16String::npos) Y_WARN_UNUSED_RESULT; -TUtf16String ToUpperRet(TUtf16String text, size_t pos = 0, size_t count = TUtf16String::npos) Y_WARN_UNUSED_RESULT; -TUtf16String ToTitleRet(TUtf16String text, size_t pos = 0, size_t count = TUtf16String::npos) Y_WARN_UNUSED_RESULT; +std::u16string ToLowerRet(std::u16string text, size_t pos = 0, size_t count = std::u16string::npos) Y_WARN_UNUSED_RESULT; +std::u16string ToUpperRet(std::u16string text, size_t pos = 0, size_t count = std::u16string::npos) Y_WARN_UNUSED_RESULT; +std::u16string ToTitleRet(std::u16string text, size_t pos = 0, size_t count = std::u16string::npos) Y_WARN_UNUSED_RESULT; -TUtf16String ToLowerRet(const TWtringBuf text, size_t pos = 0, size_t count = TWtringBuf::npos) Y_WARN_UNUSED_RESULT; -TUtf16String ToUpperRet(const TWtringBuf text, size_t pos = 0, size_t count = TWtringBuf::npos) Y_WARN_UNUSED_RESULT; -TUtf16String ToTitleRet(const TWtringBuf text, size_t pos = 0, size_t count = TWtringBuf::npos) Y_WARN_UNUSED_RESULT; +std::u16string ToLowerRet(const std::u16string_view text, size_t pos = 0, size_t count = std::u16string_view::npos) Y_WARN_UNUSED_RESULT; +std::u16string ToUpperRet(const std::u16string_view text, size_t pos = 0, size_t count = std::u16string_view::npos) Y_WARN_UNUSED_RESULT; +std::u16string ToTitleRet(const std::u16string_view text, size_t pos = 0, size_t count = std::u16string_view::npos) Y_WARN_UNUSED_RESULT; -TUtf32String ToLowerRet(const TUtf32StringBuf text, size_t pos = 0, size_t count = TWtringBuf::npos) Y_WARN_UNUSED_RESULT; -TUtf32String ToUpperRet(const TUtf32StringBuf text, size_t pos = 0, size_t count = TWtringBuf::npos) Y_WARN_UNUSED_RESULT; -TUtf32String ToTitleRet(const TUtf32StringBuf text, size_t pos = 0, size_t count = TWtringBuf::npos) Y_WARN_UNUSED_RESULT; +std::u32string ToLowerRet(const std::u32string_view text, size_t pos = 0, size_t count = std::u32string_view::npos) Y_WARN_UNUSED_RESULT; +std::u32string ToUpperRet(const std::u32string_view text, size_t pos = 0, size_t count = std::u32string_view::npos) Y_WARN_UNUSED_RESULT; +std::u32string ToTitleRet(const std::u32string_view text, size_t pos = 0, size_t count = std::u32string_view::npos) Y_WARN_UNUSED_RESULT; //! replaces the '<', '>' and '&' characters in string with '<', '>' and '&' respectively // insertBr=true - replace '\r' and '\n' with "
" template -void EscapeHtmlChars(TUtf16String& str); +void EscapeHtmlChars(std::u16string& str); //! returns number of characters in range. Handle surrogate pairs as one character. inline size_t CountWideChars(const wchar16* b, const wchar16* e) { size_t count = 0; - Y_ENSURE(b <= e, TStringBuf("invalid iterators")); + Y_ENSURE(b <= e, "invalid iterators"); while (b < e) { b = SkipSymbol(b, e); ++count; @@ -823,13 +823,13 @@ inline size_t CountWideChars(const wchar16* b, const wchar16* e) { return count; } -inline size_t CountWideChars(const TWtringBuf str) { +inline size_t CountWideChars(const std::u16string_view str) { return CountWideChars(str.begin(), str.end()); } //! checks whether the range is valid UTF-16 sequence inline bool IsValidUTF16(const wchar16* b, const wchar16* e) { - Y_ENSURE(b <= e, TStringBuf("invalid iterators")); + Y_ENSURE(b <= e, "invalid iterators"); while (b < e) { wchar32 symbol = ReadSymbolAndAdvance(b, e); if (symbol == BROKEN_RUNE) @@ -838,6 +838,6 @@ inline bool IsValidUTF16(const wchar16* b, const wchar16* e) { return true; } -inline bool IsValidUTF16(const TWtringBuf str) { +inline bool IsValidUTF16(const std::u16string_view str) { return IsValidUTF16(str.begin(), str.end()); } diff --git a/util/charset/wide_ut.cpp b/util/charset/wide_ut.cpp index 5f702b4ba34..75ed819013c 100644 --- a/util/charset/wide_ut.cpp +++ b/util/charset/wide_ut.cpp @@ -53,7 +53,7 @@ namespace { {0x01C5, 0x10428, 0x10429, 0x10447, 0x10441, 0x1C03, 0x00A0, 0x10428, 0x1043D, 0x10437}, // title }; - TUtf16String CreateUnicodeText() { + std::u16string CreateUnicodeText() { const int len = 256; wchar16 text[len] = { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x00 - 0x0F @@ -77,10 +77,10 @@ namespace { text[i] = static_cast(i + 0x0350); // 0x0410 - 0x044F } } - return TUtf16String(text, len); + return std::u16string(text, len); } - TString CreateUTF8Text() { + std::string CreateUTF8Text() { char text[] = { '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', @@ -107,12 +107,12 @@ namespace { '\xd0', '\xbf', '\xd1', '\x80', '\xd1', '\x81', '\xd1', '\x82', '\xd1', '\x83', '\xd1', '\x84', '\xd1', '\x85', '\xd1', '\x86', '\xd1', '\x87', '\xd1', '\x88', '\xd1', '\x89', '\xd1', '\x8a', '\xd1', '\x8b', '\xd1', '\x8c', '\xd1', '\x8d', '\xd1', '\x8e', '\xd1', '\x8f'}; - return TString(text, Y_ARRAY_SIZE(text)); + return std::string(text, Y_ARRAY_SIZE(text)); } //! use this function to dump UTF8 text into a file in case of any changes // void DumpUTF8Text() { - // TString s = WideToUTF8(UnicodeText); + // std::string s = WideToUTF8(UnicodeText); // std::ofstream f("utf8.txt"); // f << std::hex; // for (int i = 0; i < (int)s.size(); ++i) { @@ -165,8 +165,8 @@ namespace { class TConversionTest: public TTestBase { private: //! @note every of the text can have zeros in the middle - const TUtf16String UnicodeText_; - const TString Utf8Text_; + const std::u16string UnicodeText_; + const std::string Utf8Text_; private: UNIT_TEST_SUITE(TConversionTest); @@ -512,18 +512,18 @@ void TConversionTest::TestWriteUTF8Char() { } static void TestSurrogates(const char* str, const wchar16* wide, size_t wideSize) { - TUtf16String w = UTF8ToWide(str); + std::u16string w = UTF8ToWide(str); UNIT_ASSERT(w.size() == wideSize); UNIT_ASSERT(!memcmp(w.c_str(), wide, wideSize)); - TString s = WideToUTF8(w); + std::string s = WideToUTF8(w); UNIT_ASSERT(s == str); } void TConversionTest::TestUTF8ToWide() { - TUtf16String w = UTF8ToWide(Utf8Text_); + std::u16string w = UTF8ToWide(Utf8Text_); UNIT_ASSERT(w.size() == 256); UNIT_ASSERT(w.size() == UnicodeText_.size()); @@ -568,14 +568,14 @@ void TConversionTest::TestUTF8ToWide() { UNIT_ASSERT_VALUES_EQUAL(WideToUTF8(UTF8ToWide(WideToUTF8(UTF8ToWide( "m\xFB\xB2\xA5\xAA\xAFyeuse.sexwebcamz.com")))), - TString( + std::string( "m\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBDyeuse.sexwebcamz.com")); } void TConversionTest::TestWideToUTF8() { - TString s = WideToUTF8(UnicodeText_); + std::string s = WideToUTF8(UnicodeText_); size_t len = 0; - for (TUtf16String::const_iterator i = UnicodeText_.begin(), ie = UnicodeText_.end(); i != ie; ++i) { + for (std::u16string::const_iterator i = UnicodeText_.begin(), ie = UnicodeText_.end(); i != ie; ++i) { len += UTF8RuneLenByUCS(*i); } @@ -607,7 +607,7 @@ void TConversionTest::TestGetNumOfUTF8Chars() { } void TConversionTest::TestSubstrUTF8() { - TStringBuf utextBuf(utext, sizeof(utext)); + std::string_view utextBuf(utext, sizeof(utext)); UNIT_ASSERT(SubstrUTF8(utextBuf, 0, 2) == utextBuf.substr(0, 4)); UNIT_ASSERT(SubstrUTF8(utextBuf, 1, 1) == utextBuf.substr(2, 2)); UNIT_ASSERT(SubstrUTF8(utextBuf, 1, 2) == utextBuf.substr(2, 4)); @@ -628,7 +628,7 @@ void TConversionTest::TestUnicodeCase() { } void TConversionTest::TestUnicodeDetails() { - TUtf16String temp; + std::u16string temp; for (wchar32 i = 0; i != NUnicode::UnicodeInstancesLimit(); ++i) { temp.clear(); WriteSymbol(i, temp); @@ -661,12 +661,12 @@ class TWideUtilTest: public TTestBase { public: void TestCollapse() { - TUtf16String s; + std::u16string s; s.append(ws, Y_ARRAY_SIZE(ws)).append(3, 'a').append(ws, Y_ARRAY_SIZE(ws)).append(3, 'b').append(ws, Y_ARRAY_SIZE(ws)); Collapse(s); UNIT_ASSERT(s == ASCIIToWide(" aaa bbb ")); { - const TUtf16String w(ASCIIToWide(" a b c ")); + const std::u16string w(ASCIIToWide(" a b c ")); s = w; Collapse(s); UNIT_ASSERT(s == w); @@ -694,7 +694,7 @@ class TWideUtilTest: public TTestBase { Collapse(s); UNIT_ASSERT(s == ASCIIToWide("1 23 ")); { - const TUtf16String w = ASCIIToWide(" "); + const std::u16string w = ASCIIToWide(" "); s = w; Collapse(s); UNIT_ASSERT(s == w); @@ -716,11 +716,11 @@ class TWideUtilTest: public TTestBase { s.clear(); Collapse(s); - UNIT_ASSERT(s == TUtf16String()); + UNIT_ASSERT(s == std::u16string()); } void TestCollapseBuffer() { - TUtf16String s; + std::u16string s; s.append(ws, Y_ARRAY_SIZE(ws)).append(3, 'a').append(ws, Y_ARRAY_SIZE(ws)).append(3, 'b').append(ws, Y_ARRAY_SIZE(ws)); size_t n = Collapse(s.begin(), s.size()); s.resize(n); @@ -784,28 +784,28 @@ class TWideUtilTest: public TTestBase { s.clear(); n = Collapse(s.begin(), s.size()); UNIT_ASSERT(n == 0); - UNIT_ASSERT(s == TUtf16String()); + UNIT_ASSERT(s == std::u16string()); } void TestStrip() { - TUtf16String s; + std::u16string s; Strip(s); - UNIT_ASSERT(s == TUtf16String()); + UNIT_ASSERT(s == std::u16string()); StripLeft(s); - UNIT_ASSERT(s == TUtf16String()); + UNIT_ASSERT(s == std::u16string()); StripRight(s); - UNIT_ASSERT(s == TUtf16String()); + UNIT_ASSERT(s == std::u16string()); s = ASCIIToWide(" \t\r\n"); Strip(s); - UNIT_ASSERT(s == TUtf16String()); + UNIT_ASSERT(s == std::u16string()); s = ASCIIToWide(" \t\r\n"); StripLeft(s); - UNIT_ASSERT(s == TUtf16String()); + UNIT_ASSERT(s == std::u16string()); s = ASCIIToWide(" \t\r\n"); StripRight(s); - UNIT_ASSERT(s == TUtf16String()); + UNIT_ASSERT(s == std::u16string()); s = ASCIIToWide("\t\f\va \r\n"); Strip(s); @@ -827,7 +827,7 @@ class TWideUtilTest: public TTestBase { StripRight(s); UNIT_ASSERT(s == ASCIIToWide("\r\na\r\nb\t\tc")); - const TUtf16String w(ASCIIToWide("a b")); + const std::u16string w(ASCIIToWide("a b")); s = w; Strip(s); UNIT_ASSERT(s == w); @@ -849,25 +849,25 @@ class TWideUtilTest: public TTestBase { } void TestIsSpace() { - UNIT_ASSERT(!IsSpace(TUtf16String())); + UNIT_ASSERT(!IsSpace(std::u16string())); UNIT_ASSERT(IsSpace(ws, Y_ARRAY_SIZE(ws))); - TUtf16String w; - w.assign(ws, Y_ARRAY_SIZE(ws)).append(TUtf16String(1, '!')); + std::u16string w; + w.assign(ws, Y_ARRAY_SIZE(ws)).append(std::u16string(1, '!')); UNIT_ASSERT(!IsSpace(w.c_str(), w.size())); - w.assign(TUtf16String(1, '_')).append(ws, Y_ARRAY_SIZE(ws)); + w.assign(std::u16string(1, '_')).append(ws, Y_ARRAY_SIZE(ws)); UNIT_ASSERT(!IsSpace(w.c_str(), w.size())); - w.assign(ws, Y_ARRAY_SIZE(ws)).append(TUtf16String(1, '$')).append(ws, Y_ARRAY_SIZE(ws)); + w.assign(ws, Y_ARRAY_SIZE(ws)).append(std::u16string(1, '$')).append(ws, Y_ARRAY_SIZE(ws)); UNIT_ASSERT(!IsSpace(w.c_str(), w.size())); } void TestEscapeHtmlChars() { // characters from the first half of the ASCII table for (wchar16 c = 1; c < 0x7F; ++c) { - TUtf16String w(1, c); + std::u16string w(1, c); EscapeHtmlChars(w); switch (c) { @@ -884,13 +884,13 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(w == ASCIIToWide(""")); break; default: - UNIT_ASSERT(w == TUtf16String(1, c)); + UNIT_ASSERT(w == std::u16string(1, c)); break; } } for (wchar16 c = 1; c < 0x7F; ++c) { - TUtf16String w(1, c); + std::u16string w(1, c); EscapeHtmlChars(w); switch (c) { @@ -911,7 +911,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(w == ASCIIToWide("
")); break; default: - UNIT_ASSERT(w == TUtf16String(1, c)); + UNIT_ASSERT(w == std::u16string(1, c)); break; } } @@ -922,7 +922,7 @@ class TWideUtilTest: public TTestBase { wchar16 upperCase[n]; std::copy(wideCyrillicAlphabet, wideCyrillicAlphabet + n, upperCase); ToLower(upperCase, n); - UNIT_ASSERT(TWtringBuf(upperCase, n) == TWtringBuf(wideCyrillicAlphabet + n, n)); + UNIT_ASSERT(std::u16string_view(upperCase, n) == std::u16string_view(wideCyrillicAlphabet + n, n)); } void TestToUpper() { @@ -930,15 +930,15 @@ class TWideUtilTest: public TTestBase { wchar16 lowerCase[n]; std::copy(wideCyrillicAlphabet + n, wideCyrillicAlphabet + n * 2, lowerCase); ToUpper(lowerCase, n); - UNIT_ASSERT(TWtringBuf(lowerCase, n) == TWtringBuf(wideCyrillicAlphabet, n)); + UNIT_ASSERT(std::u16string_view(lowerCase, n) == std::u16string_view(wideCyrillicAlphabet, n)); } void TestWideString() { - const TUtf16String original = UTF32ToWide(WideStringTestData[0], CaseTestDataSize); - const TUtf16String lower = UTF32ToWide(WideStringTestData[1], CaseTestDataSize); - const TUtf16String upper = UTF32ToWide(WideStringTestData[2], CaseTestDataSize); - const TUtf16String title = UTF32ToWide(WideStringTestData[3], CaseTestDataSize); - TUtf16String temp; + const std::u16string original = UTF32ToWide(WideStringTestData[0], CaseTestDataSize); + const std::u16string lower = UTF32ToWide(WideStringTestData[1], CaseTestDataSize); + const std::u16string upper = UTF32ToWide(WideStringTestData[2], CaseTestDataSize); + const std::u16string title = UTF32ToWide(WideStringTestData[3], CaseTestDataSize); + std::u16string temp; temp = original; temp.to_lower(); @@ -966,7 +966,7 @@ class TWideUtilTest: public TTestBase { std::vector buffer(WideStringTestData[0], WideStringTestData[0] + CaseTestDataSize); std::reverse(buffer.begin(), buffer.end()); - const TUtf16String reversed = UTF32ToWide(buffer.data(), buffer.size()); + const std::u16string reversed = UTF32ToWide(buffer.data(), buffer.size()); temp = original; ReverseInPlace(temp); @@ -975,7 +975,7 @@ class TWideUtilTest: public TTestBase { void TestCountWideChars() { UNIT_ASSERT_EQUAL(CountWideChars(UTF8ToWide("привет!")), 7); - TUtf16String wideStr = UTF8ToWide("\xf0\x9f\x92\xb8привет!"); + std::u16string wideStr = UTF8ToWide("\xf0\x9f\x92\xb8привет!"); UNIT_ASSERT_EQUAL(wideStr.size(), 9); UNIT_ASSERT_EQUAL(CountWideChars(wideStr), 8); } @@ -986,11 +986,11 @@ class TWideUtilTest: public TTestBase { static wchar16 str3[] = {'h', 'e', 'l', 'l', 'o', 0xD842, '!', 0}; static wchar16 str4[] = {'h', 'e', 'l', 'l', 'o', 0xDEAD, 0xD842, '!', 0}; static wchar16 str5[] = {'h', 'e', 'l', 'l', 'o', 0xD842, 0xDEAD, 0xDEAD, '!', 0}; - UNIT_ASSERT(IsValidUTF16(TWtringBuf(str1))); - UNIT_ASSERT(IsValidUTF16(TWtringBuf(str2))); - UNIT_ASSERT(!IsValidUTF16(TWtringBuf(str3))); - UNIT_ASSERT(!IsValidUTF16(TWtringBuf(str4))); - UNIT_ASSERT(!IsValidUTF16(TWtringBuf(str5))); + UNIT_ASSERT(IsValidUTF16(std::u16string_view(str1))); + UNIT_ASSERT(IsValidUTF16(std::u16string_view(str2))); + UNIT_ASSERT(!IsValidUTF16(std::u16string_view(str3))); + UNIT_ASSERT(!IsValidUTF16(std::u16string_view(str4))); + UNIT_ASSERT(!IsValidUTF16(std::u16string_view(str5))); } void TestIsStringASCII() { @@ -1042,7 +1042,7 @@ class TWideUtilTest: public TTestBase { } void TestIsLowerWordStr() { - UNIT_ASSERT(IsLowerWord(TWtringBuf())); + UNIT_ASSERT(IsLowerWord(std::u16string_view())); UNIT_ASSERT(IsLowerWord(UTF8ToWide(""))); UNIT_ASSERT(IsLowerWord(UTF8ToWide("test"))); UNIT_ASSERT(IsLowerWord(UTF8ToWide("тест"))); // "тест" is "test" in russian (cyrrilic) @@ -1059,7 +1059,7 @@ class TWideUtilTest: public TTestBase { } void TestIsUpperWordStr() { - UNIT_ASSERT(IsUpperWord(TWtringBuf())); + UNIT_ASSERT(IsUpperWord(std::u16string_view())); UNIT_ASSERT(IsUpperWord(UTF8ToWide(""))); UNIT_ASSERT(IsUpperWord(UTF8ToWide("TEST"))); UNIT_ASSERT(IsUpperWord(UTF8ToWide("ТЕСТ"))); @@ -1076,7 +1076,7 @@ class TWideUtilTest: public TTestBase { } void TestIsTitleStr() { - UNIT_ASSERT(!IsTitleWord(TWtringBuf())); + UNIT_ASSERT(!IsTitleWord(std::u16string_view())); UNIT_ASSERT(!IsTitleWord(UTF8ToWide(""))); UNIT_ASSERT(!IsTitleWord(UTF8ToWide("t"))); UNIT_ASSERT(!IsTitleWord(UTF8ToWide("й"))); @@ -1097,7 +1097,7 @@ class TWideUtilTest: public TTestBase { } void TestIsLowerStr() { - UNIT_ASSERT(IsLower(TWtringBuf())); + UNIT_ASSERT(IsLower(std::u16string_view())); UNIT_ASSERT(IsLower(UTF8ToWide(""))); UNIT_ASSERT(IsLower(UTF8ToWide("test"))); UNIT_ASSERT(IsLower(UTF8ToWide("тест"))); // "тест" is "test" in russian (cyrrilic) @@ -1114,7 +1114,7 @@ class TWideUtilTest: public TTestBase { } void TestIsUpperStr() { - UNIT_ASSERT(IsUpper(TWtringBuf())); + UNIT_ASSERT(IsUpper(std::u16string_view())); UNIT_ASSERT(IsUpper(UTF8ToWide(""))); UNIT_ASSERT(IsUpper(UTF8ToWide("TEST"))); UNIT_ASSERT(IsUpper(UTF8ToWide("ТЕСТ"))); @@ -1134,25 +1134,25 @@ class TWideUtilTest: public TTestBase { // In these test and test for `ToUpper` and `ToTitle` we are checking that string keep // pointing to the same piece of memory we are doing it the following way: // - // TUtf16String s = ... + // std::u16string s = ... // const auto copy = s; // ... // UNIT_ASSERT(s.data() == copy.data()) // - // It saves us a couple lines (we are reusing `copy` later) and if one day `TString` will + // It saves us a couple lines (we are reusing `copy` later) and if one day `std::string` will // become non-refcounted we'll need to rewrite it to something like: // - // TUtf16String s = ... + // std::u16string s = ... // const auto* const data = s.data(); // const auto length = s.length(); // ... // UNIT_ASSERT(s.data() == data); // UNIT_ASSERT(s.length() == length); { - TUtf16String s; + std::u16string s; auto writableCopy = s; const auto copy = s; - const TUtf16String lower; + const std::u16string lower; UNIT_ASSERT(!ToLower(s)); UNIT_ASSERT(s == lower); @@ -1167,13 +1167,13 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == lower); UNIT_ASSERT(ToLowerRet(copy) == lower); - UNIT_ASSERT(ToLowerRet(TWtringBuf(copy)) == lower); + UNIT_ASSERT(ToLowerRet(std::u16string_view(copy)) == lower); } { - TUtf16String s = UTF8ToWide(""); + std::u16string s = UTF8ToWide(""); auto writableCopy = s; const auto copy = s; - const TUtf16String lower; + const std::u16string lower; UNIT_ASSERT(!ToLower(s)); UNIT_ASSERT(s == lower); @@ -1188,12 +1188,12 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == lower); UNIT_ASSERT(ToLowerRet(copy) == lower); - UNIT_ASSERT(ToLowerRet(TWtringBuf(copy)) == lower); + UNIT_ASSERT(ToLowerRet(std::u16string_view(copy)) == lower); } { - TUtf16String s; + std::u16string s; const auto copy = s; - const TUtf16String lower; + const std::u16string lower; UNIT_ASSERT(!ToLower(s, 100500)); UNIT_ASSERT(s == lower); @@ -1202,12 +1202,12 @@ class TWideUtilTest: public TTestBase { #endif UNIT_ASSERT(ToLowerRet(copy, 100500) == lower); - UNIT_ASSERT(ToLowerRet(TWtringBuf(copy), 100500) == lower); + UNIT_ASSERT(ToLowerRet(std::u16string_view(copy), 100500) == lower); } { - TUtf16String s; + std::u16string s; const auto copy = s; - const TUtf16String lower; + const std::u16string lower; UNIT_ASSERT(!ToLower(s, 100500, 1111)); UNIT_ASSERT(s == lower); @@ -1216,7 +1216,7 @@ class TWideUtilTest: public TTestBase { #endif UNIT_ASSERT(ToLowerRet(copy, 100500, 1111) == lower); - UNIT_ASSERT(ToLowerRet(TWtringBuf(copy), 100500, 1111) == lower); + UNIT_ASSERT(ToLowerRet(std::u16string_view(copy), 100500, 1111) == lower); } { auto s = UTF8ToWide("Й"); @@ -1234,7 +1234,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == lower); UNIT_ASSERT(ToLowerRet(copy) == lower); - UNIT_ASSERT(ToLowerRet(TWtringBuf(copy)) == lower); + UNIT_ASSERT(ToLowerRet(std::u16string_view(copy)) == lower); } { auto s = UTF8ToWide("й"); @@ -1255,7 +1255,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == lower); UNIT_ASSERT(ToLowerRet(copy) == lower); - UNIT_ASSERT(ToLowerRet(TWtringBuf(copy)) == lower); + UNIT_ASSERT(ToLowerRet(std::u16string_view(copy)) == lower); } { auto s = UTF8ToWide("тест"); @@ -1276,7 +1276,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == lower); UNIT_ASSERT(ToLowerRet(copy) == lower); - UNIT_ASSERT(ToLowerRet(TWtringBuf(copy)) == lower); + UNIT_ASSERT(ToLowerRet(std::u16string_view(copy)) == lower); } { auto s = UTF8ToWide("Тест"); @@ -1294,10 +1294,10 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == lower); UNIT_ASSERT(ToLowerRet(copy) == lower); - UNIT_ASSERT(ToLowerRet(TWtringBuf(copy)) == lower); + UNIT_ASSERT(ToLowerRet(std::u16string_view(copy)) == lower); } { - TUtf16String s = UTF8ToWide("тЕст"); + std::u16string s = UTF8ToWide("тЕст"); const auto copy = s; const auto lower = UTF8ToWide("тест"); @@ -1305,7 +1305,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(s == UTF8ToWide("тест")); UNIT_ASSERT(ToLowerRet(copy) == lower); - UNIT_ASSERT(ToLowerRet(TWtringBuf(copy)) == lower); + UNIT_ASSERT(ToLowerRet(std::u16string_view(copy)) == lower); } { auto s = UTF8ToWide("тЕст"); @@ -1319,7 +1319,7 @@ class TWideUtilTest: public TTestBase { #endif UNIT_ASSERT(ToLowerRet(copy, 2) == lower); - UNIT_ASSERT(ToLowerRet(TWtringBuf(copy), 2) == lower); + UNIT_ASSERT(ToLowerRet(std::u16string_view(copy), 2) == lower); } { auto s = UTF8ToWide("теСт"); @@ -1330,7 +1330,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(s == lower); UNIT_ASSERT(ToLowerRet(copy, 2) == lower); - UNIT_ASSERT(ToLowerRet(TWtringBuf(copy), 2) == lower); + UNIT_ASSERT(ToLowerRet(std::u16string_view(copy), 2) == lower); } { auto s = UTF8ToWide("теСт"); @@ -1344,7 +1344,7 @@ class TWideUtilTest: public TTestBase { #endif UNIT_ASSERT(ToLowerRet(copy, 3, 1) == lower); - UNIT_ASSERT(ToLowerRet(TWtringBuf(copy), 3, 1) == lower); + UNIT_ASSERT(ToLowerRet(std::u16string_view(copy), 3, 1) == lower); } { auto s = UTF8ToWide("теСт"); @@ -1358,16 +1358,16 @@ class TWideUtilTest: public TTestBase { #endif UNIT_ASSERT(ToLowerRet(copy, 3, 100500) == lower); - UNIT_ASSERT(ToLowerRet(TWtringBuf(copy), 3, 100500) == lower); + UNIT_ASSERT(ToLowerRet(std::u16string_view(copy), 3, 100500) == lower); } } void TestToUpperStr() { { - TUtf16String s; + std::u16string s; auto writableCopy = s; const auto copy = s; - const TUtf16String upper; + const std::u16string upper; UNIT_ASSERT(!ToUpper(s)); UNIT_ASSERT(s == upper); @@ -1382,13 +1382,13 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == upper); UNIT_ASSERT(ToUpperRet(copy) == upper); - UNIT_ASSERT(ToUpperRet(TWtringBuf(copy)) == upper); + UNIT_ASSERT(ToUpperRet(std::u16string_view(copy)) == upper); } { auto s = UTF8ToWide(""); auto writableCopy = s; const auto copy = s; - const TUtf16String upper; + const std::u16string upper; UNIT_ASSERT(!ToUpper(s)); UNIT_ASSERT(s == upper); @@ -1403,13 +1403,13 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == upper); UNIT_ASSERT(ToUpperRet(copy) == upper); - UNIT_ASSERT(ToUpperRet(TWtringBuf(copy)) == upper); + UNIT_ASSERT(ToUpperRet(std::u16string_view(copy)) == upper); } { - TUtf16String s; + std::u16string s; auto writableCopy = s; const auto copy = s; - const TUtf16String upper; + const std::u16string upper; UNIT_ASSERT(!ToUpper(s, 100500)); UNIT_ASSERT(s == upper); @@ -1424,12 +1424,12 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == upper); UNIT_ASSERT(ToUpperRet(copy, 100500) == upper); - UNIT_ASSERT(ToUpperRet(TWtringBuf(copy), 100500) == upper); + UNIT_ASSERT(ToUpperRet(std::u16string_view(copy), 100500) == upper); } { - TUtf16String s; + std::u16string s; const auto copy = s; - const TUtf16String upper; + const std::u16string upper; UNIT_ASSERT(!ToUpper(s, 100500, 1111)); UNIT_ASSERT(s == upper); @@ -1438,7 +1438,7 @@ class TWideUtilTest: public TTestBase { #endif UNIT_ASSERT(ToUpperRet(copy, 100500, 1111) == upper); - UNIT_ASSERT(ToUpperRet(TWtringBuf(copy), 100500, 1111) == upper); + UNIT_ASSERT(ToUpperRet(std::u16string_view(copy), 100500, 1111) == upper); } { auto s = UTF8ToWide("й"); @@ -1456,7 +1456,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == upper); UNIT_ASSERT(ToUpperRet(copy) == upper); - UNIT_ASSERT(ToUpperRet(TWtringBuf(copy)) == upper); + UNIT_ASSERT(ToUpperRet(std::u16string_view(copy)) == upper); } { auto s = UTF8ToWide("Й"); @@ -1477,7 +1477,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == upper); UNIT_ASSERT(ToUpperRet(copy) == upper); - UNIT_ASSERT(ToUpperRet(TWtringBuf(copy)) == upper); + UNIT_ASSERT(ToUpperRet(std::u16string_view(copy)) == upper); } { auto s = UTF8ToWide("тест"); @@ -1495,7 +1495,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == upper); UNIT_ASSERT(ToUpperRet(copy) == upper); - UNIT_ASSERT(ToUpperRet(TWtringBuf(copy)) == upper); + UNIT_ASSERT(ToUpperRet(std::u16string_view(copy)) == upper); } { auto s = UTF8ToWide("Тест"); @@ -1513,7 +1513,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == upper); UNIT_ASSERT(ToUpperRet(copy) == upper); - UNIT_ASSERT(ToUpperRet(TWtringBuf(copy)) == upper); + UNIT_ASSERT(ToUpperRet(std::u16string_view(copy)) == upper); } { auto s = UTF8ToWide("тЕст"); @@ -1531,7 +1531,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == upper); UNIT_ASSERT(ToUpperRet(copy) == upper); - UNIT_ASSERT(ToUpperRet(TWtringBuf(copy)) == upper); + UNIT_ASSERT(ToUpperRet(std::u16string_view(copy)) == upper); } { auto s = UTF8ToWide("тЕст"); @@ -1542,7 +1542,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(s == upper); UNIT_ASSERT(ToUpperRet(copy, 2) == upper); - UNIT_ASSERT(ToUpperRet(TWtringBuf(copy), 2) == upper); + UNIT_ASSERT(ToUpperRet(std::u16string_view(copy), 2) == upper); } { auto s = UTF8ToWide("теСт"); @@ -1553,7 +1553,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(s == upper); UNIT_ASSERT(ToUpperRet(copy, 2) == upper); - UNIT_ASSERT(ToUpperRet(TWtringBuf(copy), 2) == upper); + UNIT_ASSERT(ToUpperRet(std::u16string_view(copy), 2) == upper); } { auto s = UTF8ToWide("теСт"); @@ -1564,7 +1564,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(s == upper); UNIT_ASSERT(ToUpperRet(copy, 3, 1) == upper); - UNIT_ASSERT(ToUpperRet(TWtringBuf(copy), 3, 1) == upper); + UNIT_ASSERT(ToUpperRet(std::u16string_view(copy), 3, 1) == upper); } { auto s = UTF8ToWide("теСт"); @@ -1575,16 +1575,16 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(s == upper); UNIT_ASSERT(ToUpperRet(copy, 3, 100500) == upper); - UNIT_ASSERT(ToUpperRet(TWtringBuf(copy), 3, 100500) == upper); + UNIT_ASSERT(ToUpperRet(std::u16string_view(copy), 3, 100500) == upper); } } void TestToTitleStr() { { - TUtf16String s; + std::u16string s; auto writableCopy = s; const auto copy = s; - const TUtf16String title; + const std::u16string title; UNIT_ASSERT(!ToTitle(s)); UNIT_ASSERT(s == title); @@ -1599,13 +1599,13 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == title); UNIT_ASSERT(ToTitleRet(copy) == title); - UNIT_ASSERT(ToTitleRet(TWtringBuf(copy)) == title); + UNIT_ASSERT(ToTitleRet(std::u16string_view(copy)) == title); } { auto s = UTF8ToWide(""); auto writableCopy = s; const auto copy = s; - const TUtf16String title; + const std::u16string title; UNIT_ASSERT(!ToTitle(s)); UNIT_ASSERT(s == title); @@ -1620,12 +1620,12 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == title); UNIT_ASSERT(ToTitleRet(copy) == title); - UNIT_ASSERT(ToTitleRet(TWtringBuf(copy)) == title); + UNIT_ASSERT(ToTitleRet(std::u16string_view(copy)) == title); } { - TUtf16String s; + std::u16string s; const auto copy = s; - const TUtf16String title; + const std::u16string title; UNIT_ASSERT(!ToTitle(s, 100500)); UNIT_ASSERT(s == title); @@ -1634,12 +1634,12 @@ class TWideUtilTest: public TTestBase { #endif UNIT_ASSERT(ToTitleRet(copy) == title); - UNIT_ASSERT(ToTitleRet(TWtringBuf(copy)) == title); + UNIT_ASSERT(ToTitleRet(std::u16string_view(copy)) == title); } { - TUtf16String s; + std::u16string s; const auto copy = s; - const TUtf16String title; + const std::u16string title; UNIT_ASSERT(!ToTitle(s, 100500, 1111)); UNIT_ASSERT(s == title); @@ -1648,7 +1648,7 @@ class TWideUtilTest: public TTestBase { #endif UNIT_ASSERT(ToTitleRet(copy) == title); - UNIT_ASSERT(ToTitleRet(TWtringBuf(copy)) == title); + UNIT_ASSERT(ToTitleRet(std::u16string_view(copy)) == title); } { auto s = UTF8ToWide("й"); @@ -1666,7 +1666,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == title); UNIT_ASSERT(ToTitleRet(copy) == title); - UNIT_ASSERT(ToTitleRet(TWtringBuf(copy)) == title); + UNIT_ASSERT(ToTitleRet(std::u16string_view(copy)) == title); } { auto s = UTF8ToWide("Й"); @@ -1687,7 +1687,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == title); UNIT_ASSERT(ToTitleRet(copy) == title); - UNIT_ASSERT(ToTitleRet(TWtringBuf(copy)) == title); + UNIT_ASSERT(ToTitleRet(std::u16string_view(copy)) == title); } { auto s = UTF8ToWide("тест"); @@ -1705,7 +1705,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == title); UNIT_ASSERT(ToTitleRet(copy) == title); - UNIT_ASSERT(ToTitleRet(TWtringBuf(copy)) == title); + UNIT_ASSERT(ToTitleRet(std::u16string_view(copy)) == title); } { auto s = UTF8ToWide("Тест"); @@ -1726,7 +1726,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == title); UNIT_ASSERT(ToTitleRet(copy) == title); - UNIT_ASSERT(ToTitleRet(TWtringBuf(copy)) == title); + UNIT_ASSERT(ToTitleRet(std::u16string_view(copy)) == title); } { auto s = UTF8ToWide("тЕст"); @@ -1744,7 +1744,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(writableCopy == title); UNIT_ASSERT(ToTitleRet(copy) == title); - UNIT_ASSERT(ToTitleRet(TWtringBuf(copy)) == title); + UNIT_ASSERT(ToTitleRet(std::u16string_view(copy)) == title); } { auto s = UTF8ToWide("тЕст"); @@ -1755,7 +1755,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(s == title); UNIT_ASSERT(ToTitleRet(copy, 2) == title); - UNIT_ASSERT(ToTitleRet(TWtringBuf(copy), 2) == title); + UNIT_ASSERT(ToTitleRet(std::u16string_view(copy), 2) == title); } { auto s = UTF8ToWide("теСт"); @@ -1769,7 +1769,7 @@ class TWideUtilTest: public TTestBase { #endif UNIT_ASSERT(ToTitleRet(copy, 2) == title); - UNIT_ASSERT(ToTitleRet(TWtringBuf(copy), 2) == title); + UNIT_ASSERT(ToTitleRet(std::u16string_view(copy), 2) == title); } { auto s = UTF8ToWide("теСт"); @@ -1780,7 +1780,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(s == title); UNIT_ASSERT(ToTitleRet(copy, 3, 1) == title); - UNIT_ASSERT(ToTitleRet(TWtringBuf(copy), 3, 1) == title); + UNIT_ASSERT(ToTitleRet(std::u16string_view(copy), 3, 1) == title); } { auto s = UTF8ToWide("теСт"); @@ -1791,7 +1791,7 @@ class TWideUtilTest: public TTestBase { UNIT_ASSERT(s == title); UNIT_ASSERT(ToTitleRet(copy, 3, 100500) == title); - UNIT_ASSERT(ToTitleRet(TWtringBuf(copy), 3, 100500) == title); + UNIT_ASSERT(ToTitleRet(std::u16string_view(copy), 3, 100500) == title); } } }; diff --git a/util/datetime/base.cpp b/util/datetime/base.cpp index 9b4ec5234b7..6b87b6e98e6 100644 --- a/util/datetime/base.cpp +++ b/util/datetime/base.cpp @@ -5,16 +5,15 @@ #include #include #include -#include #include -TString Strftime(const char* format, const struct tm* tm) { +std::string Strftime(const char* format, const struct tm* tm) { size_t size = Max(strlen(format) * 2 + 1, 107); for (;;) { TTempBuf buf(size); int r = strftime(buf.Data(), buf.Size(), format, tm); if (r != 0) { - return TString(buf.Data(), r); + return std::string(buf.Data(), r); } size *= 2; } @@ -22,12 +21,12 @@ TString Strftime(const char* format, const struct tm* tm) { template <> TDuration FromStringImpl(const char* s, size_t len) { - return TDuration::Parse(TStringBuf(s, len)); + return TDuration::Parse(std::string_view(s, len)); } template <> bool TryFromStringImpl(const char* s, size_t len, TDuration& result) { - return TDuration::TryParse(TStringBuf(s, len), result); + return TDuration::TryParse(std::string_view(s, len), result); } namespace { @@ -145,7 +144,7 @@ void Out(IOutputStream& os, TTypeTraits::TFuncParam instant) auto len = FormatDate8601(buf, sizeof(buf), instant.TimeT()); // shouldn't happen due to current implementation of FormatDate8601() and GmTimeR() - Y_ENSURE(len, TStringBuf("Out: year does not fit into an integer")); + Y_ENSURE(len, "Out: year does not fit into an integer"); os.Write(buf, len - 1 /* 'Z' */); WriteMicroSecondsToStream(os, instant.MicroSecondsOfSecond()); @@ -172,54 +171,54 @@ void Out<::NPrivate::TPrintableLocalTime>(IOutputStream& os, TTypeTr WritePrintableLocalTimeToStream(os, localTime); } -TString TDuration::ToString() const { +std::string TDuration::ToString() const { return ::ToString(*this); } -TString TInstant::ToString() const { +std::string TInstant::ToString() const { return ::ToString(*this); } -TString TInstant::ToRfc822String() const { +std::string TInstant::ToRfc822String() const { return FormatGmTime("%a, %d %b %Y %H:%M:%S GMT"); } -TString TInstant::ToStringUpToSeconds() const { +std::string TInstant::ToStringUpToSeconds() const { char buf[64]; auto len = FormatDate8601(buf, sizeof(buf), TimeT()); if (!len) { ythrow yexception() << "TInstant::ToStringUpToSeconds: year does not fit into an integer"; } - return TString(buf, len); + return std::string(buf, len); } -TString TInstant::ToIsoStringLocal() const { +std::string TInstant::ToIsoStringLocal() const { return ::ToString(FormatIsoLocal(*this)); } -TString TInstant::ToStringLocal() const { +std::string TInstant::ToStringLocal() const { return ::ToString(FormatLocal(*this)); } -TString TInstant::ToRfc822StringLocal() const { +std::string TInstant::ToRfc822StringLocal() const { return FormatLocalTime("%a, %d %b %Y %H:%M:%S %Z"); } -TString TInstant::ToIsoStringLocalUpToSeconds() const { +std::string TInstant::ToIsoStringLocalUpToSeconds() const { return ::ToString(FormatIsoLocalUpToSeconds(*this)); } -TString TInstant::ToStringLocalUpToSeconds() const { +std::string TInstant::ToStringLocalUpToSeconds() const { return ::ToString(FormatLocalUpToSeconds(*this)); } -TString TInstant::FormatLocalTime(const char* format) const noexcept { +std::string TInstant::FormatLocalTime(const char* format) const noexcept { struct tm theTm; LocalTime(&theTm); return Strftime(format, &theTm); } -TString TInstant::FormatGmTime(const char* format) const noexcept { +std::string TInstant::FormatGmTime(const char* format) const noexcept { struct tm theTm; GmTime(&theTm); return Strftime(format, &theTm); @@ -272,26 +271,26 @@ void DateToString(char* buf, time_t when, long* sec) { } } -TString DateToString(const struct tm& theTm) { +std::string DateToString(const struct tm& theTm) { char buf[DATE_BUF_LEN]; DateToString(buf, theTm); return buf; } -TString DateToString(time_t when, long* sec) { +std::string DateToString(time_t when, long* sec) { char buf[DATE_BUF_LEN]; DateToString(buf, when, sec); return buf; } -TString YearToString(const struct tm& theTm) { +std::string YearToString(const struct tm& theTm) { Y_ENSURE(0 <= theTm.tm_year + 1900 && theTm.tm_year + 1900 <= 9999, "invalid year " + ToString(theTm.tm_year + 1900) + ", year should be in range [0, 9999]"); char buf[16]; sprintf(buf, "%04d", theTm.tm_year + 1900); return buf; } -TString YearToString(time_t when) { +std::string YearToString(time_t when) { struct tm theTm; localtime_r(&when, &theTm); diff --git a/util/datetime/base.h b/util/datetime/base.h index c43b9775d0e..a3394517128 100644 --- a/util/datetime/base.h +++ b/util/datetime/base.h @@ -5,8 +5,6 @@ #include #include #include -#include -#include #include #include #include @@ -82,16 +80,16 @@ constexpr long TVdiff(timeval r1, timeval r2) { return (1000000 * (r2.tv_sec - r1.tv_sec) + (r2.tv_usec - r1.tv_usec)); } -TString Strftime(const char* format, const struct tm* tm); +std::string Strftime(const char* format, const struct tm* tm); // Use functions below instead of sprint_date (check IGNIETFERRO-892 for details) void DateToString(char* buf, const struct tm& theTm); void DateToString(char* buf, time_t when, long* sec = nullptr); -TString DateToString(const struct tm& theTm); -TString DateToString(time_t when, long* sec = nullptr); +std::string DateToString(const struct tm& theTm); +std::string DateToString(time_t when, long* sec = nullptr); // Year in format "YYYY", throws an exception if year not in range [0, 9999] -TString YearToString(const struct tm& theTm); -TString YearToString(time_t when); +std::string YearToString(const struct tm& theTm); +std::string YearToString(time_t when); template class TTimeBase { @@ -308,9 +306,9 @@ class TDuration: public TTimeBase { } /// parses strings like 10s, 15ms, 15.05s, 20us, or just 25 (s). See parser_ut.cpp for details - static TDuration Parse(const TStringBuf input); + static TDuration Parse(const std::string_view input); - static bool TryParse(const TStringBuf input, TDuration& result); + static bool TryParse(const std::string_view input, TDuration& result); // note global Out method is defined for TDuration, so it could be written to IOutputStream as text @@ -334,7 +332,7 @@ class TDuration: public TTimeBase { return (*this = (*this / t)); } - TString ToString() const; + std::string ToString() const; }; Y_DECLARE_PODTYPE(TDuration); @@ -449,21 +447,21 @@ class TInstant: public TTimeBase { * @returns An ISO 8601 formatted string, e.g. '2015-11-21T23:30:27.991669Z'. * @note Global Out method is defined to TInstant, so it can be written as text to IOutputStream. */ - TString ToString() const; + std::string ToString() const; /** * Formats the instant using the UTC time zone. * * @returns An RFC822 formatted string, e.g. 'Sun, 06 Nov 1994 08:49:37 GMT'. */ - TString ToRfc822String() const; + std::string ToRfc822String() const; /** * Formats the instant using the UTC time zone, with second precision. * * @returns An ISO 8601 formatted string, e.g. '2015-11-21T23:30:27Z'. */ - TString ToStringUpToSeconds() const; + std::string ToStringUpToSeconds() const; /** * Formats the instant using the system time zone, with microsecond precision. @@ -471,7 +469,7 @@ class TInstant: public TTimeBase { * @returns An ISO 8601 / RFC 3339 formatted string, * e.g. '2015-11-22T04:30:27.991669+05:00'. */ - TString ToIsoStringLocal() const; + std::string ToIsoStringLocal() const; /** * Formats the instant using the system time zone, with microsecond precision. @@ -479,14 +477,14 @@ class TInstant: public TTimeBase { * @returns A semi-ISO 8601 formatted string with timezone without colon, * e.g. '2015-11-22T04:30:27.991669+0500'. */ - TString ToStringLocal() const; + std::string ToStringLocal() const; /** * Formats the instant using the system time zone. * * @returns An RFC822 formatted string, e.g. 'Sun, 06 Nov 1994 08:49:37 MSK'. */ - TString ToRfc822StringLocal() const; + std::string ToRfc822StringLocal() const; /** * Formats the instant using the system time zone, with second precision. @@ -494,7 +492,7 @@ class TInstant: public TTimeBase { * @returns An ISO 8601 / RFC 3339 formatted string, * e.g. '2015-11-22T04:30:27+05:00'. */ - TString ToIsoStringLocalUpToSeconds() const; + std::string ToIsoStringLocalUpToSeconds() const; /** * Formats the instant using the system time zone, with second precision. @@ -502,49 +500,49 @@ class TInstant: public TTimeBase { * @returns A semi-ISO 8601 formatted string with timezone without colon, * e.g. '2015-11-22T04:30:27+0500'. */ - TString ToStringLocalUpToSeconds() const; + std::string ToStringLocalUpToSeconds() const; - TString FormatLocalTime(const char* format) const noexcept; - TString FormatGmTime(const char* format) const noexcept; + std::string FormatLocalTime(const char* format) const noexcept; + std::string FormatGmTime(const char* format) const noexcept; /// See #TryParseIso8601. - static TInstant ParseIso8601(TStringBuf); + static TInstant ParseIso8601(std::string_view); /// See #TryParseRfc822. - static TInstant ParseRfc822(TStringBuf); + static TInstant ParseRfc822(std::string_view); /// See #TryParseHttp. - static TInstant ParseHttp(TStringBuf); + static TInstant ParseHttp(std::string_view); /// See #TryParseX509. - static TInstant ParseX509Validity(TStringBuf); + static TInstant ParseX509Validity(std::string_view); /// ISO 8601 Representation of Dates and Times /// /// @link https://www.iso.org/standard/40874.html Description of format. - static bool TryParseIso8601(TStringBuf input, TInstant& instant); + static bool TryParseIso8601(std::string_view input, TInstant& instant); /// RFC 822 Date and Time specification /// /// @link https://tools.ietf.org/html/rfc822#section-5 Description of format. - static bool TryParseRfc822(TStringBuf input, TInstant& instant); + static bool TryParseRfc822(std::string_view input, TInstant& instant); /// RFC 2616 3.3.1 Full Date /// /// @link https://tools.ietf.org/html/rfc2616#section-3.3.1 Description of format. - static bool TryParseHttp(TStringBuf input, TInstant& instant); + static bool TryParseHttp(std::string_view input, TInstant& instant); /// X.509 certificate validity time (see rfc5280 4.1.2.5.*) /// /// @link https://tools.ietf.org/html/rfc5280#section-4.1.2.5 Description of format. - static bool TryParseX509(TStringBuf input, TInstant& instant); + static bool TryParseX509(std::string_view input, TInstant& instant); - static TInstant ParseIso8601Deprecated(TStringBuf); - static TInstant ParseRfc822Deprecated(TStringBuf); - static TInstant ParseHttpDeprecated(TStringBuf); - static TInstant ParseX509ValidityDeprecated(TStringBuf); + static TInstant ParseIso8601Deprecated(std::string_view); + static TInstant ParseRfc822Deprecated(std::string_view); + static TInstant ParseHttpDeprecated(std::string_view); + static TInstant ParseX509ValidityDeprecated(std::string_view); - static bool TryParseIso8601Deprecated(TStringBuf input, TInstant& instant); - static bool TryParseRfc822Deprecated(TStringBuf input, TInstant& instant); - static bool TryParseHttpDeprecated(TStringBuf input, TInstant& instant); - static bool TryParseX509Deprecated(TStringBuf input, TInstant& instant); + static bool TryParseIso8601Deprecated(std::string_view input, TInstant& instant); + static bool TryParseRfc822Deprecated(std::string_view input, TInstant& instant); + static bool TryParseHttpDeprecated(std::string_view input, TInstant& instant); + static bool TryParseX509Deprecated(std::string_view input, TInstant& instant); template inline TInstant& operator+=(const T& t) noexcept { diff --git a/util/datetime/base_ut.cpp b/util/datetime/base_ut.cpp index 56b7496d199..cf13538afa8 100644 --- a/util/datetime/base_ut.cpp +++ b/util/datetime/base_ut.cpp @@ -57,7 +57,7 @@ Y_UNIT_TEST_SUITE(TestSprintDate) { char buf[DATE_BUF_LEN]; DateToString(buf, t); - TString expectedDate = "99991101"; + std::string expectedDate = "99991101"; UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); } @@ -79,7 +79,7 @@ Y_UNIT_TEST_SUITE(TestSprintDate) { char buf[DATE_BUF_LEN]; DateToString(buf, t); - const TString expectedDate = TString("00001101"); + const std::string expectedDate = std::string("00001101"); UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); } @@ -92,7 +92,7 @@ Y_UNIT_TEST_SUITE(TestSprintDate) { char buf[DATE_BUF_LEN]; DateToString(buf, t); - const TString expectedDate = TString("00990101"); + const std::string expectedDate = std::string("00990101"); UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); } @@ -102,7 +102,7 @@ Y_UNIT_TEST_SUITE(TestSprintDate) { char buf[DATE_BUF_LEN]; DateToString(buf, timestamp); - const TString expectedDate = TString("19700101"); + const std::string expectedDate = std::string("19700101"); UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); } @@ -112,14 +112,14 @@ Y_UNIT_TEST_SUITE(TestSprintDate) { char buf[DATE_BUF_LEN]; DateToString(buf, timestamp); - const TString expectedDate = TString("20180427"); + const std::string expectedDate = std::string("20180427"); UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); } - Y_UNIT_TEST(FromTimestampAsTString) { + Y_UNIT_TEST(FromTimestampAsString) { const time_t timestamp = 1524817858; - const TString expectedDate = TString("20180427"); + const std::string expectedDate = std::string("20180427"); UNIT_ASSERT_VALUES_EQUAL(expectedDate, DateToString(timestamp)); } @@ -129,7 +129,7 @@ Y_UNIT_TEST_SUITE(TestSprintDate) { t.tm_mday = 1; t.tm_mon = 0; - TString expectedYear = TString("0099"); + std::string expectedYear = std::string("0099"); UNIT_ASSERT_VALUES_EQUAL(expectedYear, YearToString(t)); } @@ -144,7 +144,7 @@ Y_UNIT_TEST_SUITE(TestSprintDate) { Y_UNIT_TEST(YearToStringAsTimestamp) { const time_t timestamp = 1524817858; - const TString expectedYear = TString("2018"); + const std::string expectedYear = std::string("2018"); UNIT_ASSERT_VALUES_EQUAL(expectedYear, YearToString(timestamp)); } @@ -161,7 +161,7 @@ Y_UNIT_TEST_SUITE(TDateTimeTest) { OldDate8601(buf1, t); sprint_date8601(buf2, t); - UNIT_ASSERT_VALUES_EQUAL(TStringBuf(buf1), TStringBuf(buf2)); + UNIT_ASSERT_VALUES_EQUAL(std::string_view(buf1), std::string_view(buf2)); } } @@ -177,7 +177,7 @@ Y_UNIT_TEST_SUITE(TDateTimeTest) { a.tm_yday == b.tm_yday); } - static inline TString Str(const struct tm& a) { + static inline std::string Str(const struct tm& a) { return TStringBuilder() << "(" << a.tm_sec << ", " << a.tm_min << ", " @@ -411,15 +411,15 @@ Y_UNIT_TEST_SUITE(DateTimeTest) { } Y_UNIT_TEST(TestInstantToString) { - UNIT_ASSERT_VALUES_EQUAL(TString("2009-08-06T15:19:06.023455Z"), ToString(TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455))); - UNIT_ASSERT_VALUES_EQUAL(TString("2022-08-23T13:04:43.023455Z"), ToString(TInstant::Seconds(1661259883) + TDuration::MicroSeconds(23455))); - UNIT_ASSERT_VALUES_EQUAL(TString("2122-11-23T15:12:26.023455Z"), ToString(TInstant::Seconds(4824889946) + TDuration::MicroSeconds(23455))); - UNIT_ASSERT_VALUES_EQUAL(TString("2009-08-06T15:19:06.023455Z"), (TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455)).ToString()); - UNIT_ASSERT_VALUES_EQUAL(TString("2009-08-06T15:19:06Z"), (TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455)).ToStringUpToSeconds()); + UNIT_ASSERT_VALUES_EQUAL(std::string("2009-08-06T15:19:06.023455Z"), ToString(TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455))); + UNIT_ASSERT_VALUES_EQUAL(std::string("2022-08-23T13:04:43.023455Z"), ToString(TInstant::Seconds(1661259883) + TDuration::MicroSeconds(23455))); + UNIT_ASSERT_VALUES_EQUAL(std::string("2122-11-23T15:12:26.023455Z"), ToString(TInstant::Seconds(4824889946) + TDuration::MicroSeconds(23455))); + UNIT_ASSERT_VALUES_EQUAL(std::string("2009-08-06T15:19:06.023455Z"), (TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455)).ToString()); + UNIT_ASSERT_VALUES_EQUAL(std::string("2009-08-06T15:19:06Z"), (TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455)).ToStringUpToSeconds()); } Y_UNIT_TEST(TestInstantToRfc822String) { - UNIT_ASSERT_VALUES_EQUAL(TString("Thu, 06 Aug 2009 15:19:06 GMT"), (TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455)).ToRfc822String()); + UNIT_ASSERT_VALUES_EQUAL(std::string("Thu, 06 Aug 2009 15:19:06 GMT"), (TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455)).ToRfc822String()); } Y_UNIT_TEST(TestInstantMath) { @@ -495,8 +495,8 @@ Y_UNIT_TEST_SUITE(DateTimeTest) { } Y_UNIT_TEST(TestNoexceptConstruction) { - UNIT_ASSERT_EXCEPTION(TDuration::MilliSeconds(FromString(TStringBuf("not a number"))), yexception); - UNIT_ASSERT_EXCEPTION(TDuration::Seconds(FromString(TStringBuf("not a number"))), yexception); + UNIT_ASSERT_EXCEPTION(TDuration::MilliSeconds(FromString(std::string_view("not a number"))), yexception); + UNIT_ASSERT_EXCEPTION(TDuration::Seconds(FromString(std::string_view("not a number"))), yexception); } Y_UNIT_TEST(TestFromValueForTDuration) { diff --git a/util/datetime/cputimer.cpp b/util/datetime/cputimer.cpp index 32ccfd4740e..14d4bd0e590 100644 --- a/util/datetime/cputimer.cpp +++ b/util/datetime/cputimer.cpp @@ -13,7 +13,7 @@ #include #endif -TTimer::TTimer(const TStringBuf message) { +TTimer::TTimer(const std::string_view message) { static const int SMALL_DURATION_CHAR_LENGTH = 9; // strlen("0.123456s") Message_.Reserve(message.length() + SMALL_DURATION_CHAR_LENGTH + 1); // +"\n" Message_ << message; @@ -79,14 +79,14 @@ ui64 TPrecisionTimer::GetCycleCount() const { return ::GetCycleCount() - Start; } -TString FormatCycles(ui64 cycles) { +std::string FormatCycles(ui64 cycles) { ui64 milliseconds = cycles / GetCyclesPerMillisecond(); ui32 ms = ui32(milliseconds % 1000); milliseconds /= 1000; ui32 secs = ui32(milliseconds % 60); milliseconds /= 60; ui32 mins = ui32(milliseconds); - TString result; + std::string result; sprintf(result, "%" PRIu32 " m %.2" PRIu32 " s %.3" PRIu32 " ms", mins, secs, ms); return result; } @@ -116,7 +116,7 @@ TFuncTimer::~TFuncTimer() { Cerr << "leave " << Func_ << " -> " << (TInstant::Now() - Start_) << Endl; } -TTimeLogger::TTimeLogger(const TString& message, bool verbose) +TTimeLogger::TTimeLogger(const std::string& message, bool verbose) : Message(message) , Verbose(verbose) , OK(false) diff --git a/util/datetime/cputimer.h b/util/datetime/cputimer.h index b24d6fae8e4..fb8775e362d 100644 --- a/util/datetime/cputimer.h +++ b/util/datetime/cputimer.h @@ -3,7 +3,6 @@ #include "base.h" #include -#include #include class TTimer { @@ -12,7 +11,7 @@ class TTimer { TStringStream Message_; public: - TTimer(const TStringBuf message = TStringBuf(" took: ")); + TTimer(const std::string_view message = " took: "); ~TTimer(); }; @@ -76,7 +75,7 @@ class TPrecisionTimer { ui64 GetCycleCount() const; }; -TString FormatCycles(ui64 cycles); +std::string FormatCycles(ui64 cycles); class TFormattedPrecisionTimer { private: @@ -113,14 +112,14 @@ class TFakeTimer { class TTimeLogger { private: - TString Message; + std::string Message; bool Verbose; bool OK; time_t Begin; ui64 BeginCycles; public: - TTimeLogger(const TString& message, bool verbose = true); + TTimeLogger(const std::string& message, bool verbose = true); ~TTimeLogger(); void SetOK(); diff --git a/util/datetime/parser.rl6 b/util/datetime/parser.rl6 index 5ea0baedc74..a8ef5401d67 100644 --- a/util/datetime/parser.rl6 +++ b/util/datetime/parser.rl6 @@ -457,23 +457,23 @@ template static inline TResult ParseUnsafe(const char* input, size_t len) { TResult r = Parse(input, len, TResult::Max()); if (ThrowExceptionOnFailure && r == TResult::Max()) - ythrow TDateTimeParseException() << "error in datetime parsing. Input data: " << TStringBuf(input, len); + ythrow TDateTimeParseException() << "error in datetime parsing. Input data: " << std::string_view(input, len); return r; } -TInstant TInstant::ParseIso8601Deprecated(const TStringBuf input) { +TInstant TInstant::ParseIso8601Deprecated(const std::string_view input) { return ParseUnsafe(input.data(), input.size()); } -TInstant TInstant::ParseRfc822Deprecated(const TStringBuf input) { +TInstant TInstant::ParseRfc822Deprecated(const std::string_view input) { return ParseUnsafe(input.data(), input.size()); } -TInstant TInstant::ParseHttpDeprecated(const TStringBuf input) { +TInstant TInstant::ParseHttpDeprecated(const std::string_view input) { return ParseUnsafe(input.data(), input.size()); } -TInstant TInstant::ParseX509ValidityDeprecated(const TStringBuf input) { +TInstant TInstant::ParseX509ValidityDeprecated(const std::string_view input) { switch (input.size()) { case 13: return ParseUnsafe(input.data(), 13); @@ -484,7 +484,7 @@ TInstant TInstant::ParseX509ValidityDeprecated(const TStringBuf input) { } } -bool TInstant::TryParseIso8601Deprecated(const TStringBuf input, TInstant& instant) { +bool TInstant::TryParseIso8601Deprecated(const std::string_view input, TInstant& instant) { const auto parsed = ParseUnsafe(input.data(), input.size()); if (TInstant::Max() == parsed) { return false; @@ -493,7 +493,7 @@ bool TInstant::TryParseIso8601Deprecated(const TStringBuf input, TInstant& insta return true; } -bool TInstant::TryParseRfc822Deprecated(const TStringBuf input, TInstant& instant) { +bool TInstant::TryParseRfc822Deprecated(const std::string_view input, TInstant& instant) { const auto parsed = ParseUnsafe(input.data(), input.size()); if (TInstant::Max() == parsed) { return false; @@ -502,7 +502,7 @@ bool TInstant::TryParseRfc822Deprecated(const TStringBuf input, TInstant& instan return true; } -bool TInstant::TryParseHttpDeprecated(const TStringBuf input, TInstant& instant) { +bool TInstant::TryParseHttpDeprecated(const std::string_view input, TInstant& instant) { const auto parsed = ParseUnsafe(input.data(), input.size()); if (TInstant::Max() == parsed) { return false; @@ -511,7 +511,7 @@ bool TInstant::TryParseHttpDeprecated(const TStringBuf input, TInstant& instant) return true; } -bool TInstant::TryParseX509Deprecated(const TStringBuf input, TInstant& instant) { +bool TInstant::TryParseX509Deprecated(const std::string_view input, TInstant& instant) { TInstant parsed; switch (input.size()) { case 13: @@ -530,19 +530,19 @@ bool TInstant::TryParseX509Deprecated(const TStringBuf input, TInstant& instant) return true; } -TInstant TInstant::ParseIso8601(const TStringBuf input) { +TInstant TInstant::ParseIso8601(const std::string_view input) { return ParseUnsafe(input.data(), input.size()); } -TInstant TInstant::ParseRfc822(const TStringBuf input) { +TInstant TInstant::ParseRfc822(const std::string_view input) { return ParseUnsafe(input.data(), input.size()); } -TInstant TInstant::ParseHttp(const TStringBuf input) { +TInstant TInstant::ParseHttp(const std::string_view input) { return ParseUnsafe(input.data(), input.size()); } -TInstant TInstant::ParseX509Validity(const TStringBuf input) { +TInstant TInstant::ParseX509Validity(const std::string_view input) { switch (input.size()) { case 13: return ParseUnsafe(input.data(), 13); @@ -553,7 +553,7 @@ TInstant TInstant::ParseX509Validity(const TStringBuf input) { } } -bool TInstant::TryParseIso8601(const TStringBuf input, TInstant& instant) { +bool TInstant::TryParseIso8601(const std::string_view input, TInstant& instant) { const auto parsed = ParseUnsafe(input.data(), input.size()); if (TInstant::Max() == parsed) { return false; @@ -562,7 +562,7 @@ bool TInstant::TryParseIso8601(const TStringBuf input, TInstant& instant) { return true; } -bool TInstant::TryParseRfc822(const TStringBuf input, TInstant& instant) { +bool TInstant::TryParseRfc822(const std::string_view input, TInstant& instant) { const auto parsed = ParseUnsafe(input.data(), input.size()); if (TInstant::Max() == parsed) { return false; @@ -571,7 +571,7 @@ bool TInstant::TryParseRfc822(const TStringBuf input, TInstant& instant) { return true; } -bool TInstant::TryParseHttp(const TStringBuf input, TInstant& instant) { +bool TInstant::TryParseHttp(const std::string_view input, TInstant& instant) { const auto parsed = ParseUnsafe(input.data(), input.size()); if (TInstant::Max() == parsed) { return false; @@ -580,7 +580,7 @@ bool TInstant::TryParseHttp(const TStringBuf input, TInstant& instant) { return true; } -bool TInstant::TryParseX509(const TStringBuf input, TInstant& instant) { +bool TInstant::TryParseX509(const std::string_view input, TInstant& instant) { TInstant parsed; switch (input.size()) { case 13: @@ -789,7 +789,7 @@ TDuration TDurationParser::GetResult(TDuration defaultValue) const { return TDuration::MicroSeconds(us); } -bool TDuration::TryParse(const TStringBuf input, TDuration& result) { +bool TDuration::TryParse(const std::string_view input, TDuration& result) { TDuration r = ::Parse(input.data(), input.size(), TDuration::Max()); if (r == TDuration::Max()) return false; @@ -797,6 +797,6 @@ bool TDuration::TryParse(const TStringBuf input, TDuration& result) { return true; } -TDuration TDuration::Parse(const TStringBuf input) { +TDuration TDuration::Parse(const std::string_view input) { return ParseUnsafe(input.data(), input.size()); } diff --git a/util/datetime/parser_deprecated_ut.cpp b/util/datetime/parser_deprecated_ut.cpp index 6ad9f885b1c..fcb6e76e4f6 100644 --- a/util/datetime/parser_deprecated_ut.cpp +++ b/util/datetime/parser_deprecated_ut.cpp @@ -466,53 +466,53 @@ Y_UNIT_TEST_SUITE(TDateTimeParseTestDeprecated) { Y_UNIT_TEST(TestTInstantTryParseDeprecated) { { - const TStringBuf s = "2009-09-19 03:37:08.1+04:00"; + const std::string_view s = "2009-09-19 03:37:08.1+04:00"; const auto i = TInstant::ParseIso8601Deprecated(s); TInstant iTry; UNIT_ASSERT(TInstant::TryParseIso8601Deprecated(s, iTry)); UNIT_ASSERT_VALUES_EQUAL(i, iTry); } { - const TStringBuf s = "2009-09aslkdjfkljasdjfl4:00"; + const std::string_view s = "2009-09aslkdjfkljasdjfl4:00"; TInstant iTry; UNIT_ASSERT_EXCEPTION(TInstant::ParseIso8601Deprecated(s), TDateTimeParseException); UNIT_ASSERT(!TInstant::TryParseIso8601Deprecated(s, iTry)); } { - const TStringBuf s = "Wed, 14 Oct 2009 16:55:33 GMT"; + const std::string_view s = "Wed, 14 Oct 2009 16:55:33 GMT"; const auto i = TInstant::ParseRfc822Deprecated(s); TInstant iTry; UNIT_ASSERT(TInstant::TryParseRfc822Deprecated(s, iTry)); UNIT_ASSERT_VALUES_EQUAL(i, iTry); } { - const TStringBuf s = "Wed, alsdjflkasjdfl:55:33 GMT"; + const std::string_view s = "Wed, alsdjflkasjdfl:55:33 GMT"; TInstant iTry; UNIT_ASSERT_EXCEPTION(TInstant::ParseRfc822Deprecated(s), TDateTimeParseException); UNIT_ASSERT(!TInstant::TryParseRfc822Deprecated(s, iTry)); } { - const TStringBuf s = "20091014165533Z"; + const std::string_view s = "20091014165533Z"; const auto i = TInstant::ParseX509ValidityDeprecated(s); TInstant iTry; UNIT_ASSERT(TInstant::TryParseX509Deprecated(s, iTry)); UNIT_ASSERT_VALUES_EQUAL(i, iTry); } { - const TStringBuf s = "200asdfasdf533Z"; + const std::string_view s = "200asdfasdf533Z"; TInstant iTry; UNIT_ASSERT_EXCEPTION(TInstant::ParseX509ValidityDeprecated(s), TDateTimeParseException); UNIT_ASSERT(!TInstant::TryParseX509Deprecated(s, iTry)); } { - const TStringBuf s = "990104074212Z"; + const std::string_view s = "990104074212Z"; const auto i = TInstant::ParseX509ValidityDeprecated(s); TInstant iTry; UNIT_ASSERT(TInstant::TryParseX509Deprecated(s, iTry)); UNIT_ASSERT_VALUES_EQUAL(i, iTry); } { - const TStringBuf s = "9901asdf4212Z"; + const std::string_view s = "9901asdf4212Z"; TInstant iTry; UNIT_ASSERT_EXCEPTION(TInstant::ParseX509ValidityDeprecated(s), TDateTimeParseException); UNIT_ASSERT(!TInstant::TryParseX509Deprecated(s, iTry)); diff --git a/util/datetime/parser_ut.cpp b/util/datetime/parser_ut.cpp index d8f3ed337a7..507fe3debca 100644 --- a/util/datetime/parser_ut.cpp +++ b/util/datetime/parser_ut.cpp @@ -283,7 +283,7 @@ Y_UNIT_TEST_SUITE(TDateTimeParseTest) { Y_UNIT_TEST(TestIso8601BeforeEpoch) { TIso8601DateTimeParser p; - static constexpr TStringBuf timestamp = "0001-01-01T00:00:00Z"; + static constexpr std::string_view timestamp = "0001-01-01T00:00:00Z"; UNIT_ASSERT(p.ParsePart(timestamp.begin(), timestamp.size())); UNIT_ASSERT_VALUES_EQUAL(p.GetDateTimeFields().Year, 1); } @@ -459,7 +459,7 @@ Y_UNIT_TEST_SUITE(TDateTimeParseTest) { } Y_UNIT_TEST(TestIso8601BigDate) { - std::vector> dates{ + std::vector> dates{ {"2019-01-01", 17897}, {"2037-01-01", 24472}, @@ -523,53 +523,53 @@ Y_UNIT_TEST_SUITE(TDateTimeParseTest) { Y_UNIT_TEST(TestTInstantTryParse) { { - const TStringBuf s = "2009-09-19 03:37:08.1+04:00"; + const std::string_view s = "2009-09-19 03:37:08.1+04:00"; const auto i = TInstant::ParseIso8601(s); TInstant iTry; UNIT_ASSERT(TInstant::TryParseIso8601(s, iTry)); UNIT_ASSERT_VALUES_EQUAL(i, iTry); } { - const TStringBuf s = "2009-09aslkdjfkljasdjfl4:00"; + const std::string_view s = "2009-09aslkdjfkljasdjfl4:00"; TInstant iTry; UNIT_ASSERT_EXCEPTION(TInstant::ParseIso8601(s), TDateTimeParseException); UNIT_ASSERT(!TInstant::TryParseIso8601(s, iTry)); } { - const TStringBuf s = "Wed, 14 Oct 2009 16:55:33 GMT"; + const std::string_view s = "Wed, 14 Oct 2009 16:55:33 GMT"; const auto i = TInstant::ParseRfc822(s); TInstant iTry; UNIT_ASSERT(TInstant::TryParseRfc822(s, iTry)); UNIT_ASSERT_VALUES_EQUAL(i, iTry); } { - const TStringBuf s = "Wed, alsdjflkasjdfl:55:33 GMT"; + const std::string_view s = "Wed, alsdjflkasjdfl:55:33 GMT"; TInstant iTry; UNIT_ASSERT_EXCEPTION(TInstant::ParseRfc822(s), TDateTimeParseException); UNIT_ASSERT(!TInstant::TryParseRfc822(s, iTry)); } { - const TStringBuf s = "20091014165533Z"; + const std::string_view s = "20091014165533Z"; const auto i = TInstant::ParseX509Validity(s); TInstant iTry; UNIT_ASSERT(TInstant::TryParseX509(s, iTry)); UNIT_ASSERT_VALUES_EQUAL(i, iTry); } { - const TStringBuf s = "200asdfasdf533Z"; + const std::string_view s = "200asdfasdf533Z"; TInstant iTry; UNIT_ASSERT_EXCEPTION(TInstant::ParseX509Validity(s), TDateTimeParseException); UNIT_ASSERT(!TInstant::TryParseX509(s, iTry)); } { - const TStringBuf s = "990104074212Z"; + const std::string_view s = "990104074212Z"; const auto i = TInstant::ParseX509Validity(s); TInstant iTry; UNIT_ASSERT(TInstant::TryParseX509(s, iTry)); UNIT_ASSERT_VALUES_EQUAL(i, iTry); } { - const TStringBuf s = "9901asdf4212Z"; + const std::string_view s = "9901asdf4212Z"; TInstant iTry; UNIT_ASSERT_EXCEPTION(TInstant::ParseX509Validity(s), TDateTimeParseException); UNIT_ASSERT(!TInstant::TryParseX509(s, iTry)); diff --git a/util/datetime/systime.cpp b/util/datetime/systime.cpp index aa935b7cf57..f0cf7bd70a0 100644 --- a/util/datetime/systime.cpp +++ b/util/datetime/systime.cpp @@ -262,7 +262,7 @@ struct tm* GmTimeR(const time_t* timer, struct tm* tmbuf) { return tmbuf; } -TString CTimeR(const time_t* timer) { +std::string CTimeR(const time_t* timer) { char sTime[32]; sTime[0] = 0; ctime_r(timer, &sTime[0]); diff --git a/util/datetime/systime.h b/util/datetime/systime.h index 491d36e802a..612b2758d0d 100644 --- a/util/datetime/systime.h +++ b/util/datetime/systime.h @@ -1,15 +1,15 @@ #pragma once #include -#include #include +#include // timegm and gmtime_r versions that don't need access to filesystem or a big stack time_t TimeGM(const struct tm* t); struct tm* GmTimeR(const time_t* timer, struct tm* tmbuf); // safe version of ctime, convinient version of ctime_r -TString CTimeR(const time_t* timer); +std::string CTimeR(const time_t* timer); #ifdef _win_ #include diff --git a/util/datetime/uptime.cpp b/util/datetime/uptime.cpp index 12476a94bfa..fe01610d66c 100644 --- a/util/datetime/uptime.cpp +++ b/util/datetime/uptime.cpp @@ -44,7 +44,7 @@ TDuration Uptime() { return TDuration::MilliSeconds(GetTickCount64()); #elif defined(_linux_) TUnbufferedFileInput in("/proc/uptime"); - TString uptimeStr = in.ReadLine(); + std::string uptimeStr = in.ReadLine(); double up, idle; if (sscanf(uptimeStr.data(), "%lf %lf", &up, &idle) < 2) { ythrow yexception() << "cannot read values from /proc/uptime"; diff --git a/util/digest/city.h b/util/digest/city.h index 675a798074f..95ea515d74c 100644 --- a/util/digest/city.h +++ b/util/digest/city.h @@ -1,8 +1,8 @@ #pragma once #include -#include +#include #include // NOTE: These functions provide CityHash 1.0 implementation whose results are *different* from @@ -46,38 +46,38 @@ inline ui64 Hash128to64(const uint128& x) { namespace NPrivateCityHash { template - inline TStringBuf GetBufFromStr(const TStringType& str) { + inline std::string_view GetBufFromStr(const TStringType& str) { static_assert(std::is_integral>::value, "invalid type passed to hash function"); - return TStringBuf(reinterpret_cast(str.data()), (str.size()) * sizeof(*str.data())); + return std::string_view(reinterpret_cast(str.data()), (str.size()) * sizeof(*str.data())); } } template inline ui64 CityHash64(const TStringType& str) { - TStringBuf buf = NPrivateCityHash::GetBufFromStr(str); + std::string_view buf = NPrivateCityHash::GetBufFromStr(str); return CityHash64(buf.data(), buf.size()); } template inline ui64 CityHash64WithSeeds(const TStringType& str, ui64 seed0, ui64 seed1) { - TStringBuf buf = NPrivateCityHash::GetBufFromStr(str); + std::string_view buf = NPrivateCityHash::GetBufFromStr(str); return CityHash64WithSeeds(buf.data(), buf.size(), seed0, seed1); } template inline ui64 CityHash64WithSeed(const TStringType& str, ui64 seed) { - TStringBuf buf = NPrivateCityHash::GetBufFromStr(str); + std::string_view buf = NPrivateCityHash::GetBufFromStr(str); return CityHash64WithSeed(buf.data(), buf.size(), seed); } template inline uint128 CityHash128(const TStringType& str) { - TStringBuf buf = NPrivateCityHash::GetBufFromStr(str); + std::string_view buf = NPrivateCityHash::GetBufFromStr(str); return CityHash128(buf.data(), buf.size()); } template inline uint128 CityHash128WithSeed(const TStringType& str, uint128 seed) { - TStringBuf buf = NPrivateCityHash::GetBufFromStr(str); + std::string_view buf = NPrivateCityHash::GetBufFromStr(str); return CityHash128WithSeed(buf.data(), buf.size(), seed); } diff --git a/util/digest/city_ut.cpp b/util/digest/city_ut.cpp index 972091c2c3d..36ee94b508b 100644 --- a/util/digest/city_ut.cpp +++ b/util/digest/city_ut.cpp @@ -4,7 +4,7 @@ Y_UNIT_TEST_SUITE(TCityTest) { Y_UNIT_TEST(TestTemplatesCompiling) { - TStringBuf s; + std::string_view s; CityHash64(s); CityHash64WithSeed(s, 1); CityHash64WithSeeds(s, 1, 2); diff --git a/util/digest/fnv_ut.cpp b/util/digest/fnv_ut.cpp index 039e110eaa9..3dec86f397d 100644 --- a/util/digest/fnv_ut.cpp +++ b/util/digest/fnv_ut.cpp @@ -6,18 +6,18 @@ Y_UNIT_TEST_SUITE(TFnvTest) { Y_UNIT_TEST(TestFnv32) { const auto h32 = ULL(2849763999); UNIT_ASSERT_EQUAL(FnvHash("1234567", 7), h32); - UNIT_ASSERT_EQUAL(FnvHash(TStringBuf("1234567")), h32); + UNIT_ASSERT_EQUAL(FnvHash(std::string_view("1234567")), h32); UNIT_ASSERT_EQUAL(FnvHash(nullptr, 0), FNV32INIT); - UNIT_ASSERT_EQUAL(FnvHash(TStringBuf()), FNV32INIT); + UNIT_ASSERT_EQUAL(FnvHash(std::string_view()), FNV32INIT); } Y_UNIT_TEST(TestFnv64) { const auto h64 = ULL(2449551094593701855); UNIT_ASSERT_EQUAL(FnvHash("1234567", 7), h64); - UNIT_ASSERT_EQUAL(FnvHash(TStringBuf("1234567")), h64); + UNIT_ASSERT_EQUAL(FnvHash(std::string_view("1234567")), h64); UNIT_ASSERT_EQUAL(FnvHash(nullptr, 0), FNV64INIT); - UNIT_ASSERT_EQUAL(FnvHash(TStringBuf()), FNV64INIT); + UNIT_ASSERT_EQUAL(FnvHash(std::string_view()), FNV64INIT); } } diff --git a/util/digest/multi_ut.cpp b/util/digest/multi_ut.cpp index dff64ff0cc1..b56d30c97bd 100644 --- a/util/digest/multi_ut.cpp +++ b/util/digest/multi_ut.cpp @@ -14,11 +14,11 @@ class TMultiHashTest: public TTestBase { private: inline void TestStrInt() { - UNIT_ASSERT_EQUAL(MultiHash(TString("1234567"), static_cast(123)), static_cast(ULL(17038203285960021630))); + UNIT_ASSERT_EQUAL(MultiHash(std::string("1234567"), static_cast(123)), static_cast(ULL(17038203285960021630))); } inline void TestIntStr() { - UNIT_ASSERT_EQUAL(MultiHash(static_cast(123), TString("1234567")), static_cast(ULL(9973288649881090712))); + UNIT_ASSERT_EQUAL(MultiHash(static_cast(123), std::string("1234567")), static_cast(ULL(9973288649881090712))); } inline void TestSimpleCollision() { diff --git a/util/digest/multi_ut.pyx b/util/digest/multi_ut.pyx index f803dca7796..196ac18a9db 100644 --- a/util/digest/multi_ut.pyx +++ b/util/digest/multi_ut.pyx @@ -1,5 +1,4 @@ from util.digest.multi cimport MultiHash -from util.generic.string cimport TString import pytest import unittest @@ -8,11 +7,11 @@ import unittest class TestMultiHash(unittest.TestCase): def test_str_int(self): - value = MultiHash(TString(b"1234567"), 123) + value = MultiHash(std::string(b"1234567"), 123) self.assertEqual(value, 17038203285960021630) def test_int_str(self): - value = MultiHash(123, TString(b"1234567")) + value = MultiHash(123, std::string(b"1234567")) self.assertEqual(value, 9973288649881090712) def test_collision(self): diff --git a/util/digest/sequence_ut.cpp b/util/digest/sequence_ut.cpp index 5ea57c69990..704c489cc0c 100644 --- a/util/digest/sequence_ut.cpp +++ b/util/digest/sequence_ut.cpp @@ -15,7 +15,7 @@ class TRangeHashTest: public TTestBase { private: inline void TestStrokaInt() { const size_t canonicalHash = static_cast(ULL(12727184940294366172)); - UNIT_ASSERT_EQUAL(canonicalHash, TRangeHash<>()(TString("12345"))); + UNIT_ASSERT_EQUAL(canonicalHash, TRangeHash<>()(std::string("12345"))); } inline void TestIntVector() { @@ -32,13 +32,13 @@ class TRangeHashTest: public TTestBase { inline void TestMap() { const size_t canonicalHash = static_cast(ULL(4415387926488545605)); - std::map testMap{{"foo", 123}, {"bar", 456}}; + std::map testMap{{"foo", 123}, {"bar", 456}}; UNIT_ASSERT_EQUAL(canonicalHash, TRangeHash<>()(testMap)); } inline void TestCollectionIndependancy() { std::vector testVec = {'a', 'b', 'c'}; - TString testStroka = "abc"; + std::string testStroka = "abc"; UNIT_ASSERT_EQUAL(TRangeHash<>()(testVec), TRangeHash<>()(testStroka)); } }; diff --git a/util/draft/date.cpp b/util/draft/date.cpp index a290c460505..2ab73602f37 100644 --- a/util/draft/date.cpp +++ b/util/draft/date.cpp @@ -36,7 +36,7 @@ static time_t ParseDate(const char* dateStr) { template <> TDate FromStringImpl(const char* data, size_t len) { - return TDate(ParseDate(TString(data, len).data())); + return TDate(ParseDate(std::string(data, len).data())); } TDate::TDate(const char* yyyymmdd) @@ -44,7 +44,7 @@ TDate::TDate(const char* yyyymmdd) { } -TDate::TDate(const TString& yyyymmdd) +TDate::TDate(const std::string& yyyymmdd) : Timestamp(GetDateStart(ParseDate(yyyymmdd.c_str()))) { } @@ -54,7 +54,7 @@ TDate::TDate(time_t ts) { } -TDate::TDate(const TString& date, const TString& format) +TDate::TDate(const std::string& date, const std::string& format) : Timestamp(GetDateStart(ParseDate(date.data(), format.data()))) { } @@ -82,7 +82,7 @@ time_t TDate::GetStartUTC() const { return TimeGM(&dateTm); } -TString TDate::ToStroka(const char* format) const { +std::string TDate::ToStroka(const char* format) const { tm dateTm; localtime_r(&Timestamp, &dateTm); return Strftime(format, &dateTm); diff --git a/util/draft/date.h b/util/draft/date.h index e3eb616fe5d..0be22400235 100644 --- a/util/draft/date.h +++ b/util/draft/date.h @@ -2,10 +2,10 @@ #include #include -#include #include #include +#include // XXX: uses system calls for trivial things. may be very slow therefore. @@ -28,9 +28,9 @@ class TDate { } TDate(const char* yyyymmdd); - TDate(const TString& yyyymmdd); + TDate(const std::string& yyyymmdd); TDate(unsigned year, unsigned month, unsigned monthDay); // month from 01, monthDay from 01 - TDate(const TString& date, const TString& format); + TDate(const std::string& date, const std::string& format); explicit TDate(time_t t); @@ -40,7 +40,7 @@ class TDate { time_t GetStartUTC() const; - TString ToStroka(const char* format = "%Y%m%d") const; + std::string ToStroka(const char* format = "%Y%m%d") const; TDate& operator++() { Timestamp = GetDateStart(Timestamp + 3 * (SECONDS_IN_DAY / 2)); @@ -117,7 +117,7 @@ inline int operator-(const TDate& left, const TDate& right) { } inline IInputStream& operator>>(IInputStream& left, TDate& right) { - TString stroka; + std::string stroka; left >> stroka; TDate date(stroka.c_str()); right = date; diff --git a/util/draft/datetime.cpp b/util/draft/datetime.cpp index d290021d5e2..d77c6718b00 100644 --- a/util/draft/datetime.cpp +++ b/util/draft/datetime.cpp @@ -177,7 +177,7 @@ namespace NDatetime { } } - TString TSimpleTM::ToString(const char* fmt) const { + std::string TSimpleTM::ToString(const char* fmt) const { struct tm t = *this; return Strftime(fmt, &t); } diff --git a/util/draft/datetime.h b/util/draft/datetime.h index 7301e32e501..504a931c2f6 100644 --- a/util/draft/datetime.h +++ b/util/draft/datetime.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include #include @@ -70,7 +69,7 @@ namespace NDatetime { // keeps the object consistent TSimpleTM& Add(EField f, i32 amount = 1); - TString ToString(const char* fmt = "%a, %d %b %Y %H:%M:%S %z") const; + std::string ToString(const char* fmt = "%a, %d %b %Y %H:%M:%S %z") const; TSimpleTM& ToUTC() { return *this = New(AsTimeT()); @@ -133,16 +132,16 @@ namespace NDatetime { }; } -inline TString date2str(const time_t date) { +inline std::string date2str(const time_t date) { struct tm dateTm; memset(&dateTm, 0, sizeof(dateTm)); localtime_r(&date, &dateTm); char buf[9]; strftime(buf, sizeof(buf), "%Y%m%d", &dateTm); - return TString(buf); + return std::string(buf); } -inline time_t str2date(const TString& dateStr) { +inline time_t str2date(const std::string& dateStr) { struct tm dateTm; memset(&dateTm, 0, sizeof(tm)); strptime(dateStr.data(), "%Y%m%d", &dateTm); diff --git a/util/draft/datetime_ut.cpp b/util/draft/datetime_ut.cpp index a5e065ef6e8..017c962cb88 100644 --- a/util/draft/datetime_ut.cpp +++ b/util/draft/datetime_ut.cpp @@ -5,19 +5,19 @@ #include Y_UNIT_TEST_SUITE(TSimpleTMTest) { - TString PrintMarker(const TString& test, int line) { + std::string PrintMarker(const std::string& test, int line) { return TStringBuilder() << "test " << test << " at line " << line; } - TString JoinMarker(const TString& marker, time_t t) { + std::string JoinMarker(const std::string& marker, time_t t) { return TStringBuilder() << marker << " (tstamp=" << t << ")"; } - TString PrintMarker(const TString& test, int line, time_t t) { + std::string PrintMarker(const std::string& test, int line, time_t t) { return JoinMarker(PrintMarker(test, line), t); } - void AssertStructTmEqual(const TString& marker, const struct tm& tmt, const NDatetime::TSimpleTM& tms) { + void AssertStructTmEqual(const std::string& marker, const struct tm& tmt, const NDatetime::TSimpleTM& tms) { UNIT_ASSERT_VALUES_EQUAL_C((int)tms.Sec, tmt.tm_sec, marker); UNIT_ASSERT_VALUES_EQUAL_C((int)tms.Min, tmt.tm_min, marker); UNIT_ASSERT_VALUES_EQUAL_C((int)tms.Hour, tmt.tm_hour, marker); @@ -32,10 +32,10 @@ Y_UNIT_TEST_SUITE(TSimpleTMTest) { #endif } - void AssertSimpleTM(const TString& mark, + void AssertSimpleTM(const std::string& mark, const NDatetime::TSimpleTM& tms, time_t tstamp, ui32 year, ui32 mon, ui32 mday, ui32 hour, ui32 minu, ui32 sec) { - TString marker = JoinMarker(mark, tstamp); + std::string marker = JoinMarker(mark, tstamp); struct tm tmt; Zero(tmt); GmTimeR(&tstamp, &tmt); diff --git a/util/draft/enum.h b/util/draft/enum.h index 18002b7df20..0260c676de1 100644 --- a/util/draft/enum.h +++ b/util/draft/enum.h @@ -2,7 +2,6 @@ #include -#include #include #include #include @@ -37,8 +36,8 @@ const V* FindEnumFromStringImpl(const char* key, const std::pair } template -TString PrintEnumItemsImpl(const std::pair* entries, size_t arraySize) { - TString result; +std::string PrintEnumItemsImpl(const std::pair* entries, size_t arraySize) { + std::string result; TStringOutput out(result); for (size_t i = 0; i < arraySize; i++) out << (i ? ", " : "") << "'" << entries[i].first << "'"; @@ -47,8 +46,8 @@ TString PrintEnumItemsImpl(const std::pair* entries, size_t arraySize) { // special version for const char* template -TString PrintEnumItemsImpl(const std::pair* entries, size_t arraySize) { - TString result; +std::string PrintEnumItemsImpl(const std::pair* entries, size_t arraySize) { + std::string result; TStringOutput out(result); for (size_t i = 0; i < arraySize; i++) out << (i ? ", " : "") << "'" << (entries[i].first ? entries[i].first : "") << "'"; @@ -91,7 +90,7 @@ inline void SetEnumFlagsForEmptySpec(B& flags, bool allIfEmpty) { // all set by default template -inline void SetEnumFlags(const std::pair (&str2Enum)[N], TStringBuf optSpec, +inline void SetEnumFlags(const std::pair (&str2Enum)[N], std::string_view optSpec, std::bitset& flags, bool allIfEmpty = true) { if (optSpec.empty()) { SetEnumFlagsForEmptySpec(flags, allIfEmpty); @@ -105,7 +104,7 @@ inline void SetEnumFlags(const std::pair (&str2Enum)[N], TString } template -inline void SetEnumFlags(const std::pair* str2Enum, TStringBuf optSpec, +inline void SetEnumFlags(const std::pair* str2Enum, std::string_view optSpec, std::bitset& flags, const size_t size, bool allIfEmpty = true) { if (optSpec.empty()) { @@ -121,7 +120,7 @@ inline void SetEnumFlags(const std::pair* str2Enum, TStringBuf o // for enums generated with GENERATE_ENUM_SERIALIZATION template -inline void SetEnumFlags(TStringBuf optSpec, std::bitset& flags, bool allIfEmpty = true) { +inline void SetEnumFlags(std::string_view optSpec, std::bitset& flags, bool allIfEmpty = true) { if (optSpec.empty()) { SetEnumFlagsForEmptySpec(flags, allIfEmpty); } else { diff --git a/util/draft/holder_vector.h b/util/draft/holder_vector.h index fcbff29235c..eec4e10a477 100644 --- a/util/draft/holder_vector.h +++ b/util/draft/holder_vector.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include template diff --git a/util/draft/holder_vector_ut.cpp b/util/draft/holder_vector_ut.cpp index f64393860af..8f6d571f7fc 100644 --- a/util/draft/holder_vector_ut.cpp +++ b/util/draft/holder_vector_ut.cpp @@ -60,8 +60,8 @@ Y_UNIT_TEST_SUITE(THolderVectorTest) { } Y_UNIT_TEST(TestUniquePtr) { - THolderVector v; - std::unique_ptr str(new TString("hello")); + THolderVector v; + std::unique_ptr str(new std::string("hello")); v.PushBack(std::move(str)); UNIT_ASSERT(v.Size() == 1); UNIT_ASSERT(str.get() == nullptr); diff --git a/util/draft/ip.h b/util/draft/ip.h index c894b72e447..2d2acfa7c85 100644 --- a/util/draft/ip.h +++ b/util/draft/ip.h @@ -79,10 +79,10 @@ static inline char* Ip6ToString(const TIp6& ip, char* buf, size_t len) { return buf; } -static inline TString Ip6ToString(const TIp6& ip) { +static inline std::string Ip6ToString(const TIp6& ip) { char buf[INET6_ADDRSTRLEN]; - return TString(Ip6ToString(ip, buf, sizeof(buf))); + return std::string(Ip6ToString(ip, buf, sizeof(buf))); } template <> @@ -105,7 +105,7 @@ static inline TIp4Or6 Ip4Or6FromString(const char* ipStr) { ythrow TSystemError() << "Failed to convert (" << ipStr << ") to ipv4 or ipv6 address"; } -static inline TString Ip4Or6ToString(const TIp4Or6& ip) { +static inline std::string Ip4Or6ToString(const TIp4Or6& ip) { if (std::holds_alternative(ip)) { return Ip6ToString(std::get(ip)); } else { diff --git a/util/folder/dirut.cpp b/util/folder/dirut.cpp index ffc9b09f967..bd1dfcd79bf 100644 --- a/util/folder/dirut.cpp +++ b/util/folder/dirut.cpp @@ -11,12 +11,28 @@ #include #include -void SlashFolderLocal(TString& folder) { - if (!folder) +namespace NStringPrivate { + template + size_t GetStringLengthWithLimit(const TCharType* s, size_t maxlen) { + Y_ASSERT(s); + size_t i = 0; + for (; i != maxlen && s[i]; ++i) + ; + return i; + } + + inline size_t GetStringLengthWithLimit(const char* s, size_t maxlen) { + Y_ASSERT(s); + return strnlen(s, maxlen); + } +} + +void SlashFolderLocal(std::string& folder) { + if (folder.empty()) return; #ifdef _win32_ size_t pos; - while ((pos = folder.find('/')) != TString::npos) + while ((pos = folder.find('/')) != std::string::npos) folder.replace(pos, 1, LOCSLASH_S); #endif if (folder[folder.size() - 1] != LOCSLASH_C) @@ -25,13 +41,13 @@ void SlashFolderLocal(TString& folder) { #ifndef _win32_ -bool correctpath(TString& folder) { +bool correctpath(std::string& folder) { return resolvepath(folder, "/"); } -bool resolvepath(TString& folder, const TString& home) { - Y_ASSERT(home && home.at(0) == '/'); - if (!folder) { +bool resolvepath(std::string& folder, const std::string& home) { + Y_ASSERT(!home.empty() && home.at(0) == '/'); + if (folder.empty()) { return false; } // may be from windows @@ -348,7 +364,7 @@ int resolvepath(char* apath, const char* rpath, const char* cpath) { return correctpath(apath, tpath); } -bool correctpath(TString& filename) { +bool correctpath(std::string& filename) { char* ptr = (char*)alloca(filename.size() + 2); if (correctpath(ptr, filename.data())) { filename = ptr; @@ -357,7 +373,7 @@ bool correctpath(TString& filename) { return false; } -bool resolvepath(TString& folder, const TString& home) { +bool resolvepath(std::string& folder, const std::string& home) { char* ptr = (char*)alloca(folder.size() + 3 + home.size()); if (resolvepath(ptr, folder.data(), home.data())) { folder = ptr; @@ -376,7 +392,7 @@ const char* GetDirectorySeparatorS() { return LOCSLASH_S; } -void RemoveDirWithContents(TString dirName) { +void RemoveDirWithContents(std::string dirName) { SlashFolderLocal(dirName); TDirIterator dir(dirName, TDirIterator::TOptions(FTS_NOSTAT)); @@ -402,7 +418,7 @@ int mkpath(char* path, int mode) { // Implementation of realpath in FreeBSD (version 9.0 and less) and GetFullPathName in Windows // did not require last component of the file name to exist (other implementations will fail // if it does not). Use RealLocation if that behaviour is required. -TString RealPath(const TString& path) { +std::string RealPath(const std::string& path) { TTempBuf result; Y_ASSERT(result.Size() > MAX_PATH); //TMP_BUF_LEN > MAX_PATH #ifdef _win_ @@ -414,10 +430,10 @@ TString RealPath(const TString& path) { return result.Data(); } -TString RealLocation(const TString& path) { +std::string RealLocation(const std::string& path) { if (NFs::Exists(path)) return RealPath(path); - TString dirpath = GetDirName(path); + std::string dirpath = GetDirName(path); if (NFs::Exists(dirpath)) return RealPath(dirpath) + GetDirectorySeparatorS() + GetFileNameComponent(path.data()); ythrow TFileError() << "RealLocation failed \"" << path << "\""; @@ -426,7 +442,7 @@ TString RealLocation(const TString& path) { int MakeTempDir(char path[/*FILENAME_MAX*/], const char* prefix) { int ret; - TString sysTmp; + std::string sysTmp; #ifdef _win32_ if (!prefix || *prefix == '/') { @@ -449,17 +465,17 @@ int MakeTempDir(char path[/*FILENAME_MAX*/], const char* prefix) { return 0; } -bool IsDir(const TString& path) { +bool IsDir(const std::string& path) { return TFileStat(path).IsDir(); } -TString GetHomeDir() { - TString s(getenv("HOME")); - if (!s) { +std::string GetHomeDir() { + std::string s(getenv("HOME")); + if (s.empty()) { #ifndef _win32_ passwd* pw = nullptr; s = getenv("USER"); - if (s) + if (!s.empty()) pw = getpwnam(s.data()); else pw = getpwuid(getuid()); @@ -505,14 +521,14 @@ const char* GetFileNameComponent(const char* f) { return f; } -TString GetSystemTempDir() { +std::string GetSystemTempDir() { #ifdef _win_ char buffer[1024]; DWORD size = GetTempPath(1024, buffer); if (!size) { ythrow TSystemError() << "failed to get system temporary directory"; } - return TString(buffer, size); + return std::string(buffer, size); #else const char* var = "TMPDIR"; const char* def = "/tmp"; @@ -522,11 +538,11 @@ TString GetSystemTempDir() { #endif } -TString ResolveDir(const char* path) { +std::string ResolveDir(const char* path) { return ResolvePath(path, true); } -bool SafeResolveDir(const char* path, TString& result) { +bool SafeResolveDir(const char* path, std::string& result) { try { result = ResolvePath(path, true); return true; @@ -535,7 +551,7 @@ bool SafeResolveDir(const char* path, TString& result) { } } -TString GetDirName(const TString& path) { +std::string GetDirName(const std::string& path) { return TFsPath(path).Dirname(); } @@ -548,12 +564,12 @@ char* realpath(const char* pathname, char resolved_path[MAXPATHLEN]) { #endif -TString GetBaseName(const TString& path) { +std::string GetBaseName(const std::string& path) { return TFsPath(path).Basename(); } static bool IsAbsolutePath(const char* str) { - return str && TPathSplitTraitsLocal::IsAbsolutePath(TStringBuf(str, NStringPrivate::GetStringLengthWithLimit(str, 3))); + return str && TPathSplitTraitsLocal::IsAbsolutePath(std::string_view(str, NStringPrivate::GetStringLengthWithLimit(str, 3))); } int ResolvePath(const char* rel, const char* abs, char res[/*MAXPATHLEN*/], bool isdir) { @@ -601,21 +617,21 @@ int ResolvePath(const char* rel, const char* abs, char res[/*MAXPATHLEN*/], bool return 0; } -TString ResolvePath(const char* rel, const char* abs, bool isdir) { +std::string ResolvePath(const char* rel, const char* abs, bool isdir) { char buf[PATH_MAX]; if (ResolvePath(rel, abs, buf, isdir)) ythrow yexception() << "cannot resolve path: \"" << rel << "\""; return buf; } -TString ResolvePath(const char* path, bool isDir) { +std::string ResolvePath(const char* path, bool isDir) { return ResolvePath(path, nullptr, isDir); } -TString StripFileComponent(const TString& fileName) { - TString dir = IsDir(fileName) ? fileName : GetDirName(fileName); +std::string StripFileComponent(const std::string& fileName) { + std::string dir = IsDir(fileName) ? fileName : GetDirName(fileName); if (!dir.empty() && dir.back() != GetDirectorySeparator()) { - dir.append(GetDirectorySeparator()); + dir.push_back(GetDirectorySeparator()); } return dir; } diff --git a/util/folder/dirut.h b/util/folder/dirut.h index 2537027b12d..d1649d83e3d 100644 --- a/util/folder/dirut.h +++ b/util/folder/dirut.h @@ -3,7 +3,6 @@ #include #include #include -#include #include #include @@ -37,57 +36,57 @@ char* mkdtemp(char* path); #endif #endif -bool IsDir(const TString& path); +bool IsDir(const std::string& path); int mkpath(char* path, int mode = 0777); -TString GetHomeDir(); +std::string GetHomeDir(); void MakeDirIfNotExist(const char* path, int mode = 0777); -inline void MakeDirIfNotExist(const TString& path, int mode = 0777) { +inline void MakeDirIfNotExist(const std::string& path, int mode = 0777) { MakeDirIfNotExist(path.data(), mode); } /// Create path making parent directories as needed void MakePathIfNotExist(const char* path, int mode = 0777); -void SlashFolderLocal(TString& folder); -bool correctpath(TString& filename); -bool resolvepath(TString& folder, const TString& home); +void SlashFolderLocal(std::string& folder); +bool correctpath(std::string& filename); +bool resolvepath(std::string& folder, const std::string& home); char GetDirectorySeparator(); const char* GetDirectorySeparatorS(); -void RemoveDirWithContents(TString dirName); +void RemoveDirWithContents(std::string dirName); const char* GetFileNameComponent(const char* f); -inline TString GetFileNameComponent(const TString& f) { +inline std::string GetFileNameComponent(const std::string& f) { return GetFileNameComponent(f.data()); } /// RealPath doesn't guarantee trailing separator to be stripped or left in place for directories. -TString RealPath(const TString& path); // throws -TString RealLocation(const TString& path); /// throws; last file name component doesn't need to exist +std::string RealPath(const std::string& path); // throws +std::string RealLocation(const std::string& path); /// throws; last file name component doesn't need to exist -TString GetSystemTempDir(); +std::string GetSystemTempDir(); int MakeTempDir(char path[/*FILENAME_MAX*/], const char* prefix); int ResolvePath(const char* rel, const char* abs, char res[/*FILENAME_MAX*/], bool isdir = false); -TString ResolvePath(const char* rel, const char* abs, bool isdir = false); -TString ResolvePath(const char* path, bool isDir = false); +std::string ResolvePath(const char* rel, const char* abs, bool isdir = false); +std::string ResolvePath(const char* path, bool isDir = false); -TString ResolveDir(const char* path); +std::string ResolveDir(const char* path); -bool SafeResolveDir(const char* path, TString& result); +bool SafeResolveDir(const char* path, std::string& result); -TString GetDirName(const TString& path); +std::string GetDirName(const std::string& path); -TString GetBaseName(const TString& path); +std::string GetBaseName(const std::string& path); -TString StripFileComponent(const TString& fileName); +std::string StripFileComponent(const std::string& fileName); class TExistenceChecker { public: diff --git a/util/folder/dirut_ut.cpp b/util/folder/dirut_ut.cpp index dbfabdd059c..a133d9f9a65 100644 --- a/util/folder/dirut_ut.cpp +++ b/util/folder/dirut_ut.cpp @@ -3,7 +3,6 @@ #include -#include #include #include @@ -16,15 +15,15 @@ Y_UNIT_TEST_SUITE(TDirutTest) { UNIT_ASSERT(IsDir(RealLocation("."))); TTempDir tempDir; - TString base = RealPath(tempDir()); + std::string base = RealPath(tempDir()); UNIT_ASSERT(!base.empty()); if (base.back() == GetDirectorySeparator()) { base.pop_back(); } - TString path; - TString pathNotNorm; + std::string path; + std::string pathNotNorm; path = base + GetDirectorySeparatorS() + "no_such_file"; UNIT_ASSERT(NFs::Exists(GetDirName(path))); @@ -57,7 +56,7 @@ Y_UNIT_TEST_SUITE(TDirutTest) { } void DoTest(const char* p, const char* base, const char* canon) { - TString path(p); + std::string path(p); UNIT_ASSERT(resolvepath(path, base)); UNIT_ASSERT(path == canon); } @@ -82,13 +81,13 @@ Y_UNIT_TEST_SUITE(TDirutTest) { Y_UNIT_TEST(TestResolvePathRelative) { TTempDir tempDir; TTempBuf tempBuf; - TString base = RealPath(tempDir()); + std::string base = RealPath(tempDir()); if (base.back() == GetDirectorySeparator()) { base.pop_back(); } // File - TString path = base + GetDirectorySeparatorS() + "file"; + std::string path = base + GetDirectorySeparatorS() + "file"; { TFixedBufferFileOutput file(path); } @@ -112,9 +111,9 @@ Y_UNIT_TEST_SUITE(TDirutTest) { } Y_UNIT_TEST(TestStripFileComponent) { - static const TString tmpDir = "tmp_dir_for_tests"; - static const TString tmpSubDir = tmpDir + GetDirectorySeparatorS() + "subdir"; - static const TString tmpFile = tmpDir + GetDirectorySeparatorS() + "file"; + static const std::string tmpDir = "tmp_dir_for_tests"; + static const std::string tmpSubDir = tmpDir + GetDirectorySeparatorS() + "subdir"; + static const std::string tmpFile = tmpDir + GetDirectorySeparatorS() + "file"; // creating tmp dir and subdirs MakeDirIfNotExist(tmpDir.data()); diff --git a/util/folder/filelist.cpp b/util/folder/filelist.cpp index b21fcdbf208..c2713137298 100644 --- a/util/folder/filelist.cpp +++ b/util/folder/filelist.cpp @@ -4,7 +4,7 @@ #include -void TFileEntitiesList::Fill(const TString& dirname, TStringBuf prefix, TStringBuf suffix, int depth, bool sort) { +void TFileEntitiesList::Fill(const std::string& dirname, std::string_view prefix, std::string_view suffix, int depth, bool sort) { TDirIterator::TOptions opts; opts.SetMaxLevel(depth); if (sort) { @@ -24,9 +24,9 @@ void TFileEntitiesList::Fill(const TString& dirname, TStringBuf prefix, TStringB continue; } - TStringBuf filename = file->fts_path + dirNameLength + 1; + std::string_view filename = file->fts_path + dirNameLength + 1; - if (filename.empty() || !filename.StartsWith(prefix) || !filename.EndsWith(suffix)) { + if (filename.empty() || !filename.starts_with(prefix) || !filename.ends_with(suffix)) { continue; } diff --git a/util/folder/filelist.h b/util/folder/filelist.h index 8851323babd..cf3210b9b6c 100644 --- a/util/folder/filelist.h +++ b/util/folder/filelist.h @@ -1,10 +1,10 @@ #pragma once #include -#include -#include #include +#include + class TFileEntitiesList { public: enum EMaskFlag { @@ -40,15 +40,15 @@ class TFileEntitiesList { return FileNamesSize; } - inline void Fill(const TString& dirname, bool sort = false) { - Fill(dirname, TStringBuf(), sort); + inline void Fill(const std::string& dirname, bool sort = false) { + Fill(dirname, std::string_view(), sort); } - inline void Fill(const TString& dirname, TStringBuf prefix, bool sort = false) { - Fill(dirname, prefix, TStringBuf(), 1, sort); + inline void Fill(const std::string& dirname, std::string_view prefix, bool sort = false) { + Fill(dirname, prefix, std::string_view(), 1, sort); } - void Fill(const TString& dirname, TStringBuf prefix, TStringBuf suffix, int depth, bool sort = false); + void Fill(const std::string& dirname, std::string_view prefix, std::string_view suffix, int depth, bool sort = false); void Restart() { Cur = FileNames.Data(); diff --git a/util/folder/filelist_ut.cpp b/util/folder/filelist_ut.cpp index 0cdcdf3d003..8fa3e9c8f3c 100644 --- a/util/folder/filelist_ut.cpp +++ b/util/folder/filelist_ut.cpp @@ -5,7 +5,6 @@ #include #include -#include class TFileListTest: public TTestBase { UNIT_TEST_SUITE(TFileListTest); @@ -25,7 +24,7 @@ void TFileListTest::TestSimple() { TFileList fileList; fileList.Fill(tempDir().data(), "", "", 1000); - TString fileName(fileList.Next()); + std::string fileName(fileList.Next()); UNIT_ASSERT_EQUAL(fileName, "subdir" LOCSLASH_S "file"); UNIT_ASSERT_EQUAL(fileList.Next(), nullptr); } @@ -41,14 +40,14 @@ void TFileListTest::TestPrefix() { TFileList fileList; { fileList.Fill(tempDir().data(), "good_file", SORT); - UNIT_ASSERT_EQUAL(TString(fileList.Next()), "good_file1"); - UNIT_ASSERT_EQUAL(TString(fileList.Next()), "good_file2"); + UNIT_ASSERT_EQUAL(std::string(fileList.Next()), "good_file1"); + UNIT_ASSERT_EQUAL(std::string(fileList.Next()), "good_file2"); UNIT_ASSERT_EQUAL(fileList.Next(), nullptr); } { fileList.Fill(tempDir().data(), "bad_file", SORT); - UNIT_ASSERT_EQUAL(TString(fileList.Next()), "bad_file1"); - UNIT_ASSERT_EQUAL(TString(fileList.Next()), "bad_file2"); + UNIT_ASSERT_EQUAL(std::string(fileList.Next()), "bad_file1"); + UNIT_ASSERT_EQUAL(std::string(fileList.Next()), "bad_file2"); UNIT_ASSERT_EQUAL(fileList.Next(), nullptr); } } diff --git a/util/folder/fts_ut.cpp b/util/folder/fts_ut.cpp index c5d59e35f42..b6514bfd86a 100644 --- a/util/folder/fts_ut.cpp +++ b/util/folder/fts_ut.cpp @@ -7,7 +7,6 @@ #include #include -#include class TFtsTest: public TTestBase { UNIT_TEST_SUITE(TFtsTest); @@ -93,7 +92,7 @@ class TTempDirWithLostAccess: public TTempDir { // Test that detects memory leak in case of error in chdir in fts_build function. void TFtsTest::TestNoLeakChangingAccessToFolder() { TTempDirWithLostAccess tempDir; - TString tmpPath = tempDir(); + std::string tmpPath = tempDir(); if (tmpPath.EndsWith(LOCSLASH_S)) { tmpPath.resize(tmpPath.size() - 1); } diff --git a/util/folder/iterator.h b/util/folder/iterator.h index 69e025b9c49..08ef4004435 100644 --- a/util/folder/iterator.h +++ b/util/folder/iterator.h @@ -6,6 +6,9 @@ #include #include #include +#include + +#include /// Note this magic API traverses directory hierarchy @@ -62,7 +65,7 @@ class TDirIterator: public TInputRangeAdaptor { } }; - inline TDirIterator(const TString& path, const TOptions& options = TOptions()) + inline TDirIterator(const std::string& path, const TOptions& options = TOptions()) : Options_(options) , Path_(path) { @@ -103,7 +106,7 @@ class TDirIterator: public TInputRangeAdaptor { private: TOptions Options_; - TString Path_; + std::string Path_; char* Trees_[2]; THolder FileTree_; }; diff --git a/util/folder/iterator_ut.cpp b/util/folder/iterator_ut.cpp index a6b09eeed7f..3d3d288f27a 100644 --- a/util/folder/iterator_ut.cpp +++ b/util/folder/iterator_ut.cpp @@ -9,7 +9,7 @@ #include #include -static TString JoinWithNewline(const std::vector& strings) { +static std::string JoinWithNewline(const std::vector& strings) { TStringStream ss; for (const auto& string : strings) { ss << string << "\n"; @@ -30,19 +30,19 @@ class TDirIteratorTest: public TTestBase { class TDirHier { public: struct TPath { - TString Path; + std::string Path; int Type; }; - inline void AddFile(const TString& path) { + inline void AddFile(const std::string& path) { Add(path, 0); } - inline void AddDir(const TString& path) { + inline void AddDir(const std::string& path) { Add(path, 1); } - inline void Add(const TString& path, int type) { + inline void Add(const std::string& path, int type) { const TPath p = { path, type}; @@ -67,15 +67,15 @@ class TDirIteratorTest: public TTestBase { Srch_[path.Path] = path; } - inline int Type(const TString& path) { - THashMap::const_iterator it = Srch_.find(path); + inline int Type(const std::string& path) { + THashMap::const_iterator it = Srch_.find(path); UNIT_ASSERT(it != Srch_.end()); return it->second.Type; } - inline bool Have(const TString& path, int type) { + inline bool Have(const std::string& path, int type) { return Type(path) == type; } @@ -87,11 +87,11 @@ class TDirIteratorTest: public TTestBase { private: std::vector Paths_; - THashMap Srch_; + THashMap Srch_; }; inline void TestLocal() { - TString dirname("." LOCSLASH_S); + std::string dirname("." LOCSLASH_S); TDirIterator d(dirname, FTS_NOCHDIR); for (auto it = d.begin(); it != d.end(); ++it) { } @@ -100,26 +100,26 @@ class TDirIteratorTest: public TTestBase { inline void TestIt() { TDirHier hier; - const TString dir = "tmpdir"; + const std::string dir = "tmpdir"; const TDirHier::TPath path = {dir, 1}; hier.Add(path); for (size_t i = 0; i < 10; ++i) { - const TString dir1 = dir + LOCSLASH_C + ToString(i); + const std::string dir1 = dir + LOCSLASH_C + ToString(i); const TDirHier::TPath path1 = {dir1, 1}; hier.Add(path1); for (size_t j = 0; j < 10; ++j) { - const TString subdir2 = ToString(j); - const TString dir2 = dir1 + LOCSLASH_C + subdir2; + const std::string subdir2 = ToString(j); + const std::string dir2 = dir1 + LOCSLASH_C + subdir2; const TDirHier::TPath path2 = {dir2, 1}; hier.Add(path2); for (size_t k = 0; k < 3; ++k) { - const TString file = dir2 + LOCSLASH_C + "file" + ToString(k); + const std::string file = dir2 + LOCSLASH_C + "file" + ToString(k); const TDirHier::TPath fpath = {file, 0}; hier.Add(fpath); @@ -137,7 +137,7 @@ class TDirIteratorTest: public TTestBase { inline void TestSkip() { TDirHier hier; - const TString dir = "tmpdir"; + const std::string dir = "tmpdir"; const TDirHier::TPath path = {dir, 1}; hier.Add(path); @@ -150,10 +150,10 @@ class TDirIteratorTest: public TTestBase { TDirIterator di(dir); UNIT_ASSERT(di.Next()); - UNIT_ASSERT_EQUAL(TStringBuf(di.Next()->fts_name), "dir1"); - UNIT_ASSERT_EQUAL(TStringBuf(di.Next()->fts_name), "dir2"); - UNIT_ASSERT_EQUAL(TStringBuf(di.Next()->fts_name), "dir2"); - UNIT_ASSERT_EQUAL(TStringBuf(di.Next()->fts_name), "dir1"); + UNIT_ASSERT_EQUAL(std::string_view(di.Next()->fts_name), "dir1"); + UNIT_ASSERT_EQUAL(std::string_view(di.Next()->fts_name), "dir2"); + UNIT_ASSERT_EQUAL(std::string_view(di.Next()->fts_name), "dir2"); + UNIT_ASSERT_EQUAL(std::string_view(di.Next()->fts_name), "dir1"); UNIT_ASSERT(di.Next()); UNIT_ASSERT_EQUAL(di.Next(), nullptr); } @@ -165,9 +165,9 @@ class TDirIteratorTest: public TTestBase { UNIT_ASSERT(di.Next()); auto ent = di.Next(); - UNIT_ASSERT_EQUAL(TStringBuf(ent->fts_name), "dir1"); + UNIT_ASSERT_EQUAL(std::string_view(ent->fts_name), "dir1"); di.Skip(ent); - UNIT_ASSERT_EQUAL(TStringBuf(di.Next()->fts_name), "dir1"); + UNIT_ASSERT_EQUAL(std::string_view(di.Next()->fts_name), "dir1"); UNIT_ASSERT(di.Next()); UNIT_ASSERT_EQUAL(di.Next(), nullptr); } @@ -175,24 +175,24 @@ class TDirIteratorTest: public TTestBase { inline void TestSort() { TDirHier dh; - const TString dir("tmpdir"); + const std::string dir("tmpdir"); //prepare fs { TMersenne rnd; - const TString prefixes[] = { + const std::string prefixes[] = { "a", "b", "xxx", "111", ""}; dh.AddDir(dir); for (size_t i = 0; i < 100; ++i) { - const TString fname = dir + LOCSLASH_C + prefixes[i % Y_ARRAY_SIZE(prefixes)] + ToString(rnd.GenRand()); + const std::string fname = dir + LOCSLASH_C + prefixes[i % Y_ARRAY_SIZE(prefixes)] + ToString(rnd.GenRand()); dh.AddFile(fname); } } - std::vector fnames; + std::vector fnames; { TDirIterator d(dir, TDirIterator::TOptions().SetSortByName()); @@ -204,7 +204,7 @@ class TDirIteratorTest: public TTestBase { } } - std::vector sorted(fnames); + std::vector sorted(fnames); Sort(sorted.begin(), sorted.end()); UNIT_ASSERT_VALUES_EQUAL(JoinWithNewline(fnames), JoinWithNewline(sorted)); diff --git a/util/folder/path.cpp b/util/folder/path.cpp index a03a0ae8c35..1aa292b545d 100644 --- a/util/folder/path.cpp +++ b/util/folder/path.cpp @@ -10,17 +10,17 @@ #include struct TFsPath::TSplit: public TAtomicRefCount, public TPathSplit { - inline TSplit(const TStringBuf path) + inline TSplit(const std::string_view path) : TPathSplit(path) { } - inline TSplit(const TString& path, const TSimpleIntrusivePtr& thatSplit, const TString::char_type* thatPathData) { + inline TSplit(const std::string& path, const TSimpleIntrusivePtr& thatSplit, const std::string::value_type* thatPathData) { for (const auto& thatPart : *thatSplit) { emplace_back(path.data() + (thatPart.data() - thatPathData), thatPart.size()); } if (!thatSplit->Drive.empty()) { - Drive = TStringBuf(path.data() + (thatSplit->Drive.data() - thatPathData), thatSplit->Drive.size()); + Drive = std::string_view(path.data() + (thatSplit->Drive.data() - thatPathData), thatSplit->Drive.size()); } IsAbsolute = thatSplit->IsAbsolute; @@ -29,7 +29,7 @@ struct TFsPath::TSplit: public TAtomicRefCount, public TPathSplit { void TFsPath::CheckDefined() const { if (!IsDefined()) { - ythrow TIoException() << TStringBuf("must be defined"); + ythrow TIoException() << "must be defined"; } } @@ -101,7 +101,7 @@ TFsPath TFsPath::RelativePath(const TFsPath& root) const { if (cnt == 0 && !absboth) { ythrow TIoException() << "No common parts in " << *this << " and " << root; } - TString r; + std::string r; for (size_t i = 0; i < rsplit.size() - cnt; i++) { r += i == 0 ? ".." : "/.."; } @@ -151,16 +151,16 @@ TFsPath& TFsPath::Fix() { return *this; } -TString TFsPath::GetName() const { +std::string TFsPath::GetName() const { if (!IsDefined()) { - return TString(); + return std::string(); } const TSplit& split = GetSplit(); if (split.size() > 0) { if (split.back() != "..") { - return TString(split.back()); + return std::string(split.back()); } else { // cannot just drop last component, because path itself may be a symlink return RealPath().GetName(); @@ -174,8 +174,8 @@ TString TFsPath::GetName() const { } } -TString TFsPath::GetExtension() const { - return TString(GetSplit().Extension()); +std::string TFsPath::GetExtension() const { + return std::string(GetSplit().Extension()); } bool TFsPath::IsAbsolute() const { @@ -198,20 +198,20 @@ TFsPath::TSplit& TFsPath::GetSplit() const { return *Split_; } -static Y_FORCE_INLINE void VerifyPath(const TStringBuf path) { - Y_ABORT_UNLESS(!path.Contains('\0'), "wrong format of TFsPath: %s", EscapeC(path).c_str()); +static Y_FORCE_INLINE void VerifyPath(const std::string_view path) { + Y_ABORT_UNLESS(!path.contains('\0'), "wrong format of TFsPath: %s", EscapeC(path).c_str()); } TFsPath::TFsPath() { } -TFsPath::TFsPath(const TString& path) +TFsPath::TFsPath(const std::string& path) : Path_(path) { VerifyPath(Path_); } -TFsPath::TFsPath(const TStringBuf path) +TFsPath::TFsPath(const std::string_view path) : Path_(ToString(path)) { VerifyPath(Path_); @@ -260,8 +260,8 @@ TFsPath& TFsPath::operator=(TFsPath&& that) { return *this; } -TFsPath TFsPath::Child(const TString& name) const { - if (!name) { +TFsPath TFsPath::Child(const std::string& name) const { + if (name.empty()) { ythrow TIoException() << "child name must not be empty"; } @@ -278,7 +278,7 @@ struct TClosedir { } }; -void TFsPath::ListNames(std::vector& children) const { +void TFsPath::ListNames(std::vector& children) const { CheckDefined(); THolder dir(opendir(this->c_str())); if (!dir) { @@ -300,7 +300,7 @@ void TFsPath::ListNames(std::vector& children) const { if (ok == nullptr) { return; } - TString name(de.d_name); + std::string name(de.d_name); if (name == "." || name == "..") { continue; } @@ -308,7 +308,7 @@ void TFsPath::ListNames(std::vector& children) const { } } -bool TFsPath::Contains(const TString& component) const { +bool TFsPath::Contains(const std::string& component) const { if (!IsDefined()) { return false; } @@ -326,16 +326,16 @@ bool TFsPath::Contains(const TString& component) const { } void TFsPath::List(std::vector& files) const { - std::vector names; + std::vector names; ListNames(names); for (auto& name : names) { files.push_back(Child(name)); } } -void TFsPath::RenameTo(const TString& newPath) const { +void TFsPath::RenameTo(const std::string& newPath) const { CheckDefined(); - if (!newPath) { + if (newPath.empty()) { ythrow TIoException() << "bad new file name"; } if (!NFs::Rename(Path_, newPath)) { @@ -344,7 +344,7 @@ void TFsPath::RenameTo(const TString& newPath) const { } void TFsPath::RenameTo(const char* newPath) const { - RenameTo(TString(newPath)); + RenameTo(std::string(newPath)); } void TFsPath::RenameTo(const TFsPath& newPath) const { @@ -472,7 +472,7 @@ void TFsPath::ForceDelete() const { } } -void TFsPath::CopyTo(const TString& newPath, bool force) const { +void TFsPath::CopyTo(const std::string& newPath, bool force) const { if (IsDirectory()) { if (force) { TFsPath(newPath).MkDirs(); @@ -499,7 +499,7 @@ void TFsPath::CopyTo(const TString& newPath, bool force) const { } } -void TFsPath::ForceRenameTo(const TString& newPath) const { +void TFsPath::ForceRenameTo(const std::string& newPath) const { try { RenameTo(newPath); } catch (const TIoSystemError& /* error */) { @@ -523,13 +523,13 @@ void Out(IOutputStream& os, const TFsPath& f) { template <> TFsPath FromStringImpl(const char* s, size_t len) { - return TFsPath{TStringBuf{s, len}}; + return TFsPath{std::string_view{s, len}}; } template <> bool TryFromStringImpl(const char* s, size_t len, TFsPath& result) { try { - result = TStringBuf{s, len}; + result = std::string_view{s, len}; return true; } catch (std::exception&) { return false; diff --git a/util/folder/path.h b/util/folder/path.h index 980044b31d1..ec2e40f3c62 100644 --- a/util/folder/path.h +++ b/util/folder/path.h @@ -4,9 +4,6 @@ #include "pathsplit.h" #include -#include -#include -#include #include #include #include @@ -25,15 +22,10 @@ class TFsPath { public: TFsPath(); - TFsPath(const TString& path); - TFsPath(const TStringBuf path); + TFsPath(const std::string& path); + TFsPath(const std::string_view path); TFsPath(const char* path); - TFsPath(const std::string& path) - : TFsPath(TStringBuf(path)) - { - } - TFsPath(const TFsPath& that); TFsPath(TFsPath&& that); @@ -56,7 +48,7 @@ class TFsPath { return Path_.c_str(); } - inline operator const TString&() const { + inline operator const std::string&() const { return Path_; } @@ -75,19 +67,19 @@ class TFsPath { TFsPath& Fix(); - inline const TString& GetPath() const { + inline const std::string& GetPath() const { return Path_; } /// last component of path, or "/" if root - TString GetName() const; + std::string GetName() const; /** * "a.b.tmp" -> "tmp" * "a.tmp" -> "tmp" * ".tmp" -> "" */ - TString GetExtension() const; + std::string GetExtension() const; bool IsAbsolute() const; bool IsRelative() const; @@ -130,14 +122,14 @@ class TFsPath { */ TFsPath Parent() const; - TString Basename() const { + std::string Basename() const { return GetName(); } - TString Dirname() const { + std::string Dirname() const { return Parent(); } - TFsPath Child(const TString& name) const; + TFsPath Child(const std::string& name) const; /** * @brief create this directory @@ -157,10 +149,10 @@ class TFsPath { // XXX: rewrite to return iterator void List(std::vector& children) const; - void ListNames(std::vector& children) const; + void ListNames(std::vector& children) const; // Check, if path contains at least one component with a specific name. - bool Contains(const TString& component) const; + bool Contains(const std::string& component) const; // fails to delete non-empty directory void DeleteIfExists() const; @@ -185,12 +177,12 @@ class TFsPath { /// throw TIoException if not exists void CheckExists() const; - void RenameTo(const TString& newPath) const; + void RenameTo(const std::string& newPath) const; void RenameTo(const char* newPath) const; void RenameTo(const TFsPath& newFile) const; - void ForceRenameTo(const TString& newPath) const; + void ForceRenameTo(const std::string& newPath) const; - void CopyTo(const TString& newPath, bool force) const; + void CopyTo(const std::string& newPath, bool force) const; void Touch() const; @@ -211,7 +203,7 @@ class TFsPath { TSplit& GetSplit() const; private: - TString Path_; + std::string Path_; /// cache mutable TSimpleIntrusivePtr Split_; }; @@ -228,7 +220,7 @@ namespace NPrivate { } template -TString JoinFsPaths(Ts&&... args) { +std::string JoinFsPaths(Ts&&... args) { TFsPath fsPath; ::NPrivate::AppendToFsPath(fsPath, std::forward(args)...); return fsPath.GetPath(); diff --git a/util/folder/path.pxd b/util/folder/path.pxd index 85af10d746b..e7dfba6a8f4 100644 --- a/util/folder/path.pxd +++ b/util/folder/path.pxd @@ -1,4 +1,4 @@ -from util.generic.string cimport TString, TStringBuf +from util.generic.string cimport std::string, std::string_view from util.generic.vector cimport TVector @@ -6,8 +6,8 @@ from util.generic.vector cimport TVector cdef extern from "util/folder/path.h" nogil: cdef cppclass TFsPath: TFsPath() except + - TFsPath(const TString&) except + - TFsPath(const TStringBuf) except + + TFsPath(const std::string&) except + + TFsPath(const std::string_view) except + TFsPath(const char*) except + void CheckDefined() except + @@ -30,10 +30,10 @@ cdef extern from "util/folder/path.h" nogil: TFsPath& Fix() except + - const TString& GetPath() const - TString GetName() const + const std::string& GetPath() const + std::string GetName() const - TString GetExtension() const + std::string GetExtension() const bint IsAbsolute() const bint IsRelative() const @@ -47,10 +47,10 @@ cdef extern from "util/folder/path.h" nogil: TFsPath Parent() const - TString Basename() const - TString Dirname() const + std::string Basename() const + std::string Dirname() const - TFsPath Child(const TString&) except + + TFsPath Child(const std::string&) except + void MkDir() except + void MkDir(const int) except + @@ -58,9 +58,9 @@ cdef extern from "util/folder/path.h" nogil: void MkDirs(const int) except + void List(TVector[TFsPath]&) except + - void ListNames(TVector[TString]&) except + + void ListNames(TVector[std::string]&) except + - bint Contains(const TString&) const + bint Contains(const std::string&) const void DeleteIfExists() except + void ForceDelete() except + @@ -74,12 +74,12 @@ cdef extern from "util/folder/path.h" nogil: bint IsSymlink() const void CheckExists() except + - void RenameTo(const TString&) except + + void RenameTo(const std::string&) except + void RenameTo(const char*) except + void RenameTo(const TFsPath&) except + - void ForceRenameTo(const TString&) except + + void ForceRenameTo(const std::string&) except + - void CopyTo(const TString&, bint) except + + void CopyTo(const std::string&, bint) except + void Touch() except + diff --git a/util/folder/path_ut.cpp b/util/folder/path_ut.cpp index fee5344d220..00fa627cea7 100644 --- a/util/folder/path_ut.cpp +++ b/util/folder/path_ut.cpp @@ -26,23 +26,23 @@ namespace { TFsPath Path_; public: - TTestDirectory(const TString& name); + TTestDirectory(const std::string& name); ~TTestDirectory(); TFsPath GetFsPath() const { return Path_; } - TFsPath Child(const TString& name) const { + TFsPath Child(const std::string& name) const { return Path_.Child(name); } }; - TTestDirectory::TTestDirectory(const TString& name) { + TTestDirectory::TTestDirectory(const std::string& name) { Y_ABORT_UNLESS(name.length() > 0, "have to specify name"); - Y_ABORT_UNLESS(name.find('.') == TString::npos, "must be simple name"); - Y_ABORT_UNLESS(name.find('/') == TString::npos, "must be simple name"); - Y_ABORT_UNLESS(name.find('\\') == TString::npos, "must be simple name"); + Y_ABORT_UNLESS(name.find('.') == std::string::npos, "must be simple name"); + Y_ABORT_UNLESS(name.find('/') == std::string::npos, "must be simple name"); + Y_ABORT_UNLESS(name.find('\\') == std::string::npos, "must be simple name"); Path_ = TFsPath(name); Path_.ForceDelete(); @@ -119,13 +119,13 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { Y_UNIT_TEST(GetName) { TTestDirectory d("GetName"); - UNIT_ASSERT_VALUES_EQUAL(TString("dfgh"), d.Child("dfgh").GetName()); + UNIT_ASSERT_VALUES_EQUAL(std::string("dfgh"), d.Child("dfgh").GetName()); // check does not fail TFsPath(".").GetName(); #ifdef _unix_ - UNIT_ASSERT_VALUES_EQUAL(TString("/"), TFsPath("/").GetName()); + UNIT_ASSERT_VALUES_EQUAL(std::string("/"), TFsPath("/").GetName()); #endif } @@ -192,7 +192,7 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { Y_UNIT_TEST(TestSlashesAndBasename) { TFsPath p("/db/BASE/primus121-025-1380131338//"); - UNIT_ASSERT_VALUES_EQUAL(p.Basename(), TString("primus121-025-1380131338")); + UNIT_ASSERT_VALUES_EQUAL(p.Basename(), std::string("primus121-025-1380131338")); TFsPath testP = p / "test"; #ifdef _win_ UNIT_ASSERT_VALUES_EQUAL(testP.GetPath(), "\\db\\BASE\\primus121-025-1380131338\\test"); @@ -205,10 +205,10 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { TFsPath p("\\db\\BASE\\primus121-025-1380131338\\\\"); TFsPath testP = p / "test"; #ifdef _win_ - UNIT_ASSERT_VALUES_EQUAL(p.Basename(), TString("primus121-025-1380131338")); + UNIT_ASSERT_VALUES_EQUAL(p.Basename(), std::string("primus121-025-1380131338")); UNIT_ASSERT_VALUES_EQUAL(testP.GetPath(), "\\db\\BASE\\primus121-025-1380131338\\test"); #else - UNIT_ASSERT_VALUES_EQUAL(p.Basename(), TString("\\db\\BASE\\primus121-025-1380131338\\\\")); + UNIT_ASSERT_VALUES_EQUAL(p.Basename(), std::string("\\db\\BASE\\primus121-025-1380131338\\\\")); UNIT_ASSERT_VALUES_EQUAL(testP.GetPath(), "\\db\\BASE\\primus121-025-1380131338\\\\/test"); #endif } @@ -217,10 +217,10 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { TFsPath p("C:\\db\\BASE\\primus121-025-1380131338\\\\"); TFsPath testP = p / "test"; #ifdef _win_ - UNIT_ASSERT_VALUES_EQUAL(p.Basename(), TString("primus121-025-1380131338")); + UNIT_ASSERT_VALUES_EQUAL(p.Basename(), std::string("primus121-025-1380131338")); UNIT_ASSERT_VALUES_EQUAL(testP.GetPath(), "C:\\db\\BASE\\primus121-025-1380131338\\test"); #else - UNIT_ASSERT_VALUES_EQUAL(p.Basename(), TString("C:\\db\\BASE\\primus121-025-1380131338\\\\")); + UNIT_ASSERT_VALUES_EQUAL(p.Basename(), std::string("C:\\db\\BASE\\primus121-025-1380131338\\\\")); UNIT_ASSERT_VALUES_EQUAL(testP.GetPath(), "C:\\db\\BASE\\primus121-025-1380131338\\\\/test"); #endif } @@ -235,11 +235,11 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { dir.Child("c").MkDir(); dir.Child("d").Touch(); - std::vector children; + std::vector children; dir.ListNames(children); std::sort(children.begin(), children.end()); - std::vector expected; + std::vector expected; expected.push_back("a"); expected.push_back("b"); expected.push_back("c"); @@ -351,12 +351,12 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { UNIT_ASSERT_VALUES_EQUAL(TFsPath() / TFsPath(), TFsPath()); #ifdef _win_ - UNIT_ASSERT_VALUES_EQUAL(TFsPath("a\\b"), TFsPath() / TString("a\\b")); + UNIT_ASSERT_VALUES_EQUAL(TFsPath("a\\b"), TFsPath() / std::string("a\\b")); UNIT_ASSERT_VALUES_EQUAL(TFsPath("a\\b"), "a\\b" / TFsPath()); UNIT_ASSERT_VALUES_EQUAL(TFsPath("\\a\\b"), TFsPath() / "\\a\\b"); UNIT_ASSERT_VALUES_EQUAL(TFsPath("\\a\\b"), "\\a\\b" / TFsPath()); #else - UNIT_ASSERT_VALUES_EQUAL(TFsPath("a/b"), TFsPath() / TString("a/b")); + UNIT_ASSERT_VALUES_EQUAL(TFsPath("a/b"), TFsPath() / std::string("a/b")); UNIT_ASSERT_VALUES_EQUAL(TFsPath("a/b"), "a/b" / TFsPath()); UNIT_ASSERT_VALUES_EQUAL(TFsPath("/a/b"), TFsPath() / "/a/b"); UNIT_ASSERT_VALUES_EQUAL(TFsPath("/a/b"), "/a/b" / TFsPath()); @@ -430,16 +430,16 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { TTempDir tempDir; TFsPath tempDirPath(tempDir()); - const TString originDir = tempDirPath.Child("origin"); + const std::string originDir = tempDirPath.Child("origin"); MakePathIfNotExist(originDir.c_str()); - const TString originFile = TFsPath(originDir).Child("data"); + const std::string originFile = TFsPath(originDir).Child("data"); { TFixedBufferFileOutput out(originFile); out << "data111!!!"; } - const TString link = tempDirPath.Child("origin_symlink"); + const std::string link = tempDirPath.Child("origin_symlink"); NFs::SymLink(originDir, link); TFsPath(link).ForceDelete(); @@ -453,16 +453,16 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { TTempDir tempDir; TFsPath tempDirPath(tempDir()); - const TString originDir = tempDirPath.Child("origin"); + const std::string originDir = tempDirPath.Child("origin"); MakePathIfNotExist(originDir.c_str()); - const TString originFile = TFsPath(originDir).Child("data"); + const std::string originFile = TFsPath(originDir).Child("data"); { TFixedBufferFileOutput out(originFile); out << "data111!!!"; } - const TString link = tempDirPath.Child("origin_symlink"); + const std::string link = tempDirPath.Child("origin_symlink"); NFs::SymLink(originFile, link); TFsPath(link).ForceDelete(); @@ -476,19 +476,19 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { TTempDir tempDir; TFsPath tempDirPath(tempDir()); - const TString symlinkedDir = tempDirPath.Child("to_remove"); + const std::string symlinkedDir = tempDirPath.Child("to_remove"); MakePathIfNotExist(symlinkedDir.c_str()); - const TString originDir = tempDirPath.Child("origin"); + const std::string originDir = tempDirPath.Child("origin"); MakePathIfNotExist(originDir.c_str()); - const TString originFile = TFsPath(originDir).Child("data"); + const std::string originFile = TFsPath(originDir).Child("data"); { TFixedBufferFileOutput out(originFile); out << "data111!!!"; } - const TString symlinkedFile = TFsPath(symlinkedDir).Child("origin_symlink"); + const std::string symlinkedFile = TFsPath(symlinkedDir).Child("origin_symlink"); NFs::SymLink(originDir, symlinkedFile); TFsPath(symlinkedDir).ForceDelete(); @@ -503,19 +503,19 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { TTempDir tempDir; TFsPath tempDirPath(tempDir()); - const TString symlinkedDir = tempDirPath.Child("to_remove"); + const std::string symlinkedDir = tempDirPath.Child("to_remove"); MakePathIfNotExist(symlinkedDir.c_str()); - const TString originDir = tempDirPath.Child("origin"); + const std::string originDir = tempDirPath.Child("origin"); MakePathIfNotExist(originDir.c_str()); - const TString originFile = TFsPath(originDir).Child("data"); + const std::string originFile = TFsPath(originDir).Child("data"); { TFixedBufferFileOutput out(originFile); out << "data111!!!"; } - const TString symlinkedFile = TFsPath(symlinkedDir).Child("origin_symlink"); + const std::string symlinkedFile = TFsPath(symlinkedDir).Child("origin_symlink"); NFs::SymLink(originFile, symlinkedFile); TFsPath(symlinkedDir).ForceDelete(); @@ -834,9 +834,9 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { #ifdef TSTRING_IS_STD_STRING Y_UNIT_TEST(TestCopySplitSSO) { // Summary length of path must be less minimal SSO length 19 bytes - constexpr TStringBuf A("a"); - constexpr TStringBuf B("b"); - constexpr TStringBuf C("c"); + constexpr std::string_view A("a"); + constexpr std::string_view B("b"); + constexpr std::string_view C("c"); for (auto constructorType = 0; constructorType < 2; ++constructorType) { TFsPath path1 = TFsPath(A) / TFsPath(B); const auto& split1 = path1.PathSplit(); @@ -853,7 +853,7 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { const auto& split2 = path2.PathSplit(); path1 = TFsPath(C); // invalidate previous Path_ in path1 const auto& newsplit1 = path1.PathSplit(); - // Check that split of path1 was overwrited (invalidate previous TStringBuf) + // Check that split of path1 was overwrited (invalidate previous std::string_view) UNIT_ASSERT_VALUES_EQUAL(newsplit1.size(), 1); UNIT_ASSERT_VALUES_EQUAL(newsplit1.at(0), C); // Check split of path2 without segfault @@ -866,9 +866,9 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { Y_UNIT_TEST(TestCopySplitNoneSSO) { // Lenght of directory name must overhead SSO length 19-23 bytes - const TString DIR_A = TString("Dir") + TString(32, 'A'); - const TString DIR_B = TString("Dir") + TString(64, 'B'); - const TString DIR_C = TString("Dir") + TString(128, 'C'); + const std::string DIR_A = std::string("Dir") + std::string(32, 'A'); + const std::string DIR_B = std::string("Dir") + std::string(64, 'B'); + const std::string DIR_C = std::string("Dir") + std::string(128, 'C'); for (auto constructorType = 0; constructorType < 2; ++constructorType) { TFsPath path1 = TFsPath(DIR_A) / TFsPath(DIR_B); auto& split1 = path1.PathSplit(); @@ -885,7 +885,7 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { const auto& split2 = path2.PathSplit(); path1 = TFsPath(DIR_C); // invalidate previous Path_ in path1 const auto& newsplit1 = path1.PathSplit(); - // Check that split of path1 was overwrited (invalidate previous TStringBuf) + // Check that split of path1 was overwrited (invalidate previous std::string_view) UNIT_ASSERT_VALUES_EQUAL(newsplit1.size(), 1); UNIT_ASSERT_VALUES_EQUAL(newsplit1.at(0), DIR_C); // Check split of path2 without segfault diff --git a/util/folder/path_ut.pyx b/util/folder/path_ut.pyx index be8d3ce35af..408361de19f 100644 --- a/util/folder/path_ut.pyx +++ b/util/folder/path_ut.pyx @@ -1,7 +1,7 @@ # cython: c_string_type=str, c_string_encoding=utf8 from util.folder.path cimport TFsPath -from util.generic.string cimport TString, TStringBuf +from util.generic.string cimport std::string, std::string_view from util.generic.vector cimport TVector import unittest @@ -17,13 +17,13 @@ class TestPath(unittest.TestCase): self.assertEqual(path.c_str(), "") def test_ctor2(self): - cdef TString str_path = "/a/b/c" + cdef std::string str_path = "/a/b/c" cdef TFsPath path = TFsPath(str_path) self.assertEqual(path.IsDefined(), True) self.assertEqual(path.c_str(), "/a/b/c") def test_ctor3(self): - cdef TStringBuf buf_path = "/a/b/c" + cdef std::string_view buf_path = "/a/b/c" cdef TFsPath path = TFsPath(buf_path) self.assertEqual(path.IsDefined(), True) self.assertEqual(path.c_str(), "/a/b/c") @@ -231,7 +231,7 @@ class TestPath(unittest.TestCase): TFsPath("test_list/c").Touch() cdef TVector[TFsPath] files - cdef TVector[TString] names + cdef TVector[std::string] names dir.List(files) dir.ListNames(names) @@ -313,7 +313,7 @@ class TestPath(unittest.TestCase): cdef TFsPath path = TFsPath("test_rename_a") path.Touch() - cdef TString path_str = "test_rename_b" + cdef std::string path_str = "test_rename_b" cdef TFsPath path_from_str = TFsPath(path_str) self.assertEqual(path.Exists(), True) self.assertEqual(path_from_str.Exists(), False) @@ -343,7 +343,7 @@ class TestPath(unittest.TestCase): path_from_str.RenameTo("") def test_copy(self): - cdef TString dst = "test_copy_dst" + cdef std::string dst = "test_copy_dst" cdef TFsPath src_path = TFsPath("test_copy_src") cdef TFsPath dst_path = TFsPath(dst) self.assertEqual(src_path.Exists(), False) diff --git a/util/folder/pathsplit.cpp b/util/folder/pathsplit.cpp index 816f6960245..f6f06e48744 100644 --- a/util/folder/pathsplit.cpp +++ b/util/folder/pathsplit.cpp @@ -1,5 +1,7 @@ #include "pathsplit.h" +#include + #include #include @@ -14,9 +16,9 @@ static inline size_t ToReserve(const T& t) { return ret; } -void TPathSplitTraitsUnix::DoParseFirstPart(const TStringBuf part) { - if (part == TStringBuf(".")) { - push_back(TStringBuf(".")); +void TPathSplitTraitsUnix::DoParseFirstPart(const std::string_view part) { + if (part == ".") { + push_back("."); return; } @@ -28,24 +30,24 @@ void TPathSplitTraitsUnix::DoParseFirstPart(const TStringBuf part) { DoParsePart(part); } -void TPathSplitTraitsUnix::DoParsePart(const TStringBuf part0) { +void TPathSplitTraitsUnix::DoParsePart(const std::string_view part0) { DoAppendHint(part0.size() / 8); - TStringBuf next(part0); - TStringBuf part; + std::string_view next(part0); + std::string_view part; - while (TStringBuf(next).TrySplit('/', part, next)) { + while (NUtils::TrySplit(next, part, next, '/')) { AppendComponent(part); } AppendComponent(next); } -void TPathSplitTraitsWindows::DoParseFirstPart(const TStringBuf part0) { - TStringBuf part(part0); +void TPathSplitTraitsWindows::DoParseFirstPart(const std::string_view part0) { + std::string_view part(part0); - if (part == TStringBuf(".")) { - push_back(TStringBuf(".")); + if (part == ".") { + push_back("."); return; } @@ -54,19 +56,19 @@ void TPathSplitTraitsWindows::DoParseFirstPart(const TStringBuf part0) { IsAbsolute = true; if (part.size() > 1 && part[1] == ':') { - Drive = part.SubStr(0, 2); - part = part.SubStr(2); + Drive = part.substr(0, 2); + part = part.substr(2); } } DoParsePart(part); } -void TPathSplitTraitsWindows::DoParsePart(const TStringBuf part0) { +void TPathSplitTraitsWindows::DoParsePart(const std::string_view part0) { DoAppendHint(part0.size() / 8); size_t pos = 0; - TStringBuf part(part0); + std::string_view part(part0); while (pos < part.size()) { while (pos < part.size() && this->IsPathSep(part[pos])) { @@ -79,35 +81,35 @@ void TPathSplitTraitsWindows::DoParsePart(const TStringBuf part0) { ++pos; } - AppendComponent(TStringBuf(begin, part.data() + pos)); + AppendComponent(std::string_view(begin, part.data() + pos)); } } -TString TPathSplitStore::DoReconstruct(const TStringBuf slash) const { - TString r; +std::string TPathSplitStore::DoReconstruct(const std::string_view slash) const { + std::string r; r.reserve(ToReserve(*this)); if (IsAbsolute) { - r.AppendNoAlias(Drive); - r.AppendNoAlias(slash); + r.append(Drive); + r.append(slash); } for (auto i = begin(); i != end(); ++i) { if (i != begin()) { - r.AppendNoAlias(slash); + r.append(slash); } - r.AppendNoAlias(*i); + r.append(*i); } return r; } -void TPathSplitStore::AppendComponent(const TStringBuf comp) { - if (!comp || comp == TStringBuf(".")) { +void TPathSplitStore::AppendComponent(const std::string_view comp) { + if (comp.empty() || comp == ".") { // ignore - } else if (comp == TStringBuf("..") && !empty() && back() != TStringBuf("..")) { + } else if (comp == ".." && !empty() && back() != "..") { pop_back(); } else { // push back first .. also @@ -115,8 +117,8 @@ void TPathSplitStore::AppendComponent(const TStringBuf comp) { } } -TStringBuf TPathSplitStore::Extension() const { - return size() > 0 ? CutExtension(back()) : TStringBuf(); +std::string_view TPathSplitStore::Extension() const { + return size() > 0 ? CutExtension(back()) : std::string_view(); } template <> @@ -124,7 +126,7 @@ void Out(IOutputStream& o, const TPathSplit& ps) { o << ps.Reconstruct(); } -TString JoinPaths(const TPathSplit& p1, const TPathSplit& p2) { +std::string JoinPaths(const TPathSplit& p1, const TPathSplit& p2) { if (p2.IsAbsolute) { ythrow yexception() << "can not join " << p1 << " and " << p2; } @@ -132,14 +134,14 @@ TString JoinPaths(const TPathSplit& p1, const TPathSplit& p2) { return TPathSplit(p1).AppendMany(p2.begin(), p2.end()).Reconstruct(); } -TStringBuf CutExtension(const TStringBuf fileName) { +std::string_view CutExtension(const std::string_view fileName) { if (fileName.empty()) { return fileName; } - TStringBuf name; - TStringBuf extension; - fileName.RSplit('.', name, extension); + std::string_view name; + std::string_view extension; + NUtils::RSplit(fileName, name, extension, '.'); if (name.empty()) { // dot at a start or not found return name; diff --git a/util/folder/pathsplit.h b/util/folder/pathsplit.h index 68170be65c5..d24940dc0d4 100644 --- a/util/folder/pathsplit.h +++ b/util/folder/pathsplit.h @@ -1,20 +1,19 @@ #pragma once -#include -#include -#include #include +#include + //do not own any data -struct TPathSplitStore: public std::vector { - TStringBuf Drive; +struct TPathSplitStore: public std::vector { + std::string_view Drive; bool IsAbsolute = false; - void AppendComponent(const TStringBuf comp); - TStringBuf Extension() const; + void AppendComponent(const std::string_view comp); + std::string_view Extension() const; protected: - TString DoReconstruct(const TStringBuf slash) const; + std::string DoReconstruct(const std::string_view slash) const; inline void DoAppendHint(size_t hint) { reserve(size() + hint); @@ -24,39 +23,39 @@ struct TPathSplitStore: public std::vector { struct TPathSplitTraitsUnix: public TPathSplitStore { static constexpr char MainPathSep = '/'; - inline TString Reconstruct() const { - return DoReconstruct(TStringBuf("/")); + inline std::string Reconstruct() const { + return DoReconstruct(std::string_view("/")); } static constexpr bool IsPathSep(const char c) noexcept { return c == '/'; } - static inline bool IsAbsolutePath(const TStringBuf path) noexcept { - return path && IsPathSep(path[0]); + static inline bool IsAbsolutePath(const std::string_view path) noexcept { + return !path.empty() && IsPathSep(path[0]); } - void DoParseFirstPart(const TStringBuf part); - void DoParsePart(const TStringBuf part); + void DoParseFirstPart(const std::string_view part); + void DoParsePart(const std::string_view part); }; struct TPathSplitTraitsWindows: public TPathSplitStore { static constexpr char MainPathSep = '\\'; - inline TString Reconstruct() const { - return DoReconstruct(TStringBuf("\\")); + inline std::string Reconstruct() const { + return DoReconstruct(std::string_view("\\")); } static constexpr bool IsPathSep(char c) noexcept { return c == '/' || c == '\\'; } - static inline bool IsAbsolutePath(const TStringBuf path) noexcept { - return path && (IsPathSep(path[0]) || (path.size() > 1 && path[1] == ':' && IsAsciiAlpha(path[0]) && (path.size() == 2 || IsPathSep(path[2])))); + static inline bool IsAbsolutePath(const std::string_view path) noexcept { + return !path.empty() && (IsPathSep(path[0]) || (path.size() > 1 && path[1] == ':' && IsAsciiAlpha(path[0]) && (path.size() == 2 || IsPathSep(path[2])))); } - void DoParseFirstPart(const TStringBuf part); - void DoParsePart(const TStringBuf part); + void DoParseFirstPart(const std::string_view part); + void DoParsePart(const std::string_view part); }; #if defined(_unix_) @@ -70,7 +69,7 @@ class TPathSplitBase: public TTraits { public: inline TPathSplitBase() = default; - inline TPathSplitBase(const TStringBuf part) { + inline TPathSplitBase(const std::string_view part) { this->ParseFirstPart(part); } @@ -80,13 +79,13 @@ class TPathSplitBase: public TTraits { return *this; } - inline TPathSplitBase& ParseFirstPart(const TStringBuf part) { + inline TPathSplitBase& ParseFirstPart(const std::string_view part) { this->DoParseFirstPart(part); return *this; } - inline TPathSplitBase& ParsePart(const TStringBuf part) { + inline TPathSplitBase& ParsePart(const std::string_view part) { this->DoParsePart(part); return *this; @@ -108,6 +107,6 @@ using TPathSplit = TPathSplitBase; using TPathSplitUnix = TPathSplitBase; using TPathSplitWindows = TPathSplitBase; -TString JoinPaths(const TPathSplit& p1, const TPathSplit& p2); +std::string JoinPaths(const TPathSplit& p1, const TPathSplit& p2); -TStringBuf CutExtension(const TStringBuf fileName); +std::string_view CutExtension(const std::string_view fileName); diff --git a/util/folder/tempdir.cpp b/util/folder/tempdir.cpp index 6fdf8f753c1..399e70ebe5e 100644 --- a/util/folder/tempdir.cpp +++ b/util/folder/tempdir.cpp @@ -21,7 +21,7 @@ TTempDir::TTempDir(const char* prefix, TCreationToken) TempDir = tempDir; } -TTempDir::TTempDir(const TString& tempDir) +TTempDir::TTempDir(const std::string& tempDir) : TempDir(tempDir) , Remove(true) { @@ -29,7 +29,7 @@ TTempDir::TTempDir(const TString& tempDir) MakeDirIfNotExist(TempDir.c_str()); } -TTempDir TTempDir::NewTempDir(const TString& root) { +TTempDir TTempDir::NewTempDir(const std::string& root) { return {root.c_str(), TCreationToken{}}; } diff --git a/util/folder/tempdir.h b/util/folder/tempdir.h index ff458f83b9b..f868f3d1cf1 100644 --- a/util/folder/tempdir.h +++ b/util/folder/tempdir.h @@ -2,7 +2,6 @@ #include "fwd.h" #include "path.h" -#include class TTempDir { public: @@ -10,18 +9,18 @@ class TTempDir { TTempDir(); /// Create new directory with this fixed name. If it already exists, clear it. - TTempDir(const TString& tempDir); + TTempDir(const std::string& tempDir); ~TTempDir(); /// Create new directory in given folder. - static TTempDir NewTempDir(const TString& root); + static TTempDir NewTempDir(const std::string& root); - const TString& operator()() const { + const std::string& operator()() const { return Name(); } - const TString& Name() const { + const std::string& Name() const { return TempDir.GetPath(); } diff --git a/util/generic/algorithm_ut.cpp b/util/generic/algorithm_ut.cpp index 1ecfa1b39c3..eee086731f5 100644 --- a/util/generic/algorithm_ut.cpp +++ b/util/generic/algorithm_ut.cpp @@ -3,18 +3,17 @@ #include "algorithm.h" #include "hash.h" #include "hash_multi_map.h" -#include "strbuf.h" #include "string.h" static auto isOne = [](char c) { return c == '1'; }; Y_UNIT_TEST_SUITE(TAlgorithm) { Y_UNIT_TEST(AnyTest) { - UNIT_ASSERT(0 == AnyOf(TStringBuf("00"), isOne)); - UNIT_ASSERT(1 == AnyOf(TStringBuf("01"), isOne)); - UNIT_ASSERT(1 == AnyOf(TStringBuf("10"), isOne)); - UNIT_ASSERT(1 == AnyOf(TStringBuf("11"), isOne)); - UNIT_ASSERT(0 == AnyOf(TStringBuf(), isOne)); + UNIT_ASSERT(0 == AnyOf(std::string_view("00"), isOne)); + UNIT_ASSERT(1 == AnyOf(std::string_view("01"), isOne)); + UNIT_ASSERT(1 == AnyOf(std::string_view("10"), isOne)); + UNIT_ASSERT(1 == AnyOf(std::string_view("11"), isOne)); + UNIT_ASSERT(0 == AnyOf(std::string_view(), isOne)); const char array00[]{'0', '0'}; UNIT_ASSERT(0 == AnyOf(array00, isOne)); @@ -23,11 +22,11 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { } Y_UNIT_TEST(AllOfTest) { - UNIT_ASSERT(0 == AllOf(TStringBuf("00"), isOne)); - UNIT_ASSERT(0 == AllOf(TStringBuf("01"), isOne)); - UNIT_ASSERT(0 == AllOf(TStringBuf("10"), isOne)); - UNIT_ASSERT(1 == AllOf(TStringBuf("11"), isOne)); - UNIT_ASSERT(1 == AllOf(TStringBuf(), isOne)); + UNIT_ASSERT(0 == AllOf(std::string_view("00"), isOne)); + UNIT_ASSERT(0 == AllOf(std::string_view("01"), isOne)); + UNIT_ASSERT(0 == AllOf(std::string_view("10"), isOne)); + UNIT_ASSERT(1 == AllOf(std::string_view("11"), isOne)); + UNIT_ASSERT(1 == AllOf(std::string_view(), isOne)); const char array01[]{'0', '1'}; UNIT_ASSERT(0 == AllOf(array01, isOne)); @@ -36,11 +35,11 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { } Y_UNIT_TEST(CountIfTest) { - UNIT_ASSERT(3 == CountIf(TStringBuf("____1________1____1_______"), isOne)); - UNIT_ASSERT(5 == CountIf(TStringBuf("1____1________1____1_______1"), isOne)); - UNIT_ASSERT(0 == CountIf(TStringBuf("___________"), isOne)); - UNIT_ASSERT(0 == CountIf(TStringBuf(), isOne)); - UNIT_ASSERT(1 == CountIf(TStringBuf("1"), isOne)); + UNIT_ASSERT(3 == CountIf(std::string_view("____1________1____1_______"), isOne)); + UNIT_ASSERT(5 == CountIf(std::string_view("1____1________1____1_______1"), isOne)); + UNIT_ASSERT(0 == CountIf(std::string_view("___________"), isOne)); + UNIT_ASSERT(0 == CountIf(std::string_view(), isOne)); + UNIT_ASSERT(1 == CountIf(std::string_view("1"), isOne)); const char array[] = "____1________1____1_______"; UNIT_ASSERT(3 == CountIf(array, isOne)); @@ -48,20 +47,20 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { Y_UNIT_TEST(CountTest) { UNIT_ASSERT(3 == Count("____1________1____1_______", '1')); - UNIT_ASSERT(3 == Count(TStringBuf("____1________1____1_______"), '1')); - UNIT_ASSERT(5 == Count(TStringBuf("1____1________1____1_______1"), '1')); - UNIT_ASSERT(0 == Count(TStringBuf("___________"), '1')); - UNIT_ASSERT(0 == Count(TStringBuf(), '1')); - UNIT_ASSERT(1 == Count(TStringBuf("1"), '1')); + UNIT_ASSERT(3 == Count(std::string_view("____1________1____1_______"), '1')); + UNIT_ASSERT(5 == Count(std::string_view("1____1________1____1_______1"), '1')); + UNIT_ASSERT(0 == Count(std::string_view("___________"), '1')); + UNIT_ASSERT(0 == Count(std::string_view(), '1')); + UNIT_ASSERT(1 == Count(std::string_view("1"), '1')); const char array[] = "____1________1____1_______"; UNIT_ASSERT(3 == Count(array, '1')); } - struct TStrokaNoCopy: TString { + struct TStrokaNoCopy: std::string { public: TStrokaNoCopy(const char* p) - : TString(p) + : std::string(p) { } @@ -82,24 +81,24 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { UNIT_ASSERT_VALUES_EQUAL(CountOf(0x61, 'a', 'b', 'c', 0x61), 2); UNIT_ASSERT_VALUES_EQUAL(CountOf(0x61, 'a', 'b', 'c', 0x61ll), 2); - // TString and const char * - UNIT_ASSERT_VALUES_EQUAL(CountOf(TString("xyz"), "123", "poi"), 0); - UNIT_ASSERT_VALUES_EQUAL(CountOf(TString("xyz"), "123", "poi", "xyz"), 1); + // std::string and const char * + UNIT_ASSERT_VALUES_EQUAL(CountOf(std::string("xyz"), "123", "poi"), 0); + UNIT_ASSERT_VALUES_EQUAL(CountOf(std::string("xyz"), "123", "poi", "xyz"), 1); - // TString and TStringBuf - UNIT_ASSERT_VALUES_EQUAL(CountOf(TString("xyz"), TStringBuf("123"), TStringBuf("poi")), 0); - UNIT_ASSERT_VALUES_EQUAL(CountOf(TString("xyz"), TStringBuf("123"), TStringBuf("poi"), - TStringBuf("xyz")), + // std::string and std::string_view + UNIT_ASSERT_VALUES_EQUAL(CountOf(std::string("xyz"), std::string_view("123"), std::string_view("poi")), 0); + UNIT_ASSERT_VALUES_EQUAL(CountOf(std::string("xyz"), std::string_view("123"), std::string_view("poi"), + std::string_view("xyz")), 1); - // TStringBuf and const char * - UNIT_ASSERT_VALUES_EQUAL(CountOf(TStringBuf("xyz"), "123", "poi"), 0); - UNIT_ASSERT_VALUES_EQUAL(CountOf(TStringBuf("xyz"), "123", "poi", "xyz"), 1); + // std::string_view and const char * + UNIT_ASSERT_VALUES_EQUAL(CountOf(std::string_view("xyz"), "123", "poi"), 0); + UNIT_ASSERT_VALUES_EQUAL(CountOf(std::string_view("xyz"), "123", "poi", "xyz"), 1); - // TStringBuf and TString - UNIT_ASSERT_VALUES_EQUAL(CountOf(TStringBuf("xyz"), TString("123"), TString("poi")), 0); - UNIT_ASSERT_VALUES_EQUAL(CountOf(TStringBuf("xyz"), TString("123"), TString("poi"), - TString("xyz")), + // std::string_view and std::string + UNIT_ASSERT_VALUES_EQUAL(CountOf(std::string_view("xyz"), std::string("123"), std::string("poi")), 0); + UNIT_ASSERT_VALUES_EQUAL(CountOf(std::string_view("xyz"), std::string("123"), std::string("poi"), + std::string("xyz")), 1); } @@ -251,18 +250,18 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { Y_UNIT_TEST(SortUniqueTest) { { - std::vector v; + std::vector v; SortUnique(v); - UNIT_ASSERT_EQUAL(v, std::vector()); + UNIT_ASSERT_EQUAL(v, std::vector()); } { const char* ar[] = {"345", "3", "123", "2", "23", "3", "2"}; - std::vector v(ar, ar + Y_ARRAY_SIZE(ar)); + std::vector v(ar, ar + Y_ARRAY_SIZE(ar)); SortUnique(v); const char* suAr[] = {"123", "2", "23", "3", "345"}; - std::vector suV(suAr, suAr + Y_ARRAY_SIZE(suAr)); + std::vector suV(suAr, suAr + Y_ARRAY_SIZE(suAr)); UNIT_ASSERT_EQUAL(v, suV); } @@ -316,9 +315,9 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { Y_UNIT_TEST(NthElementTest) { { - std::vector v; + std::vector v; NthElement(v.begin(), v.begin(), v.end()); - UNIT_ASSERT_EQUAL(v, std::vector()); + UNIT_ASSERT_EQUAL(v, std::vector()); } { @@ -336,7 +335,7 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { { const char* data[] = {"3", "234", "1231", "333", "545345", "11", "111", "55", "66"}; - std::vector testVector(data, data + Y_ARRAY_SIZE(data)); + std::vector testVector(data, data + Y_ARRAY_SIZE(data)); size_t medianInd = testVector.size() / 2; NthElement(testVector.begin(), testVector.begin() + medianInd, testVector.end()); @@ -352,7 +351,7 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { Y_UNIT_TEST(BinarySearchTest) { { - std::vector v; + std::vector v; bool test = BinarySearch(v.begin(), v.end(), "test"); UNIT_ASSERT_EQUAL(test, false); } @@ -383,8 +382,8 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { Y_UNIT_TEST(EqualRangeTest) { { - std::vector v; - using PairOfVector = std::pair::iterator, std::vector::iterator>; + std::vector v; + using PairOfVector = std::pair::iterator, std::vector::iterator>; PairOfVector tmp = EqualRange(v.begin(), v.end(), "tmp"); UNIT_ASSERT_EQUAL(tmp.first, tmp.second); @@ -428,7 +427,7 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { const int v2[] = {8, 7, 6, 6, 5, 5, 5, 4, 3, 2, 1}; UNIT_ASSERT_EQUAL(AdjacentFind(v2), std::begin(v2) + 2); - std::vector v3 = {"six", "five", "four", "three", "two", "one"}; + std::vector v3 = {"six", "five", "four", "three", "two", "one"}; UNIT_ASSERT_EQUAL(AdjacentFind(v3), v3.end()); std::vector v4 = {1, 1, 1, 1, 1}; @@ -453,9 +452,9 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { UNIT_ASSERT_EQUAL(AdjacentFindBy(v2, std::negate()), std::begin(v2) + 2); UNIT_ASSERT_EQUAL(AdjacentFindBy(v2, [](const auto& e) { return e / 8; }), std::begin(v2) + 1); - std::vector v3 = {"six", "five", "four", "three", "two", "one"}; + std::vector v3 = {"six", "five", "four", "three", "two", "one"}; UNIT_ASSERT_EQUAL(AdjacentFind(v3), v3.end()); - UNIT_ASSERT_EQUAL(AdjacentFindBy(v3, std::mem_fn(&TStringBuf::size)), v3.begin() + 1); + UNIT_ASSERT_EQUAL(AdjacentFindBy(v3, std::mem_fn(&std::string_view::size)), v3.begin() + 1); std::vector v4 = {101, 201, 301, 401, 501}; for (;;) { @@ -663,9 +662,9 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { UNIT_ASSERT_EQUAL(p1.first, 5); UNIT_ASSERT_EQUAL(p1.second, 12); - std::pair p2 = MinMax(TString("test"), TString("data")); - UNIT_ASSERT_EQUAL(p2.first, TString("data")); - UNIT_ASSERT_EQUAL(p2.second, TString("test")); + std::pair p2 = MinMax(std::string("test"), std::string("data")); + UNIT_ASSERT_EQUAL(p2.first, std::string("data")); + UNIT_ASSERT_EQUAL(p2.second, std::string("test")); } Y_UNIT_TEST(TestMaxElementBy) { @@ -690,8 +689,8 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { auto singleElementSequence = {'z'}; UNIT_ASSERT_VALUES_EQUAL(*MaxElementBy(singleElementSequence, identity), 'z'); - const TString strings[] = {"one", "two", "three", "four"}; - UNIT_ASSERT_STRINGS_EQUAL(*MaxElementBy(strings, [](TString s) { return s.size(); }), "three"); + const std::string strings[] = {"one", "two", "three", "four"}; + UNIT_ASSERT_STRINGS_EQUAL(*MaxElementBy(strings, [](std::string s) { return s.size(); }), "three"); } Y_UNIT_TEST(TestMinElementBy) { @@ -716,8 +715,8 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { auto singleElementSequence = {'z'}; UNIT_ASSERT_VALUES_EQUAL(*MinElementBy(singleElementSequence, identity), 'z'); - const std::vector strings = {"one", "two", "three", "four"}; - auto stringLength = [](TStringBuf s) { + const std::vector strings = {"one", "two", "three", "four"}; + auto stringLength = [](std::string_view s) { return s.size(); }; UNIT_ASSERT_STRINGS_EQUAL(*MinElementBy(strings, stringLength), "one"); @@ -762,7 +761,7 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { : Acc(acc) { } - void operator()(const TString& s) { + void operator()(const std::string& s) { Acc += s.size(); } void operator()(int v) { @@ -770,7 +769,7 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { } int& Acc; }; - TString s{"8-800-555-35-35"}; + std::string s{"8-800-555-35-35"}; ApplyToMany(TVisitor{res = 0}, 1, s, 5, s); UNIT_ASSERT_EQUAL(res, 12 + 2 * static_cast(s.size())); } @@ -792,7 +791,7 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { UNIT_ASSERT(AllOf(std::make_tuple(1, 2), pred)); } { - auto ts = std::make_tuple(TString{"foo"}, TString{"bar"}); + auto ts = std::make_tuple(std::string{"foo"}, std::string{"bar"}); auto pred = [](auto s) { return s.size() == 3; }; UNIT_ASSERT_VALUES_EQUAL(AllOf(ts, pred), AllOf(ts, pred)); } @@ -806,7 +805,7 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { UNIT_ASSERT(!AnyOf(std::make_tuple(1, 2), pred)); UNIT_ASSERT(!AnyOf(std::make_tuple(1, 2), pred)); { - auto ts = std::make_tuple(TString{"f"}, TString{"bar"}); + auto ts = std::make_tuple(std::string{"f"}, std::string{"bar"}); auto pred = [](auto s) { return s.size() == 3; }; UNIT_ASSERT_VALUES_EQUAL(AnyOf(ts, pred), AnyOf(ts, pred)); } @@ -901,6 +900,6 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { Y_UNIT_TEST(AccumulateWithBinOp) { std::vector v = {1, 2, 777}; - UNIT_ASSERT_VALUES_EQUAL(TString("begin;1;2;777"), Accumulate(v, TString("begin"), [](auto&& a, auto& b) { return a + ";" + ToString(b); })); + UNIT_ASSERT_VALUES_EQUAL(std::string("begin;1;2;777"), Accumulate(v, std::string("begin"), [](auto&& a, auto& b) { return a + ";" + ToString(b); })); } } diff --git a/util/generic/bitops_ut.cpp b/util/generic/bitops_ut.cpp index 90f4bc1c3ce..13925460d2c 100644 --- a/util/generic/bitops_ut.cpp +++ b/util/generic/bitops_ut.cpp @@ -141,7 +141,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { for (ui32 i = 0; i < 0x10000; ++i) { for (ui32 j = 0; j <= 32; ++j) { - UNIT_ASSERT_VALUES_EQUAL_C(i, ReverseBits(ReverseBits(i, j), j), (TString)(TStringBuilder() << i << " " << j)); + UNIT_ASSERT_VALUES_EQUAL_C(i, ReverseBits(ReverseBits(i, j), j), (std::string)(TStringBuilder() << i << " " << j)); } } } diff --git a/util/generic/buffer.cpp b/util/generic/buffer.cpp index b92697e1d00..d43460af52f 100644 --- a/util/generic/buffer.cpp +++ b/util/generic/buffer.cpp @@ -93,7 +93,7 @@ TBuffer::~TBuffer() { y_deallocate(Data_); } -void TBuffer::AsString(TString& s) { +void TBuffer::AsString(std::string& s) { s.assign(Data(), Size()); Clear(); } diff --git a/util/generic/buffer.h b/util/generic/buffer.h index a5efe398ce7..2378724b0fe 100644 --- a/util/generic/buffer.h +++ b/util/generic/buffer.h @@ -203,7 +203,7 @@ class TBuffer { /* * after this call buffer becomes empty */ - void AsString(TString& s); + void AsString(std::string& s); /* * iterator-like interface diff --git a/util/generic/buffer_ut.cpp b/util/generic/buffer_ut.cpp index 7524de8deee..dc02f48dee6 100644 --- a/util/generic/buffer_ut.cpp +++ b/util/generic/buffer_ut.cpp @@ -14,14 +14,14 @@ Y_UNIT_TEST_SUITE(TBufferTest) { buf.Resize(6); buf.EraseBack(2); - UNIT_ASSERT_EQUAL(TString(buf.data(), buf.size()), "1234"); + UNIT_ASSERT_EQUAL(std::string(buf.data(), buf.size()), "1234"); } Y_UNIT_TEST(TestAppend) { const char data[] = "1234567890qwertyuiop"; TBuffer buf(13); - TString str; + std::string str; for (size_t i = 0; i < 10; ++i) { for (size_t j = 0; j < sizeof(data) - 1; ++j) { @@ -32,7 +32,7 @@ Y_UNIT_TEST_SUITE(TBufferTest) { } } - UNIT_ASSERT_EQUAL(TString(buf.data(), buf.size()), str); + UNIT_ASSERT_EQUAL(std::string(buf.data(), buf.size()), str); } Y_UNIT_TEST(TestReset) { @@ -62,10 +62,10 @@ Y_UNIT_TEST_SUITE(TBufferTest) { buf.Resize(9); memcpy(buf.data(), content, 9); - UNIT_ASSERT_VALUES_EQUAL(TString(buf.data(), buf.size()), "some text"); + UNIT_ASSERT_VALUES_EQUAL(std::string(buf.data(), buf.size()), "some text"); buf.Resize(4); - UNIT_ASSERT_VALUES_EQUAL(TString(buf.data(), buf.size()), "some"); + UNIT_ASSERT_VALUES_EQUAL(std::string(buf.data(), buf.size()), "some"); } Y_UNIT_TEST(TestReserve) { @@ -91,11 +91,11 @@ Y_UNIT_TEST_SUITE(TBufferTest) { buf.Append('a'); UNIT_ASSERT_EQUAL(buf.Capacity(), 256); - TString tmp1 = "abcdef"; + std::string tmp1 = "abcdef"; buf.Append(tmp1.data(), tmp1.size()); UNIT_ASSERT_EQUAL(buf.Capacity(), 256); - TString tmp2 = "30498290sfokdsflj2308w"; + std::string tmp2 = "30498290sfokdsflj2308w"; buf.Resize(1020); buf.Append(tmp2.data(), tmp2.size()); UNIT_ASSERT_EQUAL(buf.Capacity(), 2048); @@ -104,7 +104,7 @@ Y_UNIT_TEST_SUITE(TBufferTest) { Y_UNIT_TEST(TestShrinkToFit) { TBuffer buf; - TString content = "some text"; + std::string content = "some text"; buf.Append(content.data(), content.size()); UNIT_ASSERT_EQUAL(buf.Size(), 9); UNIT_ASSERT_EQUAL(buf.Capacity(), 16); @@ -112,7 +112,7 @@ Y_UNIT_TEST_SUITE(TBufferTest) { buf.ShrinkToFit(); UNIT_ASSERT_EQUAL(buf.Size(), 9); UNIT_ASSERT_EQUAL(buf.Capacity(), 9); - UNIT_ASSERT_EQUAL(TString(buf.data(), buf.size()), content); + UNIT_ASSERT_EQUAL(std::string(buf.data(), buf.size()), content); const size_t MB = 1024 * 1024; buf.Resize(MB); @@ -134,13 +134,13 @@ Y_UNIT_TEST(TestAlignUp) { buf.AlignUp(4, '!'); UNIT_ASSERT(buf.Size() % 4 == 0); - UNIT_ASSERT_VALUES_EQUAL(TString(~buf, +buf), "some text!!!"); + UNIT_ASSERT_VALUES_EQUAL(std::string(~buf, +buf), "some text!!!"); char addContent[] = "1234"; buf.Append(addContent, sizeof(addContent)); buf.AlignUp(4, 'X'); UNIT_ASSERT(buf.Size() % 4 == 0); - UNIT_ASSERT_VALUES_EQUAL(TString(~buf, +buf), "some text!!!1234"); + UNIT_ASSERT_VALUES_EQUAL(std::string(~buf, +buf), "some text!!!1234"); } #endif @@ -184,10 +184,10 @@ Y_UNIT_TEST(TestSpeed) { buf.Append("Some ", 5); buf.Fill('!', 5); buf.Append(" text.", 6); - UNIT_ASSERT_VALUES_EQUAL(TString(buf.data(), buf.size()), "Some !!!!! text."); + UNIT_ASSERT_VALUES_EQUAL(std::string(buf.data(), buf.size()), "Some !!!!! text."); buf.Chop(5, 6); - UNIT_ASSERT_VALUES_EQUAL(TString(buf.data(), buf.size()), "Some text."); + UNIT_ASSERT_VALUES_EQUAL(std::string(buf.data(), buf.size()), "Some text."); } Y_UNIT_TEST(TestComparison) { diff --git a/util/generic/function_ref_ut.cpp b/util/generic/function_ref_ut.cpp index 59f9ae35cd8..ac1a34975c2 100644 --- a/util/generic/function_ref_ut.cpp +++ b/util/generic/function_ref_ut.cpp @@ -130,7 +130,7 @@ Y_UNIT_TEST_SUITE(TestFunctionRef) { ref1 = [] { return 'a'; }; ref1 = [] { return 124u; }; - TFunctionRef ref2{Greet}; + TFunctionRef ref2{Greet}; } Y_UNIT_TEST(StatelessLambdaLifetime) { diff --git a/util/generic/fwd.h b/util/generic/fwd.h index a0e54eb389b..84da6f5fdf8 100644 --- a/util/generic/fwd.h +++ b/util/generic/fwd.h @@ -4,20 +4,6 @@ #include -template > -class TBasicString; - -using TString = TBasicString; -using TUtf16String = TBasicString; -using TUtf32String = TBasicString; - -template > -class TBasicStringBuf; - -using TStringBuf = TBasicStringBuf; -using TWtringBuf = TBasicStringBuf; -using TUtf32StringBuf = TBasicStringBuf; - //misc class TBuffer; diff --git a/util/generic/guid.cpp b/util/generic/guid.cpp index 8b907457bc6..08d820f58bd 100644 --- a/util/generic/guid.cpp +++ b/util/generic/guid.cpp @@ -9,14 +9,14 @@ #include namespace { - inline void LowerCaseHex(TString& s) { + inline void LowerCaseHex(std::string& s) { for (auto&& c : s) { c = AsciiToLower(c); } } } -TString TGUID::AsGuidString() const { +std::string TGUID::AsGuidString() const { TStringBuilder s; s.reserve(50); s << Hex(dw[0], 0) << '-' << Hex(dw[1], 0) << '-' << Hex(dw[2], 0) << '-' << Hex(dw[3], 0); @@ -24,7 +24,7 @@ TString TGUID::AsGuidString() const { return std::move(s); } -TString TGUID::AsUuidString() const { +std::string TGUID::AsUuidString() const { TStringBuilder s; s.reserve(50); s << Hex(dw[0], HF_FULL) << '-'; @@ -65,11 +65,11 @@ TGUID TGUID::CreateTimebased() { return result; } -TString GetGuidAsString(const TGUID& g) { +std::string GetGuidAsString(const TGUID& g) { return g.AsGuidString(); } -TString CreateGuidAsString() { +std::string CreateGuidAsString() { return TGUID::Create().AsGuidString(); } @@ -87,7 +87,7 @@ static bool GetDigit(const char c, ui32& digit) { return true; } -bool GetGuid(const TStringBuf s, TGUID& result) { +bool GetGuid(const std::string_view s, TGUID& result) { size_t partId = 0; ui64 partValue = 0; bool isEmptyPart = true; @@ -129,7 +129,7 @@ bool GetGuid(const TStringBuf s, TGUID& result) { // Parses GUID from s and checks that it's valid. // In case of error returns TGUID(). -TGUID GetGuid(const TStringBuf s) { +TGUID GetGuid(const std::string_view s) { TGUID result; if (GetGuid(s, result)) { @@ -139,7 +139,7 @@ TGUID GetGuid(const TStringBuf s) { return TGUID(); } -bool GetUuid(const TStringBuf s, TGUID& result) { +bool GetUuid(const std::string_view s, TGUID& result) { if (s.size() != 36) { return false; } @@ -175,7 +175,7 @@ bool GetUuid(const TStringBuf s, TGUID& result) { // Parses GUID from uuid and checks that it's valid. // In case of error returns TGUID(). -TGUID GetUuid(const TStringBuf s) { +TGUID GetUuid(const std::string_view s) { TGUID result; if (GetUuid(s, result)) { diff --git a/util/generic/guid.h b/util/generic/guid.h index 93e06a81f87..0bb2b332ccf 100644 --- a/util/generic/guid.h +++ b/util/generic/guid.h @@ -26,14 +26,14 @@ struct TGUID { } // xxxx-xxxx-xxxx-xxxx - TString AsGuidString() const; + std::string AsGuidString() const; /** * RFC4122 GUID, which described in * https://en.wikipedia.org/wiki/Universally_unique_identifier * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx **/ - TString AsUuidString() const; + std::string AsUuidString() const; static TGUID Create(); @@ -66,14 +66,14 @@ struct THash { }; void CreateGuid(TGUID* res); -TString GetGuidAsString(const TGUID& g); -TString CreateGuidAsString(); -TGUID GetGuid(TStringBuf s); -bool GetGuid(TStringBuf s, TGUID& result); +std::string GetGuidAsString(const TGUID& g); +std::string CreateGuidAsString(); +TGUID GetGuid(std::string_view s); +bool GetGuid(std::string_view s, TGUID& result); /** * Functions for correct parsing RFC4122 GUID, which described in * https://en.wikipedia.org/wiki/Universally_unique_identifier **/ -TGUID GetUuid(TStringBuf s); -bool GetUuid(TStringBuf s, TGUID& result); +TGUID GetUuid(std::string_view s); +bool GetUuid(std::string_view s, TGUID& result); diff --git a/util/generic/guid_ut.cpp b/util/generic/guid_ut.cpp index 82347941e60..fc0ccac6428 100644 --- a/util/generic/guid_ut.cpp +++ b/util/generic/guid_ut.cpp @@ -17,7 +17,7 @@ Y_UNIT_TEST_SUITE(TGuidTest) { struct TTest { TGUID G; - TString S; + std::string S; }; Y_UNIT_TEST(Test1) { @@ -121,7 +121,7 @@ Y_UNIT_TEST_SUITE(TGuidTest) { } Y_UNIT_TEST(TimeBased) { - TString guid = TGUID::CreateTimebased().AsUuidString(); + std::string guid = TGUID::CreateTimebased().AsUuidString(); UNIT_ASSERT(!guid.empty()); UNIT_ASSERT_EQUAL(guid[14], '1'); } diff --git a/util/generic/hash_set_ut.pyx b/util/generic/hash_set_ut.pyx index bdcf6284afa..b431df5682a 100644 --- a/util/generic/hash_set_ut.pyx +++ b/util/generic/hash_set_ut.pyx @@ -1,7 +1,7 @@ # cython: c_string_type=str, c_string_encoding=utf8 from util.generic.hash_set cimport THashSet -from util.generic.string cimport TString +from util.generic.string cimport std::string import pytest import unittest @@ -30,24 +30,24 @@ class TestHashSet(unittest.TestCase): assert c1 == c2 def test_insert_erase(self): - cdef THashSet[TString] tmp + cdef THashSet[std::string] tmp self.assertTrue(tmp.insert("one").second) self.assertFalse(tmp.insert("one").second) self.assertTrue(tmp.insert("two").second) - cdef TString one = "one" - cdef TString two = "two" + cdef std::string one = "one" + cdef std::string two = "two" self.assertEqual(tmp.erase(one), 1) self.assertEqual(tmp.erase(two), 1) self.assertEqual(tmp.size(), 0) self.assertTrue(tmp.empty()) def test_iterators_and_find(self): - cdef THashSet[TString] tmp + cdef THashSet[std::string] tmp self.assertTrue(tmp.begin() == tmp.end()) self.assertTrue(tmp.find("1") == tmp.end()) tmp.insert("1") self.assertTrue(tmp.begin() != tmp.end()) - cdef THashSet[TString].iterator it = tmp.find("1") + cdef THashSet[std::string].iterator it = tmp.find("1") self.assertTrue(it != tmp.end()) self.assertEqual(deref(it), "1") diff --git a/util/generic/hash_table.cpp b/util/generic/hash_table.cpp index a6b119e821e..2b27972d7c9 100644 --- a/util/generic/hash_table.cpp +++ b/util/generic/hash_table.cpp @@ -5,47 +5,47 @@ const void* const _yhashtable_empty_data[] = {(void*)3, nullptr, (void*)1}; -TString NPrivate::MapKeyToString(TStringBuf key) { +std::string NPrivate::MapKeyToString(std::string_view key) { constexpr size_t HASH_KEY_MAX_LENGTH = 500; try { return EscapeC(key.substr(0, HASH_KEY_MAX_LENGTH)); } catch (...) { - return "TStringBuf"; + return "std::string_view"; } } -TString NPrivate::MapKeyToString(unsigned short key) { +std::string NPrivate::MapKeyToString(unsigned short key) { return ToString(key); } -TString NPrivate::MapKeyToString(short key) { +std::string NPrivate::MapKeyToString(short key) { return ToString(key); } -TString NPrivate::MapKeyToString(unsigned int key) { +std::string NPrivate::MapKeyToString(unsigned int key) { return ToString(key); } -TString NPrivate::MapKeyToString(int key) { +std::string NPrivate::MapKeyToString(int key) { return ToString(key); } -TString NPrivate::MapKeyToString(unsigned long key) { +std::string NPrivate::MapKeyToString(unsigned long key) { return ToString(key); } -TString NPrivate::MapKeyToString(long key) { +std::string NPrivate::MapKeyToString(long key) { return ToString(key); } -TString NPrivate::MapKeyToString(unsigned long long key) { +std::string NPrivate::MapKeyToString(unsigned long long key) { return ToString(key); } -TString NPrivate::MapKeyToString(long long key) { +std::string NPrivate::MapKeyToString(long long key) { return ToString(key); } -void NPrivate::ThrowKeyNotFoundInHashTableException(const TStringBuf keyRepresentation) { +void NPrivate::ThrowKeyNotFoundInHashTableException(const std::string_view keyRepresentation) { ythrow yexception() << "Key not found in hashtable: " << keyRepresentation; } diff --git a/util/generic/hash_table.h b/util/generic/hash_table.h index 0fbbef67892..abfe0f229e4 100644 --- a/util/generic/hash_table.h +++ b/util/generic/hash_table.h @@ -1404,33 +1404,33 @@ void THashTable::copy_from_dynamic(const THashTable& ht) { namespace NPrivate { template - inline TString MapKeyToString(const Key&) { + inline std::string MapKeyToString(const Key&) { return TypeName(); } - TString MapKeyToString(TStringBuf key); - TString MapKeyToString(unsigned short key); - TString MapKeyToString(short key); - TString MapKeyToString(unsigned int key); - TString MapKeyToString(int key); - TString MapKeyToString(unsigned long key); - TString MapKeyToString(long key); - TString MapKeyToString(unsigned long long key); - TString MapKeyToString(long long key); + std::string MapKeyToString(std::string_view key); + std::string MapKeyToString(unsigned short key); + std::string MapKeyToString(short key); + std::string MapKeyToString(unsigned int key); + std::string MapKeyToString(int key); + std::string MapKeyToString(unsigned long key); + std::string MapKeyToString(long key); + std::string MapKeyToString(unsigned long long key); + std::string MapKeyToString(long long key); - inline TString MapKeyToString(const TString& key) { - return MapKeyToString(TStringBuf(key)); + inline std::string MapKeyToString(const std::string& key) { + return MapKeyToString(std::string_view(key)); } - inline TString MapKeyToString(const char* key) { - return MapKeyToString(TStringBuf(key)); + inline std::string MapKeyToString(const char* key) { + return MapKeyToString(std::string_view(key)); } - inline TString MapKeyToString(char* key) { - return MapKeyToString(TStringBuf(key)); + inline std::string MapKeyToString(char* key) { + return MapKeyToString(std::string_view(key)); } - [[noreturn]] void ThrowKeyNotFoundInHashTableException(const TStringBuf keyRepresentation); + [[noreturn]] void ThrowKeyNotFoundInHashTableException(const std::string_view keyRepresentation); } // Cannot name it just 'Hash' because it clashes with too many class members in the code. diff --git a/util/generic/hash_ut.cpp b/util/generic/hash_ut.cpp index f8bf4e8eac2..eb8f8bb334d 100644 --- a/util/generic/hash_ut.cpp +++ b/util/generic/hash_ut.cpp @@ -120,7 +120,7 @@ class THashTest: public TTestBase { UNIT_TEST_SUITE_REGISTRATION(THashTest); void THashTest::TestHMapConstructorsAndAssignments() { - using container = THashMap; + using container = THashMap; container c1; c1["one"] = 1; @@ -171,7 +171,7 @@ void THashTest::TestHMapConstructorsAndAssignments() { } void THashTest::TestHMap1() { - using maptype = THashMap, TEqualTo>; + using maptype = THashMap, TEqualTo>; maptype m; // Store mappings between roman numerals and decimals. m['l'] = "50"; @@ -187,11 +187,11 @@ void THashTest::TestHMap1() { UNIT_ASSERT(m.contains('z')); UNIT_ASSERT(m.count('z') == 1); - auto p = m.insert(std::pair('c', TString("100"))); + auto p = m.insert(std::pair('c', std::string("100"))); UNIT_ASSERT(p.second); - p = m.insert(std::pair('c', TString("100"))); + p = m.insert(std::pair('c', std::string("100"))); UNIT_ASSERT(!p.second); // Some iterators compare check, really compile time checks @@ -208,7 +208,7 @@ void THashTest::TestHMap1() { } void THashTest::TestHMapEqualityOperator() { - using container = THashMap; + using container = THashMap; container base; base["one"] = 1; @@ -231,7 +231,7 @@ void THashTest::TestHMapEqualityOperator() { } void THashTest::TestHMMapEqualityOperator() { - using container = THashMultiMap; + using container = THashMultiMap; using value = container::value_type; container base; @@ -266,7 +266,7 @@ void THashTest::TestHMMapEqualityOperator() { } void THashTest::TestHMMapConstructorsAndAssignments() { - using container = THashMultiMap; + using container = THashMultiMap; container c1; c1.insert(container::value_type("one", 1)); @@ -610,7 +610,7 @@ void THashTest::TestHMSetEmplace() { } void THashTest::TestInsertErase() { - using hmap = THashMap, TEqualTo>; + using hmap = THashMap, TEqualTo>; using val_type = hmap::value_type; { @@ -640,10 +640,10 @@ void THashTest::TestInsertErase() { namespace { struct TItem: public TSimpleRefCount { - const TString Key; - const TString Value; + const std::string Key; + const std::string Value; - TItem(const TString& key, const TString& value) + TItem(const std::string& key, const std::string& value) : Key(key) , Value(value) { @@ -653,26 +653,26 @@ namespace { using TItemPtr = TIntrusivePtr; struct TSelectKey { - const TString& operator()(const TItemPtr& item) const { + const std::string& operator()(const TItemPtr& item) const { return item->Key; } }; using TItemMapBase = THashTable< TItemPtr, - TString, - THash, + std::string, + THash, TSelectKey, - TEqualTo, + TEqualTo, std::allocator>; struct TItemMap: public TItemMapBase { TItemMap() - : TItemMapBase(1, THash(), TEqualTo()) + : TItemMapBase(1, THash(), TEqualTo()) { } - TItem& Add(const TString& key, const TString& value) { + TItem& Add(const std::string& key, const std::string& value) { insert_ctx ins; iterator it = find_i(key, ins); if (it == end()) { @@ -1183,11 +1183,11 @@ void THashTest::TestAt() { } \ } - TEST_AT_THROWN_EXCEPTION(TString, TString, TString, "111", "111"); - TEST_AT_THROWN_EXCEPTION(TString, TString, const TString, "111", "111"); - TEST_AT_THROWN_EXCEPTION(TString, TString, TStringBuf, "111", "111"); - TEST_AT_THROWN_EXCEPTION(TString, TString, const TStringBuf, "111", "111"); - TEST_AT_THROWN_EXCEPTION(TStringBuf, TStringBuf, const char*, "111", "111"); + TEST_AT_THROWN_EXCEPTION(std::string, std::string, std::string, "111", "111"); + TEST_AT_THROWN_EXCEPTION(std::string, std::string, const std::string, "111", "111"); + TEST_AT_THROWN_EXCEPTION(std::string, std::string, std::string_view, "111", "111"); + TEST_AT_THROWN_EXCEPTION(std::string, std::string, const std::string_view, "111", "111"); + TEST_AT_THROWN_EXCEPTION(std::string_view, std::string_view, const char*, "111", "111"); TEST_AT_THROWN_EXCEPTION(int, int, short, 11, "11"); TEST_AT_THROWN_EXCEPTION(int, int, int, -1, "-1"); TEST_AT_THROWN_EXCEPTION(int, int, long, 111, "111"); @@ -1198,36 +1198,36 @@ void THashTest::TestAt() { TEST_AT_THROWN_EXCEPTION(int, int, unsigned long long, 1000000000000ll, "1000000000000"); char key[] = {11, 12, 0, 1, 2, 11, 0}; - TEST_AT_THROWN_EXCEPTION(TString, TString, char*, key, "\\x0B\\x0C"); - TEST_AT_THROWN_EXCEPTION(TString, TString, TStringBuf, TStringBuf(key, sizeof(key) - 1), "\\x0B\\x0C\\0\\1\\2\\x0B"); + TEST_AT_THROWN_EXCEPTION(std::string, std::string, char*, key, "\\x0B\\x0C"); + TEST_AT_THROWN_EXCEPTION(std::string, std::string, std::string_view, std::string_view(key, sizeof(key) - 1), "\\x0B\\x0C\\0\\1\\2\\x0B"); #undef TEST_AT_THROWN_EXCEPTION } void THashTest::TestHMapInitializerList() { - THashMap h1 = {{"foo", "bar"}, {"bar", "baz"}, {"baz", "qux"}}; - THashMap h2; - h2.insert(std::pair("foo", "bar")); - h2.insert(std::pair("bar", "baz")); - h2.insert(std::pair("baz", "qux")); + THashMap h1 = {{"foo", "bar"}, {"bar", "baz"}, {"baz", "qux"}}; + THashMap h2; + h2.insert(std::pair("foo", "bar")); + h2.insert(std::pair("bar", "baz")); + h2.insert(std::pair("baz", "qux")); UNIT_ASSERT_EQUAL(h1, h2); } void THashTest::TestHMMapInitializerList() { - THashMultiMap h1 = { + THashMultiMap h1 = { {"foo", "bar"}, {"foo", "baz"}, {"baz", "qux"}}; - THashMultiMap h2; - h2.insert(std::pair("foo", "bar")); - h2.insert(std::pair("foo", "baz")); - h2.insert(std::pair("baz", "qux")); + THashMultiMap h2; + h2.insert(std::pair("foo", "bar")); + h2.insert(std::pair("foo", "baz")); + h2.insert(std::pair("baz", "qux")); UNIT_ASSERT_EQUAL(h1, h2); } void THashTest::TestHSetInitializerList() { - THashSet h1 = {"foo", "bar", "baz"}; - THashSet h2; + THashSet h1 = {"foo", "bar", "baz"}; + THashSet h2; h2.insert("foo"); h2.insert("bar"); h2.insert("baz"); @@ -1235,8 +1235,8 @@ void THashTest::TestHSetInitializerList() { } void THashTest::TestHMSetInitializerList() { - THashMultiSet h1 = {"foo", "foo", "bar", "baz"}; - THashMultiSet h2; + THashMultiSet h1 = {"foo", "foo", "bar", "baz"}; + THashMultiSet h2; h2.insert("foo"); h2.insert("foo"); h2.insert("bar"); @@ -1328,9 +1328,9 @@ void THashTest::TestTupleHash() { void THashTest::TestStringHash() { // Make sure that different THash<> variants behave in the same way - const size_t expected = ComputeHash(TString("hehe")); + const size_t expected = ComputeHash(std::string("hehe")); UNIT_ASSERT_VALUES_EQUAL(ComputeHash("hehe"), expected); // char[5] UNIT_ASSERT_VALUES_EQUAL(ComputeHash("hehe"sv), expected); // std::string_view - UNIT_ASSERT_VALUES_EQUAL(ComputeHash(TStringBuf("hehe")), expected); // TStringBuf + UNIT_ASSERT_VALUES_EQUAL(ComputeHash(std::string_view("hehe")), expected); // std::string_view UNIT_ASSERT_VALUES_EQUAL(ComputeHash("hehe"), expected); // const char* } diff --git a/util/generic/hash_ut.pyx b/util/generic/hash_ut.pyx index ecf6dac2e68..9e54ba20e1e 100644 --- a/util/generic/hash_ut.pyx +++ b/util/generic/hash_ut.pyx @@ -1,7 +1,6 @@ # cython: c_string_type=str, c_string_encoding=utf8 from util.generic.hash cimport THashMap -from util.generic.string cimport TString import pytest import unittest @@ -10,17 +9,17 @@ from libcpp.pair cimport pair from cython.operator cimport dereference as deref -def _check_convert(THashMap[TString, int] x): +def _check_convert(THashMap[std::string, int] x): return x class TestHash(unittest.TestCase): def test_constructors_and_assignments(self): - cdef THashMap[TString, int] c1 + cdef THashMap[std::string, int] c1 c1["one"] = 1 c1["two"] = 2 - cdef THashMap[TString, int] c2 = THashMap[TString, int](c1) + cdef THashMap[std::string, int] c2 = THashMap[std::string, int](c1) self.assertEqual(2, c1.size()) self.assertEqual(2, c2.size()) self.assertEqual(1, c1.at("one")) @@ -34,14 +33,14 @@ class TestHash(unittest.TestCase): self.assertEqual(3, c1.at("three")) def test_equality_operator(self): - cdef THashMap[TString, int] base + cdef THashMap[std::string, int] base base["one"] = 1 base["two"] = 2 - cdef THashMap[TString, int] c1 = THashMap[TString, int](base) + cdef THashMap[std::string, int] c1 = THashMap[std::string, int](base) self.assertTrue(c1==base) - cdef THashMap[TString, int] c2 + cdef THashMap[std::string, int] c2 c2["one"] = 1 c2["two"] = 2 self.assertTrue(c2 == base) @@ -49,29 +48,29 @@ class TestHash(unittest.TestCase): c2["three"] = 3 self.assertTrue(c2 != base) - cdef THashMap[TString, int] c3 = THashMap[TString, int](base) + cdef THashMap[std::string, int] c3 = THashMap[std::string, int](base) c3["one"] = 0 self.assertTrue(c3 != base) def test_insert_erase(self): - cdef THashMap[TString, int] tmp - self.assertTrue(tmp.insert(pair[TString, int]("one", 0)).second) - self.assertFalse(tmp.insert(pair[TString, int]("one", 1)).second) - self.assertTrue(tmp.insert(pair[TString, int]("two", 2)).second) - cdef TString one = "one" - cdef TString two = "two" + cdef THashMap[std::string, int] tmp + self.assertTrue(tmp.insert(pair[std::string, int]("one", 0)).second) + self.assertFalse(tmp.insert(pair[std::string, int]("one", 1)).second) + self.assertTrue(tmp.insert(pair[std::string, int]("two", 2)).second) + cdef std::string one = "one" + cdef std::string two = "two" self.assertEqual(tmp.erase(one), 1) self.assertEqual(tmp.erase(two), 1) self.assertEqual(tmp.size(), 0) self.assertTrue(tmp.empty()) def test_iterators_and_find(self): - cdef THashMap[TString, int] tmp + cdef THashMap[std::string, int] tmp self.assertTrue(tmp.begin() == tmp.end()) self.assertTrue(tmp.find("1") == tmp.end()) tmp["1"] = 1 self.assertTrue(tmp.begin() != tmp.end()) - cdef THashMap[TString, int].iterator it = tmp.find("1") + cdef THashMap[std::string, int].iterator it = tmp.find("1") self.assertTrue(it != tmp.end()) self.assertEqual(deref(it).second, 1) diff --git a/util/generic/is_in_ut.cpp b/util/generic/is_in_ut.cpp index ccfce0f7415..d07e39bdfd5 100644 --- a/util/generic/is_in_ut.cpp +++ b/util/generic/is_in_ut.cpp @@ -6,7 +6,6 @@ #include "hash_set.h" #include "is_in.h" #include "set.h" -#include "strbuf.h" #include "string.h" Y_UNIT_TEST_SUITE(TIsIn) { @@ -41,16 +40,16 @@ Y_UNIT_TEST_SUITE(TIsIn) { } Y_UNIT_TEST(IsInTest) { - TestIsInWithCont>(std::make_pair("found", "1")); - TestIsInWithCont>(std::make_pair("found", "1")); + TestIsInWithCont>(std::make_pair("found", "1")); + TestIsInWithCont>(std::make_pair("found", "1")); - TestIsInWithCont>("found"); - TestIsInWithCont>("found"); - TestIsInWithCont>("found"); - TestIsInWithCont>("found"); + TestIsInWithCont>("found"); + TestIsInWithCont>("found"); + TestIsInWithCont>("found"); + TestIsInWithCont>("found"); // vector also compiles and works - std::vector v; + std::vector v; v.push_back("found"); UNIT_ASSERT(IsIn(v, "found")); UNIT_ASSERT(!IsIn(v, "not found")); @@ -59,12 +58,12 @@ Y_UNIT_TEST_SUITE(TIsIn) { UNIT_ASSERT(IsIn(v.begin(), v.end(), "found")); UNIT_ASSERT(!IsIn(v.begin(), v.end(), "not found")); - // Works with TString (it has find, but find is not used) - TString s = "found"; + // Works with std::string (it has find, but find is not used) + std::string s = "found"; UNIT_ASSERT(IsIn(s, 'f')); UNIT_ASSERT(!IsIn(s, 'z')); - TStringBuf b = "found"; + std::string_view b = "found"; UNIT_ASSERT(IsIn(b, 'f')); UNIT_ASSERT(!IsIn(b, 'z')); } @@ -79,36 +78,36 @@ Y_UNIT_TEST_SUITE(TIsIn) { UNIT_ASSERT(IsIn({6}, 6)); UNIT_ASSERT(!IsIn({6}, 7)); UNIT_ASSERT(!IsIn(std::initializer_list(), 6)); - UNIT_ASSERT(IsIn({TStringBuf("abc"), TStringBuf("def")}, TStringBuf("abc"))); - UNIT_ASSERT(IsIn({TStringBuf("abc"), TStringBuf("def")}, TStringBuf("def"))); - UNIT_ASSERT(IsIn({"abc", "def"}, TStringBuf("def"))); + UNIT_ASSERT(IsIn({std::string_view("abc"), std::string_view("def")}, std::string_view("abc"))); + UNIT_ASSERT(IsIn({std::string_view("abc"), std::string_view("def")}, std::string_view("def"))); + UNIT_ASSERT(IsIn({"abc", "def"}, std::string_view("def"))); UNIT_ASSERT(IsIn({abc, def}, def)); // direct pointer comparison - UNIT_ASSERT(!IsIn({TStringBuf("abc"), TStringBuf("def")}, TStringBuf("ghi"))); - UNIT_ASSERT(!IsIn({"abc", "def"}, TStringBuf("ghi"))); - UNIT_ASSERT(!IsIn({"abc", "def"}, TString("ghi"))); + UNIT_ASSERT(!IsIn({std::string_view("abc"), std::string_view("def")}, std::string_view("ghi"))); + UNIT_ASSERT(!IsIn({"abc", "def"}, std::string_view("ghi"))); + UNIT_ASSERT(!IsIn({"abc", "def"}, std::string("ghi"))); - const TStringBuf str = "abc////"; + const std::string_view str = "abc////"; - UNIT_ASSERT(IsIn({"abc", "def"}, TStringBuf{str.data(), 3})); + UNIT_ASSERT(IsIn({"abc", "def"}, std::string_view{str.data(), 3})); } Y_UNIT_TEST(ConfOfTest) { UNIT_ASSERT(IsIn({1, 2, 3}, 1)); UNIT_ASSERT(!IsIn({1, 2, 3}, 4)); - const TString b = "b"; + const std::string b = "b"; UNIT_ASSERT(!IsIn({"a", "b", "c"}, b.data())); // compares pointers by value. Whether it's good or not. - UNIT_ASSERT(IsIn(std::vector({"a", "b", "c"}), b.data())); - UNIT_ASSERT(IsIn(std::vector({"a", "b", "c"}), "b")); + UNIT_ASSERT(IsIn(std::vector({"a", "b", "c"}), b.data())); + UNIT_ASSERT(IsIn(std::vector({"a", "b", "c"}), "b")); } Y_UNIT_TEST(IsInArrayTest) { - const TString array[] = {"a", "b", "d"}; + const std::string array[] = {"a", "b", "d"}; UNIT_ASSERT(IsIn(array, "a")); - UNIT_ASSERT(IsIn(array, TString("b"))); + UNIT_ASSERT(IsIn(array, std::string("b"))); UNIT_ASSERT(!IsIn(array, "c")); - UNIT_ASSERT(IsIn(array, TStringBuf("d"))); + UNIT_ASSERT(IsIn(array, std::string_view("d"))); } } diff --git a/util/generic/iterator_ut.cpp b/util/generic/iterator_ut.cpp index 2c6f9b66726..9bdc2ba3a54 100644 --- a/util/generic/iterator_ut.cpp +++ b/util/generic/iterator_ut.cpp @@ -38,23 +38,23 @@ Y_UNIT_TEST_SUITE(TInputRangeAdaptor) { class TUrlPart: public TInputRangeAdaptor { public: - TUrlPart(const TStringBuf& url) + TUrlPart(const std::string_view& url) : Url_(url) { } - NStlIterator::TProxy Next() { + NStlIterator::TProxy Next() { return Url_.NextTok('/'); } private: - TStringBuf Url_; + std::string_view Url_; }; Y_UNIT_TEST(TUrlPart) { - const std::vector expected = {TStringBuf("yandex.ru"), TStringBuf("search?")}; + const std::vector expected = {std::string_view("yandex.ru"), std::string_view("search?")}; auto expected_part = expected.begin(); - for (const TStringBuf& part : TUrlPart(TStringBuf("yandex.ru/search?"))) { + for (const std::string_view& part : TUrlPart(std::string_view("yandex.ru/search?"))) { UNIT_ASSERT_VALUES_EQUAL(part, *expected_part); ++expected_part; } diff --git a/util/generic/join.h b/util/generic/join.h new file mode 100644 index 00000000000..e69de29bb2d diff --git a/util/generic/lazy_value_ut.cpp b/util/generic/lazy_value_ut.cpp index ff5a0156c53..73c58f35d2d 100644 --- a/util/generic/lazy_value_ut.cpp +++ b/util/generic/lazy_value_ut.cpp @@ -98,14 +98,14 @@ Y_UNIT_TEST_SUITE(TLazyValueTestSuite) { { } - const TString& GetData() const { + const std::string& GetData() const { return *Data_; } private: - TLazyValue Data_; + TLazyValue Data_; - TString ParseData() { + std::string ParseData() { CountParseDataCalled++; return "hi"; } diff --git a/util/generic/list.cpp b/util/generic/list.cpp deleted file mode 100644 index 471c2a14b77..00000000000 --- a/util/generic/list.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "list.h" diff --git a/util/generic/list.h b/util/generic/list.h deleted file mode 100644 index 7b0b8ffc723..00000000000 --- a/util/generic/list.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "fwd.h" - -#include - -#include -#include -#include -#include - -template -class TList: public std::list> { - using TBase = std::list>; - -public: - using TBase::TBase; - - inline explicit operator bool() const noexcept { - return !this->empty(); - } -}; diff --git a/util/generic/list.pxd b/util/generic/list.pxd deleted file mode 100644 index 5f3d92eac1c..00000000000 --- a/util/generic/list.pxd +++ /dev/null @@ -1,65 +0,0 @@ -cdef extern from "util/generic/list.h": - cdef cppclass TList[T]: - TList() except + - TList(TList&) except + - TList(size_t, T&) except + - - cppclass iterator: - iterator() - iterator(iterator &) - T& operator*() - iterator operator++() - iterator operator--() - bint operator==(iterator) - bint operator!=(iterator) - cppclass reverse_iterator: - reverse_iterator() - reverse_iterator(iterator &) - T& operator*() - reverse_iterator operator++() - reverse_iterator operator--() - bint operator==(reverse_iterator) - bint operator!=(reverse_iterator) - cppclass const_iterator(iterator): - pass - cppclass const_reverse_iterator(reverse_iterator): - pass - bint operator==(TList&, TList&) - bint operator!=(TList&, TList&) - bint operator<(TList&, TList&) - bint operator>(TList&, TList&) - bint operator<=(TList&, TList&) - bint operator>=(TList&, TList&) - void assign(size_t, T&) - T& back() - iterator begin() - const_iterator const_begin "begin"() - void clear() - bint empty() - iterator end() - const_iterator const_end "end"() - iterator erase(iterator) - iterator erase(iterator, iterator) - T& front() - iterator insert(iterator, T&) - void insert(iterator, size_t, T&) - size_t max_size() - void merge(TList&) - void pop_back() - void pop_front() - void push_back(T&) - void push_front(T&) - reverse_iterator rbegin() - const_reverse_iterator const_rbegin "rbegin"() - void remove(T&) - reverse_iterator rend() - const_reverse_iterator const_rend "rend"() - void resize(size_t, T&) - void reverse() - size_t size() - void sort() - void swap(TList&) - void splice(iterator, TList&) - void splice(iterator, TList&, iterator) - void splice(iterator, TList&, iterator, iterator) - void unique() diff --git a/util/generic/list_ut.pyx b/util/generic/list_ut.pyx deleted file mode 100644 index 129e5bc9b67..00000000000 --- a/util/generic/list_ut.pyx +++ /dev/null @@ -1,158 +0,0 @@ -from util.generic.list cimport TList - -import unittest -from cython.operator cimport preincrement - - -class TestList(unittest.TestCase): - - def test_ctor1(self): - cdef TList[int] tmp = TList[int]() - self.assertEqual(tmp.size(), 0) - - def test_ctor2(self): - cdef TList[int] tmp = TList[int](10, 42) - self.assertEqual(tmp.size(), 10) - self.assertEqual(tmp.front(), 42) - - def test_ctor3(self): - cdef TList[int] tmp = TList[int](10, 42) - cdef TList[int] tmp2 = TList[int](tmp) - self.assertEqual(tmp2.size(), 10) - self.assertEqual(tmp2.front(), 42) - - def test_operator_assign(self): - cdef TList[int] tmp2 - tmp2.push_back(1) - tmp2.push_back(2) - - cdef TList[int] tmp3 - tmp3.push_back(1) - tmp3.push_back(3) - - tmp3 = tmp2 - - def test_compare(self): - cdef TList[int] tmp1 - tmp1.push_back(1) - tmp1.push_back(2) - - cdef TList[int] tmp2 - tmp2.push_back(1) - tmp2.push_back(2) - - cdef TList[int] tmp3 - tmp3.push_back(1) - tmp3.push_back(3) - - self.assertTrue(tmp1 == tmp2) - self.assertTrue(tmp1 != tmp3) - - self.assertTrue(tmp1 < tmp3) - self.assertTrue(tmp1 <= tmp3) - - self.assertTrue(tmp3 > tmp1) - self.assertTrue(tmp3 >= tmp1) - - def test_push_pop_back(self): - cdef TList[int] tmp - self.assertEqual(tmp.size(), 0) - - tmp.push_back(42) - self.assertEqual(tmp.size(), 1) - self.assertEqual(tmp.back(), 42) - - tmp.push_back(77) - self.assertEqual(tmp.size(), 2) - self.assertEqual(tmp.back(), 77) - - tmp.pop_back() - self.assertEqual(tmp.size(), 1) - self.assertEqual(tmp.back(), 42) - - tmp.pop_back() - self.assertEqual(tmp.size(), 0) - - def test_front(self): - cdef TList[int] tmp - tmp.push_back(42) - self.assertEqual(tmp.front(), 42) - - def test_empty(self): - cdef TList[int] tmp - self.assertTrue(tmp.empty()) - tmp.push_back(42) - self.assertFalse(tmp.empty()) - - def test_max_size(self): - cdef TList[int] tmp - self.assertTrue(tmp.max_size() > 0) - - def test_resize(self): - cdef TList[int] tmp - - tmp.resize(100, 42) - self.assertEqual(tmp.size(), 100) - self.assertEqual(tmp.front(), 42) - self.assertEqual(tmp.back(), 42) - - def test_iter(self): - cdef TList[int] tmp - tmp.push_back(1) - tmp.push_back(20) - tmp.push_back(300) - - self.assertEqual([i for i in tmp], [1, 20, 300]) - - def test_iterator(self): - cdef TList[int] tmp - - self.assertTrue(tmp.begin() == tmp.end()) - self.assertTrue(tmp.rbegin() == tmp.rend()) - self.assertTrue(tmp.const_begin() == tmp.const_end()) - self.assertTrue(tmp.const_rbegin() == tmp.const_rend()) - - tmp.push_back(1) - - self.assertTrue(tmp.begin() != tmp.end()) - self.assertTrue(tmp.rbegin() != tmp.rend()) - self.assertTrue(tmp.const_begin() != tmp.const_end()) - self.assertTrue(tmp.const_rbegin() != tmp.const_rend()) - - self.assertTrue(preincrement(tmp.begin()) == tmp.end()) - self.assertTrue(preincrement(tmp.rbegin()) == tmp.rend()) - self.assertTrue(preincrement(tmp.const_begin()) == tmp.const_end()) - self.assertTrue(preincrement(tmp.const_rbegin()) == tmp.const_rend()) - - def test_assign(self): - cdef TList[int] tmp - - tmp.assign(10, 42) - self.assertEqual(tmp.size(), 10) - self.assertEqual(tmp.front(), 42) - self.assertEqual(tmp.back(), 42) - - def test_insert(self): - cdef TList[int] tmp - tmp.push_back(1) - tmp.push_back(2) - tmp.push_back(3) - - tmp.insert(tmp.begin(), 8) - self.assertEqual([i for i in tmp], [8, 1, 2, 3]) - - tmp.insert(tmp.begin(), 2, 6) - self.assertEqual([i for i in tmp], [6, 6, 8, 1, 2, 3]) - - def test_erase(self): - cdef TList[int] tmp - tmp.push_back(1) - tmp.push_back(2) - tmp.push_back(3) - tmp.push_back(4) - - tmp.erase(preincrement(tmp.begin())) - self.assertEqual([i for i in tmp], [1, 3, 4]) - - tmp.erase(tmp.begin(), preincrement(preincrement(tmp.begin()))) - self.assertEqual([i for i in tmp], [4]) diff --git a/util/generic/map.cpp b/util/generic/map.cpp deleted file mode 100644 index b323fbb46d6..00000000000 --- a/util/generic/map.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "map.h" diff --git a/util/generic/map.h b/util/generic/map.h deleted file mode 100644 index 13ff5cae7cf..00000000000 --- a/util/generic/map.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include "fwd.h" -#include "mapfindptr.h" - -#include -#include - -#include -#include -#include -#include - -template -class TMap: public std::map>>, public TMapOps> { - using TBase = std::map>>; - -public: - using TBase::TBase; - - inline explicit operator bool() const noexcept { - return !this->empty(); - } -}; - -template -class TMultiMap: public std::multimap>> { - using TBase = std::multimap>>; - -public: - using TBase::TBase; - - inline explicit operator bool() const noexcept { - return !this->empty(); - } -}; diff --git a/util/generic/map.pxd b/util/generic/map.pxd deleted file mode 100644 index 5c9fc32804e..00000000000 --- a/util/generic/map.pxd +++ /dev/null @@ -1,46 +0,0 @@ -from libcpp.pair cimport pair - -cdef extern from "util/generic/map.h" nogil: - cdef cppclass TMap[T, U]: - cppclass iterator: - pair[T, U]& operator*() - iterator operator++() - iterator operator--() - bint operator==(iterator) - bint operator!=(iterator) - - cppclass const_iterator(iterator): - pass - - TMap() except + - TMap(TMap&) except + - U& operator[](T&) - TMap& operator=(TMap&) - - bint operator==(TMap&) - bint operator!=(TMap&) - bint operator<(TMap&) - bint operator>(TMap&) - bint operator<=(TMap&) - bint operator>=(TMap&) - - U& at(T&) except + - iterator begin() - const_iterator const_begin "begin"() - void clear() - size_t count(T&) - bint empty() - iterator end() - const_iterator const_end "end"() - pair[iterator, iterator] equal_range(T&) - void erase(iterator) except + - void erase(iterator, iterator) except + - size_t erase(T&) - iterator find(T&) - bint contains(T&) - const_iterator const_find "find"(T&) - pair[iterator, bint] insert(pair[T, U]) # XXX pair[T,U]& - iterator insert(iterator, pair[T, U]) # XXX pair[T,U]& - size_t max_size() - size_t size() - void swap(TMap&) diff --git a/util/generic/mapfindptr_ut.cpp b/util/generic/mapfindptr_ut.cpp index 4178f3a0cc1..8b7ec831df5 100644 --- a/util/generic/mapfindptr_ut.cpp +++ b/util/generic/mapfindptr_ut.cpp @@ -8,7 +8,7 @@ #include "mapfindptr.h" Y_UNIT_TEST_SUITE(TMapFindPtrTest) { - struct TTestMap: std::map, TMapOps {}; + struct TTestMap: std::map, TMapOps {}; Y_UNIT_TEST(TestDerivedClass) { TTestMap a; @@ -19,7 +19,7 @@ Y_UNIT_TEST_SUITE(TMapFindPtrTest) { UNIT_ASSERT_EQUAL(a.FindPtr(0), nullptr); //test mutation - if (TString* p = a.FindPtr(42)) { + if (std::string* p = a.FindPtr(42)) { *p = "dog"; } UNIT_ASSERT(a.FindPtr(42)); @@ -35,12 +35,12 @@ Y_UNIT_TEST_SUITE(TMapFindPtrTest) { } Y_UNIT_TEST(TestTemplateFind) { - THashMap m; + THashMap m; - m[TString("x")] = 2; + m[std::string("x")] = 2; - UNIT_ASSERT(m.FindPtr(TStringBuf("x"))); - UNIT_ASSERT_EQUAL(*m.FindPtr(TStringBuf("x")), 2); + UNIT_ASSERT(m.FindPtr(std::string_view("x"))); + UNIT_ASSERT_EQUAL(*m.FindPtr(std::string_view("x")), 2); } Y_UNIT_TEST(TestValue) { @@ -58,8 +58,8 @@ Y_UNIT_TEST_SUITE(TMapFindPtrTest) { a[1] = "lol"; - const TString str123 = "123"; - TString str1234 = "1234"; + const std::string str123 = "123"; + std::string str1234 = "1234"; UNIT_ASSERT_VALUES_EQUAL(a.ValueRef(1, str123), "lol"); UNIT_ASSERT_VALUES_EQUAL(a.ValueRef(2, str123), "123"); diff --git a/util/generic/overloaded.h b/util/generic/overloaded.h index 96a97e44bc8..3653eb29464 100644 --- a/util/generic/overloaded.h +++ b/util/generic/overloaded.h @@ -5,11 +5,11 @@ * * Typically used with lambdas to construct type-matching visitors for e.g. std::variant: * ``` - * std::variant var; + * std::variant var; * Visit(TOverloaded{ * [](int val) { Cerr << "int: " << val; }, * [](void* val) { Cerr << "ptr: " << val; }, - * [](const TString& val) { Cerr << "str: " << val; }, + * [](const std::string& val) { Cerr << "str: " << val; }, * }, var); * ``` * diff --git a/util/generic/overloaded_ut.cpp b/util/generic/overloaded_ut.cpp index f3d73895ad9..fb165ee162e 100644 --- a/util/generic/overloaded_ut.cpp +++ b/util/generic/overloaded_ut.cpp @@ -40,7 +40,7 @@ Y_UNIT_TEST_SUITE(TOverloadedTest) { Y_UNIT_TEST(TupleTest) { std::tuple t{5, 3.14, true, 20}; - TString res; + std::string res; ForEach(t, TOverloaded{ [&](int val) { res += "(int) " + ToString(val) + ' '; }, diff --git a/util/generic/ptr_ut.cpp b/util/generic/ptr_ut.cpp index d35bd8f7e93..ba2176ffb15 100644 --- a/util/generic/ptr_ut.cpp +++ b/util/generic/ptr_ut.cpp @@ -768,13 +768,13 @@ struct TCustomCounter: TSimpleCounter { void TPointerTest::TestRefCountedPtrsInHashSet() { // test common case - TestRefCountedPtrsInHashSetImpl>(); - TestRefCountedPtrsInHashSetImpl>(); + TestRefCountedPtrsInHashSetImpl>(); + TestRefCountedPtrsInHashSetImpl>(); TestRefCountedPtrsInHashSetImpl>(); TestRefCountedPtrsInHashSetImpl>(); // test with custom ops - TestRefCountedPtrsInHashSetImpl>(); + TestRefCountedPtrsInHashSetImpl>(); TestRefCountedPtrsInHashSetImpl>(); TestRefCountedPtrsInHashSetImpl>(); } diff --git a/util/generic/ptr_ut.pyx b/util/generic/ptr_ut.pyx index 759681a2cbb..7b63c73ecad 100644 --- a/util/generic/ptr_ut.pyx +++ b/util/generic/ptr_ut.pyx @@ -1,6 +1,5 @@ from libcpp.utility cimport pair from util.generic.ptr cimport MakeAtomicShared, TAtomicSharedPtr, THolder -from util.generic.string cimport TString from util.system.types cimport ui64 import pytest @@ -10,17 +9,17 @@ import unittest class TestHolder(unittest.TestCase): def test_basic(self): - cdef THolder[TString] holder - holder.Reset(new TString("aaa")) + cdef THolder[std::string] holder + holder.Reset(new std::string("aaa")) assert holder.Get()[0] == "aaa" holder.Destroy() assert holder.Get() == NULL - holder.Reset(new TString("bbb")) + holder.Reset(new std::string("bbb")) assert holder.Get()[0] == "bbb" - holder.Reset(new TString("ccc")) + holder.Reset(new std::string("ccc")) assert holder.Get()[0] == "ccc" def test_make_atomic_shared(self): - cdef TAtomicSharedPtr[pair[ui64, TString]] atomic_shared_ptr = MakeAtomicShared[pair[ui64, TString]](15, "Some string") + cdef TAtomicSharedPtr[pair[ui64, std::string]] atomic_shared_ptr = MakeAtomicShared[pair[ui64, std::string]](15, "Some string") assert atomic_shared_ptr.Get().first == 15 assert atomic_shared_ptr.Get().second == "Some string" diff --git a/util/generic/queue.h b/util/generic/queue.h index f5959f68f28..37edf334939 100644 --- a/util/generic/queue.h +++ b/util/generic/queue.h @@ -2,7 +2,6 @@ #include "fwd.h" #include "deque.h" -#include "vector.h" #include "utility.h" #include diff --git a/util/generic/singleton_ut.cpp b/util/generic/singleton_ut.cpp index 35ba90cd760..c8cf1625e1e 100644 --- a/util/generic/singleton_ut.cpp +++ b/util/generic/singleton_ut.cpp @@ -14,14 +14,14 @@ Y_UNIT_TEST_SUITE(TestSingleton) { } struct TWithParams { - explicit TWithParams(const ui32 data1 = 0, const TString& data2 = TString()) + explicit TWithParams(const ui32 data1 = 0, const std::string& data2 = std::string()) : Data1(data1) , Data2(data2) { } ui32 Data1; - TString Data2; + std::string Data2; }; Y_UNIT_TEST(TestConstructorParamsOrder) { diff --git a/util/generic/strbase.h b/util/generic/strbase.h deleted file mode 100644 index 86e5c0f2f25..00000000000 --- a/util/generic/strbase.h +++ /dev/null @@ -1,554 +0,0 @@ -#pragma once - -// Some of these includes are just a legacy from previous implementation. -// We don't need them here, but removing them is tricky because it breaks all -// kinds of builds downstream -#include "mem_copy.h" -#include "ptr.h" -#include "utility.h" - -#include -#include -#include - -#include - -#include -#include -#include -#include - -namespace NStringPrivate { - template - size_t GetStringLengthWithLimit(const TCharType* s, size_t maxlen) { - Y_ASSERT(s); - size_t i = 0; - for (; i != maxlen && s[i]; ++i) - ; - return i; - } - - inline size_t GetStringLengthWithLimit(const char* s, size_t maxlen) { - Y_ASSERT(s); - return strnlen(s, maxlen); - } -} - -template > -class TStringBase { - using TStringView = std::basic_string_view; - using TStringViewWithTraits = std::basic_string_view; - -public: - using TChar = TCharType; - using TTraits = TTraitsType; - using TSelf = TStringBase; - - using size_type = size_t; - using difference_type = ptrdiff_t; - static constexpr size_t npos = size_t(-1); - - using const_iterator = const TCharType*; - using const_reference = const TCharType&; - - using const_reverse_iterator = std::reverse_iterator; - - static constexpr size_t StrLen(const TCharType* s) noexcept { - if (Y_LIKELY(s)) { - return TTraits::length(s); - } - return 0; - } - - template - inline constexpr operator std::basic_string_view() const { - return std::basic_string_view(data(), size()); - } - - template - inline explicit operator std::basic_string() const { - return std::basic_string(Ptr(), Len()); - } - - /** - * @param Pointer to character inside the string, or nullptr. - * @return Offset from string beginning (in chars), or npos on nullptr. - */ - inline size_t off(const TCharType* ret) const noexcept { - return ret ? (size_t)(ret - Ptr()) : npos; - } - - inline size_t IterOff(const_iterator it) const noexcept { - return begin() <= it && end() > it ? size_t(it - begin()) : npos; - } - - constexpr const_iterator begin() const noexcept { - return Ptr(); - } - - constexpr const_iterator end() const noexcept { - return Ptr() + size(); - } - - constexpr const_iterator cbegin() const noexcept { - return begin(); - } - - constexpr const_iterator cend() const noexcept { - return end(); - } - - constexpr const_reverse_iterator rbegin() const noexcept { - return const_reverse_iterator(Ptr() + size()); - } - - constexpr const_reverse_iterator rend() const noexcept { - return const_reverse_iterator(Ptr()); - } - - constexpr const_reverse_iterator crbegin() const noexcept { - return rbegin(); - } - - constexpr const_reverse_iterator crend() const noexcept { - return rend(); - } - - inline TCharType back() const noexcept { - Y_ASSERT(!this->empty()); - return Ptr()[Len() - 1]; - } - - inline TCharType front() const noexcept { - Y_ASSERT(!empty()); - return Ptr()[0]; - } - - constexpr const TCharType* data() const noexcept { - return Ptr(); - } - - constexpr inline size_t size() const noexcept { - return Len(); - } - - constexpr inline bool is_null() const noexcept { - return *Ptr() == 0; - } - - Y_PURE_FUNCTION constexpr inline bool empty() const noexcept { - return Len() == 0; - } - - constexpr inline explicit operator bool() const noexcept { - return !empty(); - } - -public: // style-guide compliant methods - constexpr const TCharType* Data() const noexcept { - return Ptr(); - } - - constexpr size_t Size() const noexcept { - return Len(); - } - - Y_PURE_FUNCTION constexpr bool Empty() const noexcept { - return 0 == Len(); - } - -private: - static constexpr TStringView LegacySubString(const TStringView view, size_t p, size_t n) noexcept { - p = Min(p, view.length()); - return view.substr(p, n); - } - -public: - // ~~~ Comparison ~~~ : FAMILY0(int, compare) - static constexpr int compare(const TSelf& s1, const TSelf& s2) noexcept { - return s1.AsStringView().compare(s2.AsStringView()); - } - - static constexpr int compare(const TCharType* p, const TSelf& s2) noexcept { - TCharType null{0}; - return TStringViewWithTraits(p ? p : &null).compare(s2.AsStringView()); - } - - static constexpr int compare(const TSelf& s1, const TCharType* p) noexcept { - TCharType null{0}; - return s1.AsStringView().compare(p ? p : &null); - } - - static constexpr int compare(const TStringView s1, const TStringView s2) noexcept { - return TStringViewWithTraits(s1.data(), s1.size()).compare(TStringViewWithTraits(s2.data(), s2.size())); - } - - template - constexpr int compare(const T& t) const noexcept { - return compare(*this, t); - } - - constexpr int compare(size_t p, size_t n, const TStringView t) const noexcept { - return compare(LegacySubString(*this, p, n), t); - } - - constexpr int compare(size_t p, size_t n, const TStringView t, size_t p1, size_t n1) const noexcept { - return compare(LegacySubString(*this, p, n), LegacySubString(t, p1, n1)); - } - - constexpr int compare(size_t p, size_t n, const TStringView t, size_t n1) const noexcept { - return compare(LegacySubString(*this, p, n), LegacySubString(t, 0, n1)); - } - - constexpr int compare(const TCharType* p, size_t len) const noexcept { - return compare(*this, TStringView(p, len)); - } - - static constexpr bool equal(const TSelf& s1, const TSelf& s2) noexcept { - return s1.AsStringView() == s2.AsStringView(); - } - - static constexpr bool equal(const TSelf& s1, const TCharType* p) noexcept { - if (p == nullptr) { - return s1.Len() == 0; - } - - return s1.AsStringView() == p; - } - - static constexpr bool equal(const TCharType* p, const TSelf& s2) noexcept { - return equal(s2, p); - } - - static constexpr bool equal(const TStringView s1, const TStringView s2) noexcept { - return TStringViewWithTraits{s1.data(), s1.size()} == TStringViewWithTraits{s2.data(), s2.size()}; - } - - template - constexpr bool equal(const T& t) const noexcept { - return equal(*this, t); - } - - constexpr bool equal(size_t p, size_t n, const TStringView t) const noexcept { - return equal(LegacySubString(*this, p, n), t); - } - - constexpr bool equal(size_t p, size_t n, const TStringView t, size_t p1, size_t n1) const noexcept { - return equal(LegacySubString(*this, p, n), LegacySubString(t, p1, n1)); - } - - constexpr bool equal(size_t p, size_t n, const TStringView t, size_t n1) const noexcept { - return equal(LegacySubString(*this, p, n), LegacySubString(t, 0, n1)); - } - - static constexpr bool StartsWith(const TCharType* what, size_t whatLen, const TCharType* with, size_t withLen) noexcept { - return withLen <= whatLen && TStringViewWithTraits(what, withLen) == TStringViewWithTraits(with, withLen); - } - - static constexpr bool EndsWith(const TCharType* what, size_t whatLen, const TCharType* with, size_t withLen) noexcept { - return withLen <= whatLen && TStringViewWithTraits(what + whatLen - withLen, withLen) == TStringViewWithTraits(with, withLen); - } - - constexpr bool StartsWith(const TCharType* s, size_t n) const noexcept { - return StartsWith(Ptr(), Len(), s, n); - } - - constexpr bool StartsWith(const TStringView s) const noexcept { - return StartsWith(s.data(), s.length()); - } - - constexpr bool StartsWith(TCharType ch) const noexcept { - return !empty() && TTraits::eq(*Ptr(), ch); - } - - constexpr bool EndsWith(const TCharType* s, size_t n) const noexcept { - return EndsWith(Ptr(), Len(), s, n); - } - - constexpr bool EndsWith(const TStringView s) const noexcept { - return EndsWith(s.data(), s.length()); - } - - constexpr bool EndsWith(TCharType ch) const noexcept { - return !empty() && TTraits::eq(Ptr()[Len() - 1], ch); - } - - template - constexpr bool operator==(const TStringBase& s2) const noexcept { - return equal(*this, s2); - } - - constexpr bool operator==(TStringView s2) const noexcept { - return equal(*this, s2); - } - - constexpr bool operator==(const TCharType* pc) const noexcept { - return equal(*this, pc); - } - -#ifndef __cpp_impl_three_way_comparison - friend constexpr bool operator==(const TCharType* pc, const TSelf& s) noexcept { - return equal(pc, s); - } - - template - friend constexpr bool operator!=(const TSelf& s1, const TStringBase& s2) noexcept { - return !(s1 == s2); - } - - friend constexpr bool operator!=(const TSelf& s1, TStringView s2) noexcept { - return !(s1 == s2); - } - - friend constexpr bool operator!=(const TSelf& s, const TCharType* pc) noexcept { - return !(s == pc); - } - - friend constexpr bool operator!=(const TCharType* pc, const TSelf& s) noexcept { - return !(pc == s); - } -#endif - - template - friend constexpr bool operator<(const TSelf& s1, const TStringBase& s2) noexcept { - return compare(s1, s2) < 0; - } - - friend constexpr bool operator<(const TSelf& s1, TStringView s2) noexcept { - return compare(s1, s2) < 0; - } - - friend constexpr bool operator<(const TSelf& s, const TCharType* pc) noexcept { - return compare(s, pc) < 0; - } - - friend constexpr bool operator<(const TCharType* pc, const TSelf& s) noexcept { - return compare(pc, s) < 0; - } - - template - friend constexpr bool operator<=(const TSelf& s1, const TStringBase& s2) noexcept { - return compare(s1, s2) <= 0; - } - - friend constexpr bool operator<=(const TSelf& s1, TStringView s2) noexcept { - return compare(s1, s2) <= 0; - } - - friend constexpr bool operator<=(const TSelf& s, const TCharType* pc) noexcept { - return compare(s, pc) <= 0; - } - - friend constexpr bool operator<=(const TCharType* pc, const TSelf& s) noexcept { - return compare(pc, s) <= 0; - } - - template - friend constexpr bool operator>(const TSelf& s1, const TStringBase& s2) noexcept { - return compare(s1, s2) > 0; - } - - friend constexpr bool operator>(const TSelf& s1, TStringView s2) noexcept { - return compare(s1, s2) > 0; - } - - friend constexpr bool operator>(const TSelf& s, const TCharType* pc) noexcept { - return compare(s, pc) > 0; - } - - friend constexpr bool operator>(const TCharType* pc, const TSelf& s) noexcept { - return compare(pc, s) > 0; - } - - template - friend constexpr bool operator>=(const TSelf& s1, const TStringBase& s2) noexcept { - return compare(s1, s2) >= 0; - } - - friend constexpr bool operator>=(const TSelf& s1, TStringView s2) noexcept { - return compare(s1, s2) >= 0; - } - - friend constexpr bool operator>=(const TSelf& s, const TCharType* pc) noexcept { - return compare(s, pc) >= 0; - } - - friend constexpr bool operator>=(const TCharType* pc, const TSelf& s) noexcept { - return compare(pc, s) >= 0; - } - - // ~~ Read access ~~ - inline TCharType at(size_t pos) const noexcept { - if (Y_LIKELY(pos < Len())) { - return (Ptr())[pos]; - } - return 0; - } - - inline TCharType operator[](size_t pos) const noexcept { - Y_ASSERT(pos < this->size()); - - return Ptr()[pos]; - } - - //~~~~Search~~~~ - /** - * @return Position of the substring inside this string, or `npos` if not found. - */ - inline size_t find(const TStringView s, size_t pos = 0) const noexcept { - return find(s.data(), pos, s.size()); - } - - inline size_t find(const TCharType* s, size_t pos, size_t count) const noexcept { - return AsStringView().find(s, pos, count); - } - - inline size_t find(TCharType c, size_t pos = 0) const noexcept { - return AsStringView().find(c, pos); - } - - inline size_t rfind(TCharType c) const noexcept { - return AsStringView().rfind(c); - } - - inline size_t rfind(TCharType c, size_t pos) const noexcept { - if (pos == 0) { - return npos; - } - return AsStringView().rfind(c, pos - 1); - } - - inline size_t rfind(const TStringView str, size_t pos = npos) const { - return AsStringView().rfind(str.data(), pos, str.size()); - } - - //~~~~Contains~~~~ - /** - * @returns Whether this string contains the provided substring. - */ - inline bool Contains(const TStringView s, size_t pos = 0) const noexcept { - return !s.length() || find(s, pos) != npos; - } - - inline bool Contains(TChar c, size_t pos = 0) const noexcept { - return find(c, pos) != npos; - } - - inline void Contains(std::enable_if::value, char> c, size_t pos = 0) const noexcept { - return find(ui8(c), pos) != npos; - } - - //~~~~Character Set Search~~~ - inline size_t find_first_of(TCharType c) const noexcept { - return find_first_of(c, 0); - } - - inline size_t find_first_of(TCharType c, size_t pos) const noexcept { - return find(c, pos); - } - - inline size_t find_first_of(const TStringView set) const noexcept { - return find_first_of(set, 0); - } - - inline size_t find_first_of(const TStringView set, size_t pos) const noexcept { - return AsStringView().find_first_of(set.data(), pos, set.size()); - } - - inline size_t find_first_not_of(TCharType c) const noexcept { - return find_first_not_of(c, 0); - } - - inline size_t find_first_not_of(TCharType c, size_t pos) const noexcept { - return find_first_not_of(TStringView(&c, 1), pos); - } - - inline size_t find_first_not_of(const TStringView set) const noexcept { - return find_first_not_of(set, 0); - } - - inline size_t find_first_not_of(const TStringView set, size_t pos) const noexcept { - return AsStringView().find_first_not_of(set.data(), pos, set.size()); - } - - inline size_t find_last_of(TCharType c, size_t pos = npos) const noexcept { - return find_last_of(&c, pos, 1); - } - - inline size_t find_last_of(const TStringView set, size_t pos = npos) const noexcept { - return find_last_of(set.data(), pos, set.length()); - } - - inline size_t find_last_of(const TCharType* set, size_t pos, size_t n) const noexcept { - return AsStringView().find_last_of(set, pos, n); - } - - inline size_t find_last_not_of(TCharType c, size_t pos = npos) const noexcept { - return AsStringView().find_last_not_of(c, pos); - } - - inline size_t find_last_not_of(const TStringView set, size_t pos = npos) const noexcept { - return find_last_not_of(set.data(), pos, set.length()); - } - - inline size_t find_last_not_of(const TCharType* set, size_t pos, size_t n) const noexcept { - return AsStringView().find_last_not_of(set, pos, n); - } - - inline size_t copy(TCharType* pc, size_t n, size_t pos) const { - if (pos > Len()) { - throw std::out_of_range("TStringBase::copy"); - } - - return CopyImpl(pc, n, pos); - } - - inline size_t copy(TCharType* pc, size_t n) const noexcept { - return CopyImpl(pc, n, 0); - } - - inline size_t strcpy(TCharType* pc, size_t n) const noexcept { - if (n) { - n = copy(pc, n - 1); - pc[n] = 0; - } - - return n; - } - - inline TDerived copy() const Y_WARN_UNUSED_RESULT { - return TDerived(Ptr(), Len()); - } - - // ~~~ Partial copy ~~~~ - TDerived substr(size_t pos, size_t n = npos) const Y_WARN_UNUSED_RESULT { - return TDerived(*This(), pos, n); - } - -private: - using GenericFinder = const TCharType* (*)(const TCharType*, size_t, const TCharType*, size_t); - - constexpr TStringViewWithTraits AsStringView() const { - return static_cast(*this); - } - - constexpr inline const TCharType* Ptr() const noexcept { - return This()->data(); - } - - constexpr inline size_t Len() const noexcept { - return This()->length(); - } - - constexpr inline const TDerived* This() const noexcept { - return static_cast(this); - } - - inline size_t CopyImpl(TCharType* pc, size_t n, size_t pos) const noexcept { - const size_t toCopy = Min(Len() - pos, n); - - TTraits::copy(pc, Ptr() + pos, toCopy); - - return toCopy; - } -}; diff --git a/util/generic/strbuf.cpp b/util/generic/strbuf.cpp deleted file mode 100644 index 896dbf0651f..00000000000 --- a/util/generic/strbuf.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "strbuf.h" - -#include - -std::ostream& operator<<(std::ostream& os, TStringBuf buf) { - os.write(buf.data(), buf.size()); - return os; -} diff --git a/util/generic/strbuf.h b/util/generic/strbuf.h deleted file mode 100644 index 7b066292997..00000000000 --- a/util/generic/strbuf.h +++ /dev/null @@ -1,543 +0,0 @@ -#pragma once - -#include "fwd.h" -#include "strbase.h" -#include "utility.h" -#include "typetraits.h" - -#include - -using namespace std::string_view_literals; - -template -class TBasicStringBuf: public std::basic_string_view, - public TStringBase, TCharType, TTraits> { -private: - using TdSelf = TBasicStringBuf; - using TBase = TStringBase; - using TStringView = std::basic_string_view; - -public: - using char_type = TCharType; // TODO: DROP - using traits_type = TTraits; - - //Resolving some ambiguity between TStringBase and std::basic_string_view - //for typenames - using typename TStringView::const_iterator; - using typename TStringView::const_reference; - using typename TStringView::const_reverse_iterator; - using typename TStringView::iterator; - using typename TStringView::reference; - using typename TStringView::reverse_iterator; - using typename TStringView::size_type; - using typename TStringView::value_type; - - //for constants - using TStringView::npos; - - //for methods and operators - using TStringView::begin; - using TStringView::cbegin; - using TStringView::cend; - using TStringView::crbegin; - using TStringView::crend; - using TStringView::end; - using TStringView::rbegin; - using TStringView::rend; - - using TStringView::data; - using TStringView::empty; - using TStringView::size; - - using TStringView::operator[]; - - /* - * WARN: - * TBase::at silently return 0 in case of range error, - * while std::string_view throws std::out_of_range. - */ - using TBase::at; - using TStringView::back; - using TStringView::front; - - using TStringView::find; - /* - * WARN: - * TBase::*find* methods take into account TCharTraits, - * while TTStringView::*find* would use default std::char_traits. - */ - using TBase::find_first_not_of; - using TBase::find_first_of; - using TBase::find_last_not_of; - using TBase::find_last_of; - using TBase::rfind; - - using TStringView::copy; - /* - * WARN: - * TBase::compare takes into account TCharTraits, - * thus making it possible to implement case-insensitive string buffers, - * if it is using TStringBase::compare - */ - using TBase::compare; - - /* - * WARN: - * TBase::substr properly checks boundary cases and clamps them with maximum valid values, - * while TStringView::substr throws std::out_of_range error. - */ - using TBase::substr; - - /* - * WARN: - * Constructing std::string_view(nullptr, non_zero_size) ctor - * results in undefined behavior according to the standard. - * In libc++ this UB results in runtime assertion, though it is better - * to generate compilation error instead. - */ - constexpr inline TBasicStringBuf(std::nullptr_t begin, size_t size) = delete; - - constexpr inline TBasicStringBuf(const TCharType* data, size_t size) noexcept - : TStringView(data, size) - { - } - - constexpr TBasicStringBuf(const TCharType* data) noexcept - /* - * WARN: TBase::StrLen properly handles nullptr, - * while std::string_view (using std::char_traits) will abort in such case - */ - : TStringView(data, TBase::StrLen(data)) - { - } - - constexpr inline TBasicStringBuf(const TCharType* beg, const TCharType* end) noexcept - : TStringView(beg, end - beg) - { - } - - template - inline TBasicStringBuf(const TStringBase& str) noexcept - : TStringView(str.data(), str.size()) - { - } - - template - inline TBasicStringBuf(const std::basic_string& str) noexcept - : TStringView(str) - { - } - - template - constexpr TBasicStringBuf(std::basic_string_view view) noexcept - : TStringView(view) - { - } - - constexpr inline TBasicStringBuf() noexcept { - /* - * WARN: - * This ctor can not be defaulted due to the following feature of default initialization: - * If T is a const-qualified type, it must be a class type with a user-provided default constructor. - * (see https://en.cppreference.com/w/cpp/language/default_initialization). - * - * This means, that a class with default ctor can not be a constant member of another class with default ctor. - */ - } - - inline TBasicStringBuf(const TBasicStringBuf& src, size_t pos, size_t n) noexcept - : TBasicStringBuf(src) - { - Skip(pos).Trunc(n); - } - - inline TBasicStringBuf(const TBasicStringBuf& src, size_t pos) noexcept - : TBasicStringBuf(src, pos, TBase::npos) - { - } - - Y_PURE_FUNCTION inline TBasicStringBuf SubString(size_t pos, size_t n) const noexcept { - pos = Min(pos, size()); - n = Min(n, size() - pos); - return TBasicStringBuf(data() + pos, n); - } - -public: - void Clear() { - *this = TdSelf(); - } - - constexpr bool IsInited() const noexcept { - return data() != nullptr; - } - -public: - /** - * Tries to split string in two parts using given delimiter character. - * Searches for the delimiter, scanning string from the beginning. - * The delimiter is excluded from the result. Both out parameters are - * left unmodified if there was no delimiter character in string. - * - * @param[in] delim Delimiter character. - * @param[out] l The first part of split result. - * @param[out] r The second part of split result. - * @returns Whether the split was actually performed. - */ - inline bool TrySplit(TCharType delim, TdSelf& l, TdSelf& r) const noexcept { - return TrySplitOn(TBase::find(delim), l, r); - } - - /** - * Tries to split string in two parts using given delimiter character. - * Searches for the delimiter, scanning string from the end. - * The delimiter is excluded from the result. Both out parameters are - * left unmodified if there was no delimiter character in string. - * - * @param[in] delim Delimiter character. - * @param[out] l The first part of split result. - * @param[out] r The second part of split result. - * @returns Whether the split was actually performed. - */ - inline bool TryRSplit(TCharType delim, TdSelf& l, TdSelf& r) const noexcept { - return TrySplitOn(TBase::rfind(delim), l, r); - } - - /** - * Tries to split string in two parts using given delimiter sequence. - * Searches for the delimiter, scanning string from the beginning. - * The delimiter sequence is excluded from the result. Both out parameters - * are left unmodified if there was no delimiter character in string. - * - * @param[in] delim Delimiter sequence. - * @param[out] l The first part of split result. - * @param[out] r The second part of split result. - * @returns Whether the split was actually performed. - */ - inline bool TrySplit(TdSelf delim, TdSelf& l, TdSelf& r) const noexcept { - return TrySplitOn(TBase::find(delim), l, r, delim.size()); - } - - /** - * Tries to split string in two parts using given delimiter sequence. - * Searches for the delimiter, scanning string from the end. - * The delimiter sequence is excluded from the result. Both out parameters - * are left unmodified if there was no delimiter character in string. - * - * @param[in] delim Delimiter sequence. - * @param[out] l The first part of split result. - * @param[out] r The second part of split result. - * @returns Whether the split was actually performed. - */ - inline bool TryRSplit(TdSelf delim, TdSelf& l, TdSelf& r) const noexcept { - return TrySplitOn(TBase::rfind(delim), l, r, delim.size()); - } - - inline void Split(TCharType delim, TdSelf& l, TdSelf& r) const noexcept { - SplitTemplate(delim, l, r); - } - - inline void RSplit(TCharType delim, TdSelf& l, TdSelf& r) const noexcept { - RSplitTemplate(delim, l, r); - } - - inline void Split(TdSelf delim, TdSelf& l, TdSelf& r) const noexcept { - SplitTemplate(delim, l, r); - } - - inline void RSplit(TdSelf delim, TdSelf& l, TdSelf& r) const noexcept { - RSplitTemplate(delim, l, r); - } - -private: - // splits on a delimiter at a given position; delimiter is excluded - void DoSplitOn(size_t pos, TdSelf& l, TdSelf& r, size_t len) const noexcept { - Y_ASSERT(pos != TBase::npos); - - // make a copy in case one of l/r is really *this - const TdSelf tok = SubStr(pos + len); - l = Head(pos); - r = tok; - } - -public: - // In all methods below with @pos parameter, @pos is supposed to be - // a result of string find()/rfind()/find_first() or other similiar functions, - // returning either position within string length [0..size()) or npos. - // For all other @pos values (out of string index range) the behaviour isn't well defined - // For example, for TStringBuf s("abc"): - // s.TrySplitOn(s.find('z'), ...) is false, but s.TrySplitOn(100500, ...) is true. - - bool TrySplitOn(size_t pos, TdSelf& l, TdSelf& r, size_t len = 1) const noexcept { - if (TBase::npos == pos) - return false; - - DoSplitOn(pos, l, r, len); - return true; - } - - void SplitOn(size_t pos, TdSelf& l, TdSelf& r, size_t len = 1) const noexcept { - if (!TrySplitOn(pos, l, r, len)) { - l = *this; - r = TdSelf(); - } - } - - bool TrySplitAt(size_t pos, TdSelf& l, TdSelf& r) const noexcept { - return TrySplitOn(pos, l, r, 0); - } - - void SplitAt(size_t pos, TdSelf& l, TdSelf& r) const noexcept { - SplitOn(pos, l, r, 0); - } - - /* - // Not implemented intentionally, use TrySplitOn() instead - void RSplitOn(size_t pos, TdSelf& l, TdSelf& r) const noexcept; - void RSplitAt(size_t pos, TdSelf& l, TdSelf& r) const noexcept; -*/ - -public: - Y_PURE_FUNCTION inline TdSelf After(TCharType c) const noexcept { - TdSelf l, r; - return TrySplit(c, l, r) ? r : *this; - } - - Y_PURE_FUNCTION inline TdSelf Before(TCharType c) const noexcept { - TdSelf l, r; - return TrySplit(c, l, r) ? l : *this; - } - - Y_PURE_FUNCTION inline TdSelf RAfter(TCharType c) const noexcept { - TdSelf l, r; - return TryRSplit(c, l, r) ? r : *this; - } - - Y_PURE_FUNCTION inline TdSelf RBefore(TCharType c) const noexcept { - TdSelf l, r; - return TryRSplit(c, l, r) ? l : *this; - } - -public: - inline bool AfterPrefix(const TdSelf& prefix, TdSelf& result) const noexcept { - if (this->StartsWith(prefix)) { - result = Tail(prefix.size()); - return true; - } - return false; - } - - inline bool BeforeSuffix(const TdSelf& suffix, TdSelf& result) const noexcept { - if (this->EndsWith(suffix)) { - result = Head(size() - suffix.size()); - return true; - } - return false; - } - - // returns true if string started with `prefix`, false otherwise - inline bool SkipPrefix(const TdSelf& prefix) noexcept { - return AfterPrefix(prefix, *this); - } - - // returns true if string ended with `suffix`, false otherwise - inline bool ChopSuffix(const TdSelf& suffix) noexcept { - return BeforeSuffix(suffix, *this); - } - -public: - // returns tail, including pos - TdSelf SplitOffAt(size_t pos) { - const TdSelf tok = SubStr(pos); - Trunc(pos); - return tok; - } - - // returns head, tail includes pos - TdSelf NextTokAt(size_t pos) { - const TdSelf tok = Head(pos); - Skip(pos); - return tok; - } - - TdSelf SplitOffOn(size_t pos) { - TdSelf tok; - SplitOn(pos, *this, tok); - return tok; - } - - TdSelf NextTokOn(size_t pos) { - TdSelf tok; - SplitOn(pos, tok, *this); - return tok; - } - /* - // See comment on RSplitOn() above - TdSelf RSplitOffOn(size_t pos); - TdSelf RNextTokOn(size_t pos); -*/ - -public: - TdSelf SplitOff(TCharType delim) { - TdSelf tok; - Split(delim, *this, tok); - return tok; - } - - TdSelf RSplitOff(TCharType delim) { - TdSelf tok; - RSplit(delim, tok, *this); - return tok; - } - - bool NextTok(TCharType delim, TdSelf& tok) { - return NextTokTemplate(delim, tok); - } - - bool NextTok(TdSelf delim, TdSelf& tok) { - return NextTokTemplate(delim, tok); - } - - bool RNextTok(TCharType delim, TdSelf& tok) { - return RNextTokTemplate(delim, tok); - } - - bool RNextTok(TdSelf delim, TdSelf& tok) { - return RNextTokTemplate(delim, tok); - } - - bool ReadLine(TdSelf& tok) { - if (NextTok('\n', tok)) { - while (!tok.empty() && tok.back() == '\r') { - tok.remove_suffix(1); - } - - return true; - } - - return false; - } - - TdSelf NextTok(TCharType delim) { - return NextTokTemplate(delim); - } - - TdSelf RNextTok(TCharType delim) { - return RNextTokTemplate(delim); - } - - TdSelf NextTok(TdSelf delim) { - return NextTokTemplate(delim); - } - - TdSelf RNextTok(TdSelf delim) { - return RNextTokTemplate(delim); - } - -public: // string subsequences - /// Cut last @c shift characters (or less if length is less than @c shift) - inline TdSelf& Chop(size_t shift) noexcept { - this->remove_suffix(std::min(shift, size())); - return *this; - } - - /// Cut first @c shift characters (or less if length is less than @c shift) - inline TdSelf& Skip(size_t shift) noexcept { - this->remove_prefix(std::min(shift, size())); - return *this; - } - - /// Sets the start pointer to a position relative to the end - inline TdSelf& RSeek(size_t tailSize) noexcept { - if (size() > tailSize) { - //WARN: removing TStringView:: will lead to an infinite recursion - *this = TStringView::substr(size() - tailSize, tailSize); - } - - return *this; - } - - // coverity[exn_spec_violation] - inline TdSelf& Trunc(size_t targetSize) noexcept { - // Coverity false positive issue - // exn_spec_violation: An exception of type "std::out_of_range" is thrown but the exception specification "noexcept" doesn't allow it to be thrown. This will result in a call to terminate(). - // fun_call_w_exception: Called function TStringView::substr throws an exception of type "std::out_of_range". - // Suppress this issue because we pass argument pos=0 and string_view can't throw std::out_of_range. - *this = TStringView::substr(0, targetSize); //WARN: removing TStringView:: will lead to an infinite recursion - return *this; - } - - Y_PURE_FUNCTION inline TdSelf SubStr(size_t beg) const noexcept { - return TdSelf(*this).Skip(beg); - } - - Y_PURE_FUNCTION inline TdSelf SubStr(size_t beg, size_t len) const noexcept { - return SubStr(beg).Trunc(len); - } - - Y_PURE_FUNCTION inline TdSelf Head(size_t pos) const noexcept { - return TdSelf(*this).Trunc(pos); - } - - Y_PURE_FUNCTION inline TdSelf Tail(size_t pos) const noexcept { - return SubStr(pos); - } - - Y_PURE_FUNCTION inline TdSelf Last(size_t len) const noexcept { - return TdSelf(*this).RSeek(len); - } - -private: - template - TdSelf NextTokTemplate(TDelimiterType delim) { - TdSelf tok; - Split(delim, tok, *this); - return tok; - } - - template - TdSelf RNextTokTemplate(TDelimiterType delim) { - TdSelf tok; - RSplit(delim, *this, tok); - return tok; - } - - template - bool NextTokTemplate(TDelimiterType delim, TdSelf& tok) { - if (!empty()) { - tok = NextTokTemplate(delim); - return true; - } - return false; - } - - template - bool RNextTokTemplate(TDelimiterType delim, TdSelf& tok) { - if (!empty()) { - tok = RNextTokTemplate(delim); - return true; - } - return false; - } - - template - inline void SplitTemplate(TDelimiterType delim, TdSelf& l, TdSelf& r) const noexcept { - if (!TrySplit(delim, l, r)) { - l = *this; - r = TdSelf(); - } - } - - template - inline void RSplitTemplate(TDelimiterType delim, TdSelf& l, TdSelf& r) const noexcept { - if (!TryRSplit(delim, l, r)) { - r = *this; - l = TdSelf(); - } - } -}; - -std::ostream& operator<<(std::ostream& os, TStringBuf buf); - -constexpr TStringBuf operator""_sb(const char* str, size_t len) { - return TStringBuf{str, len}; -} diff --git a/util/generic/strbuf_ut.cpp b/util/generic/strbuf_ut.cpp deleted file mode 100644 index 245b93bc98d..00000000000 --- a/util/generic/strbuf_ut.cpp +++ /dev/null @@ -1,391 +0,0 @@ -#include "strbuf.h" - -#include - -#include - -Y_UNIT_TEST_SUITE(TStrBufTest) { - Y_UNIT_TEST(TestConstructorsAndOperators) { - TStringBuf str("qwerty"); - - UNIT_ASSERT_EQUAL(*str.data(), 'q'); - UNIT_ASSERT_EQUAL(str.size(), 6); - - TStringBuf str1("qwe\0rty"sv); - TStringBuf str2(str1.data()); - UNIT_ASSERT_VALUES_UNEQUAL(str1, str2); - UNIT_ASSERT_VALUES_EQUAL(str1.size(), 7); - UNIT_ASSERT_VALUES_EQUAL(str2.size(), 3); - - std::string_view helloWorld("Hello, World!"); - TStringBuf fromStringView(helloWorld); - UNIT_ASSERT_EQUAL(fromStringView.data(), helloWorld.data()); - UNIT_ASSERT_EQUAL(fromStringView.size(), helloWorld.size()); - - std::string_view fromStringBuf = fromStringView; - UNIT_ASSERT_EQUAL(helloWorld.data(), fromStringBuf.data()); - UNIT_ASSERT_EQUAL(helloWorld.size(), fromStringBuf.size()); - } - - Y_UNIT_TEST(TestConstExpr) { - static constexpr TStringBuf str1("qwe\0rty", 7); - static constexpr TStringBuf str2(str1.data(), str1.size()); - static constexpr TStringBuf str3 = "qwe\0rty"sv; - - UNIT_ASSERT_VALUES_EQUAL(str1.size(), 7); - - UNIT_ASSERT_VALUES_EQUAL(str1, str2); - UNIT_ASSERT_VALUES_EQUAL(str2, str3); - UNIT_ASSERT_VALUES_EQUAL(str1, str3); - - static constexpr std::string_view view1(str1); - UNIT_ASSERT_VALUES_EQUAL(str1, view1); - static_assert(str1.data() == view1.data()); - static_assert(str1.size() == view1.size()); - - static constexpr TStringBuf str4(view1); - UNIT_ASSERT_VALUES_EQUAL(str1, str4); - static_assert(str1.data() == str4.data()); - static_assert(str1.size() == str4.size()); - } - - Y_UNIT_TEST(TestConstExprComparison) { - static constexpr TStringBuf str1("qwe\0rty"sv); - static constexpr TStringBuf str2("qw"); - - static_assert(str1 != str2); - static_assert(str1 >= str2); - static_assert(str1.StartsWith(str2)); - static_assert(!str1.EndsWith(str2)); - } - - Y_UNIT_TEST(TestAfter) { - TStringBuf str("qwerty"); - - UNIT_ASSERT_VALUES_EQUAL(str.After('w'), TStringBuf("erty")); - UNIT_ASSERT_VALUES_EQUAL(str.After('x'), TStringBuf("qwerty")); - UNIT_ASSERT_VALUES_EQUAL(str.After('y'), TStringBuf()); - UNIT_ASSERT_STRINGS_EQUAL(str.After('='), str); - - // Also works properly on empty strings - TStringBuf empty; - UNIT_ASSERT_STRINGS_EQUAL(empty.After('x'), empty); - } - - Y_UNIT_TEST(TestBefore) { - TStringBuf str("qwerty"); - - UNIT_ASSERT_VALUES_EQUAL(str.Before('w'), TStringBuf("q")); - UNIT_ASSERT_VALUES_EQUAL(str.Before('x'), TStringBuf("qwerty")); - UNIT_ASSERT_VALUES_EQUAL(str.Before('y'), TStringBuf("qwert")); - UNIT_ASSERT_VALUES_EQUAL(str.Before('q'), TStringBuf()); - } - - Y_UNIT_TEST(TestRAfterBefore) { - TStringBuf str("a/b/c"); - UNIT_ASSERT_STRINGS_EQUAL(str.RAfter('/'), "c"); - UNIT_ASSERT_STRINGS_EQUAL(str.RAfter('_'), str); - UNIT_ASSERT_STRINGS_EQUAL(str.RAfter('a'), "/b/c"); - UNIT_ASSERT_STRINGS_EQUAL(str.RBefore('/'), "a/b"); - UNIT_ASSERT_STRINGS_EQUAL(str.RBefore('_'), str); - UNIT_ASSERT_STRINGS_EQUAL(str.RBefore('a'), ""); - } - - Y_UNIT_TEST(TestAfterPrefix) { - TStringBuf str("cat_dog"); - - TStringBuf r = "the_same"; - UNIT_ASSERT(!str.AfterPrefix("dog", r)); - UNIT_ASSERT_EQUAL(r, "the_same"); - UNIT_ASSERT(str.AfterPrefix("cat_", r)); - UNIT_ASSERT_EQUAL(r, "dog"); - - //example: - str = "http://ya.ru"; - if (str.AfterPrefix("http://", r)) { - UNIT_ASSERT_EQUAL(r, "ya.ru"); - } - - // SkipPrefix() - TStringBuf a = "abcdef"; - UNIT_ASSERT(a.SkipPrefix("a") && a == "bcdef"); - UNIT_ASSERT(a.SkipPrefix("bc") && a == "def"); - UNIT_ASSERT(a.SkipPrefix("") && a == "def"); - UNIT_ASSERT(!a.SkipPrefix("xyz") && a == "def"); - UNIT_ASSERT(!a.SkipPrefix("defg") && a == "def"); - UNIT_ASSERT(a.SkipPrefix("def") && a == ""); - UNIT_ASSERT(a.SkipPrefix("") && a == ""); - UNIT_ASSERT(!a.SkipPrefix("def") && a == ""); - } - - Y_UNIT_TEST(TestBeforeSuffix) { - TStringBuf str("cat_dog"); - - TStringBuf r = "the_same"; - UNIT_ASSERT(!str.BeforeSuffix("cat", r)); - UNIT_ASSERT_EQUAL(r, "the_same"); - UNIT_ASSERT(str.BeforeSuffix("_dog", r)); - UNIT_ASSERT_EQUAL(r, "cat"); - - //example: - str = "maps.yandex.com.ua"; - if (str.BeforeSuffix(".ru", r)) { - UNIT_ASSERT_EQUAL(r, "maps.yandex"); - } - - // ChopSuffix() - TStringBuf a = "abcdef"; - UNIT_ASSERT(a.ChopSuffix("f") && a == "abcde"); - UNIT_ASSERT(a.ChopSuffix("de") && a == "abc"); - UNIT_ASSERT(a.ChopSuffix("") && a == "abc"); - UNIT_ASSERT(!a.ChopSuffix("xyz") && a == "abc"); - UNIT_ASSERT(!a.ChopSuffix("abcd") && a == "abc"); - UNIT_ASSERT(a.ChopSuffix("abc") && a == ""); - UNIT_ASSERT(a.ChopSuffix("") && a == ""); - UNIT_ASSERT(!a.ChopSuffix("abc") && a == ""); - } - - Y_UNIT_TEST(TestEmpty) { - UNIT_ASSERT(TStringBuf().empty()); - UNIT_ASSERT(!TStringBuf("q").empty()); - } - - Y_UNIT_TEST(TestShift) { - TStringBuf qw("qwerty"); - TStringBuf str; - - str = qw; - str.Chop(10); - UNIT_ASSERT(str.empty()); - - str = qw; - UNIT_ASSERT_EQUAL(str.SubStr(2), TStringBuf("erty")); - UNIT_ASSERT_EQUAL(str.Skip(3), qw.SubStr(3)); - str.Chop(1); - UNIT_ASSERT_EQUAL(str, TStringBuf("rt")); - } - - Y_UNIT_TEST(TestSplit) { - TStringBuf qw("qwerty"); - TStringBuf lt, rt; - - rt = qw; - lt = rt.NextTok('r'); - UNIT_ASSERT_EQUAL(lt, TStringBuf("qwe")); - UNIT_ASSERT_EQUAL(rt, TStringBuf("ty")); - - lt = qw; - rt = lt.SplitOff('r'); - UNIT_ASSERT_EQUAL(lt, TStringBuf("qwe")); - UNIT_ASSERT_EQUAL(rt, TStringBuf("ty")); - - rt = qw; - lt = rt.NextTok('r'); - TStringBuf ty = rt.NextTok('r'); // no 'r' in "ty" - UNIT_ASSERT_EQUAL(rt.size(), 0); - UNIT_ASSERT_EQUAL(ty, TStringBuf("ty")); - } - - Y_UNIT_TEST(TestNextTok) { - TStringBuf buf("12q45q"); - TStringBuf tok; - - UNIT_ASSERT(buf.NextTok('q', tok) && tok == "12"); - UNIT_ASSERT(buf.NextTok('q', tok) && tok == "45"); - UNIT_ASSERT(!buf.NextTok('q', tok)); - } - - Y_UNIT_TEST(TestNextStringTok) { - TStringBuf buf1("a@@b@@c"); - UNIT_ASSERT_EQUAL(buf1.NextTok("@@"), TStringBuf("a")); - UNIT_ASSERT_EQUAL(buf1.NextTok("@@"), TStringBuf("b")); - UNIT_ASSERT_EQUAL(buf1.NextTok("@@"), TStringBuf("c")); - UNIT_ASSERT_EQUAL(buf1, TStringBuf()); - - TStringBuf buf2("a@@b@@c"); - UNIT_ASSERT_EQUAL(buf2.RNextTok("@@"), TStringBuf("c")); - UNIT_ASSERT_EQUAL(buf2.RNextTok("@@"), TStringBuf("b")); - UNIT_ASSERT_EQUAL(buf2.RNextTok("@@"), TStringBuf("a")); - UNIT_ASSERT_EQUAL(buf2, TStringBuf()); - - TStringBuf buf3("a@@b@@c"); - UNIT_ASSERT_EQUAL(buf3.RNextTok("@@@"), TStringBuf("a@@b@@c")); - UNIT_ASSERT_EQUAL(buf3, TStringBuf()); - } - - Y_UNIT_TEST(TestReadLine) { - TStringBuf buf("12\n45\r\n\r\n23"); - TStringBuf tok; - - buf.ReadLine(tok); - UNIT_ASSERT_VALUES_EQUAL(tok, "12"); - - buf.ReadLine(tok); - UNIT_ASSERT_VALUES_EQUAL(tok, "45"); - - buf.ReadLine(tok); - UNIT_ASSERT_VALUES_EQUAL(tok, ""); - - buf.ReadLine(tok); - UNIT_ASSERT_VALUES_EQUAL(tok, "23"); - - UNIT_ASSERT(!buf.ReadLine(tok)); - } - - Y_UNIT_TEST(TestRFind) { - TStringBuf buf1 = "123123456"; - UNIT_ASSERT_EQUAL(buf1.rfind('3'), 5); - UNIT_ASSERT_EQUAL(buf1.rfind('4'), 6); - UNIT_ASSERT_EQUAL(buf1.rfind('7'), TStringBuf::npos); - - TStringBuf buf2; - UNIT_ASSERT_EQUAL(buf2.rfind('3'), TStringBuf::npos); - - TStringBuf buf3 = TStringBuf("123123456", 6); - UNIT_ASSERT_EQUAL(buf3.rfind('3'), 5); - UNIT_ASSERT_EQUAL(buf3.rfind('4'), TStringBuf::npos); - UNIT_ASSERT_EQUAL(buf3.rfind('7'), TStringBuf::npos); - - TStringBuf buf4 = TStringBuf("123123456", 5); - UNIT_ASSERT_EQUAL(buf4.rfind('3'), 2); - } - - Y_UNIT_TEST(TestRNextTok) { - TStringBuf buf1("a.b.c"); - UNIT_ASSERT_EQUAL(buf1.RNextTok('.'), TStringBuf("c")); - UNIT_ASSERT_EQUAL(buf1, TStringBuf("a.b")); - - TStringBuf buf2("a"); - UNIT_ASSERT_EQUAL(buf2.RNextTok('.'), TStringBuf("a")); - UNIT_ASSERT_EQUAL(buf2, TStringBuf()); - - TStringBuf buf3("ab cd ef"), tok; - UNIT_ASSERT(buf3.RNextTok(' ', tok) && tok == "ef" && buf3 == "ab cd"); - UNIT_ASSERT(buf3.RNextTok(' ', tok) && tok == "cd" && buf3 == "ab"); - UNIT_ASSERT(buf3.RNextTok(' ', tok) && tok == "ab" && buf3 == ""); - UNIT_ASSERT(!buf3.RNextTok(' ', tok) && tok == "ab" && buf3 == ""); // not modified - } - - Y_UNIT_TEST(TestRSplitOff) { - TStringBuf buf1("a.b.c"); - UNIT_ASSERT_EQUAL(buf1.RSplitOff('.'), TStringBuf("a.b")); - UNIT_ASSERT_EQUAL(buf1, TStringBuf("c")); - - TStringBuf buf2("a"); - UNIT_ASSERT_EQUAL(buf2.RSplitOff('.'), TStringBuf()); - UNIT_ASSERT_EQUAL(buf2, TStringBuf("a")); - } - - Y_UNIT_TEST(TestCBeginCEnd) { - const char helloThere[] = "Hello there"; - TStringBuf s{helloThere}; - - size_t index = 0; - for (auto it = s.cbegin(); s.cend() != it; ++it, ++index) { - UNIT_ASSERT_VALUES_EQUAL(helloThere[index], *it); - } - } - - Y_UNIT_TEST(TestSplitOnAt) { - TStringBuf s = "abcabc"; - TStringBuf l, r; - - size_t pos = s.find('a'); - UNIT_ASSERT(s.TrySplitOn(pos, l, r)); - UNIT_ASSERT(l == "" && r == "bcabc"); - UNIT_ASSERT(s.TrySplitAt(pos, l, r)); - UNIT_ASSERT(l == "" && r == "abcabc"); - - pos = s.find("ca"); - UNIT_ASSERT(s.TrySplitOn(pos, l, r)); - UNIT_ASSERT(l == "ab" && r == "abc"); - UNIT_ASSERT(s.TrySplitOn(pos, l, r, 2)); - UNIT_ASSERT(l == "ab" && r == "bc"); - UNIT_ASSERT(s.TrySplitAt(pos, l, r)); - UNIT_ASSERT(l == "ab" && r == "cabc"); - - // out of range - pos = 100500; - UNIT_ASSERT(s.TrySplitOn(pos, l, r)); // still true - UNIT_ASSERT(l == "abcabc" && r == ""); - l = "111"; - r = "222"; - UNIT_ASSERT(s.TrySplitAt(pos, l, r)); // still true - UNIT_ASSERT(l == "abcabc" && r == ""); - - // npos - pos = s.find("missing"); - l = "111"; - r = "222"; - UNIT_ASSERT(!s.TrySplitOn(pos, l, r)); - UNIT_ASSERT(l == "111" && r == "222"); // not modified - s.SplitOn(pos, l, r); - UNIT_ASSERT(l == "abcabc" && r == ""); // modified - - l = "111"; - r = "222"; - UNIT_ASSERT(!s.TrySplitAt(pos, l, r)); - UNIT_ASSERT(l == "111" && r == "222"); // not modified - s.SplitAt(pos, l, r); - UNIT_ASSERT(l == "abcabc" && r == ""); // modified - } - - template - void PassByConstReference(const T& val) { - // In https://st.yandex-team.ru/IGNIETFERRO-294 was assumed that `const char[]` types are compile time strings - // and that CharTraits::Length may not be called for them. Unfortunately that is not true, `char[]` types - // are easily converted to `const char[]` if they are passed to a function accepting `const T&`. - UNIT_ASSERT(TStringBuf(val).size() == 5); - } - - Y_UNIT_TEST(TestPassingArraysByConstReference) { - char data[] = "Hello\0word"; - PassByConstReference(data); - } - - Y_UNIT_TEST(TestTruncate) { - TStringBuf s = "123"; - s.Trunc(5); - UNIT_ASSERT_STRINGS_EQUAL(s, "123"); - s.Trunc(3); - UNIT_ASSERT_STRINGS_EQUAL(s, "123"); - s.Trunc(1); - UNIT_ASSERT_STRINGS_EQUAL(s, "1"); - s.Trunc(0); - UNIT_ASSERT_STRINGS_EQUAL(s, ""); - s.Trunc(0); - UNIT_ASSERT_STRINGS_EQUAL(s, ""); - } - - Y_UNIT_TEST(TestLiterals) { - auto s = "123"_sb; - static_assert(std::is_same_v); - UNIT_ASSERT_VALUES_EQUAL(s, TStringBuf("123")); - } -} - -Y_UNIT_TEST_SUITE(TWtrBufTest) { - Y_UNIT_TEST(TestConstExpr) { - static constexpr TWtringBuf str1(u"qwe\0rty", 7); - static constexpr TWtringBuf str2(str1.data(), str1.size()); - static constexpr TWtringBuf str3 = u"qwe\0rty"sv; - - UNIT_ASSERT_VALUES_EQUAL(str1.size(), 7); - - UNIT_ASSERT_VALUES_EQUAL(str1, str2); - UNIT_ASSERT_VALUES_EQUAL(str2, str3); - UNIT_ASSERT_VALUES_EQUAL(str1, str3); - - static constexpr std::u16string_view view1(str1); - UNIT_ASSERT_VALUES_EQUAL(str1, view1); - static_assert(str1.data() == view1.data()); - static_assert(str1.size() == view1.size()); - - static constexpr TWtringBuf str4(view1); - UNIT_ASSERT_VALUES_EQUAL(str1, str4); - static_assert(str1.data() == str4.data()); - static_assert(str1.size() == str4.size()); - - static_assert(str1 == str2); - } -} diff --git a/util/generic/string.cpp b/util/generic/string.cpp deleted file mode 100644 index a2b303f2605..00000000000 --- a/util/generic/string.cpp +++ /dev/null @@ -1,155 +0,0 @@ -#include "string.h" - -#include -#include -#include -#include - -#include - -alignas(32) const char NULL_STRING_REPR[128] = {0}; - -std::ostream& operator<<(std::ostream& os, const TString& s) { - return os.write(s.data(), s.size()); -} - -std::istream& operator>>(std::istream& is, TString& s) { - return is >> s.MutRef(); -} - -template <> -bool TBasicString>::to_lower(size_t pos, size_t n) { - return Transform([](size_t, char c) { return AsciiToLower(c); }, pos, n); -} - -template <> -bool TBasicString>::to_upper(size_t pos, size_t n) { - return Transform([](size_t, char c) { return AsciiToUpper(c); }, pos, n); -} - -template <> -bool TBasicString>::to_title(size_t pos, size_t n) { - if (n == 0) { - return false; - } - bool changed = to_upper(pos, 1); - return to_lower(pos + 1, n - 1) || changed; -} - -template <> -TUtf16String& -TBasicString>::AppendAscii(const ::TStringBuf& s) { - ReserveAndResize(size() + s.size()); - - auto dst = begin() + size() - s.size(); - - for (const char* src = s.data(); dst != end(); ++dst, ++src) { - *dst = static_cast(*src); - } - - return *this; -} - -template <> -TUtf16String& -TBasicString>::AppendUtf8(const ::TStringBuf& s) { - size_t oldSize = size(); - ReserveAndResize(size() + s.size() * 4); - size_t written = 0; - size_t pos = UTF8ToWideImpl(s.data(), s.size(), begin() + oldSize, written); - if (pos != s.size()) { - ythrow yexception() << "failed to decode UTF-8 string at pos " << pos << ::NDetail::InStringMsg(s.data(), s.size()); - } - resize(oldSize + written); - - return *this; -} - -template <> -bool TBasicString>::to_lower(size_t pos, size_t n) { - return ToLower(*this, pos, n); -} - -template <> -bool TBasicString>::to_upper(size_t pos, size_t n) { - return ToUpper(*this, pos, n); -} - -template <> -bool TBasicString>::to_title(size_t pos, size_t n) { - return ToTitle(*this, pos, n); -} - -template <> -TUtf32String& -TBasicString>::AppendAscii(const ::TStringBuf& s) { - ReserveAndResize(size() + s.size()); - - auto dst = begin() + size() - s.size(); - - for (const char* src = s.data(); dst != end(); ++dst, ++src) { - *dst = static_cast(*src); - } - - return *this; -} - -template <> -TBasicString>& -TBasicString>::AppendUtf16(const ::TWtringBuf& s) { - const size_t oldSize = size(); - ReserveAndResize(size() + WideToUTF8BufferSize(s.size())); - - size_t written = 0; - WideToUTF8(s.data(), s.size(), begin() + oldSize, written); - - resize(oldSize + written); - - return *this; -} - -template <> -TUtf32String& -TBasicString>::AppendUtf8(const ::TStringBuf& s) { - size_t oldSize = size(); - ReserveAndResize(size() + s.size() * 4); - size_t written = 0; - size_t pos = UTF8ToWideImpl(s.data(), s.size(), begin() + oldSize, written); - if (pos != s.size()) { - ythrow yexception() << "failed to decode UTF-8 string at pos " << pos << ::NDetail::InStringMsg(s.data(), s.size()); - } - resize(oldSize + written); - - return *this; -} - -template <> -TUtf32String& -TBasicString>::AppendUtf16(const ::TWtringBuf& s) { - size_t oldSize = size(); - ReserveAndResize(size() + s.size() * 2); - - wchar32* oldEnd = begin() + oldSize; - wchar32* end = oldEnd; - NDetail::UTF16ToUTF32ImplScalar(s.data(), s.data() + s.size(), end); - size_t written = end - oldEnd; - - resize(oldSize + written); - - return *this; -} - -template <> -bool TBasicString>::to_lower(size_t pos, size_t n) { - return ToLower(*this, pos, n); -} - -template <> -bool TBasicString>::to_upper(size_t pos, size_t n) { - return ToUpper(*this, pos, n); -} - -template <> -bool TBasicString>::to_title(size_t pos, size_t n) { - return ToTitle(*this, pos, n); -} diff --git a/util/generic/string.h b/util/generic/string.h deleted file mode 100644 index e71a2ddb84c..00000000000 --- a/util/generic/string.h +++ /dev/null @@ -1,1294 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "ptr.h" -#include "utility.h" -#include "bitops.h" -#include "explicit_type.h" -#include "reserve.h" -#include "singleton.h" -#include "strbase.h" -#include "strbuf.h" -#include "string_hash.h" - -#if defined(address_sanitizer_enabled) || defined(thread_sanitizer_enabled) - #include "hide_ptr.h" -#endif - -template -void ResizeUninitialized(std::basic_string& s, size_t len) { -#if defined(_YNDX_LIBCXX_ENABLE_STRING_RESIZE_UNINITIALIZED) - s.resize_uninitialized(len); -#else - s.resize(len); -#endif -} - -#define Y_NOEXCEPT - -#ifndef TSTRING_IS_STD_STRING -template -class TStringPtrOps { -public: - static inline void Ref(T* t) noexcept { - if (t != T::NullStr()) { - t->Ref(); - } - } - - static inline void UnRef(T* t) noexcept { - if (t != T::NullStr()) { - t->UnRef(); - } - } - - static inline long RefCount(const T* t) noexcept { - if (t == T::NullStr()) { - return -1; - } - - return t->RefCount(); - } -}; - -alignas(32) extern const char NULL_STRING_REPR[128]; - -struct TRefCountHolder { - TAtomicCounter C = 1; -}; - -template -struct TStdString: public TRefCountHolder, public B { - template - inline TStdString(Args&&... args) - : B(std::forward(args)...) - { - } - - inline bool IsNull() const noexcept { - return this == NullStr(); - } - - static TStdString* NullStr() noexcept { - #ifdef _LIBCPP_VERSION - return (TStdString*)NULL_STRING_REPR; - #else - return Singleton(); - #endif - } - -private: - friend TStringPtrOps; - inline void Ref() noexcept { - C.Inc(); - } - - inline void UnRef() noexcept { - if (C.Val() == 1 || C.Dec() == 0) { - delete this; - } - } - - inline long RefCount() const noexcept { - return C.Val(); - } -}; - -template -class TBasicCharRef { -public: - using TChar = typename TStringType::TChar; - - TBasicCharRef(TStringType& s, size_t pos) - : S_(s) - , Pos_(pos) - { - } - - operator TChar() const { - return S_.at(Pos_); - } - - TChar* operator&() { - return S_.begin() + Pos_; - } - - const TChar* operator&() const { - return S_.cbegin() + Pos_; - } - - TBasicCharRef& operator=(TChar c) { - Y_ASSERT(Pos_ < S_.size() || (Pos_ == S_.size() && !c)); - - S_.Detach()[Pos_] = c; - - return *this; - } - - TBasicCharRef& operator=(const TBasicCharRef& other) { - return this->operator=(static_cast(other)); - } - - /* - * WARN: - * Though references are copyable types according to the standard, - * the behavior of this explicit default specification is different from the one - * implemented by the assignment operator above. - * - * An attempt to explicitly delete it will break valid invocations like - * auto c = flag ? s[i] : s[j]; - */ - TBasicCharRef(const TBasicCharRef&) = default; - -private: - TStringType& S_; - size_t Pos_; -}; -#endif - -template -class TBasicString: public TStringBase, TCharType, TTraits> { -public: - // TODO: Move to private section - using TBase = TStringBase; - using TStringType = std::basic_string; -#ifdef TSTRING_IS_STD_STRING - using TStorage = TStringType; - using reference = typename TStorage::reference; -#else - using TStdStr = TStdString; - using TStorage = TIntrusivePtr>; - using reference = TBasicCharRef; -#endif - using char_type = TCharType; // TODO: DROP - using value_type = TCharType; - using traits_type = TTraits; - - using iterator = TCharType*; - using reverse_iterator = std::reverse_iterator; - using typename TBase::const_iterator; - using typename TBase::const_reference; - using typename TBase::const_reverse_iterator; - - struct TUninitialized { - explicit TUninitialized(size_t size) - : Size(size) - { - } - - size_t Size; - }; - - static size_t max_size() noexcept { - static size_t res = TStringType().max_size(); - - return res; - } - -protected: -#ifdef TSTRING_IS_STD_STRING - TStorage Storage_; -#else - TStorage S_; - - template - static TStorage Construct(A&&... a) { - return {new TStdStr(std::forward(a)...), typename TStorage::TNoIncrement()}; - } - - static TStorage Construct() noexcept { - return TStdStr::NullStr(); - } - - TStdStr& StdStr() noexcept { - return *S_; - } - - const TStdStr& StdStr() const noexcept { - return *S_; - } - - /** - * Makes a distinct copy of this string. `IsDetached()` is always true after this call. - * - * @throw std::length_error - */ - void Clone() { - Construct(StdStr()).Swap(S_); - } - - size_t RefCount() const noexcept { - return S_.RefCount(); - } -#endif - -public: - inline const TStringType& ConstRef() const { -#ifdef TSTRING_IS_STD_STRING - return Storage_; -#else - return StdStr(); -#endif - } - - inline TStringType& MutRef() { -#ifdef TSTRING_IS_STD_STRING - return Storage_; -#else - Detach(); - - return StdStr(); -#endif - } - - inline const_reference operator[](size_t pos) const noexcept { - Y_ASSERT(pos <= length()); - - return this->data()[pos]; - } - - inline reference operator[](size_t pos) noexcept { - Y_ASSERT(pos <= length()); - -#ifdef TSTRING_IS_STD_STRING - return Storage_[pos]; -#else - return reference(*this, pos); -#endif - } - - using TBase::back; - - inline reference back() noexcept { - Y_ASSERT(!this->empty()); - -#ifdef TSTRING_IS_STD_STRING - return Storage_.back(); -#else - if (Y_UNLIKELY(this->empty())) { - return reference(*this, 0); - } - - return reference(*this, length() - 1); -#endif - } - - using TBase::front; - - inline reference front() noexcept { - Y_ASSERT(!this->empty()); - -#ifdef TSTRING_IS_STD_STRING - return Storage_.front(); -#else - return reference(*this, 0); -#endif - } - - inline size_t length() const noexcept { - return ConstRef().length(); - } - - inline const TCharType* data() const noexcept { - return ConstRef().data(); - } - - inline const TCharType* c_str() const noexcept { - return ConstRef().c_str(); - } - - // ~~~ STL compatible method to obtain data pointer ~~~ - iterator begin() { - return &*MutRef().begin(); - } - - iterator vend() { - return &*MutRef().end(); - } - - reverse_iterator rbegin() { - return reverse_iterator(vend()); - } - - reverse_iterator rend() { - return reverse_iterator(begin()); - } - - using TBase::begin; //!< const_iterator TStringBase::begin() const - using TBase::cbegin; //!< const_iterator TStringBase::cbegin() const - using TBase::cend; //!< const_iterator TStringBase::cend() const - using TBase::crbegin; //!< const_reverse_iterator TStringBase::crbegin() const - using TBase::crend; //!< const_reverse_iterator TStringBase::crend() const - using TBase::end; //!< const_iterator TStringBase::end() const - using TBase::rbegin; //!< const_reverse_iterator TStringBase::rbegin() const - using TBase::rend; //!< const_reverse_iterator TStringBase::rend() const - - inline size_t capacity() const noexcept { -#ifdef TSTRING_IS_STD_STRING - return Storage_.capacity(); -#else - if (S_->IsNull()) { - return 0; - } - - return S_->capacity(); -#endif - } - - TCharType* Detach() { -#ifdef TSTRING_IS_STD_STRING - return Storage_.data(); -#else - if (Y_UNLIKELY(!IsDetached())) { - Clone(); - } - - return (TCharType*)S_->data(); -#endif - } - - bool IsDetached() const { -#ifdef TSTRING_IS_STD_STRING - return true; -#else - return 1 == RefCount(); -#endif - } - - // ~~~ Size and capacity ~~~ - TBasicString& resize(size_t n, TCharType c = ' ') { // remove or append - MutRef().resize(n, c); - - return *this; - } - - // ~~~ Constructor ~~~ : FAMILY0(,TBasicString) - TBasicString() noexcept -#ifndef TSTRING_IS_STD_STRING - : S_(Construct()) -#endif - { - } - - inline explicit TBasicString(::NDetail::TReserveTag rt) -#ifndef TSTRING_IS_STD_STRING - : S_(Construct<>()) -#endif - { - reserve(rt.Capacity); - } - - inline TBasicString(const TBasicString& s) -#ifdef TSTRING_IS_STD_STRING - : Storage_(s.Storage_) -#else - : S_(s.S_) -#endif - { - } - - inline TBasicString(TBasicString&& s) noexcept -#ifdef TSTRING_IS_STD_STRING - : Storage_(std::move(s.Storage_)) -#else - : S_(Construct()) -#endif - { -#ifdef TSTRING_IS_STD_STRING -#else - s.swap(*this); -#endif - } - - template - explicit inline TBasicString(const std::basic_string& s) - : TBasicString(s.data(), s.size()) - { - } - - template - inline TBasicString(std::basic_string&& s) -#ifdef TSTRING_IS_STD_STRING - : Storage_(std::move(s)) -#else - : S_(s.empty() ? Construct() : Construct(std::move(s))) -#endif - { - } - - TBasicString(const TBasicString& s, size_t pos, size_t n) Y_NOEXCEPT -#ifdef TSTRING_IS_STD_STRING - : Storage_(s.Storage_, pos, n) -#else - : S_(n ? Construct(s, pos, n) : Construct()) -#endif - { - } - - TBasicString(const TCharType* pc) - : TBasicString(pc, TBase::StrLen(pc)) - { - } - // TODO thegeorg@: uncomment and fix clients - // TBasicString(std::nullptr_t) = delete; - - TBasicString(const TCharType* pc, size_t n) -#ifdef TSTRING_IS_STD_STRING - : Storage_(pc, n) -#else - : S_(n ? Construct(pc, n) : Construct()) -#endif - { - } - TBasicString(std::nullptr_t, size_t) = delete; - - TBasicString(const TCharType* pc, size_t pos, size_t n) - : TBasicString(pc + pos, n) - { - } - -#ifdef TSTRING_IS_STD_STRING - explicit TBasicString(TExplicitType c) { - Storage_.push_back(c); - } -#else - explicit TBasicString(TExplicitType c) - : TBasicString(&c.Value(), 1) - { - } - explicit TBasicString(const reference& c) - : TBasicString(&c, 1) - { - } -#endif - - TBasicString(size_t n, TCharType c) -#ifdef TSTRING_IS_STD_STRING - : Storage_(n, c) -#else - : S_(Construct(n, c)) -#endif - { - } - - /** - * Constructs an uninitialized string of size `uninitialized.Size`. The proper - * way to use this ctor is via `TBasicString::Uninitialized` factory function. - * - * @throw std::length_error - */ - TBasicString(TUninitialized uninitialized) -#if !defined(TSTRING_IS_STD_STRING) - : S_(Construct<>()) -#endif - { - ReserveAndResize(uninitialized.Size); - } - - TBasicString(const TCharType* b, const TCharType* e) - : TBasicString(b, e - b) - { - } - - explicit TBasicString(const TBasicStringBuf s) - : TBasicString(s.data(), s.size()) - { - } - - template - explicit inline TBasicString(const std::basic_string_view& s) - : TBasicString(s.data(), s.size()) - { - } - - /** - * WARN: - * Certain invocations of this method will result in link-time error. - * You are free to implement corresponding methods in string.cpp if you need them. - */ - static TBasicString FromAscii(const ::TStringBuf& s) { - return TBasicString().AppendAscii(s); - } - - static TBasicString FromUtf8(const ::TStringBuf& s) { - return TBasicString().AppendUtf8(s); - } - - static TBasicString FromUtf16(const ::TWtringBuf& s) { - return TBasicString().AppendUtf16(s); - } - - static TBasicString Uninitialized(size_t n) { - return TBasicString(TUninitialized(n)); - } - -private: - template - static size_t SumLength(const TBasicStringBuf s1, const R&... r) noexcept { - return s1.size() + SumLength(r...); - } - - template - static size_t SumLength(const TCharType /*s1*/, const R&... r) noexcept { - return 1 + SumLength(r...); - } - - static constexpr size_t SumLength() noexcept { - return 0; - } - - template - static void CopyAll(TCharType* p, const TBasicStringBuf s, const R&... r) { - TTraits::copy(p, s.data(), s.size()); - CopyAll(p + s.size(), r...); - } - - template ::value>> - static void CopyAll(TCharType* p, const TNextCharType s, const R&... r) { - p[0] = s; - CopyAll(p + 1, r...); - } - - static void CopyAll(TCharType*) noexcept { - } - -public: - Y_REINITIALIZES_OBJECT inline void clear() noexcept { -#ifdef TSTRING_IS_STD_STRING - Storage_.clear(); -#else - if (IsDetached()) { - S_->clear(); - - return; - } - - Construct().Swap(S_); -#endif - } - - template - static inline TBasicString Join(const R&... r) { - TBasicString s{TUninitialized{SumLength(r...)}}; - - TBasicString::CopyAll((TCharType*)s.data(), r...); - - return s; - } - - // ~~~ Assignment ~~~ : FAMILY0(TBasicString&, assign); - TBasicString& assign(size_t size, TCharType ch) { - ReserveAndResize(size); - std::fill(begin(), vend(), ch); - return *this; - } - - TBasicString& assign(const TBasicString& s) { - TBasicString(s).swap(*this); - - return *this; - } - - TBasicString& assign(const TBasicString& s, size_t pos, size_t n) { - return assign(TBasicString(s, pos, n)); - } - - TBasicString& assign(const TCharType* pc) { - return assign(pc, TBase::StrLen(pc)); - } - - TBasicString& assign(TCharType ch) { - return assign(&ch, 1); - } - - TBasicString& assign(const TCharType* pc, size_t len) { -#if defined(address_sanitizer_enabled) || defined(thread_sanitizer_enabled) - pc = (const TCharType*)HidePointerOrigin((void*)pc); -#endif - if (IsDetached()) { - MutRef().assign(pc, len); - } else { - TBasicString(pc, len).swap(*this); - } - - return *this; - } - - TBasicString& assign(const TCharType* first, const TCharType* last) { - return assign(first, last - first); - } - - TBasicString& assign(const TCharType* pc, size_t pos, size_t n) { - return assign(pc + pos, n); - } - - TBasicString& assign(const TBasicStringBuf s) { - return assign(s.data(), s.size()); - } - - TBasicString& assign(const TBasicStringBuf s, size_t spos, size_t sn = TBase::npos) { - return assign(s.SubString(spos, sn)); - } - - inline TBasicString& AssignNoAlias(const TCharType* pc, size_t len) { - return assign(pc, len); - } - - inline TBasicString& AssignNoAlias(const TCharType* b, const TCharType* e) { - return AssignNoAlias(b, e - b); - } - - TBasicString& AssignNoAlias(const TBasicStringBuf s) { - return AssignNoAlias(s.data(), s.size()); - } - - TBasicString& AssignNoAlias(const TBasicStringBuf s, size_t spos, size_t sn = TBase::npos) { - return AssignNoAlias(s.SubString(spos, sn)); - } - - /** - * WARN: - * Certain invocations of this method will result in link-time error. - * You are free to implement corresponding methods in string.cpp if you need them. - */ - auto AssignAscii(const ::TStringBuf& s) { - clear(); - return AppendAscii(s); - } - - auto AssignUtf8(const ::TStringBuf& s) { - clear(); - return AppendUtf8(s); - } - - auto AssignUtf16(const ::TWtringBuf& s) { - clear(); - return AppendUtf16(s); - } - - TBasicString& operator=(const TBasicString& s) { - return assign(s); - } - - TBasicString& operator=(TBasicString&& s) noexcept { - swap(s); - return *this; - } - - template - TBasicString& operator=(std::basic_string&& s) noexcept { - TBasicString(std::move(s)).swap(*this); - - return *this; - } - - TBasicString& operator=(const TBasicStringBuf s) { - return assign(s); - } - - TBasicString& operator=(std::initializer_list il) { - return assign(il.begin(), il.end()); - } - - TBasicString& operator=(const TCharType* s) { - return assign(s); - } - TBasicString& operator=(std::nullptr_t) = delete; - - TBasicString& operator=(TExplicitType ch) { - return assign(ch); - } - - inline void reserve(size_t len) { - MutRef().reserve(len); - } - - // ~~~ Appending ~~~ : FAMILY0(TBasicString&, append); - inline TBasicString& append(size_t count, TCharType ch) { - MutRef().append(count, ch); - - return *this; - } - - inline TBasicString& append(const TBasicString& s) { - MutRef().append(s.ConstRef()); - - return *this; - } - - inline TBasicString& append(const TBasicString& s, size_t pos, size_t n) { - MutRef().append(s.ConstRef(), pos, n); - - return *this; - } - - inline TBasicString& append(const TCharType* pc) Y_NOEXCEPT { - MutRef().append(pc); - - return *this; - } - - inline TBasicString& append(TCharType c) { - MutRef().push_back(c); - - return *this; - } - - inline TBasicString& append(const TCharType* first, const TCharType* last) { - MutRef().append(first, last); - - return *this; - } - - inline TBasicString& append(const TCharType* pc, size_t len) { - MutRef().append(pc, len); - - return *this; - } - - inline void ReserveAndResize(size_t len) { - ::ResizeUninitialized(MutRef(), len); - } - - TBasicString& AppendNoAlias(const TCharType* pc, size_t len) { - if (len) { - auto s = this->size(); - - ReserveAndResize(s + len); - memcpy(&*(begin() + s), pc, len * sizeof(*pc)); - } - - return *this; - } - - TBasicString& AppendNoAlias(const TBasicStringBuf s) { - return AppendNoAlias(s.data(), s.size()); - } - - TBasicString& AppendNoAlias(const TBasicStringBuf s, size_t spos, size_t sn = TBase::npos) { - return AppendNoAlias(s.SubString(spos, sn)); - } - - TBasicString& append(const TBasicStringBuf s) { - return append(s.data(), s.size()); - } - - TBasicString& append(const TBasicStringBuf s, size_t spos, size_t sn = TBase::npos) { - return append(s.SubString(spos, sn)); - } - - TBasicString& append(const TCharType* pc, size_t pos, size_t n, size_t pc_len = TBase::npos) { - return append(pc + pos, Min(n, pc_len - pos)); - } - - /** - * WARN: - * Certain invocations of this method will result in link-time error. - * You are free to implement corresponding methods in string.cpp if you need them. - */ - TBasicString& AppendAscii(const ::TStringBuf& s); - - TBasicString& AppendUtf8(const ::TStringBuf& s); - - TBasicString& AppendUtf16(const ::TWtringBuf& s); - - inline void push_back(TCharType c) { - // TODO - append(c); - } - - template - TBasicString& operator+=(const T& s) { - return append(s); - } - - template - friend TBasicString operator*(const TBasicString& s, T count) { - TBasicString result; - - for (T i = 0; i < count; ++i) { - result += s; - } - - return result; - } - - template - TBasicString& operator*=(T count) { - TBasicString temp; - - for (T i = 0; i < count; ++i) { - temp += *this; - } - - swap(temp); - - return *this; - } - - operator const TStringType&() const noexcept { - return this->ConstRef(); - } - - operator TStringType&() { - return this->MutRef(); - } - - /* - * Following overloads of "operator+" aim to choose the cheapest implementation depending on - * summand types: lvalues, detached rvalues, shared rvalues. - * - * General idea is to use the detached-rvalue argument (left of right) to store the result - * wherever possible. If a buffer in rvalue is large enough this saves a re-allocation. If - * both arguments are rvalues we check which one is detached. If both of them are detached then - * the left argument is obviously preferrable because you won't need to shift the data. - * - * If an rvalue is shared then it's basically the same as lvalue because you cannot use its - * buffer to store the sum. However, we rely on the fact that append() and prepend() are already - * optimized for the shared case and detach the string into the buffer large enough to store - * the sum (compared to the detach+reallocation). This way, if we have only one rvalue argument - * (left or right) then we simply append/prepend into it, without checking if it's detached or - * not. This will be checked inside ReserveAndResize anyway. - * - * If both arguments cannot be used to store the sum (e.g. two lvalues) then we fall back to the - * Join function that constructs a resulting string in the new buffer with the minimum overhead: - * malloc + memcpy + memcpy. - */ - - friend TBasicString operator+(TBasicString&& s1, const TBasicString& s2) Y_WARN_UNUSED_RESULT { - s1 += s2; - return std::move(s1); - } - - friend TBasicString operator+(const TBasicString& s1, TBasicString&& s2) Y_WARN_UNUSED_RESULT { - s2.prepend(s1); - return std::move(s2); - } - - friend TBasicString operator+(TBasicString&& s1, TBasicString&& s2) Y_WARN_UNUSED_RESULT { -#if 0 && !defined(TSTRING_IS_STD_STRING) - if (!s1.IsDetached() && s2.IsDetached()) { - s2.prepend(s1); - return std::move(s2); - } -#endif - s1 += s2; - return std::move(s1); - } - - friend TBasicString operator+(TBasicString&& s1, const TBasicStringBuf s2) Y_WARN_UNUSED_RESULT { - s1 += s2; - return std::move(s1); - } - - friend TBasicString operator+(TBasicString&& s1, const TCharType* s2) Y_WARN_UNUSED_RESULT { - s1 += s2; - return std::move(s1); - } - - friend TBasicString operator+(TBasicString&& s1, TCharType s2) Y_WARN_UNUSED_RESULT { - s1 += s2; - return std::move(s1); - } - - friend TBasicString operator+(TExplicitType ch, const TBasicString& s) Y_WARN_UNUSED_RESULT { - return Join(TCharType(ch), s); - } - - friend TBasicString operator+(TExplicitType ch, TBasicString&& s) Y_WARN_UNUSED_RESULT { - s.prepend(ch); - return std::move(s); - } - - friend TBasicString operator+(const TBasicString& s1, const TBasicString& s2) Y_WARN_UNUSED_RESULT { - return Join(s1, s2); - } - - friend TBasicString operator+(const TBasicString& s1, const TBasicStringBuf s2) Y_WARN_UNUSED_RESULT { - return Join(s1, s2); - } - - friend TBasicString operator+(const TBasicString& s1, const TCharType* s2) Y_WARN_UNUSED_RESULT { - return Join(s1, s2); - } - - friend TBasicString operator+(const TBasicString& s1, TCharType s2) Y_WARN_UNUSED_RESULT { - return Join(s1, TBasicStringBuf(&s2, 1)); - } - - friend TBasicString operator+(const TCharType* s1, TBasicString&& s2) Y_WARN_UNUSED_RESULT { - s2.prepend(s1); - return std::move(s2); - } - - friend TBasicString operator+(const TBasicStringBuf s1, TBasicString&& s2) Y_WARN_UNUSED_RESULT { - s2.prepend(s1); - return std::move(s2); - } - - friend TBasicString operator+(const TBasicStringBuf s1, const TBasicString& s2) Y_WARN_UNUSED_RESULT { - return Join(s1, s2); - } - - friend TBasicString operator+(const TCharType* s1, const TBasicString& s2) Y_WARN_UNUSED_RESULT { - return Join(s1, s2); - } - - friend TBasicString operator+(std::basic_string l, TBasicString r) { - return std::move(l) + r.ConstRef(); - } - - friend TBasicString operator+(TBasicString l, std::basic_string r) { - return l.ConstRef() + std::move(r); - } - - // ~~~ Prepending ~~~ : FAMILY0(TBasicString&, prepend); - TBasicString& prepend(const TBasicString& s) { - MutRef().insert(0, s.ConstRef()); - - return *this; - } - - TBasicString& prepend(const TBasicString& s, size_t pos, size_t n) { - MutRef().insert(0, s.ConstRef(), pos, n); - - return *this; - } - - TBasicString& prepend(const TCharType* pc) { - MutRef().insert(0, pc); - - return *this; - } - - TBasicString& prepend(size_t n, TCharType c) { - MutRef().insert(size_t(0), n, c); - - return *this; - } - - TBasicString& prepend(TCharType c) { - MutRef().insert(size_t(0), 1, c); - - return *this; - } - - TBasicString& prepend(const TBasicStringBuf s, size_t spos = 0, size_t sn = TBase::npos) { - return insert(0, s, spos, sn); - } - - // ~~~ Insertion ~~~ : FAMILY1(TBasicString&, insert, size_t pos); - TBasicString& insert(size_t pos, const TBasicString& s) { - MutRef().insert(pos, s.ConstRef()); - - return *this; - } - - TBasicString& insert(size_t pos, const TBasicString& s, size_t pos1, size_t n1) { - MutRef().insert(pos, s.ConstRef(), pos1, n1); - - return *this; - } - - TBasicString& insert(size_t pos, const TCharType* pc) { - MutRef().insert(pos, pc); - - return *this; - } - - TBasicString& insert(size_t pos, const TCharType* pc, size_t len) { - MutRef().insert(pos, pc, len); - - return *this; - } - - TBasicString& insert(const_iterator pos, const_iterator b, const_iterator e) { -#ifdef TSTRING_IS_STD_STRING - Storage_.insert(Storage_.begin() + this->off(pos), b, e); - - return *this; -#else - return insert(this->off(pos), b, e - b); -#endif - } - - TBasicString& insert(size_t pos, size_t n, TCharType c) { - MutRef().insert(pos, n, c); - - return *this; - } - - TBasicString& insert(const_iterator pos, size_t len, TCharType ch) { - return this->insert(this->off(pos), len, ch); - } - - TBasicString& insert(const_iterator pos, TCharType ch) { - return this->insert(pos, 1, ch); - } - - TBasicString& insert(size_t pos, const TBasicStringBuf s, size_t spos = 0, size_t sn = TBase::npos) { - MutRef().insert(pos, s, spos, sn); - - return *this; - } - - // ~~~ Removing ~~~ - TBasicString& remove(size_t pos, size_t n) Y_NOEXCEPT { - if (pos < length()) { - MutRef().erase(pos, n); - } - - return *this; - } - - TBasicString& remove(size_t pos = 0) Y_NOEXCEPT { - if (pos < length()) { - MutRef().erase(pos); - } - - return *this; - } - - TBasicString& erase(size_t pos = 0, size_t n = TBase::npos) Y_NOEXCEPT { - MutRef().erase(pos, n); - - return *this; - } - - TBasicString& erase(const_iterator b, const_iterator e) Y_NOEXCEPT { - return erase(this->off(b), e - b); - } - - TBasicString& erase(const_iterator i) Y_NOEXCEPT { - return erase(i, i + 1); - } - - TBasicString& pop_back() Y_NOEXCEPT { - Y_ASSERT(!this->empty()); - - MutRef().pop_back(); - - return *this; - } - - // ~~~ replacement ~~~ : FAMILY2(TBasicString&, replace, size_t pos, size_t n); - TBasicString& replace(size_t pos, size_t n, const TBasicString& s) Y_NOEXCEPT { - MutRef().replace(pos, n, s.ConstRef()); - - return *this; - } - - TBasicString& replace(size_t pos, size_t n, const TBasicString& s, size_t pos1, size_t n1) Y_NOEXCEPT { - MutRef().replace(pos, n, s.ConstRef(), pos1, n1); - - return *this; - } - - TBasicString& replace(size_t pos, size_t n, const TCharType* pc) Y_NOEXCEPT { - MutRef().replace(pos, n, pc); - - return *this; - } - - TBasicString& replace(size_t pos, size_t n, const TCharType* s, size_t len) Y_NOEXCEPT { - MutRef().replace(pos, n, s, len); - - return *this; - } - - TBasicString& replace(size_t pos, size_t n, const TCharType* s, size_t spos, size_t sn) Y_NOEXCEPT { - MutRef().replace(pos, n, s + spos, sn - spos); - - return *this; - } - - TBasicString& replace(size_t pos, size_t n1, size_t n2, TCharType c) Y_NOEXCEPT { - MutRef().replace(pos, n1, n2, c); - - return *this; - } - - TBasicString& replace(size_t pos, size_t n, const TBasicStringBuf s, size_t spos = 0, size_t sn = TBase::npos) Y_NOEXCEPT { - MutRef().replace(pos, n, s, spos, sn); - - return *this; - } - - void swap(TBasicString& s) noexcept { -#ifdef TSTRING_IS_STD_STRING - std::swap(Storage_, s.Storage_); -#else - S_.Swap(s.S_); -#endif - } - - /** - * @returns String suitable for debug printing (like Python's `repr()`). - * Format of the string is unspecified and may be changed over time. - */ - TBasicString Quote() const { - extern TBasicString EscapeC(const TBasicString&); - - return TBasicString() + '"' + EscapeC(*this) + '"'; - } - - /** - * Modifies the case of the string, depending on the operation. - * @return false if no changes have been made. - * - * @warning when the value_type is char, these methods will not work with non-ASCII letters. - */ - bool to_lower(size_t pos = 0, size_t n = TBase::npos); - bool to_upper(size_t pos = 0, size_t n = TBase::npos); - bool to_title(size_t pos = 0, size_t n = TBase::npos); - -public: - /** - * Modifies the substring of length `n` starting from `pos`, applying `f` to each position and symbol. - * - * @return false if no changes have been made. - */ - template - bool Transform(T&& f, size_t pos = 0, size_t n = TBase::npos) { - size_t len = length(); - - if (pos > len) { - pos = len; - } - - if (n > len - pos) { - n = len - pos; - } - - bool changed = false; - - for (size_t i = pos; i != pos + n; ++i) { -#ifdef TSTRING_IS_STD_STRING - auto c = f(i, Storage_[i]); - - if (c != Storage_[i]) { - changed = true; - - Storage_[i] = c; - } -#else - auto c = f(i, data()[i]); - if (c != data()[i]) { - if (!changed) { - Detach(); - changed = true; - } - - begin()[i] = c; - } -#endif - } - - return changed; - } -}; - -std::ostream& operator<<(std::ostream&, const TString&); -std::istream& operator>>(std::istream&, TString&); - -template -TBasicString to_lower(const TBasicString& s) { - TBasicString ret(s); - ret.to_lower(); - return ret; -} - -template -TBasicString to_upper(const TBasicString& s) { - TBasicString ret(s); - ret.to_upper(); - return ret; -} - -template -TBasicString to_title(const TBasicString& s) { - TBasicString ret(s); - ret.to_title(); - return ret; -} - -namespace std { - template <> - struct hash { - using argument_type = TString; - using result_type = size_t; - inline result_type operator()(argument_type const& s) const noexcept { - return NHashPrivate::ComputeStringHash(s.data(), s.size()); - } - }; -} - -#undef Y_NOEXCEPT - -template -inline S LegacySubstr(const S& s, size_t pos, size_t n = S::npos) { - size_t len = s.length(); - - pos = Min(pos, len); - n = Min(n, len - pos); - - return S(s, pos, n); -} - -template -inline S&& LegacyReplace(S&& s, size_t pos, Args&&... args) { - if (pos <= s.length()) { - s.replace(pos, std::forward(args)...); - } - - return s; -} - -template -inline S&& LegacyErase(S&& s, size_t pos, Args&&... args) { - if (pos <= s.length()) { - s.erase(pos, std::forward(args)...); - } - - return s; -} - -inline const char* LegacyStr(const char* s) noexcept { - return s ? s : ""; -} - -// interop -template -auto& MutRef(TBasicString& s) { - return s.MutRef(); -} - -template -const auto& ConstRef(const TBasicString& s) noexcept { - return s.ConstRef(); -} - -template -auto& MutRef(std::basic_string& s) noexcept { - return s; -} - -template -const auto& ConstRef(const std::basic_string& s) noexcept { - return s; -} - -template -void ResizeUninitialized(TBasicString& s, size_t len) { - s.ReserveAndResize(len); -} diff --git a/util/generic/string.pxd b/util/generic/string.pxd deleted file mode 100644 index c25f7392a1d..00000000000 --- a/util/generic/string.pxd +++ /dev/null @@ -1,118 +0,0 @@ -from libcpp.string cimport string as _std_string - -cdef extern from "" nogil: - - cdef cppclass TStringBuf: - TStringBuf() except + - TStringBuf(const char*) except + - TStringBuf(const char*, size_t) except + - const char* data() - char* Data() - size_t size() - size_t Size() - - -cdef extern from "" nogil: - - size_t npos "TString::npos" - - # Inheritance is bogus, but it's safe to assume TString is-a TStringBuf via implicit cast - cdef cppclass TString(TStringBuf): - TString() except + - TString(TString&) except + - TString(_std_string&) except + - TString(TString&, size_t, size_t) except + - TString(char*) except + - TString(char*, size_t) except + - TString(char*, size_t, size_t) except + - # as a TString formed by a repetition of character c, n times. - TString(size_t, char) except + - TString(char*, char*) except + - TString(TStringBuf&) except + - TString(TStringBuf&, TStringBuf&) except + - TString(TStringBuf&, TStringBuf&, TStringBuf&) except + - - const char* c_str() - size_t max_size() - size_t length() - void resize(size_t) except + - void resize(size_t, char c) except + - size_t capacity() - void reserve(size_t) except + - void clear() except + - bint empty() - - char& at(size_t) - char& operator[](size_t) - int compare(TStringBuf&) - - TString& append(TStringBuf&) except + - TString& append(TStringBuf&, size_t, size_t) except + - TString& append(char *) except + - TString& append(char *, size_t) except + - TString& append(size_t, char) except + - - void push_back(char c) except + - - TString& assign(TStringBuf&) except + - TString& assign(TStringBuf&, size_t, size_t) except + - TString& assign(char *) except + - TString& assign(char *, size_t) except + - - TString& insert(size_t, TString&) except + - TString& insert(size_t, TString&, size_t, size_t) except + - TString& insert(size_t, char* s) except + - TString& insert(size_t, char* s, size_t) except + - TString& insert(size_t, size_t, char c) except + - - size_t copy(char *, size_t) except + - size_t copy(char *, size_t, size_t) except + - - size_t find(TStringBuf&) - size_t find(TStringBuf&, size_t pos) - size_t find(char) - size_t find(char, size_t pos) - - size_t rfind(TStringBuf&) - size_t rfind(TStringBuf&, size_t pos) - size_t rfind(char) - size_t rfind(char, size_t pos) - - size_t find_first_of(char c) - size_t find_first_of(char c, size_t pos) - size_t find_first_of(TStringBuf& set) - size_t find_first_of(TStringBuf& set, size_t pos) - - size_t find_first_not_of(char c) - size_t find_first_not_of(char c, size_t pos) - size_t find_first_not_of(TStringBuf& set) - size_t find_first_not_of(TStringBuf& set, size_t pos) - - size_t find_last_of(char c) - size_t find_last_of(char c, size_t pos) - size_t find_last_of(TStringBuf& set) - size_t find_last_of(TStringBuf& set, size_t pos) - - TString substr(size_t pos) except + - TString substr(size_t pos, size_t n) except + - - TString operator+(TStringBuf& rhs) except + - TString operator+(char* rhs) except + - - bint operator==(TStringBuf&) - bint operator==(char*) - - bint operator!=(TStringBuf&) - bint operator!=(char*) - - bint operator<(TStringBuf&) - bint operator<(char*) - - bint operator>(TStringBuf&) - bint operator>(char*) - - bint operator<=(TStringBuf&) - bint operator<=(char*) - - bint operator>=(TStringBuf&) - bint operator>=(char*) diff --git a/util/generic/string_transparent_hash_ut.cpp b/util/generic/string_transparent_hash_ut.cpp index b87fa2843e6..aeecf1b8543 100644 --- a/util/generic/string_transparent_hash_ut.cpp +++ b/util/generic/string_transparent_hash_ut.cpp @@ -1,6 +1,5 @@ #include "string.h" #include "vector.h" -#include "strbuf.h" #include #include @@ -11,9 +10,9 @@ Y_UNIT_TEST_SUITE(StringHashFunctorTests) { Y_UNIT_TEST(TestTransparencyWithUnorderedSet) { // Using Abseil hash set because `std::unordered_set` is transparent only from C++20 (while // we stuck with C++17 right now). - absl::flat_hash_set, TEqualTo> s = {"foo"}; + absl::flat_hash_set, TEqualTo> s = {"foo"}; // If either `THash` or `TEqualTo` is not transparent compilation will fail. - UNIT_ASSERT_UNEQUAL(s.find(TStringBuf("foo")), s.end()); - UNIT_ASSERT_EQUAL(s.find(TStringBuf("bar")), s.end()); + UNIT_ASSERT_UNEQUAL(s.find(std::string_view("foo")), s.end()); + UNIT_ASSERT_EQUAL(s.find(std::string_view("bar")), s.end()); } } diff --git a/util/generic/string_ut.cpp b/util/generic/string_ut.cpp deleted file mode 100644 index 99e47e9f7f6..00000000000 --- a/util/generic/string_ut.cpp +++ /dev/null @@ -1,1246 +0,0 @@ -#include "deque.h" -#include "strbuf.h" -#include "string_ut.h" -#include "vector.h" -#include "yexception.h" - -#include -#include -#include -#include - -#include -#include -#include -#include - -#ifdef TSTRING_IS_STD_STRING -static_assert(sizeof(TString) == sizeof(std::string), "expect sizeof(TString) == sizeof(std::string)"); -#else -static_assert(sizeof(TString) == sizeof(const char*), "expect sizeof(TString) == sizeof(const char*)"); -#endif - -class TStringTestZero: public TTestBase { - UNIT_TEST_SUITE(TStringTestZero); - UNIT_TEST(TestZero); - UNIT_TEST_SUITE_END(); - -public: - void TestZero() { - const char data[] = "abc\0def\0"; - TString s(data, sizeof(data)); - UNIT_ASSERT(s.size() == sizeof(data)); - UNIT_ASSERT(s.StartsWith(s)); - UNIT_ASSERT(s.EndsWith(s)); - UNIT_ASSERT(s.Contains('\0')); - - const char raw_def[] = "def"; - const char raw_zero[] = "\0"; - TString def(raw_def, sizeof(raw_def) - 1); - TString zero(raw_zero, sizeof(raw_zero) - 1); - UNIT_ASSERT_EQUAL(4, s.find(raw_def)); - UNIT_ASSERT_EQUAL(4, s.find(def)); - UNIT_ASSERT_EQUAL(4, s.find_first_of(raw_def)); - UNIT_ASSERT_EQUAL(3, s.find_first_of(zero)); - UNIT_ASSERT_EQUAL(7, s.find_first_not_of(def, 4)); - - const char nonSubstring[] = "def\0ghi"; - UNIT_ASSERT_EQUAL(TString::npos, s.find(TString(nonSubstring, sizeof(nonSubstring)))); - - TString copy = s; - copy.replace(copy.size() - 1, 1, "z"); - UNIT_ASSERT(s != copy); - copy.replace(copy.size() - 1, 1, "\0", 0, 1); - UNIT_ASSERT(s == copy); - - TString prefix(data, 5); - UNIT_ASSERT(s.StartsWith(prefix)); - UNIT_ASSERT(s != prefix); - UNIT_ASSERT(s > prefix); - UNIT_ASSERT(s > s.data()); - UNIT_ASSERT(s == TString(s.data(), s.size())); - UNIT_ASSERT(data < s); - - s.remove(5); - UNIT_ASSERT(s == prefix); - } -}; - -UNIT_TEST_SUITE_REGISTRATION(TStringTestZero); - -template -class TStringStdTestImpl { - using TChar = typename TStringType::char_type; - using TTraits = typename TStringType::traits_type; - using TView = std::basic_string_view; - - TTestData Data_; - -protected: - void Constructor() { - UNIT_ASSERT_EXCEPTION(TStringType((size_t)-1, *Data_.a()), std::length_error); - } - - void reserve() { -#if 0 - TStringType s; - UNIT_ASSERT_EXCEPTION(s.reserve(s.max_size() + 1), std::length_error); - - // Non-shared behaviour - never shrink - - s.reserve(256); - #ifndef TSTRING_IS_STD_STRING - const auto* data = s.data(); - - UNIT_ASSERT(s.capacity() >= 256); - - s.reserve(128); - - UNIT_ASSERT(s.capacity() >= 256 && s.data() == data); - #endif - - s.resize(64, 'x'); - s.reserve(10); - - #ifdef TSTRING_IS_STD_STRING - UNIT_ASSERT(s.capacity() >= 64); - #else - UNIT_ASSERT(s.capacity() >= 256 && s.data() == data); - #endif - - #ifndef TSTRING_IS_STD_STRING - // Shared behaviour - always reallocate, just as much as requisted - - TStringType holder = s; - - UNIT_ASSERT(s.capacity() >= 256); - - s.reserve(128); - - UNIT_ASSERT(s.capacity() >= 128 && s.capacity() < 256 && s.data() != data); - UNIT_ASSERT(s.IsDetached()); - - s.resize(64, 'x'); - data = s.data(); - holder = s; - - s.reserve(10); - - UNIT_ASSERT(s.capacity() >= 64 && s.capacity() < 128 && s.data() != data); - UNIT_ASSERT(s.IsDetached()); - #endif -#endif - } - - void short_string() { - TStringType const ref_short_str1(Data_.str1()), ref_short_str2(Data_.str2()); - TStringType short_str1(ref_short_str1), short_str2(ref_short_str2); - TStringType const ref_long_str1(Data_.str__________________________________________________1()); - TStringType const ref_long_str2(Data_.str__________________________________________________2()); - TStringType long_str1(ref_long_str1), long_str2(ref_long_str2); - - UNIT_ASSERT(short_str1 == ref_short_str1); - UNIT_ASSERT(long_str1 == ref_long_str1); - - { - TStringType str1(short_str1); - str1 = long_str1; - UNIT_ASSERT(str1 == ref_long_str1); - } - - { - TStringType str1(long_str1); - str1 = short_str1; - UNIT_ASSERT(str1 == ref_short_str1); - } - - { - short_str1.swap(short_str2); - UNIT_ASSERT((short_str1 == ref_short_str2) && (short_str2 == ref_short_str1)); - short_str1.swap(short_str2); - } - - { - long_str1.swap(long_str2); - UNIT_ASSERT((long_str1 == ref_long_str2) && (long_str2 == ref_long_str1)); - long_str1.swap(long_str2); - } - - { - short_str1.swap(long_str1); - UNIT_ASSERT((short_str1 == ref_long_str1) && (long_str1 == ref_short_str1)); - short_str1.swap(long_str1); - } - - { - long_str1.swap(short_str1); - UNIT_ASSERT((short_str1 == ref_long_str1) && (long_str1 == ref_short_str1)); - long_str1.swap(short_str1); - } - - { - //This is to test move constructor - std::vector str_vect; - - str_vect.push_back(short_str1); - str_vect.push_back(long_str1); - str_vect.push_back(short_str2); - str_vect.push_back(long_str2); - - UNIT_ASSERT(str_vect[0] == ref_short_str1); - UNIT_ASSERT(str_vect[1] == ref_long_str1); - UNIT_ASSERT(str_vect[2] == ref_short_str2); - UNIT_ASSERT(str_vect[3] == ref_long_str2); - } - } - - void erase() { - TChar const* c_str = Data_.Hello_World(); - TStringType str(c_str); - UNIT_ASSERT(str == c_str); - - str.erase(str.begin() + 1, str.end() - 1); // Erase all but first and last. - - size_t i; - for (i = 0; i < str.size(); ++i) { - switch (i) { - case 0: - UNIT_ASSERT(str[i] == *Data_.H()); - break; - - case 1: - UNIT_ASSERT(str[i] == *Data_.d()); - break; - - default: - UNIT_ASSERT(false); - } - } - - str.insert(1, c_str); - str.erase(str.begin()); // Erase first element. - str.erase(str.end() - 1); // Erase last element. - UNIT_ASSERT(str == c_str); - str.clear(); // Erase all. - UNIT_ASSERT(str.empty()); - - str = c_str; - UNIT_ASSERT(str == c_str); - - str.erase(1, str.size() - 1); // Erase all but first and last. - for (i = 0; i < str.size(); i++) { - switch (i) { - case 0: - UNIT_ASSERT(str[i] == *Data_.H()); - break; - - case 1: - UNIT_ASSERT(str[i] == *Data_.d()); - break; - - default: - UNIT_ASSERT(false); - } - } - - str.erase(1); - UNIT_ASSERT(str == Data_.H()); - } - - void data() { - TStringType xx; - - // ISO-IEC-14882:1998(E), 21.3.6, paragraph 3 - UNIT_ASSERT(xx.data() != nullptr); - } - - void c_str() { - TStringType low(Data_._2004_01_01()); - TStringType xx; - TStringType yy; - - // ISO-IEC-14882:1998(E), 21.3.6, paragraph 1 - UNIT_ASSERT(*(yy.c_str()) == 0); - - // Blocks A and B should follow each other. - // Block A: - xx = Data_._123456(); - xx += low; - UNIT_ASSERT(xx.c_str() == TView(Data_._1234562004_01_01())); - // End of block A - - // Block B: - xx = Data_._1234(); - xx += Data_._5(); - UNIT_ASSERT(xx.c_str() == TView(Data_._12345())); - // End of block B - } - - void null_char_of_empty() { - const TStringType s; - - //NOTE: https://a.yandex-team.ru/arcadia/junk/grechnik/test_string?rev=r12602052 - i64 i = s[s.size()]; - UNIT_ASSERT_VALUES_EQUAL(i, 0); - } - - void null_char() { - // ISO/IEC 14882:1998(E), ISO/IEC 14882:2003(E), 21.3.4 ('... the const version') - const TStringType s(Data_._123456()); - - UNIT_ASSERT(s[s.size()] == 0); - } - - // Allowed since C++17, see http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2475 - void null_char_assignment_to_subscript_of_empty() { - TStringType s; - -#ifdef TSTRING_IS_STD_STRING - using reference = volatile typename TStringType::value_type&; -#else - using reference = typename TStringType::reference; -#endif - reference trailing_zero = s[s.size()]; - trailing_zero = 0; - UNIT_ASSERT(trailing_zero == 0); - } - - // Allowed since C++17, see http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2475 - void null_char_assignment_to_subscript_of_nonempty() { - TStringType s(Data_._123456()); - -#ifdef TSTRING_IS_STD_STRING - using reference = volatile typename TStringType::value_type&; -#else - using reference = typename TStringType::reference; -#endif - reference trailing_zero = s[s.size()]; - trailing_zero = 0; - UNIT_ASSERT(trailing_zero == 0); - } - -#ifndef TSTRING_IS_STD_STRING - // Dereferencing string end() is not allowed by C++ standard as of C++20, avoid using in real code. - void null_char_assignment_to_end_of_empty() { - TStringType s; - - volatile auto& trailing_zero = *(s.begin() + s.size()); - trailing_zero = 0; - UNIT_ASSERT(trailing_zero == 0); - } - - // Dereferencing string end() is not allowed by C++ standard as of C++20, avoid using in real code. - void null_char_assignment_to_end_of_nonempty() { - TStringType s(Data_._123456()); - - volatile auto& trailing_zero = *(s.begin() + s.size()); - trailing_zero = 0; - UNIT_ASSERT(trailing_zero == 0); - } -#endif - - void insert() { - TStringType strorg = Data_.This_is_test_string_for_string_calls(); - TStringType str; - - // In case of reallocation there is no auto reference problem - // so we reserve a big enough TStringType to be sure to test this - // particular point. - - str.reserve(100); - str = strorg; - - //test self insertion: - str.insert(10, str.c_str() + 5, 15); - UNIT_ASSERT(str == Data_.This_is_teis_test_string_st_string_for_string_calls()); - - str = strorg; - str.insert(15, str.c_str() + 5, 25); - UNIT_ASSERT(str == Data_.This_is_test_stis_test_string_for_stringring_for_string_calls()); - - str = strorg; - str.insert(0, str.c_str() + str.size() - 4, 4); - UNIT_ASSERT(str == Data_.allsThis_is_test_string_for_string_calls()); - - str = strorg; - str.insert(0, str.c_str() + str.size() / 2 - 1, str.size() / 2 + 1); - UNIT_ASSERT(str == Data_.ng_for_string_callsThis_is_test_string_for_string_calls()); - - str = strorg; - typename TStringType::iterator b = str.begin(); - typename TStringType::const_iterator s = str.begin() + str.size() / 2 - 1; - typename TStringType::const_iterator e = str.end(); - str.insert(b, s, e); - UNIT_ASSERT(str == Data_.ng_for_string_callsThis_is_test_string_for_string_calls()); - -#if 0 - // AV - str = strorg; - str.insert(str.begin(), str.begin() + str.size() / 2 - 1, str.end()); - UNIT_ASSERT(str == Data.ng_for_string_callsThis_is_test_string_for_string_calls()); -#endif - - TStringType str0; - str0.insert(str0.begin(), 5, *Data_._0()); - UNIT_ASSERT(str0 == Data_._00000()); - - TStringType str1; - { - typename TStringType::size_type pos = 0, nb = 2; - str1.insert(pos, nb, *Data_._1()); - } - UNIT_ASSERT(str1 == Data_._11()); - - str0.insert(0, str1); - UNIT_ASSERT(str0 == Data_._1100000()); - - TStringType str2(Data_._2345()); - str0.insert(str0.size(), str2, 1, 2); - UNIT_ASSERT(str0 == Data_._110000034()); - - str1.insert(str1.begin() + 1, 2, *Data_._2()); - UNIT_ASSERT(str1 == Data_._1221()); - - str1.insert(2, Data_._333333(), 3); - UNIT_ASSERT(str1 == Data_._1233321()); - - str1.insert(4, Data_._4444()); - UNIT_ASSERT(str1 == Data_._12334444321()); - - str1.insert(str1.begin() + 6, *Data_._5()); - UNIT_ASSERT(str1 == Data_._123344544321()); - } - - void resize() { - TStringType s; - - s.resize(0); - - UNIT_ASSERT(*s.c_str() == 0); - - s = Data_._1234567(); - - s.resize(0); - UNIT_ASSERT(*s.c_str() == 0); - - s = Data_._1234567(); - s.resize(1); - UNIT_ASSERT(s.size() == 1); - UNIT_ASSERT(*s.c_str() == *Data_._1()); - UNIT_ASSERT(*(s.c_str() + 1) == 0); - - s = Data_._1234567(); -#if 0 - s.resize(10); -#else - s.resize(10, 0); -#endif - UNIT_ASSERT(s.size() == 10); - UNIT_ASSERT(s[6] == *Data_._7()); - UNIT_ASSERT(s[7] == 0); - UNIT_ASSERT(s[8] == 0); - UNIT_ASSERT(s[9] == 0); - } - - void find() { - TStringType s(Data_.one_two_three_one_two_three()); - - UNIT_ASSERT(s.find(Data_.one()) == 0); - UNIT_ASSERT(s.find(*Data_.t()) == 4); - UNIT_ASSERT(s.find(*Data_.t(), 5) == 8); - - UNIT_ASSERT(s.find(Data_.four()) == TStringType::npos); - UNIT_ASSERT(s.find(Data_.one(), TStringType::npos) == TStringType::npos); - UNIT_ASSERT(s.find_first_of(Data_.abcde()) == 2); - UNIT_ASSERT(s.find_first_not_of(Data_.enotw_()) == 9); - } - - void capacity() { - TStringType s; - - UNIT_ASSERT(s.capacity() < s.max_size()); - UNIT_ASSERT(s.capacity() >= s.size()); - - for (int i = 0; i < 18; ++i) { - s += ' '; - - UNIT_ASSERT(s.capacity() > 0); - UNIT_ASSERT(s.capacity() < s.max_size()); - UNIT_ASSERT(s.capacity() >= s.size()); - } - } - - void assign() { - TStringType s; - TChar const* cstr = Data_.test_string_for_assign(); - - s.assign(cstr, cstr + 22); - UNIT_ASSERT(s == Data_.test_string_for_assign()); - - TStringType s2(Data_.other_test_string()); - s.assign(s2); - UNIT_ASSERT(s == s2); - - static TStringType str1; - static TStringType str2; - - // short TStringType optim: - str1 = Data_._123456(); - // longer than short TStringType: - str2 = Data_._1234567890123456789012345678901234567890(); - - UNIT_ASSERT(str1[5] == *Data_._6()); - UNIT_ASSERT(str2[29] == *Data_._0()); - } - - void copy() { - TStringType s(Data_.foo()); - TChar dest[4]; - dest[0] = dest[1] = dest[2] = dest[3] = 1; - s.copy(dest, 4); - int pos = 0; - UNIT_ASSERT(dest[pos++] == *Data_.f()); - UNIT_ASSERT(dest[pos++] == *Data_.o()); - UNIT_ASSERT(dest[pos++] == *Data_.o()); - UNIT_ASSERT(dest[pos++] == 1); - - dest[0] = dest[1] = dest[2] = dest[3] = 1; - s.copy(dest, 4, 2); - pos = 0; - UNIT_ASSERT(dest[pos++] == *Data_.o()); - UNIT_ASSERT(dest[pos++] == 1); - - UNIT_ASSERT_EXCEPTION(s.copy(dest, 4, 5), std::out_of_range); - } - - void cbegin_cend() { - const char helloThere[] = "Hello there"; - TString s = helloThere; - size_t index = 0; - for (auto it = s.cbegin(); s.cend() != it; ++it, ++index) { - UNIT_ASSERT_VALUES_EQUAL(helloThere[index], *it); - } - } - - void compare() { - TStringType str1(Data_.abcdef()); - TStringType str2; - - str2 = Data_.abcdef(); - UNIT_ASSERT(str1.compare(str2) == 0); - UNIT_ASSERT(str1.compare(str2.data(), str2.size()) == 0); - str2 = Data_.abcde(); - UNIT_ASSERT(str1.compare(str2) > 0); - UNIT_ASSERT(str1.compare(str2.data(), str2.size()) > 0); - str2 = Data_.abcdefg(); - UNIT_ASSERT(str1.compare(str2) < 0); - UNIT_ASSERT(str1.compare(str2.data(), str2.size()) < 0); - - UNIT_ASSERT(str1.compare(Data_.abcdef()) == 0); - UNIT_ASSERT(str1.compare(Data_.abcde()) > 0); - UNIT_ASSERT(str1.compare(Data_.abcdefg()) < 0); - - str2 = Data_.cde(); - UNIT_ASSERT(str1.compare(2, 3, str2) == 0); - str2 = Data_.cd(); - UNIT_ASSERT(str1.compare(2, 3, str2) > 0); - str2 = Data_.cdef(); - UNIT_ASSERT(str1.compare(2, 3, str2) < 0); - - str2 = Data_.abcdef(); - UNIT_ASSERT(str1.compare(2, 3, str2, 2, 3) == 0); - UNIT_ASSERT(str1.compare(2, 3, str2, 2, 2) > 0); - UNIT_ASSERT(str1.compare(2, 3, str2, 2, 4) < 0); - - UNIT_ASSERT(str1.compare(2, 3, Data_.cdefgh(), 3) == 0); - UNIT_ASSERT(str1.compare(2, 3, Data_.cdefgh(), 2) > 0); - UNIT_ASSERT(str1.compare(2, 3, Data_.cdefgh(), 4) < 0); - } - - void find_last_of() { - // 21.3.6.4 - TStringType s(Data_.one_two_three_one_two_three()); - - UNIT_ASSERT(s.find_last_of(Data_.abcde()) == 26); - UNIT_ASSERT(s.find_last_of(TStringType(Data_.abcde())) == 26); - - TStringType test(Data_.aba()); - - UNIT_ASSERT(test.find_last_of(Data_.a(), 2, 1) == 2); - UNIT_ASSERT(test.find_last_of(Data_.a(), 1, 1) == 0); - UNIT_ASSERT(test.find_last_of(Data_.a(), 0, 1) == 0); - - UNIT_ASSERT(test.find_last_of(*Data_.a(), 2) == 2); - UNIT_ASSERT(test.find_last_of(*Data_.a(), 1) == 0); - UNIT_ASSERT(test.find_last_of(*Data_.a(), 0) == 0); - } -#if 0 - void rfind() { - // 21.3.6.2 - TStringType s(Data.one_two_three_one_two_three()); - - UNIT_ASSERT(s.rfind(Data.two()) == 18); - UNIT_ASSERT(s.rfind(Data.two(), 0) == TStringType::npos); - UNIT_ASSERT(s.rfind(Data.two(), 11) == 4); - UNIT_ASSERT(s.rfind(*Data.w()) == 19); - - TStringType test(Data.aba()); - - UNIT_ASSERT(test.rfind(Data.a(), 2, 1) == 2); - UNIT_ASSERT(test.rfind(Data.a(), 1, 1) == 0); - UNIT_ASSERT(test.rfind(Data.a(), 0, 1) == 0); - - UNIT_ASSERT(test.rfind(*Data.a(), 2) == 2); - UNIT_ASSERT(test.rfind(*Data.a(), 1) == 0); - UNIT_ASSERT(test.rfind(*Data.a(), 0) == 0); - } -#endif - void find_last_not_of() { - // 21.3.6.6 - TStringType s(Data_.one_two_three_one_two_three()); - - UNIT_ASSERT(s.find_last_not_of(Data_.ehortw_()) == 15); - - TStringType test(Data_.aba()); - - UNIT_ASSERT(test.find_last_not_of(Data_.a(), 2, 1) == 1); - UNIT_ASSERT(test.find_last_not_of(Data_.b(), 2, 1) == 2); - UNIT_ASSERT(test.find_last_not_of(Data_.a(), 1, 1) == 1); - UNIT_ASSERT(test.find_last_not_of(Data_.b(), 1, 1) == 0); - UNIT_ASSERT(test.find_last_not_of(Data_.a(), 0, 1) == TStringType::npos); - UNIT_ASSERT(test.find_last_not_of(Data_.b(), 0, 1) == 0); - - UNIT_ASSERT(test.find_last_not_of(*Data_.a(), 2) == 1); - UNIT_ASSERT(test.find_last_not_of(*Data_.b(), 2) == 2); - UNIT_ASSERT(test.find_last_not_of(*Data_.a(), 1) == 1); - UNIT_ASSERT(test.find_last_not_of(*Data_.b(), 1) == 0); - UNIT_ASSERT(test.find_last_not_of(*Data_.a(), 0) == TStringType::npos); - UNIT_ASSERT(test.find_last_not_of(*Data_.b(), 0) == 0); - } -#if 0 - void replace() { - // This test case is for the non template basic_TString::replace method, - // this is why we play with the const iterators and reference to guaranty - // that the right method is called. - - const TStringType v(Data._78()); - TStringType s(Data._123456()); - TStringType const& cs = s; - - typename TStringType::iterator i = s.begin() + 1; - s.replace(i, i + 3, v.begin(), v.end()); - UNIT_ASSERT(s == Data._17856()); - - s = Data._123456(); - i = s.begin() + 1; - s.replace(i, i + 1, v.begin(), v.end()); - UNIT_ASSERT(s == Data._1783456()); - - s = Data._123456(); - i = s.begin() + 1; - typename TStringType::const_iterator ci = s.begin() + 1; - s.replace(i, i + 3, ci + 3, cs.end()); - UNIT_ASSERT(s == Data._15656()); - - s = Data._123456(); - i = s.begin() + 1; - ci = s.begin() + 1; - s.replace(i, i + 3, ci, ci + 2); - UNIT_ASSERT(s == Data._12356()); - - s = Data._123456(); - i = s.begin() + 1; - ci = s.begin() + 1; - s.replace(i, i + 3, ci + 1, cs.end()); - UNIT_ASSERT(s == Data._1345656()); - - s = Data._123456(); - i = s.begin(); - ci = s.begin() + 1; - s.replace(i, i, ci, ci + 1); - UNIT_ASSERT(s == Data._2123456()); - - s = Data._123456(); - s.replace(s.begin() + 4, s.end(), cs.begin(), cs.end()); - UNIT_ASSERT(s == Data._1234123456()); - - // This is the test for the template replace method. - - s = Data._123456(); - typename TStringType::iterator b = s.begin() + 4; - typename TStringType::iterator e = s.end(); - typename TStringType::const_iterator rb = s.begin(); - typename TStringType::const_iterator re = s.end(); - s.replace(b, e, rb, re); - UNIT_ASSERT(s == Data._1234123456()); - - s = Data._123456(); - s.replace(s.begin() + 4, s.end(), s.begin(), s.end()); - UNIT_ASSERT(s == Data._1234123456()); - - TStringType strorg(Data.This_is_test_StringT_for_StringT_calls()); - TStringType str = strorg; - str.replace(5, 15, str.c_str(), 10); - UNIT_ASSERT(str == Data.This_This_is_tefor_StringT_calls()); - - str = strorg; - str.replace(5, 5, str.c_str(), 10); - UNIT_ASSERT(str == Data.This_This_is_test_StringT_for_StringT_calls()); - - #if !defined(STLPORT) || defined(_STLP_MEMBER_TEMPLATES) - deque cdeque; - cdeque.push_back(*Data.I()); - str.replace(str.begin(), str.begin() + 11, cdeque.begin(), cdeque.end()); - UNIT_ASSERT(str == Data.Is_test_StringT_for_StringT_calls()); - #endif - } -#endif -}; // TStringStdTestImpl - -class TStringTest: public TTestBase, private TStringTestImpl> { -public: - UNIT_TEST_SUITE(TStringTest); - UNIT_TEST(TestMaxSize); - UNIT_TEST(TestConstructors); - UNIT_TEST(TestReplace); -#ifndef TSTRING_IS_STD_STRING - UNIT_TEST(TestRefCount); -#endif - UNIT_TEST(TestFind); - UNIT_TEST(TestContains); - UNIT_TEST(TestOperators); - UNIT_TEST(TestMulOperators); - UNIT_TEST(TestFuncs); - UNIT_TEST(TestUtils); - UNIT_TEST(TestEmpty); - UNIT_TEST(TestJoin); - UNIT_TEST(TestCopy); - UNIT_TEST(TestStrCpy); - UNIT_TEST(TestPrefixSuffix); -#ifndef TSTRING_IS_STD_STRING - UNIT_TEST(TestCharRef); -#endif - UNIT_TEST(TestBack) - UNIT_TEST(TestFront) - UNIT_TEST(TestIterators); - UNIT_TEST(TestReverseIterators); - UNIT_TEST(TestAppendUtf16) - UNIT_TEST(TestFillingAssign) - UNIT_TEST(TestStdStreamApi) - //UNIT_TEST(TestOperatorsCI); must fail - UNIT_TEST_SUITE_END(); - - void TestAppendUtf16() { - TString appended = TString("А роза упала").AppendUtf16(u" на лапу Азора"); - UNIT_ASSERT(appended == "А роза упала на лапу Азора"); - } - - void TestFillingAssign() { - TString s("abc"); - s.assign(5, 'a'); - UNIT_ASSERT_VALUES_EQUAL(s, "aaaaa"); - } - - void TestStdStreamApi() { - const TString data = "abracadabra"; - std::stringstream ss; - ss << data; - - UNIT_ASSERT_VALUES_EQUAL(data, ss.str()); - - ss << '\n' - << data << std::endl; - - TString read = "xxx"; - ss >> read; - UNIT_ASSERT_VALUES_EQUAL(read, data); - } -}; - -UNIT_TEST_SUITE_REGISTRATION(TStringTest); - -class TWideStringTest: public TTestBase, private TStringTestImpl> { -public: - UNIT_TEST_SUITE(TWideStringTest); - UNIT_TEST(TestConstructors); - UNIT_TEST(TestReplace); -#ifndef TSTRING_IS_STD_STRING - UNIT_TEST(TestRefCount); -#endif - UNIT_TEST(TestFind); - UNIT_TEST(TestContains); - UNIT_TEST(TestOperators); - UNIT_TEST(TestLetOperator) - UNIT_TEST(TestMulOperators); - UNIT_TEST(TestFuncs); - UNIT_TEST(TestUtils); - UNIT_TEST(TestEmpty); - UNIT_TEST(TestJoin); - UNIT_TEST(TestCopy); - UNIT_TEST(TestStrCpy); - UNIT_TEST(TestPrefixSuffix); -#ifndef TSTRING_IS_STD_STRING - UNIT_TEST(TestCharRef); -#endif - UNIT_TEST(TestBack); - UNIT_TEST(TestFront) - UNIT_TEST(TestDecodingMethods); - UNIT_TEST(TestIterators); - UNIT_TEST(TestReverseIterators); - UNIT_TEST(TestStringLiterals); - UNIT_TEST_SUITE_END(); - -private: - void TestDecodingMethods() { - UNIT_ASSERT(TUtf16String::FromAscii("").empty()); - UNIT_ASSERT(TUtf16String::FromAscii("abc") == ASCIIToWide("abc")); - - const char* text = "123kx83abcd ej)#$%ddja&%J&"; - TUtf16String wtext = ASCIIToWide(text); - - UNIT_ASSERT(wtext == TUtf16String::FromAscii(text)); - - TString strtext(text); - UNIT_ASSERT(wtext == TUtf16String::FromAscii(strtext)); - - TStringBuf strbuftext(text); - UNIT_ASSERT(wtext == TUtf16String::FromAscii(strbuftext)); - - UNIT_ASSERT(wtext.substr(5) == TUtf16String::FromAscii(text + 5)); - - const wchar16 wideCyrillicAlphabet[] = { - 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, - 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, - 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F, - 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F, - 0x00}; - - TUtf16String strWide(wideCyrillicAlphabet); - TString strUtf8 = WideToUTF8(strWide); - - UNIT_ASSERT(strWide == TUtf16String::FromUtf8(strUtf8.c_str())); - UNIT_ASSERT(strWide == TUtf16String::FromUtf8(strUtf8)); - UNIT_ASSERT(strWide == TUtf16String::FromUtf8(TStringBuf(strUtf8))); - - // assign - - TUtf16String s1; - s1.AssignAscii("1234"); - UNIT_ASSERT(s1 == ASCIIToWide("1234")); - - s1.AssignUtf8(strUtf8); - UNIT_ASSERT(s1 == strWide); - - s1.AssignAscii(text); - UNIT_ASSERT(s1 == wtext); - - // append - - TUtf16String s2; - TUtf16String testAppend = strWide; - s2.AppendUtf8(strUtf8); - UNIT_ASSERT(testAppend == s2); - - testAppend += ' '; - s2.AppendAscii(" "); - UNIT_ASSERT(testAppend == s2); - - testAppend += '_'; - s2.AppendUtf8("_"); - UNIT_ASSERT(testAppend == s2); - - testAppend += wtext; - s2.AppendAscii(text); - UNIT_ASSERT(testAppend == s2); - - testAppend += wtext; - s2.AppendUtf8(text); - UNIT_ASSERT(testAppend == s2); - } - - void TestLetOperator() { - TUtf16String str; - - str = wchar16('X'); - UNIT_ASSERT(str == TUtf16String::FromAscii("X")); - - const TUtf16String hello = TUtf16String::FromAscii("hello"); - str = hello.data(); - UNIT_ASSERT(str == hello); - - str = hello; - UNIT_ASSERT(str == hello); - } - - void TestStringLiterals() { - TUtf16String s1 = u"hello"; - UNIT_ASSERT_VALUES_EQUAL(s1, TUtf16String::FromAscii("hello")); - - TUtf16String s2 = u"привет"; - UNIT_ASSERT_VALUES_EQUAL(s2, TUtf16String::FromUtf8("привет")); - } -}; - -UNIT_TEST_SUITE_REGISTRATION(TWideStringTest); - -class TUtf32StringTest: public TTestBase, private TStringTestImpl> { -public: - UNIT_TEST_SUITE(TUtf32StringTest); - UNIT_TEST(TestConstructors); - UNIT_TEST(TestReplace); -#ifndef TSTRING_IS_STD_STRING - UNIT_TEST(TestRefCount); -#endif - UNIT_TEST(TestFind); - UNIT_TEST(TestContains); - UNIT_TEST(TestOperators); - UNIT_TEST(TestLetOperator) - UNIT_TEST(TestMulOperators); - UNIT_TEST(TestFuncs); - UNIT_TEST(TestUtils); - UNIT_TEST(TestEmpty); - UNIT_TEST(TestJoin); - UNIT_TEST(TestCopy); - UNIT_TEST(TestStrCpy); - UNIT_TEST(TestPrefixSuffix); -#ifndef TSTRING_IS_STD_STRING - UNIT_TEST(TestCharRef); -#endif - UNIT_TEST(TestBack); - UNIT_TEST(TestFront) - UNIT_TEST(TestDecodingMethods); - UNIT_TEST(TestDecodingMethodsMixedStr); - UNIT_TEST(TestIterators); - UNIT_TEST(TestReverseIterators); - UNIT_TEST(TestStringLiterals); - UNIT_TEST_SUITE_END(); - -private: - void TestDecodingMethods() { - UNIT_ASSERT(TUtf32String::FromAscii("").empty()); - UNIT_ASSERT(TUtf32String::FromAscii("abc") == ASCIIToUTF32("abc")); - - const char* text = "123kx83abcd ej)#$%ddja&%J&"; - TUtf32String wtext = ASCIIToUTF32(text); - - UNIT_ASSERT(wtext == TUtf32String::FromAscii(text)); - - TString strtext(text); - UNIT_ASSERT(wtext == TUtf32String::FromAscii(strtext)); - - TStringBuf strbuftext(text); - UNIT_ASSERT(wtext == TUtf32String::FromAscii(strbuftext)); - - UNIT_ASSERT(wtext.substr(5) == TUtf32String::FromAscii(text + 5)); - - const wchar32 wideCyrillicAlphabet[] = { - 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, - 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, - 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F, - 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F, - 0x00}; - - TUtf32String strWide(wideCyrillicAlphabet); - TString strUtf8 = WideToUTF8(strWide); - - UNIT_ASSERT(strWide == TUtf32String::FromUtf8(strUtf8.c_str())); - UNIT_ASSERT(strWide == TUtf32String::FromUtf8(strUtf8)); - UNIT_ASSERT(strWide == TUtf32String::FromUtf8(TStringBuf(strUtf8))); - - // assign - - TUtf32String s1; - s1.AssignAscii("1234"); - UNIT_ASSERT(s1 == ASCIIToUTF32("1234")); - - s1.AssignUtf8(strUtf8); - UNIT_ASSERT(s1 == strWide); - - s1.AssignAscii(text); - UNIT_ASSERT(s1 == wtext); - - // append - - TUtf32String s2; - TUtf32String testAppend = strWide; - s2.AppendUtf8(strUtf8); - UNIT_ASSERT(testAppend == s2); - - testAppend += ' '; - s2.AppendAscii(" "); - UNIT_ASSERT(testAppend == s2); - - testAppend += '_'; - s2.AppendUtf8("_"); - UNIT_ASSERT(testAppend == s2); - - testAppend += wtext; - s2.AppendAscii(text); - UNIT_ASSERT(testAppend == s2); - - testAppend += wtext; - s2.AppendUtf8(text); - - UNIT_ASSERT(testAppend == s2); - } - - void TestDecodingMethodsMixedStr() { - UNIT_ASSERT(TUtf32String::FromAscii("").empty()); - UNIT_ASSERT(TUtf32String::FromAscii("abc") == ASCIIToUTF32("abc")); - - const char* text = "123kx83abcd ej)#$%ddja&%J&"; - TUtf32String wtext = ASCIIToUTF32(text); - - UNIT_ASSERT(wtext == TUtf32String::FromAscii(text)); - - TString strtext(text); - UNIT_ASSERT(wtext == TUtf32String::FromAscii(strtext)); - - TStringBuf strbuftext(text); - UNIT_ASSERT(wtext == TUtf32String::FromAscii(strbuftext)); - - UNIT_ASSERT(wtext.substr(5) == TUtf32String::FromAscii(text + 5)); - - const wchar32 cyrilicAndLatinWide[] = { - 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, - 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, - 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F, - wchar32('z'), - 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F, - wchar32('z'), - 0x00}; - - TUtf32String strWide(cyrilicAndLatinWide); - TString strUtf8 = WideToUTF8(strWide); - - UNIT_ASSERT(strWide == TUtf32String::FromUtf8(strUtf8.c_str())); - UNIT_ASSERT(strWide == TUtf32String::FromUtf8(strUtf8)); - UNIT_ASSERT(strWide == UTF8ToUTF32(strUtf8)); - UNIT_ASSERT(strWide == UTF8ToUTF32(strUtf8)); - UNIT_ASSERT(strWide == TUtf32String::FromUtf8(TStringBuf(strUtf8))); - - // assign - - TUtf32String s1; - s1.AssignAscii("1234"); - UNIT_ASSERT(s1 == ASCIIToUTF32("1234")); - - s1.AssignUtf8(strUtf8); - UNIT_ASSERT(s1 == strWide); - - s1.AssignAscii(text); - UNIT_ASSERT(s1 == wtext); - - // append - - TUtf32String s2; - TUtf32String testAppend = strWide; - s2.AppendUtf16(UTF8ToWide(strUtf8)); - UNIT_ASSERT(testAppend == s2); - - testAppend += ' '; - s2.AppendAscii(" "); - UNIT_ASSERT(testAppend == s2); - - testAppend += '_'; - s2.AppendUtf8("_"); - UNIT_ASSERT(testAppend == s2); - - testAppend += wtext; - s2.AppendAscii(text); - UNIT_ASSERT(testAppend == s2); - - testAppend += wtext; - s2.AppendUtf8(text); - - UNIT_ASSERT(testAppend == s2); - } - - void TestLetOperator() { - TUtf32String str; - - str = wchar32('X'); - UNIT_ASSERT(str == TUtf32String::FromAscii("X")); - - const TUtf32String hello = TUtf32String::FromAscii("hello"); - str = hello.data(); - UNIT_ASSERT(str == hello); - - str = hello; - UNIT_ASSERT(str == hello); - } - - void TestStringLiterals() { - TUtf32String s1 = U"hello"; - UNIT_ASSERT_VALUES_EQUAL(s1, TUtf32String::FromAscii("hello")); - - TUtf32String s2 = U"привет"; - UNIT_ASSERT_VALUES_EQUAL(s2, TUtf32String::FromUtf8("привет")); - } -}; - -UNIT_TEST_SUITE_REGISTRATION(TUtf32StringTest); - -class TStringStdTest: public TTestBase, private TStringStdTestImpl> { -public: - UNIT_TEST_SUITE(TStringStdTest); - UNIT_TEST(Constructor); - UNIT_TEST(reserve); - UNIT_TEST(short_string); - UNIT_TEST(erase); - UNIT_TEST(data); - UNIT_TEST(c_str); - UNIT_TEST(null_char_of_empty); - UNIT_TEST(null_char); - UNIT_TEST(null_char_assignment_to_subscript_of_empty); - UNIT_TEST(null_char_assignment_to_subscript_of_nonempty); -#ifndef TSTRING_IS_STD_STRING - UNIT_TEST(null_char_assignment_to_end_of_empty); - UNIT_TEST(null_char_assignment_to_end_of_nonempty); -#endif - UNIT_TEST(insert); - UNIT_TEST(resize); - UNIT_TEST(find); - UNIT_TEST(capacity); - UNIT_TEST(assign); - UNIT_TEST(copy); - UNIT_TEST(cbegin_cend); - UNIT_TEST(compare); - UNIT_TEST(find_last_of); -#if 0 - UNIT_TEST(rfind); - UNIT_TEST(replace); -#endif - UNIT_TEST(find_last_not_of); - UNIT_TEST_SUITE_END(); -}; - -UNIT_TEST_SUITE_REGISTRATION(TStringStdTest); - -class TWideStringStdTest: public TTestBase, private TStringStdTestImpl> { -public: - UNIT_TEST_SUITE(TWideStringStdTest); - UNIT_TEST(Constructor); - UNIT_TEST(reserve); - UNIT_TEST(short_string); - UNIT_TEST(erase); - UNIT_TEST(data); - UNIT_TEST(c_str); - UNIT_TEST(null_char_of_empty); - UNIT_TEST(null_char); - UNIT_TEST(null_char_assignment_to_subscript_of_empty); - UNIT_TEST(null_char_assignment_to_subscript_of_nonempty); -#ifndef TSTRING_IS_STD_STRING - UNIT_TEST(null_char_assignment_to_end_of_empty); - UNIT_TEST(null_char_assignment_to_end_of_nonempty); -#endif - UNIT_TEST(insert); - UNIT_TEST(resize); - UNIT_TEST(find); - UNIT_TEST(capacity); - UNIT_TEST(assign); - UNIT_TEST(copy); - UNIT_TEST(cbegin_cend); - UNIT_TEST(compare); - UNIT_TEST(find_last_of); -#if 0 - UNIT_TEST(rfind); - UNIT_TEST(replace); -#endif - UNIT_TEST(find_last_not_of); - UNIT_TEST_SUITE_END(); -}; - -UNIT_TEST_SUITE_REGISTRATION(TWideStringStdTest); - -Y_UNIT_TEST_SUITE(TStringConversionTest) { - Y_UNIT_TEST(ConversionToStdStringTest) { - TString abra = "cadabra"; - std::string stdAbra = abra; - UNIT_ASSERT_VALUES_EQUAL(stdAbra, "cadabra"); - } - - Y_UNIT_TEST(ConversionToStdStringViewTest) { - TString abra = "cadabra"; - std::string_view stdAbra = abra; - UNIT_ASSERT_VALUES_EQUAL(stdAbra, "cadabra"); - } -} - -Y_UNIT_TEST_SUITE(HashFunctorTests) { - Y_UNIT_TEST(TestTransparency) { - THash h; - const char* ptr = "a"; - const TStringBuf strbuf = ptr; - const TString str = ptr; - const std::string stdStr = ptr; - UNIT_ASSERT_VALUES_EQUAL(h(ptr), h(strbuf)); - UNIT_ASSERT_VALUES_EQUAL(h(ptr), h(str)); - UNIT_ASSERT_VALUES_EQUAL(h(ptr), h(stdStr)); - } -} - -#if !defined(TSTRING_IS_STD_STRING) -Y_UNIT_TEST_SUITE(StdNonConformant) { - Y_UNIT_TEST(TestEraseNoThrow) { - TString x; - - LegacyErase(x, 10); - } - - Y_UNIT_TEST(TestReplaceNoThrow) { - TString x; - - LegacyReplace(x, 0, 0, "1"); - - UNIT_ASSERT_VALUES_EQUAL(x, "1"); - - LegacyReplace(x, 10, 0, "1"); - - UNIT_ASSERT_VALUES_EQUAL(x, "1"); - } - - Y_UNIT_TEST(TestNoAlias) { - TString s = "x"; - - s.AppendNoAlias("abc", 3); - - UNIT_ASSERT_VALUES_EQUAL(s, "xabc"); - UNIT_ASSERT_VALUES_EQUAL(TString(s.c_str()), "xabc"); - } -} -#endif - -Y_UNIT_TEST_SUITE(Interop) { - static void Mutate(std::string& s) { - s += "y"; - } - - static void Mutate(TString& s) { - Mutate(MutRef(s)); - } - - Y_UNIT_TEST(TestMutate) { - TString x = "x"; - - Mutate(x); - - UNIT_ASSERT_VALUES_EQUAL(x, "xy"); - } - - static std::string TransformStd(const std::string& s) { - return s + "y"; - } - - static TString Transform(const TString& s) { - return TransformStd(s); - } - - Y_UNIT_TEST(TestTransform) { - UNIT_ASSERT_VALUES_EQUAL(Transform(TString("x")), "xy"); - } - - Y_UNIT_TEST(TestTemp) { - UNIT_ASSERT_VALUES_EQUAL("x" + ConstRef(TString("y")), "xy"); - } -} diff --git a/util/generic/string_ut.h b/util/generic/string_ut.h deleted file mode 100644 index 3a91f61dd18..00000000000 --- a/util/generic/string_ut.h +++ /dev/null @@ -1,1162 +0,0 @@ -#pragma once - -#include "string.h" - -#include - -#include - -template -struct TCharBuffer { - CharT Data[N]; - //! copies characters from string to the internal buffer without any conversion - //! @param s a string that must contain only characters less than 0x7F - explicit TCharBuffer(const char* s) { - // copy all symbols including null terminated symbol - for (size_t i = 0; i < N; ++i) { - Data[i] = s[i]; - } - } - const CharT* GetData() const { - return Data; - } -}; - -template <> -struct TCharBuffer { - const char* Data; - //! stores pointer to string - explicit TCharBuffer(const char* s) - : Data(s) - { - } - const char* GetData() const { - return Data; - } -}; - -#define DECLARE_AND_RETURN_BUFFER(s) \ - static TCharBuffer buf(s); \ - return buf.GetData(); - -//! @attention this class can process characters less than 0x7F only (the low half of ASCII table) -template -struct TTestData { - // words - const CharT* str1() { - DECLARE_AND_RETURN_BUFFER("str1"); - } - const CharT* str2() { - DECLARE_AND_RETURN_BUFFER("str2"); - } - const CharT* str__________________________________________________1() { - DECLARE_AND_RETURN_BUFFER("str 1"); - } - const CharT* str__________________________________________________2() { - DECLARE_AND_RETURN_BUFFER("str 2"); - } - const CharT* one() { - DECLARE_AND_RETURN_BUFFER("one"); - } - const CharT* two() { - DECLARE_AND_RETURN_BUFFER("two"); - } - const CharT* three() { - DECLARE_AND_RETURN_BUFFER("three"); - } - const CharT* thrii() { - DECLARE_AND_RETURN_BUFFER("thrii"); - } - const CharT* four() { - DECLARE_AND_RETURN_BUFFER("four"); - } - const CharT* enotw_() { - DECLARE_AND_RETURN_BUFFER("enotw "); - } - const CharT* foo() { - DECLARE_AND_RETURN_BUFFER("foo"); - } - const CharT* abcdef() { - DECLARE_AND_RETURN_BUFFER("abcdef"); - } - const CharT* abcdefg() { - DECLARE_AND_RETURN_BUFFER("abcdefg"); - } - const CharT* aba() { - DECLARE_AND_RETURN_BUFFER("aba"); - } - const CharT* hr() { - DECLARE_AND_RETURN_BUFFER("hr"); - } - const CharT* hrt() { - DECLARE_AND_RETURN_BUFFER("hrt"); - } - const CharT* thr() { - DECLARE_AND_RETURN_BUFFER("thr"); - } - const CharT* tw() { - DECLARE_AND_RETURN_BUFFER("tw"); - } - const CharT* ow() { - DECLARE_AND_RETURN_BUFFER("ow"); - } - const CharT* opq() { - DECLARE_AND_RETURN_BUFFER("opq"); - } - const CharT* xyz() { - DECLARE_AND_RETURN_BUFFER("xyz"); - } - const CharT* abc() { - DECLARE_AND_RETURN_BUFFER("abc"); - } - const CharT* abcd() { - DECLARE_AND_RETURN_BUFFER("abcd"); - } - const CharT* abcde() { - DECLARE_AND_RETURN_BUFFER("abcde"); - } - const CharT* abcc() { - DECLARE_AND_RETURN_BUFFER("abcc"); - } - const CharT* abce() { - DECLARE_AND_RETURN_BUFFER("abce"); - } - const CharT* qwe() { - DECLARE_AND_RETURN_BUFFER("qwe"); - } - const CharT* cd() { - DECLARE_AND_RETURN_BUFFER("cd"); - } - const CharT* cde() { - DECLARE_AND_RETURN_BUFFER("cde"); - } - const CharT* cdef() { - DECLARE_AND_RETURN_BUFFER("cdef"); - } - const CharT* cdefgh() { - DECLARE_AND_RETURN_BUFFER("cdefgh"); - } - const CharT* ehortw_() { - DECLARE_AND_RETURN_BUFFER("ehortw "); - } - const CharT* fg() { - DECLARE_AND_RETURN_BUFFER("fg"); - } - const CharT* abcdefgh() { - DECLARE_AND_RETURN_BUFFER("abcdefgh"); - } - - // phrases - const CharT* Hello_World() { - DECLARE_AND_RETURN_BUFFER("Hello World"); - } - const CharT* This_is_test_string_for_string_calls() { - DECLARE_AND_RETURN_BUFFER("This is test string for string calls"); - } - const CharT* This_is_teis_test_string_st_string_for_string_calls() { - DECLARE_AND_RETURN_BUFFER("This is teis test string st string for string calls"); - } - const CharT* This_is_test_stis_test_string_for_stringring_for_string_calls() { - DECLARE_AND_RETURN_BUFFER("This is test stis test string for stringring for string calls"); - } - const CharT* allsThis_is_test_string_for_string_calls() { - DECLARE_AND_RETURN_BUFFER("allsThis is test string for string calls"); - } - const CharT* ng_for_string_callsThis_is_test_string_for_string_calls() { - DECLARE_AND_RETURN_BUFFER("ng for string callsThis is test string for string calls"); - } - const CharT* one_two_three_one_two_three() { - DECLARE_AND_RETURN_BUFFER("one two three one two three"); - } - const CharT* test_string_for_assign() { - DECLARE_AND_RETURN_BUFFER("test string for assign"); - } - const CharT* other_test_string() { - DECLARE_AND_RETURN_BUFFER("other test string"); - } - const CharT* This_This_is_tefor_string_calls() { - DECLARE_AND_RETURN_BUFFER("This This is tefor string calls"); - } - const CharT* This_This_is_test_string_for_string_calls() { - DECLARE_AND_RETURN_BUFFER("This This is test string for string calls"); - } - - const CharT* _0123456x() { - DECLARE_AND_RETURN_BUFFER("0123456x"); - } - const CharT* _0123456xy() { - DECLARE_AND_RETURN_BUFFER("0123456xy"); - } - const CharT* _0123456xyz() { - DECLARE_AND_RETURN_BUFFER("0123456xyz"); - } - const CharT* _0123456xyzZ() { - DECLARE_AND_RETURN_BUFFER("0123456xyzZ"); - } - const CharT* _0123456xyzZ0() { - DECLARE_AND_RETURN_BUFFER("0123456xyzZ0"); - } - const CharT* abc0123456xyz() { - DECLARE_AND_RETURN_BUFFER("abc0123456xyz"); - } - const CharT* BCabc0123456xyz() { - DECLARE_AND_RETURN_BUFFER("BCabc0123456xyz"); - } - const CharT* qweBCabc0123456xyz() { - DECLARE_AND_RETURN_BUFFER("qweBCabc0123456xyz"); - } - const CharT* _1qweBCabc0123456xyz() { - DECLARE_AND_RETURN_BUFFER("1qweBCabc0123456xyz"); - } - const CharT* _01abc23456() { - DECLARE_AND_RETURN_BUFFER("01abc23456"); - } - const CharT* _01ABCabc23456() { - DECLARE_AND_RETURN_BUFFER("01ABCabc23456"); - } - const CharT* ABC() { - DECLARE_AND_RETURN_BUFFER("ABC"); - } - const CharT* ABCD() { - DECLARE_AND_RETURN_BUFFER("ABCD"); - } - const CharT* QWE() { - DECLARE_AND_RETURN_BUFFER("QWE"); - } - const CharT* XYZ() { - DECLARE_AND_RETURN_BUFFER("XYZ"); - } - const CharT* W01ABCabc23456() { - DECLARE_AND_RETURN_BUFFER("W01ABCabc23456"); - } - const CharT* abcd456() { - DECLARE_AND_RETURN_BUFFER("abcd456"); - } - const CharT* abcdABCD() { - DECLARE_AND_RETURN_BUFFER("abcdABCD"); - } - const CharT* abcdABC123() { - DECLARE_AND_RETURN_BUFFER("abcdABC123"); - } - const CharT* z123z123() { - DECLARE_AND_RETURN_BUFFER("z123z123"); - } - const CharT* ASDF1234QWER() { - DECLARE_AND_RETURN_BUFFER("ASDF1234QWER"); - } - const CharT* asdf1234qwer() { - DECLARE_AND_RETURN_BUFFER("asdf1234qwer"); - } - const CharT* asDF1234qWEr() { - DECLARE_AND_RETURN_BUFFER("asDF1234qWEr"); - } - const CharT* AsDF1234qWEr() { - DECLARE_AND_RETURN_BUFFER("AsDF1234qWEr"); - } - const CharT* Asdf1234qwer() { - DECLARE_AND_RETURN_BUFFER("Asdf1234qwer"); - } - const CharT* Asdf1234qwerWWWW() { - DECLARE_AND_RETURN_BUFFER("Asdf1234qwerWWWW"); - } - const CharT* Asdf() { - DECLARE_AND_RETURN_BUFFER("Asdf"); - } - const CharT* orig() { - DECLARE_AND_RETURN_BUFFER("orig"); - } - const CharT* fdfdsfds() { - DECLARE_AND_RETURN_BUFFER("fdfdsfds"); - } - - // numbers - const CharT* _0() { - DECLARE_AND_RETURN_BUFFER("0"); - } - const CharT* _00() { - DECLARE_AND_RETURN_BUFFER("00"); - } - const CharT* _0000000000() { - DECLARE_AND_RETURN_BUFFER("0000000000"); - } - const CharT* _00000() { - DECLARE_AND_RETURN_BUFFER("00000"); - } - const CharT* _0123() { - DECLARE_AND_RETURN_BUFFER("0123"); - } - const CharT* _01230123() { - DECLARE_AND_RETURN_BUFFER("01230123"); - } - const CharT* _01234() { - DECLARE_AND_RETURN_BUFFER("01234"); - } - const CharT* _0123401234() { - DECLARE_AND_RETURN_BUFFER("0123401234"); - } - const CharT* _012345() { - DECLARE_AND_RETURN_BUFFER("012345"); - } - const CharT* _0123456() { - DECLARE_AND_RETURN_BUFFER("0123456"); - } - const CharT* _1() { - DECLARE_AND_RETURN_BUFFER("1"); - } - const CharT* _11() { - DECLARE_AND_RETURN_BUFFER("11"); - } - const CharT* _1100000() { - DECLARE_AND_RETURN_BUFFER("1100000"); - } - const CharT* _110000034() { - DECLARE_AND_RETURN_BUFFER("110000034"); - } - const CharT* _12() { - DECLARE_AND_RETURN_BUFFER("12"); - } - const CharT* _123() { - DECLARE_AND_RETURN_BUFFER("123"); - } - const CharT* _1233321() { - DECLARE_AND_RETURN_BUFFER("1233321"); - } - const CharT* _1221() { - DECLARE_AND_RETURN_BUFFER("1221"); - } - const CharT* _1234123456() { - DECLARE_AND_RETURN_BUFFER("1234123456"); - } - const CharT* _12334444321() { - DECLARE_AND_RETURN_BUFFER("12334444321"); - } - const CharT* _123344544321() { - DECLARE_AND_RETURN_BUFFER("123344544321"); - } - const CharT* _1234567890123456789012345678901234567890() { - DECLARE_AND_RETURN_BUFFER("1234567890123456789012345678901234567890"); - } - const CharT* _1234() { - DECLARE_AND_RETURN_BUFFER("1234"); - } - const CharT* _12345() { - DECLARE_AND_RETURN_BUFFER("12345"); - } - const CharT* _123456() { - DECLARE_AND_RETURN_BUFFER("123456"); - } - const CharT* _1234567() { - DECLARE_AND_RETURN_BUFFER("1234567"); - } - const CharT* _1234561234() { - DECLARE_AND_RETURN_BUFFER("1234561234"); - } - const CharT* _12356() { - DECLARE_AND_RETURN_BUFFER("12356"); - } - const CharT* _1345656() { - DECLARE_AND_RETURN_BUFFER("1345656"); - } - const CharT* _15656() { - DECLARE_AND_RETURN_BUFFER("15656"); - } - const CharT* _17856() { - DECLARE_AND_RETURN_BUFFER("17856"); - } - const CharT* _1783456() { - DECLARE_AND_RETURN_BUFFER("1783456"); - } - const CharT* _2() { - DECLARE_AND_RETURN_BUFFER("2"); - } - const CharT* _2123456() { - DECLARE_AND_RETURN_BUFFER("2123456"); - } - const CharT* _23() { - DECLARE_AND_RETURN_BUFFER("23"); - } - const CharT* _2345() { - DECLARE_AND_RETURN_BUFFER("2345"); - } - const CharT* _3() { - DECLARE_AND_RETURN_BUFFER("3"); - } - const CharT* _345() { - DECLARE_AND_RETURN_BUFFER("345"); - } - const CharT* _3456() { - DECLARE_AND_RETURN_BUFFER("3456"); - } - const CharT* _333333() { - DECLARE_AND_RETURN_BUFFER("333333"); - } - const CharT* _389() { - DECLARE_AND_RETURN_BUFFER("389"); - } - const CharT* _4294967295() { - DECLARE_AND_RETURN_BUFFER("4294967295"); - } - const CharT* _4444() { - DECLARE_AND_RETURN_BUFFER("4444"); - } - const CharT* _5() { - DECLARE_AND_RETURN_BUFFER("5"); - } - const CharT* _6() { - DECLARE_AND_RETURN_BUFFER("6"); - } - const CharT* _6543210() { - DECLARE_AND_RETURN_BUFFER("6543210"); - } - const CharT* _7() { - DECLARE_AND_RETURN_BUFFER("7"); - } - const CharT* _78() { - DECLARE_AND_RETURN_BUFFER("78"); - } - const CharT* _2004_01_01() { - DECLARE_AND_RETURN_BUFFER("2004-01-01"); - } - const CharT* _1234562004_01_01() { - DECLARE_AND_RETURN_BUFFER("1234562004-01-01"); - } - const CharT* _0123456_12345() { - DECLARE_AND_RETURN_BUFFER("0123456_12345"); - } - - // letters - const CharT* a() { - DECLARE_AND_RETURN_BUFFER("a"); - } - const CharT* b() { - DECLARE_AND_RETURN_BUFFER("b"); - } - const CharT* c() { - DECLARE_AND_RETURN_BUFFER("c"); - } - const CharT* d() { - DECLARE_AND_RETURN_BUFFER("d"); - } - const CharT* e() { - DECLARE_AND_RETURN_BUFFER("e"); - } - const CharT* f() { - DECLARE_AND_RETURN_BUFFER("f"); - } - const CharT* h() { - DECLARE_AND_RETURN_BUFFER("h"); - } - const CharT* o() { - DECLARE_AND_RETURN_BUFFER("o"); - } - const CharT* p() { - DECLARE_AND_RETURN_BUFFER("p"); - } - const CharT* q() { - DECLARE_AND_RETURN_BUFFER("q"); - } - const CharT* r() { - DECLARE_AND_RETURN_BUFFER("r"); - } - const CharT* s() { - DECLARE_AND_RETURN_BUFFER("s"); - } - const CharT* t() { - DECLARE_AND_RETURN_BUFFER("t"); - } - const CharT* w() { - DECLARE_AND_RETURN_BUFFER("w"); - } - const CharT* x() { - DECLARE_AND_RETURN_BUFFER("x"); - } - const CharT* y() { - DECLARE_AND_RETURN_BUFFER("y"); - } - const CharT* z() { - DECLARE_AND_RETURN_BUFFER("z"); - } - const CharT* H() { - DECLARE_AND_RETURN_BUFFER("H"); - } - const CharT* I() { - DECLARE_AND_RETURN_BUFFER("I"); - } - const CharT* W() { - DECLARE_AND_RETURN_BUFFER("W"); - } - - const CharT* Space() { - DECLARE_AND_RETURN_BUFFER(" "); - } - const CharT* Empty() { - DECLARE_AND_RETURN_BUFFER(""); - } - - size_t HashOf_0123456() { - return 0; - } -}; - -template <> -size_t TTestData::HashOf_0123456() { - return 1229863857ul; -} - -template <> -size_t TTestData::HashOf_0123456() { - return 2775195331ul; -} - -template -class TStringTestImpl { -protected: - using char_type = typename TStringType::char_type; - using traits_type = typename TStringType::traits_type; - - TTestData Data; - -public: - void TestMaxSize() { - const size_t badMaxVal = TStringType{}.max_size() + 1; - - TStringType s; - UNIT_CHECK_GENERATED_EXCEPTION(s.reserve(badMaxVal), std::length_error); - } - - void TestConstructors() { - TStringType s0(nullptr); - UNIT_ASSERT(s0.size() == 0); - UNIT_ASSERT_EQUAL(s0, TStringType()); - - TStringType s; - TStringType s1(*Data._0()); - TStringType s2(Data._0()); - UNIT_ASSERT(s1 == s2); - - TStringType fromZero(0); - UNIT_ASSERT_VALUES_EQUAL(fromZero.size(), 0u); - - TStringType fromChar(char_type('a')); - UNIT_ASSERT_VALUES_EQUAL(fromChar.size(), 1u); - UNIT_ASSERT_VALUES_EQUAL(fromChar[0], char_type('a')); - -#ifndef TSTRING_IS_STD_STRING - TStringType s3 = TStringType::Uninitialized(10); - UNIT_ASSERT(s3.size() == 10); -#endif - - TStringType s4(Data._0123456(), 1, 3); - UNIT_ASSERT(s4 == Data._123()); - - TStringType s5(5, *Data._0()); - UNIT_ASSERT(s5 == Data._00000()); - - TStringType s6(Data._0123456()); - UNIT_ASSERT(s6 == Data._0123456()); - TStringType s7(s6); - UNIT_ASSERT(s7 == s6); -#ifndef TSTRING_IS_STD_STRING - UNIT_ASSERT(s7.c_str() == s6.c_str()); -#endif - - TStringType s8(s7, 1, 3); - UNIT_ASSERT(s8 == Data._123()); - - TStringType s9(*Data._1()); - UNIT_ASSERT(s9 == Data._1()); - - TStringType s10(Reserve(100)); - UNIT_ASSERT(s10.empty()); - UNIT_ASSERT(s10.capacity() >= 100); - } - - void TestReplace() { - TStringType s(Data._0123456()); - UNIT_ASSERT(s.copy() == Data._0123456()); - - // append family - s.append(Data.x()); - UNIT_ASSERT(s == Data._0123456x()); - -#ifdef TSTRING_IS_STD_STRING - s.append(Data.xyz() + 1, 1); -#else - s.append(Data.xyz(), 1, 1); -#endif - UNIT_ASSERT(s == Data._0123456xy()); - - s.append(TStringType(Data.z())); - UNIT_ASSERT(s == Data._0123456xyz()); - - s.append(TStringType(Data.XYZ()), 2, 1); - UNIT_ASSERT(s == Data._0123456xyzZ()); - - s.append(*Data._0()); - UNIT_ASSERT(s == Data._0123456xyzZ0()); - - // prepend family - s = Data._0123456xyz(); - s.prepend(TStringType(Data.abc())); - UNIT_ASSERT(s == Data.abc0123456xyz()); - - s.prepend(TStringType(Data.ABC()), 1, 2); - UNIT_ASSERT(s == Data.BCabc0123456xyz()); - - s.prepend(Data.qwe()); - UNIT_ASSERT(s == Data.qweBCabc0123456xyz()); - - s.prepend(*Data._1()); - UNIT_ASSERT(s == Data._1qweBCabc0123456xyz()); - - // substr - s = Data.abc0123456xyz(); - s = s.substr(3, 7); - UNIT_ASSERT(s == Data._0123456()); - - // insert family - s.insert(2, Data.abc()); - UNIT_ASSERT(s == Data._01abc23456()); - - s.insert(2, TStringType(Data.ABC())); - UNIT_ASSERT(s == Data._01ABCabc23456()); - - s.insert(0, TStringType(Data.QWE()), 1, 1); - UNIT_ASSERT(s == Data.W01ABCabc23456()); - - // replace family - s = Data._01abc23456(); - s.replace(0, 7, Data.abcd()); - UNIT_ASSERT(s == Data.abcd456()); - - s.replace(4, 3, TStringType(Data.ABCD())); - UNIT_ASSERT(s == Data.abcdABCD()); - - s.replace(7, 10, TStringType(Data._01234()), 1, 3); - UNIT_ASSERT(s == Data.abcdABC123()); - UNIT_ASSERT(Data.abcdABC123() == s); - - // remove, erase - s.remove(4); - UNIT_ASSERT(s == Data.abcd()); - s.erase(3); - UNIT_ASSERT(s == Data.abc()); - - // Read access - s = Data._012345(); - UNIT_ASSERT(s.at(1) == *Data._1()); - UNIT_ASSERT(s[1] == *Data._1()); - UNIT_ASSERT(s.at(s.size()) == 0); - UNIT_ASSERT(s[s.size()] == 0); - } - -#ifndef TSTRING_IS_STD_STRING - void TestRefCount() { - using TStr = TStringType; - - struct TestStroka: public TStr { - using TStr::TStr; - // un-protect - using TStr::RefCount; - }; - - TestStroka s1(Data.orig()); - UNIT_ASSERT_EQUAL(s1.RefCount() == 1, true); - TestStroka s2(s1); - UNIT_ASSERT_EQUAL(s1.RefCount() == 2, true); - UNIT_ASSERT_EQUAL(s2.RefCount() == 2, true); - UNIT_ASSERT_EQUAL(s1.c_str() == s2.c_str(), true); // the same pointer - char_type* beg = s2.begin(); - UNIT_ASSERT_EQUAL(s1 == beg, true); - UNIT_ASSERT_EQUAL(s1.RefCount() == 1, true); - UNIT_ASSERT_EQUAL(s2.RefCount() == 1, true); - UNIT_ASSERT_EQUAL(s1.c_str() == s2.c_str(), false); - } -#endif - - // Find family - - void TestFind() { - const TStringType s(Data._0123456_12345()); - const TStringType s2(Data._0123()); - - UNIT_ASSERT(s.find(Data._345()) == 3); - UNIT_ASSERT(s.find(Data._345(), 5) == 10); - - UNIT_ASSERT(s.find(Data._345(), 20) == TStringType::npos); - UNIT_ASSERT(s.find(*Data._3()) == 3); - UNIT_ASSERT(s.find(TStringType(Data._345())) == 3); - UNIT_ASSERT(s.find(TStringType(Data._345()), 2) == 3); - - UNIT_ASSERT(s.find_first_of(TStringType(Data._389())) == 3); - UNIT_ASSERT(s.find_first_of(Data._389()) == 3); - UNIT_ASSERT(s.find_first_of(Data._389(), s.size()) == TStringType::npos); - UNIT_ASSERT(s.find_first_not_of(Data._123()) == 0); - UNIT_ASSERT(s.find_first_of('6') == 6); - UNIT_ASSERT(s.find_first_of('1', 2) == 8); - UNIT_ASSERT(s.find_first_not_of('0') == 1); - UNIT_ASSERT(s.find_first_not_of('1', 1) == 2); - - const TStringType rs = Data._0123401234(); - UNIT_ASSERT(rs.rfind(*Data._3()) == 8); - - const TStringType empty; - UNIT_ASSERT(empty.find(empty) == 0); - UNIT_ASSERT(s.find(empty, 0) == 0); - UNIT_ASSERT(s.find(empty, 1) == 1); - UNIT_ASSERT(s.find(empty, s.length()) == s.length()); - UNIT_ASSERT(s.find(empty, s.length() + 1) == TStringType::npos); - - UNIT_ASSERT(s.rfind(empty) == s.length()); - UNIT_ASSERT(empty.rfind(empty) == 0); - UNIT_ASSERT(empty.rfind(s) == TStringType::npos); - - UNIT_ASSERT(s2.rfind(s) == TStringType::npos); - UNIT_ASSERT(s.rfind(s2) == 0); - UNIT_ASSERT(s.rfind(TStringType(Data._345())) == 10); - UNIT_ASSERT(s.rfind(TStringType(Data._345()), 13) == 10); - UNIT_ASSERT(s.rfind(TStringType(Data._345()), 10) == 10); - UNIT_ASSERT(s.rfind(TStringType(Data._345()), 9) == 3); - UNIT_ASSERT(s.rfind(TStringType(Data._345()), 6) == 3); - UNIT_ASSERT(s.rfind(TStringType(Data._345()), 3) == 3); - UNIT_ASSERT(s.rfind(TStringType(Data._345()), 2) == TStringType::npos); - } - - void TestContains() { - const TStringType s(Data._0123456_12345()); - const TStringType s2(Data._0123()); - - UNIT_ASSERT(s.Contains(Data._345())); - UNIT_ASSERT(!s2.Contains(Data._345())); - - UNIT_ASSERT(s.Contains('1')); - UNIT_ASSERT(!s.Contains('*')); - - TStringType empty; - UNIT_ASSERT(s.Contains(empty)); - UNIT_ASSERT(!empty.Contains(s)); - UNIT_ASSERT(empty.Contains(empty)); - UNIT_ASSERT(s.Contains(empty, s.length())); - } - - // Operators - - void TestOperators() { - TStringType s(Data._0123456()); - - // operator += - s += TStringType(Data.x()); - UNIT_ASSERT(s == Data._0123456x()); - - s += Data.y(); - UNIT_ASSERT(s == Data._0123456xy()); - - s += *Data.z(); - UNIT_ASSERT(s == Data._0123456xyz()); - - // operator + - s = Data._0123456(); - s = s + TStringType(Data.x()); - UNIT_ASSERT(s == Data._0123456x()); - - s = s + Data.y(); - UNIT_ASSERT(s == Data._0123456xy()); - - s = s + *Data.z(); - UNIT_ASSERT(s == Data._0123456xyz()); - - // operator != - s = Data._012345(); - UNIT_ASSERT(s != TStringType(Data.xyz())); - UNIT_ASSERT(s != Data.xyz()); - UNIT_ASSERT(Data.xyz() != s); - - // operator < - UNIT_ASSERT_EQUAL(s < TStringType(Data.xyz()), true); - UNIT_ASSERT_EQUAL(s < Data.xyz(), true); - UNIT_ASSERT_EQUAL(Data.xyz() < s, false); - - // operator <= - UNIT_ASSERT_EQUAL(s <= TStringType(Data.xyz()), true); - UNIT_ASSERT_EQUAL(s <= Data.xyz(), true); - UNIT_ASSERT_EQUAL(Data.xyz() <= s, false); - - // operator > - UNIT_ASSERT_EQUAL(s > TStringType(Data.xyz()), false); - UNIT_ASSERT_EQUAL(s > Data.xyz(), false); - UNIT_ASSERT_EQUAL(Data.xyz() > s, true); - - // operator >= - UNIT_ASSERT_EQUAL(s >= TStringType(Data.xyz()), false); - UNIT_ASSERT_EQUAL(s >= Data.xyz(), false); - UNIT_ASSERT_EQUAL(Data.xyz() >= s, true); - } - - void TestOperatorsCI() { - TStringType s(Data.ABCD()); - UNIT_ASSERT(s > Data.abc0123456xyz()); - UNIT_ASSERT(s == Data.abcd()); - - using TCIStringBuf = TBasicStringBuf; - - UNIT_ASSERT(s > TCIStringBuf(Data.abc0123456xyz())); - UNIT_ASSERT(TCIStringBuf(Data.abc0123456xyz()) < s); - UNIT_ASSERT(s == TCIStringBuf(Data.abcd())); - } - - void TestMulOperators() { - { - TStringType s(Data._0()); - s *= 10; - UNIT_ASSERT_EQUAL(s, TStringType(Data._0000000000())); - } - { - TStringType s = TStringType(Data._0()) * 2; - UNIT_ASSERT_EQUAL(s, TStringType(Data._00())); - } - } - - // Test any other functions - - void TestFuncs() { - TStringType s(Data._0123456()); - UNIT_ASSERT(s.c_str() == s.data()); - - // length() - UNIT_ASSERT(s.length() == s.size()); - UNIT_ASSERT(s.length() == traits_type::length(s.data())); - - // is_null() - TStringType s1(Data.Empty()); - UNIT_ASSERT(s1.is_null() == true); - UNIT_ASSERT(s1.is_null() == s1.empty()); - UNIT_ASSERT(s1.is_null() == !s1); - - TStringType s2(s); - UNIT_ASSERT(s2 == s); - - // reverse() - ReverseInPlace(s2); - UNIT_ASSERT(s2 == Data._6543210()); - - // to_upper() - s2 = Data.asdf1234qwer(); - s2.to_upper(); - UNIT_ASSERT(s2 == Data.ASDF1234QWER()); - - // to_lower() - s2.to_lower(); - UNIT_ASSERT(s2 == Data.asdf1234qwer()); - - // to_title() - s2 = Data.asDF1234qWEr(); - s2.to_title(); - UNIT_ASSERT(s2 == Data.Asdf1234qwer()); - - s2 = Data.AsDF1234qWEr(); - s2.to_title(); - UNIT_ASSERT(s2 == Data.Asdf1234qwer()); - - // Friend functions - s2 = Data.asdf1234qwer(); - TStringType s3 = to_upper(s2); - UNIT_ASSERT(s3 == Data.ASDF1234QWER()); - s3 = to_lower(s2); - UNIT_ASSERT(s3 == Data.asdf1234qwer()); - s3 = to_title(s2); - UNIT_ASSERT(s3 == Data.Asdf1234qwer()); - s2 = s3; - - // resize family - s2.resize(s2.size()); // without length change - UNIT_ASSERT(s2 == Data.Asdf1234qwer()); - - s2.resize(s2.size() + 4, *Data.W()); - UNIT_ASSERT(s2 == Data.Asdf1234qwerWWWW()); - - s2.resize(4); - UNIT_ASSERT(s2 == Data.Asdf()); - - // assign family - s2 = Data.asdf1234qwer(); - s2.assign(s, 1, 3); - UNIT_ASSERT(s2 == Data._123()); - - s2.assign(Data._0123456(), 4); - UNIT_ASSERT(s2 == Data._0123()); - - s2.assign('1'); - UNIT_ASSERT(s2 == Data._1()); - - s2.assign(Data._0123456()); - UNIT_ASSERT(s2 == Data._0123456()); - - // hash() - TStringType sS = s2; // type 'TStringType' is used as is - - ComputeHash(sS); /*size_t hash_val = sS.hash(); - - try { - //UNIT_ASSERT(hash_val == Data.HashOf_0123456()); - } catch (...) { - Cerr << hash_val << Endl; - throw; - }*/ - - s2.assign(Data._0123456(), 2, 2); - UNIT_ASSERT(s2 == Data._23()); - - // s2.reserve(); - - TStringType s5(Data.abcde()); - s5.clear(); - UNIT_ASSERT(s5 == Data.Empty()); - } - - void TestUtils() { - TStringType s; - s = Data._01230123(); - TStringType from = Data._0(); - TStringType to = Data.z(); - - SubstGlobal(s, from, to); - UNIT_ASSERT(s == Data.z123z123()); - } - - void TestEmpty() { - TStringType s; - s = Data._2(); - s = TStringType(Data.fdfdsfds(), (size_t)0, (size_t)0); - UNIT_ASSERT(s.empty()); - } - - void TestJoin() { - UNIT_ASSERT_EQUAL(TStringType::Join(Data._12(), Data._3456()), Data._123456()); - UNIT_ASSERT_EQUAL(TStringType::Join(Data._12(), TStringType(Data._3456())), Data._123456()); - UNIT_ASSERT_EQUAL(TStringType::Join(TStringType(Data._12()), Data._3456()), Data._123456()); - UNIT_ASSERT_EQUAL(TStringType::Join(Data._12(), Data._345(), Data._6()), Data._123456()); - UNIT_ASSERT_EQUAL(TStringType::Join(Data._12(), TStringType(Data._345()), Data._6()), Data._123456()); - UNIT_ASSERT_EQUAL(TStringType::Join(TStringType(Data._12()), TStringType(Data._345()), Data._6()), Data._123456()); - UNIT_ASSERT_EQUAL(TStringType::Join(TStringType(Data.a()), Data.b(), TStringType(Data.cd()), TStringType(Data.e()), Data.fg(), TStringType(Data.h())), Data.abcdefgh()); - UNIT_ASSERT_EQUAL(TStringType::Join(TStringType(Data.a()), static_cast('b'), TStringType(Data.cd()), TStringType(Data.e()), Data.fg(), TStringType(Data.h())), Data.abcdefgh()); - } - - void TestCopy() { - TStringType s(Data.abcd()); - TStringType c = s.copy(); - - UNIT_ASSERT_EQUAL(s, c); - UNIT_ASSERT(s.end() != c.end()); - } - - void TestStrCpy() { - { - TStringType s(Data.abcd()); - char_type data[5]; - - data[4] = 1; - - s.strcpy(data, 4); - - UNIT_ASSERT_EQUAL(data[0], *Data.a()); - UNIT_ASSERT_EQUAL(data[1], *Data.b()); - UNIT_ASSERT_EQUAL(data[2], *Data.c()); - UNIT_ASSERT_EQUAL(data[3], 0); - UNIT_ASSERT_EQUAL(data[4], 1); - } - - { - TStringType s(Data.abcd()); - char_type data[5]; - - s.strcpy(data, 5); - - UNIT_ASSERT_EQUAL(data[0], *Data.a()); - UNIT_ASSERT_EQUAL(data[1], *Data.b()); - UNIT_ASSERT_EQUAL(data[2], *Data.c()); - UNIT_ASSERT_EQUAL(data[3], *Data.d()); - UNIT_ASSERT_EQUAL(data[4], 0); - } - } - - void TestPrefixSuffix() { - const TStringType emptyStr; - UNIT_ASSERT_EQUAL(emptyStr.StartsWith('x'), false); - UNIT_ASSERT_EQUAL(emptyStr.EndsWith('x'), false); - UNIT_ASSERT_EQUAL(emptyStr.StartsWith(0), false); - UNIT_ASSERT_EQUAL(emptyStr.EndsWith(0), false); - UNIT_ASSERT_EQUAL(emptyStr.StartsWith(emptyStr), true); - UNIT_ASSERT_EQUAL(emptyStr.EndsWith(emptyStr), true); - - const char_type chars[] = {'h', 'e', 'l', 'l', 'o', 0}; - const TStringType str(chars); - UNIT_ASSERT_EQUAL(str.StartsWith('h'), true); - UNIT_ASSERT_EQUAL(str.StartsWith('o'), false); - UNIT_ASSERT_EQUAL(str.EndsWith('o'), true); - UNIT_ASSERT_EQUAL(str.EndsWith('h'), false); - UNIT_ASSERT_EQUAL(str.StartsWith(emptyStr), true); - UNIT_ASSERT_EQUAL(str.EndsWith(emptyStr), true); - } - -#ifndef TSTRING_IS_STD_STRING - void TestCharRef() { - const char_type abc[] = {'a', 'b', 'c', 0}; - const char_type bbc[] = {'b', 'b', 'c', 0}; - const char_type cbc[] = {'c', 'b', 'c', 0}; - - TStringType s0 = abc; - TStringType s1 = s0; - - UNIT_ASSERT(!s0.IsDetached()); - UNIT_ASSERT(!s1.IsDetached()); - - /* Read access shouldn't detach. */ - UNIT_ASSERT_VALUES_EQUAL(s0[0], (ui8)'a'); - UNIT_ASSERT(!s0.IsDetached()); - UNIT_ASSERT(!s1.IsDetached()); - - /* Writing should detach. */ - s1[0] = (ui8)'b'; - TStringType s2 = s0; - s0[0] = (ui8)'c'; - - UNIT_ASSERT_VALUES_EQUAL(s0, cbc); - UNIT_ASSERT_VALUES_EQUAL(s1, bbc); - UNIT_ASSERT_VALUES_EQUAL(s2, abc); - UNIT_ASSERT(s0.IsDetached()); - UNIT_ASSERT(s1.IsDetached()); - UNIT_ASSERT(s2.IsDetached()); - - /* Accessing null terminator is OK. Note that writing into it is UB. */ - UNIT_ASSERT_VALUES_EQUAL(s0[3], (ui8)'\0'); - UNIT_ASSERT_VALUES_EQUAL(s1[3], (ui8)'\0'); - UNIT_ASSERT_VALUES_EQUAL(s2[3], (ui8)'\0'); - - /* Assignment one char reference to another results in modification of underlying character */ - { - const char_type dark_eyed[] = {'d', 'a', 'r', 'k', '-', 'e', 'y', 'e', 'd', 0}; - const char_type red_eared[] = {'r', 'e', 'd', '-', 'e', 'a', 'r', 'e', 'd', 0}; - TStringType s0 = dark_eyed; - TStringType s1 = TStringType::Uninitialized(s0.size()); - for (size_t i = 0; i < s1.size(); ++i) { - const size_t j = (3u * (i + 1u) ^ 1u) % s0.size(); - s1[i] = s0[j]; - } - UNIT_ASSERT_VALUES_EQUAL(s1, red_eared); - } - } -#endif - - void TestBack() { - const char_type chars[] = {'f', 'o', 'o', 0}; - - TStringType str = chars; - const TStringType constStr = str; - - UNIT_ASSERT_VALUES_EQUAL(constStr.back(), (ui8)'o'); - UNIT_ASSERT_VALUES_EQUAL(str.back(), (ui8)'o'); - - str.back() = 'r'; - UNIT_ASSERT_VALUES_EQUAL(constStr.back(), (ui8)'o'); - UNIT_ASSERT_VALUES_EQUAL(str.back(), (ui8)'r'); - } - - void TestFront() { - const char_type chars[] = {'f', 'o', 'o', 0}; - - TStringType str = chars; - const TStringType constStr = str; - - UNIT_ASSERT_VALUES_EQUAL(constStr.front(), (ui8)'f'); - UNIT_ASSERT_VALUES_EQUAL(str.front(), (ui8)'f'); - - str.front() = 'r'; - UNIT_ASSERT_VALUES_EQUAL(constStr.front(), (ui8)'f'); - UNIT_ASSERT_VALUES_EQUAL(str.front(), (ui8)'r'); - } - - void TestIterators() { - const char_type chars[] = {'f', 'o', 0}; - - TStringType str = chars; - const TStringType constStr = str; - - typename TStringType::const_iterator itBegin = str.begin(); - typename TStringType::const_iterator itEnd = str.end(); - typename TStringType::const_iterator citBegin = constStr.begin(); - typename TStringType::const_iterator citEnd = constStr.end(); - - UNIT_ASSERT_VALUES_EQUAL(*itBegin, (ui8)'f'); - UNIT_ASSERT_VALUES_EQUAL(*citBegin, (ui8)'f'); - - str.front() = 'r'; - UNIT_ASSERT_VALUES_EQUAL(*itBegin, (ui8)'r'); - UNIT_ASSERT_VALUES_EQUAL(*citBegin, (ui8)'f'); - - UNIT_ASSERT_VALUES_EQUAL(2, itEnd - itBegin); - UNIT_ASSERT_VALUES_EQUAL(2, citEnd - citBegin); - - UNIT_ASSERT_VALUES_EQUAL(*(++itBegin), (ui8)'o'); - UNIT_ASSERT_VALUES_EQUAL(*(++citBegin), (ui8)'o'); - - UNIT_ASSERT_VALUES_EQUAL(*(--itBegin), (ui8)'r'); - UNIT_ASSERT_VALUES_EQUAL(*(--citBegin), (ui8)'f'); - - UNIT_ASSERT_VALUES_EQUAL(*(itBegin++), (ui8)'r'); - UNIT_ASSERT_VALUES_EQUAL(*(citBegin++), (ui8)'f'); - UNIT_ASSERT_VALUES_EQUAL(*itBegin, (ui8)'o'); - UNIT_ASSERT_VALUES_EQUAL(*citBegin, (ui8)'o'); - - UNIT_ASSERT_VALUES_EQUAL(*(itBegin--), (ui8)'o'); - UNIT_ASSERT_VALUES_EQUAL(*(citBegin--), (ui8)'o'); - UNIT_ASSERT_VALUES_EQUAL(*itBegin, (ui8)'r'); - UNIT_ASSERT_VALUES_EQUAL(*citBegin, (ui8)'f'); - } - - void TestReverseIterators() { - const char_type chars[] = {'f', 'o', 0}; - - TStringType str = chars; - const TStringType constStr = str; - - typename TStringType::reverse_iterator ritBegin = str.rbegin(); - typename TStringType::reverse_iterator ritEnd = str.rend(); - typename TStringType::const_reverse_iterator critBegin = constStr.rbegin(); - typename TStringType::const_reverse_iterator critEnd = constStr.rend(); - - UNIT_ASSERT_VALUES_EQUAL(*ritBegin, (ui8)'o'); - UNIT_ASSERT_VALUES_EQUAL(*critBegin, (ui8)'o'); - - str.back() = (ui8)'r'; - UNIT_ASSERT_VALUES_EQUAL(*ritBegin, (ui8)'r'); - UNIT_ASSERT_VALUES_EQUAL(*critBegin, (ui8)'o'); - - UNIT_ASSERT_VALUES_EQUAL(2, ritEnd - ritBegin); - UNIT_ASSERT_VALUES_EQUAL(2, critEnd - critBegin); - - UNIT_ASSERT_VALUES_EQUAL(*(++ritBegin), (ui8)'f'); - UNIT_ASSERT_VALUES_EQUAL(*(++critBegin), (ui8)'f'); - - UNIT_ASSERT_VALUES_EQUAL(*(--ritBegin), (ui8)'r'); - UNIT_ASSERT_VALUES_EQUAL(*(--critBegin), (ui8)'o'); - - UNIT_ASSERT_VALUES_EQUAL(*(ritBegin++), (ui8)'r'); - UNIT_ASSERT_VALUES_EQUAL(*(critBegin++), (ui8)'o'); - UNIT_ASSERT_VALUES_EQUAL(*ritBegin, (ui8)'f'); - UNIT_ASSERT_VALUES_EQUAL(*critBegin, (ui8)'f'); - - UNIT_ASSERT_VALUES_EQUAL(*(ritBegin--), (ui8)'f'); - UNIT_ASSERT_VALUES_EQUAL(*(critBegin--), (ui8)'f'); - UNIT_ASSERT_VALUES_EQUAL(*ritBegin, (ui8)'r'); - UNIT_ASSERT_VALUES_EQUAL(*critBegin, (ui8)'o'); - - *ritBegin = (ui8)'e'; - UNIT_ASSERT_VALUES_EQUAL(*ritBegin, (ui8)'e'); - - str = chars; - auto it = std::find_if( - str.rbegin(), str.rend(), - [](char_type c) { return c == 'o'; }); - UNIT_ASSERT_EQUAL(it, str.rbegin()); - } -}; diff --git a/util/generic/string_ut.pyx b/util/generic/string_ut.pyx deleted file mode 100644 index 36d5c6de242..00000000000 --- a/util/generic/string_ut.pyx +++ /dev/null @@ -1,225 +0,0 @@ -# cython: c_string_type=str, c_string_encoding=utf8 - -from libcpp.string cimport string as std_string -from util.generic.string cimport TString, npos - -import pytest -import unittest - -import sys - - -class TestStroka(unittest.TestCase): - def test_unicode(self): - cdef TString x = "привет" - self.assertEqual(x, "привет") - - - def test_ctor1(self): - cdef TString tmp = TString() - cdef TString tmp2 = TString(tmp) - self.assertEqual(tmp2, "") - - def test_ctor2(self): - cdef std_string tmp = b"hello" - cdef TString tmp2 = TString(tmp) - self.assertEqual(tmp2, "hello") - - def test_ctor3(self): - cdef TString tmp = b"hello" - cdef TString tmp2 = TString(tmp, 0, 4) - self.assertEqual(tmp2, "hell") - - def test_ctor4(self): - cdef TString tmp = TString(b"hello") - self.assertEqual(tmp, "hello") - - def test_ctor5(self): - cdef TString tmp = TString(b"hello", 4) - self.assertEqual(tmp, "hell") - - def test_ctor6(self): - cdef TString tmp = TString(b"hello", 1, 3) - self.assertEqual(tmp, "ell") - - def test_ctor7(self): - cdef TString tmp = TString(3, 'x') - self.assertEqual(tmp, "xxx") - - def test_ctor8(self): - cdef bytes tmp = b"hello" - cdef TString tmp2 = TString(tmp, tmp + 4) - self.assertEqual(tmp2, "hell") - - def test_compare(self): - cdef TString tmp1 = b"abacab" - cdef TString tmp2 = b"abacab" - cdef TString tmp3 = b"abacac" - - self.assertTrue(tmp1.compare(tmp2) == 0) - self.assertTrue(tmp1.compare(tmp3) < 0) - self.assertTrue(tmp3.compare(tmp1) > 0) - - self.assertTrue(tmp1 == tmp2) - self.assertTrue(tmp1 != tmp3) - - self.assertTrue(tmp1 < tmp3) - self.assertTrue(tmp1 <= tmp3) - - self.assertTrue(tmp3 > tmp1) - self.assertTrue(tmp3 >= tmp1) - - def test_operator_assign(self): - cdef TString tmp = b"hello" - cdef TString tmp2 = tmp - self.assertEqual(tmp2, "hello") - - def test_operator_plus(self): - cdef TString tmp = TString(b"hello ") + TString(b"world") - self.assertEqual(tmp, "hello world") - - def test_c_str(self): - cdef TString tmp = b"hello" - if sys.version_info.major == 2: - self.assertEqual(bytes(tmp.c_str()), b"hello") - else: - self.assertEqual(bytes(tmp.c_str(), 'utf8'), b"hello") - - def test_length(self): - cdef TString tmp = b"hello" - self.assertEqual(tmp.size(), tmp.length()) - - def test_index(self): - cdef TString tmp = b"hello" - - self.assertEqual(tmp[0], b'h') - self.assertEqual(tmp.at(0), b'h') - - self.assertEqual(tmp[4], b'o') - self.assertEqual(tmp.at(4), b'o') - - # Actually, TString::at() is noexcept - # with pytest.raises(IndexError): - # tmp.at(100) - - def test_append(self): - cdef TString tmp - cdef TString tmp2 = b"fuu" - - tmp.append(tmp2) - self.assertEqual(tmp, "fuu") - - tmp.append(tmp2, 1, 2) - self.assertEqual(tmp, "fuuuu") - - tmp.append("ll ") - self.assertEqual(tmp, "fuuuull ") - - tmp.append("of greatness", 4) - self.assertEqual(tmp, "fuuuull of g") - - tmp.append(2, b'o') - self.assertEqual(tmp, "fuuuull of goo") - - tmp.push_back(b'z') - self.assertEqual(tmp, "fuuuull of gooz") - - def test_assign(self): - cdef TString tmp - - tmp.assign(b"one") - self.assertEqual(tmp, "one") - - tmp.assign(b"two hundred", 0, 3) - self.assertEqual(tmp, "two") - - tmp.assign(b"three") - self.assertEqual(tmp, "three") - - tmp.assign(b"three fiddy", 5) - self.assertEqual(tmp, "three") - - def test_insert(self): - cdef TString tmp - - tmp = b"xx" - tmp.insert(1, b"foo") - self.assertEqual(tmp, "xfoox") - - tmp = b"xx" - tmp.insert(1, b"haxor", 1, 3) - self.assertEqual(tmp, "xaxox") - - tmp = b"xx" - tmp.insert(1, b"foo") - self.assertEqual(tmp, "xfoox") - - tmp = b"xx" - tmp.insert(1, b"foozzy", 3) - self.assertEqual(tmp, "xfoox") - - tmp = b"xx" - tmp.insert(1, 2, b'u') - self.assertEqual(tmp, "xuux") - - def test_copy(self): - cdef char buf[16] - cdef TString tmp = b"hello" - tmp.copy(buf, 5, 0) - self.assertEqual(buf[:5], "hello") - - def test_find(self): - cdef TString haystack = b"whole lotta bytes" - cdef TString needle = "hole" - - self.assertEqual(haystack.find(needle), 1) - self.assertEqual(haystack.find(needle, 3), npos) - - self.assertEqual(haystack.find(b'h'), 1) - self.assertEqual(haystack.find(b'h', 3), npos) - - def test_rfind(self): - cdef TString haystack = b"whole lotta bytes" - cdef TString needle = b"hole" - - self.assertEqual(haystack.rfind(needle), 1) - self.assertEqual(haystack.rfind(needle, 0), npos) - - self.assertEqual(haystack.rfind(b'h'), 1) - self.assertEqual(haystack.rfind(b'h', 0), npos) - - def test_find_first_of(self): - cdef TString haystack = b"whole lotta bytes" - cdef TString cset = b"hxz" - - self.assertEqual(haystack.find_first_of(b'h'), 1) - self.assertEqual(haystack.find_first_of(b'h', 3), npos) - - self.assertEqual(haystack.find_first_of(cset), 1) - self.assertEqual(haystack.find_first_of(cset, 3), npos) - - def test_first_not_of(self): - cdef TString haystack = b"whole lotta bytes" - cdef TString cset = b"wxz" - - self.assertEqual(haystack.find_first_not_of(b'w'), 1) - self.assertEqual(haystack.find_first_not_of(b'w', 3), 3) - - self.assertEqual(haystack.find_first_not_of(cset), 1) - self.assertEqual(haystack.find_first_not_of(cset, 3), 3) - - def test_find_last_of(self): - cdef TString haystack = b"whole lotta bytes" - cdef TString cset = b"hxz" - - self.assertEqual(haystack.find_last_of(b'h'), 1) - self.assertEqual(haystack.find_last_of(b'h', 0), npos) - - self.assertEqual(haystack.find_last_of(cset), 1) - self.assertEqual(haystack.find_last_of(cset, 0), npos) - - def test_substr(self): - cdef TString tmp = b"foobar" - - self.assertEqual(tmp.substr(1), "oobar") - self.assertEqual(tmp.substr(1, 4), "ooba") diff --git a/util/generic/typetraits.h b/util/generic/typetraits.h index 24386bd277e..7346bbc572c 100644 --- a/util/generic/typetraits.h +++ b/util/generic/typetraits.h @@ -7,6 +7,7 @@ #include #include #include +#include #if _LIBCPP_STD_VER >= 17 template diff --git a/util/generic/typetraits_ut.cpp b/util/generic/typetraits_ut.cpp index fa35c2f0a2e..fe96e3a42b3 100644 --- a/util/generic/typetraits_ut.cpp +++ b/util/generic/typetraits_ut.cpp @@ -113,7 +113,7 @@ Y_UNIT_TEST_SUITE(TTypeTraitsTest) { } Y_UNIT_TEST(TestClass) { - UNIT_ASSERT(std::is_class::value); + UNIT_ASSERT(std::is_class::value); UNIT_ASSERT(!std::is_class::value); UNIT_ASSERT(!std::is_class::value); UNIT_ASSERT(!std::is_class::value); @@ -461,9 +461,6 @@ static_assert(!TIsSpecializationOf>::value, ""); static_assert(TIsTemplateBaseOf>::value); static_assert(TIsTemplateBaseOf>::value); static_assert(TIsTemplateBaseOf::value); -static_assert(TIsTemplateBaseOf::value); -static_assert(TIsTemplateBaseOf::value); -static_assert(TIsTemplateBaseOf::value); // test for TIsIterable static_assert(TIsIterable>::value, ""); diff --git a/util/generic/utility_ut.cpp b/util/generic/utility_ut.cpp index b4b8739794b..316f0a3e1f7 100644 --- a/util/generic/utility_ut.cpp +++ b/util/generic/utility_ut.cpp @@ -24,7 +24,7 @@ class TTest { }; struct TUnorderedTag { - TStringBuf Tag; + std::string_view Tag; }; static bool operator<(const TUnorderedTag, const TUnorderedTag) { diff --git a/util/generic/vector.cpp b/util/generic/vector.cpp deleted file mode 100644 index 7da6cc5e869..00000000000 --- a/util/generic/vector.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "vector.h" diff --git a/util/generic/vector.h b/util/generic/vector.h deleted file mode 100644 index 580621fb1fa..00000000000 --- a/util/generic/vector.h +++ /dev/null @@ -1,131 +0,0 @@ -#pragma once - -#include "fwd.h" -#include "reserve.h" - -#include -#include -#include - -template -class TVector: public std::vector> { -public: - using TBase = std::vector>; - using TSelf = TVector; - using size_type = typename TBase::size_type; - - inline TVector() - : TBase() - { - } - - inline TVector(const typename TBase::allocator_type& a) - : TBase(a) - { - } - - inline explicit TVector(::NDetail::TReserveTag rt) - : TBase() - { - this->reserve(rt.Capacity); - } - - inline explicit TVector(::NDetail::TReserveTag rt, const typename TBase::allocator_type& a) - : TBase(a) - { - this->reserve(rt.Capacity); - } - - inline explicit TVector(size_type count) - : TBase(count) - { - } - - inline explicit TVector(size_type count, const typename TBase::allocator_type& a) - : TBase(count, a) - { - } - - inline TVector(size_type count, const T& val) - : TBase(count, val) - { - } - - inline TVector(size_type count, const T& val, const typename TBase::allocator_type& a) - : TBase(count, val, a) - { - } - - inline TVector(std::initializer_list il) - : TBase(il) - { - } - - inline TVector(std::initializer_list il, const typename TBase::allocator_type& a) - : TBase(il, a) - { - } - - inline TVector(const TSelf& src) - : TBase(src) - { - } - - inline TVector(TSelf&& src) noexcept - : TBase(std::forward(src)) - { - } - - template - inline TVector(TIter first, TIter last) - : TBase(first, last) - { - } - - inline TSelf& operator=(const TSelf& src) { - TBase::operator=(src); - return *this; - } - - inline TSelf& operator=(TSelf&& src) noexcept { - TBase::operator=(std::forward(src)); - return *this; - } - - inline TSelf& operator=(std::initializer_list il) { - this->assign(il.begin(), il.end()); - return *this; - } - - inline explicit operator bool() const noexcept { - return !this->empty(); - } - - Y_PURE_FUNCTION inline bool empty() const noexcept { - return TBase::empty(); - } - - inline yssize_t ysize() const noexcept { - return (yssize_t)TBase::size(); - } - -#ifdef _YNDX_LIBCXX_ENABLE_VECTOR_POD_RESIZE_UNINITIALIZED - void yresize(size_type newSize) { - if (std::is_pod::value) { - TBase::resize_uninitialized(newSize); - } else { - TBase::resize(newSize); - } - } -#else - void yresize(size_type newSize) { - TBase::resize(newSize); - } -#endif - - inline void crop(size_type size) { - if (this->size() > size) { - this->erase(this->begin() + size, this->end()); - } - } -}; diff --git a/util/generic/vector.pxd b/util/generic/vector.pxd deleted file mode 100644 index 99dde95d48f..00000000000 --- a/util/generic/vector.pxd +++ /dev/null @@ -1,83 +0,0 @@ -cdef extern from "" nogil: - cdef cppclass TVector[T]: - cppclass iterator: - T& operator*() - iterator operator++() - iterator operator--() - iterator operator+(size_t) - iterator operator-(size_t) - bint operator==(iterator) - bint operator!=(iterator) - bint operator<(iterator) - bint operator>(iterator) - bint operator<=(iterator) - bint operator>=(iterator) - - cppclass reverse_iterator: - T& operator*() - reverse_iterator operator++() - reverse_iterator operator--() - reverse_iterator operator+(size_t) - reverse_iterator operator-(size_t) - bint operator==(reverse_iterator) - bint operator!=(reverse_iterator) - bint operator<(reverse_iterator) - bint operator>(reverse_iterator) - bint operator<=(reverse_iterator) - bint operator>=(reverse_iterator) - - cppclass const_iterator(iterator): - pass - - cppclass const_reverse_iterator(reverse_iterator): - pass - - TVector() except + - TVector(TVector&) except + - TVector(size_t) except + - TVector(size_t, T&) except + - - bint operator==(TVector&) - bint operator!=(TVector&) - bint operator<(TVector&) - bint operator>(TVector&) - bint operator<=(TVector&) - bint operator>=(TVector&) - - void assign(size_t, const T&) except + - void assign[input_iterator](input_iterator, input_iterator) except + - - T& at(size_t) except + - T& operator[](size_t) - - T& back() - iterator begin() - const_iterator const_begin "begin"() - size_t capacity() - void clear() except + - bint empty() - iterator end() - const_iterator const_end "end"() - iterator erase(iterator) except + - iterator erase(iterator, iterator) except + - T& front() - iterator insert(iterator, const T&) except + - void insert(iterator, size_t, const T&) except + - void insert[Iter](iterator, Iter, Iter) except + - size_t max_size() - void pop_back() except + - void push_back(T&) except + - void emplace_back(...) except + - reverse_iterator rbegin() - const_reverse_iterator const_rbegin "rbegin"() - reverse_iterator rend() - const_reverse_iterator const_rend "rend"() - void reserve(size_t) except + - void resize(size_t) except + - void resize(size_t, T&) except + - size_t size() - void swap(TVector&) except + - - # C++11 methods - T* data() - void shrink_to_fit() except + diff --git a/util/generic/vector_ut.cpp b/util/generic/vector_ut.cpp deleted file mode 100644 index 63b10edab59..00000000000 --- a/util/generic/vector_ut.cpp +++ /dev/null @@ -1,587 +0,0 @@ -#include "vector.h" - -#include - -#include -#include "yexception.h" - -#include - -class TYVectorTest: public TTestBase { - UNIT_TEST_SUITE(TYVectorTest); - UNIT_TEST(TestConstructorsAndAssignments) - UNIT_TEST(TestTildeEmptyToNull) - UNIT_TEST(TestTilde) - UNIT_TEST(Test1) - UNIT_TEST(Test2) - UNIT_TEST(Test3) - UNIT_TEST(Test4) - UNIT_TEST(Test5) - UNIT_TEST(Test6) - UNIT_TEST(Test7) - UNIT_TEST(TestCapacity) - UNIT_TEST(TestAt) - UNIT_TEST(TestPointer) - UNIT_TEST(TestAutoRef) - UNIT_TEST(TestIterators) - UNIT_TEST(TestShrink) - //UNIT_TEST(TestEbo) - UNIT_TEST(TestFillInConstructor) - UNIT_TEST(TestYResize) - UNIT_TEST(TestCrop) - UNIT_TEST(TestInitializeList) - UNIT_TEST_SUITE_END(); - -private: - void TestConstructorsAndAssignments() { - using container = TVector; - - container c1; - c1.push_back(100); - c1.push_back(200); - - container c2(c1); - - UNIT_ASSERT_VALUES_EQUAL(2, c1.size()); - UNIT_ASSERT_VALUES_EQUAL(2, c2.size()); - UNIT_ASSERT_VALUES_EQUAL(100, c1.at(0)); - UNIT_ASSERT_VALUES_EQUAL(200, c2.at(1)); - - container c3(std::move(c1)); - - UNIT_ASSERT_VALUES_EQUAL(0, c1.size()); - UNIT_ASSERT_VALUES_EQUAL(2, c3.size()); - UNIT_ASSERT_VALUES_EQUAL(100, c3.at(0)); - - c2.push_back(300); - c3 = c2; - - UNIT_ASSERT_VALUES_EQUAL(3, c2.size()); - UNIT_ASSERT_VALUES_EQUAL(3, c3.size()); - UNIT_ASSERT_VALUES_EQUAL(300, c3.at(2)); - - c2.push_back(400); - c3 = std::move(c2); - - UNIT_ASSERT_VALUES_EQUAL(0, c2.size()); - UNIT_ASSERT_VALUES_EQUAL(4, c3.size()); - UNIT_ASSERT_VALUES_EQUAL(400, c3.at(3)); - } - - inline void TestTildeEmptyToNull() { - TVector v; - UNIT_ASSERT_EQUAL(nullptr, v.data()); - } - - inline void TestTilde() { - TVector v; - v.push_back(10); - v.push_back(20); - - UNIT_ASSERT_EQUAL(10, (v.data())[0]); - UNIT_ASSERT_EQUAL(20, (v.data())[1]); - - for (int i = 0; i < 10000; ++i) { - v.push_back(99); - } - - UNIT_ASSERT_EQUAL(10, (v.data())[0]); - UNIT_ASSERT_EQUAL(20, (v.data())[1]); - UNIT_ASSERT_EQUAL(99, (v.data())[3]); - UNIT_ASSERT_EQUAL(99, (v.data())[4]); - } - - // Copy-paste of STLPort tests - - void Test1() { - TVector v1; // Empty vector of integers. - - UNIT_ASSERT(v1.empty() == true); - UNIT_ASSERT(v1.size() == 0); - UNIT_ASSERT(!v1); - - // UNIT_ASSERT(v1.max_size() == INT_MAX / sizeof(int)); - // cout << "max_size = " << v1.max_size() << endl; - v1.push_back(42); // Add an integer to the vector. - - UNIT_ASSERT(v1.size() == 1); - UNIT_ASSERT(v1); - - UNIT_ASSERT(v1[0] == 42); - - { - TVector> vect(10); - TVector>::iterator it(vect.begin()), end(vect.end()); - for (; it != end; ++it) { - UNIT_ASSERT((*it).empty()); - UNIT_ASSERT((*it).size() == 0); - UNIT_ASSERT((*it).capacity() == 0); - UNIT_ASSERT((*it).begin() == (*it).end()); - } - } - } - - void Test2() { - TVector v1; // Empty vector of doubles. - v1.push_back(32.1); - v1.push_back(40.5); - TVector v2; // Another empty vector of doubles. - v2.push_back(3.56); - - UNIT_ASSERT(v1.size() == 2); - UNIT_ASSERT(v1[0] == 32.1); - UNIT_ASSERT(v1[1] == 40.5); - - UNIT_ASSERT(v2.size() == 1); - UNIT_ASSERT(v2[0] == 3.56); - v1.swap(v2); // Swap the vector's contents. - - UNIT_ASSERT(v1.size() == 1); - UNIT_ASSERT(v1[0] == 3.56); - - UNIT_ASSERT(v2.size() == 2); - UNIT_ASSERT(v2[0] == 32.1); - UNIT_ASSERT(v2[1] == 40.5); - - v2 = v1; // Assign one vector to another. - - UNIT_ASSERT(v2.size() == 1); - UNIT_ASSERT(v2[0] == 3.56); - } - - void Test3() { - using vec_type = TVector; - - vec_type v1; // Empty vector of characters. - v1.push_back('h'); - v1.push_back('i'); - - UNIT_ASSERT(v1.size() == 2); - UNIT_ASSERT(v1[0] == 'h'); - UNIT_ASSERT(v1[1] == 'i'); - - vec_type v2(v1.begin(), v1.end()); - v2[1] = 'o'; // Replace second character. - - UNIT_ASSERT(v2.size() == 2); - UNIT_ASSERT(v2[0] == 'h'); - UNIT_ASSERT(v2[1] == 'o'); - - UNIT_ASSERT((v1 == v2) == false); - - UNIT_ASSERT((v1 < v2) == true); - } - - void Test4() { - TVector v(4); - - v[0] = 1; - v[1] = 4; - v[2] = 9; - v[3] = 16; - - UNIT_ASSERT(v.front() == 1); - UNIT_ASSERT(v.back() == 16); - - v.push_back(25); - - UNIT_ASSERT(v.back() == 25); - UNIT_ASSERT(v.size() == 5); - - v.pop_back(); - - UNIT_ASSERT(v.back() == 16); - UNIT_ASSERT(v.size() == 4); - } - - void Test5() { - int array[] = {1, 4, 9, 16}; - - TVector v(array, array + 4); - - UNIT_ASSERT(v.size() == 4); - - UNIT_ASSERT(v[0] == 1); - UNIT_ASSERT(v[1] == 4); - UNIT_ASSERT(v[2] == 9); - UNIT_ASSERT(v[3] == 16); - } - - void Test6() { - int array[] = {1, 4, 9, 16, 25, 36}; - - TVector v(array, array + 6); - TVector::iterator vit; - - UNIT_ASSERT(v.size() == 6); - UNIT_ASSERT(v[0] == 1); - UNIT_ASSERT(v[1] == 4); - UNIT_ASSERT(v[2] == 9); - UNIT_ASSERT(v[3] == 16); - UNIT_ASSERT(v[4] == 25); - UNIT_ASSERT(v[5] == 36); - - vit = v.erase(v.begin()); // Erase first element. - UNIT_ASSERT(*vit == 4); - - UNIT_ASSERT(v.size() == 5); - UNIT_ASSERT(v[0] == 4); - UNIT_ASSERT(v[1] == 9); - UNIT_ASSERT(v[2] == 16); - UNIT_ASSERT(v[3] == 25); - UNIT_ASSERT(v[4] == 36); - - vit = v.erase(v.end() - 1); // Erase last element. - UNIT_ASSERT(vit == v.end()); - - UNIT_ASSERT(v.size() == 4); - UNIT_ASSERT(v[0] == 4); - UNIT_ASSERT(v[1] == 9); - UNIT_ASSERT(v[2] == 16); - UNIT_ASSERT(v[3] == 25); - - v.erase(v.begin() + 1, v.end() - 1); // Erase all but first and last. - - UNIT_ASSERT(v.size() == 2); - UNIT_ASSERT(v[0] == 4); - UNIT_ASSERT(v[1] == 25); - } - - void Test7() { - int array1[] = {1, 4, 25}; - int array2[] = {9, 16}; - - TVector v(array1, array1 + 3); - TVector::iterator vit; - vit = v.insert(v.begin(), 0); // Insert before first element. - UNIT_ASSERT(*vit == 0); - - vit = v.insert(v.end(), 36); // Insert after last element. - UNIT_ASSERT(*vit == 36); - - UNIT_ASSERT(v.size() == 5); - UNIT_ASSERT(v[0] == 0); - UNIT_ASSERT(v[1] == 1); - UNIT_ASSERT(v[2] == 4); - UNIT_ASSERT(v[3] == 25); - UNIT_ASSERT(v[4] == 36); - - // Insert contents of array2 before fourth element. - v.insert(v.begin() + 3, array2, array2 + 2); - - UNIT_ASSERT(v.size() == 7); - - UNIT_ASSERT(v[0] == 0); - UNIT_ASSERT(v[1] == 1); - UNIT_ASSERT(v[2] == 4); - UNIT_ASSERT(v[3] == 9); - UNIT_ASSERT(v[4] == 16); - UNIT_ASSERT(v[5] == 25); - UNIT_ASSERT(v[6] == 36); - - size_t curCapacity = v.capacity(); - v.clear(); - UNIT_ASSERT(v.empty()); - - //check that clear save reserved data - UNIT_ASSERT_EQUAL(curCapacity, v.capacity()); - - v.insert(v.begin(), 5, 10); - UNIT_ASSERT(v.size() == 5); - UNIT_ASSERT(v[0] == 10); - UNIT_ASSERT(v[1] == 10); - UNIT_ASSERT(v[2] == 10); - UNIT_ASSERT(v[3] == 10); - UNIT_ASSERT(v[4] == 10); - } - - struct TestStruct { - unsigned int a[3]; - }; - - void TestCapacity() { - { - TVector v; - - UNIT_ASSERT(v.capacity() == 0); - v.push_back(42); - UNIT_ASSERT(v.capacity() >= 1); - v.reserve(5000); - UNIT_ASSERT(v.capacity() >= 5000); - } - - { - TVector v(Reserve(100)); - - UNIT_ASSERT(v.capacity() >= 100); - UNIT_ASSERT(v.size() == 0); - } - - { - //Test that used to generate an assertion when using __debug_alloc. - TVector va; - va.reserve(1); - va.reserve(2); - } - } - - void TestAt() { - TVector v; - TVector const& cv = v; - - v.push_back(10); - UNIT_ASSERT(v.at(0) == 10); - v.at(0) = 20; - UNIT_ASSERT(cv.at(0) == 20); - - UNIT_ASSERT_EXCEPTION(v.at(1) = 20, std::out_of_range); - } - - void TestPointer() { - TVector v1; - TVector v2 = v1; - TVector v3; - - v3.insert(v3.end(), v1.begin(), v1.end()); - } - - void TestAutoRef() { - TVector ref; - for (int i = 0; i < 5; ++i) { - ref.push_back(i); - } - - TVector> v_v_int(1, ref); - v_v_int.push_back(v_v_int[0]); - v_v_int.push_back(ref); - v_v_int.push_back(v_v_int[0]); - v_v_int.push_back(v_v_int[0]); - v_v_int.push_back(ref); - - TVector>::iterator vvit(v_v_int.begin()), vvitEnd(v_v_int.end()); - for (; vvit != vvitEnd; ++vvit) { - UNIT_ASSERT(*vvit == ref); - } - } - - struct Point { - int x, y; - }; - - struct PointEx: public Point { - PointEx() - : builtFromBase(false) - { - } - PointEx(const Point&) - : builtFromBase(true) - { - } - - bool builtFromBase; - }; - - void TestIterators() { - TVector vint(10, 0); - TVector const& crvint = vint; - - UNIT_ASSERT(vint.begin() == vint.begin()); - UNIT_ASSERT(crvint.begin() == vint.begin()); - UNIT_ASSERT(vint.begin() == crvint.begin()); - UNIT_ASSERT(crvint.begin() == crvint.begin()); - - UNIT_ASSERT(vint.begin() != vint.end()); - UNIT_ASSERT(crvint.begin() != vint.end()); - UNIT_ASSERT(vint.begin() != crvint.end()); - UNIT_ASSERT(crvint.begin() != crvint.end()); - - UNIT_ASSERT(vint.rbegin() == vint.rbegin()); - // Not Standard: - //UNIT_ASSERT(vint.rbegin() == crvint.rbegin()); - //UNIT_ASSERT(crvint.rbegin() == vint.rbegin()); - UNIT_ASSERT(crvint.rbegin() == crvint.rbegin()); - - UNIT_ASSERT(vint.rbegin() != vint.rend()); - // Not Standard: - //UNIT_ASSERT(vint.rbegin() != crvint.rend()); - //UNIT_ASSERT(crvint.rbegin() != vint.rend()); - UNIT_ASSERT(crvint.rbegin() != crvint.rend()); - } - - void TestShrink() { - TVector v; - v.resize(1000); - v.resize(10); - v.shrink_to_fit(); - UNIT_ASSERT_EQUAL(v.capacity(), 10); - v.push_back(0); - v.shrink_to_fit(); - UNIT_ASSERT_EQUAL(v.capacity(), 11); - } - - /* This test check a potential issue with empty base class - * optimization. Some compilers (VC6) do not implement it - * correctly resulting ina wrong behavior. */ - void TestEbo() { - // We use heap memory as test failure can corrupt vector internal - // representation making executable crash on vector destructor invocation. - // We prefer a simple memory leak, internal corruption should be reveal - // by size or capacity checks. - using V = TVector; - V* pv1 = new V(1, 1); - V* pv2 = new V(10, 2); - - size_t v1Capacity = pv1->capacity(); - size_t v2Capacity = pv2->capacity(); - - pv1->swap(*pv2); - - UNIT_ASSERT(pv1->size() == 10); - UNIT_ASSERT(pv1->capacity() == v2Capacity); - UNIT_ASSERT((*pv1)[5] == 2); - - UNIT_ASSERT(pv2->size() == 1); - UNIT_ASSERT(pv2->capacity() == v1Capacity); - UNIT_ASSERT((*pv2)[0] == 1); - - delete pv2; - delete pv1; - } - - void TestFillInConstructor() { - for (int k = 0; k < 3; ++k) { - TVector v(100); - UNIT_ASSERT_VALUES_EQUAL(100u, v.size()); - for (size_t i = 0; i < v.size(); ++i) { - UNIT_ASSERT_VALUES_EQUAL(0, v[i]); - } - // fill with garbage for the next iteration - for (size_t i = 0; i < v.size(); ++i) { - v[i] = 10; - } - } - } - - struct TPod { - int x; - - operator int() { - return x; - } - }; - - struct TNonPod { - int x; - TNonPod() { - x = 0; - } - - operator int() { - return x; - } - }; - - template - class TDebugAlloc: public std::allocator { - public: - using TBase = std::allocator; - - T* allocate(typename TBase::size_type n) { - auto p = TBase::allocate(n); - for (size_t i = 0; i < n; ++i) { - memset(p + i, 0xab, sizeof(T)); - } - return p; - } - - template - struct rebind { - using other = TDebugAlloc; - }; - }; - - template - void TestYResize() { -#ifdef _YNDX_LIBCXX_ENABLE_VECTOR_POD_RESIZE_UNINITIALIZED - constexpr bool ALLOW_UNINITIALIZED = std::is_pod_v; -#else - constexpr bool ALLOW_UNINITIALIZED = false; -#endif - - TVector> v; - - v.reserve(5); - auto firstBegin = v.begin(); - - v.yresize(5); // No realloc, no initialization if allowed - UNIT_ASSERT(firstBegin == v.begin()); - for (int i = 0; i < 5; ++i) { - UNIT_ASSERT_VALUES_EQUAL(bool(v[i]), ALLOW_UNINITIALIZED); - } - - v.yresize(20); // Realloc, still no initialization - UNIT_ASSERT(firstBegin != v.begin()); - for (int i = 0; i < 20; ++i) { - UNIT_ASSERT_VALUES_EQUAL(bool(v[i]), ALLOW_UNINITIALIZED); - } - } - - struct TNoDefaultConstructor { - TNoDefaultConstructor() = delete; - explicit TNoDefaultConstructor(int val) - : Val(val) - { - } - - int Val; - }; - - void TestCrop() { - TVector vec; - vec.emplace_back(42); - vec.emplace_back(1337); - vec.emplace_back(8888); - vec.crop(1); // Should not require default constructor - UNIT_ASSERT(vec.size() == 1); - UNIT_ASSERT(vec[0].Val == 42); - vec.crop(50); // Does nothing if new size is greater than the current size() - UNIT_ASSERT(vec.size() == 1); - UNIT_ASSERT(vec[0].Val == 42); - } - - void TestYResize() { - TestYResize(); - TestYResize(); - TestYResize(); - } - - void CheckInitializeList(const TVector& v) { - for (size_t i = 0; i < v.size(); ++i) { - UNIT_ASSERT_EQUAL(v[i], static_cast(i)); - } - } - - void TestInitializeList() { - { - TVector v; - v.assign({0, 1, 2}); - CheckInitializeList(v); - } - { - TVector v = {0, 1, 2}; - CheckInitializeList(v); - } - { - TVector v; - v = {0, 1, 2}; - CheckInitializeList(v); - } - { - TVector v = {0, 3}; - v.insert(v.begin() + 1, {1, 2}); - CheckInitializeList(v); - } - } -}; - -UNIT_TEST_SUITE_REGISTRATION(TYVectorTest); diff --git a/util/generic/vector_ut.pyx b/util/generic/vector_ut.pyx deleted file mode 100644 index 904e67733b3..00000000000 --- a/util/generic/vector_ut.pyx +++ /dev/null @@ -1,221 +0,0 @@ -# cython: c_string_type=str, c_string_encoding=utf8 - -from util.generic.vector cimport TVector -from util.generic.string cimport TString - -import pytest -import unittest - - -def _check_convert(TVector[TString] x): - return x - - -class TestVector(unittest.TestCase): - - def test_ctor1(self): - cdef TVector[int] tmp = TVector[int]() - self.assertEqual(tmp.size(), 0) - - def test_ctor2(self): - cdef TVector[int] tmp = TVector[int](10) - self.assertEqual(tmp.size(), 10) - self.assertEqual(tmp[0], 0) - - def test_ctor3(self): - cdef TVector[int] tmp = TVector[int](10, 42) - self.assertEqual(tmp.size(), 10) - self.assertEqual(tmp[0], 42) - - def test_ctor4(self): - cdef TVector[int] tmp = TVector[int](10, 42) - cdef TVector[int] tmp2 = TVector[int](tmp) - self.assertEqual(tmp2.size(), 10) - self.assertEqual(tmp2[0], 42) - - def test_operator_assign(self): - cdef TVector[int] tmp2 - tmp2.push_back(1) - tmp2.push_back(2) - - cdef TVector[int] tmp3 - tmp3.push_back(1) - tmp3.push_back(3) - - self.assertEqual(tmp2[1], 2) - self.assertEqual(tmp3[1], 3) - - tmp3 = tmp2 - - self.assertEqual(tmp2[1], 2) - self.assertEqual(tmp3[1], 2) - - def test_compare(self): - cdef TVector[int] tmp1 - tmp1.push_back(1) - tmp1.push_back(2) - - cdef TVector[int] tmp2 - tmp2.push_back(1) - tmp2.push_back(2) - - cdef TVector[int] tmp3 - tmp3.push_back(1) - tmp3.push_back(3) - - self.assertTrue(tmp1 == tmp2) - self.assertTrue(tmp1 != tmp3) - - self.assertTrue(tmp1 < tmp3) - self.assertTrue(tmp1 <= tmp3) - - self.assertTrue(tmp3 > tmp1) - self.assertTrue(tmp3 >= tmp1) - - def test_index(self): - cdef TVector[int] tmp = TVector[int](10, 42) - - self.assertEqual(tmp[0], 42) - self.assertEqual(tmp[5], 42) - - self.assertEqual(tmp.data()[0], 42) - self.assertEqual(tmp.data()[5], 42) - - self.assertEqual(tmp.at(0), 42) - self.assertEqual(tmp.at(5), 42) - - with pytest.raises(IndexError): - tmp.at(100) - - def test_push_pop_back(self): - cdef TVector[int] tmp - self.assertEqual(tmp.size(), 0) - - tmp.push_back(42) - self.assertEqual(tmp.size(), 1) - self.assertEqual(tmp.back(), 42) - - tmp.push_back(77) - self.assertEqual(tmp.size(), 2) - self.assertEqual(tmp.back(), 77) - - tmp.pop_back() - self.assertEqual(tmp.size(), 1) - self.assertEqual(tmp.back(), 42) - - tmp.pop_back() - self.assertEqual(tmp.size(), 0) - - def test_front(self): - cdef TVector[int] tmp - tmp.push_back(42) - self.assertEqual(tmp.front(), 42) - - def test_empty(self): - cdef TVector[int] tmp - self.assertTrue(tmp.empty()) - tmp.push_back(42) - self.assertFalse(tmp.empty()) - - def test_max_size(self): - cdef TVector[int] tmp - self.assertTrue(tmp.max_size() > 0) - - def test_reserve_resize(self): - cdef TVector[int] tmp - tmp.reserve(1000) - self.assertEqual(tmp.capacity(), 1000) - - tmp.resize(100) - self.assertEqual(tmp.size(), 100) - self.assertEqual(tmp.front(), 0) - self.assertEqual(tmp.back(), 0) - - tmp.shrink_to_fit() - tmp.clear() - - tmp.resize(100, 42) - self.assertEqual(tmp.size(), 100) - self.assertEqual(tmp.front(), 42) - self.assertEqual(tmp.back(), 42) - - def test_iter(self): - cdef TVector[int] tmp - tmp.push_back(1) - tmp.push_back(20) - tmp.push_back(300) - - self.assertEqual([i for i in tmp], [1, 20, 300]) - - def test_iterator(self): - cdef TVector[int] tmp - - self.assertTrue(tmp.begin() == tmp.end()) - self.assertTrue(tmp.rbegin() == tmp.rend()) - self.assertTrue(tmp.const_begin() == tmp.const_end()) - self.assertTrue(tmp.const_rbegin() == tmp.const_rend()) - - tmp.push_back(1) - - self.assertTrue(tmp.begin() != tmp.end()) - self.assertTrue(tmp.rbegin() != tmp.rend()) - self.assertTrue(tmp.const_begin() != tmp.const_end()) - self.assertTrue(tmp.const_rbegin() != tmp.const_rend()) - - self.assertTrue(tmp.begin() < tmp.end()) - self.assertTrue(tmp.rbegin() < tmp.rend()) - self.assertTrue(tmp.const_begin() < tmp.const_end()) - self.assertTrue(tmp.const_rbegin() < tmp.const_rend()) - - self.assertTrue(tmp.begin() + 1 == tmp.end()) - self.assertTrue(tmp.rbegin() + 1 == tmp.rend()) - self.assertTrue(tmp.const_begin() + 1 == tmp.const_end()) - self.assertTrue(tmp.const_rbegin() + 1 == tmp.const_rend()) - - def test_assign(self): - cdef TVector[int] tmp - - tmp.assign(10, 42) - self.assertEqual(tmp.size(), 10) - self.assertEqual(tmp.front(), 42) - self.assertEqual(tmp.back(), 42) - - def test_insert(self): - cdef TVector[int] tmp - tmp.push_back(1) - tmp.push_back(2) - tmp.push_back(3) - - cdef TVector[int] tmp2 - tmp2.push_back(7) - tmp2.push_back(9) - - tmp.insert(tmp.begin(), 8) - self.assertEqual([i for i in tmp], [8, 1, 2, 3]) - - tmp.insert(tmp.begin(), 2, 6) - self.assertEqual([i for i in tmp], [6, 6, 8, 1, 2, 3]) - - tmp.insert(tmp.begin(), tmp2.begin(), tmp2.end()) - self.assertEqual([i for i in tmp], [7, 9, 6, 6, 8, 1, 2, 3]) - - def test_erase(self): - cdef TVector[int] tmp - tmp.push_back(1) - tmp.push_back(2) - tmp.push_back(3) - tmp.push_back(4) - - tmp.erase(tmp.begin() + 1) - self.assertEqual([i for i in tmp], [1, 3, 4]) - - tmp.erase(tmp.begin(), tmp.begin() + 2) - self.assertEqual([i for i in tmp], [4]) - - def test_convert(self): - src = ['foo', 'bar', 'baz'] - self.assertEqual(_check_convert(src), src) - - bad_src = ['foo', 42] - with self.assertRaises(TypeError): - _check_convert(bad_src) diff --git a/util/generic/xrange_ut.cpp b/util/generic/xrange_ut.cpp index 7bca2562ce5..90eb38620bc 100644 --- a/util/generic/xrange_ut.cpp +++ b/util/generic/xrange_ut.cpp @@ -158,7 +158,7 @@ Y_UNIT_TEST_SUITE(XRange) { template static void TestIteratorDifferenceImpl(TRange range, int a, int b, std::optional step) { - auto fmtCase = [&]() -> TString { return TStringBuilder() << "xrange(" << a << ", " << b << (step ? ", " + ToString(*step) : TString{}) << ")"; }; + auto fmtCase = [&]() -> std::string { return TStringBuilder() << "xrange(" << a << ", " << b << (step ? ", " + ToString(*step) : std::string{}) << ")"; }; auto begin = std::begin(range); auto end = std::end(range); auto distance = end - begin; diff --git a/util/generic/yexception.cpp b/util/generic/yexception.cpp index ac62a8f4d21..6580c358757 100644 --- a/util/generic/yexception.cpp +++ b/util/generic/yexception.cpp @@ -1,6 +1,9 @@ #include "bt_exception.h" #include "yexception.h" +#include + +#include #include #include @@ -16,13 +19,13 @@ static void FormatExceptionTo(IOutputStream& out, const std::exception& exceptio out << "(" << TypeName(exception) << ") " << exception.what(); } -TString FormatExc(const std::exception& exception) { +std::string FormatExc(const std::exception& exception) { TStringStream out; FormatExceptionTo(out, exception); return out.Str(); } -TString CurrentExceptionMessage() { +std::string CurrentExceptionMessage() { auto exceptionPtr = std::current_exception(); if (exceptionPtr) { try { @@ -31,7 +34,7 @@ TString CurrentExceptionMessage() { const TBackTrace* bt = e.BackTrace(); if (bt) { - return TString::Join(bt->PrintToString(), TStringBuf("\n"), FormatExc(e)); + return NUtils::Join(bt->PrintToString(), std::string_view("\n"), FormatExc(e)); } return FormatExc(e); @@ -86,7 +89,7 @@ void FormatCurrentExceptionTo(IOutputStream& out) { } } -TString FormatCurrentException() { +std::string FormatCurrentException() { TStringStream out; FormatCurrentExceptionTo(out); return out.Str(); @@ -119,21 +122,21 @@ std::string CurrentExceptionTypeName() { void TSystemError::Init() { yexception& exc = *this; - exc << TStringBuf("("); - exc << TStringBuf(LastSystemErrorText(Status_)); - exc << TStringBuf(") "); + exc << std::string_view("("); + exc << std::string_view(LastSystemErrorText(Status_)); + exc << std::string_view(") "); } NPrivateException::yexception::yexception() { ZeroTerminate(); } -TStringBuf NPrivateException::yexception::AsStrBuf() const { +std::string_view NPrivateException::yexception::AsStrBuf() const { if (Buf_.Left()) { - return TStringBuf(Buf_.Data(), Buf_.Filled()); + return std::string_view(Buf_.Data(), Buf_.Filled()); } - return TStringBuf(Buf_.Data(), Buf_.Filled() - 1); + return std::string_view(Buf_.Data(), Buf_.Filled() - 1); } void NPrivateException::yexception::ZeroTerminate() noexcept { diff --git a/util/generic/yexception.h b/util/generic/yexception.h index 526023f5420..2ce3dd8aadf 100644 --- a/util/generic/yexception.h +++ b/util/generic/yexception.h @@ -1,7 +1,6 @@ #pragma once #include "bt_exception.h" -#include "strbuf.h" #include "string.h" #include "utility.h" #include "va_args.h" @@ -56,7 +55,7 @@ namespace NPrivateException { ZeroTerminate(); } - TStringBuf AsStrBuf() const; + std::string_view AsStrBuf() const; private: void ZeroTerminate() noexcept; @@ -75,7 +74,7 @@ namespace NPrivateException { template static inline T&& operator+(const TSourceLocation& sl, T&& t) { - return std::forward(t << sl << TStringBuf(": ")); + return std::forward(t << sl << std::string_view(": ")); } } @@ -145,7 +144,7 @@ namespace NPrivate { /// exception message consists of single constant string struct TSimpleExceptionMessage { TSourceLocation Location; - TStringBuf Message; + std::string_view Message; }; [[noreturn]] void ThrowYException(const TSimpleExceptionMessage& sm); @@ -154,7 +153,7 @@ namespace NPrivate { void fputs(const std::exception& e, FILE* f = stderr); -TString CurrentExceptionMessage(); +std::string CurrentExceptionMessage(); /** * Formats current exception for logging purposes. Includes formatted backtrace if it is stored @@ -165,7 +164,7 @@ TString CurrentExceptionMessage(); * The lack of current exception prior to the invocation indicates logical bug in the client code. * Y_ABORT_UNLESS asserts the existence of exception, otherwise panic and abort. */ -TString FormatCurrentException(); +std::string FormatCurrentException(); void FormatCurrentExceptionTo(IOutputStream& out); /* @@ -185,7 +184,7 @@ bool UncaughtException() noexcept; std::string CurrentExceptionTypeName(); -TString FormatExc(const std::exception& exception); +std::string FormatExc(const std::exception& exception); #define Y_THROW_UNLESS_EX(CONDITION, THROW_EXPRESSION) \ do { \ @@ -207,10 +206,10 @@ TString FormatExc(const std::exception& exception); } \ } while (false) -#define Y_ENSURE_IMPL_1(CONDITION) Y_ENSURE_SIMPLE(CONDITION, ::TStringBuf("Condition violated: `" Y_STRINGIZE(CONDITION) "'"), ::NPrivate::ThrowYException) +#define Y_ENSURE_IMPL_1(CONDITION) Y_ENSURE_SIMPLE(CONDITION, ::std::string_view("Condition violated: `" Y_STRINGIZE(CONDITION) "'"), ::NPrivate::ThrowYException) #define Y_ENSURE_IMPL_2(CONDITION, MESSAGE) Y_ENSURE_EX(CONDITION, yexception() << MESSAGE) -#define Y_ENSURE_BT_IMPL_1(CONDITION) Y_ENSURE_SIMPLE(CONDITION, ::TStringBuf("Condition violated: `" Y_STRINGIZE(CONDITION) "'"), ::NPrivate::ThrowYExceptionWithBacktrace) +#define Y_ENSURE_BT_IMPL_1(CONDITION) Y_ENSURE_SIMPLE(CONDITION, ::std::string_view("Condition violated: `" Y_STRINGIZE(CONDITION) "'"), ::NPrivate::ThrowYExceptionWithBacktrace) #define Y_ENSURE_BT_IMPL_2(CONDITION, MESSAGE) Y_ENSURE_EX(CONDITION, TWithBackTrace() << MESSAGE) /** diff --git a/util/generic/yexception_ut.cpp b/util/generic/yexception_ut.cpp index e47de0c95a4..c0d29aac8a9 100644 --- a/util/generic/yexception_ut.cpp +++ b/util/generic/yexception_ut.cpp @@ -112,7 +112,7 @@ class TExceptionTest: public TTestBase { try { Y_ENSURE_BT(4 > 6); } catch (...) { - const TString msg = CurrentExceptionMessage(); + const std::string msg = CurrentExceptionMessage(); UNIT_ASSERT(msg.Contains("4 > 6")); UNIT_ASSERT(msg.Contains("\n")); EnsureCurrentExceptionHasBackTrace(); @@ -126,7 +126,7 @@ class TExceptionTest: public TTestBase { Y_ENSURE_BT(4 > 6, "custom " << "message"); } catch (...) { - const TString msg = CurrentExceptionMessage(); + const std::string msg = CurrentExceptionMessage(); UNIT_ASSERT(!msg.Contains("4 > 6")); UNIT_ASSERT(msg.Contains("custom message")); UNIT_ASSERT(msg.Contains("\n")); @@ -143,9 +143,9 @@ class TExceptionTest: public TTestBase { throw std::logic_error("some exception"); // is instance of std::exception UNIT_ASSERT(false); } catch (...) { - TString exceptionMessage = FormatCurrentException(); + std::string exceptionMessage = FormatCurrentException(); UNIT_ASSERT(exceptionMessage.Contains("(std::logic_error) some exception")); - std::vector backtraceStrs = StringSplitter(exceptionMessage).Split('\n'); + std::vector backtraceStrs = StringSplitter(exceptionMessage).Split('\n'); UNIT_ASSERT(backtraceStrs.size() > 1); } } @@ -166,7 +166,7 @@ class TExceptionTest: public TTestBase { Throw1DontMove(); UNIT_ASSERT(false); } catch (...) { - TString expected = "Caught:\n" + std::string expected = "Caught:\n" "(yexception) util/generic/yexception_ut.cpp:4: blabla\n" "Failed to print backtrace: " "(yexception) util/generic/yexception_ut.cpp:8: 1 qw 12.1"; @@ -176,7 +176,7 @@ class TExceptionTest: public TTestBase { throw std::logic_error("std exception"); UNIT_ASSERT(false); } catch (...) { - TString expected = "Caught:\n" + std::string expected = "Caught:\n" "(std::logic_error) std exception\n" "Failed to print backtrace: " "(yexception) util/generic/yexception_ut.cpp:8: 1 qw 12.1"; @@ -269,14 +269,14 @@ class TExceptionTest: public TTestBase { throw yexception() << 1 << " qw " << 12.1; UNIT_ASSERT(false); } catch (...) { - const TString err = CurrentExceptionMessage(); + const std::string err = CurrentExceptionMessage(); UNIT_ASSERT(err.Contains("1 qw 12.1")); } } static inline void CheckCurrentExceptionContains(const char* message) { - TString err = CurrentExceptionMessage(); + std::string err = CurrentExceptionMessage(); SubstGlobal(err, '\\', '/'); // remove backslashes from path in message UNIT_ASSERT(err.Contains(message)); } @@ -341,7 +341,7 @@ class TExceptionTest: public TTestBase { void TestMessageCrop() { TTempBuf tmp; size_t size = tmp.Size() * 1.5; - TString s; + std::string s; s.reserve(size); TMersenne generator(42); for (int j = 0; j < 50; ++j) { @@ -355,7 +355,7 @@ class TExceptionTest: public TTestBase { } void TestTIoSystemErrorSpecialMethods() { - TString testStr{"systemError"}; + std::string testStr{"systemError"}; TIoSystemError err; err << testStr; UNIT_ASSERT(err.AsStrBuf().Contains(testStr)); diff --git a/util/memory/blob.cpp b/util/memory/blob.cpp index 91da5cadca3..9a59aff9115 100644 --- a/util/memory/blob.cpp +++ b/util/memory/blob.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -72,12 +71,12 @@ class TStringBlobBase: public TBlob::TBase, public TRefCounted; public: - inline TStringBlobBase(const TString& s) + inline TStringBlobBase(const std::string& s) : S_(s) { } - TStringBlobBase(TString&& s) noexcept + TStringBlobBase(std::string&& s) noexcept : S_(std::move(s)) { } @@ -92,12 +91,12 @@ class TStringBlobBase: public TBlob::TBase, public TRefCounted @@ -109,7 +108,7 @@ class TMappedBlobBase: public TBlob::TBase, public TRefCounted(map, 0, static_cast(toMap), mode); } -TBlob TBlob::FromFileSingleThreaded(const TString& path, EMappingMode mode) { +TBlob TBlob::FromFileSingleThreaded(const std::string& path, EMappingMode mode) { return ConstructAsMap(path, mode); } -TBlob TBlob::FromFile(const TString& path, EMappingMode mode) { +TBlob TBlob::FromFile(const std::string& path, EMappingMode mode) { return ConstructAsMap(path, mode); } @@ -236,11 +235,11 @@ TBlob TBlob::FromFile(const TFile& file, EMappingMode mode) { return ConstructAsMap(file, mode); } -TBlob TBlob::FromFileSingleThreaded(const TString& path) { +TBlob TBlob::FromFileSingleThreaded(const std::string& path) { return ConstructAsMap(path, EMappingMode::Standard); } -TBlob TBlob::FromFile(const TString& path) { +TBlob TBlob::FromFile(const std::string& path) { return ConstructAsMap(path, EMappingMode::Standard); } @@ -252,11 +251,11 @@ TBlob TBlob::FromFile(const TFile& file) { return ConstructAsMap(file, EMappingMode::Standard); } -TBlob TBlob::PrechargedFromFileSingleThreaded(const TString& path) { +TBlob TBlob::PrechargedFromFileSingleThreaded(const std::string& path) { return ConstructAsMap(path, EMappingMode::Precharged); } -TBlob TBlob::PrechargedFromFile(const TString& path) { +TBlob TBlob::PrechargedFromFile(const std::string& path) { return ConstructAsMap(path, EMappingMode::Precharged); } @@ -268,11 +267,11 @@ TBlob TBlob::PrechargedFromFile(const TFile& file) { return ConstructAsMap(file, EMappingMode::Precharged); } -TBlob TBlob::LockedFromFileSingleThreaded(const TString& path) { +TBlob TBlob::LockedFromFileSingleThreaded(const std::string& path) { return ConstructAsMap(path, EMappingMode::Locked); } -TBlob TBlob::LockedFromFile(const TString& path) { +TBlob TBlob::LockedFromFile(const std::string& path) { return ConstructAsMap(path, EMappingMode::Locked); } @@ -324,12 +323,12 @@ static inline TBlob ConstructFromFileContent(const TFile& file, ui64 offset, ui6 return ReadFromFile(file, offset, static_cast(length)); } -TBlob TBlob::FromFileContentSingleThreaded(const TString& path) { +TBlob TBlob::FromFileContentSingleThreaded(const std::string& path) { TFile file(path, RdOnly); return ConstructFromFileContent(file, 0, file.GetLength()); } -TBlob TBlob::FromFileContent(const TString& path) { +TBlob TBlob::FromFileContent(const std::string& path) { TFile file(path, RdOnly); return ConstructFromFileContent(file, 0, file.GetLength()); } @@ -401,18 +400,18 @@ TBlob ConstructFromString(S&& s) { return ret; } -TBlob TBlob::FromStringSingleThreaded(const TString& s) { +TBlob TBlob::FromStringSingleThreaded(const std::string& s) { return ConstructFromString(s); } -TBlob TBlob::FromStringSingleThreaded(TString&& s) { +TBlob TBlob::FromStringSingleThreaded(std::string&& s) { return ConstructFromString(std::move(s)); } -TBlob TBlob::FromString(const TString& s) { +TBlob TBlob::FromString(const std::string& s) { return ConstructFromString(s); } -TBlob TBlob::FromString(TString&& s) { +TBlob TBlob::FromString(std::string&& s) { return ConstructFromString(std::move(s)); } diff --git a/util/memory/blob.h b/util/memory/blob.h index 20c02a68dfb..de522a51713 100644 --- a/util/memory/blob.h +++ b/util/memory/blob.h @@ -1,10 +1,11 @@ #pragma once #include -#include #include #include +#include + class TMemoryMap; class IInputStream; class TFile; @@ -136,8 +137,8 @@ class TBlob { return (const unsigned char*)Data(); } - inline TStringBuf AsStringBuf() const noexcept { - return TStringBuf(AsCharPtr(), size()); + inline std::string_view AsStringBuf() const noexcept { + return std::string_view(AsCharPtr(), size()); } /// Drops the data array. @@ -201,10 +202,10 @@ class TBlob { static TBlob NoCopy(const void* data, size_t length); /// Creates a blob with a single-threaded (non atomic) refcounter. It maps the file on the path as data. - static TBlob FromFileSingleThreaded(const TString& path, EMappingMode); + static TBlob FromFileSingleThreaded(const std::string& path, EMappingMode); /// Creates a blob with a multi-threaded (atomic) refcounter. It maps the file on the path as data. - static TBlob FromFile(const TString& path, EMappingMode); + static TBlob FromFile(const std::string& path, EMappingMode); /// Creates a blob with a single-threaded (non atomic) refcounter. It maps the file on the path as data. static TBlob FromFileSingleThreaded(const TFile& file, EMappingMode); @@ -213,10 +214,10 @@ class TBlob { static TBlob FromFile(const TFile& file, EMappingMode); /// Creates a blob with a single-threaded (non atomic) refcounter. It maps the file on the path as data. - static TBlob FromFileSingleThreaded(const TString& path); + static TBlob FromFileSingleThreaded(const std::string& path); /// Creates a blob with a multi-threaded (atomic) refcounter. It maps the file on the path as data. - static TBlob FromFile(const TString& path); + static TBlob FromFile(const std::string& path); /// Creates a blob with a single-threaded (non atomic) refcounter. It maps the file on the path as data. static TBlob FromFileSingleThreaded(const TFile& file); @@ -227,10 +228,10 @@ class TBlob { // TODO: drop Precharged* functions. /// Creates a precharged blob with a single-threaded (non atomic) refcounter. It maps the file on the path as data. - static TBlob PrechargedFromFileSingleThreaded(const TString& path); + static TBlob PrechargedFromFileSingleThreaded(const std::string& path); /// Creates a precharged blob with a multi-threaded (atomic) refcounter. It maps the file on the path as data. - static TBlob PrechargedFromFile(const TString& path); + static TBlob PrechargedFromFile(const std::string& path); /// Creates a precharged blob with a single-threaded (non atomic) refcounter. It maps the file content as data. static TBlob PrechargedFromFileSingleThreaded(const TFile& file); @@ -239,10 +240,10 @@ class TBlob { static TBlob PrechargedFromFile(const TFile& file); /// Creates a locked blob with a single-threaded (non atomic) refcounter. It maps the file on the path as data. - static TBlob LockedFromFileSingleThreaded(const TString& path); + static TBlob LockedFromFileSingleThreaded(const std::string& path); /// Creates a locked blob with a multi-threaded (atomic) refcounter. It maps the file on the path as data. - static TBlob LockedFromFile(const TString& path); + static TBlob LockedFromFile(const std::string& path); /// Creates a locked blob with a single-threaded (non atomic) refcounter. It maps the file content as data. static TBlob LockedFromFileSingleThreaded(const TFile& file); @@ -263,10 +264,10 @@ class TBlob { static TBlob FromMemoryMap(const TMemoryMap& map, ui64 offset, size_t length); /// Creates a blob with a single-threaded (non atomic) refcounter. Dynamically allocates memory and copies data from the file on the path using pread(). - static TBlob FromFileContentSingleThreaded(const TString& path); + static TBlob FromFileContentSingleThreaded(const std::string& path); /// Creates a blob with a multi-threaded (atomic) refcounter. Dynamically allocates memory and copies data from the file on the path using pread(). - static TBlob FromFileContent(const TString& path); + static TBlob FromFileContent(const std::string& path); /// Creates a blob with a single-threaded (non atomic) refcounter. Dynamically allocates memory and copies data from the file using pread(). static TBlob FromFileContentSingleThreaded(const TFile& file); @@ -294,17 +295,17 @@ class TBlob { /// @details The input object becomes empty. static TBlob FromBuffer(TBuffer& in); - /// Creates a blob from TString with a single-threaded (non atomic) refcounter. - static TBlob FromStringSingleThreaded(const TString& s); + /// Creates a blob from std::string with a single-threaded (non atomic) refcounter. + static TBlob FromStringSingleThreaded(const std::string& s); - /// Creates a blob from TString with a single-threaded (non atomic) refcounter. Doesn't copy its content. - static TBlob FromStringSingleThreaded(TString&& s); + /// Creates a blob from std::string with a single-threaded (non atomic) refcounter. Doesn't copy its content. + static TBlob FromStringSingleThreaded(std::string&& s); - /// Creates a blob from TString with a multi-threaded (atomic) refcounter. - static TBlob FromString(const TString& s); + /// Creates a blob from std::string with a multi-threaded (atomic) refcounter. + static TBlob FromString(const std::string& s); - /// Creates a blob from TString with a multi-threaded (atomic) refcounter. Doesn't copy its content. - static TBlob FromString(TString&& s); + /// Creates a blob from std::string with a multi-threaded (atomic) refcounter. Doesn't copy its content. + static TBlob FromString(std::string&& s); private: inline void Ref() noexcept { diff --git a/util/memory/blob.pxd b/util/memory/blob.pxd index c68525024ac..9e224a549fc 100644 --- a/util/memory/blob.pxd +++ b/util/memory/blob.pxd @@ -1,6 +1,5 @@ from libcpp cimport bool as bool_t -from util.generic.string cimport TString from util.system.types cimport ui8 @@ -27,13 +26,13 @@ cdef extern from "util/memory/blob.h" nogil: TBlob Copy(const void* data, size_t length) except + @staticmethod - TBlob FromFile(const TString& path) except + + TBlob FromFile(const std::string& path) except + @staticmethod - TBlob PrechargedFromFile(const TString& path) except + + TBlob PrechargedFromFile(const std::string& path) except + @staticmethod - TBlob FromString(const TString& s) except + + TBlob FromString(const std::string& s) except + ui8& operator[](size_t) const TBlob& operator=(TBlob&) diff --git a/util/memory/blob_ut.cpp b/util/memory/blob_ut.cpp index e048f8617a8..90c0cec1bbe 100644 --- a/util/memory/blob_ut.cpp +++ b/util/memory/blob_ut.cpp @@ -28,18 +28,18 @@ Y_UNIT_TEST_SUITE(TBlobTest) { } Y_UNIT_TEST(TestFromStream) { - TString s("sjklfgsdyutfuyas54fa78s5f89a6df790asdf7"); + std::string s("sjklfgsdyutfuyas54fa78s5f89a6df790asdf7"); TMemoryInput mi(s.data(), s.size()); TBlob b = TBlob::FromStreamSingleThreaded(mi); - UNIT_ASSERT_EQUAL(TString((const char*)b.Data(), b.Length()), s); + UNIT_ASSERT_EQUAL(std::string((const char*)b.Data(), b.Length()), s); } Y_UNIT_TEST(TestFromString) { - TString s("dsfkjhgsadftusadtf"); + std::string s("dsfkjhgsadftusadtf"); TBlob b(TBlob::FromString(s)); - UNIT_ASSERT_EQUAL(TString((const char*)b.Data(), b.Size()), s); + UNIT_ASSERT_EQUAL(std::string((const char*)b.Data(), b.Size()), s); const auto expectedRef = TArrayRef{(ui8*)s.data(), s.size()}; UNIT_ASSERT_EQUAL(TArrayRef{b}, expectedRef); } @@ -55,7 +55,7 @@ Y_UNIT_TEST_SUITE(TBlobTest) { } Y_UNIT_TEST(TestFromFile) { - TString path = "testfile"; + std::string path = "testfile"; TOFStream stream(path); stream.Write("1234", 4); @@ -63,7 +63,7 @@ Y_UNIT_TEST_SUITE(TBlobTest) { auto testMode = [](TBlob blob) { UNIT_ASSERT_EQUAL(blob.Size(), 4); - UNIT_ASSERT_EQUAL(TStringBuf(static_cast(blob.Data()), 4), "1234"); + UNIT_ASSERT_EQUAL(std::string_view(static_cast(blob.Data()), 4), "1234"); }; testMode(TBlob::FromFile(path)); @@ -72,7 +72,7 @@ Y_UNIT_TEST_SUITE(TBlobTest) { } Y_UNIT_TEST(TestEmptyLockedFiles) { - TString path = MakeTempName(); + std::string path = MakeTempName(); TFsPath(path).Touch(); TBlob::LockedFromFile(path); } diff --git a/util/memory/blob_ut.pyx b/util/memory/blob_ut.pyx index b4b501adc65..d2a2fad6a46 100644 --- a/util/memory/blob_ut.pyx +++ b/util/memory/blob_ut.pyx @@ -1,7 +1,6 @@ # cython: c_string_type=str, c_string_encoding=utf8 from libcpp.string cimport string as std_string -from util.generic.string cimport TString from util.memory.blob cimport TBlob import pytest @@ -52,7 +51,7 @@ class TestBlob(unittest.TestCase): self.assertEqual(tmp.IsNull(), False) def test_from_string(self): - cdef TBlob tmp = TBlob.FromString(TString("hello world")) + cdef TBlob tmp = TBlob.FromString(std::string("hello world")) self.assertEqual(tmp.Size(), 11) self.assertEqual(tmp.AsCharPtr()[:tmp.Size()], "hello world") self.assertEqual(tmp.Empty(), False) diff --git a/util/memory/pool.h b/util/memory/pool.h index a7f09cd37b0..63cecb6d3ad 100644 --- a/util/memory/pool.h +++ b/util/memory/pool.h @@ -7,7 +7,6 @@ #include #include #include -#include #include #include @@ -219,17 +218,17 @@ class TMemoryPool { } template - inline TBasicStringBuf AppendString(const TBasicStringBuf& buf) { - return TBasicStringBuf(Append(buf.data(), buf.size()), buf.size()); + inline std::basic_string_view AppendString(const std::basic_string_view& buf) { + return std::basic_string_view(Append(buf.data(), buf.size()), buf.size()); } template - inline TBasicStringBuf AppendCString(const TBasicStringBuf& buf) { + inline std::basic_string_view AppendCString(const std::basic_string_view& buf) { TChar* ret = static_cast(Allocate((buf.size() + 1) * sizeof(TChar))); std::char_traits::copy(ret, buf.data(), buf.size()); *(ret + buf.size()) = 0; - return TBasicStringBuf(ret, buf.size()); + return std::basic_string_view(ret, buf.size()); } inline size_t Available() const noexcept { diff --git a/util/memory/pool_ut.cpp b/util/memory/pool_ut.cpp index 627023d3bbb..7ea5eb71568 100644 --- a/util/memory/pool_ut.cpp +++ b/util/memory/pool_ut.cpp @@ -146,11 +146,11 @@ class TMemPoolTest: public TTestBase { : ConstructorType(2) { } - TConstructorTest(const TString&, const TString&) + TConstructorTest(const std::string&, const std::string&) : ConstructorType(3) { } - TConstructorTest(TString&&, TString&&) + TConstructorTest(std::string&&, std::string&&) : ConstructorType(4) { } diff --git a/util/memory/segmented_string_pool.h b/util/memory/segmented_string_pool.h index 60315521539..a4913aeec69 100644 --- a/util/memory/segmented_string_pool.h +++ b/util/memory/segmented_string_pool.h @@ -4,12 +4,11 @@ #include #include #include -#include -#include #include #include #include +#include /* * Non-reallocated storage for the objects of POD type @@ -171,7 +170,7 @@ class segmented_string_pool: public segmented_pool { rv[len] = 0; return rv; } - char* Append(const TStringBuf s) { + char* Append(const std::string_view s) { return append(s.data(), s.size()); } void align_4() { diff --git a/util/memory/tempbuf_ut.cpp b/util/memory/tempbuf_ut.cpp index d6bcf9d5461..f501710ee4b 100644 --- a/util/memory/tempbuf_ut.cpp +++ b/util/memory/tempbuf_ut.cpp @@ -27,7 +27,7 @@ class TTempBufTest: public TTestBase { tmp.Append("def", 3); UNIT_ASSERT_EQUAL(tmp.Filled(), 6); - UNIT_ASSERT_EQUAL(TString(tmp.Data(), tmp.Filled()), "abcdef"); + UNIT_ASSERT_EQUAL(std::string(tmp.Data(), tmp.Filled()), "abcdef"); } }; diff --git a/util/network/address.cpp b/util/network/address.cpp index 47badf504f2..f10377a49f7 100644 --- a/util/network/address.cpp +++ b/util/network/address.cpp @@ -53,7 +53,7 @@ static inline void PrintAddr(IOutputStream& out, const IRemoteAddr& addr) { case AF_UNIX: { const sockaddr_un* sa = (const sockaddr_un*)a; - out << TStringBuf(sa->sun_path); + out << std::string_view(sa->sun_path); break; } @@ -123,13 +123,13 @@ void NAddr::PrintHost(IOutputStream& out, const IRemoteAddr& addr) { PrintAddr(out, addr); } -TString NAddr::PrintHost(const IRemoteAddr& addr) { +std::string NAddr::PrintHost(const IRemoteAddr& addr) { TStringStream ss; PrintAddr(ss, addr); return ss.Str(); } -TString NAddr::PrintHostAndPort(const IRemoteAddr& addr) { +std::string NAddr::PrintHostAndPort(const IRemoteAddr& addr) { TStringStream ss; PrintAddr(ss, addr); return ss.Str(); @@ -208,8 +208,8 @@ socklen_t NAddr::SockAddrLength(const sockaddr* addr) { ythrow yexception() << "unsupported address family: " << addr->sa_family; } -TUnixSocketAddr::TUnixSocketAddr(TStringBuf path) { - Y_ENSURE(path.Size() <= UNIX_PATH_LIMIT - 1, +TUnixSocketAddr::TUnixSocketAddr(std::string_view path) { + Y_ENSURE(path.size() <= UNIX_PATH_LIMIT - 1, "Unix path \"" << path << "\" is longer than " << UNIX_PATH_LIMIT); SockAddr_.Set(path); } diff --git a/util/network/address.h b/util/network/address.h index fac61ce665a..c444d09d01b 100644 --- a/util/network/address.h +++ b/util/network/address.h @@ -4,7 +4,6 @@ #include "socket.h" #include -#include #include namespace NAddr { @@ -23,8 +22,8 @@ namespace NAddr { IRemoteAddrPtr GetPeerAddr(SOCKET s); void PrintHost(IOutputStream& out, const IRemoteAddr& addr); - TString PrintHost(const IRemoteAddr& addr); - TString PrintHostAndPort(const IRemoteAddr& addr); + std::string PrintHost(const IRemoteAddr& addr); + std::string PrintHostAndPort(const IRemoteAddr& addr); bool IsLoopback(const IRemoteAddr& addr); bool IsSame(const IRemoteAddr& lhs, const IRemoteAddr& rhs); @@ -137,7 +136,7 @@ namespace NAddr { class TUnixSocketAddr: public IRemoteAddr { public: - explicit TUnixSocketAddr(TStringBuf path); + explicit TUnixSocketAddr(std::string_view path); const sockaddr* Addr() const override; diff --git a/util/network/address_ut.cpp b/util/network/address_ut.cpp index 28f45172ffe..94bb07dfcaa 100644 --- a/util/network/address_ut.cpp +++ b/util/network/address_ut.cpp @@ -9,12 +9,12 @@ Y_UNIT_TEST_SUITE(IRemoteAddr_ToString) { THolder opaque(new TOpaqueAddr); IRemoteAddr* addr = opaque.Get(); - TString s = ToString(*addr); + std::string s = ToString(*addr); UNIT_ASSERT_VALUES_EQUAL("(raw all zeros)", s); opaque->MutableAddr()->sa_data[10] = 17; - TString t = ToString(*addr); + std::string t = ToString(*addr); UNIT_ASSERT_C(t.StartsWith("(raw 0 0"), t); UNIT_ASSERT_C(t.EndsWith(')'), t); @@ -25,8 +25,8 @@ Y_UNIT_TEST_SUITE(IRemoteAddr_ToString) { TNetworkAddress::TIterator it = address.Begin(); UNIT_ASSERT(it != address.End()); UNIT_ASSERT(it->ai_family == AF_INET6); - TString toString = ToString((const IRemoteAddr&)TAddrInfo(&*it)); - UNIT_ASSERT_VALUES_EQUAL(TString("[::1]:22"), toString); + std::string toString = ToString((const IRemoteAddr&)TAddrInfo(&*it)); + UNIT_ASSERT_VALUES_EQUAL(std::string("[::1]:22"), toString); } Y_UNIT_TEST(Loopback) { diff --git a/util/network/endpoint.cpp b/util/network/endpoint.cpp index 9acdd069408..8843dd588af 100644 --- a/util/network/endpoint.cpp +++ b/util/network/endpoint.cpp @@ -7,7 +7,7 @@ TEndpoint::TEndpoint(const TEndpoint::TAddrRef& addr) const sockaddr* sa = Addr_->Addr(); if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6 && sa->sa_family != AF_UNIX) { - ythrow yexception() << TStringBuf("endpoint can contain only ipv4, ipv6 or unix address"); + ythrow yexception() << std::string_view("endpoint can contain only ipv4, ipv6 or unix address"); } } @@ -60,7 +60,7 @@ size_t TEndpoint::Hash() const { return IntHash(ptr[0] ^ ptr[1] ^ sa6->sin6_port); } else { const sockaddr_un* un = (const sockaddr_un*)sa; - THash strHash; + THash strHash; return strHash(un->sun_path); } diff --git a/util/network/endpoint.h b/util/network/endpoint.h index a3e59b49250..47a67824e19 100644 --- a/util/network/endpoint.h +++ b/util/network/endpoint.h @@ -32,7 +32,7 @@ class TEndpoint { return Addr_->Addr()->sa_family == AF_UNIX; } - inline TString IpToString() const { + inline std::string IpToString() const { return NAddr::PrintHost(*Addr_); } diff --git a/util/network/endpoint_ut.cpp b/util/network/endpoint_ut.cpp index 5e552b1e19a..69a1c650060 100644 --- a/util/network/endpoint_ut.cpp +++ b/util/network/endpoint_ut.cpp @@ -3,7 +3,6 @@ #include #include -#include Y_UNIT_TEST_SUITE(TEndpointTest) { Y_UNIT_TEST(TestSimple) { @@ -53,8 +52,8 @@ Y_UNIT_TEST_SUITE(TEndpointTest) { TEndpoint ep3(new NAddr::TAddrInfo(&*na3.Begin())); UNIT_ASSERT(ep3.IsIpV6()); - UNIT_ASSERT(ep3.IpToString().StartsWith(TStringBuf("2a02:6b8:0:1410:"))); - UNIT_ASSERT(ep3.IpToString().EndsWith(TStringBuf(":5f6c:f3c2"))); + UNIT_ASSERT(ep3.IpToString().StartsWith(std::string_view("2a02:6b8:0:1410:"))); + UNIT_ASSERT(ep3.IpToString().EndsWith(std::string_view(":5f6c:f3c2"))); UNIT_ASSERT_VALUES_EQUAL(54321, ep3.Port()); TNetworkAddress na4("2a02:6b8:0:1410:0::5f6c:f3c2", 1); @@ -87,8 +86,8 @@ Y_UNIT_TEST_SUITE(TEndpointTest) { } Y_UNIT_TEST(TestEqual) { - const TString ip1 = "2a02:6b8:0:1410::5f6c:f3c2"; - const TString ip2 = "2a02:6b8:0:1410::5f6c:f3c3"; + const std::string ip1 = "2a02:6b8:0:1410::5f6c:f3c2"; + const std::string ip2 = "2a02:6b8:0:1410::5f6c:f3c3"; TNetworkAddress na1(ip1, 24242); TEndpoint ep1(new NAddr::TAddrInfo(&*na1.Begin())); diff --git a/util/network/interface.h b/util/network/interface.h index 4d801cc94ab..db49edcd772 100644 --- a/util/network/interface.h +++ b/util/network/interface.h @@ -2,11 +2,11 @@ #include "address.h" -#include +#include namespace NAddr { struct TNetworkInterface { - TString Name; + std::string Name; IRemoteAddrRef Address; IRemoteAddrRef Mask; }; diff --git a/util/network/ip.h b/util/network/ip.h index dc7c2d24a06..8e79ea59f55 100644 --- a/util/network/ip.h +++ b/util/network/ip.h @@ -5,9 +5,10 @@ #include #include -#include #include +#include + /// IPv4 address in network format using TIpHost = ui32; @@ -36,15 +37,15 @@ static inline char* IpToString(TIpHost ip, char* buf, size_t len) { return buf; } -static inline TString IpToString(TIpHost ip) { +static inline std::string IpToString(TIpHost ip) { char buf[INET_ADDRSTRLEN]; - return TString(IpToString(ip, buf, sizeof(buf))); + return std::string(IpToString(ip, buf, sizeof(buf))); } static inline TIpHost ResolveHost(const char* data, size_t len) { TIpHost ret; - const TString s(data, len); + const std::string s(data, len); if (NResolver::GetHostIP(s.data(), &ret) != 0) { ythrow TSystemError(NResolver::GetDnsError()) << "can not resolve(" << s << ")"; @@ -69,7 +70,7 @@ struct TIpAddress: public sockaddr_in { Set(ip, port); } - inline TIpAddress(TStringBuf ip, TIpPort port) { + inline TIpAddress(std::string_view ip, TIpPort port) { Set(ResolveHost(ip.data(), ip.size()), port); } diff --git a/util/network/poller.cpp b/util/network/poller.cpp index 7954d0e8b55..6018628c570 100644 --- a/util/network/poller.cpp +++ b/util/network/poller.cpp @@ -3,9 +3,11 @@ #include +#include + namespace { struct TMutexLocking { - using TMyMutex = TMutex; + using TMyMutex = std::mutex; }; } diff --git a/util/network/pollerimpl.h b/util/network/pollerimpl.h index 801b4b7b22d..a4fff8a7fa5 100644 --- a/util/network/pollerimpl.h +++ b/util/network/pollerimpl.h @@ -3,14 +3,15 @@ #include "socket.h" #include -#include +#include #include #include #include -#include #include #include +#include + #if defined(_freebsd_) || defined(_darwin_) #define HAVE_KQUEUE_POLLER #endif @@ -433,7 +434,8 @@ class TSelectPoller: public TSelectPollerNoTemplate { } inline void SetImpl(void* data, SOCKET fd, int what) { - with_lock (CommandLock_) { + { + std::lock_guard guard(CommandLock_); Commands_.push_back(TCommand(fd, what, data)); } @@ -441,7 +443,8 @@ class TSelectPoller: public TSelectPollerNoTemplate { } inline void Remove(SOCKET fd) noexcept { - with_lock (CommandLock_) { + { + std::lock_guard guard(CommandLock_); Commands_.push_back(TCommand(fd, 0)); } @@ -481,7 +484,8 @@ class TSelectPoller: public TSelectPollerNoTemplate { } inline size_t WaitBase(TEvent* events, size_t len, int timeout) noexcept { - with_lock (CommandLock_) { + { + std::lock_guard guard(CommandLock_); for (auto command = Commands_.begin(); command != Commands_.end(); ++command) { if (command->Filter_ != 0) { Fds_.Set(command->Fd_, command->Cookie_, command->Filter_); diff --git a/util/network/sock.h b/util/network/sock.h index b9b73b2d377..1bd5948dda9 100644 --- a/util/network/sock.h +++ b/util/network/sock.h @@ -30,7 +30,7 @@ struct ISockAddr { virtual sockaddr* SockAddr() = 0; virtual const sockaddr* SockAddr() const = 0; // address in human readable form - virtual TString ToString() const = 0; + virtual std::string ToString() const = 0; protected: // below are the implemetation methods that can be called by T*Socket classes @@ -77,7 +77,7 @@ struct TSockAddrLocal: public ISockAddr { strlcpy(Path, path, PathSize); } - inline void Set(TStringBuf path) noexcept { + inline void Set(std::string_view path) noexcept { Clear(); in.sin_family = AF_INET; in.sin_addr.s_addr = IpFromString("127.0.0.1"); @@ -93,8 +93,8 @@ struct TSockAddrLocal: public ISockAddr { return (const struct sockaddr*)(&in); } - TString ToString() const { - return TString(Path); + std::string ToString() const { + return std::string(Path); } TFsPath ToPath() const { @@ -156,7 +156,7 @@ struct TSockAddrLocal: public sockaddr_un, public ISockAddr { Clear(); } - TSockAddrLocal(TStringBuf path) { + TSockAddrLocal(std::string_view path) { Set(path); } @@ -182,7 +182,7 @@ struct TSockAddrLocal: public sockaddr_un, public ISockAddr { strlcpy(sun_path, path, sizeof(sun_path)); } - inline void Set(TStringBuf path) noexcept { + inline void Set(std::string_view path) noexcept { Clear(); sun_family = AF_UNIX; strlcpy(sun_path, path.data(), Min(sizeof(sun_path), path.size() + 1)); @@ -196,8 +196,8 @@ struct TSockAddrLocal: public sockaddr_un, public ISockAddr { return (const struct sockaddr*)(const struct sockaddr_un*)this; } - TString ToString() const override { - return TString(sun_path); + std::string ToString() const override { + return std::string(sun_path); } TFsPath ToPath() const { @@ -259,7 +259,7 @@ struct TSockAddrInet: public sockaddr_in, public ISockAddr { return (const struct sockaddr*)(const struct sockaddr_in*)this; } - TString ToString() const override { + std::string ToString() const override { return IpToString(sin_addr.s_addr) + ":" + ::ToString(InetToHost(sin_port)); } @@ -325,7 +325,7 @@ struct TSockAddrInet6: public sockaddr_in6, public ISockAddr { return (const struct sockaddr*)(const struct sockaddr_in6*)this; } - TString ToString() const override { + std::string ToString() const override { return "[" + GetIp() + "]:" + ::ToString(InetToHost(sin6_port)); } @@ -342,10 +342,10 @@ struct TSockAddrInet6: public sockaddr_in6, public ISockAddr { return 0; } - TString GetIp() const noexcept { + std::string GetIp() const noexcept { char ip6[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, (void*)&sin6_addr, ip6, INET6_ADDRSTRLEN); - return TString(ip6); + return std::string(ip6); } TIpPort GetPort() const noexcept { diff --git a/util/network/sock_ut.cpp b/util/network/sock_ut.cpp index c702e1ba04d..b59bdf2a84c 100644 --- a/util/network/sock_ut.cpp +++ b/util/network/sock_ut.cpp @@ -167,9 +167,9 @@ Y_UNIT_TEST_SUITE(TSocketTest) { } Y_UNIT_TEST(DetermingPath) { - const TString connectionString = "/var/run/some.sock http://localhost/endpoint"; + const std::string connectionString = "/var/run/some.sock http://localhost/endpoint"; - TStringBuf sockPath, endpoint; + std::string_view sockPath, endpoint; StringSplitter(connectionString).Split(' ').SkipEmpty().CollectInto(&sockPath, &endpoint); TSockAddrLocal sal; diff --git a/util/network/socket.cpp b/util/network/socket.cpp index f6b1132222c..f57bd630ac7 100644 --- a/util/network/socket.cpp +++ b/util/network/socket.cpp @@ -947,7 +947,7 @@ void TSocketOutput::DoWriteV(const TPart* parts, size_t count) { namespace { //https://bugzilla.mozilla.org/attachment.cgi?id=503263&action=diff - struct TLocalNames: public THashSet { + struct TLocalNames: public THashSet { inline TLocalNames() { insert("localhost"); insert("localhost.localdomain"); @@ -1004,7 +1004,7 @@ class TNetworkAddress::TImpl: public TAtomicRefCount { inline TImpl(const char* host, ui16 port, int flags) : Info_(nullptr, TAddrInfoDeleter{}) { - const TString port_st(ToString(port)); + const std::string port_st(ToString(port)); struct addrinfo hints; memset(&hints, 0, sizeof(hints)); @@ -1069,12 +1069,12 @@ TNetworkAddress::TNetworkAddress(const TUnixSocketPath& unixSocketPath, int flag { } -TNetworkAddress::TNetworkAddress(const TString& host, ui16 port, int flags) +TNetworkAddress::TNetworkAddress(const std::string& host, ui16 port, int flags) : Impl_(new TImpl(host.data(), port, flags)) { } -TNetworkAddress::TNetworkAddress(const TString& host, ui16 port) +TNetworkAddress::TNetworkAddress(const std::string& host, ui16 port) : Impl_(new TImpl(host.data(), port, 0)) { } diff --git a/util/network/socket.h b/util/network/socket.h index 40c8648b409..e80b2c7fd16 100644 --- a/util/network/socket.h +++ b/util/network/socket.h @@ -153,11 +153,11 @@ class TNetworkResolutionError: public yexception { }; struct TUnixSocketPath { - TString Path; + std::string Path; // Constructor for create unix domain socket path from string with path in filesystem // TUnixSocketPath("/tmp/unixsocket") -> "/tmp/unixsocket" - explicit TUnixSocketPath(const TString& path) + explicit TUnixSocketPath(const std::string& path) : Path(path) { } @@ -213,8 +213,8 @@ class TNetworkAddress { }; TNetworkAddress(ui16 port); - TNetworkAddress(const TString& host, ui16 port); - TNetworkAddress(const TString& host, ui16 port, int flags); + TNetworkAddress(const std::string& host, ui16 port); + TNetworkAddress(const std::string& host, ui16 port, int flags); TNetworkAddress(const TUnixSocketPath& unixSocketPath, int flags = 0); ~TNetworkAddress(); diff --git a/util/network/socket_ut.cpp b/util/network/socket_ut.cpp index 1a15cc10251..cf368de3fa6 100644 --- a/util/network/socket_ut.cpp +++ b/util/network/socket_ut.cpp @@ -46,7 +46,7 @@ void TSockTest::TestSock() { TSocket s(addr); TSocketOutput so(s); TSocketInput si(s); - const TStringBuf req = "GET / HTTP/1.1\r\nHost: yandex.ru\r\n\r\n"; + const std::string_view req = "GET / HTTP/1.1\r\nHost: yandex.ru\r\n\r\n"; so.Write(req.data(), req.size()); @@ -63,7 +63,7 @@ void TSockTest::TestTimeout() { } int realTimeout = (int)(millisec() - startTime); if (realTimeout > timeout + 2000) { - TString err = TStringBuilder() << "Timeout exceeded: " << realTimeout << " ms (expected " << timeout << " ms)"; + std::string err = TStringBuilder() << "Timeout exceeded: " << realTimeout << " ms (expected " << timeout << " ms)"; UNIT_FAIL(err); } } @@ -74,7 +74,7 @@ void TSockTest::TestConnectionRefused() { } void TSockTest::TestNetworkResolutionError() { - TString errMsg; + std::string errMsg; try { TNetworkAddress addr("", 0); } catch (const TNetworkResolutionError& e) { @@ -86,19 +86,19 @@ void TSockTest::TestNetworkResolutionError() { } int expectedErr = EAI_NONAME; - TString expectedErrMsg = gai_strerror(expectedErr); - if (errMsg.find(expectedErrMsg) == TString::npos) { + std::string expectedErrMsg = gai_strerror(expectedErr); + if (errMsg.find(expectedErrMsg) == std::string::npos) { UNIT_FAIL("TNetworkResolutionError contains\nInvalid msg: " + errMsg + "\nExpected msg: " + expectedErrMsg + "\n"); } } void TSockTest::TestNetworkResolutionErrorMessage() { #ifdef _unix_ - auto str = [](int code) -> TString { + auto str = [](int code) -> std::string { return TNetworkResolutionError(code).what(); }; - auto expected = [](int code) -> TString { + auto expected = [](int code) -> std::string { return gai_strerror(code); }; @@ -189,7 +189,7 @@ void TSockTest::TestReusePortAvailCheck() { #if defined _linux_ utsname sysInfo; Y_ABORT_UNLESS(!uname(&sysInfo), "Error while call uname: %s", LastSystemErrorText()); - TStringBuf release(sysInfo.release); + std::string_view release(sysInfo.release); release = release.substr(0, release.find_first_not_of(".0123456789")); int v1 = FromString(release.NextTok('.')); int v2 = FromString(release.NextTok('.')); diff --git a/util/random/entropy.cpp b/util/random/entropy.cpp index 3617edb83d7..d0a163caf44 100644 --- a/util/random/entropy.cpp +++ b/util/random/entropy.cpp @@ -82,7 +82,7 @@ namespace { { ui32 store[12]; - out << TStringBuf(CpuBrand(store)); + out << std::string_view(CpuBrand(store)); } out << NFs::CurrentWorkingDirectory(); diff --git a/util/random/shuffle_ut.cpp b/util/random/shuffle_ut.cpp index 8cab95d8b25..ca85667c4bf 100644 --- a/util/random/shuffle_ut.cpp +++ b/util/random/shuffle_ut.cpp @@ -9,7 +9,7 @@ Y_UNIT_TEST_SUITE(TRandUtilsTest) { template static void TestRange(A&&... args) { - TString s0, s1; + std::string s0, s1; ShuffleRange(s1, args...); s1 = "0"; ShuffleRange(s1, args...); @@ -23,7 +23,7 @@ Y_UNIT_TEST_SUITE(TRandUtilsTest) { template static void TestIter(A&&... args) { - TString s0, s1; + std::string s0, s1; auto f = [&]() { auto b = s1.begin(); @@ -47,7 +47,7 @@ Y_UNIT_TEST_SUITE(TRandUtilsTest) { template static void TestIterPartial(A&&... args) { - TString s0, s1; + std::string s0, s1; auto f = [&](int shuffledSize) { auto b = s1.begin(); diff --git a/util/str_stl.h b/util/str_stl.h index d71633ee641..ec1acdf7c63 100644 --- a/util/str_stl.h +++ b/util/str_stl.h @@ -1,13 +1,15 @@ #pragma once +#include + #include #include -#include +#include #include -#include #include #include +#include #include #include @@ -23,9 +25,6 @@ namespace std { bool operator()(const char* x, const char* y) const { return strcmp(x, y) == 0; } - bool operator()(const char* x, const TStringBuf y) const { - return strlen(x) == y.size() && memcmp(x, y.data(), y.size()) == 0; - } using is_transparent = void; }; } @@ -51,7 +50,7 @@ namespace NHashPrivate { struct TStringHash { using is_transparent = void; - inline size_t operator()(const TBasicStringBuf s) const noexcept { + inline size_t operator()(const std::basic_string_view s) const noexcept { return NHashPrivate::ComputeStringHash(s.data(), s.size()); } }; @@ -80,32 +79,28 @@ template struct hash: ::NHashPrivate::TStringHash { }; -template <> -struct THash: ::NHashPrivate::TStringHash { -}; - template <> struct THash: ::NHashPrivate::TStringHash { }; -template <> -struct hash: ::NHashPrivate::TStringHash { -}; +// template <> +// struct hash: ::NHashPrivate::TStringHash { +// }; -template <> -struct hash: ::NHashPrivate::TStringHash { -}; +// template <> +// struct hash: ::NHashPrivate::TStringHash { +// }; template <> -struct THash: ::NHashPrivate::TStringHash { +struct THash: ::NHashPrivate::TStringHash { }; -template <> -struct hash: ::NHashPrivate::TStringHash { -}; +// template <> +// struct hash: ::NHashPrivate::TStringHash { +// }; template <> -struct THash: ::NHashPrivate::TStringHash { +struct THash: ::NHashPrivate::TStringHash { }; template @@ -198,17 +193,12 @@ struct TEqualTo: public TEqualTo { }; template <> -struct TEqualTo: public TEqualTo { - using is_transparent = void; -}; - -template <> -struct TEqualTo: public TEqualTo { +struct TEqualTo: public TEqualTo { using is_transparent = void; }; template <> -struct TEqualTo: public TEqualTo { +struct TEqualTo: public TEqualTo { using is_transparent = void; }; @@ -233,15 +223,8 @@ struct TCIEqualTo { }; template <> -struct TCIEqualTo { - inline bool operator()(const TStringBuf a, const TStringBuf b) const { - return a.size() == b.size() && strnicmp(a.data(), b.data(), a.size()) == 0; - } -}; - -template <> -struct TCIEqualTo { - inline bool operator()(const TString& a, const TString& b) const { +struct TCIEqualTo { + inline bool operator()(const std::string& a, const std::string& b) const { return a.size() == b.size() && strnicmp(a.data(), b.data(), a.size()) == 0; } }; @@ -251,17 +234,12 @@ struct TLess: public std::less { }; template <> -struct TLess: public TLess { - using is_transparent = void; -}; - -template <> -struct TLess: public TLess { +struct TLess: public TLess { using is_transparent = void; }; template <> -struct TLess: public TLess { +struct TLess: public TLess { using is_transparent = void; }; @@ -270,16 +248,11 @@ struct TGreater: public std::greater { }; template <> -struct TGreater: public TGreater { - using is_transparent = void; -}; - -template <> -struct TGreater: public TGreater { +struct TGreater: public TGreater { using is_transparent = void; }; template <> -struct TGreater: public TGreater { +struct TGreater: public TGreater { using is_transparent = void; }; diff --git a/util/stream/aligned.cpp b/util/stream/aligned.cpp index 2fd12d15b74..981177fa0c1 100644 --- a/util/stream/aligned.cpp +++ b/util/stream/aligned.cpp @@ -12,7 +12,7 @@ size_t TAlignedInput::DoSkip(size_t len) { return ret; } -size_t TAlignedInput::DoReadTo(TString& st, char ch) { +size_t TAlignedInput::DoReadTo(std::string& st, char ch) { size_t ret = Stream_->ReadTo(st, ch); Position_ += ret; return ret; diff --git a/util/stream/aligned.h b/util/stream/aligned.h index 70e7be05a99..1853b926dcb 100644 --- a/util/stream/aligned.h +++ b/util/stream/aligned.h @@ -44,7 +44,7 @@ class TAlignedInput: public IInputStream { private: size_t DoRead(void* ptr, size_t len) override; size_t DoSkip(size_t len) override; - size_t DoReadTo(TString& st, char ch) override; + size_t DoReadTo(std::string& st, char ch) override; ui64 DoReadAll(IOutputStream& out) override; private: diff --git a/util/stream/buffer.cpp b/util/stream/buffer.cpp index ac4c85f2880..25469ed8901 100644 --- a/util/stream/buffer.cpp +++ b/util/stream/buffer.cpp @@ -1,6 +1,8 @@ #include "buffer.h" #include +#include + class TBufferOutput::TImpl { public: inline TImpl(TBuffer& buf) diff --git a/util/stream/buffer_ut.cpp b/util/stream/buffer_ut.cpp index 34946961907..aa60c133a34 100644 --- a/util/stream/buffer_ut.cpp +++ b/util/stream/buffer_ut.cpp @@ -25,7 +25,7 @@ Y_UNIT_TEST_SUITE(TBufferTest) { TBuffer buffer("1234567890", 10); TBufferInput input(buffer); - TString tmp; + std::string tmp; UNIT_ASSERT_VALUES_EQUAL(input.ReadTo(tmp, '3'), 3); UNIT_ASSERT_VALUES_EQUAL(tmp, "12"); @@ -36,7 +36,7 @@ Y_UNIT_TEST_SUITE(TBufferTest) { Y_UNIT_TEST(WriteViaNextAndUndo) { TBuffer buffer; TBufferOutput output(buffer); - TString str; + std::string str; for (size_t i = 0; i < 10000; ++i) { str.push_back('a' + (i % 20)); diff --git a/util/stream/buffered.cpp b/util/stream/buffered.cpp index c7d6647ec1d..9124b428df2 100644 --- a/util/stream/buffered.cpp +++ b/util/stream/buffered.cpp @@ -5,6 +5,8 @@ #include #include +#include + class TBufferedInput::TImpl: public TAdditionalStorage { public: inline TImpl(IInputStream* slave) @@ -62,10 +64,10 @@ class TBufferedInput::TImpl: public TAdditionalStorage { return MemInput_.Skip(len); } - inline size_t ReadTo(TString& st, char to) { + inline size_t ReadTo(std::string& st, char to) { st.clear(); - TString s_tmp; + std::string s_tmp; size_t ret = 0; @@ -139,7 +141,7 @@ size_t TBufferedInput::DoNext(const void** ptr, size_t len) { return Impl_->Next(ptr, len); } -size_t TBufferedInput::DoReadTo(TString& st, char ch) { +size_t TBufferedInput::DoReadTo(std::string& st, char ch) { return Impl_->ReadTo(st, ch); } diff --git a/util/stream/buffered.h b/util/stream/buffered.h index 0847186141f..1a799fd57b8 100644 --- a/util/stream/buffered.h +++ b/util/stream/buffered.h @@ -39,7 +39,7 @@ class TBufferedInput: public IZeroCopyInput { protected: size_t DoRead(void* buf, size_t len) override; - size_t DoReadTo(TString& st, char ch) override; + size_t DoReadTo(std::string& st, char ch) override; size_t DoSkip(size_t len) override; size_t DoNext(const void** ptr, size_t len) override; diff --git a/util/stream/buffered_ut.cpp b/util/stream/buffered_ut.cpp index 41d2fc30308..d560959913e 100644 --- a/util/stream/buffered_ut.cpp +++ b/util/stream/buffered_ut.cpp @@ -2,7 +2,6 @@ #include -#include #include Y_UNIT_TEST_SUITE(TestBufferedIO) { @@ -12,7 +11,7 @@ Y_UNIT_TEST_SUITE(TestBufferedIO) { for (size_t i = 0; i < 1000; ++i) { const size_t c = r.GenRand() % 10000; - TString s; + std::string s; for (size_t j = 0; j < c; ++j) { s.append('A' + (r.GenRand() % 10)); @@ -23,8 +22,8 @@ Y_UNIT_TEST_SUITE(TestBufferedIO) { } Y_UNIT_TEST(TestEqual) { - TString s1; - TString s2; + std::string s1; + std::string s2; Run(TBuffered(8000, s1)); Run(TAdaptivelyBuffered(s2)); @@ -33,7 +32,7 @@ Y_UNIT_TEST_SUITE(TestBufferedIO) { } Y_UNIT_TEST(Test1) { - TString s; + std::string s; TBuffered(100, s).Write("1", 1); @@ -41,7 +40,7 @@ Y_UNIT_TEST_SUITE(TestBufferedIO) { } Y_UNIT_TEST(Test2) { - TString s; + std::string s; TBuffered(1, s).Write("12", 2); @@ -49,7 +48,7 @@ Y_UNIT_TEST_SUITE(TestBufferedIO) { } Y_UNIT_TEST(Test3) { - TString s; + std::string s; auto&& b = TBuffered(1, s); @@ -61,7 +60,7 @@ Y_UNIT_TEST_SUITE(TestBufferedIO) { } Y_UNIT_TEST(Test4) { - TString s; + std::string s; auto&& b = TBuffered(1, s); @@ -74,7 +73,7 @@ Y_UNIT_TEST_SUITE(TestBufferedIO) { } template - inline void DoGenAndWrite(TOut&& output, TString& str) { + inline void DoGenAndWrite(TOut&& output, std::string& str) { TMersenne r; for (size_t i = 0; i < 43210; ++i) { str.append('A' + (r.GenRand() % 10)); @@ -94,21 +93,21 @@ Y_UNIT_TEST_SUITE(TestBufferedIO) { } Y_UNIT_TEST(TestWriteViaNextAndUndo) { - TString str1, str2; + std::string str1, str2; DoGenAndWrite(TBuffered(5000, str1), str2); UNIT_ASSERT_STRINGS_EQUAL(str1, str2); } Y_UNIT_TEST(TestWriteViaNextAndUndoAdaptive) { - TString str1, str2; + std::string str1, str2; DoGenAndWrite(TAdaptivelyBuffered(str1), str2); UNIT_ASSERT_STRINGS_EQUAL(str1, str2); } Y_UNIT_TEST(TestInput) { - TString s("0123456789abcdefghijklmn"); + std::string s("0123456789abcdefghijklmn"); TBuffered in(5, s); char c; UNIT_ASSERT_VALUES_EQUAL(in.Read(&c, 1), 1); //1 @@ -129,9 +128,9 @@ Y_UNIT_TEST_SUITE(TestBufferedIO) { } Y_UNIT_TEST(TestReadTo) { - TString s("0123456789abc"); + std::string s("0123456789abc"); TBuffered in(2, s); - TString t; + std::string t; UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '8'), 1); diff --git a/util/stream/direct_io.cpp b/util/stream/direct_io.cpp index 649033af341..ee1f9fcb686 100644 --- a/util/stream/direct_io.cpp +++ b/util/stream/direct_io.cpp @@ -33,7 +33,7 @@ void TRandomAccessFileOutput::DoFlush() { File->FlushData(); } -TBufferedFileOutputEx::TBufferedFileOutputEx(const TString& path, EOpenMode oMode, size_t buflen) +TBufferedFileOutputEx::TBufferedFileOutputEx(const std::string& path, EOpenMode oMode, size_t buflen) : TRandomAccessFileOutput(*(new TDirectIOBufferedFile(path, oMode, buflen))) , FileHolder(File) { diff --git a/util/stream/direct_io.h b/util/stream/direct_io.h index 2e1f2e07dd7..83a8c465d4d 100644 --- a/util/stream/direct_io.h +++ b/util/stream/direct_io.h @@ -34,7 +34,7 @@ class TRandomAccessFileOutput: public IOutputStream { class TBufferedFileOutputEx: public TRandomAccessFileOutput { public: - TBufferedFileOutputEx(const TString& path, EOpenMode oMode, size_t buflen = 1 << 17); + TBufferedFileOutputEx(const std::string& path, EOpenMode oMode, size_t buflen = 1 << 17); private: void DoFlush() override; diff --git a/util/stream/direct_io_ut.cpp b/util/stream/direct_io_ut.cpp index 1fada06ecb2..edaedcd87d4 100644 --- a/util/stream/direct_io_ut.cpp +++ b/util/stream/direct_io_ut.cpp @@ -1,6 +1,5 @@ #include -#include #include #include "buffered.h" @@ -28,7 +27,7 @@ Y_UNIT_TEST_SUITE(TDirectIOTests) { // filling file // TEMPLATE|TEMPLATE|TEMPLATE|... - const auto fileName = TString("test.file"); + const auto fileName = std::string("test.file"); auto&& directIOBuffer = TDirectIOBufferedFile{fileName, RdWr | CreateAlways | mode}; { auto&& output = TRandomAccessFileOutput{directIOBuffer}; diff --git a/util/stream/file.cpp b/util/stream/file.cpp index dcf89a48ab7..31ec270198b 100644 --- a/util/stream/file.cpp +++ b/util/stream/file.cpp @@ -8,7 +8,7 @@ TUnbufferedFileInput::TUnbufferedFileInput(const char* path) { } -TUnbufferedFileInput::TUnbufferedFileInput(const TString& path) +TUnbufferedFileInput::TUnbufferedFileInput(const std::string& path) : TUnbufferedFileInput(TFile(path, OPEN_MODE)) { } @@ -54,7 +54,7 @@ TUnbufferedFileOutput::TUnbufferedFileOutput(const char* path) { } -TUnbufferedFileOutput::TUnbufferedFileOutput(const TString& path) +TUnbufferedFileOutput::TUnbufferedFileOutput(const std::string& path) : TUnbufferedFileOutput(TFile(path, OPEN_MODE)) { } @@ -101,7 +101,7 @@ TMappedFileInput::TMappedFileInput(const TFile& file) Reset(Impl_->Data(), Impl_->Size()); } -TMappedFileInput::TMappedFileInput(const TString& path) +TMappedFileInput::TMappedFileInput(const std::string& path) : TMemoryInput(nullptr, 0) , Impl_(new TImpl(TFile(path, OpenExisting | RdOnly))) { diff --git a/util/stream/file.h b/util/stream/file.h index 692a593d973..f6346c1db35 100644 --- a/util/stream/file.h +++ b/util/stream/file.h @@ -23,7 +23,7 @@ class TUnbufferedFileInput: public IInputStream { public: TUnbufferedFileInput(const char* path); - TUnbufferedFileInput(const TString& path); + TUnbufferedFileInput(const std::string& path); TUnbufferedFileInput(const std::filesystem::path& path); TUnbufferedFileInput(const TFile& file); @@ -43,7 +43,7 @@ class TUnbufferedFileInput: public IInputStream { class TMappedFileInput: public TMemoryInput { public: TMappedFileInput(const TFile& file); - TMappedFileInput(const TString& path); + TMappedFileInput(const std::string& path); ~TMappedFileInput() override; private: @@ -60,7 +60,7 @@ class TMappedFileInput: public TMemoryInput { class TUnbufferedFileOutput: public IOutputStream { public: TUnbufferedFileOutput(const char* path); - TUnbufferedFileOutput(const TString& path); + TUnbufferedFileOutput(const std::string& path); TUnbufferedFileOutput(const std::filesystem::path& path); TUnbufferedFileOutput(const TFile& file); ~TUnbufferedFileOutput() override; diff --git a/util/stream/file_ut.cpp b/util/stream/file_ut.cpp index ac0f09796ee..9acb7803a99 100644 --- a/util/stream/file_ut.cpp +++ b/util/stream/file_ut.cpp @@ -19,14 +19,14 @@ Y_UNIT_TEST_SUITE(TFileTest) { { TUnbufferedFileInput input(TmpFileName); - TString s = input.ReadAll(); + std::string s = input.ReadAll(); UNIT_ASSERT_VALUES_EQUAL(s, TmpFileContents); } { TUnbufferedFileInput input(TmpFileName); input.Skip(TmpFileSubstring - TmpFileContents); - TString s = input.ReadAll(); + std::string s = input.ReadAll(); UNIT_ASSERT_VALUES_EQUAL(s, "chivalrous plan"); } @@ -37,8 +37,8 @@ Y_UNIT_TEST_SUITE(TFileTest) { { TUnbufferedFileInput input(TmpFileName); - TString s = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(s, TString::Join(TmpFileContents, TmpFileContents)); + std::string s = input.ReadAll(); + UNIT_ASSERT_VALUES_EQUAL(s, TUtils::Join(TmpFileContents, TmpFileContents)); } } @@ -52,7 +52,7 @@ Y_UNIT_TEST_SUITE(TFileTest) { { TMappedFileInput input(TmpFileName); - TString s = input.ReadAll(); + std::string s = input.ReadAll(); UNIT_ASSERT(s.empty()); } } @@ -65,7 +65,7 @@ Y_UNIT_TEST_SUITE(TFileTest) { TFileInput fileInput(readEnd); UNIT_ASSERT_VALUES_EQUAL(write(fds[1], "hello\n", 6), 6); - TString line; + std::string line; UNIT_ASSERT(fileInput.ReadLine(line)); UNIT_ASSERT_STRINGS_EQUAL(line, "hello"); close(fds[1]); diff --git a/util/stream/format.h b/util/stream/format.h index dc46684b397..9a8ffafdcbe 100644 --- a/util/stream/format.h +++ b/util/stream/format.h @@ -4,7 +4,6 @@ #include "output.h" #include -#include #include #include #include @@ -125,18 +124,18 @@ namespace NFormatPrivate { template TStream& ToStreamImpl(TStream& stream, const TBaseNumber& value) { char buf[8 * sizeof(T) + 1]; /* Add 1 for sign. */ - TStringBuf str(buf, IntToString(value.Value, buf, sizeof(buf))); + std::string_view str(buf, IntToString(value.Value, buf, sizeof(buf))); if (str[0] == '-') { stream << '-'; - str.Skip(1); + str.remove_prefix(1); } if (value.Flags & HF_ADDX) { if (Base == 16) { - stream << TStringBuf("0x"); + stream << std::string_view("0x"); } else if (Base == 2) { - stream << TStringBuf("0b"); + stream << std::string_view("0b"); } } @@ -160,9 +159,9 @@ namespace NFormatPrivate { template struct TBaseText { - TBasicStringBuf Text; + std::basic_string_view Text; - inline TBaseText(const TBasicStringBuf text) + inline TBaseText(const std::basic_string_view text) : Text(text) { } @@ -193,7 +192,7 @@ namespace NFormatPrivate { IOutputStream& operator<<(IOutputStream& o, const TFloatPrecision& prec) { char buf[512]; size_t count = FloatToString(prec.Value, buf, sizeof(buf), prec.Mode, prec.NDigits); - o << TStringBuf(buf, count); + o << std::string_view(buf, count); return o; } @@ -355,14 +354,14 @@ static constexpr ::NFormatPrivate::TBaseNumber SBin(const T& value, const * * Example usage: * @code - * stream << HexText(TStringBuf("abcи")); // Will output "61 62 63 D0 B8" - * stream << HexText(TWtringBuf(u"abcи")); // Will output "0061 0062 0063 0438" + * stream << HexText(std::string_view("abcи")); // Will output "61 62 63 D0 B8" + * stream << HexText(std::u16string_view(u"abcи")); // Will output "0061 0062 0063 0438" * @endcode * * @param value String to output. */ template -static inline ::NFormatPrivate::TBaseText HexText(const TBasicStringBuf value) { +static inline ::NFormatPrivate::TBaseText HexText(const std::basic_string_view value) { return ::NFormatPrivate::TBaseText(value); } @@ -374,13 +373,13 @@ static inline ::NFormatPrivate::TBaseText HexText(const TBasicStringB * * Example usage: * @code - * stream << BinText(TStringBuf("aaa")); // Will output "01100001 01100001 01100001" + * stream << BinText(std::string_view("aaa")); // Will output "01100001 01100001 01100001" * @endcode * * @param value String to output. */ template -static inline ::NFormatPrivate::TBaseText BinText(const TBasicStringBuf value) { +static inline ::NFormatPrivate::TBaseText BinText(const std::basic_string_view value) { return ::NFormatPrivate::TBaseText(value); } diff --git a/util/stream/format_std_ut.cpp b/util/stream/format_std_ut.cpp index b9030003822..d2296c4909d 100644 --- a/util/stream/format_std_ut.cpp +++ b/util/stream/format_std_ut.cpp @@ -6,7 +6,7 @@ Y_UNIT_TEST_SUITE(StdOstreamFormattingTest) { template - TString ToStringViaOstream(T baseNumber) { + std::string ToStringViaOstream(T baseNumber) { std::stringstream ss; ss << baseNumber; return ss.str(); diff --git a/util/stream/format_ut.cpp b/util/stream/format_ut.cpp index 43245aeb48e..84e35c7b914 100644 --- a/util/stream/format_ut.cpp +++ b/util/stream/format_ut.cpp @@ -49,12 +49,12 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { Y_UNIT_TEST(TestHexText) { { TStringStream ss; - ss << HexText(TStringBuf("abcи")); + ss << HexText(std::string_view("abcи")); UNIT_ASSERT_VALUES_EQUAL("61 62 63 D0 B8", ss.Str()); } { TStringStream ss; - TUtf16String w = UTF8ToWide("abcи"); + std::u16string w = UTF8ToWide("abcи"); ss << HexText(w); UNIT_ASSERT_VALUES_EQUAL("0061 0062 0063 0438", ss.Str()); } @@ -76,9 +76,9 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { } Y_UNIT_TEST(TestBinText) { - UNIT_ASSERT_VALUES_EQUAL(ToString(BinText(TStringBuf("\1"))), "00000001"); - UNIT_ASSERT_VALUES_EQUAL(ToString(BinText(TStringBuf("\1\1"))), "00000001 00000001"); - UNIT_ASSERT_VALUES_EQUAL(ToString(BinText(TStringBuf("aaa"))), "01100001 01100001 01100001"); + UNIT_ASSERT_VALUES_EQUAL(ToString(BinText(std::string_view("\1"))), "00000001"); + UNIT_ASSERT_VALUES_EQUAL(ToString(BinText(std::string_view("\1\1"))), "00000001 00000001"); + UNIT_ASSERT_VALUES_EQUAL(ToString(BinText(std::string_view("aaa"))), "01100001 01100001 01100001"); } Y_UNIT_TEST(TestPrec) { diff --git a/util/stream/hex.cpp b/util/stream/hex.cpp index 1c05330504a..6a51ab6b4b5 100644 --- a/util/stream/hex.cpp +++ b/util/stream/hex.cpp @@ -16,7 +16,7 @@ void HexEncode(const void* in, size_t len, IOutputStream& out) { } void HexDecode(const void* in, size_t len, IOutputStream& out) { - Y_ENSURE(!(len & 1), TStringBuf("Odd buffer length passed to HexDecode")); + Y_ENSURE(!(len & 1), std::string_view("Odd buffer length passed to HexDecode")); static const size_t NUM_OF_BYTES = 32; char buffer[NUM_OF_BYTES]; diff --git a/util/stream/hex_ut.cpp b/util/stream/hex_ut.cpp index 5074a0b6168..350190d2f71 100644 --- a/util/stream/hex_ut.cpp +++ b/util/stream/hex_ut.cpp @@ -4,14 +4,14 @@ #include "str.h" Y_UNIT_TEST_SUITE(THexCodingTest) { - void TestImpl(const TString& data) { - TString encoded; + void TestImpl(const std::string& data) { + std::string encoded; TStringOutput encodedOut(encoded); HexEncode(data.data(), data.size(), encodedOut); UNIT_ASSERT_EQUAL(encoded.size(), data.size() * 2); - TString decoded; + std::string decoded; TStringOutput decodedOut(decoded); HexDecode(encoded.data(), encoded.size(), decodedOut); @@ -19,7 +19,7 @@ Y_UNIT_TEST_SUITE(THexCodingTest) { } Y_UNIT_TEST(TestEncodeDecodeToStream) { - TString data = "100ABAcaba500,$%0987123456 \n\t\x01\x02\x03."; + std::string data = "100ABAcaba500,$%0987123456 \n\t\x01\x02\x03."; TestImpl(data); } diff --git a/util/stream/input.cpp b/util/stream/input.cpp index 6e8170f2f9f..599364efbba 100644 --- a/util/stream/input.cpp +++ b/util/stream/input.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include #include @@ -17,7 +16,7 @@ IInputStream::IInputStream() noexcept = default; IInputStream::~IInputStream() = default; -size_t IInputStream::DoReadTo(TString& st, char to) { +size_t IInputStream::DoReadTo(std::string& st, char to) { char ch; if (!Read(&ch, 1)) { @@ -78,7 +77,7 @@ void IInputStream::LoadOrFail(void* buf, size_t len) { } } -size_t IInputStream::ReadLine(TString& st) { +size_t IInputStream::ReadLine(std::string& st) { const size_t ret = ReadTo(st, '\n'); if (ret && !st.empty() && st.back() == '\r') { @@ -88,8 +87,8 @@ size_t IInputStream::ReadLine(TString& st) { return ret; } -size_t IInputStream::ReadLine(TUtf16String& w) { - TString s; +size_t IInputStream::ReadLine(std::u16string& w) { + std::string s; size_t result = ReadLine(s); if (result) { @@ -99,8 +98,8 @@ size_t IInputStream::ReadLine(TUtf16String& w) { return result; } -TString IInputStream::ReadLine() { - TString ret; +std::string IInputStream::ReadLine() { + std::string ret; if (!ReadLine(ret)) { ythrow yexception() << "can not read line from stream"; @@ -109,8 +108,8 @@ TString IInputStream::ReadLine() { return ret; } -TString IInputStream::ReadTo(char ch) { - TString ret; +std::string IInputStream::ReadTo(char ch) { + std::string ret; if (!ReadTo(ret, ch)) { ythrow yexception() << "can not read from stream"; @@ -145,8 +144,8 @@ size_t IInputStream::DoSkip(size_t sz) { return total; } -TString IInputStream::ReadAll() { - TString result; +std::string IInputStream::ReadAll() { + std::string result; TStringOutput stream(result); DoReadAll(stream); @@ -190,7 +189,7 @@ namespace { free(B_); } - size_t DoReadTo(TString& st, char ch) override { + size_t DoReadTo(std::string& st, char ch) override { auto&& guard = Guard(M_); (void)guard; @@ -207,9 +206,9 @@ namespace { return 0; } - st.AssignNoAlias(B_, r); + st.assign(B_, r); - if (st && st.back() == ch) { + if (!st.empty() && st.back() == ch) { st.pop_back(); } @@ -227,7 +226,7 @@ namespace { using TGetLine = TGetLineBase; #else struct TGetLine: public TGetLineBase { - size_t DoReadTo(TString& st, char ch) override { + size_t DoReadTo(std::string& st, char ch) override { if (ch == '\n') { size_t len = 0; auto r = fgetln(F_, &len); @@ -262,7 +261,7 @@ static inline bool IsStdDelimiter(char c) { return (c == '\0') || (c == ' ') || (c == '\r') || (c == '\n') || (c == '\t'); } -static void ReadUpToDelimiter(IInputStream& i, TString& s) { +static void ReadUpToDelimiter(IInputStream& i, std::string& s) { char c; while (i.ReadChar(c)) { // skip delimiters if (!IsStdDelimiter(c)) { @@ -278,14 +277,14 @@ static void ReadUpToDelimiter(IInputStream& i, TString& s) { // specialization for string-related stuff template <> -void In(IInputStream& i, TString& s) { +void In(IInputStream& i, std::string& s) { s.resize(0); ReadUpToDelimiter(i, s); } template <> -void In(IInputStream& i, TUtf16String& w) { - TString s; +void In(IInputStream& i, std::u16string& w) { + std::string s; ReadUpToDelimiter(i, s); if (s.empty()) { diff --git a/util/stream/input.h b/util/stream/input.h index f0d5807ed27..7e21f56aa36 100644 --- a/util/stream/input.h +++ b/util/stream/input.h @@ -66,7 +66,7 @@ class IInputStream: public TNonCopyable { * @returns Total number of characters read from the stream. * A return value of zero signals end of stream. */ - inline size_t ReadTo(TString& st, char ch) { + inline size_t ReadTo(std::string& st, char ch) { return DoReadTo(st, ch); } @@ -98,7 +98,7 @@ class IInputStream: public TNonCopyable { * * @returns Contents of this stream as a string. */ - TString ReadAll(); + std::string ReadAll(); /** * Reads all data from this stream and writes it into a provided output @@ -119,7 +119,7 @@ class IInputStream: public TNonCopyable { * because end of stream has already been * reached. */ - TString ReadLine(); + std::string ReadLine(); /** * Reads all characters from the stream until the given character is @@ -132,7 +132,7 @@ class IInputStream: public TNonCopyable { * because end of stream has already been * reached. */ - TString ReadTo(char ch); + std::string ReadTo(char ch); /** * Reads all data from the stream until the first occurrence of '\n' and @@ -143,7 +143,7 @@ class IInputStream: public TNonCopyable { * @returns Total number of characters read from the stream. * A return value of zero signals end of stream. */ - size_t ReadLine(TString& st); + size_t ReadLine(std::string& st); /** * Reads UTF8 encoded characters from the stream the first occurrence of '\n', @@ -155,7 +155,7 @@ class IInputStream: public TNonCopyable { * @returns Total number of characters read from the stream. * A return value of zero signals end of stream. */ - size_t ReadLine(TUtf16String& w); + size_t ReadLine(std::u16string& w); /** * Skips some data from the stream without reading / copying it. Note that @@ -204,7 +204,7 @@ class IInputStream: public TNonCopyable { * A return value of zero signals end of stream. * @throws yexception If IO error occurs. */ - virtual size_t DoReadTo(TString& st, char ch); + virtual size_t DoReadTo(std::string& st, char ch); /** * Reads all data from this stream and writes it into a provided output diff --git a/util/stream/input_ut.cpp b/util/stream/input_ut.cpp index 4a93f5458e6..1e262b5111a 100644 --- a/util/stream/input_ut.cpp +++ b/util/stream/input_ut.cpp @@ -21,7 +21,7 @@ class TMockStdIn { } template - void ForInput(const TStringBuf text, const FuncType& func) { + void ForInput(const std::string_view text, const FuncType& func) { TFile tempFile(TFile::Temporary("input_ut")); tempFile.Write(text.data(), text.size()); tempFile.FlushData(); @@ -70,7 +70,7 @@ class TNoOutput: public IOutputStream { class TSimpleStringInput: public IInputStream { public: - TSimpleStringInput(const TString& string) + TSimpleStringInput(const std::string& string) : String_(string) { } @@ -89,7 +89,7 @@ class TSimpleStringInput: public IInputStream { } private: - TString String_; + std::string String_; }; Y_UNIT_TEST_SUITE(TInputTest) { @@ -108,7 +108,7 @@ Y_UNIT_TEST_SUITE(TInputTest) { TSimpleStringInput in("0123456789abc"); - TString t; + std::string t; UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 5); @@ -120,7 +120,7 @@ Y_UNIT_TEST_SUITE(TInputTest) { Y_UNIT_TEST(TestReadLine) { TSimpleStringInput in("1\n22\n333"); - TString t; + std::string t; UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 2); UNIT_ASSERT_VALUES_EQUAL(t, "1"); UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 3); @@ -132,7 +132,7 @@ Y_UNIT_TEST_SUITE(TInputTest) { } Y_UNIT_TEST(TestStdInReadTo) { - std::pair, TStringBuf> testPairs[] = { + std::pair, std::string_view> testPairs[] = { {{"", '\n'}, ""}, {{"\n", '\n'}, ""}, {{"\n\t", '\t'}, "\n"}, @@ -142,13 +142,13 @@ Y_UNIT_TEST_SUITE(TInputTest) { TMockStdIn stdIn; for (const auto& testPair : testPairs) { - const TStringBuf text = testPair.first.first; + const std::string_view text = testPair.first.first; const char delim = testPair.first.second; - const TStringBuf expectedValue = testPair.second; + const std::string_view expectedValue = testPair.second; stdIn.ForInput(text, [=] { - TString value; + std::string value; Cin.ReadTo(value, delim); UNIT_ASSERT_VALUES_EQUAL(value, expectedValue); }); diff --git a/util/stream/ios_ut.cpp b/util/stream/ios_ut.cpp index cdd9972d747..640dc807fc9 100644 --- a/util/stream/ios_ut.cpp +++ b/util/stream/ios_ut.cpp @@ -52,14 +52,14 @@ class TStreamsTest: public TTestBase { UNIT_TEST_SUITE_REGISTRATION(TStreamsTest); void TStreamsTest::TestIStreamOperators() { - TString data("first line\r\nsecond\t\xd1\x82\xd0\xb5\xd1\x81\xd1\x82 line\r\n 1 -4 59 4320000000009999999 c\n -1.5 1e-110"); + std::string data("first line\r\nsecond\t\xd1\x82\xd0\xb5\xd1\x81\xd1\x82 line\r\n 1 -4 59 4320000000009999999 c\n -1.5 1e-110"); TStringInput si(data); - TString l1; - TString l2; - TString l3; - TUtf16String w1; - TString l4; + std::string l1; + std::string l2; + std::string l3; + std::u16string w1; + std::string l4; ui16 i1; i16 i2; i32 i3; @@ -119,7 +119,7 @@ void TStreamsTest::TestStringStream() { } void TStreamsTest::TestGenericRead() { - TString s("1234567890"); + std::string s("1234567890"); TStringInput si(s); char buf[1024]; @@ -130,7 +130,7 @@ void TStreamsTest::TestGenericRead() { } void TStreamsTest::TestGenericWrite() { - TString s; + std::string s; TStringOutput so(s); so.Write("123456", 6); @@ -140,7 +140,7 @@ void TStreamsTest::TestGenericWrite() { } void TStreamsTest::TestReadLine() { - TString data("1234\r\n5678\nqw"); + std::string data("1234\r\n5678\nqw"); TStringInput si(data); UNIT_ASSERT_EQUAL(si.ReadLine(), "1234"); @@ -170,7 +170,7 @@ void TStreamsTest::TestMemoryStream() { class TMyStringOutput: public IOutputStream { public: - inline TMyStringOutput(TString& s, size_t buflen) noexcept + inline TMyStringOutput(std::string& s, size_t buflen) noexcept : S_(s) , BufLen_(buflen) { @@ -184,7 +184,7 @@ class TMyStringOutput: public IOutputStream { } void DoWriteV(const TPart* p, size_t count) override { - TString s; + std::string s; for (size_t i = 0; i < count; ++i) { s.append((const char*)p[i].buf, p[i].len); @@ -199,14 +199,14 @@ class TMyStringOutput: public IOutputStream { }; void TStreamsTest::TestBufferedIO() { - TString s; + std::string s; { const size_t buflen = 7; TBuffered bo(buflen, s, buflen); for (size_t i = 0; i < 1000; ++i) { - TString str(" "); + std::string str(" "); str += ToString(i % 10); bo.Write(str.data(), str.size()); @@ -222,7 +222,7 @@ void TStreamsTest::TestBufferedIO() { TBuffered bi(buflen, s); for (size_t i = 0; i < 1000; ++i) { - TString str(" "); + std::string str(" "); str += ToString(i % 10); char buf[3]; @@ -240,7 +240,7 @@ void TStreamsTest::TestBufferedIO() { { const size_t buflen = 13; TBuffered bo(buflen, s, buflen); - TString f = "1234567890"; + std::string f = "1234567890"; for (size_t i = 0; i < 10; ++i) { f += f; @@ -256,7 +256,7 @@ void TStreamsTest::TestBufferedIO() { void TStreamsTest::TestBufferStream() { TBufferStream stream; - TString s = "test"; + std::string s = "test"; stream.Write(s.data(), s.size()); char buf[5]; @@ -280,7 +280,7 @@ void TStreamsTest::TestBufferStream() { namespace { class TStringListInput: public IWalkInput { public: - TStringListInput(const std::vector& data) + TStringListInput(const std::vector& data) : Data_(data) , Index_(0) { @@ -292,14 +292,14 @@ namespace { return 0; } - const TString& string = Data_[Index_++]; + const std::string& string = Data_[Index_++]; *ptr = string.data(); return string.size(); } private: - const std::vector& Data_; + const std::vector& Data_; size_t Index_; }; @@ -331,7 +331,7 @@ namespace { "123", "\t\r "}; void TestStreamReadTo1(IInputStream& input, const char* comment) { - TString tmp; + std::string tmp; input.ReadTo(tmp, 'c'); UNIT_ASSERT_VALUES_EQUAL_C(tmp, "111a222b333", comment); @@ -347,7 +347,7 @@ namespace { } void TestStreamReadTo2(IInputStream& input, const char* comment) { - TString s; + std::string s; size_t i = 0; while (input.ReadLine(s)) { UNIT_ASSERT_C(i < Y_ARRAY_SIZE(Expected), comment); @@ -367,7 +367,7 @@ namespace { } void TestStrokaInput(IInputStream& input, const char* comment) { - TString line; + std::string line; ui32 i = 0; TInstant start = Now(); while (input.ReadLine(line)) { @@ -378,7 +378,7 @@ namespace { } template - void TestStreamReadTo(const TString& text, T test) { + void TestStreamReadTo(const std::string& text, T test) { TStringInput is(text); test(is, "TStringInput"); TMemoryInput mi(text.data(), text.size()); @@ -389,7 +389,7 @@ namespace { TStringInput slave(text); TBufferedInput bdi(&slave); test(bdi, "TBufferedInput"); - std::vector lst(1, text); + std::vector lst(1, text); TStringListInput sli(lst); test(sli, "IWalkInput"); } @@ -399,13 +399,13 @@ void TStreamsTest::TestReadTo() { TestStreamReadTo("111a222b333c444d555e666f", TestStreamReadTo1); TestStreamReadTo(Text, TestStreamReadTo2); TestStreamReadTo("111a222b333c444d555e666f", TestStreamReadTo3); - TString withZero = "one"; + std::string withZero = "one"; withZero.append('\0').append("two").append('\0').append("three"); TestStreamReadTo(withZero, TestStreamReadTo4); } void TStreamsTest::TestStrokaInput() { - TString s; + std::string s; for (ui32 i = 0; i < 100000; ++i) { std::vector d(i % 1000, 'a'); s.append(d.data(), d.size()); @@ -415,9 +415,9 @@ void TStreamsTest::TestStrokaInput() { } void TStreamsTest::TestWtrokaInput() { - const TString s(Text); + const std::string s(Text); TStringInput is(s); - TUtf16String w; + std::u16string w; size_t i = 0; while (is.ReadLine(w)) { @@ -429,12 +429,12 @@ void TStreamsTest::TestWtrokaInput() { } void TStreamsTest::TestWtrokaOutput() { - TString s; + std::string s; TStringOutput os(s); const size_t n = sizeof(Expected) / sizeof(Expected[0]); for (size_t i = 0; i < n; ++i) { - TUtf16String w = UTF8ToWide(Expected[i]); + std::u16string w = UTF8ToWide(Expected[i]); os << w; @@ -451,7 +451,7 @@ void TStreamsTest::TestWtrokaOutput() { } void TStreamsTest::TestWchar16Output() { - TString s; + std::string s; TStringOutput os(s); os << wchar16(97); // latin a os << u'\u044E'; // cyrillic ю @@ -465,7 +465,7 @@ void TStreamsTest::TestWchar16Output() { } void TStreamsTest::TestWchar32Output() { - TString s; + std::string s; TStringOutput os(s); os << wchar32(97); // latin a os << U'\u044E'; // cyrillic ю @@ -479,8 +479,8 @@ void TStreamsTest::TestWchar32Output() { } void TStreamsTest::TestUtf16StingOutputByChars() { - TString s = "\xd1\x87\xd0\xb8\xd1\x81\xd1\x82\xd0\xb8\xd1\x87\xd0\xb8\xd1\x81\xd1\x82\xd0\xb8"; - TUtf16String w = UTF8ToWide(s); + std::string s = "\xd1\x87\xd0\xb8\xd1\x81\xd1\x82\xd0\xb8\xd1\x87\xd0\xb8\xd1\x81\xd1\x82\xd0\xb8"; + std::u16string w = UTF8ToWide(s); UNIT_ASSERT_VALUES_EQUAL(w.size(), 10); diff --git a/util/stream/length.cpp b/util/stream/length.cpp index 9907fe2ac94..9283f3321c9 100644 --- a/util/stream/length.cpp +++ b/util/stream/length.cpp @@ -28,7 +28,7 @@ size_t TCountingInput::DoSkip(size_t len) { return ret; } -size_t TCountingInput::DoReadTo(TString& st, char ch) { +size_t TCountingInput::DoReadTo(std::string& st, char ch) { const size_t ret = Slave_->ReadTo(st, ch); Count_ += ret; return ret; diff --git a/util/stream/length.h b/util/stream/length.h index 4d508ae24d2..3a962158700 100644 --- a/util/stream/length.h +++ b/util/stream/length.h @@ -59,7 +59,7 @@ class TCountingInput: public IInputStream { private: size_t DoRead(void* buf, size_t len) override; size_t DoSkip(size_t len) override; - size_t DoReadTo(TString& st, char ch) override; + size_t DoReadTo(std::string& st, char ch) override; ui64 DoReadAll(IOutputStream& out) override; private: diff --git a/util/stream/length_ut.cpp b/util/stream/length_ut.cpp index 89684489544..4a665cbae62 100644 --- a/util/stream/length_ut.cpp +++ b/util/stream/length_ut.cpp @@ -2,7 +2,6 @@ #include -#include Y_UNIT_TEST_SUITE(TestLengthIO) { Y_UNIT_TEST(TestLengthLimitedInput) { @@ -20,7 +19,7 @@ Y_UNIT_TEST_SUITE(TestLengthIO) { TStringStream s1("abc\ndef\n"); TCountingInput l1(&s1); - TString s; + std::string s; l1.ReadLine(s); UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 4); @@ -41,10 +40,10 @@ Y_UNIT_TEST_SUITE(TestLengthIO) { l1.Write('1'); UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 1); - l1.Write(TString("abcd")); + l1.Write(std::string("abcd")); UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 5); - TString buf("aaa"); + std::string buf("aaa"); IOutputStream::TPart parts[] = {{buf.data(), buf.size()}, {buf.data(), buf.size()}, {buf.data(), buf.size()}}; l1.Write(parts, 3); UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 14); diff --git a/util/stream/mem.cpp b/util/stream/mem.cpp index 22a3339e274..d342226cfe2 100644 --- a/util/stream/mem.cpp +++ b/util/stream/mem.cpp @@ -14,7 +14,7 @@ TMemoryInput::TMemoryInput(const void* buf, size_t len) noexcept { } -TMemoryInput::TMemoryInput(const TStringBuf buf) noexcept +TMemoryInput::TMemoryInput(const std::string_view buf) noexcept : Buf_(buf.data()) , Len_(buf.size()) { @@ -39,7 +39,7 @@ void TMemoryInput::DoUndo(size_t len) { TMemoryOutput::~TMemoryOutput() = default; size_t TMemoryOutput::DoNext(void** ptr) { - Y_ENSURE(Buf_ < End_, TStringBuf("memory output stream exhausted")); + Y_ENSURE(Buf_ < End_, std::string_view("memory output stream exhausted")); *ptr = Buf_; size_t bufferSize = End_ - Buf_; Buf_ = End_; @@ -53,13 +53,13 @@ void TMemoryOutput::DoUndo(size_t len) { void TMemoryOutput::DoWrite(const void* buf, size_t len) { char* end = Buf_ + len; - Y_ENSURE(end <= End_, TStringBuf("memory output stream exhausted")); + Y_ENSURE(end <= End_, std::string_view("memory output stream exhausted")); memcpy(Buf_, buf, len); Buf_ = end; } void TMemoryOutput::DoWriteC(char c) { - Y_ENSURE(Buf_ < End_, TStringBuf("memory output stream exhausted")); + Y_ENSURE(Buf_ < End_, std::string_view("memory output stream exhausted")); *Buf_++ = c; } diff --git a/util/stream/mem.h b/util/stream/mem.h index cad6dfb9754..6a8cb95c9b2 100644 --- a/util/stream/mem.h +++ b/util/stream/mem.h @@ -3,7 +3,6 @@ #include "zerocopy.h" #include "zerocopy_output.h" -#include /** * @addtogroup Streams_Memory @@ -26,10 +25,10 @@ class TMemoryInput: public IZeroCopyInputFastReadTo { * @param len Size of the memory block. */ TMemoryInput(const void* buf, size_t len) noexcept; - TMemoryInput(TString&&) = delete; - explicit TMemoryInput(const TStringBuf buf) noexcept; + TMemoryInput(std::string&&) = delete; + explicit TMemoryInput(const std::string_view buf) noexcept; explicit TMemoryInput(const char* zstr) - : TMemoryInput(TStringBuf(zstr)) + : TMemoryInput(std::string_view(zstr)) { } @@ -221,8 +220,8 @@ class TMemoryWriteBuffer: public TMemoryOutput { * @returns Data that has been written into this * stream as a string. */ - TStringBuf Str() const { - return TStringBuf(Beg(), Buf()); + std::string_view Str() const { + return std::string_view(Beg(), Buf()); } char* Beg() const { diff --git a/util/stream/mem_ut.cpp b/util/stream/mem_ut.cpp index f388ae66acf..199fc8c8d04 100644 --- a/util/stream/mem_ut.cpp +++ b/util/stream/mem_ut.cpp @@ -4,10 +4,10 @@ Y_UNIT_TEST_SUITE(TestMemIO) { Y_UNIT_TEST(TestReadTo) { - TString s("0123456789abc"); + std::string s("0123456789abc"); TMemoryInput in(s); - TString t; + std::string t; UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 5); diff --git a/util/stream/multi.cpp b/util/stream/multi.cpp index b2354298a0c..da0eee2aac1 100644 --- a/util/stream/multi.cpp +++ b/util/stream/multi.cpp @@ -22,7 +22,7 @@ size_t TMultiInput::DoRead(void* buf, size_t len) { return C_->Read(buf, len); } -size_t TMultiInput::DoReadTo(TString& st, char ch) { +size_t TMultiInput::DoReadTo(std::string& st, char ch) { size_t ret = C_->ReadTo(st, ch); if (ret == st.size() + 1) { // found a symbol, not eof return ret; @@ -34,7 +34,7 @@ size_t TMultiInput::DoReadTo(TString& st, char ch) { if (ret == 0) { ret += C_->ReadTo(st, ch); } else { - TString tmp; + std::string tmp; ret += C_->ReadTo(tmp, ch); st += tmp; } diff --git a/util/stream/multi.h b/util/stream/multi.h index 8bfd462d99e..322876b8578 100644 --- a/util/stream/multi.h +++ b/util/stream/multi.h @@ -18,7 +18,7 @@ class TMultiInput: public IInputStream { private: size_t DoRead(void* buf, size_t len) override; size_t DoSkip(size_t len) override; - size_t DoReadTo(TString& st, char ch) override; + size_t DoReadTo(std::string& st, char ch) override; private: IInputStream* C_; diff --git a/util/stream/multi_ut.cpp b/util/stream/multi_ut.cpp index 61869cdf1cb..361ff65b059 100644 --- a/util/stream/multi_ut.cpp +++ b/util/stream/multi_ut.cpp @@ -7,23 +7,23 @@ Y_UNIT_TEST_SUITE(TestMultiInput) { TMemoryInput Input1; TMemoryInput Input2; TMultiInput MultiInput; - TTestCase(const TStringBuf in1, const TStringBuf in2) + TTestCase(const std::string_view in1, const std::string_view in2) : Input1(in1) , Input2(in2) , MultiInput(&Input1, &Input2) { } void TestReadToResult(char c, size_t expectedRetval, - const TString& expectedValue, - const TString& initValue = "") { - TString t = initValue; + const std::string& expectedValue, + const std::string& initValue = "") { + std::string t = initValue; UNIT_ASSERT_VALUES_EQUAL(MultiInput.ReadTo(t, c), expectedRetval); UNIT_ASSERT_VALUES_EQUAL(t, expectedValue); } }; Y_UNIT_TEST(TestReadTo) { - TString t; + std::string t; TTestCase simpleCase("0123456789abc", "defghijk"); simpleCase.TestReadToResult('7', 8, "0123456"); diff --git a/util/stream/output.cpp b/util/stream/output.cpp index 719c877764a..d2654721367 100644 --- a/util/stream/output.cpp +++ b/util/stream/output.cpp @@ -11,8 +11,8 @@ #if defined(_android_) #include #include - #include #include + #include #endif #include @@ -90,14 +90,9 @@ static void WriteString(IOutputStream& o, const wchar32* w, size_t n) { o.Write(data, written); } -template <> -void Out(IOutputStream& o, const TString& p) { - o.Write(p.data(), p.size()); -} - template <> void Out(IOutputStream& o, const std::string& p) { - o.Write(p.data(), p.length()); + o.Write(p.data(), p.size()); } template <> @@ -120,21 +115,6 @@ void Out(IOutputStream& o, const std::filesystem::path& p o.Write(p.string()); } -template <> -void Out(IOutputStream& o, const TStringBuf& p) { - o.Write(p.data(), p.length()); -} - -template <> -void Out(IOutputStream& o, const TWtringBuf& p) { - WriteString(o, p.data(), p.length()); -} - -template <> -void Out(IOutputStream& o, const TUtf32StringBuf& p) { - WriteString(o, p.data(), p.length()); -} - template <> void Out(IOutputStream& o, const wchar16* w) { if (w) { @@ -154,12 +134,12 @@ void Out(IOutputStream& o, const wchar32* w) { } template <> -void Out(IOutputStream& o, const TUtf16String& w) { +void Out(IOutputStream& o, const std::u16string& w) { WriteString(o, w.c_str(), w.size()); } template <> -void Out(IOutputStream& o, const TUtf32String& w) { +void Out(IOutputStream& o, const std::u32string& w) { WriteString(o, w.c_str(), w.size()); } @@ -215,23 +195,6 @@ void Out::reference>(IOutputStream& o, const std::vec } #endif -#ifndef TSTRING_IS_STD_STRING -template <> -void Out>(IOutputStream& o, const TBasicCharRef& c) { - o << static_cast(c); -} - -template <> -void Out>(IOutputStream& o, const TBasicCharRef& c) { - o << static_cast(c); -} - -template <> -void Out>(IOutputStream& o, const TBasicCharRef& c) { - o << static_cast(c); -} -#endif - template <> void Out(IOutputStream& o, const void* t) { o << Hex(size_t(t)); @@ -246,7 +209,7 @@ using TNullPtr = decltype(nullptr); template <> void Out(IOutputStream& o, TTypeTraits::TFuncParam) { - o << TStringBuf("nullptr"); + o << std::string_view("nullptr"); } #define DEF_OPTIONAL(TYPE) \ @@ -263,8 +226,7 @@ DEF_OPTIONAL(ui32); DEF_OPTIONAL(i64); DEF_OPTIONAL(ui64); DEF_OPTIONAL(std::string); -DEF_OPTIONAL(TString); -DEF_OPTIONAL(TStringBuf); +DEF_OPTIONAL(std::string_view); #if defined(_android_) namespace { @@ -294,22 +256,20 @@ namespace { private: virtual void DoWrite(const void* buf, size_t len) override { - with_lock (BufferMutex) { - Buffer.Write(buf, len); - } + std::lock_guard guard(BufferMutex); + Buffer.Write(buf, len); } virtual void DoFlush() override { - with_lock (BufferMutex) { - LogFuncPtr(ANDROID_LOG_DEBUG, GetTag(), Buffer.Data()); - Buffer.Clear(); - } + std::lock_guard guard(BufferMutex); + LogFuncPtr(ANDROID_LOG_DEBUG, GetTag(), Buffer.Data()); + Buffer.Clear(); } virtual const char* GetTag() const = 0; private: - TMutex BufferMutex; + std::mutex BufferMutex; TStringStream Buffer; TLogFuncPtr LogFuncPtr; }; diff --git a/util/stream/output.h b/util/stream/output.h index 08e67c3c6f0..9163360e233 100644 --- a/util/stream/output.h +++ b/util/stream/output.h @@ -4,10 +4,9 @@ #include "labeled.h" #include -#include -#include #include +#include #include /** @@ -30,7 +29,7 @@ class IOutputStream: public TNonCopyable { { } - inline TPart(const TStringBuf s) noexcept + inline TPart(const std::string_view s) noexcept : buf(s.data()) , len(s.size()) { @@ -79,7 +78,7 @@ class IOutputStream: public TNonCopyable { * * @param st String to write. */ - inline void Write(const TStringBuf st) { + inline void Write(const std::string_view st) { Write(st.data(), st.size()); } diff --git a/util/stream/output.pxd b/util/stream/output.pxd index 2fccc26d9b8..029ac355f7e 100644 --- a/util/stream/output.pxd +++ b/util/stream/output.pxd @@ -1,4 +1,4 @@ -from util.generic.string cimport TStringBuf +from util.generic.string cimport std::string_view cdef extern from "" nogil: @@ -8,5 +8,5 @@ cdef extern from "" nogil: void Finish() except+ void WriteChar "Write"(char) except+ - void WriteBuf "Write"(const TStringBuf) except+ + void WriteBuf "Write"(const std::string_view) except+ void Write(const void*, size_t) except+ diff --git a/util/stream/pipe.cpp b/util/stream/pipe.cpp index 51be1934a75..92cfdee99a9 100644 --- a/util/stream/pipe.cpp +++ b/util/stream/pipe.cpp @@ -1,13 +1,14 @@ #include "pipe.h" #include +#include #include #include class TPipeBase::TImpl { public: - inline TImpl(const TString& command, const char* mode) + inline TImpl(const std::string& command, const char* mode) : Pipe_(nullptr) { #ifndef _freebsd_ @@ -17,7 +18,7 @@ class TPipeBase::TImpl { #endif Pipe_ = ::popen(command.data(), mode); if (Pipe_ == nullptr) { - ythrow TSystemError() << "failed to open pipe: " << command.Quote(); + ythrow TSystemError() << "failed to open pipe: " << NUtils::Quote(command); } } @@ -31,14 +32,14 @@ class TPipeBase::TImpl { FILE* Pipe_; }; -TPipeBase::TPipeBase(const TString& command, const char* mode) +TPipeBase::TPipeBase(const std::string& command, const char* mode) : Impl_(new TImpl(command, mode)) { } TPipeBase::~TPipeBase() = default; -TPipeInput::TPipeInput(const TString& command) +TPipeInput::TPipeInput(const std::string& command) : TPipeBase(command, "r") { } @@ -61,7 +62,7 @@ size_t TPipeInput::DoRead(void* buf, size_t len) { return bytesRead; } -TPipeOutput::TPipeOutput(const TString& command) +TPipeOutput::TPipeOutput(const std::string& command) : TPipeBase(command, "w") { } diff --git a/util/stream/pipe.h b/util/stream/pipe.h index 18525b9517d..1434045df24 100644 --- a/util/stream/pipe.h +++ b/util/stream/pipe.h @@ -5,7 +5,6 @@ #include #include -#include /** * @addtogroup Streams_Pipes @@ -24,7 +23,7 @@ class TPipeBase { * @param mode Data transfer mode for the pipe. Use * "r" for reading and "w" for writing. */ - TPipeBase(const TString& command, const char* mode); + TPipeBase(const std::string& command, const char* mode); virtual ~TPipeBase(); protected: @@ -45,7 +44,7 @@ class TPipeInput: protected TPipeBase, public IInputStream { * * @param command Command line to start a process with. */ - TPipeInput(const TString& command); + TPipeInput(const std::string& command); private: size_t DoRead(void* buf, size_t len) override; @@ -64,7 +63,7 @@ class TPipeOutput: protected TPipeBase, public IOutputStream { * * @param command Command line to start a process with. */ - TPipeOutput(const TString& command); + TPipeOutput(const std::string& command); /** * Waits for the process to terminate and throws an exception if it ended diff --git a/util/stream/printf_ut.cpp b/util/stream/printf_ut.cpp index 0eab1670625..f968b99d66c 100644 --- a/util/stream/printf_ut.cpp +++ b/util/stream/printf_ut.cpp @@ -2,7 +2,6 @@ #include "printf.h" #include "str.h" -#include #include @@ -23,7 +22,7 @@ Y_UNIT_TEST_SUITE(TStreamPrintfTest) { } Y_UNIT_TEST(TestLargePrintf) { - TString s = NUnitTest::RandomString(1000000); + std::string s = NUnitTest::RandomString(1000000); TStringStream ss; Printf(ss, "%s", s.data()); diff --git a/util/stream/str.cpp b/util/stream/str.cpp index a0c3b1c75c7..242116353ac 100644 --- a/util/stream/str.cpp +++ b/util/stream/str.cpp @@ -1,5 +1,7 @@ #include "str.h" +#include + static constexpr size_t MIN_BUFFER_GROW_SIZE = 16; TStringInput::~TStringInput() = default; @@ -23,7 +25,7 @@ size_t TStringOutput::DoNext(void** ptr) { S_->reserve(FastClp2(S_->capacity() + MIN_BUFFER_GROW_SIZE)); } size_t previousSize = S_->size(); - ResizeUninitialized(*S_, S_->capacity()); + S_->resize(S_->capacity()); *ptr = S_->begin() + previousSize; return S_->size() - previousSize; } diff --git a/util/stream/str.h b/util/stream/str.h index 028bd572c03..7e6fa1537fd 100644 --- a/util/stream/str.h +++ b/util/stream/str.h @@ -3,10 +3,11 @@ #include "zerocopy.h" #include "zerocopy_output.h" -#include #include #include +#include + /** * @addtogroup Streams_Strings * @{ @@ -25,17 +26,17 @@ class TStringInput: public IZeroCopyInputFastReadTo { * up to the user to make sure that the string doesn't get destroyed while * this stream is in use. * - * For reading data from `TStringBuf`s, see `TMemoryInput` (`util/stream/mem.h`). + * For reading data from `std::string_view`s, see `TMemoryInput` (`util/stream/mem.h`). * * @param s String to read from. */ - inline TStringInput(const TString& s) noexcept + inline TStringInput(const std::string& s) noexcept : S_(&s) , Pos_(0) { } - TStringInput(const TString&&) = delete; + TStringInput(const std::string&&) = delete; ~TStringInput() override; @@ -52,7 +53,7 @@ class TStringInput: public IZeroCopyInputFastReadTo { void DoUndo(size_t len) override; private: - const TString* S_; + const std::string* S_; size_t Pos_; friend class TStringStream; @@ -73,7 +74,7 @@ class TStringOutput: public IZeroCopyOutput { * * @param s String to append to. */ - inline TStringOutput(TString& s) noexcept + inline TStringOutput(std::string& s) noexcept : S_(&s) { } @@ -101,14 +102,14 @@ class TStringOutput: public IZeroCopyOutput { void DoWriteC(char c) override; private: - TString* S_; + std::string* S_; }; /** * String input/output stream, similar to `std::stringstream`. */ -class TStringStream: private TEmbedPolicy, public TStringInput, public TStringOutput { - using TEmbeddedString = TEmbedPolicy; +class TStringStream: private TEmbedPolicy, public TStringInput, public TStringOutput { + using TEmbeddedString = TEmbedPolicy; public: inline TStringStream() @@ -118,7 +119,7 @@ class TStringStream: private TEmbedPolicy, public TStringInput, public { } - inline TStringStream(const TString& string) + inline TStringStream(const std::string& string) : TEmbeddedString(string) , TStringInput(*TEmbeddedString::Ptr()) , TStringOutput(*TEmbeddedString::Ptr()) @@ -152,14 +153,14 @@ class TStringStream: private TEmbedPolicy, public TStringInput, public /** * @returns String that this stream is writing into. */ - inline TString& Str() noexcept { + inline std::string& Str() noexcept { return *Ptr(); } /** * @returns String that this stream is writing into. */ - inline const TString& Str() const noexcept { + inline const std::string& Str() const noexcept { return *Ptr(); } diff --git a/util/stream/str.pxd b/util/stream/str.pxd index 76dc16a8220..312b83b0ff5 100644 --- a/util/stream/str.pxd +++ b/util/stream/str.pxd @@ -1,12 +1,12 @@ from util.generic.ptr cimport THolder -from util.generic.string cimport TString, TStringBuf +from util.generic.string cimport std::string, std::string_view from util.stream.output cimport IOutputStream cdef extern from "" nogil: cdef cppclass TStringOutput(IOutputStream): TStringOutput() except+ - TStringOutput(TString&) except+ + TStringOutput(std::string&) except+ void Reserve(size_t) except+ ctypedef THolder[TStringOutput] TStringOutputPtr diff --git a/util/stream/str_ut.cpp b/util/stream/str_ut.cpp index fc6b46c31a4..a9d497f7613 100644 --- a/util/stream/str_ut.cpp +++ b/util/stream/str_ut.cpp @@ -8,39 +8,39 @@ const T ReturnConstTemp(); Y_UNIT_TEST_SUITE(TStringInputOutputTest) { Y_UNIT_TEST(Lvalue) { - TString str = "Hello, World!"; + std::string str = "Hello, World!"; TStringInput input(str); - TString result = input.ReadAll(); + std::string result = input.ReadAll(); UNIT_ASSERT_VALUES_EQUAL(result, str); } Y_UNIT_TEST(ConstRef) { - TString str = "Hello, World!"; - const TString& r = str; + std::string str = "Hello, World!"; + const std::string& r = str; TStringInput input(r); - TString result = input.ReadAll(); + std::string result = input.ReadAll(); UNIT_ASSERT_VALUES_EQUAL(result, str); } Y_UNIT_TEST(NonConstRef) { - TString str = "Hello, World!"; - TString& r = str; + std::string str = "Hello, World!"; + std::string& r = str; TStringInput input(r); - TString result = input.ReadAll(); + std::string result = input.ReadAll(); UNIT_ASSERT_VALUES_EQUAL(result, str); } Y_UNIT_TEST(Transfer) { - TString inputString = "some_string"; + std::string inputString = "some_string"; TStringInput input(inputString); - TString outputString; + std::string outputString; TStringOutput output(outputString); TransferData(&input, &output); @@ -49,9 +49,9 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { } Y_UNIT_TEST(SkipReadAll) { - TString string0 = "All animals are equal, but some animals are more equal than others."; + std::string string0 = "All animals are equal, but some animals are more equal than others."; - TString string1; + std::string string1; for (size_t i = 1; i <= string0.size(); i++) { string1 += string0.substr(0, i); } @@ -63,7 +63,7 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { left -= input0.Skip(left); } - TString string2 = input0.ReadAll(); + std::string string2 = input0.ReadAll(); UNIT_ASSERT_VALUES_EQUAL(string2, string1.substr(5)); } @@ -78,8 +78,8 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { } Y_UNIT_TEST(TestReadTo) { - TString s("0123456789abc"); - TString t; + std::string s("0123456789abc"); + std::string t; TStringInput in0(s); UNIT_ASSERT_VALUES_EQUAL(in0.ReadTo(t, '7'), 8); @@ -89,9 +89,9 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { } Y_UNIT_TEST(WriteViaNextAndUndo) { - TString str1; + std::string str1; TStringOutput output(str1); - TString str2; + std::string str2; for (size_t i = 0; i < 10000; ++i) { str2.push_back('a' + (i % 20)); @@ -114,7 +114,7 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { } Y_UNIT_TEST(Write) { - TString str; + std::string str; TStringOutput output(str); output << "1" << "22" @@ -130,7 +130,7 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { } Y_UNIT_TEST(WriteChars) { - TString str; + std::string str; TStringOutput output(str); output << '1' << '2' << '3' << '4' << '5' << '6' << '7' << '8' << '9' << '0'; @@ -138,7 +138,7 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { } Y_UNIT_TEST(MoveConstructor) { - TString str; + std::string str; TStringOutput output1(str); output1 << "foo"; diff --git a/util/stream/str_ut.pyx b/util/stream/str_ut.pyx index 2ae617303f9..8e5b902ddc3 100644 --- a/util/stream/str_ut.pyx +++ b/util/stream/str_ut.pyx @@ -3,7 +3,7 @@ from cython.operator cimport dereference from util.generic.ptr cimport THolder -from util.generic.string cimport TString, TStringBuf +from util.generic.string cimport std::string, std::string_view from util.stream.str cimport TStringOutput, TStringOutputPtr import unittest @@ -14,11 +14,11 @@ class TestStringOutput(unittest.TestCase): cdef TStringOutput output def test_ctor2(self): - cdef TString string + cdef std::string string cdef THolder[TStringOutput] string_output = THolder[TStringOutput](new TStringOutput(string)) def test_write_char(self): - cdef TString string + cdef std::string string cdef TStringOutputPtr string_output = TStringOutputPtr(new TStringOutput(string)) self.assertEqual(string, "") @@ -30,7 +30,7 @@ class TestStringOutput(unittest.TestCase): self.assertEqual(string, "123") def test_write_void(self): - cdef TString string + cdef std::string string cdef TStringOutputPtr string_output = TStringOutputPtr(new TStringOutput(string)) self.assertEqual(string, "") @@ -42,19 +42,19 @@ class TestStringOutput(unittest.TestCase): self.assertEqual(string, "1234") def test_write_buf(self): - cdef TString string + cdef std::string string cdef TStringOutputPtr string_output = TStringOutputPtr(new TStringOutput(string)) self.assertEqual(string, "") - dereference(string_output.Get()).WriteBuf(TStringBuf("1")) + dereference(string_output.Get()).WriteBuf(std::string_view("1")) self.assertEqual(string, "1") - dereference(string_output.Get()).WriteBuf(TStringBuf("2")) + dereference(string_output.Get()).WriteBuf(std::string_view("2")) self.assertEqual(string, "12") - dereference(string_output.Get()).WriteBuf(TStringBuf("34")) + dereference(string_output.Get()).WriteBuf(std::string_view("34")) self.assertEqual(string, "1234") def test_reserve(self): - cdef TString string + cdef std::string string cdef TStringOutputPtr string_output = TStringOutputPtr(new TStringOutput(string)) self.assertEqual(string, "") dereference(string_output.Get()).Reserve(50) diff --git a/util/stream/tokenizer.h b/util/stream/tokenizer.h index b2398efdd1a..411dcf4e9c7 100644 --- a/util/stream/tokenizer.h +++ b/util/stream/tokenizer.h @@ -4,7 +4,6 @@ #include #include -#include #include #include @@ -76,8 +75,8 @@ class TStreamTokenizer { return this; } - inline TStringBuf operator*() noexcept { - return TStringBuf{Data_, Len_}; + inline std::string_view operator*() noexcept { + return std::string_view{Data_, Len_}; } private: diff --git a/util/stream/tokenizer_ut.cpp b/util/stream/tokenizer_ut.cpp index afc566da86e..00e19ae2391 100644 --- a/util/stream/tokenizer_ut.cpp +++ b/util/stream/tokenizer_ut.cpp @@ -1,13 +1,12 @@ #include #include -#include #include "mem.h" #include "null.h" #include "tokenizer.h" -static inline void CheckIfNullTerminated(const TStringBuf str) { +static inline void CheckIfNullTerminated(const std::string_view str) { UNIT_ASSERT_VALUES_EQUAL('\0', *(str.data() + str.size())); } @@ -17,7 +16,7 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { auto&& tokenizer = TStreamTokenizer{&input}; auto tokensCount = size_t{}; for (auto it = tokenizer.begin(); tokenizer.end() != it; ++it) { - CheckIfNullTerminated(TStringBuf{it->Data(), it->Length()}); + CheckIfNullTerminated(std::string_view{it->Data(), it->Length()}); ++tokensCount; } UNIT_ASSERT_VALUES_EQUAL(0, tokensCount); @@ -30,7 +29,7 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { auto&& tokenizer = TStreamTokenizer{&input}; auto tokensCount = size_t{}; for (auto it = tokenizer.begin(); tokenizer.end() != it; ++it) { - CheckIfNullTerminated(TStringBuf{it->Data(), it->Length()}); + CheckIfNullTerminated(std::string_view{it->Data(), it->Length()}); UNIT_ASSERT_VALUES_EQUAL(0, it->Length()); ++tokensCount; } @@ -40,14 +39,14 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { Y_UNIT_TEST(LastTokenendDoesntSatisfyPredicateTest) { const char data[] = "abc\ndef\nxxxxxx"; const auto dataSize = Y_ARRAY_SIZE(data) - 1; - const TStringBuf tokens[] = {TStringBuf("abc"), TStringBuf("def"), TStringBuf("xxxxxx")}; + const std::string_view tokens[] = {std::string_view("abc"), std::string_view("def"), std::string_view("xxxxxx")}; const auto tokensSize = Y_ARRAY_SIZE(tokens); auto&& input = TMemoryInput{data, dataSize}; auto&& tokenizer = TStreamTokenizer{&input}; auto tokensCount = size_t{}; for (auto it = tokenizer.begin(); tokenizer.end() != it; ++it) { UNIT_ASSERT(tokensCount < tokensSize); - const auto token = TStringBuf{it->Data(), it->Length()}; + const auto token = std::string_view{it->Data(), it->Length()}; CheckIfNullTerminated(token); UNIT_ASSERT_VALUES_EQUAL(tokens[tokensCount], token); ++tokensCount; @@ -58,14 +57,14 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { Y_UNIT_TEST(FirstTokenIsEmptyTest) { const char data[] = "\ndef\nxxxxxx"; const auto dataSize = Y_ARRAY_SIZE(data) - 1; - const TStringBuf tokens[] = {TStringBuf(), TStringBuf("def"), TStringBuf("xxxxxx")}; + const std::string_view tokens[] = {std::string_view(), std::string_view("def"), std::string_view("xxxxxx")}; const auto tokensSize = Y_ARRAY_SIZE(tokens); auto&& input = TMemoryInput{data, dataSize}; auto&& tokenizer = TStreamTokenizer{&input}; auto tokensCount = size_t{}; for (auto it = tokenizer.begin(); tokenizer.end() != it; ++it) { UNIT_ASSERT(tokensCount < tokensSize); - const auto token = TStringBuf{it->Data(), it->Length()}; + const auto token = std::string_view{it->Data(), it->Length()}; CheckIfNullTerminated(token); UNIT_ASSERT_VALUES_EQUAL(tokens[tokensCount], token); ++tokensCount; @@ -80,7 +79,7 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { auto&& tokenizer = TStreamTokenizer{&input}; auto tokensCount = size_t{}; for (auto it = tokenizer.begin(); tokenizer.end() != it; ++it) { - const auto token = TStringBuf{it->Data(), it->Length()}; + const auto token = std::string_view{it->Data(), it->Length()}; CheckIfNullTerminated(token); UNIT_ASSERT_VALUES_EQUAL(data, token); ++tokensCount; @@ -91,14 +90,14 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { Y_UNIT_TEST(SimpleTest) { const char data[] = "qwerty\n1234567890\n"; const auto dataSize = Y_ARRAY_SIZE(data) - 1; - const TStringBuf tokens[] = {TStringBuf("qwerty"), TStringBuf("1234567890")}; + const std::string_view tokens[] = {std::string_view("qwerty"), std::string_view("1234567890")}; const auto tokensSize = Y_ARRAY_SIZE(tokens); auto&& input = TMemoryInput{data, dataSize}; auto&& tokenizer = TStreamTokenizer{&input}; auto tokensCount = size_t{}; for (auto it = tokenizer.begin(); tokenizer.end() != it; ++it) { UNIT_ASSERT(tokensCount < tokensSize); - const auto token = TStringBuf{it->Data(), it->Length()}; + const auto token = std::string_view{it->Data(), it->Length()}; CheckIfNullTerminated(token); UNIT_ASSERT_VALUES_EQUAL(tokens[tokensCount], token); ++tokensCount; @@ -115,14 +114,14 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { const char data[] = "abc|def|xxxxxx"; const auto dataSize = Y_ARRAY_SIZE(data) - 1; - const TStringBuf tokens[] = {TStringBuf("abc"), TStringBuf("def"), TStringBuf("xxxxxx")}; + const std::string_view tokens[] = {std::string_view("abc"), std::string_view("def"), std::string_view("xxxxxx")}; const auto tokensSize = Y_ARRAY_SIZE(tokens); auto&& input = TMemoryInput{data, dataSize}; auto&& tokenizer = TStreamTokenizer{&input}; auto tokensCount = size_t{}; for (auto it = tokenizer.begin(); tokenizer.end() != it; ++it) { UNIT_ASSERT(tokensCount < tokensSize); - const auto token = TStringBuf{it->Data(), it->Length()}; + const auto token = std::string_view{it->Data(), it->Length()}; CheckIfNullTerminated(token); UNIT_ASSERT_VALUES_EQUAL(tokens[tokensCount], token); ++tokensCount; @@ -139,15 +138,15 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { const char data[] = "abc|def|xxxxxx,abc|def|xxxxxx"; const auto dataSize = Y_ARRAY_SIZE(data) - 1; - const TStringBuf tokens[] = {TStringBuf("abc"), TStringBuf("def"), TStringBuf("xxxxxx"), - TStringBuf("abc"), TStringBuf("def"), TStringBuf("xxxxxx")}; + const std::string_view tokens[] = {std::string_view("abc"), std::string_view("def"), std::string_view("xxxxxx"), + std::string_view("abc"), std::string_view("def"), std::string_view("xxxxxx")}; const auto tokensSize = Y_ARRAY_SIZE(tokens); auto&& input = TMemoryInput{data, dataSize}; auto&& tokenizer = TStreamTokenizer{&input}; auto tokensCount = size_t{}; for (auto it = tokenizer.begin(); tokenizer.end() != it; ++it) { UNIT_ASSERT(tokensCount < tokensSize); - const auto token = TStringBuf{it->Data(), it->Length()}; + const auto token = std::string_view{it->Data(), it->Length()}; CheckIfNullTerminated(token); UNIT_ASSERT_VALUES_EQUAL(tokens[tokensCount], token); ++tokensCount; @@ -168,7 +167,7 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { auto&& tokenizer = TStreamTokenizer{&input}; auto tokensCount = size_t{}; for (auto it = tokenizer.begin(); tokenizer.end() != it; ++it) { - const auto token = TStringBuf{it->Data(), it->Length()}; + const auto token = std::string_view{it->Data(), it->Length()}; CheckIfNullTerminated(token); UNIT_ASSERT_VALUES_EQUAL(data, token); ++tokensCount; @@ -189,7 +188,7 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { auto&& tokenizer = TStreamTokenizer{&input}; auto tokensCount = size_t{}; for (auto it = tokenizer.begin(); tokenizer.end() != it; ++it) { - CheckIfNullTerminated(TStringBuf{it->Data(), it->Length()}); + CheckIfNullTerminated(std::string_view{it->Data(), it->Length()}); UNIT_ASSERT_VALUES_EQUAL(0, it->Length()); ++tokensCount; } @@ -199,13 +198,13 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { Y_UNIT_TEST(FirstTokenHasSizeOfTheBufferTest) { const char data[] = "xxxxx\nxx"; const auto dataSize = Y_ARRAY_SIZE(data) - 1; - const TStringBuf tokens[] = {TStringBuf("xxxxx"), TStringBuf("xx")}; + const std::string_view tokens[] = {std::string_view("xxxxx"), std::string_view("xx")}; const auto tokensSize = Y_ARRAY_SIZE(tokens); auto&& input = TMemoryInput{data, dataSize}; auto&& tokenizer = TStreamTokenizer{&input, TEol{}, tokens[0].size()}; auto tokensCount = size_t{}; for (auto it = tokenizer.begin(); tokenizer.end() != it; ++it) { - const auto token = TStringBuf{it->Data(), it->Length()}; + const auto token = std::string_view{it->Data(), it->Length()}; CheckIfNullTerminated(token); UNIT_ASSERT_VALUES_EQUAL(tokens[tokensCount], token); ++tokensCount; @@ -220,7 +219,7 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { auto&& tokenizer = TStreamTokenizer{&input, TEol{}, dataSize}; auto tokensCount = size_t{}; for (auto it = tokenizer.begin(); tokenizer.end() != it; ++it) { - const auto token = TStringBuf{it->Data(), it->Length()}; + const auto token = std::string_view{it->Data(), it->Length()}; CheckIfNullTerminated(token); UNIT_ASSERT_VALUES_EQUAL(data, token); ++tokensCount; @@ -231,13 +230,13 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { Y_UNIT_TEST(BufferSizeInitialSizeSmallerThanTokenTest) { const char data[] = "xxxxx\nxx"; const auto dataSize = Y_ARRAY_SIZE(data) - 1; - const TStringBuf tokens[] = {TStringBuf("xxxxx"), TStringBuf("xx")}; + const std::string_view tokens[] = {std::string_view("xxxxx"), std::string_view("xx")}; const auto tokensSize = Y_ARRAY_SIZE(tokens); auto&& input = TMemoryInput{data, dataSize}; auto&& tokenizer = TStreamTokenizer{&input, TEol{}, 1}; auto tokensCount = size_t{}; for (auto it = tokenizer.begin(); tokenizer.end() != it; ++it) { - const auto token = TStringBuf{it->Data(), it->Length()}; + const auto token = std::string_view{it->Data(), it->Length()}; CheckIfNullTerminated(token); UNIT_ASSERT_VALUES_EQUAL(tokens[tokensCount], token); ++tokensCount; @@ -248,7 +247,7 @@ Y_UNIT_TEST_SUITE(TStreamTokenizerTests) { Y_UNIT_TEST(RangeBasedForTest) { const char data[] = "abc\ndef\nxxxxxx"; const auto dataSize = Y_ARRAY_SIZE(data) - 1; - const TStringBuf tokens[] = {TStringBuf("abc"), TStringBuf("def"), TStringBuf("xxxxxx")}; + const std::string_view tokens[] = {std::string_view("abc"), std::string_view("def"), std::string_view("xxxxxx")}; const auto tokensSize = Y_ARRAY_SIZE(tokens); auto&& input = TMemoryInput{data, dataSize}; auto&& tokenizer = TStreamTokenizer{&input}; diff --git a/util/stream/walk.cpp b/util/stream/walk.cpp index 57dc9ab036c..a9704e6fe04 100644 --- a/util/stream/walk.cpp +++ b/util/stream/walk.cpp @@ -1,6 +1,5 @@ #include "walk.h" -#include void IWalkInput::DoUndo(size_t len) { Len_ += len; diff --git a/util/stream/walk_ut.cpp b/util/stream/walk_ut.cpp index 282c8d0a629..7528e58b230 100644 --- a/util/stream/walk_ut.cpp +++ b/util/stream/walk_ut.cpp @@ -4,7 +4,7 @@ class TStringListInput: public IWalkInput { public: - TStringListInput(const std::vector& data) + TStringListInput(const std::vector& data) : Data_(data) , Index_(0) { @@ -16,20 +16,20 @@ class TStringListInput: public IWalkInput { return 0; } - const TString& string = Data_[Index_++]; + const std::string& string = Data_[Index_++]; *ptr = string.data(); return string.size(); } private: - const std::vector& Data_; + const std::vector& Data_; size_t Index_; }; Y_UNIT_TEST_SUITE(TWalkTest) { Y_UNIT_TEST(ReadTo) { - std::vector data; + std::vector data; data.push_back("111a"); data.push_back("222b"); data.push_back("333c"); @@ -39,17 +39,17 @@ Y_UNIT_TEST_SUITE(TWalkTest) { TStringListInput input(data); - TString tmp1 = input.ReadTo('c'); + std::string tmp1 = input.ReadTo('c'); UNIT_ASSERT_VALUES_EQUAL(tmp1, "111a222b333"); char tmp2; input.Read(&tmp2, 1); UNIT_ASSERT_VALUES_EQUAL(tmp2, '4'); - TString tmp3 = input.ReadTo('6'); + std::string tmp3 = input.ReadTo('6'); UNIT_ASSERT_VALUES_EQUAL(tmp3, "44d555e"); - TString tmp4 = input.ReadAll(); + std::string tmp4 = input.ReadAll(); UNIT_ASSERT_VALUES_EQUAL(tmp4, "66f"); } } diff --git a/util/stream/zerocopy.cpp b/util/stream/zerocopy.cpp index 542e438fc99..1b4387bf07f 100644 --- a/util/stream/zerocopy.cpp +++ b/util/stream/zerocopy.cpp @@ -34,7 +34,7 @@ size_t IZeroCopyInput::DoSkip(size_t len) { IZeroCopyInputFastReadTo::~IZeroCopyInputFastReadTo() = default; -size_t IZeroCopyInputFastReadTo::DoReadTo(TString& st, char ch) { +size_t IZeroCopyInputFastReadTo::DoReadTo(std::string& st, char ch) { const char* ptr; size_t len = Next(&ptr); if (!len) { diff --git a/util/stream/zerocopy.h b/util/stream/zerocopy.h index 7676b1baffb..23ff04a2443 100644 --- a/util/stream/zerocopy.h +++ b/util/stream/zerocopy.h @@ -70,7 +70,7 @@ class IZeroCopyInputFastReadTo: public IZeroCopyInput { IZeroCopyInputFastReadTo& operator=(IZeroCopyInputFastReadTo&&) noexcept = default; protected: - size_t DoReadTo(TString& st, char ch) override; + size_t DoReadTo(std::string& st, char ch) override; private: /** diff --git a/util/stream/zerocopy_output_ut.cpp b/util/stream/zerocopy_output_ut.cpp index e81f7fb056c..9798302ec4f 100644 --- a/util/stream/zerocopy_output_ut.cpp +++ b/util/stream/zerocopy_output_ut.cpp @@ -2,13 +2,12 @@ #include -#include // This version of string output stream is written here only // for testing IZeroCopyOutput implementation of DoWrite. class TSimpleStringOutput: public IZeroCopyOutput { public: - TSimpleStringOutput(TString& s) noexcept + TSimpleStringOutput(std::string& s) noexcept : S_(s) { } @@ -29,14 +28,14 @@ class TSimpleStringOutput: public IZeroCopyOutput { S_.resize(S_.size() - len); } - TString& S_; + std::string& S_; }; Y_UNIT_TEST_SUITE(TestZerocopyOutput) { Y_UNIT_TEST(Write) { - TString str; + std::string str; TSimpleStringOutput output(str); - TString result; + std::string result; for (size_t i = 0; i < 1000; ++i) { result.push_back('a' + (i % 20)); diff --git a/util/stream/zlib.cpp b/util/stream/zlib.cpp index 06f181ee53a..7040a132740 100644 --- a/util/stream/zlib.cpp +++ b/util/stream/zlib.cpp @@ -79,7 +79,7 @@ namespace { class TZLibDecompress::TImpl: private TZLibCommon, public TChunkedZeroCopyInput { public: - inline TImpl(IZeroCopyInput* in, ZLib::StreamType type, TStringBuf dict) + inline TImpl(IZeroCopyInput* in, ZLib::StreamType type, std::string_view dict) : TChunkedZeroCopyInput(in) , Dict(dict) { @@ -157,13 +157,13 @@ class TZLibDecompress::TImpl: private TZLibCommon, public TChunkedZeroCopyInput } bool AllowMultipleStreams_ = true; - TStringBuf Dict; + std::string_view Dict; }; namespace { class TDecompressStream: public IZeroCopyInput, public TZLibDecompress::TImpl, public TAdditionalStorage { public: - inline TDecompressStream(IInputStream* input, ZLib::StreamType type, TStringBuf dict) + inline TDecompressStream(IInputStream* input, ZLib::StreamType type, std::string_view dict) : TZLibDecompress::TImpl(this, type, dict) , Stream_(input) { @@ -317,12 +317,12 @@ class TZLibCompress::TImpl: public TAdditionalStorage, private TZLibCommo THolder GZHeader_; }; -TZLibDecompress::TZLibDecompress(IZeroCopyInput* input, ZLib::StreamType type, TStringBuf dict) +TZLibDecompress::TZLibDecompress(IZeroCopyInput* input, ZLib::StreamType type, std::string_view dict) : Impl_(new TZeroCopyDecompress(input, type, dict)) { } -TZLibDecompress::TZLibDecompress(IInputStream* input, ZLib::StreamType type, size_t buflen, TStringBuf dict) +TZLibDecompress::TZLibDecompress(IInputStream* input, ZLib::StreamType type, size_t buflen, std::string_view dict) : Impl_(new (buflen) TDecompressStream(input, type, dict)) { } diff --git a/util/stream/zlib.h b/util/stream/zlib.h index e7de7c81b74..6dd78e1e377 100644 --- a/util/stream/zlib.h +++ b/util/stream/zlib.h @@ -47,9 +47,9 @@ namespace ZLib { */ class TZLibDecompress: public IInputStream { public: - TZLibDecompress(IZeroCopyInput* input, ZLib::StreamType type = ZLib::Auto, TStringBuf dict = {}); + TZLibDecompress(IZeroCopyInput* input, ZLib::StreamType type = ZLib::Auto, std::string_view dict = {}); TZLibDecompress(IInputStream* input, ZLib::StreamType type = ZLib::Auto, size_t buflen = ZLib::ZLIB_BUF_LEN, - TStringBuf dict = {}); + std::string_view dict = {}); /** * Allows/disallows multiple sequential compressed streams. Allowed by default. @@ -104,7 +104,7 @@ class TZLibCompress: public IOutputStream { return *this; } - inline TParams& SetDict(const TStringBuf dict) noexcept { + inline TParams& SetDict(const std::string_view dict) noexcept { Dict = dict; return *this; @@ -114,7 +114,7 @@ class TZLibCompress: public IOutputStream { ZLib::StreamType Type; size_t CompressionLevel; size_t BufLen; - TStringBuf Dict; + std::string_view Dict; }; inline TZLibCompress(const TParams& params) { diff --git a/util/stream/zlib_ut.cpp b/util/stream/zlib_ut.cpp index 95e419a4f47..bdbc642983e 100644 --- a/util/stream/zlib_ut.cpp +++ b/util/stream/zlib_ut.cpp @@ -51,8 +51,8 @@ class TThrowingStream: public IOutputStream { }; Y_UNIT_TEST_SUITE(TZLibTest) { - static const TString DATA = "8s7d5vc6s5vc67sa4c65ascx6asd4xcv76adsfxv76s"; - static const TString DATA2 = "cn8wk2bd9vb3vdfif83g1ks94bfiovtwv"; + static const std::string DATA = "8s7d5vc6s5vc67sa4c65ascx6asd4xcv76adsfxv76s"; + static const std::string DATA2 = "cn8wk2bd9vb3vdfif83g1ks94bfiovtwv"; Y_UNIT_TEST(Compress) { TUnbufferedFileOutput o(ZDATA); @@ -75,8 +75,8 @@ Y_UNIT_TEST_SUITE(TZLibTest) { } Y_UNIT_TEST(Dictionary) { - static constexpr TStringBuf data = ""; - static constexpr TStringBuf dict = ""; + static constexpr std::string_view data = ""; + static constexpr std::string_view dict = ""; for (auto type : {ZLib::Raw, ZLib::ZLib}) { TStringStream compressed; { @@ -148,7 +148,7 @@ Y_UNIT_TEST_SUITE(TZLibTest) { } Y_UNIT_TEST(CompressFlush) { - TString data = ""; + std::string data = ""; for (size_t i = 0; i < 32; ++i) { TTempFile tmpFile(ZDATA); @@ -195,10 +195,10 @@ Y_UNIT_TEST_SUITE(TZLibTest) { Y_UNIT_TEST(CompressFlushSmallBuffer) { for (size_t bufferSize = 16; bufferSize < 32; ++bufferSize) { - TString firstData = ""; + std::string firstData = ""; for (size_t firstDataSize = 0; firstDataSize < 16; ++firstDataSize) { - TString secondData = ""; + std::string secondData = ""; for (size_t secondDataSize = 0; secondDataSize < 16; ++secondDataSize) { TTempFile tmpFile(ZDATA); diff --git a/util/string/ascii.cpp b/util/string/ascii.cpp index 95edb95cc88..f930865bb9c 100644 --- a/util/string/ascii.cpp +++ b/util/string/ascii.cpp @@ -43,7 +43,7 @@ extern const unsigned char NPrivate::ASCII_LOWER[256] = { }; // clang-format on -int AsciiCompareIgnoreCase(const TStringBuf s1, const TStringBuf s2) noexcept { +int AsciiCompareIgnoreCase(const std::string_view s1, const std::string_view s2) noexcept { if (s1.size() <= s2.size()) { if (int cmp = strnicmp(s1.data(), s2.data(), s1.size())) { return cmp; diff --git a/util/string/ascii.h b/util/string/ascii.h index 94bf920e817..21b1e66c7b5 100644 --- a/util/string/ascii.h +++ b/util/string/ascii.h @@ -1,8 +1,11 @@ #pragma once +#include + #include #include -#include + +#include // ctype.h-like functions, locale-independent: // IsAscii{Upper,Lower,Digit,Alpha,Alnum,Space} and @@ -31,13 +34,6 @@ namespace NPrivate { using type = T; }; -#ifndef TSTRING_IS_STD_STRING - template - struct TDereference> { - using type = typename String::value_type; - }; -#endif - template using TDereferenced = typename TDereference::type; @@ -51,13 +47,6 @@ namespace NPrivate { return c >= static_cast(0) && c <= static_cast(127); } - -#ifndef TSTRING_IS_STD_STRING - template - bool RangeOk(const TBasicCharRef& c) { - return RangeOk(static_cast(c)); - } -#endif } constexpr bool IsAscii(const int c) noexcept { @@ -179,7 +168,7 @@ static inline bool AsciiEqualsIgnoreCase(const char* s1, const char* s2) noexcep * * @return true iff @c s1 ans @c s2 are case-insensitively equal. */ -static inline bool AsciiEqualsIgnoreCase(const TStringBuf s1, const TStringBuf s2) noexcept { +static inline bool AsciiEqualsIgnoreCase(const std::string_view s1, const std::string_view s2) noexcept { if (s1.size() != s2.size()) { return false; } @@ -217,7 +206,7 @@ static inline int AsciiCompareIgnoreCase(const char* s1, const char* s2) noexcep * - positive otherwise, * similar to stricmp. */ -Y_PURE_FUNCTION int AsciiCompareIgnoreCase(const TStringBuf s1, const TStringBuf s2) noexcept; +Y_PURE_FUNCTION int AsciiCompareIgnoreCase(const std::string_view s1, const std::string_view s2) noexcept; /** * ASCII case-sensitive string comparison (for proper UTF8 strings @@ -228,7 +217,7 @@ Y_PURE_FUNCTION int AsciiCompareIgnoreCase(const TStringBuf s1, const TStringBuf * * @return true iff @c s2 are case-sensitively prefix of @c s1. */ -static inline bool AsciiHasPrefix(const TStringBuf s1, const TStringBuf s2) noexcept { +static inline bool AsciiHasPrefix(const std::string_view s1, const std::string_view s2) noexcept { return (s1.size() >= s2.size()) && memcmp(s1.data(), s2.data(), s2.size()) == 0; } @@ -238,7 +227,7 @@ static inline bool AsciiHasPrefix(const TStringBuf s1, const TStringBuf s2) noex * * @return true iff @c s2 are case-insensitively prefix of @c s1. */ -static inline bool AsciiHasPrefixIgnoreCase(const TStringBuf s1, const TStringBuf s2) noexcept { +static inline bool AsciiHasPrefixIgnoreCase(const std::string_view s1, const std::string_view s2) noexcept { return (s1.size() >= s2.size()) && strnicmp(s1.data(), s2.data(), s2.size()) == 0; } @@ -248,6 +237,6 @@ static inline bool AsciiHasPrefixIgnoreCase(const TStringBuf s1, const TStringBu * * @return true iff @c s2 are case-insensitively suffix of @c s1. */ -static inline bool AsciiHasSuffixIgnoreCase(const TStringBuf s1, const TStringBuf s2) noexcept { +static inline bool AsciiHasSuffixIgnoreCase(const std::string_view s1, const std::string_view s2) noexcept { return (s1.size() >= s2.size()) && strnicmp((s1.data() + (s1.size() - s2.size())), s2.data(), s2.size()) == 0; } diff --git a/util/string/ascii_ut.cpp b/util/string/ascii_ut.cpp index 89069fee50a..f2bd0c7945b 100644 --- a/util/string/ascii_ut.cpp +++ b/util/string/ascii_ut.cpp @@ -64,19 +64,19 @@ Y_UNIT_TEST_SUITE(TAsciiTest) { Y_UNIT_TEST(CompareTest) { UNIT_ASSERT(AsciiEqualsIgnoreCase("qqq", "qQq")); - UNIT_ASSERT(AsciiEqualsIgnoreCase("qqq", TStringBuf("qQq"))); - TString qq = "qq"; - TString qQ = "qQ"; + UNIT_ASSERT(AsciiEqualsIgnoreCase("qqq", std::string_view("qQq"))); + std::string qq = "qq"; + std::string qQ = "qQ"; UNIT_ASSERT(AsciiEqualsIgnoreCase(qq, qQ)); - TString x = "qqqA"; - TString y = "qQqB"; - TString z = "qQnB"; - TString zz = "qQqq"; - TString zzz = "qQqqq"; - TStringBuf xs = TStringBuf(x.data(), 3); - TStringBuf ys = TStringBuf(y.data(), 3); - TStringBuf zs = TStringBuf(z.data(), 3); + std::string x = "qqqA"; + std::string y = "qQqB"; + std::string z = "qQnB"; + std::string zz = "qQqq"; + std::string zzz = "qQqqq"; + std::string_view xs = std::string_view(x.data(), 3); + std::string_view ys = std::string_view(y.data(), 3); + std::string_view zs = std::string_view(z.data(), 3); UNIT_ASSERT(AsciiCompareIgnoreCase(xs, ys) == 0); UNIT_ASSERT(AsciiCompareIgnoreCase(xs, zs) > 0); UNIT_ASSERT(AsciiCompareIgnoreCase(xs, zz) < 0); diff --git a/util/string/builder.cpp b/util/string/builder.cpp index a3821d33991..33e53456396 100644 --- a/util/string/builder.cpp +++ b/util/string/builder.cpp @@ -4,5 +4,5 @@ template <> void Out(IOutputStream& os, const TStringBuilder& sb) { - os << static_cast(sb); + os << static_cast(sb); } diff --git a/util/string/builder.h b/util/string/builder.h index 9620fc86067..e5668fd1a31 100644 --- a/util/string/builder.h +++ b/util/string/builder.h @@ -2,10 +2,9 @@ #include #include -#include namespace NPrivateStringBuilder { - class TStringBuilder: public TString { + class TStringBuilder: public std::string { public: inline TStringBuilder() : Out(*this) @@ -13,7 +12,7 @@ namespace NPrivateStringBuilder { } TStringBuilder(TStringBuilder&& rhs) noexcept - : TString(std::move(rhs)) + : std::string(std::move(rhs)) , Out(*this) { } diff --git a/util/string/builder_ut.cpp b/util/string/builder_ut.cpp index 22def683ec2..563a088ce7e 100644 --- a/util/string/builder_ut.cpp +++ b/util/string/builder_ut.cpp @@ -2,15 +2,15 @@ #include -static void TestEquals(const TString& expected, const TString& actual) { +static void TestEquals(const std::string& expected, const std::string& actual) { UNIT_ASSERT_VALUES_EQUAL(expected, actual); } struct TClassWithStreamOperator { ui32 Id; - TString Name; + std::string Name; - TClassWithStreamOperator(ui32 id, const TString& name) + TClassWithStreamOperator(ui32 id, const std::string& name) : Id(id) , Name(name) { @@ -30,7 +30,7 @@ Y_UNIT_TEST_SUITE(TStringBuilderTest) { } Y_UNIT_TEST(TestStringBuilderOut) { - TString s; + std::string s; TStringOutput out(s); TStringBuilder sb; sb << "a"; @@ -40,11 +40,11 @@ Y_UNIT_TEST_SUITE(TStringBuilderTest) { Y_UNIT_TEST(TestStringBuilderRValue) { struct TRValueAcceptTester { - static bool IsRValue(const TString&) { + static bool IsRValue(const std::string&) { return false; } - static bool IsRValue(TString&&) { + static bool IsRValue(std::string&&) { return true; } }; diff --git a/util/string/cast.cpp b/util/string/cast.cpp index 2922b986c22..69a29ce1f93 100644 --- a/util/string/cast.cpp +++ b/util/string/cast.cpp @@ -15,7 +15,6 @@ #include -#include #include #include #include @@ -66,7 +65,7 @@ namespace { inline size_t FormatFlt(T t, char* buf, size_t len) { const int ret = snprintf(buf, len, TFltModifiers::ModifierWrite, t); - Y_ENSURE(ret >= 0 && (size_t)ret <= len, TStringBuf("cannot format float")); + Y_ENSURE(ret >= 0 && (size_t)ret <= len, std::string_view("cannot format float")); return (size_t)ret; } @@ -257,21 +256,21 @@ namespace { [[noreturn]] static Y_NO_INLINE void ThrowParseError(EParseStatus status, const TChar* data, size_t len, const TChar* pos) { Y_ASSERT(status != PS_OK); - typedef TBasicString TStringType; + typedef std::basic_string TStringType; switch (status) { case PS_EMPTY_STRING: - ythrow TFromStringException() << TStringBuf("Cannot parse empty string as number. "); + ythrow TFromStringException() << std::string_view("Cannot parse empty string as number. "); case PS_PLUS_STRING: - ythrow TFromStringException() << TStringBuf("Cannot parse string \"+\" as number. "); + ythrow TFromStringException() << std::string_view("Cannot parse string \"+\" as number. "); case PS_MINUS_STRING: - ythrow TFromStringException() << TStringBuf("Cannot parse string \"-\" as number. "); + ythrow TFromStringException() << std::string_view("Cannot parse string \"-\" as number. "); case PS_BAD_SYMBOL: - ythrow TFromStringException() << TStringBuf("Unexpected symbol \"") << EscapeC(*pos) << TStringBuf("\" at pos ") << (pos - data) << TStringBuf(" in string ") << TStringType(data, len).Quote() << TStringBuf(". "); + ythrow TFromStringException() << std::string_view("Unexpected symbol \"") << EscapeC(*pos) << std::string_view("\" at pos ") << (pos - data) << std::string_view(" in string ") << NUtils::Quote(TStringType(data, len)) << std::string_view(". "); case PS_OVERFLOW: - ythrow TFromStringException() << TStringBuf("Integer overflow in string ") << TStringType(data, len).Quote() << TStringBuf(". "); + ythrow TFromStringException() << std::string_view("Integer overflow in string ") << NUtils::Quote(TStringType(data, len)) << std::string_view(". "); default: - ythrow yexception() << TStringBuf("Unknown error code in string converter. "); + ythrow yexception() << std::string_view("Unknown error code in string converter. "); } } @@ -316,7 +315,7 @@ namespace { return ret; } - ythrow TFromStringException() << TStringBuf("cannot parse float(") << TStringBuf(data, len) << TStringBuf(")"); + ythrow TFromStringException() << std::string_view("cannot parse float(") << std::string_view(data, len) << std::string_view(")"); } #define DEF_FLT_MOD(type, modifierWrite, modifierRead) \ @@ -406,7 +405,7 @@ DEF_FLT_SPEC(long double) template <> size_t ToStringImpl(bool t, char* buf, size_t len) { - Y_ENSURE(len, TStringBuf("zero length")); + Y_ENSURE(len, std::string_view("zero length")); *buf = t ? '1' : '0'; return 1; } @@ -426,7 +425,7 @@ bool TryFromStringImpl(const char* data, size_t len, bool& result) { return true; } } - TStringBuf buf(data, len); + std::string_view buf(data, len); if (IsTrue(buf)) { result = true; return true; @@ -442,20 +441,15 @@ bool FromStringImpl(const char* data, size_t len) { bool result; if (!TryFromStringImpl(data, len, result)) { - ythrow TFromStringException() << TStringBuf("Cannot parse bool(") << TStringBuf(data, len) << TStringBuf("). "); + ythrow TFromStringException() << std::string_view("Cannot parse bool(") << std::string_view(data, len) << std::string_view("). "); } return result; } template <> -TString FromStringImpl(const char* data, size_t len) { - return TString(data, len); -} - -template <> -TStringBuf FromStringImpl(const char* data, size_t len) { - return TStringBuf(data, len); +std::string_view FromStringImpl(const char* data, size_t len) { + return std::string_view(data, len); } template <> @@ -469,28 +463,22 @@ std::filesystem::path FromStringImpl(const char* data, si } template <> -TUtf16String FromStringImpl(const wchar16* data, size_t len) { - return TUtf16String(data, len); +std::u16string FromStringImpl(const wchar16* data, size_t len) { + return std::u16string(data, len); } template <> -TWtringBuf FromStringImpl(const wchar16* data, size_t len) { - return TWtringBuf(data, len); +std::u16string_view FromStringImpl(const wchar16* data, size_t len) { + return std::u16string_view(data, len); } // Try-versions template <> -bool TryFromStringImpl(const char* data, size_t len, TStringBuf& result) { +bool TryFromStringImpl(const char* data, size_t len, std::string_view& result) { result = {data, len}; return true; } -template <> -bool TryFromStringImpl(const char* data, size_t len, TString& result) { - result = TString(data, len); - return true; -} - template <> bool TryFromStringImpl(const char* data, size_t len, std::string& result) { result.assign(data, len); @@ -498,14 +486,14 @@ bool TryFromStringImpl(const char* data, size_t len, std::string& r } template <> -bool TryFromStringImpl(const wchar16* data, size_t len, TWtringBuf& result) { +bool TryFromStringImpl(const wchar16* data, size_t len, std::u16string_view& result) { result = {data, len}; return true; } template <> -bool TryFromStringImpl(const wchar16* data, size_t len, TUtf16String& result) { - result = TUtf16String(data, len); +bool TryFromStringImpl(const wchar16* data, size_t len, std::u16string& result) { + result = std::u16string(data, len); return true; } @@ -620,7 +608,7 @@ template <> double FromStringImpl(const char* data, size_t len) { double d = 0.0; if (!TryFromStringImpl(data, len, d)) { - ythrow TFromStringException() << TStringBuf("cannot parse float(") << TStringBuf(data, len) << TStringBuf(")"); + ythrow TFromStringException() << std::string_view("cannot parse float(") << std::string_view(data, len) << std::string_view(")"); } return d; } diff --git a/util/string/cast.h b/util/string/cast.h index 26818c8626e..53f987d1eb1 100644 --- a/util/string/cast.h +++ b/util/string/cast.h @@ -1,12 +1,13 @@ #pragma once +#include + #include #include -#include -#include #include #include +#include #include /* @@ -52,26 +53,26 @@ size_t FloatToString(float t, char* buf, size_t len, EFloatToStringMode mode = P size_t FloatToString(double t, char* buf, size_t len, EFloatToStringMode mode = PREC_AUTO, int ndigits = 0); template -inline TString FloatToString(const T& t, EFloatToStringMode mode = PREC_AUTO, int ndigits = 0) { +inline std::string FloatToString(const T& t, EFloatToStringMode mode = PREC_AUTO, int ndigits = 0) { char buf[512]; // Max() with mode = PREC_POINT_DIGITS has 309 digits before the decimal point size_t count = FloatToString(t, buf, sizeof(buf), mode, ndigits); - return TString(buf, count); + return std::string(buf, count); } namespace NPrivate { template struct TToString { - static inline TString Cvt(const T& t) { + static inline std::string Cvt(const T& t) { char buf[512]; - return TString(buf, ToString(t, buf, sizeof(buf))); + return std::string(buf, ToString(t, buf, sizeof(buf))); } }; template struct TToString { - static inline TString Cvt(const T& t) { - TString s; + static inline std::string Cvt(const T& t) { + std::string s; TStringOutput o(s); o << t; return s; @@ -83,25 +84,25 @@ namespace NPrivate { * some clever implementations... */ template -inline TString ToString(const T& t) { +inline std::string ToString(const T& t) { using TR = std::remove_cv_t; return ::NPrivate::TToString::value>::Cvt((const TR&)t); } -inline const TString& ToString(const TString& s Y_LIFETIME_BOUND) noexcept { +inline const std::string& ToString(const std::string& s Y_LIFETIME_BOUND) noexcept { return s; } -inline TString&& ToString(TString&& s Y_LIFETIME_BOUND) noexcept { +inline std::string&& ToString(std::string&& s Y_LIFETIME_BOUND) noexcept { return std::move(s); } -inline TString ToString(const char* s) { +inline std::string ToString(const char* s) { return s; } -inline TString ToString(char* s) { +inline std::string ToString(char* s) { return s; } @@ -109,15 +110,15 @@ inline TString ToString(char* s) { * Wrapper for wide strings. */ template -inline TUtf16String ToWtring(const T& t) { - return TUtf16String::FromAscii(ToString(t)); +inline std::u16string ToWtring(const T& t) { + return NUtils::FromAscii(ToString(t)); } -inline const TUtf16String& ToWtring(const TUtf16String& w Y_LIFETIME_BOUND) noexcept { +inline const std::u16string& ToWtring(const std::u16string& w Y_LIFETIME_BOUND) noexcept { return w; } -inline TUtf16String&& ToWtring(TUtf16String&& w Y_LIFETIME_BOUND) noexcept { +inline std::u16string&& ToWtring(std::u16string&& w Y_LIFETIME_BOUND) noexcept { return std::move(w); } @@ -153,12 +154,7 @@ inline T FromString(const TChar* data) { } template -inline T FromString(const TStringBuf& s) { - return ::FromString(s.data(), s.size()); -} - -template -inline T FromString(const TString& s) { +inline T FromString(const std::string_view& s) { return ::FromString(s.data(), s.size()); } @@ -168,17 +164,17 @@ inline T FromString(const std::string& s) { } template <> -inline TString FromString(const TString& s) { +inline std::string FromString(const std::string& s) { return s; } template -inline T FromString(const TWtringBuf& s) { - return ::FromString(s.data(), s.size()); +inline T FromString(const std::u16string_view& s) { + return ::FromString(s.data(), s.size()); } template -inline T FromString(const TUtf16String& s) { +inline T FromString(const std::u16string& s) { return ::FromString(s.data(), s.size()); } @@ -248,12 +244,7 @@ inline bool TryFromString(const TChar* data, const size_t len, T& result, const } template -inline bool TryFromString(const TStringBuf& s, T& result) { - return TryFromString(s.data(), s.size(), result); -} - -template -inline bool TryFromString(const TString& s, T& result) { +inline bool TryFromString(const std::string_view& s, T& result) { return TryFromString(s.data(), s.size(), result); } @@ -263,17 +254,17 @@ inline bool TryFromString(const std::string& s, T& result) { } template -inline bool TryFromString(const TWtringBuf& s, T& result) { +inline bool TryFromString(const std::u16string_view& s, T& result) { return TryFromString(s.data(), s.size(), result); } template -inline bool TryFromString(const TUtf16String& s, T& result) { +inline bool TryFromString(const std::u16string& s, T& result) { return TryFromString(s.data(), s.size(), result); } template -inline std::optional TryFromString(TBasicStringBuf s) { +inline std::optional TryFromString(std::basic_string_view s) { std::optional result{std::in_place}; if (!TryFromString(s, *result)) { result.reset(); @@ -284,22 +275,17 @@ inline std::optional TryFromString(TBasicStringBuf s) { template inline std::optional TryFromString(const TChar* data) { - return TryFromString(TBasicStringBuf(data)); -} - -template -inline std::optional TryFromString(const TString& s) { - return TryFromString(TStringBuf(s)); + return TryFromString(std::basic_string_view(data)); } template inline std::optional TryFromString(const std::string& s) { - return TryFromString(TStringBuf(s)); + return TryFromString(std::string_view(s)); } template -inline std::optional TryFromString(const TUtf16String& s) { - return TryFromString(TWtringBuf(s)); +inline std::optional TryFromString(const std::u16string& s) { + return TryFromString(std::u16string_view(s)); } template @@ -309,7 +295,7 @@ inline bool TryFromStringWithDefault(const TStringType& s, T& result, const T& d template inline bool TryFromStringWithDefault(const char* s, T& result, const T& def) { - return TryFromStringWithDefault(TStringBuf(s), result, def); + return TryFromStringWithDefault(std::string_view(s), result, def); } template @@ -332,7 +318,7 @@ inline T FromStringWithDefault(const TStringType& s, const T& def) { template inline T FromStringWithDefault(const char* s, const T& def) { - return FromStringWithDefault(TStringBuf(s), def); + return FromStringWithDefault(std::string_view(s), def); } template @@ -347,12 +333,12 @@ template size_t IntToString(T t, char* buf, size_t len); template -inline TString IntToString(T t) { +inline std::string IntToString(T t) { static_assert(std::is_arithmetic>::value, "expect std::is_arithmetic>::value"); char buf[256]; - return TString(buf, IntToString(t, buf, sizeof(buf))); + return std::string(buf, IntToString(t, buf, sizeof(buf))); } template @@ -376,16 +362,16 @@ inline TInt IntFromString(const TStringType& str) { return IntFromString(str.data(), str.size()); } -static inline TString ToString(const TStringBuf str) { - return TString(str); +static inline std::string ToString(const std::string_view str) { + return std::string(str); } -static inline TUtf16String ToWtring(const TWtringBuf wtr) { - return TUtf16String(wtr); +static inline std::u16string ToWtring(const std::u16string_view wtr) { + return std::u16string(wtr); } -static inline TUtf32String ToUtf32String(const TUtf32StringBuf wtr) { - return TUtf32String(wtr); +static inline std::u32string ToUtf32String(const std::u32string_view wtr) { + return std::u32string(wtr); } template @@ -427,14 +413,14 @@ class TIntStringBuf { #endif } - constexpr operator TStringBuf() const noexcept { - return TStringBuf(Buf_, Size_); + constexpr operator std::string_view() const noexcept { + return std::string_view(Buf_, Size_); } constexpr static ui32 Convert(T t, TChar* buf, size_t bufLen) { bufLen = std::min(bufferSize, bufLen); if (std::is_signed::value && t < 0) { - Y_ENSURE(bufLen >= 2, TStringBuf("not enough room in buffer")); + Y_ENSURE(bufLen >= 2, std::string_view("not enough room in buffer")); buf[0] = '-'; const auto mt = std::make_unsigned_t(-(t + 1)) + std::make_unsigned_t(1); return ConvertUnsigned(mt, &buf[1], bufLen - 1) + 1; @@ -445,7 +431,7 @@ class TIntStringBuf { private: constexpr static ui32 ConvertUnsigned(typename std::make_unsigned::type t, TChar* buf, ui32 bufLen) { - Y_ENSURE(bufLen, TStringBuf("zero length")); + Y_ENSURE(bufLen, std::string_view("zero length")); if (t == 0) { *buf = '0'; @@ -465,7 +451,7 @@ class TIntStringBuf { ++l; t = v; } - Y_ENSURE(!t, TStringBuf("not enough room in buffer")); + Y_ENSURE(!t, std::string_view("not enough room in buffer")); if (buf != be) { for (ui32 i = 0; i < l; ++i) { *buf = *be; diff --git a/util/string/cast.pxd b/util/string/cast.pxd index dc23619e1e6..a49fcaab8ac 100644 --- a/util/string/cast.pxd +++ b/util/string/cast.pxd @@ -1,10 +1,8 @@ -from util.generic.string cimport TString - from libcpp cimport bool as bool_t cdef extern from "" nogil: - T FromString[T](const TString&) except + - bool_t TryFromString[T](const TString&, T&) except + - TString ToString[T](const T&) except + + T FromString[T](const std::string&) except + + bool_t TryFromString[T](const std::string&, T&) except + + std::string ToString[T](const T&) except + cdef double StrToD(const char* b, char** se) except + diff --git a/util/string/cast_ut.cpp b/util/string/cast_ut.cpp index 41300bab871..31ff719c225 100644 --- a/util/string/cast_ut.cpp +++ b/util/string/cast_ut.cpp @@ -43,12 +43,12 @@ template struct TRet { template - inline A IntFromStringForCheck(const TString& str) { + inline A IntFromStringForCheck(const std::string& str) { return IntFromString(str); } template - inline bool TryIntFromStringForCheck(const TString& str, A& result) { + inline bool TryIntFromStringForCheck(const std::string& str, A& result) { return TryIntFromString(str, result); } @@ -126,7 +126,7 @@ inline TRet F() { #if 0 template -inline void CheckConvertToBuffer(const T& value, const size_t size, const TString& canonValue) { +inline void CheckConvertToBuffer(const T& value, const size_t size, const std::string& canonValue) { const size_t maxSize = 256; char buffer[maxSize]; const char magic = 0x7F; @@ -143,8 +143,8 @@ inline void CheckConvertToBuffer(const T& value, const size_t size, const TStrin // check that no bytes after length was trashed for (size_t i = length; i < maxSize; ++i) UNIT_ASSERT_VALUES_EQUAL(buffer[i], magic); - TStringBuf result(buffer, length); - UNIT_ASSERT_VALUES_EQUAL(result, TStringBuf(canonValue)); + std::string_view result(buffer, length); + UNIT_ASSERT_VALUES_EQUAL(result, std::string_view(canonValue)); } } #endif @@ -317,12 +317,12 @@ Y_UNIT_TEST_SUITE(TCastTest) { } Y_UNIT_TEST(TestLiteral) { - UNIT_ASSERT_VALUES_EQUAL(ToString("abc"), TString("abc")); + UNIT_ASSERT_VALUES_EQUAL(ToString("abc"), std::string("abc")); } Y_UNIT_TEST(TestFromStringStringBuf) { - TString a = "xyz"; - TStringBuf b = FromString(a); + std::string a = "xyz"; + std::string_view b = FromString(a); UNIT_ASSERT_VALUES_EQUAL(a, b); UNIT_ASSERT_VALUES_EQUAL((void*)a.data(), (void*)b.data()); } @@ -344,23 +344,23 @@ Y_UNIT_TEST_SUITE(TCastTest) { CheckConvertToBuffer(1005000000000000ull, 32, "1005000000000000"); CheckConvertToBuffer(1005000000000000ull, 3, "1005000000000000"); - // TString longNumber = TString("1.") + TString(1 << 20, '1'); + // std::string longNumber = std::string("1.") + std::string(1 << 20, '1'); // UNIT_ASSERT_EXCEPTION(FromString(longNumber), yexception); } #endif Y_UNIT_TEST(TestWide) { - TUtf16String iw = u"-100500"; + std::u16string iw = u"-100500"; int iv = 0; UNIT_ASSERT_VALUES_EQUAL(TryFromString(iw, iv), true); UNIT_ASSERT_VALUES_EQUAL(iv, -100500); ui64 uv = 0; - TUtf16String uw = u"21474836470"; + std::u16string uw = u"21474836470"; UNIT_ASSERT_VALUES_EQUAL(TryFromString(uw, uv), true); UNIT_ASSERT_VALUES_EQUAL(uv, 21474836470ull); - TWtringBuf bw(uw.data(), uw.size()); + std::u16string_view bw(uw.data(), uw.size()); uv = 0; UNIT_ASSERT_VALUES_EQUAL(TryFromString(uw, uv), true); UNIT_ASSERT_VALUES_EQUAL(uv, 21474836470ull); @@ -375,7 +375,7 @@ Y_UNIT_TEST_SUITE(TCastTest) { size_t res = 0; const size_t def1 = 42; - TString s1("100500"); + std::string s1("100500"); UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s1, res, def1), true); UNIT_ASSERT_VALUES_EQUAL(res, 100500); @@ -390,7 +390,7 @@ Y_UNIT_TEST_SUITE(TCastTest) { UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s1), 100500); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault("100500", def1), 100500); - TString s2("100q500"); + std::string s2("100q500"); UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s2, res), false); UNIT_ASSERT_VALUES_EQUAL(res, size_t()); @@ -413,7 +413,7 @@ Y_UNIT_TEST_SUITE(TCastTest) { int res2 = 0; const int def2 = -6; - TUtf16String s3 = u"-100500"; + std::u16string s3 = u"-100500"; UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s3, res2, def2), true); UNIT_ASSERT_VALUES_EQUAL(res2, -100500); @@ -424,7 +424,7 @@ Y_UNIT_TEST_SUITE(TCastTest) { UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s3, def2), -100500); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s3), size_t()); - TUtf16String s4 = u"-f100500"; + std::u16string s4 = u"-f100500"; UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s4, res2, def2), false); UNIT_ASSERT_VALUES_EQUAL(res2, def2); @@ -452,13 +452,13 @@ Y_UNIT_TEST_SUITE(TCastTest) { Y_UNIT_TEST(TestAutoDetectType) { UNIT_ASSERT_DOUBLES_EQUAL((float)FromString("0.0001"), 0.0001, EPS); UNIT_ASSERT_DOUBLES_EQUAL((double)FromString("0.0015", sizeof("0.0015") - 2), 0.001, EPS); - UNIT_ASSERT_DOUBLES_EQUAL((long double)FromString(TStringBuf("0.0001")), 0.0001, EPS); - UNIT_ASSERT_DOUBLES_EQUAL((float)FromString(TString("10E-5")), 10E-5, EPS); + UNIT_ASSERT_DOUBLES_EQUAL((long double)FromString(std::string_view("0.0001")), 0.0001, EPS); + UNIT_ASSERT_DOUBLES_EQUAL((float)FromString(std::string("10E-5")), 10E-5, EPS); UNIT_ASSERT_VALUES_EQUAL((bool)FromString("da"), true); UNIT_ASSERT_VALUES_EQUAL((bool)FromString("no"), false); UNIT_ASSERT_VALUES_EQUAL((short)FromString(u"9000"), 9000); UNIT_ASSERT_VALUES_EQUAL((int)FromString(u"-100500"), -100500); - UNIT_ASSERT_VALUES_EQUAL((unsigned long long)FromString(TWtringBuf(u"42", 1)), 4); + UNIT_ASSERT_VALUES_EQUAL((unsigned long long)FromString(std::u16string_view(u"42", 1)), 4); int integer = FromString("125"); ui16 wideCharacterCode = FromString(u"125"); UNIT_ASSERT_VALUES_EQUAL(integer, wideCharacterCode); @@ -474,38 +474,38 @@ Y_UNIT_TEST_SUITE(TCastTest) { Y_UNIT_TEST(TryStringBuf) { { - constexpr TStringBuf hello = "hello"; - TStringBuf out; + constexpr std::string_view hello = "hello"; + std::string_view out; UNIT_ASSERT(TryFromString(hello, out)); UNIT_ASSERT_VALUES_EQUAL(hello, out); } { - constexpr TStringBuf empty = ""; - TStringBuf out; + constexpr std::string_view empty = ""; + std::string_view out; UNIT_ASSERT(TryFromString(empty, out)); UNIT_ASSERT_VALUES_EQUAL(empty, out); } { - constexpr TStringBuf empty; - TStringBuf out; + constexpr std::string_view empty; + std::string_view out; UNIT_ASSERT(TryFromString(empty, out)); UNIT_ASSERT_VALUES_EQUAL(empty, out); } { const auto hello = u"hello"; - TWtringBuf out; + std::u16string_view out; UNIT_ASSERT(TryFromString(hello, out)); UNIT_ASSERT_VALUES_EQUAL(hello, out); } { - const TUtf16String empty; - TWtringBuf out; + const std::u16string empty; + std::u16string_view out; UNIT_ASSERT(TryFromString(empty, out)); UNIT_ASSERT_VALUES_EQUAL(empty, out); } { - constexpr TWtringBuf empty; - TWtringBuf out; + constexpr std::u16string_view empty; + std::u16string_view out; UNIT_ASSERT(TryFromString(empty, out)); UNIT_ASSERT_VALUES_EQUAL(empty, out); } @@ -563,30 +563,30 @@ Y_UNIT_TEST_SUITE(TCastTest) { } Y_UNIT_TEST(TestTIntStringBuf) { - static_assert(TStringBuf(TIntStringBuf(111)) == TStringBuf("111")); - static_assert(TStringBuf(TIntStringBuf(-111)) == TStringBuf("-111")); - UNIT_ASSERT_VALUES_EQUAL(TStringBuf(TIntStringBuf(0)), "0"sv); - UNIT_ASSERT_VALUES_EQUAL(TStringBuf(TIntStringBuf(1111)), "1111"sv); - UNIT_ASSERT_VALUES_EQUAL(TStringBuf(TIntStringBuf(-1)), "-1"sv); - UNIT_ASSERT_VALUES_EQUAL(TStringBuf(TIntStringBuf(-1111)), "-1111"sv); + static_assert(std::string_view(TIntStringBuf(111)) == std::string_view("111")); + static_assert(std::string_view(TIntStringBuf(-111)) == std::string_view("-111")); + UNIT_ASSERT_VALUES_EQUAL(std::string_view(TIntStringBuf(0)), "0"sv); + UNIT_ASSERT_VALUES_EQUAL(std::string_view(TIntStringBuf(1111)), "1111"sv); + UNIT_ASSERT_VALUES_EQUAL(std::string_view(TIntStringBuf(-1)), "-1"sv); + UNIT_ASSERT_VALUES_EQUAL(std::string_view(TIntStringBuf(-1111)), "-1111"sv); constexpr auto v = TIntStringBuf(-1111); - UNIT_ASSERT_VALUES_EQUAL(TStringBuf(v), TStringBuf(ToString(-1111))); - UNIT_ASSERT_VALUES_EQUAL(TStringBuf(TIntStringBuf(65535)), TStringBuf("65535")); - UNIT_ASSERT_VALUES_EQUAL(TStringBuf(TIntStringBuf(32767)), TStringBuf("32767")); - UNIT_ASSERT_VALUES_EQUAL(TStringBuf(TIntStringBuf(-32768)), TStringBuf("-32768")); + UNIT_ASSERT_VALUES_EQUAL(std::string_view(v), std::string_view(ToString(-1111))); + UNIT_ASSERT_VALUES_EQUAL(std::string_view(TIntStringBuf(65535)), std::string_view("65535")); + UNIT_ASSERT_VALUES_EQUAL(std::string_view(TIntStringBuf(32767)), std::string_view("32767")); + UNIT_ASSERT_VALUES_EQUAL(std::string_view(TIntStringBuf(-32768)), std::string_view("-32768")); - UNIT_ASSERT_VALUES_EQUAL(TStringBuf(TIntStringBuf(127)), TStringBuf("1111111")); - UNIT_ASSERT_VALUES_EQUAL(TStringBuf(TIntStringBuf(-128)), TStringBuf("-10000000")); + UNIT_ASSERT_VALUES_EQUAL(std::string_view(TIntStringBuf(127)), std::string_view("1111111")); + UNIT_ASSERT_VALUES_EQUAL(std::string_view(TIntStringBuf(-128)), std::string_view("-10000000")); } Y_UNIT_TEST(TestTrivial) { - UNIT_ASSERT_VALUES_EQUAL(ToString(ToString(ToString("abc"))), TString("abc")); + UNIT_ASSERT_VALUES_EQUAL(ToString(ToString(ToString("abc"))), std::string("abc")); // May cause compilation error: - // const TString& ref = ToString(TString{"foo"}); + // const std::string& ref = ToString(std::string{"foo"}); - const TString ok = ToString(TString{"foo"}); + const std::string ok = ToString(syd::string{"foo"}); UNIT_ASSERT_VALUES_EQUAL(ok, "foo"); UNIT_ASSERT_VALUES_EQUAL(ToString(ToString(ok)), "foo"); } diff --git a/util/string/escape.cpp b/util/string/escape.cpp index cd09a7dbd02..559f6848edf 100644 --- a/util/string/escape.cpp +++ b/util/string/escape.cpp @@ -149,7 +149,7 @@ namespace { } template -TBasicString& EscapeCImpl(const TChar* str, size_t len, TBasicString& r) { +std::basic_string& EscapeCImpl(const TChar* str, size_t len, std::basic_string& r) { using TEscapeUtil = ::TEscapeUtil; TChar buffer[TEscapeUtil::ESCAPE_C_BUFFER_SIZE]; @@ -174,20 +174,19 @@ TBasicString& EscapeCImpl(const TChar* str, size_t len, TBasicString(const TString::TChar* str, size_t len, TString& r); -template TUtf16String& EscapeCImpl(const TUtf16String::TChar* str, size_t len, TUtf16String& r); +template std::string& EscapeCImpl(const std::string::value_type* str, size_t len, std::string& r); +template std::u16string& EscapeCImpl(const std::u16string::value_type* str, size_t len, std::u16string& r); namespace { template inline void AppendUnicode(TStr& s, wchar32 v) { char buf[10]; size_t sz = 0; - WriteUTF8Char(v, sz, (ui8*)buf); - s.AppendNoAlias(buf, sz); + s.append(buf, sz); } - inline void AppendUnicode(TUtf16String& s, wchar32 v) { + inline void AppendUnicode(std::u16string& s, wchar32 v) { WriteSymbol(v, s); } @@ -240,28 +239,28 @@ static TStr& DoUnescapeC(const TChar* p, size_t sz, TStr& res) { switch (*p) { default: - res.append(*p); + res.push_back(*p); break; case 'a': - res.append('\a'); + res.push_back('\a'); break; case 'b': - res.append('\b'); + res.push_back('\b'); break; case 'f': - res.append('\f'); + res.push_back('\f'); break; case 'n': - res.append('\n'); + res.push_back('\n'); break; case 'r': - res.append('\r'); + res.push_back('\r'); break; case 't': - res.append('\t'); + res.push_back('\t'); break; case 'v': - res.append('\v'); + res.push_back('\v'); break; case 'u': { ui16 cp[2]; @@ -276,7 +275,7 @@ static TStr& DoUnescapeC(const TChar* p, size_t sz, TStr& res) { p += 4; } } else { - res.append(*p); + res.push_back(*p); } break; @@ -284,7 +283,7 @@ static TStr& DoUnescapeC(const TChar* p, size_t sz, TStr& res) { case 'U': if (CountHex<8>(p + 1, pe) != 8) { - res.append(*p); + res.push_back(*p); } else { AppendUnicode(res, IntFromString(p + 1, 8)); p += 8; @@ -292,10 +291,10 @@ static TStr& DoUnescapeC(const TChar* p, size_t sz, TStr& res) { break; case 'x': if (ui32 v = CountHex<2>(p + 1, pe)) { - res.append((TChar)IntFromString(p + 1, v)); + res.push_back((TChar)IntFromString(p + 1, v)); p += v; } else { - res.append(*p); + res.push_back(*p); } break; @@ -304,7 +303,7 @@ static TStr& DoUnescapeC(const TChar* p, size_t sz, TStr& res) { case '2': case '3': { ui32 v = CountOct<3>(p, pe); // v is always positive - res.append((TChar)IntFromString(p, v)); + res.push_back((TChar)IntFromString(p, v)); p += v - 1; } break; case '4': @@ -312,7 +311,7 @@ static TStr& DoUnescapeC(const TChar* p, size_t sz, TStr& res) { case '6': case '7': { ui32 v = CountOct<2>(p, pe); // v is always positive - res.append((TChar)IntFromString(p, v)); + res.push_back((TChar)IntFromString(p, v)); p += v - 1; } break; } @@ -331,24 +330,24 @@ static TStr& DoUnescapeC(const TChar* p, size_t sz, TStr& res) { } template -TBasicString& UnescapeCImpl(const TChar* p, size_t sz, TBasicString& res) { +std::basic_string& UnescapeCImpl(const TChar* p, size_t sz, std::basic_string& res) { return DoUnescapeC(p, sz, res); } template TChar* UnescapeC(const TChar* str, size_t len, TChar* buf) { struct TUnboundedString { - void append(TChar ch) noexcept { + void push_back(TChar ch) noexcept { *P++ = ch; } void append(const TChar* b, const TChar* e) noexcept { while (b != e) { - append(*b++); + push_back(*b++); } } - void AppendNoAlias(const TChar* s, size_t l) noexcept { + void append(const TChar* s, size_t l) noexcept { append(s, s + l); } @@ -358,8 +357,8 @@ TChar* UnescapeC(const TChar* str, size_t len, TChar* buf) { return DoUnescapeC(str, len, bufbuf).P; } -template TString& UnescapeCImpl(const TString::TChar* str, size_t len, TString& r); -template TUtf16String& UnescapeCImpl(const TUtf16String::TChar* str, size_t len, TUtf16String& r); +template std::string& UnescapeCImpl(const std::string::value_type* str, size_t len, std::string& r); +template std::u16string& UnescapeCImpl(const std::u16string::value_type* str, size_t len, std::u16string& r); template char* UnescapeC(const char* str, size_t len, char* buf); @@ -398,36 +397,55 @@ size_t UnescapeCCharLen(const TChar* begin, const TChar* end) { } template size_t UnescapeCCharLen(const char* begin, const char* end); -template size_t UnescapeCCharLen(const TUtf16String::TChar* begin, const TUtf16String::TChar* end); +template size_t UnescapeCCharLen(const std::u16string::value_type* begin, const std::u16string::value_type* end); -TString& EscapeC(const TStringBuf str, TString& s) { +std::string& EscapeC(const std::string_view str, std::string& s) { return EscapeC(str.data(), str.size(), s); } -TUtf16String& EscapeC(const TWtringBuf str, TUtf16String& w) { +std::u16string& EscapeC(const std::u16string_view str, std::u16string& w) { return EscapeC(str.data(), str.size(), w); } -TString EscapeC(const TString& str) { +std::string EscapeC(const std::string& str) { return EscapeC(str.data(), str.size()); } -TUtf16String EscapeC(const TUtf16String& str) { +std::u16string EscapeC(const std::u16string& str) { return EscapeC(str.data(), str.size()); } -TString& UnescapeC(const TStringBuf str, TString& s) { +std::string& UnescapeC(const std::string_view str, std::string& s) { return UnescapeC(str.data(), str.size(), s); } -TUtf16String& UnescapeC(const TWtringBuf str, TUtf16String& w) { +std::u16string& UnescapeC(const std::u16string_view str, std::u16string& w) { return UnescapeC(str.data(), str.size(), w); } -TString UnescapeC(const TStringBuf str) { +std::string UnescapeC(const std::string_view str) { return UnescapeC(str.data(), str.size()); } -TUtf16String UnescapeC(const TWtringBuf str) { +std::u16string UnescapeC(const std::u16string_view str) { return UnescapeC(str.data(), str.size()); } + +namespace NUtils { + +template <> +std::string GetQuoteLiteral() { + return "\""; +} + +template <> +std::u16string GetQuoteLiteral() { + return u"\""; +} + +template <> +std::u32string GetQuoteLiteral() { + return U"\""; +} + +} diff --git a/util/string/escape.h b/util/string/escape.h index b01be65b0ee..d340b4a3086 100644 --- a/util/string/escape.h +++ b/util/string/escape.h @@ -1,66 +1,65 @@ #pragma once -#include -#include +#include template -TBasicString& EscapeCImpl(const TChar* str, size_t len, TBasicString&); +std::basic_string& EscapeCImpl(const TChar* str, size_t len, std::basic_string&); template -TBasicString& UnescapeCImpl(const TChar* str, size_t len, TBasicString&); +std::basic_string& UnescapeCImpl(const TChar* str, size_t len, std::basic_string&); template TChar* UnescapeC(const TChar* str, size_t len, TChar* buf); template -static inline TBasicString& EscapeC(const TChar* str, size_t len, TBasicString& s) { +static inline std::basic_string& EscapeC(const TChar* str, size_t len, std::basic_string& s) { return EscapeCImpl(str, len, s); } template -static inline TBasicString EscapeC(const TChar* str, size_t len) { - TBasicString s; +static inline std::basic_string EscapeC(const TChar* str, size_t len) { + std::basic_string s; return EscapeC(str, len, s); } template -static inline TBasicString EscapeC(const TBasicStringBuf& str) { +static inline std::basic_string EscapeC(const std::basic_string_view& str) { return EscapeC(str.data(), str.size()); } template -static inline TBasicString& UnescapeC(const TChar* str, size_t len, TBasicString& s) { +static inline std::basic_string& UnescapeC(const TChar* str, size_t len, std::basic_string& s) { return UnescapeCImpl(str, len, s); } template -static inline TBasicString UnescapeC(const TChar* str, size_t len) { - TBasicString s; +static inline std::basic_string UnescapeC(const TChar* str, size_t len) { + std::basic_string s; return UnescapeCImpl(str, len, s); } template -static inline TBasicString EscapeC(TChar ch) { +static inline std::basic_string EscapeC(TChar ch) { return EscapeC(&ch, 1); } template -static inline TBasicString EscapeC(const TChar* str) { +static inline std::basic_string EscapeC(const TChar* str) { return EscapeC(str, std::char_traits::length(str)); } -TString& EscapeC(const TStringBuf str, TString& res); -TUtf16String& EscapeC(const TWtringBuf str, TUtf16String& res); +std::string& EscapeC(const std::string_view str, std::string& res); +std::u16string& EscapeC(const std::u16string_view str, std::u16string& res); -// these two need to be methods, because of TBasicString::Quote implementation -TString EscapeC(const TString& str); -TUtf16String EscapeC(const TUtf16String& str); +// these two need to be methods, because of std::basic_string::Quote implementation +std::string EscapeC(const std::string& str); +std::u16string EscapeC(const std::u16string& str); -TString& UnescapeC(const TStringBuf str, TString& res); -TUtf16String& UnescapeC(const TWtringBuf str, TUtf16String& res); +std::string& UnescapeC(const std::string_view str, std::string& res); +std::u16string& UnescapeC(const std::u16string_view str, std::u16string& res); -TString UnescapeC(const TStringBuf str); -TUtf16String UnescapeC(const TWtringBuf wtr); +std::string UnescapeC(const std::string_view str); +std::u16string UnescapeC(const std::u16string_view wtr); /// Returns number of chars in escape sequence. /// - 0, if begin >= end @@ -68,3 +67,20 @@ TUtf16String UnescapeC(const TWtringBuf wtr); /// - at least 2 (including '\'), if [begin, end) starts with an escaped symbol template size_t UnescapeCCharLen(const TChar* begin, const TChar* end); + +namespace NUtils { + +template +std::basic_string GetQuoteLiteral(); + +template +std::basic_string Quote(std::basic_string_view s) { + return GetQuoteLiteral() + EscapeC(s) + GetQuoteLiteral(); +} + +template +std::basic_string Quote(const std::basic_string& s) { + return Quote(std::basic_string_view(s)); +} + +} diff --git a/util/string/escape_ut.cpp b/util/string/escape_ut.cpp index cd38ecffd34..c24c4133436 100644 --- a/util/string/escape_ut.cpp +++ b/util/string/escape_ut.cpp @@ -2,17 +2,16 @@ #include -#include #include using namespace std::string_view_literals; namespace { struct TExample { - TString Expected; - TString Source; + std::string Expected; + std::string Source; - TExample(const TStringBuf expected, const TStringBuf source) + TExample(const std::string_view expected, const std::string_view source) : Expected{expected} , Source{source} { @@ -49,42 +48,42 @@ static const TExample CommonTestData[] = { Y_UNIT_TEST_SUITE(TEscapeCTest) { Y_UNIT_TEST(TestStrokaEscapeC) { for (const auto& e : CommonTestData) { - TString expected(e.Expected); - TString source(e.Source); - TString actual(EscapeC(e.Source)); - TString actual2(UnescapeC(e.Expected)); + std::string expected(e.Expected); + std::string source(e.Source); + std::string actual(EscapeC(e.Source)); + std::string actual2(UnescapeC(e.Expected)); UNIT_ASSERT_VALUES_EQUAL(e.Expected, actual); UNIT_ASSERT_VALUES_EQUAL(e.Source, actual2); } - UNIT_ASSERT_VALUES_EQUAL("http://ya.ru/\\x17\\n\\xAB", EscapeC(TString("http://ya.ru/\x17\n\xab"))); - UNIT_ASSERT_VALUES_EQUAL("http://ya.ru/\x17\n\xab", UnescapeC(TString("http://ya.ru/\\x17\\n\\xAB"))); + UNIT_ASSERT_VALUES_EQUAL("http://ya.ru/\\x17\\n\\xAB", EscapeC(std::string("http://ya.ru/\x17\n\xab"))); + UNIT_ASSERT_VALUES_EQUAL("http://ya.ru/\x17\n\xab", UnescapeC(std::string("http://ya.ru/\\x17\\n\\xAB"))); UNIT_ASSERT_VALUES_EQUAL("h", EscapeC('h')); - UNIT_ASSERT_VALUES_EQUAL("h", UnescapeC(TString("h"))); + UNIT_ASSERT_VALUES_EQUAL("h", UnescapeC(std::string("h"))); UNIT_ASSERT_VALUES_EQUAL("\\xFF", EscapeC('\xFF')); - UNIT_ASSERT_VALUES_EQUAL("\xFF", UnescapeC(TString("\\xFF"))); + UNIT_ASSERT_VALUES_EQUAL("\xFF", UnescapeC(std::string("\\xFF"))); - UNIT_ASSERT_VALUES_EQUAL("\\377f", EscapeC(TString("\xff" + UNIT_ASSERT_VALUES_EQUAL("\\377f", EscapeC(std::string("\xff" "f"))); UNIT_ASSERT_VALUES_EQUAL("\xff" "f", - UnescapeC(TString("\\377f"))); - UNIT_ASSERT_VALUES_EQUAL("\\xFFg", EscapeC(TString("\xff" + UnescapeC(std::string("\\377f"))); + UNIT_ASSERT_VALUES_EQUAL("\\xFFg", EscapeC(std::string("\xff" "g"))); UNIT_ASSERT_VALUES_EQUAL("\xff" "g", - UnescapeC(TString("\\xFFg"))); - UNIT_ASSERT_VALUES_EQUAL("\xEA\x9A\x96", UnescapeC(TString("\\uA696"))); - UNIT_ASSERT_VALUES_EQUAL("Странный компроматтест", UnescapeC(TString("\\u0421\\u0442\\u0440\\u0430\\u043d\\u043d\\u044b\\u0439 \\u043a\\u043e\\u043c\\u043f\\u0440\\u043e\\u043c\\u0430\\u0442тест"))); + UnescapeC(std::string("\\xFFg"))); + UNIT_ASSERT_VALUES_EQUAL("\xEA\x9A\x96", UnescapeC(std::string("\\uA696"))); + UNIT_ASSERT_VALUES_EQUAL("Странный компроматтест", UnescapeC(std::string("\\u0421\\u0442\\u0440\\u0430\\u043d\\u043d\\u044b\\u0439 \\u043a\\u043e\\u043c\\u043f\\u0440\\u043e\\u043c\\u0430\\u0442тест"))); } Y_UNIT_TEST(TestWtrokaEscapeC) { for (const auto& e : CommonTestData) { - TUtf16String expected(UTF8ToWide(e.Expected)); - TUtf16String source(UTF8ToWide(e.Source)); - TUtf16String actual(EscapeC(source)); - TUtf16String actual2(UnescapeC(expected)); + std::u16string expected(UTF8ToWide(e.Expected)); + std::u16string source(UTF8ToWide(e.Source)); + std::u16string actual(EscapeC(source)); + std::u16string actual2(UnescapeC(expected)); UNIT_ASSERT_VALUES_EQUAL(expected, actual); UNIT_ASSERT_VALUES_EQUAL(source, actual2); @@ -96,12 +95,12 @@ Y_UNIT_TEST_SUITE(TEscapeCTest) { } Y_UNIT_TEST(TestEscapeTrigraphs) { - UNIT_ASSERT_VALUES_EQUAL("?", EscapeC(TString("?"))); - UNIT_ASSERT_VALUES_EQUAL("\\x3F?", EscapeC(TString("??"))); - UNIT_ASSERT_VALUES_EQUAL("\\x3F\\x3F?", EscapeC(TString("???"))); + UNIT_ASSERT_VALUES_EQUAL("?", EscapeC(std::string("?"))); + UNIT_ASSERT_VALUES_EQUAL("\\x3F?", EscapeC(std::string("??"))); + UNIT_ASSERT_VALUES_EQUAL("\\x3F\\x3F?", EscapeC(std::string("???"))); // ok but may cause warning about trigraphs - // UNIT_ASSERT_VALUES_EQUAL("[x]?z", EscapeC(TString("??(x??)?z"))); - UNIT_ASSERT_VALUES_EQUAL("\\x3F?x\\x3F\\x3F?z", EscapeC(TString("??x???z"))); + // UNIT_ASSERT_VALUES_EQUAL("[x]?z", EscapeC(std::string("??(x??)?z"))); + UNIT_ASSERT_VALUES_EQUAL("\\x3F?x\\x3F\\x3F?z", EscapeC(std::string("??x???z"))); } Y_UNIT_TEST(TestUnescapeCCharLen) { @@ -137,7 +136,7 @@ Y_UNIT_TEST_SUITE(TEscapeCTest) { for (const auto& x : CommonTestData) { char* end = UnescapeC(x.Expected.data(), x.Expected.size(), buf); - UNIT_ASSERT_VALUES_EQUAL(x.Source, TStringBuf(buf, end)); + UNIT_ASSERT_VALUES_EQUAL(x.Source, std::string_view(buf, end)); } } diff --git a/util/string/hex.cpp b/util/string/hex.cpp index 667397987fc..16cdd7765c6 100644 --- a/util/string/hex.cpp +++ b/util/string/hex.cpp @@ -32,7 +32,7 @@ char* HexEncode(const void* in, size_t len, char* out) { void* HexDecode(const void* in, size_t len, void* ptr) { const char* b = (const char*)in; const char* e = b + len; - Y_ENSURE(!(len & 1), TStringBuf("Odd buffer length passed to HexDecode")); + Y_ENSURE(!(len & 1), std::string_view("Odd buffer length passed to HexDecode")); char* out = (char*)ptr; @@ -44,19 +44,19 @@ void* HexDecode(const void* in, size_t len, void* ptr) { return out; } -TString HexEncode(const void* in, size_t len) { - TString ret; +std::string HexEncode(const void* in, size_t len) { + std::string ret; - ret.ReserveAndResize(len << 1); + ret.resize(len << 1); HexEncode(in, len, ret.begin()); return ret; } -TString HexDecode(const void* in, size_t len) { - TString ret; +std::string HexDecode(const void* in, size_t len) { + std::string ret; - ret.ReserveAndResize(len >> 1); + ret.resize(len >> 1); HexDecode(in, len, ret.begin()); return ret; diff --git a/util/string/hex.h b/util/string/hex.h index af3d2d528fa..d882872daeb 100644 --- a/util/string/hex.h +++ b/util/string/hex.h @@ -1,6 +1,5 @@ #pragma once -#include #include #include @@ -28,9 +27,9 @@ inline static int String2Byte(const char* s) { char* HexEncode(const void* in, size_t len, char* out); -TString HexEncode(const void* in, size_t len); +std::string HexEncode(const void* in, size_t len); -inline TString HexEncode(const TStringBuf h) { +inline std::string HexEncode(const std::string_view h) { return HexEncode(h.data(), h.size()); } @@ -51,9 +50,9 @@ void* HexDecode(const void* in, size_t len, void* ptr); * * @example HexDecode("beef", 4) => {190, 239} */ -TString HexDecode(const void* in, size_t len); +std::string HexDecode(const void* in, size_t len); //! Convert an ASCII hex-string (case-insensitive) to the binary form. Note that h.Size() must be even (+h % 2 == 0). -inline TString HexDecode(const TStringBuf h) { +inline std::string HexDecode(const std::string_view h) { return HexDecode(h.data(), h.size()); } diff --git a/util/string/join.h b/util/string/join.h index e3c08ef584a..eb834e75132 100644 --- a/util/string/join.h +++ b/util/string/join.h @@ -1,44 +1,43 @@ #pragma once -#include #include #include #include "cast.h" /* - * Default implementation of AppendToString uses a temporary TString object which is inefficient. You can overload it + * Default implementation of AppendToString uses a temporary std::string object which is inefficient. You can overload it * for your type to speed up string joins. If you already have an Out() or operator<<() implementation you can simply * do the following: * - * inline void AppendToString(TString& dst, const TMyType& t) { + * inline void AppendToString(std::string& dst, const TMyType& t) { * TStringOutput o(dst); * o << t; * } * * Unfortunately we can't do this by default because for some types ToString() is defined while Out() is not. - * For standard types (strings of all kinds and arithmetic types) we don't use a temporary TString in AppendToString(). + * For standard types (strings of all kinds and arithmetic types) we don't use a temporary std::string in AppendToString(). */ template inline std::enable_if_t>::value, void> -AppendToString(TBasicString& dst, const T& t) { - dst.AppendNoAlias(ToString(t)); +AppendToString(std::basic_string& dst, const T& t) { + dst.append(ToString(t)); } template inline std::enable_if_t>::value, void> -AppendToString(TBasicString& dst, const T& t) { +AppendToString(std::basic_string& dst, const T& t) { char buf[512]; dst.append(buf, ToString>(t, buf, sizeof(buf))); } template -inline void AppendToString(TBasicString& dst, const TCharType* t) { +inline void AppendToString(std::basic_string& dst, const TCharType* t) { dst.append(t); } template -inline void AppendToString(TBasicString& dst, TBasicStringBuf t) { +inline void AppendToString(std::basic_string& dst, std::basic_string_view t) { dst.append(t); } @@ -51,12 +50,12 @@ namespace NPrivate { } template <> - inline size_t GetLength(const TString& s) { + inline size_t GetLength(const std::string& s) { return s.length(); } template <> - inline size_t GetLength(const TStringBuf& s) { + inline size_t GetLength(const std::string_view& s) { return s.length(); } @@ -65,29 +64,29 @@ namespace NPrivate { return (s ? std::char_traits::length(s) : 0); } - inline size_t GetAppendLength(const TStringBuf /*delim*/) { + inline size_t GetAppendLength(const std::string_view /*delim*/) { return 0; } template - size_t GetAppendLength(const TStringBuf delim, const TFirst& f, const TRest&... r) { + size_t GetAppendLength(const std::string_view delim, const TFirst& f, const TRest&... r) { return delim.length() + ::NPrivate::GetLength(f) + ::NPrivate::GetAppendLength(delim, r...); } } template -inline void AppendJoinNoReserve(TBasicString&, TBasicStringBuf) { +inline void AppendJoinNoReserve(std::basic_string&, std::basic_string_view) { } template -inline void AppendJoinNoReserve(TBasicString& dst, TBasicStringBuf delim, const TFirst& f, const TRest&... r) { +inline void AppendJoinNoReserve(std::basic_string& dst, std::basic_string_view delim, const TFirst& f, const TRest&... r) { AppendToString(dst, delim); AppendToString(dst, f); AppendJoinNoReserve(dst, delim, r...); } template -inline void AppendJoin(TString& dst, const TStringBuf delim, const TValues&... values) { +inline void AppendJoin(std::string& dst, const std::string_view delim, const TValues&... values) { const size_t appendLength = ::NPrivate::GetAppendLength(delim, values...); if (appendLength > 0) { dst.reserve(dst.length() + appendLength); @@ -96,8 +95,8 @@ inline void AppendJoin(TString& dst, const TStringBuf delim, const TValues&... v } template -inline TString Join(const TStringBuf delim, const TFirst& f, const TRest&... r) { - TString ret = ToString(f); +inline std::string Join(const std::string_view delim, const TFirst& f, const TRest&... r) { + std::string ret = ToString(f); AppendJoin(ret, delim, r...); return ret; } @@ -106,14 +105,14 @@ inline TString Join(const TStringBuf delim, const TFirst& f, const TRest&... r) // but any char value @v will be printed as corresponding numeric code. // For example, Join('a', 'a', 'a') will print "97a97" (see unit-test). template -inline TString Join(char cdelim, const TValues&... v) { - return Join(TStringBuf(&cdelim, 1), v...); +inline std::string Join(char cdelim, const TValues&... v) { + return Join(std::string_view(&cdelim, 1), v...); } namespace NPrivate { template - inline TBasicString JoinRange(TBasicStringBuf delim, const TIter beg, const TIter end) { - TBasicString out; + inline std::basic_string JoinRange(std::basic_string_view delim, const TIter beg, const TIter end) { + std::basic_string out; if (beg != end) { size_t total = ::NPrivate::GetLength(*beg); for (TIter pos = beg; ++pos != end;) { @@ -135,54 +134,54 @@ namespace NPrivate { } // namespace NPrivate template -TString JoinRange(std::string_view delim, const TIter beg, const TIter end) { +std::string JoinRange(std::string_view delim, const TIter beg, const TIter end) { return ::NPrivate::JoinRange(delim, beg, end); } template -TString JoinRange(char delim, const TIter beg, const TIter end) { - TStringBuf delimBuf(&delim, 1); +std::string JoinRange(char delim, const TIter beg, const TIter end) { + std::string_view delimBuf(&delim, 1); return ::NPrivate::JoinRange(delimBuf, beg, end); } template -TUtf16String JoinRange(std::u16string_view delim, const TIter beg, const TIter end) { +std::u16string JoinRange(std::u16string_view delim, const TIter beg, const TIter end) { return ::NPrivate::JoinRange(delim, beg, end); } template -TUtf16String JoinRange(wchar16 delim, const TIter beg, const TIter end) { - TWtringBuf delimBuf(&delim, 1); +std::u16string JoinRange(wchar16 delim, const TIter beg, const TIter end) { + std::u16string_view delimBuf(&delim, 1); return ::NPrivate::JoinRange(delimBuf, beg, end); } template -TUtf32String JoinRange(std::u32string_view delim, const TIter beg, const TIter end) { +std::u32string JoinRange(std::u32string_view delim, const TIter beg, const TIter end) { return ::NPrivate::JoinRange(delim, beg, end); } template -TUtf32String JoinRange(wchar32 delim, const TIter beg, const TIter end) { - TUtf32StringBuf delimBuf(&delim, 1); +std::u32string JoinRange(wchar32 delim, const TIter beg, const TIter end) { + std::u32string_view delimBuf(&delim, 1); return ::NPrivate::JoinRange(delimBuf, beg, end); } template -inline TBasicString JoinSeq(std::basic_string_view delim, const TContainer& data) { +inline std::basic_string JoinSeq(std::basic_string_view delim, const TContainer& data) { using std::begin; using std::end; return JoinRange(delim, begin(data), end(data)); } template -inline TBasicString JoinSeq(const TCharType* delim, const TContainer& data) { - TBasicStringBuf delimBuf = delim; +inline std::basic_string JoinSeq(const TCharType* delim, const TContainer& data) { + std::basic_string_view delimBuf = delim; return JoinSeq(delimBuf, data); } template -inline TBasicString JoinSeq(const TBasicString& delim, const TContainer& data) { - TBasicStringBuf delimBuf = delim; +inline std::basic_string JoinSeq(const std::basic_string& delim, const TContainer& data) { + std::basic_string_view delimBuf = delim; return JoinSeq(delimBuf, data); } @@ -191,14 +190,14 @@ inline std::enable_if_t< std::is_same_v || std::is_same_v || std::is_same_v, - TBasicString> + std::basic_string> JoinSeq(TCharType delim, const TContainer& data) { - TBasicStringBuf delimBuf(&delim, 1); + std::basic_string_view delimBuf(&delim, 1); return JoinSeq(delimBuf, data); } /** \brief Functor for streaming iterative objects from TIterB e to TIterE b, separated with delim. - * Difference from JoinSeq, JoinRange, Join is the lack of TString object - all depends on operator<< for the type and + * Difference from JoinSeq, JoinRange, Join is the lack of std::string object - all depends on operator<< for the type and * realization of IOutputStream */ template @@ -213,7 +212,7 @@ struct TRangeJoiner { return stream; } - constexpr TRangeJoiner(TStringBuf delim, TIterB&& b, TIterE&& e) + constexpr TRangeJoiner(std::string_view delim, TIterB&& b, TIterE&& e) : delim(delim) , b(std::forward(b)) , e(std::forward(e)) @@ -221,45 +220,45 @@ struct TRangeJoiner { } private: - const TStringBuf delim; + const std::string_view delim; const TIterB b; const TIterE e; }; template -constexpr auto MakeRangeJoiner(TStringBuf delim, TIterB&& b, TIterE&& e) { +constexpr auto MakeRangeJoiner(std::string_view delim, TIterB&& b, TIterE&& e) { return TRangeJoiner(delim, std::forward(b), std::forward(e)); } template -constexpr auto MakeRangeJoiner(TStringBuf delim, const TContainer& data) { +constexpr auto MakeRangeJoiner(std::string_view delim, const TContainer& data) { return MakeRangeJoiner(delim, std::cbegin(data), std::cend(data)); } template -constexpr auto MakeRangeJoiner(TStringBuf delim, const std::initializer_list& data) { +constexpr auto MakeRangeJoiner(std::string_view delim, const std::initializer_list& data) { return MakeRangeJoiner(delim, std::cbegin(data), std::cend(data)); } -/* We force (std::initializer_list) input type for (TString) and (const char*) types because: - * # When (std::initializer_list) is used, TString objects are copied into the initializer_list object. - * Storing TStringBufs instead is faster, even with COW-enabled strings. - * # For (const char*) we calculate length only once and store it in TStringBuf. Otherwise strlen scan would be executed +/* We force (std::initializer_list) input type for (std::string) and (const char*) types because: + * # When (std::initializer_list) is used, std::string objects are copied into the initializer_list object. + * Storing std::string_views instead is faster, even with COW-enabled strings. + * # For (const char*) we calculate length only once and store it in std::string_view. Otherwise strlen scan would be executed * in both GetAppendLength and AppendToString. For string literals constant lengths get propagated in compile-time. * * This way JoinSeq(",", { s1, s2 }) always does the right thing whatever types s1 and s2 have. * - * If someone needs to join std::initializer_list -- it still works because of the TContainer template above. + * If someone needs to join std::initializer_list -- it still works because of the TContainer template above. */ template inline std::enable_if_t< - !std::is_same, TString>::value && !std::is_same, const char*>::value, - TString> -JoinSeq(const TStringBuf delim, const std::initializer_list& data) { + !std::is_same, std::string>::value && !std::is_same, const char*>::value, + std::string> +JoinSeq(const std::string_view delim, const std::initializer_list& data) { return JoinRange(delim, data.begin(), data.end()); } -inline TString JoinSeq(const TStringBuf delim, const std::initializer_list& data) { +inline std::string JoinSeq(const std::string_view delim, const std::initializer_list& data) { return JoinRange(delim, data.begin(), data.end()); } diff --git a/util/string/join_ut.cpp b/util/string/join_ut.cpp index 22a40bb1911..c4818da5c16 100644 --- a/util/string/join_ut.cpp +++ b/util/string/join_ut.cpp @@ -9,7 +9,7 @@ struct TCustomData { std::vector Ints; }; -TString ToString(const TCustomData& d) { +std::string ToString(const TCustomData& d) { return JoinSeq("__", d.Ints); } @@ -17,9 +17,9 @@ Y_UNIT_TEST_SUITE(JoinStringTest) { Y_UNIT_TEST(ScalarItems) { UNIT_ASSERT_EQUAL(Join(',', 10, 11.1, "foobar"), "10,11.1,foobar"); UNIT_ASSERT_EQUAL(Join(", ", 10, 11.1, "foobar"), "10, 11.1, foobar"); - UNIT_ASSERT_EQUAL(Join(", ", 10, 11.1, TString("foobar")), "10, 11.1, foobar"); + UNIT_ASSERT_EQUAL(Join(", ", 10, 11.1, std::string("foobar")), "10, 11.1, foobar"); - UNIT_ASSERT_EQUAL(Join('#', 0, "a", "foobar", -1.4, TStringBuf("aaa")), "0#a#foobar#-1.4#aaa"); + UNIT_ASSERT_EQUAL(Join('#', 0, "a", "foobar", -1.4, std::string_view("aaa")), "0#a#foobar#-1.4#aaa"); UNIT_ASSERT_EQUAL(Join("", "", ""), ""); UNIT_ASSERT_EQUAL(Join("", "a", "b", "c"), "abc"); UNIT_ASSERT_EQUAL(Join("", "a", "b", "", "c"), "abc"); @@ -43,8 +43,8 @@ Y_UNIT_TEST_SUITE(JoinStringTest) { static const char* const result = "1 22 333"; static const char* const v[] = {"1", "22", "333"}; std::vector vchar(v, v + sizeof(v) / sizeof(v[0])); - std::vector vbuf(v, v + sizeof(v) / sizeof(v[0])); - std::vector vstring(v, v + sizeof(v) / sizeof(v[0])); + std::vector vbuf(v, v + sizeof(v) / sizeof(v[0])); + std::vector vstring(v, v + sizeof(v) / sizeof(v[0])); // ranges UNIT_ASSERT_EQUAL(JoinRange(" ", v, v + 3), result); @@ -121,8 +121,8 @@ Y_UNIT_TEST_SUITE(JoinStringTest) { // initializer lists with explicit types UNIT_ASSERT_EQUAL(JoinSeq(" ", std::initializer_list{v[0], v[1], v[2]}), result); UNIT_ASSERT_EQUAL(JoinSeq(" ", std::initializer_list{vchar[0], vchar[1], vchar[2]}), result); - UNIT_ASSERT_EQUAL(JoinSeq(" ", std::initializer_list{vbuf[0], vbuf[1], vbuf[2]}), result); - UNIT_ASSERT_EQUAL(JoinSeq(" ", std::initializer_list{vstring[0], vstring[1], vstring[2]}), result); + UNIT_ASSERT_EQUAL(JoinSeq(" ", std::initializer_list{vbuf[0], vbuf[1], vbuf[2]}), result); + UNIT_ASSERT_EQUAL(JoinSeq(" ", std::initializer_list{vstring[0], vstring[1], vstring[2]}), result); { TStringStream stream; stream << MakeRangeJoiner(" ", std::initializer_list{v[0], v[1], v[2]}); @@ -135,12 +135,12 @@ Y_UNIT_TEST_SUITE(JoinStringTest) { } { TStringStream stream; - stream << MakeRangeJoiner(" ", std::initializer_list{vbuf[0], vbuf[1], vbuf[2]}); + stream << MakeRangeJoiner(" ", std::initializer_list{vbuf[0], vbuf[1], vbuf[2]}); UNIT_ASSERT_EQUAL(stream.Str(), result); } { TStringStream stream; - stream << MakeRangeJoiner(" ", std::initializer_list{vstring[0], vstring[1], vstring[2]}); + stream << MakeRangeJoiner(" ", std::initializer_list{vstring[0], vstring[1], vstring[2]}); UNIT_ASSERT_EQUAL(stream.Str(), result); } diff --git a/util/string/printf.cpp b/util/string/printf.cpp index 5b7c34d4e14..32698e07bbf 100644 --- a/util/string/printf.cpp +++ b/util/string/printf.cpp @@ -3,13 +3,13 @@ #include #include -int vsprintf(TString& s, const char* c, va_list params) { - TStringOutput so(s.remove()); +int vsprintf(std::string& s, const char* c, va_list params) { + TStringOutput so(s.erase()); return Printf(so, c, params); } -int sprintf(TString& s, const char* c, ...) { +int sprintf(std::string& s, const char* c, ...) { va_list params; va_start(params, c); const int k = vsprintf(s, c, params); @@ -17,8 +17,8 @@ int sprintf(TString& s, const char* c, ...) { return k; } -TString Sprintf(const char* c, ...) { - TString s; +std::string Sprintf(const char* c, ...) { + std::string s; va_list params; va_start(params, c); vsprintf(s, c, params); @@ -26,7 +26,7 @@ TString Sprintf(const char* c, ...) { return s; } -int fcat(TString& s, const char* c, ...) { +int fcat(std::string& s, const char* c, ...) { TStringOutput so(s); va_list params; diff --git a/util/string/printf.h b/util/string/printf.h index 925c6edaff1..47e9e08a0e7 100644 --- a/util/string/printf.h +++ b/util/string/printf.h @@ -6,8 +6,8 @@ #include /// formatted print. return printed length: -int Y_PRINTF_FORMAT(2, 0) vsprintf(TString& s, const char* c, va_list params); +int Y_PRINTF_FORMAT(2, 0) vsprintf(std::string& s, const char* c, va_list params); /// formatted print. return printed length: -int Y_PRINTF_FORMAT(2, 3) sprintf(TString& s, const char* c, ...); -TString Y_PRINTF_FORMAT(1, 2) Sprintf(const char* c, ...); -int Y_PRINTF_FORMAT(2, 3) fcat(TString& s, const char* c, ...); +int Y_PRINTF_FORMAT(2, 3) sprintf(std::string& s, const char* c, ...); +std::string Y_PRINTF_FORMAT(1, 2) Sprintf(const char* c, ...); +int Y_PRINTF_FORMAT(2, 3) fcat(std::string& s, const char* c, ...); diff --git a/util/string/printf_ut.cpp b/util/string/printf_ut.cpp index 2b2f980b70e..a1848b93dc3 100644 --- a/util/string/printf_ut.cpp +++ b/util/string/printf_ut.cpp @@ -2,21 +2,21 @@ #include -Y_UNIT_TEST_SUITE(TStringPrintf) { +Y_UNIT_TEST_SUITE(StringPrintf) { Y_UNIT_TEST(TestSprintf) { - TString s; + std::string s; int len = sprintf(s, "Hello %s", "world"); - UNIT_ASSERT_EQUAL(s, TString("Hello world")); + UNIT_ASSERT_EQUAL(s, std::string("Hello world")); UNIT_ASSERT_EQUAL(len, 11); } Y_UNIT_TEST(TestFcat) { - TString s; + std::string s; int len = sprintf(s, "Hello %s", "world"); - UNIT_ASSERT_EQUAL(s, TString("Hello world")); + UNIT_ASSERT_EQUAL(s, std::string("Hello world")); UNIT_ASSERT_EQUAL(len, 11); len = fcat(s, " qwqw%s", "as"); - UNIT_ASSERT_EQUAL(s, TString("Hello world qwqwas")); + UNIT_ASSERT_EQUAL(s, std::string("Hello world qwqwas")); UNIT_ASSERT_EQUAL(len, 7); } diff --git a/util/string/reverse.cpp b/util/string/reverse.cpp index 3a5954f2f3d..5e537dbcdec 100644 --- a/util/string/reverse.cpp +++ b/util/string/reverse.cpp @@ -1,17 +1,16 @@ #include "reverse.h" -#include -#include #include #include +#include -void ReverseInPlace(TString& string) { +void ReverseInPlace(std::string& string) { auto* begin = string.begin(); std::reverse(begin, begin + string.size()); } -void ReverseInPlace(TUtf16String& string) { +void ReverseInPlace(std::u16string& string) { auto* begin = string.begin(); const auto len = string.size(); auto* end = begin + string.size(); @@ -27,7 +26,7 @@ void ReverseInPlace(TUtf16String& string) { std::copy(buffer.begin(), buffer.end(), begin); } -void ReverseInPlace(TUtf32String& string) { +void ReverseInPlace(std::u32string& string) { auto* begin = string.begin(); std::reverse(begin, begin + string.size()); } diff --git a/util/string/reverse.h b/util/string/reverse.h index 3a70eef061d..cddfc54f089 100644 --- a/util/string/reverse.h +++ b/util/string/reverse.h @@ -2,7 +2,9 @@ #include -void ReverseInPlace(TString& string); +#include + +void ReverseInPlace(std::string& string); /** NB. UTF-16 is variable-length encoding because of the surrogate pairs. * This function takes this into account and treats a surrogate pair as a single symbol. @@ -11,6 +13,6 @@ void ReverseInPlace(TString& string); * will become * E [C D] B A */ -void ReverseInPlace(TUtf16String& string); +void ReverseInPlace(std::u16string& string); -void ReverseInPlace(TUtf32String& string); +void ReverseInPlace(std::u32string& string); diff --git a/util/string/split.cpp b/util/string/split.cpp index 72197d6925b..12f0af0970d 100644 --- a/util/string/split.cpp +++ b/util/string/split.cpp @@ -15,10 +15,10 @@ inline size_t Split(const char* ptr, const char* delim, std::vector& val return values.size(); } -size_t Split(const char* ptr, const char* delim, std::vector& values) { - return Split(ptr, delim, values); +size_t Split(const char* ptr, const char* delim, std::vector& values) { + return Split(ptr, delim, values); } -size_t Split(const TString& in, const TString& delim, std::vector& res) { +size_t Split(const std::string& in, const std::string& delim, std::vector& res) { return Split(in.data(), delim.data(), res); } diff --git a/util/string/split.h b/util/string/split.h index 228ce09d20e..5f322a6dee4 100644 --- a/util/string/split.h +++ b/util/string/split.h @@ -3,15 +3,14 @@ #include "strspn.h" #include "cast.h" +#include + #include #include #include #include #include -#include -#include #include -#include #include #include #include @@ -307,7 +306,7 @@ struct TContainerConvertingConsumer { template inline bool Consume(I* b, I* d, I* /*e*/) { - TConsumerBackInserter::DoInsert(C, FromString(TStringBuf(b, d))); + TConsumerBackInserter::DoInsert(C, FromString(std::string_view(b, d))); return true; } @@ -411,30 +410,30 @@ static inline void Split(char* buf, char ch, T* res) { /// Old good slow split function. /// Field delimter is any number of symbols specified in delim (no empty strings in res vector) /// @return number of elements created -size_t Split(const char* in, const char* delim, std::vector& res); -size_t Split(const TString& in, const TString& delim, std::vector& res); +size_t Split(const char* in, const char* delim, std::vector& res); +size_t Split(const std::string& in, const std::string& delim, std::vector& res); -/// Old split reimplemented for TStringBuf using the new code +/// Old split reimplemented for std::string_view using the new code /// Note that delim can be constructed from char* automatically (it is not cheap though) -inline size_t Split(const TStringBuf s, const TSetDelimiter& delim, std::vector& res) { +inline size_t Split(const std::string_view s, const TSetDelimiter& delim, std::vector& res) { res.clear(); - TContainerConsumer> res1(&res); - TSkipEmptyTokens>> consumer(&res1); + TContainerConsumer> res1(&res); + TSkipEmptyTokens>> consumer(&res1); SplitString(s.data(), s.data() + s.size(), delim, consumer); return res.size(); } template -void GetNext(TStringBuf& s, D delim, P& param) { - TStringBuf next = s.NextTok(delim); - Y_ENSURE(next.IsInited(), TStringBuf("Split: number of fields less than number of Split output arguments")); +void GetNext(std::string_view& s, D delim, P& param) { + std::string_view next = NUtils::NextTok(s, delim); + Y_ENSURE(next.data() != nullptr, std::string_view("Split: number of fields less than number of Split output arguments")); param = FromString

(next); } template -void GetNext(TStringBuf& s, D delim, std::optional

& param) { - TStringBuf next = s.NextTok(delim); - if (next.IsInited()) { +void GetNext(std::string_view& s, D delim, std::optional

& param) { + std::string_view next = NUtils::NextTok(s, delim); + if (next.data() != nullptr) { param = FromString

(next); } else { param.reset(); @@ -442,16 +441,16 @@ void GetNext(TStringBuf& s, D delim, std::optional

& param) { } // example: -// Split(TStringBuf("Sherlock,2014,36.6"), ',', name, year, temperature); +// Split(std::string_view("Sherlock,2014,36.6"), ',', name, year, temperature); template -void Split(TStringBuf s, D delim, P1& p1, P2& p2) { +void Split(std::string_view s, D delim, P1& p1, P2& p2) { GetNext(s, delim, p1); GetNext(s, delim, p2); - Y_ENSURE(!s.IsInited(), TStringBuf("Split: number of fields more than number of Split output arguments")); + Y_ENSURE(s.data() == nullptr, std::string_view("Split: number of fields more than number of Split output arguments")); } template -void Split(TStringBuf s, D delim, P1& p1, P2& p2, Other&... other) { +void Split(std::string_view s, D delim, P1& p1, P2& p2, Other&... other) { GetNext(s, delim, p1); Split(s, delim, p2, other...); } @@ -464,18 +463,18 @@ void Split(TStringBuf s, D delim, P1& p1, P2& p2, Other&... other) { * * Some examples: * \code - * std::vector values = StringSplitter("1\t2\t3").Split('\t'); + * std::vector values = StringSplitter("1\t2\t3").Split('\t'); * - * for(TStringBuf part: StringSplitter("1::2::::3").SplitByString("::").SkipEmpty()) { + * for(std::string_view part: StringSplitter("1::2::::3").SplitByString("::").SkipEmpty()) { * Cerr << part; * } * - * std::vector firstTwoValues = StringSplitter("1\t2\t3").Split('\t').Take(2); + * std::vector firstTwoValues = StringSplitter("1\t2\t3").Split('\t').Take(2); * \endcode * * Use `Collect` or `AddTo` to store split results into an existing container: * \code - * std::vector values = {"0"}; + * std::vector values = {"0"}; * StringSplitter("1\t2\t3").Split('\t').AddTo(&values); * \endcode * Note that `Collect` clears target container, while `AddTo` just inserts values. @@ -603,7 +602,7 @@ namespace NStringSplitPrivate { struct TStringBufOfImpl { using type = std::conditional_t< THasData::value, - TBasicStringBuf, + std::basic_string_view, TIteratorRange>; }; @@ -1047,17 +1046,17 @@ auto StringSplitter(Iterator begin, Iterator end) { template auto StringSplitter(const Char* begin, const Char* end) { - return ::NStringSplitPrivate::MakeStringSplitter(TBasicStringBuf(begin, end)); + return ::NStringSplitPrivate::MakeStringSplitter(std::basic_string_view(begin, end)); } template auto StringSplitter(const Char* begin, size_t len) { - return ::NStringSplitPrivate::MakeStringSplitter(TBasicStringBuf(begin, len)); + return ::NStringSplitPrivate::MakeStringSplitter(std::basic_string_view(begin, len)); } template auto StringSplitter(const Char* str) { - return ::NStringSplitPrivate::MakeStringSplitter(TBasicStringBuf(str)); + return ::NStringSplitPrivate::MakeStringSplitter(std::basic_string_view(str)); } template >::value, int> = 0> diff --git a/util/string/split_ut.cpp b/util/string/split_ut.cpp index 96a216fa11d..05cf1d3160b 100644 --- a/util/string/split_ut.cpp +++ b/util/string/split_ut.cpp @@ -94,35 +94,35 @@ void TestConsumerOnRange(TResult& good, I* b, I* e, I* d) { UNIT_ASSERT_EQUAL(good, test); } -using TStrokaConsumer = TContainerConsumer>; +using TStrokaConsumer = TContainerConsumer>; -void TestLimitingConsumerOnString(std::vector& good, const char* str, const char* d, size_t n, const char* last) { - std::vector test; +void TestLimitingConsumerOnString(std::vector& good, const char* str, const char* d, size_t n, const char* last) { + std::vector test; TStrokaConsumer consumer(&test); TLimitingConsumer limits(n, &consumer); TCharDelimiter delim(*d); SplitString(str, delim, limits); Cmp(good, test); UNIT_ASSERT_EQUAL(good, test); - UNIT_ASSERT_EQUAL(TString(limits.Last), TString(last)); // Quite unobvious behaviour. Why the last token is not added to slave consumer? + UNIT_ASSERT_EQUAL(std::string(limits.Last), std::string(last)); // Quite unobvious behaviour. Why the last token is not added to slave consumer? } -void TestLimitingConsumerOnRange(std::vector& good, const char* b, const char* e, const char* d, size_t n, const char* last) { - std::vector test; +void TestLimitingConsumerOnRange(std::vector& good, const char* b, const char* e, const char* d, size_t n, const char* last) { + std::vector test; TStrokaConsumer consumer(&test); TLimitingConsumer limits(n, &consumer); TCharDelimiter delim(*d); SplitString(b, e, delim, limits); Cmp(good, test); UNIT_ASSERT_EQUAL(good, test); - UNIT_ASSERT_EQUAL(TString(limits.Last), TString(last)); + UNIT_ASSERT_EQUAL(std::string(limits.Last), std::string(last)); } Y_UNIT_TEST_SUITE(SplitStringTest) { Y_UNIT_TEST(TestCharSingleDelimiter) { - TString data("qw ab qwabcab"); - TString canonic[] = {"qw", "ab", "", "qwabcab"}; - std::vector good(canonic, canonic + 4); + std::string data("qw ab qwabcab"); + std::string canonic[] = {"qw", "ab", "", "qwabcab"}; + std::vector good(canonic, canonic + 4); TCharDelimiter delim(' '); TestDelimiterOnString(good, data.data(), delim); @@ -130,9 +130,9 @@ Y_UNIT_TEST_SUITE(SplitStringTest) { } Y_UNIT_TEST(TestWideSingleDelimiter) { - TUtf16String data(u"qw ab qwabcab"); - TUtf16String canonic[] = {u"qw", u"ab", TUtf16String(), u"qwabcab"}; - std::vector good(canonic, canonic + 4); + std::u16string data(u"qw ab qwabcab"); + std::u16string canonic[] = {u"qw", u"ab", std::u16string(), u"qwabcab"}; + std::vector good(canonic, canonic + 4); TCharDelimiter delim(' '); TestDelimiterOnString(good, data.data(), delim); @@ -140,7 +140,7 @@ Y_UNIT_TEST_SUITE(SplitStringTest) { } Y_UNIT_TEST(TestConvertToIntCharSingleDelimiter) { - TString data("42 4242 -12345 0"); + std::string data("42 4242 -12345 0"); i32 canonic[] = {42, 4242, -12345, 0}; std::vector good(canonic, canonic + 4); TCharDelimiter delim(' '); @@ -150,36 +150,36 @@ Y_UNIT_TEST_SUITE(SplitStringTest) { } Y_UNIT_TEST(TestCharSkipEmpty) { - TString data("qw ab qwabcab "); - TString canonic[] = {"qw", "ab", "qwabcab"}; - std::vector good(canonic, canonic + 3); + std::string data("qw ab qwabcab "); + std::string canonic[] = {"qw", "ab", "qwabcab"}; + std::vector good(canonic, canonic + 3); TestConsumerOnString>(good, data.data(), " "); TestConsumerOnRange>(good, data.data(), data.end(), " "); } Y_UNIT_TEST(TestCharKeepDelimiters) { - TString data("qw ab qwabcab "); - TString canonic[] = {"qw", " ", "ab", " ", "", " ", "qwabcab", " ", ""}; - std::vector good(canonic, canonic + 9); + std::string data("qw ab qwabcab "); + std::string canonic[] = {"qw", " ", "ab", " ", "", " ", "qwabcab", " ", ""}; + std::vector good(canonic, canonic + 9); TestConsumerOnString>(good, data.data(), " "); TestConsumerOnRange>(good, data.data(), data.end(), " "); } Y_UNIT_TEST(TestCharLimit) { - TString data("qw ab qwabcab "); - TString canonic[] = {"qw", "ab"}; - std::vector good(canonic, canonic + 2); + std::string data("qw ab qwabcab "); + std::string canonic[] = {"qw", "ab"}; + std::vector good(canonic, canonic + 2); TestLimitingConsumerOnString(good, data.data(), " ", 3, " qwabcab "); TestLimitingConsumerOnRange(good, data.data(), data.end(), " ", 3, " qwabcab "); } Y_UNIT_TEST(TestCharStringDelimiter) { - TString data("qw ab qwababcab"); - TString canonic[] = {"qw ", " qw", "", "c", ""}; - std::vector good(canonic, canonic + 5); + std::string data("qw ab qwababcab"); + std::string canonic[] = {"qw ", " qw", "", "c", ""}; + std::vector good(canonic, canonic + 5); TStringDelimiter delim("ab"); TestDelimiterOnString(good, data.data(), delim); @@ -187,10 +187,10 @@ Y_UNIT_TEST_SUITE(SplitStringTest) { } Y_UNIT_TEST(TestWideStringDelimiter) { - TUtf16String data(u"qw ab qwababcab"); - TUtf16String canonic[] = {u"qw ", u" qw", TUtf16String(), u"c", TUtf16String()}; - std::vector good(canonic, canonic + 5); - TUtf16String wideDelim(u"ab"); + std::u16string data(u"qw ab qwababcab"); + std::u16string canonic[] = {u"qw ", u" qw", std::u16string(), u"c", std::u16string()}; + std::vector good(canonic, canonic + 5); + std::u16string wideDelim(u"ab"); TStringDelimiter delim(wideDelim.data()); TestDelimiterOnString(good, data.data(), delim); @@ -198,9 +198,9 @@ Y_UNIT_TEST_SUITE(SplitStringTest) { } Y_UNIT_TEST(TestCharSetDelimiter) { - TString data("qw ab qwababccab"); - TString canonic[] = {"q", " ab q", "abab", "", "ab"}; - std::vector good(canonic, canonic + 5); + std::string data("qw ab qwababccab"); + std::string canonic[] = {"q", " ab q", "abab", "", "ab"}; + std::vector good(canonic, canonic + 5); TSetDelimiter delim("wc"); TestDelimiterOnString(good, data.data(), delim); @@ -208,54 +208,54 @@ Y_UNIT_TEST_SUITE(SplitStringTest) { } Y_UNIT_TEST(TestWideSetDelimiter) { - TUtf16String data(u"qw ab qwababccab"); - TUtf16String canonic[] = {u"q", u" ab q", u"abab", TUtf16String(), u"ab"}; - std::vector good(canonic, canonic + 5); - TUtf16String wideDelim(u"wc"); + std::u16string data(u"qw ab qwababccab"); + std::u16string canonic[] = {u"q", u" ab q", u"abab", std::u16string(), u"ab"}; + std::vector good(canonic, canonic + 5); + std::u16string wideDelim(u"wc"); TSetDelimiter delim(wideDelim.data()); TestDelimiterOnString(good, data.data(), delim); } Y_UNIT_TEST(TestWideSetDelimiterRange) { - TUtf16String data(u"qw ab qwababccab"); - TUtf16String canonic[] = {u"q", u" ab q", u"abab", TUtf16String(), u"ab"}; - std::vector good(1); - TUtf16String wideDelim(u"wc"); + std::u16string data(u"qw ab qwababccab"); + std::u16string canonic[] = {u"q", u" ab q", u"abab", std::u16string(), u"ab"}; + std::vector good(1); + std::u16string wideDelim(u"wc"); TSetDelimiter delim(wideDelim.data()); - std::vector test; - TContainerConsumer> consumer(&test); + std::vector test; + TContainerConsumer> consumer(&test); SplitString(data.data(), data.data(), delim, consumer); // Empty string is still inserted into consumer Cmp(good, test); good.assign(canonic, canonic + 4); - good.push_back(TUtf16String()); + good.push_back(std::u16string()); test.clear(); SplitString(data.data(), data.end() - 2, delim, consumer); Cmp(good, test); } Y_UNIT_TEST(TestSplit) { - TString data("qw ab qwababcba"); - TString canonic[] = {"qw ", " qw", "c"}; - std::vector good(canonic, canonic + 3); - TString delim = "ab"; - std::vector test; + std::string data("qw ab qwababcba"); + std::string canonic[] = {"qw ", " qw", "c"}; + std::vector good(canonic, canonic + 3); + std::string delim = "ab"; + std::vector test; Split(data, delim, test); Cmp(good, test); - std::vector test1; + std::vector test1; Split(data, delim.data(), test1); Cmp(good, test1); } Y_UNIT_TEST(ConvenientSplitTest) { - TString data("abc 22 33.5 xyz"); - TString str; + std::string data("abc 22 33.5 xyz"); + std::string str; int num1 = 0; double num2 = 0; - TStringBuf strBuf; + std::string_view strBuf; Split(data, ' ', str, num1, num2, strBuf); UNIT_ASSERT_VALUES_EQUAL(str, "abc"); UNIT_ASSERT_VALUES_EQUAL(num1, 22); @@ -264,8 +264,8 @@ Y_UNIT_TEST_SUITE(SplitStringTest) { } Y_UNIT_TEST(ConvenientSplitTestWithMaybe) { - TString data("abc 42"); - TString str; + std::string data("abc 42"); + std::string str; std::optional num2 = 1; std::optional maybe = 1; @@ -277,8 +277,8 @@ Y_UNIT_TEST_SUITE(SplitStringTest) { } Y_UNIT_TEST(ConvenientSplitTestExceptions) { - TString data("abc 22 33"); - TString s1, s2, s3, s4; + std::string data("abc 22 33"); + std::string s1, s2, s3, s4; UNIT_ASSERT_EXCEPTION(Split(data, ' ', s1, s2), yexception); UNIT_ASSERT_NO_EXCEPTION(Split(data, ' ', s1, s2, s3)); @@ -286,8 +286,8 @@ Y_UNIT_TEST_SUITE(SplitStringTest) { } Y_UNIT_TEST(ConvenientSplitTestOptionalExceptions) { - TString data("abc 22 33"); - TString s1, s2; + std::string data("abc 22 33"); + std::string s1, s2; std::optional m1, m2; UNIT_ASSERT_EXCEPTION(Split(data, ' ', s1, m1), yexception); @@ -335,18 +335,18 @@ Y_UNIT_TEST_SUITE(StringSplitter) { } Y_UNIT_TEST(TestSplitLimited) { - std::vector expected = {"1", "2", "3,4,5"}; - std::vector actual = StringSplitter("1,2,3,4,5").Split(',').Limit(3).ToList(); + std::vector expected = {"1", "2", "3,4,5"}; + std::vector actual = StringSplitter("1,2,3,4,5").Split(',').Limit(3).ToList(); UNIT_ASSERT_VALUES_EQUAL(expected, actual); } Y_UNIT_TEST(TestSplitLimitedWithEmptySkip) { - std::vector expected = {"1", "2", "3,4,5"}; - std::vector actual = StringSplitter("1,,,2,,,,3,4,5").Split(',').SkipEmpty().Limit(3).ToList(); + std::vector expected = {"1", "2", "3,4,5"}; + std::vector actual = StringSplitter("1,,,2,,,,3,4,5").Split(',').SkipEmpty().Limit(3).ToList(); UNIT_ASSERT_VALUES_EQUAL(expected, actual); expected = {"1", "2", ",,,3,4,5"}; - actual = StringSplitter("1,2,,,,3,4,5").Split(',').Limit(3).SkipEmpty().ToList(); + actual = StringSplitter("1,2,,,,3,4,5").Split(',').Limit(3).SkipEmpty().ToList(); UNIT_ASSERT_VALUES_EQUAL(expected, actual); } @@ -361,18 +361,18 @@ Y_UNIT_TEST_SUITE(StringSplitter) { } Y_UNIT_TEST(TestSplitBySetLimited) { - std::vector expected = {"1", "2", "3,4:5"}; - std::vector actual = StringSplitter("1,2:3,4:5").SplitBySet(",:").Limit(3).ToList(); + std::vector expected = {"1", "2", "3,4:5"}; + std::vector actual = StringSplitter("1,2:3,4:5").SplitBySet(",:").Limit(3).ToList(); UNIT_ASSERT_VALUES_EQUAL(expected, actual); } Y_UNIT_TEST(TestSplitBySetLimitedWithEmptySkip) { - std::vector expected = {"1", "2", "3,4:5"}; - std::vector actual = StringSplitter("1,:,2::::,3,4:5").SplitBySet(",:").SkipEmpty().Limit(3).ToList(); + std::vector expected = {"1", "2", "3,4:5"}; + std::vector actual = StringSplitter("1,:,2::::,3,4:5").SplitBySet(",:").SkipEmpty().Limit(3).ToList(); UNIT_ASSERT_VALUES_EQUAL(expected, actual); expected = {"1", ",2::::,3,4:5"}; - actual = StringSplitter("1,:,2::::,3,4:5").SplitBySet(",:").Limit(3).SkipEmpty().ToList(); + actual = StringSplitter("1,:,2::::,3,4:5").SplitBySet(",:").Limit(3).SkipEmpty().ToList(); UNIT_ASSERT_VALUES_EQUAL(expected, actual); } @@ -387,22 +387,22 @@ Y_UNIT_TEST_SUITE(StringSplitter) { } Y_UNIT_TEST(TestSplitByStringLimited) { - std::vector expected = {"1", "2", "3ab4ab5"}; - std::vector actual = StringSplitter("1ab2ab3ab4ab5").SplitByString("ab").Limit(3).ToList(); + std::vector expected = {"1", "2", "3ab4ab5"}; + std::vector actual = StringSplitter("1ab2ab3ab4ab5").SplitByString("ab").Limit(3).ToList(); UNIT_ASSERT_VALUES_EQUAL(expected, actual); } Y_UNIT_TEST(TestSplitByStringLimitedWithEmptySkip) { - std::vector expected = {"1", "2", "3ab4ab5"}; - std::vector actual = StringSplitter("1abab2ababababab3ab4ab5").SplitByString("ab").SkipEmpty().Limit(3).ToList(); + std::vector expected = {"1", "2", "3ab4ab5"}; + std::vector actual = StringSplitter("1abab2ababababab3ab4ab5").SplitByString("ab").SkipEmpty().Limit(3).ToList(); UNIT_ASSERT_VALUES_EQUAL(expected, actual); } Y_UNIT_TEST(TestSplitByFunc) { - TString s = "123 456 \t\n789\n10\t 20"; - std::vector pattern = {"123", "456", "789", "10", "20"}; + std::string s = "123 456 \t\n789\n10\t 20"; + std::vector pattern = {"123", "456", "789", "10", "20"}; - std::vector tokens; + std::vector tokens; auto f = [](char a) { return a == ' ' || a == '\t' || a == '\n'; }; for (auto v : StringSplitter(s).SplitByFunc(f)) { if (v) { @@ -414,16 +414,16 @@ Y_UNIT_TEST_SUITE(StringSplitter) { } Y_UNIT_TEST(TestSplitByFuncLimited) { - std::vector expected = {"1", "2", "3a4b5"}; + std::vector expected = {"1", "2", "3a4b5"}; auto f = [](char a) { return a == 'a' || a == 'b'; }; - std::vector actual = StringSplitter("1a2b3a4b5").SplitByFunc(f).Limit(3).ToList(); + std::vector actual = StringSplitter("1a2b3a4b5").SplitByFunc(f).Limit(3).ToList(); UNIT_ASSERT_VALUES_EQUAL(expected, actual); } Y_UNIT_TEST(TestSplitByFuncLimitedWithEmptySkip) { - std::vector expected = {"1", "2", "3a4b5"}; + std::vector expected = {"1", "2", "3a4b5"}; auto f = [](char a) { return a == 'a' || a == 'b'; }; - std::vector actual = StringSplitter("1aaba2bbababa3a4b5").SplitByFunc(f).SkipEmpty().Limit(3).Take(3).ToList(); + std::vector actual = StringSplitter("1aaba2bbababa3a4b5").SplitByFunc(f).SkipEmpty().Limit(3).Take(3).ToList(); UNIT_ASSERT_VALUES_EQUAL(expected, actual); } @@ -445,26 +445,26 @@ Y_UNIT_TEST_SUITE(StringSplitter) { } Y_UNIT_TEST(TestTake) { - std::vector expected = {"1", "2", "3"}; - UNIT_ASSERT_VALUES_EQUAL(expected, StringSplitter("1 2 3 4 5 6 7 8 9 10").Split(' ').Take(3).ToList()); + std::vector expected = {"1", "2", "3"}; + UNIT_ASSERT_VALUES_EQUAL(expected, StringSplitter("1 2 3 4 5 6 7 8 9 10").Split(' ').Take(3).ToList()); expected = {"1", "2"}; - UNIT_ASSERT_VALUES_EQUAL(expected, StringSplitter(" 1 2 3 ").Split(' ').SkipEmpty().Take(2).ToList()); + UNIT_ASSERT_VALUES_EQUAL(expected, StringSplitter(" 1 2 3 ").Split(' ').SkipEmpty().Take(2).ToList()); expected = {"1", "2", "3"}; - UNIT_ASSERT_VALUES_EQUAL(expected, StringSplitter("1 2 3 4 5 6 7 8 9 10").Split(' ').Take(5).Take(3).ToList()); - UNIT_ASSERT_VALUES_EQUAL(expected, StringSplitter("1 2 3 4 5 6 7 8 9 10").Split(' ').Take(3).Take(5).ToList()); + UNIT_ASSERT_VALUES_EQUAL(expected, StringSplitter("1 2 3 4 5 6 7 8 9 10").Split(' ').Take(5).Take(3).ToList()); + UNIT_ASSERT_VALUES_EQUAL(expected, StringSplitter("1 2 3 4 5 6 7 8 9 10").Split(' ').Take(3).Take(5).ToList()); expected = {"1", "2"}; - UNIT_ASSERT_VALUES_EQUAL(expected, StringSplitter(" 1 2 3 ").Split(' ').Take(4).SkipEmpty().ToList()); + UNIT_ASSERT_VALUES_EQUAL(expected, StringSplitter(" 1 2 3 ").Split(' ').Take(4).SkipEmpty().ToList()); expected = {"1"}; - UNIT_ASSERT_VALUES_EQUAL(expected, StringSplitter(" 1 2 3 ").Split(' ').Take(4).SkipEmpty().Take(1).ToList()); + UNIT_ASSERT_VALUES_EQUAL(expected, StringSplitter(" 1 2 3 ").Split(' ').Take(4).SkipEmpty().Take(1).ToList()); } Y_UNIT_TEST(TestCompile) { - (void)StringSplitter(TString()); - (void)StringSplitter(TStringBuf()); + (void)StringSplitter(std::string()); + (void)StringSplitter(std::string_view()); (void)StringSplitter("", 0); } @@ -489,23 +489,23 @@ Y_UNIT_TEST_SUITE(StringSplitter) { } Y_UNIT_TEST(TestStringSplitterConsume) { - std::vector expected = {"1", "2", "3"}; - std::vector actual; - auto func = [&actual](const TBasicStringBuf& token) { - actual.push_back(TString(token)); + std::vector expected = {"1", "2", "3"}; + std::vector actual; + auto func = [&actual](const std::basic_string_view& token) { + actual.push_back(std::string(token)); }; StringSplitter("1 2 3").Split(' ').Consume(func); UNIT_ASSERT_VALUES_EQUAL(expected, actual); } Y_UNIT_TEST(TestStringSplitterConsumeConditional) { - std::vector expected = {"1", "2"}; - std::vector actual; - auto func = [&actual](const TBasicStringBuf& token) { + std::vector expected = {"1", "2"}; + std::vector actual; + auto func = [&actual](const std::basic_string_view& token) { if (token == "3") { return false; } - actual.push_back(TString(token)); + actual.push_back(std::string(token)); return true; }; bool completed = StringSplitter("1 2 3 4 5").Split(' ').Consume(func); @@ -514,27 +514,27 @@ Y_UNIT_TEST_SUITE(StringSplitter) { } Y_UNIT_TEST(TestStringSplitterToList) { - std::vector expected = {"1", "2", "3"}; - std::vector actual = StringSplitter("1 2 3").Split(' ').ToList(); + std::vector expected = {"1", "2", "3"}; + std::vector actual = StringSplitter("1 2 3").Split(' ').ToList(); UNIT_ASSERT_VALUES_EQUAL(expected, actual); } Y_UNIT_TEST(TestStringSplitterCollectPushBack) { - std::vector expected = {"1", "2", "3"}; - std::vector actual; + std::vector expected = {"1", "2", "3"}; + std::vector actual; StringSplitter("1 2 3").Split(' ').Collect(&actual); UNIT_ASSERT_VALUES_EQUAL(expected, actual); } Y_UNIT_TEST(TestStringSplitterCollectInsert) { - TSet expected = {"1", "2", "3"}; - TSet actual; + TSet expected = {"1", "2", "3"}; + TSet actual; StringSplitter("1 2 3 1 2 3").Split(' ').Collect(&actual); UNIT_ASSERT_VALUES_EQUAL(expected, actual); } Y_UNIT_TEST(TestStringSplitterCollectClears) { - std::vector v; + std::vector v; StringSplitter("1 2 3").Split(' ').Collect(&v); UNIT_ASSERT_VALUES_EQUAL(v.size(), 3); StringSplitter("4 5").Split(' ').Collect(&v); @@ -542,7 +542,7 @@ Y_UNIT_TEST_SUITE(StringSplitter) { } Y_UNIT_TEST(TestStringSplitterAddToDoesntClear) { - std::vector v; + std::vector v; StringSplitter("1 2 3").Split(' ').AddTo(&v); UNIT_ASSERT_VALUES_EQUAL(v.size(), 3); StringSplitter("4 5").Split(' ').AddTo(&v); @@ -551,7 +551,7 @@ Y_UNIT_TEST_SUITE(StringSplitter) { Y_UNIT_TEST(TestSplitStringInto) { int a = -1; - TStringBuf s; + std::string_view s; double d = -1; StringSplitter("2 substr 1.02").Split(' ').CollectInto(&a, &s, &d); UNIT_ASSERT_VALUES_EQUAL(a, 2); @@ -562,7 +562,7 @@ Y_UNIT_TEST_SUITE(StringSplitter) { } Y_UNIT_TEST(TestSplitStringWithIgnore) { - TStringBuf s; + std::string_view s; StringSplitter("x y z").Split(' ').CollectInto(&std::ignore, &s, &std::ignore); UNIT_ASSERT_VALUES_EQUAL(s, "y"); @@ -594,7 +594,7 @@ Y_UNIT_TEST_SUITE(StringSplitter) { Y_UNIT_TEST(TestOwningSplit1) { int sum = 0; - for (const auto& it : StringSplitter(TString("1,2,3")).Split(',')) { + for (const auto& it : StringSplitter(std::string("1,2,3")).Split(',')) { sum += FromString(it.Token()); } @@ -604,7 +604,7 @@ Y_UNIT_TEST_SUITE(StringSplitter) { Y_UNIT_TEST(TestOwningSplit2) { int sum = 0; - TString str("1,2,3"); + std::string str("1,2,3"); for (const auto& it : StringSplitter(str).Split(',')) { sum += FromString(it.Token()); } @@ -615,7 +615,7 @@ Y_UNIT_TEST_SUITE(StringSplitter) { Y_UNIT_TEST(TestOwningSplit3) { int sum = 0; - const TString str("1,2,3"); + const std::string str("1,2,3"); for (const auto& it : StringSplitter(str).Split(',')) { sum += FromString(it.Token()); } @@ -624,43 +624,43 @@ Y_UNIT_TEST_SUITE(StringSplitter) { } Y_UNIT_TEST(TestAssigment) { - std::vector expected0 = {"1", "2", "3", "4"}; - std::vector actual0 = StringSplitter("1 2 3 4").Split(' '); + std::vector expected0 = {"1", "2", "3", "4"}; + std::vector actual0 = StringSplitter("1 2 3 4").Split(' '); UNIT_ASSERT_VALUES_EQUAL(expected0, actual0); - TSet expected1 = {"11", "22", "33", "44"}; - TSet actual1 = StringSplitter("11 22 33 44").Split(' '); + TSet expected1 = {"11", "22", "33", "44"}; + TSet actual1 = StringSplitter("11 22 33 44").Split(' '); UNIT_ASSERT_VALUES_EQUAL(expected1, actual1); - TSet expected2 = {"11", "aa"}; - auto actual2 = static_cast>(StringSplitter("11 aa 11 11 aa").Split(' ')); + TSet expected2 = {"11", "aa"}; + auto actual2 = static_cast>(StringSplitter("11 aa 11 11 aa").Split(' ')); UNIT_ASSERT_VALUES_EQUAL(expected2, actual2); - std::vector expected3 = {"dd", "bb"}; - auto actual3 = std::vector(StringSplitter("dd\tbb").Split('\t')); + std::vector expected3 = {"dd", "bb"}; + auto actual3 = std::vector(StringSplitter("dd\tbb").Split('\t')); UNIT_ASSERT_VALUES_EQUAL(expected3, actual3); } Y_UNIT_TEST(TestRangeBasedFor) { - std::vector actual0 = {"11", "22", "33", "44"}; + std::vector actual0 = {"11", "22", "33", "44"}; size_t num = 0; - for (TStringBuf elem : StringSplitter("11 22 33 44").Split(' ')) { + for (std::string_view elem : StringSplitter("11 22 33 44").Split(' ')) { UNIT_ASSERT_VALUES_EQUAL(elem, actual0[num++]); } - std::vector actual1 = {"another", "one,", "and", "another", "one"}; + std::vector actual1 = {"another", "one,", "and", "another", "one"}; num = 0; - for (TStringBuf elem : StringSplitter(TStringBuf("another one, and \n\n another one")).SplitBySet(" \n").SkipEmpty()) { + for (std::string_view elem : StringSplitter(std::string_view("another one, and \n\n another one")).SplitBySet(" \n").SkipEmpty()) { UNIT_ASSERT_VALUES_EQUAL(elem, actual1[num++]); } - std::vector actual2 = {u"привет,", u"как", u"дела"}; + std::vector actual2 = {u"привет,", u"как", u"дела"}; num = 0; - for (TWtringBuf elem : StringSplitter(u"привет, как дела").Split(wchar16(' '))) { + for (std::u16string_view elem : StringSplitter(u"привет, как дела").Split(wchar16(' '))) { UNIT_ASSERT_VALUES_EQUAL(elem, actual2[num++]); } - std::vector copy(4); + std::vector copy(4); auto v = StringSplitter("11 22 33 44").Split(' '); Copy(v.begin(), v.end(), copy.begin()); UNIT_ASSERT_VALUES_EQUAL(actual0, copy); @@ -731,14 +731,14 @@ Y_UNIT_TEST_SUITE(StringSplitter) { } Y_UNIT_TEST(TestArcadiaStdInterop) { - std::vector expected0 = {"a", "b"}; - std::vector expected1 = {"a", "b"}; + std::vector expected0 = {"a", "b"}; + std::vector expected1 = {"a", "b"}; std::string src1("a b"); std::string_view src2("a b"); - std::vector actual0 = StringSplitter(src1).Split(' ').SkipEmpty(); - std::vector actual1 = StringSplitter(src2).Split(' ').SkipEmpty(); - std::vector actual2 = StringSplitter(src1).Split(' ').SkipEmpty(); - std::vector actual3 = StringSplitter(src2).Split(' ').SkipEmpty(); + std::vector actual0 = StringSplitter(src1).Split(' ').SkipEmpty(); + std::vector actual1 = StringSplitter(src2).Split(' ').SkipEmpty(); + std::vector actual2 = StringSplitter(src1).Split(' ').SkipEmpty(); + std::vector actual3 = StringSplitter(src2).Split(' ').SkipEmpty(); UNIT_ASSERT_VALUES_EQUAL(expected0, actual0); UNIT_ASSERT_VALUES_EQUAL(expected0, actual1); UNIT_ASSERT_VALUES_EQUAL(expected1, actual2); @@ -749,28 +749,28 @@ Y_UNIT_TEST_SUITE(StringSplitter) { const char* b = "a;b"; const char* e = b + 3; - std::vector v; + std::vector v; StringSplitter(b, e).Split(';').AddTo(&v); - std::vector expected = {"a", "b"}; + std::vector expected = {"a", "b"}; UNIT_ASSERT_VALUES_EQUAL(v, expected); } Y_UNIT_TEST(TestCStringRef) { - TString s = "lol"; + std::string s = "lol"; char* str = s.Detach(); - std::vector v = StringSplitter(str).Split('o'); - std::vector expected = {"l", "l"}; + std::vector v = StringSplitter(str).Split('o'); + std::vector expected = {"l", "l"}; UNIT_ASSERT_VALUES_EQUAL(v, expected); } Y_UNIT_TEST(TestSplitVector) { std::vector buffer = {'a', ';', 'b'}; - std::vector v = StringSplitter(buffer).Split(';'); + std::vector v = StringSplitter(buffer).Split(';'); - std::vector expected = {"a", "b"}; + std::vector expected = {"a", "b"}; UNIT_ASSERT_VALUES_EQUAL(v, expected); } diff --git a/util/string/strip.cpp b/util/string/strip.cpp index 1cf5e33096f..e6610ff76f7 100644 --- a/util/string/strip.cpp +++ b/util/string/strip.cpp @@ -3,15 +3,15 @@ #include -void CollapseText(const TString& from, TString& to, size_t maxLen) { +void CollapseText(const std::string& from, std::string& to, size_t maxLen) { Collapse(from, to, maxLen); StripInPlace(to); if (to.size() >= maxLen) { - to.remove(maxLen - 5); // " ..." + to.erase(maxLen - 5); // " ..." ReverseInPlace(to); size_t pos = to.find_first_of(" .,;"); - if (pos != TString::npos && pos < 32) { - to.remove(0, pos + 1); + if (pos != std::string::npos && pos < 32) { + to.erase(0, pos + 1); } ReverseInPlace(to); to.append(" ..."); diff --git a/util/string/strip.h b/util/string/strip.h index 666300e0e71..30f274dbdce 100644 --- a/util/string/strip.h +++ b/util/string/strip.h @@ -2,8 +2,6 @@ #include "ascii.h" -#include -#include #include #include @@ -183,12 +181,12 @@ template } /// Copies the given string removing leading and trailing spaces. -static inline bool Strip(const TString& from, TString& to) { +static inline bool Strip(const std::string& from, std::string& to) { return StripString(from, to); } /// Removes leading and trailing spaces from the string. -inline TString& StripInPlace(TString& s) { +inline std::string& StripInPlace(std::string& s) { Strip(s, s); return s; } @@ -199,8 +197,8 @@ inline void StripInPlace(T& s) { } /// Returns a copy of the given string with removed leading and trailing spaces. -[[nodiscard]] inline TString Strip(const TString& s) { - TString ret = s; +[[nodiscard]] inline std::string Strip(const std::string& s) { + std::string ret = s; Strip(ret, ret); return ret; } @@ -232,7 +230,7 @@ bool CollapseImpl(const TStringType& from, TStringType& to, size_t maxLen, const if (isWhitespace(to[i]) && (to[i] != ' ' || isWhitespace(to[i + 1]))) { size_t tailSize = maxLen - i; size_t newTailSize = CollapseImpl(to.begin() + i, tailSize, isWhitespace); - to.remove(i + newTailSize, tailSize - newTailSize); + to.erase(i + newTailSize, tailSize - newTailSize); return true; } } @@ -271,12 +269,12 @@ template return ret; } -void CollapseText(const TString& from, TString& to, size_t maxLen); +void CollapseText(const std::string& from, std::string& to, size_t maxLen); /// The same as Collapse() + truncates the string to maxLen. /// @details An ellipsis is inserted at the end of the truncated line. -inline void CollapseText(TString& s, size_t maxLen) { - TString to; +inline void CollapseText(std::string& s, size_t maxLen) { + std::string to; CollapseText(s, to, maxLen); s = to; } diff --git a/util/string/strip_ut.cpp b/util/string/strip_ut.cpp index 283ab66fdfa..05a8c81eaa2 100644 --- a/util/string/strip_ut.cpp +++ b/util/string/strip_ut.cpp @@ -6,10 +6,10 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { struct TStripTest { - TStringBuf Str; - TStringBuf StripLeftRes; - TStringBuf StripRightRes; - TStringBuf StripRes; + std::string_view Str; + std::string_view StripLeftRes; + std::string_view StripRightRes; + std::string_view StripRes; }; static constexpr TStripTest StripTests[] = { {" 012 ", "012 ", " 012", "012"}, @@ -28,9 +28,9 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { Y_UNIT_TEST(TestStrip) { for (const auto& test : StripTests) { - TString inputStr(test.Str); + std::string inputStr(test.Str); - TString s; + std::string s; Strip(inputStr, s); UNIT_ASSERT_EQUAL(s, test.StripRes); @@ -38,7 +38,7 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { UNIT_ASSERT_EQUAL(StripStringLeft(inputStr), test.StripLeftRes); UNIT_ASSERT_EQUAL(StripStringRight(inputStr), test.StripRightRes); - TStringBuf inputStrBuf(test.Str); + std::string_view inputStrBuf(test.Str); UNIT_ASSERT_EQUAL(StripString(inputStrBuf), test.StripRes); UNIT_ASSERT_EQUAL(StripStringLeft(inputStrBuf), test.StripLeftRes); UNIT_ASSERT_EQUAL(StripStringRight(inputStrBuf), test.StripRightRes); @@ -47,14 +47,14 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { Y_UNIT_TEST(TestStripInPlace) { for (const auto& test : StripTests) { - TString str(test.Str); + std::string str(test.Str); Y_ASSERT(str.IsDetached() || str.empty()); // prerequisite of the test; check that we don't try to modify shared COW-string in-place by accident const void* stringPtrPrior = str.data(); StripInPlace(str); const void* stringPtrAfter = str.data(); UNIT_ASSERT_VALUES_EQUAL(str, test.StripRes); if (!test.Str.empty()) { - UNIT_ASSERT_EQUAL_C(stringPtrPrior, stringPtrAfter, TString(test.Str).Quote()); // StripInPlace should reuse buffer of original string + UNIT_ASSERT_EQUAL_C(stringPtrPrior, stringPtrAfter, std::string(test.Str).Quote()); // StripInPlace should reuse buffer of original string } } } @@ -73,7 +73,7 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { for (auto test : tests) { UNIT_ASSERT_EQUAL( - StripString(TString(test.Str), EqualsStripAdapter('/')), + StripString(std::string(test.Str), EqualsStripAdapter('/')), test.Result); }; } @@ -93,44 +93,44 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { for (const auto& test : tests) { UNIT_ASSERT_EQUAL( - StripStringLeft(TString(test.Str), EqualsStripAdapter('/')), + StripStringLeft(std::string(test.Str), EqualsStripAdapter('/')), test.ResultLeft); UNIT_ASSERT_EQUAL( - StripStringRight(TString(test.Str), EqualsStripAdapter('/')), + StripStringRight(std::string(test.Str), EqualsStripAdapter('/')), test.ResultRight); }; } Y_UNIT_TEST(TestNullStringStrip) { - TStringBuf nullString(nullptr, nullptr); + std::string_view nullString(nullptr, nullptr); UNIT_ASSERT_EQUAL( StripString(nullString), - TString()); + std::string()); } Y_UNIT_TEST(TestWtrokaStrip) { - UNIT_ASSERT_EQUAL(StripString(TWtringBuf(u" abc ")), u"abc"); - UNIT_ASSERT_EQUAL(StripStringLeft(TWtringBuf(u" abc ")), u"abc "); - UNIT_ASSERT_EQUAL(StripStringRight(TWtringBuf(u" abc ")), u" abc"); + UNIT_ASSERT_EQUAL(StripString(std::u16string_view(u" abc ")), u"abc"); + UNIT_ASSERT_EQUAL(StripStringLeft(std::u16string_view(u" abc ")), u"abc "); + UNIT_ASSERT_EQUAL(StripStringRight(std::u16string_view(u" abc ")), u" abc"); } Y_UNIT_TEST(TestWtrokaCustomStrip) { UNIT_ASSERT_EQUAL( StripString( - TWtringBuf(u"/abc/"), + std::u16string_view(u"/abc/"), EqualsStripAdapter(u'/')), u"abc"); } Y_UNIT_TEST(TestCollapseUtf32) { - TUtf32String s; + std::u32string s; Collapse(UTF8ToUTF32(" 123 456 "), s, IsWhitespace); UNIT_ASSERT(s == UTF8ToUTF32(" 123 456 ")); Collapse(UTF8ToUTF32(" 123 456 "), s, IsWhitespace, 10); UNIT_ASSERT(s == UTF8ToUTF32(" 123 456 ")); s = UTF8ToUTF32(" a b c "); - TUtf32String s2 = s; + std::u32string s2 = s; CollapseInPlace(s2, IsWhitespace); UNIT_ASSERT(s == s2); @@ -140,14 +140,14 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { } Y_UNIT_TEST(TestCollapseUtf16) { - TUtf16String s; + std::u16string s; Collapse(UTF8ToWide(" 123 456 "), s); UNIT_ASSERT(s == UTF8ToWide(" 123 456 ")); Collapse(UTF8ToWide(" 123 456 "), s, 10); UNIT_ASSERT(s == UTF8ToWide(" 123 456 ")); s = UTF8ToWide(" a b c "); - TUtf16String s2 = s; + std::u16string s2 = s; CollapseInPlace(s2); UNIT_ASSERT(s == s2); @@ -157,14 +157,14 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { } Y_UNIT_TEST(TestCollapse) { - TString s; - Collapse(TString(" 123 456 "), s); + std::string s; + Collapse(std::string(" 123 456 "), s); UNIT_ASSERT(s == " 123 456 "); - Collapse(TString(" 123 456 "), s, 10); + Collapse(std::string(" 123 456 "), s, 10); UNIT_ASSERT(s == " 123 456 "); - s = TString(" a b c "); - TString s2 = s; + s = std::string(" a b c "); + std::string s2 = s; CollapseInPlace(s2); UNIT_ASSERT(s == s2); @@ -174,9 +174,9 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { } Y_UNIT_TEST(TestCollapseText) { - TString abs1("Very long description string written in unknown language."); - TString abs2(abs1); - TString abs3(abs1); + std::string abs1("Very long description string written in unknown language."); + std::string abs2(abs1); + std::string abs3(abs1); CollapseText(abs1, 204); CollapseText(abs2, 54); CollapseText(abs3, 49); diff --git a/util/string/strspn_ut.cpp b/util/string/strspn_ut.cpp index 975374cba4e..c672f8d3ba1 100644 --- a/util/string/strspn_ut.cpp +++ b/util/string/strspn_ut.cpp @@ -4,7 +4,7 @@ Y_UNIT_TEST_SUITE(TStrSpnTest) { Y_UNIT_TEST(FindFirstOf) { - const TString s("some text!"); + const std::string s("some text!"); UNIT_ASSERT_EQUAL(TCompactStrSpn("mos").FindFirstOf(s.begin(), s.end()), s.begin()); UNIT_ASSERT_EQUAL(TCompactStrSpn("mos").FindFirstOf(s.c_str()), s.begin()); @@ -20,14 +20,14 @@ Y_UNIT_TEST_SUITE(TStrSpnTest) { // Must be const. If not, non-const begin() will clone the shared empty string // and the next assertion will possibly use invalidated end iterator. - const TString empty; + const std::string empty; UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(empty.begin(), empty.end()), empty.end()); UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(empty.c_str()), empty.end()); } Y_UNIT_TEST(FindFirstNotOf) { - const TString s("abacabaxyz"); + const std::string s("abacabaxyz"); UNIT_ASSERT_EQUAL(TCompactStrSpn("123").FindFirstNotOf(s.begin(), s.end()), s.begin()); UNIT_ASSERT_EQUAL(TCompactStrSpn("123").FindFirstNotOf(s.c_str()), s.begin()); @@ -41,33 +41,33 @@ Y_UNIT_TEST_SUITE(TStrSpnTest) { UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxyz").FindFirstNotOf(s.begin(), s.end()), s.end()); UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxyz").FindFirstNotOf(s.c_str()), s.end()); - const TString empty; + const std::string empty; UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(empty.begin(), empty.end()), empty.end()); UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(empty.c_str()), empty.end()); } Y_UNIT_TEST(FindFirstOfReverse) { - TStringBuf s("some text"); + std::string_view s("some text"); UNIT_ASSERT_EQUAL(TCompactStrSpn("xt").FindFirstOf(s.rbegin(), s.rend()), s.rbegin()); UNIT_ASSERT_EQUAL(TCompactStrSpn("mos").FindFirstOf(s.rbegin(), s.rend()), s.rend() - 3); UNIT_ASSERT_EQUAL(TCompactStrSpn("s").FindFirstOf(s.rbegin(), s.rend()), s.rend() - 1); UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(s.rbegin(), s.rend()), s.rend()); - TStringBuf empty; + std::string_view empty; UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(empty.rbegin(), empty.rend()), empty.rend()); } Y_UNIT_TEST(FindFirstNotOfReverse) { - TStringBuf s("_abacabaxyz"); + std::string_view s("_abacabaxyz"); UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(s.rbegin(), s.rend()), s.rbegin()); UNIT_ASSERT_EQUAL(TCompactStrSpn("xyz").FindFirstNotOf(s.rbegin(), s.rend()), s.rbegin() + 3); UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxyz").FindFirstNotOf(s.rbegin(), s.rend()), s.rend() - 1); UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxyz_").FindFirstNotOf(s.rbegin(), s.rend()), s.rend()); - TStringBuf empty; + std::string_view empty; UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(empty.rbegin(), empty.rend()), empty.rend()); } } diff --git a/util/string/subst.cpp b/util/string/subst.cpp index b2df328dc18..ecf6c2892b8 100644 --- a/util/string/subst.cpp +++ b/util/string/subst.cpp @@ -1,7 +1,5 @@ #include "subst.h" -#include -#include #include #include @@ -43,7 +41,7 @@ static bool IsIntersect(const T& a, const U& b) noexcept { * Uses two separate implementations (inplace for shrink and append for grow case) * See IGNIETFERRO-394 **/ -template > +template > static inline size_t SubstGlobalImpl(TStringType& s, const TStringViewType from, const TStringViewType to, size_t fromPos = 0) { if (from.empty()) { return 0; @@ -157,45 +155,26 @@ static_assert(sizeof(wchar32) == sizeof(char32_t), ""); static_assert(std::is_unsigned::value == std::is_unsigned::value, ""); static_assert(std::is_unsigned::value == std::is_unsigned::value, ""); -size_t SubstGlobal(TString& text, const TStringBuf what, const TStringBuf with, size_t from) { +size_t SubstGlobal(std::string& text, const std::string_view what, const std::string_view with, size_t from) { return SubstGlobalImpl(text, what, with, from); } -size_t SubstGlobal(std::string& text, const TStringBuf what, const TStringBuf with, size_t from) { +size_t SubstGlobal(std::u16string& text, const std::u16string_view what, const std::u16string_view with, size_t from) { return SubstGlobalImpl(text, what, with, from); } -size_t SubstGlobal(TUtf16String& text, const TWtringBuf what, const TWtringBuf with, size_t from) { +size_t SubstGlobal(std::u32string& text, const std::u32string_view what, const std::u32string_view with, size_t from) { return SubstGlobalImpl(text, what, with, from); } -size_t SubstGlobal(TUtf32String& text, const TUtf32StringBuf what, const TUtf32StringBuf with, size_t from) { - return SubstGlobalImpl(text, what, with, from); -} - -size_t SubstGlobal(std::u16string& text, const TWtringBuf what, const TWtringBuf with, size_t from) { - return SubstGlobalImpl(text, - std::u16string_view(reinterpret_cast(what.data()), what.size()), - std::u16string_view(reinterpret_cast(with.data()), with.size()), - from); -} - -size_t SubstGlobal(TString& text, char what, char with, size_t from) { - return SubstCharGlobalImpl(text, what, with, from); -} - size_t SubstGlobal(std::string& text, char what, char with, size_t from) { return SubstCharGlobalImpl(text, what, with, from); } -size_t SubstGlobal(TUtf16String& text, wchar16 what, wchar16 with, size_t from) { - return SubstCharGlobalImpl(text, (char16_t)what, (char16_t)with, from); -} - size_t SubstGlobal(std::u16string& text, wchar16 what, wchar16 with, size_t from) { return SubstCharGlobalImpl(text, (char16_t)what, (char16_t)with, from); } -size_t SubstGlobal(TUtf32String& text, wchar32 what, wchar32 with, size_t from) { +size_t SubstGlobal(std::u32string& text, wchar32 what, wchar32 with, size_t from) { return SubstCharGlobalImpl(text, (char32_t)what, (char32_t)with, from); } diff --git a/util/string/subst.h b/util/string/subst.h index 45b622fbefd..bec23624ba2 100644 --- a/util/string/subst.h +++ b/util/string/subst.h @@ -3,6 +3,8 @@ #include #include +#include +#include /* Replace all occurences of substring `what` with string `with` starting from position `from`. * @@ -13,11 +15,9 @@ * * @return Number of replacements occured. */ -size_t SubstGlobal(TString& text, TStringBuf what, TStringBuf with, size_t from = 0); -size_t SubstGlobal(std::string& text, TStringBuf what, TStringBuf with, size_t from = 0); -size_t SubstGlobal(TUtf16String& text, TWtringBuf what, TWtringBuf with, size_t from = 0); -size_t SubstGlobal(std::u16string& text, TWtringBuf what, TWtringBuf with, size_t from = 0); -size_t SubstGlobal(TUtf32String& text, TUtf32StringBuf what, TUtf32StringBuf with, size_t from = 0); +size_t SubstGlobal(std::string& text, std::string_view what, std::string_view with, size_t from = 0); +size_t SubstGlobal(std::u16string& text, std::u16string_view what, std::u16string_view with, size_t from = 0); +size_t SubstGlobal(std::u32string& text, std::u32string_view what, std::u32string_view with, size_t from = 0); /* Replace all occurences of character `what` with character `with` starting from position `from`. * @@ -28,15 +28,13 @@ size_t SubstGlobal(TUtf32String& text, TUtf32StringBuf what, TUtf32StringBuf wit * * @return Number of replacements occured. */ -size_t SubstGlobal(TString& text, char what, char with, size_t from = 0); size_t SubstGlobal(std::string& text, char what, char with, size_t from = 0); -size_t SubstGlobal(TUtf16String& text, wchar16 what, wchar16 with, size_t from = 0); size_t SubstGlobal(std::u16string& text, wchar16 what, wchar16 with, size_t from = 0); -size_t SubstGlobal(TUtf32String& text, wchar32 what, wchar32 with, size_t from = 0); +size_t SubstGlobal(std::u32string& text, wchar32 what, wchar32 with, size_t from = 0); // TODO(yazevnul): // - rename `SubstGlobal` to `ReplaceAll` for convenience -// - add `SubstGlobalCopy(TStringBuf)` for convenience +// - add `SubstGlobalCopy(std::string_view)` for convenience // - add `RemoveAll(text, what, from)` as a shortcut for `SubstGlobal(text, what, "", from)` // - rename file to `replace.h` diff --git a/util/string/subst_ut.cpp b/util/string/subst_ut.cpp index 91d0db7b18a..8bc030a1e77 100644 --- a/util/string/subst_ut.cpp +++ b/util/string/subst_ut.cpp @@ -6,11 +6,11 @@ Y_UNIT_TEST_SUITE(TStringSubst) { static const size_t MIN_FROM_CTX = 4; - static const std::vector ALL_FROM{TString("F"), TString("FF")}; - static const std::vector ALL_TO{TString(""), TString("T"), TString("TT"), TString("TTT")}; + static const std::vector ALL_FROM{std::string("F"), std::string("FF")}; + static const std::vector ALL_TO{std::string(""), std::string("T"), std::string("TT"), std::string("TTT")}; - static void AssertSubstGlobal(const TString& sFrom, const TString& sTo, const TString& from, const TString& to, const size_t fromPos, const size_t numSubst) { - TString s = sFrom; + static void AssertSubstGlobal(const std::string& sFrom, const std::string& sTo, const std::string& from, const std::string& to, const size_t fromPos, const size_t numSubst) { + std::string s = sFrom; size_t res = SubstGlobal(s, from, to, fromPos); UNIT_ASSERT_VALUES_EQUAL_C(res, numSubst, TStringBuilder() << "numSubst=" << numSubst << ", fromPos=" << fromPos << ", " << sFrom << " -> " << sTo); @@ -31,7 +31,7 @@ Y_UNIT_TEST_SUITE(TStringSubst) { const size_t maxSz = fromSz + MIN_FROM_CTX; for (size_t sz = minSz; sz <= maxSz; ++sz) { for (size_t fromPos = 0; fromPos < sz; ++fromPos) { - TString s{sz, '.'}; + std::string s{sz, '.'}; for (const auto& to : ALL_TO) { AssertSubstGlobal(s, s, from, to, fromPos, 0); } @@ -49,9 +49,9 @@ Y_UNIT_TEST_SUITE(TStringSubst) { for (size_t fromPos = 0; fromPos <= sz - fromSz; ++fromPos) { for (size_t fromBeg = 0; fromBeg < fromPos; ++fromBeg) { const auto parts = { - TString{fromBeg, '.'}, - TString{sz - fromSz - fromBeg, '.'}}; - TString s = JoinSeq(from, parts); + std::string(fromBeg, '.'), + std::string(sz - fromSz - fromBeg, '.')}; + std::string s = JoinSeq(from, parts); for (const auto& to : ALL_TO) { AssertSubstGlobal(s, s, from, to, fromPos, 0); } @@ -61,8 +61,8 @@ Y_UNIT_TEST_SUITE(TStringSubst) { } } - static void DoTestSubstGlobal(std::vector& parts, const size_t minBeg, const size_t sz, - const TString& from, const size_t fromPos, const size_t numSubst) { + static void DoTestSubstGlobal(std::vector& parts, const size_t minBeg, const size_t sz, + const std::string& from, const size_t fromPos, const size_t numSubst) { const size_t numLeft = numSubst - parts.size(); for (size_t fromBeg = minBeg; fromBeg <= sz - numLeft * from.size(); ++fromBeg) { if (parts.empty()) { @@ -73,10 +73,10 @@ Y_UNIT_TEST_SUITE(TStringSubst) { if (numLeft == 1) { parts.emplace_back(sz - fromBeg - from.size(), '.'); - TString sFrom = JoinSeq(from, parts); + std::string sFrom = JoinSeq(from, parts); UNIT_ASSERT_VALUES_EQUAL_C(sFrom.size(), sz, sFrom); for (const auto& to : ALL_TO) { - TString sTo = JoinSeq(to, parts); + std::string sTo = JoinSeq(to, parts); AssertSubstGlobal(sFrom, sTo, from, to, fromPos, numSubst); } parts.pop_back(); @@ -89,7 +89,7 @@ Y_UNIT_TEST_SUITE(TStringSubst) { } static void DoTestSubstGlobal(size_t numSubst) { - std::vector parts; + std::vector parts; for (const auto& from : ALL_FROM) { const size_t fromSz = from.size(); const size_t minSz = numSubst * fromSz; @@ -120,64 +120,64 @@ Y_UNIT_TEST_SUITE(TStringSubst) { } Y_UNIT_TEST(TestSubstGlobalOld) { - TString s; + std::string s; s = "aaa"; SubstGlobal(s, "a", "bb"); - UNIT_ASSERT_EQUAL(s, TString("bbbbbb")); + UNIT_ASSERT_EQUAL(s, std::string("bbbbbb")); s = "aaa"; SubstGlobal(s, "a", "b"); - UNIT_ASSERT_EQUAL(s, TString("bbb")); + UNIT_ASSERT_EQUAL(s, std::string("bbb")); s = "aaa"; SubstGlobal(s, "a", ""); - UNIT_ASSERT_EQUAL(s, TString("")); + UNIT_ASSERT_EQUAL(s, std::string("")); s = "abcdefbcbcdfb"; SubstGlobal(s, "bc", "bbc", 2); - UNIT_ASSERT_EQUAL(s, TString("abcdefbbcbbcdfb")); + UNIT_ASSERT_EQUAL(s, std::string("abcdefbbcbbcdfb")); s = "Москва ~ Париж"; SubstGlobal(s, " ~ ", " "); - UNIT_ASSERT_EQUAL(s, TString("Москва Париж")); + UNIT_ASSERT_EQUAL(s, std::string("Москва Париж")); } Y_UNIT_TEST(TestSubstGlobalOldRet) { - const TString s1 = "aaa"; - const TString s2 = SubstGlobalCopy(s1, "a", "bb"); - UNIT_ASSERT_EQUAL(s2, TString("bbbbbb")); + const std::string s1 = "aaa"; + const std::string s2 = SubstGlobalCopy(s1, "a", "bb"); + UNIT_ASSERT_EQUAL(s2, std::string("bbbbbb")); - const TString s3 = "aaa"; - const TString s4 = SubstGlobalCopy(s3, "a", "b"); - UNIT_ASSERT_EQUAL(s4, TString("bbb")); + const std::string s3 = "aaa"; + const std::string s4 = SubstGlobalCopy(s3, "a", "b"); + UNIT_ASSERT_EQUAL(s4, std::string("bbb")); - const TString s5 = "aaa"; - const TString s6 = SubstGlobalCopy(s5, "a", ""); - UNIT_ASSERT_EQUAL(s6, TString("")); + const std::string s5 = "aaa"; + const std::string s6 = SubstGlobalCopy(s5, "a", ""); + UNIT_ASSERT_EQUAL(s6, std::string("")); - const TString s7 = "abcdefbcbcdfb"; - const TString s8 = SubstGlobalCopy(s7, "bc", "bbc", 2); - UNIT_ASSERT_EQUAL(s8, TString("abcdefbbcbbcdfb")); + const std::string s7 = "abcdefbcbcdfb"; + const std::string s8 = SubstGlobalCopy(s7, "bc", "bbc", 2); + UNIT_ASSERT_EQUAL(s8, std::string("abcdefbbcbbcdfb")); - const TString s9 = "Москва ~ Париж"; - const TString s10 = SubstGlobalCopy(s9, " ~ ", " "); - UNIT_ASSERT_EQUAL(s10, TString("Москва Париж")); + const std::string s9 = "Москва ~ Париж"; + const std::string s10 = SubstGlobalCopy(s9, " ~ ", " "); + UNIT_ASSERT_EQUAL(s10, std::string("Москва Париж")); } Y_UNIT_TEST(TestSubstCharGlobal) { - TUtf16String w = u"abcdabcd"; + std::u16string w = u"abcdabcd"; SubstGlobal(w, wchar16('b'), wchar16('B'), 3); UNIT_ASSERT_EQUAL(w, u"abcdaBcd"); - TString s = "aaa"; + std::string s = "aaa"; SubstGlobal(s, 'a', 'b', 1); - UNIT_ASSERT_EQUAL(s, TString("abb")); + UNIT_ASSERT_EQUAL(s, std::string("abb")); } Y_UNIT_TEST(TestSubstCharGlobalRet) { - const TUtf16String w1 = u"abcdabcd"; - const TUtf16String w2 = SubstGlobalCopy(w1, wchar16('b'), wchar16('B'), 3); + const std::u16string w1 = u"abcdabcd"; + const std::u16string w2 = SubstGlobalCopy(w1, wchar16('b'), wchar16('B'), 3); UNIT_ASSERT_EQUAL(w2, u"abcdaBcd"); - const TString s1 = "aaa"; - const TString s2 = SubstGlobalCopy(s1, 'a', 'b', 1); - UNIT_ASSERT_EQUAL(s2, TString("abb")); + const std::string s1 = "aaa"; + const std::string s2 = SubstGlobalCopy(s1, 'a', 'b', 1); + UNIT_ASSERT_EQUAL(s2, std::string("abb")); } Y_UNIT_TEST(TestSubstStdString) { @@ -194,58 +194,58 @@ Y_UNIT_TEST_SUITE(TStringSubst) { Y_UNIT_TEST(TestSubstGlobalChar) { { - const TString s = "a"; - const TString st = "b"; - TString ss = s; + const std::string s = "a"; + const std::string st = "b"; + std::string ss = s; UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b')); UNIT_ASSERT_VALUES_EQUAL(st, ss); } { - const TString s = "aa"; - const TString st = "bb"; - TString ss = s; + const std::string s = "aa"; + const std::string st = "bb"; + std::string ss = s; UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b')); UNIT_ASSERT_VALUES_EQUAL(st, ss); } { - const TString s = "aaa"; - const TString st = "bbb"; - TString ss = s; + const std::string s = "aaa"; + const std::string st = "bbb"; + std::string ss = s; UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b')); UNIT_ASSERT_VALUES_EQUAL(st, ss); } { - const TString s = "aaaa"; - const TString st = "bbbb"; - TString ss = s; + const std::string s = "aaaa"; + const std::string st = "bbbb"; + std::string ss = s; UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b')); UNIT_ASSERT_VALUES_EQUAL(st, ss); } { - const TString s = "aaaaa"; - const TString st = "bbbbb"; - TString ss = s; + const std::string s = "aaaaa"; + const std::string st = "bbbbb"; + std::string ss = s; UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b')); UNIT_ASSERT_VALUES_EQUAL(st, ss); } { - const TString s = "aaaaaa"; - const TString st = "bbbbbb"; - TString ss = s; + const std::string s = "aaaaaa"; + const std::string st = "bbbbbb"; + std::string ss = s; UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b')); UNIT_ASSERT_VALUES_EQUAL(st, ss); } { - const TString s = "aaaaaaa"; - const TString st = "bbbbbbb"; - TString ss = s; + const std::string s = "aaaaaaa"; + const std::string st = "bbbbbbb"; + std::string ss = s; UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b')); UNIT_ASSERT_VALUES_EQUAL(st, ss); } { - const TString s = "aaaaaaaa"; - const TString st = "bbbbbbbb"; - TString ss = s; + const std::string s = "aaaaaaaa"; + const std::string st = "bbbbbbbb"; + std::string ss = s; UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b')); UNIT_ASSERT_VALUES_EQUAL(st, ss); } diff --git a/util/string/type.cpp b/util/string/type.cpp index 49671c02c2c..27b28a3bdf9 100644 --- a/util/string/type.cpp +++ b/util/string/type.cpp @@ -24,11 +24,11 @@ static bool IsNumberT(const TStringType& s) noexcept { return std::all_of(s.begin(), s.end(), IsAsciiDigit); } -bool IsNumber(const TStringBuf s) noexcept { +bool IsNumber(const std::string_view s) noexcept { return IsNumberT(s); } -bool IsNumber(const TWtringBuf s) noexcept { +bool IsNumber(const std::u16string_view s) noexcept { return IsNumberT(s); } @@ -41,17 +41,17 @@ static bool IsHexNumberT(const TStringType& s) noexcept { return std::all_of(s.begin(), s.end(), IsAsciiHex); } -bool IsHexNumber(const TStringBuf s) noexcept { +bool IsHexNumber(const std::string_view s) noexcept { return IsHexNumberT(s); } -bool IsHexNumber(const TWtringBuf s) noexcept { +bool IsHexNumber(const std::u16string_view s) noexcept { return IsHexNumberT(s); } namespace { template - bool IsCaseInsensitiveAnyOf(TStringBuf str, const std::array& options) { + bool IsCaseInsensitiveAnyOf(std::string_view str, const std::array& options) { for (auto option : options) { if (str.size() == option.size() && ::strnicmp(str.data(), option.data(), str.size()) == 0) { return true; @@ -61,8 +61,8 @@ namespace { } } //anonymous namespace -bool IsTrue(const TStringBuf v) noexcept { - static constexpr std::array trueOptions{ +bool IsTrue(const std::string_view v) noexcept { + static constexpr std::array trueOptions{ "true", "t", "yes", @@ -73,8 +73,8 @@ bool IsTrue(const TStringBuf v) noexcept { return IsCaseInsensitiveAnyOf(v, trueOptions); } -bool IsFalse(const TStringBuf v) noexcept { - static constexpr std::array falseOptions{ +bool IsFalse(const std::string_view v) noexcept { + static constexpr std::array falseOptions{ "false", "f", "no", diff --git a/util/string/type.h b/util/string/type.h index d6cb29ea589..938d54a1ddd 100644 --- a/util/string/type.h +++ b/util/string/type.h @@ -1,23 +1,22 @@ #pragma once -#include Y_PURE_FUNCTION bool IsSpace(const char* s, size_t len) noexcept; /// Checks if a string is a set of only space symbols. -Y_PURE_FUNCTION static inline bool IsSpace(const TStringBuf s) noexcept { +Y_PURE_FUNCTION static inline bool IsSpace(const std::string_view s) noexcept { return IsSpace(s.data(), s.size()); } /// Returns "true" if the given string is an arabic number ([0-9]+) -Y_PURE_FUNCTION bool IsNumber(const TStringBuf s) noexcept; +Y_PURE_FUNCTION bool IsNumber(const std::string_view s) noexcept; -Y_PURE_FUNCTION bool IsNumber(const TWtringBuf s) noexcept; +Y_PURE_FUNCTION bool IsNumber(const std::u16string_view s) noexcept; /// Returns "true" if the given string is a hex number ([0-9a-fA-F]+) -Y_PURE_FUNCTION bool IsHexNumber(const TStringBuf s) noexcept; +Y_PURE_FUNCTION bool IsHexNumber(const std::string_view s) noexcept; -Y_PURE_FUNCTION bool IsHexNumber(const TWtringBuf s) noexcept; +Y_PURE_FUNCTION bool IsHexNumber(const std::u16string_view s) noexcept; /* Tests if the given string is case insensitive equal to one of: * - "true", @@ -28,7 +27,7 @@ Y_PURE_FUNCTION bool IsHexNumber(const TWtringBuf s) noexcept; * - "1", * - "da". */ -Y_PURE_FUNCTION bool IsTrue(const TStringBuf value) noexcept; +Y_PURE_FUNCTION bool IsTrue(const std::string_view value) noexcept; /* Tests if the given string is case insensitive equal to one of: * - "false", @@ -39,4 +38,4 @@ Y_PURE_FUNCTION bool IsTrue(const TStringBuf value) noexcept; * - "0", * - "net". */ -Y_PURE_FUNCTION bool IsFalse(const TStringBuf value) noexcept; +Y_PURE_FUNCTION bool IsFalse(const std::string_view value) noexcept; diff --git a/util/string/type_ut.cpp b/util/string/type_ut.cpp index 03e7af62bd0..779da13be55 100644 --- a/util/string/type_ut.cpp +++ b/util/string/type_ut.cpp @@ -62,7 +62,7 @@ Y_UNIT_TEST_SUITE(TStringClassify) { UNIT_ASSERT(IsHexNumber("1234567890a")); UNIT_ASSERT(!IsHexNumber("12345xx67890a")); UNIT_ASSERT(!IsHexNumber("foobar")); - UNIT_ASSERT(!IsHexNumber(TString())); + UNIT_ASSERT(!IsHexNumber(std::string())); UNIT_ASSERT(IsHexNumber(u"0")); UNIT_ASSERT(IsHexNumber(u"aaaadddAAAAA")); @@ -71,6 +71,6 @@ Y_UNIT_TEST_SUITE(TStringClassify) { UNIT_ASSERT(IsHexNumber(u"1234567890a")); UNIT_ASSERT(!IsHexNumber(u"12345xx67890a")); UNIT_ASSERT(!IsHexNumber(u"foobar")); - UNIT_ASSERT(!IsHexNumber(TUtf16String())); + UNIT_ASSERT(!IsHexNumber(std::u16string())); } } diff --git a/util/string/util.cpp b/util/string/util.cpp index b14f20bf75a..40f42670541 100644 --- a/util/string/util.cpp +++ b/util/string/util.cpp @@ -6,7 +6,7 @@ #include #include -int a2i(const TString& s) { +int a2i(const std::string& s) { return atoi(s.c_str()); } @@ -49,20 +49,20 @@ Tr::Tr(const char* from, const char* to) { } } -size_t Tr::FindFirstChangePosition(const TString& str) const { +size_t Tr::FindFirstChangePosition(const std::string& str) const { for (auto it = str.begin(); it != str.end(); ++it) { if (ConvertChar(*it) != *it) { return it - str.begin(); } } - return TString::npos; + return std::string::npos; } -void Tr::Do(TString& str) const { +void Tr::Do(std::string& str) const { const size_t changePosition = FindFirstChangePosition(str); - if (changePosition == TString::npos) { + if (changePosition == std::string::npos) { return; } diff --git a/util/string/util.h b/util/string/util.h index 0d77a5042b8..9e8bc9737d8 100644 --- a/util/string/util.h +++ b/util/string/util.h @@ -7,12 +7,10 @@ #include #include -#include -#include /// @addtogroup Strings_Miscellaneous /// @{ -int a2i(const TString& s); +int a2i(const std::string& s); /// Removes the last character if it is equal to c. template @@ -23,19 +21,19 @@ inline void RemoveIfLast(T& s, int c) { } /// Adds lastCh symbol to the the of the string if it is not already there. -inline void addIfNotLast(TString& s, int lastCh) { +inline void addIfNotLast(std::string& s, int lastCh) { size_t len = s.length(); if (!len || s[len - 1] != lastCh) { - s.append(char(lastCh)); + s.push_back(char(lastCh)); } } /// @details Finishes the string with lastCh1 if lastCh2 is not present in the string and lastCh1 is not already at the end of the string. /// Else, if lastCh2 is not equal to the symbol before the last, it finishes the string with lastCh2. /// @todo ?? Define, when to apply the function. Is in use several times for URLs parsing. -inline void addIfAbsent(TString& s, char lastCh1, char lastCh2) { +inline void addIfAbsent(std::string& s, char lastCh1, char lastCh2) { size_t pos = s.find(lastCh2); - if (pos == TString::npos) { + if (pos == std::string::npos) { //s.append((char)lastCh1); addIfNotLast(s, lastCh1); } else if (pos < s.length() - 1) { @@ -173,17 +171,17 @@ class Tr { for (size_t i = 0; i < l && s[i]; i++) s[i] = ConvertChar(s[i]); } - void Do(TString& str) const; + void Do(std::string& str) const; private: char Map[256]; - size_t FindFirstChangePosition(const TString& str) const; + size_t FindFirstChangePosition(const std::string& str) const; }; // Removes all occurrences of given character from string template -void RemoveAll(TStringType& str, typename TStringType::char_type ch) { +void RemoveAll(TStringType& str, typename TStringType::value_type ch) { size_t pos = str.find(ch); // 'find' to avoid cloning of string in 'TString.begin()' if (pos == TStringType::npos) return; diff --git a/util/string/util_ut.cpp b/util/string/util_ut.cpp index 18a2d8e1957..d000e4a1ee6 100644 --- a/util/string/util_ut.cpp +++ b/util/string/util_ut.cpp @@ -36,7 +36,7 @@ class TStrUtilTest: public TTestBase { }; for (const T* t = tests; t != std::end(tests); ++t) { - TString str(t->Str); + std::string str(t->Str); RemoveAll(str, t->Ch); UNIT_ASSERT_EQUAL(t->Result, str); } diff --git a/util/string/vector.cpp b/util/string/vector.cpp index add2eac0fd9..9967815fef3 100644 --- a/util/string/vector.cpp +++ b/util/string/vector.cpp @@ -5,12 +5,12 @@ #include template -static inline void DoSplit2(TConsumer& c, TDelim& d, const TBasicStringBuf str, int) { +static inline void DoSplit2(TConsumer& c, TDelim& d, const std::basic_string_view str, int) { SplitString(str.data(), str.data() + str.size(), d, c); } template -static inline void DoSplit1(TConsumer& cc, TDelim& d, const TBasicStringBuf str, int opts) { +static inline void DoSplit1(TConsumer& cc, TDelim& d, const std::basic_string_view str, int opts) { if (opts & KEEP_EMPTY_TOKENS) { DoSplit2(cc, d, str, opts); } else { @@ -21,8 +21,8 @@ static inline void DoSplit1(TConsumer& cc, TDelim& d, const TBasicStringBuf -static inline void DoSplit0(C* res, const TBasicStringBuf str, TDelim& d, size_t maxFields, int options) { - using TStringType = std::conditional_t::value, TUtf16String, TString>; +static inline void DoSplit0(C* res, const std::basic_string_view str, TDelim& d, size_t maxFields, int options) { + using TStringType = std::conditional_t::value, std::u16string, std::string>; res->clear(); if (!str.data()) { @@ -46,8 +46,8 @@ static inline void DoSplit0(C* res, const TBasicStringBuf str, TDelim& d, } template -static void SplitStringImplT(std::vector::value, TUtf16String, TString>>* res, - const TBasicStringBuf str, const TChr* delim, size_t maxFields, int options) { +static void SplitStringImplT(std::vector::value, std::u16string, std::string>>* res, + const std::basic_string_view str, const TChr* delim, size_t maxFields, int options) { if (!*delim) { return; } @@ -63,27 +63,27 @@ static void SplitStringImplT(std::vector* res, const char* ptr, const char* delim, size_t maxFields, int options) { - return SplitStringImplT(res, TStringBuf(ptr), delim, maxFields, options); +void ::NPrivate::SplitStringImpl(std::vector* res, const char* ptr, const char* delim, size_t maxFields, int options) { + return SplitStringImplT(res, std::string_view(ptr), delim, maxFields, options); } -void ::NPrivate::SplitStringImpl(std::vector* res, const char* ptr, size_t len, const char* delim, size_t maxFields, int options) { - return SplitStringImplT(res, TStringBuf(ptr, len), delim, maxFields, options); +void ::NPrivate::SplitStringImpl(std::vector* res, const char* ptr, size_t len, const char* delim, size_t maxFields, int options) { + return SplitStringImplT(res, std::string_view(ptr, len), delim, maxFields, options); } -void ::NPrivate::SplitStringImpl(std::vector* res, const wchar16* ptr, const wchar16* delimiter, size_t maxFields, int options) { - return SplitStringImplT(res, TWtringBuf(ptr), delimiter, maxFields, options); +void ::NPrivate::SplitStringImpl(std::vector* res, const wchar16* ptr, const wchar16* delimiter, size_t maxFields, int options) { + return SplitStringImplT(res, std::u16string_view(ptr), delimiter, maxFields, options); } -void ::NPrivate::SplitStringImpl(std::vector* res, const wchar16* ptr, size_t len, const wchar16* delimiter, size_t maxFields, int options) { - return SplitStringImplT(res, TWtringBuf(ptr, len), delimiter, maxFields, options); +void ::NPrivate::SplitStringImpl(std::vector* res, const wchar16* ptr, size_t len, const wchar16* delimiter, size_t maxFields, int options) { + return SplitStringImplT(res, std::u16string_view(ptr, len), delimiter, maxFields, options); } -TUtf16String JoinStrings(const std::vector& v, const TWtringBuf delim) { +std::u16string JoinStrings(const std::vector& v, const std::u16string_view delim) { return JoinStrings(v.begin(), v.end(), delim); } -TUtf16String JoinStrings(const std::vector& v, size_t index, size_t count, const TWtringBuf delim) { +std::u16string JoinStrings(const std::vector& v, size_t index, size_t count, const std::u16string_view delim) { const size_t f = Min(index, v.size()); const size_t l = f + Min(count, v.size() - f); diff --git a/util/string/vector.h b/util/string/vector.h index c5a6c6f6992..bd4dc967855 100644 --- a/util/string/vector.h +++ b/util/string/vector.h @@ -3,10 +3,6 @@ #include "cast.h" #include "split.h" -#include -#include -#include -#include #include #include @@ -17,14 +13,14 @@ namespace NPrivate { - void SplitStringImpl(std::vector* res, const char* ptr, + void SplitStringImpl(std::vector* res, const char* ptr, const char* delimiter, size_t maxFields, int options); - void SplitStringImpl(std::vector* res, const char* ptr, size_t len, + void SplitStringImpl(std::vector* res, const char* ptr, size_t len, const char* delimiter, size_t maxFields, int options); - void SplitStringImpl(std::vector* res, const wchar16* ptr, + void SplitStringImpl(std::vector* res, const wchar16* ptr, const wchar16* delimiter, size_t maxFields, int options); - void SplitStringImpl(std::vector* res, const wchar16* ptr, size_t len, + void SplitStringImpl(std::vector* res, const wchar16* ptr, size_t len, const wchar16* delimiter, size_t maxFields, int options); template @@ -32,12 +28,12 @@ namespace NPrivate { template <> struct TStringDeducer { - using type = TString; + using type = std::string; }; template <> struct TStringDeducer { - using type = TUtf16String; + using type = std::u16string; }; } @@ -67,11 +63,11 @@ SplitString(const typename ::NPrivate::TStringDeducer::type& str, const C* de } template -inline TString JoinStrings(TIter begin, TIter end, const TStringBuf delim) { +inline std::string JoinStrings(TIter begin, TIter end, const std::string_view delim) { if (begin == end) - return TString(); + return std::string(); - TString result = ToString(*begin); + std::string result = ToString(*begin); for (++begin; begin != end; ++begin) { result.append(delim); @@ -82,11 +78,11 @@ inline TString JoinStrings(TIter begin, TIter end, const TStringBuf delim) { } template -inline TUtf16String JoinStrings(TIter begin, TIter end, const TWtringBuf delim) { +inline std::u16string JoinStrings(TIter begin, TIter end, const std::u16string_view delim) { if (begin == end) - return TUtf16String(); + return std::u16string(); - TUtf16String result = ToWtring(*begin); + std::u16string result = ToWtring(*begin); for (++begin; begin != end; ++begin) { result.append(delim); @@ -96,29 +92,29 @@ inline TUtf16String JoinStrings(TIter begin, TIter end, const TWtringBuf delim) return result; } -/// Concatenates elements of given std::vector. -inline TString JoinStrings(const std::vector& v, const TStringBuf delim) { +/// Concatenates elements of given std::vector. +inline std::string JoinStrings(const std::vector& v, const std::string_view delim) { return JoinStrings(v.begin(), v.end(), delim); } -inline TString JoinStrings(const std::vector& v, size_t index, size_t count, const TStringBuf delim) { +inline std::string JoinStrings(const std::vector& v, size_t index, size_t count, const std::string_view delim) { Y_ASSERT(index + count <= v.size() && "JoinStrings(): index or count out of range"); return JoinStrings(v.begin() + index, v.begin() + index + count, delim); } template -inline TString JoinVectorIntoString(const std::vector& v, const TStringBuf delim) { +inline std::string JoinVectorIntoString(const std::vector& v, const std::string_view delim) { return JoinStrings(v.begin(), v.end(), delim); } template -inline TString JoinVectorIntoString(const std::vector& v, size_t index, size_t count, const TStringBuf delim) { +inline std::string JoinVectorIntoString(const std::vector& v, size_t index, size_t count, const std::string_view delim) { Y_ASSERT(index + count <= v.size() && "JoinVectorIntoString(): index or count out of range"); return JoinStrings(v.begin() + index, v.begin() + index + count, delim); } -TUtf16String JoinStrings(const std::vector& v, const TWtringBuf delim); -TUtf16String JoinStrings(const std::vector& v, size_t index, size_t count, const TWtringBuf delim); +std::u16string JoinStrings(const std::vector& v, const std::u16string_view delim); +std::u16string JoinStrings(const std::vector& v, size_t index, size_t count, const std::u16string_view delim); //! Converts vector of strings to vector of type T variables template diff --git a/util/string/vector_ut.cpp b/util/string/vector_ut.cpp index 3a692fe9771..a095b7320f2 100644 --- a/util/string/vector_ut.cpp +++ b/util/string/vector_ut.cpp @@ -5,13 +5,13 @@ Y_UNIT_TEST_SUITE(TStringJoinTest) { Y_UNIT_TEST(Test1) { - std::vector v; + std::vector v; UNIT_ASSERT_EQUAL(JoinStrings(v, ToWtring("")), ToWtring("")); } Y_UNIT_TEST(Test2) { - std::vector v; + std::vector v; v.push_back(ToWtring("1")); v.push_back(ToWtring("2")); @@ -20,7 +20,7 @@ Y_UNIT_TEST_SUITE(TStringJoinTest) { } Y_UNIT_TEST(Test3) { - std::vector v; + std::vector v; v.push_back(ToWtring("1")); v.push_back(ToWtring("2")); @@ -29,9 +29,9 @@ Y_UNIT_TEST_SUITE(TStringJoinTest) { } Y_UNIT_TEST(TestJoinWStrings) { - const TUtf16String str = u"Яндекс"; - const std::vector v(1, str); + const std::u16string str = u"Яндекс"; + const std::vector v(1, str); - UNIT_ASSERT_EQUAL(JoinStrings(v, TUtf16String()), str); + UNIT_ASSERT_EQUAL(JoinStrings(v, std::u16string()), str); } } diff --git a/util/system/atexit.cpp b/util/system/atexit.cpp index f761cdcebf6..47d872635f9 100644 --- a/util/system/atexit.cpp +++ b/util/system/atexit.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -59,10 +60,9 @@ namespace { } inline void Register(TAtExitFunc func, void* ctx, size_t priority) { - with_lock (Lock_) { - Store_.push_back({func, ctx, priority, Store_.size()}); - Items_.push(&Store_.back()); - } + std::lock_guard guard(Lock_); + Store_.push_back({func, ctx, priority, Store_.size()}); + Items_.push(&Store_.back()); } inline bool FinishStarted() const { @@ -92,15 +92,14 @@ namespace { if (TAtExit* const atExit = atExitPtr.load(std::memory_order_acquire)) { return atExit; } - with_lock (atExitLock) { - if (TAtExit* const atExit = atExitPtr.load()) { - return atExit; - } - atexit(OnExit); - TAtExit* const atExit = new (atExitMem) TAtExit; - atExitPtr.store(atExit, std::memory_order_release); + std::lock_guard guard(atExitLock); + if (TAtExit* const atExit = atExitPtr.load()) { return atExit; } + atexit(OnExit); + TAtExit* const atExit = new (atExitMem) TAtExit; + atExitPtr.store(atExit, std::memory_order_release); + return atExit; } } diff --git a/util/system/backtrace.cpp b/util/system/backtrace.cpp index f8d37e8443c..84ae8975a0c 100644 --- a/util/system/backtrace.cpp +++ b/util/system/backtrace.cpp @@ -5,10 +5,9 @@ #include #include #include -#include #ifdef _win_ - #include "mutex.h" + #include #ifndef OPTIONAL #define OPTIONAL @@ -183,7 +182,7 @@ namespace { } TResolvedSymbol Resolve(void* sym, char* buf, size_t len) { - TGuard guard(Mutex); + std::lock_guard guard(Mutex); TResolvedSymbol ret = { "??", @@ -209,7 +208,7 @@ namespace { return ret; } - TMutex Mutex; + std::mutex Mutex; HMODULE Library; TSymInitializeFunc SymInitializeFunc; TSymCleanupFunc SymCleanupFunc; @@ -277,7 +276,7 @@ void TBackTrace::PrintTo(IOutputStream& out) const { FormatBackTraceFn(&out, Data, Size); } -TString TBackTrace::PrintToString() const { +std::string TBackTrace::PrintToString() const { TStringStream ss; PrintTo(ss); return ss.Str(); diff --git a/util/system/backtrace.h b/util/system/backtrace.h index 2fce7585c3a..4193621747c 100644 --- a/util/system/backtrace.h +++ b/util/system/backtrace.h @@ -35,7 +35,7 @@ class TBackTrace { TBackTrace(); void Capture(); void PrintTo(IOutputStream&) const; - TString PrintToString() const; + std::string PrintToString() const; size_t size() const; const void* const* data() const; operator TBackTraceView() const; diff --git a/util/system/compat.cpp b/util/system/compat.cpp index 5b9b17f53f9..585a64092bc 100644 --- a/util/system/compat.cpp +++ b/util/system/compat.cpp @@ -1,7 +1,6 @@ #include "compat.h" #include "progname.h" -#include #ifdef _win_ #include "winint.h" diff --git a/util/system/compiler.h b/util/system/compiler.h index d7fb18a7759..1051aac591b 100644 --- a/util/system/compiler.h +++ b/util/system/compiler.h @@ -36,7 +36,7 @@ * int main() { * Hi(); // void Hi() * Do(); // void Do() [T = int] - * Do(); // void Do() [T = TString] + * Do(); // void Do() [T = TString] * } * @endcode */ diff --git a/util/system/condvar.cpp b/util/system/condvar.cpp deleted file mode 100644 index b4d748e4e96..00000000000 --- a/util/system/condvar.cpp +++ /dev/null @@ -1,145 +0,0 @@ -#include "event.h" -#include "mutex.h" -#include "yassert.h" -#include "condvar.h" -#include "datetime.h" -#include "spinlock.h" - -#include -#include -#include - -#if defined(_unix_) - #include - #include - #include -#endif - -namespace { - class TCondVarImpl { - using TLock = TAdaptiveLock; - - struct TWaitEvent: public TIntrusiveListItem, public TSystemEvent { - }; - - using TWaitEvents = TIntrusiveList; - - public: - inline ~TCondVarImpl() { - Y_ASSERT(Events_.Empty()); - } - - inline void Signal() noexcept { - with_lock (Lock_) { - if (!Events_.Empty()) { - Events_.PopFront()->Signal(); - } - } - } - - inline void BroadCast() noexcept { - with_lock (Lock_) { - //TODO - while (!Events_.Empty()) { - Events_.PopFront()->Signal(); - } - } - } - - inline bool WaitD(TMutex& m, TInstant deadLine) noexcept { - TWaitEvent event; - - with_lock (Lock_) { - Events_.PushBack(&event); - } - - m.Release(); - - const bool signalled = event.WaitD(deadLine); - - m.Acquire(); - - with_lock (Lock_) { - event.Unlink(); - } - - return signalled; - } - - private: - TWaitEvents Events_; - TLock Lock_; - }; -} - -#if defined(_win_) -class TCondVar::TImpl: public TCondVarImpl { -}; -#else -class TCondVar::TImpl { -public: - inline TImpl() { - if (pthread_cond_init(&Cond_, nullptr)) { - ythrow yexception() << "can not create condvar(" << LastSystemErrorText() << ")"; - } - } - - inline ~TImpl() { - int ret = pthread_cond_destroy(&Cond_); - Y_ABORT_UNLESS(ret == 0, "pthread_cond_destroy failed: %s", LastSystemErrorText(ret)); - } - - inline void Signal() noexcept { - int ret = pthread_cond_signal(&Cond_); - Y_ABORT_UNLESS(ret == 0, "pthread_cond_signal failed: %s", LastSystemErrorText(ret)); - } - - inline bool WaitD(TMutex& lock, TInstant deadLine) noexcept { - if (deadLine == TInstant::Max()) { - int ret = pthread_cond_wait(&Cond_, (pthread_mutex_t*)lock.Handle()); - Y_ABORT_UNLESS(ret == 0, "pthread_cond_wait failed: %s", LastSystemErrorText(ret)); - return true; - } else { - struct timespec spec; - - Zero(spec); - - spec.tv_sec = deadLine.Seconds(); - spec.tv_nsec = deadLine.NanoSecondsOfSecond(); - - int ret = pthread_cond_timedwait(&Cond_, (pthread_mutex_t*)lock.Handle(), &spec); - - Y_ABORT_UNLESS(ret == 0 || ret == ETIMEDOUT, "pthread_cond_timedwait failed: %s", LastSystemErrorText(ret)); - - return ret == 0; - } - } - - inline void BroadCast() noexcept { - int ret = pthread_cond_broadcast(&Cond_); - Y_ABORT_UNLESS(ret == 0, "pthread_cond_broadcast failed: %s", LastSystemErrorText(ret)); - } - -private: - pthread_cond_t Cond_; -}; -#endif - -TCondVar::TCondVar() - : Impl_(new TImpl) -{ -} - -TCondVar::~TCondVar() = default; - -void TCondVar::BroadCast() noexcept { - Impl_->BroadCast(); -} - -void TCondVar::Signal() noexcept { - Impl_->Signal(); -} - -bool TCondVar::WaitD(TMutex& mutex, TInstant deadLine) noexcept { - return Impl_->WaitD(mutex, deadLine); -} diff --git a/util/system/condvar.h b/util/system/condvar.h deleted file mode 100644 index 569162717cb..00000000000 --- a/util/system/condvar.h +++ /dev/null @@ -1,71 +0,0 @@ -#pragma once - -#include "mutex.h" - -#include -#include -#include - -#include - -class TCondVar { -public: - TCondVar(); - ~TCondVar(); - - void BroadCast() noexcept; - void Signal() noexcept; - - /* - * returns false if failed by timeout - */ - bool WaitD(TMutex& m, TInstant deadline) noexcept; - - template - inline bool WaitD(TMutex& m, TInstant deadline, P pred) noexcept { - while (!pred()) { - if (!WaitD(m, deadline)) { - return pred(); - } - } - return true; - } - - /* - * returns false if failed by timeout - */ - inline bool WaitT(TMutex& m, TDuration timeout) noexcept { - return WaitD(m, timeout.ToDeadLine()); - } - - template - inline bool WaitT(TMutex& m, TDuration timeout, P pred) noexcept { - return WaitD(m, timeout.ToDeadLine(), std::move(pred)); - } - - /* - * infinite wait - */ - inline void WaitI(TMutex& m) noexcept { - WaitD(m, TInstant::Max()); - } - - template - inline void WaitI(TMutex& m, P pred) noexcept { - WaitD(m, TInstant::Max(), std::move(pred)); - } - - //deprecated - inline void Wait(TMutex& m) noexcept { - WaitI(m); - } - - template - inline void Wait(TMutex& m, P pred) noexcept { - WaitI(m, std::move(pred)); - } - -private: - class TImpl; - THolder Impl_; -}; diff --git a/util/system/condvar_ut.cpp b/util/system/condvar_ut.cpp deleted file mode 100644 index c6cce1c0a7a..00000000000 --- a/util/system/condvar_ut.cpp +++ /dev/null @@ -1,191 +0,0 @@ -#include "mutex.h" -#include "guard.h" -#include "condvar.h" - -#include - -#include - -#include - -class TCondVarTest: public TTestBase { - UNIT_TEST_SUITE(TCondVarTest); - UNIT_TEST(TestBasics) - UNIT_TEST(TestSyncronize) - UNIT_TEST_SUITE_END(); - - struct TSharedData { - TMutex mutex; - TCondVar condVar1; - TCondVar condVar2; - - std::atomic stopWaiting = false; - - std::atomic in = 0; - std::atomic out = 0; - - std::atomic waited = 0; - - bool failed = false; - }; - - class TThreadTask: public IObjectInQueue { - public: - using PFunc = void (TThreadTask::*)(void); - - TThreadTask(PFunc func, size_t id, size_t totalIds, TSharedData& data) - : Func_(func) - , Id_(id) - , TotalIds_(totalIds) - , Data_(data) - { - } - - void Process(void*) override { - THolder This(this); - - (this->*Func_)(); - } - -#define FAIL_ASSERT(cond) \ - if (!(cond)) { \ - Data_.failed = true; \ - } - void RunBasics() { - Y_ASSERT(TotalIds_ == 3); - - if (Id_ < 2) { - TGuard guard(Data_.mutex); - while (!Data_.stopWaiting.load()) { - bool res = Data_.condVar1.WaitT(Data_.mutex, TDuration::Seconds(1)); - FAIL_ASSERT(res == true); - } - } else { - usleep(100000); - Data_.stopWaiting.store(true); - - TGuard guard(Data_.mutex); - Data_.condVar1.Signal(); - Data_.condVar1.Signal(); - } - } - - void RunBasicsWithPredicate() { - Y_ASSERT(TotalIds_ == 3); - - if (Id_ < 2) { - TGuard guard(Data_.mutex); - const auto res = Data_.condVar1.WaitT(Data_.mutex, TDuration::Seconds(1), [&] { - return Data_.stopWaiting.load(); - }); - FAIL_ASSERT(res == true); - } else { - usleep(100000); - Data_.stopWaiting.store(true); - - TGuard guard(Data_.mutex); - Data_.condVar1.Signal(); - Data_.condVar1.Signal(); - } - } - - void RunSyncronize() { - for (size_t i = 0; i < 10; ++i) { - TGuard guard(Data_.mutex); - ++Data_.in; - if (Data_.in.load() == TotalIds_) { - Data_.out.store(0); - Data_.condVar1.BroadCast(); - } else { - ++Data_.waited; - while (Data_.in.load() < TotalIds_) { - bool res = Data_.condVar1.WaitT(Data_.mutex, TDuration::Seconds(1)); - FAIL_ASSERT(res == true); - } - } - - ++Data_.out; - if (Data_.out.load() == TotalIds_) { - Data_.in.store(0); - Data_.condVar2.BroadCast(); - } else { - while (Data_.out.load() < TotalIds_) { - bool res = Data_.condVar2.WaitT(Data_.mutex, TDuration::Seconds(1)); - FAIL_ASSERT(res == true); - } - } - } - - FAIL_ASSERT(Data_.waited.load() == (TotalIds_ - 1) * 10); - } - - void RunSyncronizeWithPredicate() { - for (size_t i = 0; i < 10; ++i) { - TGuard guard(Data_.mutex); - ++Data_.in; - if (Data_.in.load() == TotalIds_) { - Data_.out.store(0); - Data_.condVar1.BroadCast(); - } else { - ++Data_.waited; - const auto res = Data_.condVar1.WaitT(Data_.mutex, TDuration::Seconds(1), [&] { - return Data_.in.load() >= TotalIds_; - }); - FAIL_ASSERT(res == true); - } - - ++Data_.out; - if (Data_.out.load() == TotalIds_) { - Data_.in.store(0); - Data_.condVar2.BroadCast(); - } else { - const auto res = Data_.condVar2.WaitT(Data_.mutex, TDuration::Seconds(1), [&] { - return Data_.out.load() >= TotalIds_; - }); - FAIL_ASSERT(res == true); - } - } - - FAIL_ASSERT(Data_.waited == (TotalIds_ - 1) * 10); - } -#undef FAIL_ASSERT - - private: - PFunc Func_; - size_t Id_; - size_t TotalIds_; - TSharedData& Data_; - }; - -private: -#define RUN_CYCLE(what, count) \ - Q_.Start(count); \ - for (size_t i = 0; i < count; ++i) { \ - UNIT_ASSERT(Q_.Add(new TThreadTask(&TThreadTask::what, i, count, Data_))); \ - } \ - Q_.Stop(); \ - bool b = Data_.failed; \ - Data_.failed = false; \ - UNIT_ASSERT(!b); - - inline void TestBasics() { - RUN_CYCLE(RunBasics, 3); - } - - inline void TestBasicsWithPredicate() { - RUN_CYCLE(RunBasicsWithPredicate, 3); - } - - inline void TestSyncronize() { - RUN_CYCLE(RunSyncronize, 6); - } - - inline void TestSyncronizeWithPredicate() { - RUN_CYCLE(RunSyncronizeWithPredicate, 6); - } -#undef RUN_CYCLE - TSharedData Data_; - TThreadPool Q_; -}; - -UNIT_TEST_SUITE_REGISTRATION(TCondVarTest); diff --git a/util/system/context.cpp b/util/system/context.cpp index ad993090884..cd03d0e1dcf 100644 --- a/util/system/context.cpp +++ b/util/system/context.cpp @@ -221,14 +221,14 @@ TContMachineContext::TContMachineContext() : Fiber_(ConvertThreadToFiber(this)) , MainFiber_(true) { - Y_ENSURE(Fiber_, TStringBuf("fiber error")); + Y_ENSURE(Fiber_, std::string_view("fiber error")); } TContMachineContext::TContMachineContext(const TContClosure& c) : Fiber_(CreateFiber(c.Stack.size(), (LPFIBER_START_ROUTINE)ContextTrampoLine, (LPVOID)c.TrampoLine)) , MainFiber_(false) { - Y_ENSURE(Fiber_, TStringBuf("fiber error")); + Y_ENSURE(Fiber_, std::string_view("fiber error")); } TContMachineContext::~TContMachineContext() { diff --git a/util/system/demangle_impl.h b/util/system/demangle_impl.h index 081004f976b..d3e44c571bf 100644 --- a/util/system/demangle_impl.h +++ b/util/system/demangle_impl.h @@ -1,7 +1,6 @@ #pragma once #include -#include namespace NPrivate { diff --git a/util/system/direct_io.cpp b/util/system/direct_io.cpp index 1b6c39f42ca..e0069a10501 100644 --- a/util/system/direct_io.cpp +++ b/util/system/direct_io.cpp @@ -1,5 +1,7 @@ #include "direct_io.h" +#include + #include #include #include @@ -21,12 +23,12 @@ namespace { Y_ABORT_UNLESS(!uname(&sysInfo), "Error while call uname: %s", LastSystemErrorText()); - TStringBuf release(sysInfo.release); + std::string_view release(sysInfo.release); release = release.substr(0, release.find_first_not_of(".0123456789")); - int v1 = FromString(release.NextTok('.')); - int v2 = FromString(release.NextTok('.')); - int v3 = FromString(release.NextTok('.')); + int v1 = FromString(NUtils::NextTok(release, '.')); + int v2 = FromString(NUtils::NextTok(release, '.')); + int v3 = FromString(NUtils::NextTok(release, '.')); int linuxVersionCode = KERNEL_VERSION(v1, v2, v3); if (linuxVersionCode < KERNEL_VERSION(2, 4, 10)) { @@ -46,7 +48,7 @@ namespace { }; } -TDirectIOBufferedFile::TDirectIOBufferedFile(const TString& path, EOpenMode oMode, size_t buflen /*= 1 << 17*/) +TDirectIOBufferedFile::TDirectIOBufferedFile(const std::string& path, EOpenMode oMode, size_t buflen /*= 1 << 17*/) : File(path, oMode) , Alignment(0) , DataLen(0) diff --git a/util/system/direct_io.h b/util/system/direct_io.h index 6a3325a9608..46829a55fe8 100644 --- a/util/system/direct_io.h +++ b/util/system/direct_io.h @@ -11,7 +11,7 @@ // - Supports writing odd sized files by turning off direct IO for the last chunk. class TDirectIOBufferedFile { public: - TDirectIOBufferedFile(const TString& path, EOpenMode oMode, size_t buflen = 1 << 17); + TDirectIOBufferedFile(const std::string& path, EOpenMode oMode, size_t buflen = 1 << 17); ~TDirectIOBufferedFile(); void FlushData(); diff --git a/util/system/direct_io_ut.cpp b/util/system/direct_io_ut.cpp index 231d0749746..e26f76e1de4 100644 --- a/util/system/direct_io_ut.cpp +++ b/util/system/direct_io_ut.cpp @@ -97,14 +97,14 @@ Y_UNIT_TEST_SUITE(TDirectIoErrorHandling) { Y_UNIT_TEST(WritingReadOnlyFileBufferFlushed) { // Note the absence of Direct TDirectIOBufferedFile file(FileName_, RdOnly | OpenAlways, 1); - TString buffer = "Hello"; + std::string buffer = "Hello"; UNIT_ASSERT_EXCEPTION(file.Write(buffer.data(), buffer.size()), TFileError); NFs::Remove(FileName_); } Y_UNIT_TEST(WritingReadOnlyFileAllInBuffer) { TDirectIOBufferedFile file(FileName_, RdOnly | Direct | Seq | OpenAlways, 1 << 15); - TString buffer = "Hello"; + std::string buffer = "Hello"; // Doesn't throw because of buffering. file.Write(buffer.data(), buffer.size()); diff --git a/util/system/dynlib.cpp b/util/system/dynlib.cpp index 9d2541c25f1..f7742c8921d 100644 --- a/util/system/dynlib.cpp +++ b/util/system/dynlib.cpp @@ -1,10 +1,11 @@ #include "dynlib.h" #include "guard.h" -#include "mutex.h" #include #include +#include + #ifdef _win32_ #include "winint.h" @@ -25,7 +26,7 @@ using HINSTANCE = void*; #define DLLSYM(hndl, name) dlsym(hndl, name) #endif -inline TString DLLERR() { +inline std::string DLLERR() { #ifdef _unix_ return dlerror(); #endif @@ -38,7 +39,7 @@ inline TString DLLERR() { return "DLLERR() unknown error"; while (cnt && isspace(msg[cnt - 1])) --cnt; - TString err(msg, 0, cnt); + std::string err(msg, 0, cnt); LocalFree(msg); return err; #endif @@ -57,12 +58,12 @@ class TDynamicLibrary::TImpl { } } - class TCreateMutex: public TMutex { + class TCreateMutex: public std::mutex { }; public: static inline TImpl* SafeCreate(const char* path, int flags) { - auto guard = Guard(*Singleton()); + std::lock_guard guard(*Singleton()); return new TImpl(path, flags); } @@ -99,7 +100,7 @@ class TDynamicLibrary::TImpl { TDynamicLibrary::TDynamicLibrary() noexcept { } -TDynamicLibrary::TDynamicLibrary(const TString& path, int flags) { +TDynamicLibrary::TDynamicLibrary(const std::string& path, int flags) { Open(path.data(), flags); } diff --git a/util/system/dynlib.h b/util/system/dynlib.h index 66eaf4a5c1c..782a8cccc81 100644 --- a/util/system/dynlib.h +++ b/util/system/dynlib.h @@ -3,7 +3,6 @@ #include "defaults.h" #include -#include #define Y_GET_FUNC(dll, name) FUNC_##name((dll).Sym(#name)) #define Y_GET_FUNC_OPTIONAL(dll, name) FUNC_##name((dll).SymOptional(#name)) @@ -23,7 +22,7 @@ class TDynamicLibrary { public: TDynamicLibrary() noexcept; - TDynamicLibrary(const TString& path, int flags = DEFAULT_DLLOPEN_FLAGS); + TDynamicLibrary(const std::string& path, int flags = DEFAULT_DLLOPEN_FLAGS); ~TDynamicLibrary(); void Open(const char* path, int flags = DEFAULT_DLLOPEN_FLAGS); @@ -44,8 +43,8 @@ class TExternalSymbol { private: TLib* PLib; TDynamicLibrary* DLib; - TString lname; - TString vname; + std::string lname; + std::string vname; public: TExternalSymbol() noexcept { @@ -98,16 +97,16 @@ class TExternalSymbol { delete DLib; DLib = 0; PLib = 0; - lname.remove(); - vname.remove(); + lname.erase(); + vname.erase(); } TLib* Symbol() const noexcept { return PLib; } - const TString& LibName() const noexcept { + const std::string& LibName() const noexcept { return lname; } - const TString& VtblName() const noexcept { + const std::string& VtblName() const noexcept { return vname; } bool IsStatic() const noexcept { diff --git a/util/system/env.cpp b/util/system/env.cpp index 98d9682b36f..79d0b8d3ca2 100644 --- a/util/system/env.cpp +++ b/util/system/env.cpp @@ -1,6 +1,5 @@ #include "env.h" -#include #include #ifdef _win_ @@ -21,7 +20,7 @@ * - https://a.yandex-team.ru/review/108892/details */ -TString GetEnv(const TString& key, const TString& def) { +std::string GetEnv(const std::string& key, const std::string& def) { #ifdef _win_ size_t len = GetEnvironmentVariableA(key.data(), nullptr, 0); @@ -29,7 +28,7 @@ TString GetEnv(const TString& key, const TString& def) { if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) { return def; } - return TString{}; + return std::string{}; } std::vector buffer(len); @@ -42,14 +41,14 @@ TString GetEnv(const TString& key, const TString& def) { } } while (len > bufferSize); - return TString(buffer.data(), len); + return std::string(buffer.data(), len); #else const char* env = getenv(key.data()); - return env ? TString(env) : def; + return env ? std::string(env) : def; #endif } -void SetEnv(const TString& key, const TString& value) { +void SetEnv(const std::string& key, const std::string& value) { bool isOk = false; int errorCode = 0; #ifdef _win_ diff --git a/util/system/env.h b/util/system/env.h index e2ccdd1e954..dd45b4ba8ab 100644 --- a/util/system/env.h +++ b/util/system/env.h @@ -1,6 +1,5 @@ #pragma once -#include /** * Search the environment list provided by the host environment for associated variable. @@ -16,7 +15,7 @@ * @note Calls to `GetEnv` and `SetEnv` from different threads must be synchronized. * @see SetEnv */ -TString GetEnv(const TString& key, const TString& def = TString()); +std::string GetEnv(const std::string& key, const std::string& def = std::string()); /** * Add or change environment variable provided by the host environment. @@ -29,4 +28,4 @@ TString GetEnv(const TString& key, const TString& def = TString()); * @note Calls to `GetEnv` and `SetEnv` from different threads must be synchronized. * @see GetEnv */ -void SetEnv(const TString& key, const TString& value); +void SetEnv(const std::string& key, const std::string& value); diff --git a/util/system/env_ut.cpp b/util/system/env_ut.cpp index e03cc016589..ea6856e641a 100644 --- a/util/system/env_ut.cpp +++ b/util/system/env_ut.cpp @@ -1,31 +1,30 @@ #include -#include #include "env.h" Y_UNIT_TEST_SUITE(EnvTest) { Y_UNIT_TEST(GetSetEnvTest) { - TString key = "util_GETENV_TestVar"; - TString value = "Some value for env var"; - TString def = "Some default value for env var"; + std::string key = "util_GETENV_TestVar"; + std::string value = "Some value for env var"; + std::string def = "Some default value for env var"; // first of all, it should be clear - UNIT_ASSERT_VALUES_EQUAL(GetEnv(key), TString()); + UNIT_ASSERT_VALUES_EQUAL(GetEnv(key), std::string()); UNIT_ASSERT_VALUES_EQUAL(GetEnv(key, def), def); SetEnv(key, value); // set and see what value we get here UNIT_ASSERT_VALUES_EQUAL(GetEnv(key), value); UNIT_ASSERT_VALUES_EQUAL(GetEnv(key, def), value); // set empty value - SetEnv(key, TString()); - UNIT_ASSERT_VALUES_EQUAL(GetEnv(key), TString()); + SetEnv(key, std::string()); + UNIT_ASSERT_VALUES_EQUAL(GetEnv(key), std::string()); // check for long values, see IGNIETFERRO-214 - TString longKey = "util_GETENV_TestVarLong"; - TString longValue{1500, 't'}; - UNIT_ASSERT_VALUES_EQUAL(GetEnv(longKey), TString()); + std::string longKey = "util_GETENV_TestVarLong"; + std::string longValue{1500, 't'}; + UNIT_ASSERT_VALUES_EQUAL(GetEnv(longKey), std::string()); SetEnv(longKey, longValue); UNIT_ASSERT_VALUES_EQUAL(GetEnv(longKey), longValue); - SetEnv(longKey, TString()); - UNIT_ASSERT_VALUES_EQUAL(GetEnv(longKey), TString()); + SetEnv(longKey, std::string()); + UNIT_ASSERT_VALUES_EQUAL(GetEnv(longKey), std::string()); } } diff --git a/util/system/event.cpp b/util/system/event.cpp index 42311d48352..ef414de7c51 100644 --- a/util/system/event.cpp +++ b/util/system/event.cpp @@ -1,8 +1,9 @@ #include "defaults.h" #include "event.h" -#include "mutex.h" -#include "condvar.h" + +#include +#include #ifdef _win_ #include "winint.h" @@ -54,14 +55,15 @@ class TSystemEvent::TEvImpl: public TAtomicRefCount { return; // shortcut } - with_lock (Mutex) { + { + std::lock_guard guard(Mutex); Signaled.store(true, std::memory_order_release); } if (Manual) { - Cond.BroadCast(); + Cond.notify_all(); } else { - Cond.Signal(); + Cond.notify_one(); } } @@ -76,18 +78,18 @@ class TSystemEvent::TEvImpl: public TAtomicRefCount { bool resSignaled = true; - with_lock (Mutex) { - while (!Signaled.load(std::memory_order_acquire)) { - if (!Cond.WaitD(Mutex, deadLine)) { - resSignaled = Signaled.load(std::memory_order_acquire); // timed out, but Signaled could have been set + std::unique_lock ulock(Mutex); + while (!Signaled.load(std::memory_order_acquire)) { + auto duration = std::chrono::microseconds(deadLine.MicroSeconds()); + if (Cond.wait_until(ulock, std::chrono::time_point(duration)) == std::cv_status::timeout) { + resSignaled = Signaled.load(std::memory_order_acquire); // timed out, but Signaled could have been set - break; - } + break; } + } - if (!Manual) { - Signaled.store(false, std::memory_order_release); - } + if (!Manual) { + Signaled.store(false, std::memory_order_release); } return resSignaled; @@ -98,8 +100,8 @@ class TSystemEvent::TEvImpl: public TAtomicRefCount { #ifdef _win_ HANDLE cond; #else - TCondVar Cond; - TMutex Mutex; + std::condition_variable Cond; + std::mutex Mutex; std::atomic Signaled = false; bool Manual; #endif diff --git a/util/system/execpath.cpp b/util/system/execpath.cpp index 9e1b5cec93c..0b63bfa56f2 100644 --- a/util/system/execpath.cpp +++ b/util/system/execpath.cpp @@ -22,8 +22,8 @@ #include "fs.h" #if defined(_freebsd_) -static inline bool GoodPath(const TString& path) { - return path.find('/') != TString::npos; +static inline bool GoodPath(const std::string& path) { + return path.find('/') != std::string::npos; } static inline int FreeBSDSysCtl(int* mib, size_t mibSize, TTempBuf& res) { @@ -41,39 +41,39 @@ static inline int FreeBSDSysCtl(int* mib, size_t mibSize, TTempBuf& res) { return errno; } -static inline TString FreeBSDGetExecPath() { +static inline std::string FreeBSDGetExecPath() { int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; TTempBuf buf; int r = FreeBSDSysCtl(mib, Y_ARRAY_SIZE(mib), buf); if (r == 0) { - return TString(buf.Data(), buf.Filled() - 1); + return std::string(buf.Data(), buf.Filled() - 1); } else if (r == ENOTSUP) { // older FreeBSD version /* * BSD analogue for /proc/self is /proc/curproc. * See: * https://www.freebsd.org/cgi/man.cgi?query=procfs&sektion=5&format=html */ - TString path("/proc/curproc/file"); + std::string path("/proc/curproc/file"); return NFs::ReadLink(path); } else { - return TString(); + return std::string(); } } -static inline TString FreeBSDGetArgv0() { +static inline std::string FreeBSDGetArgv0() { int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, getpid()}; TTempBuf buf; int r = FreeBSDSysCtl(mib, Y_ARRAY_SIZE(mib), buf); if (r == 0) { - return TString(buf.Data()); + return std::string(buf.Data()); } else if (r == ENOTSUP) { - return TString(); + return std::string(); } else { ythrow yexception() << "FreeBSDGetArgv0() failed: " << LastSystemErrorText(); } } -static inline bool FreeBSDGuessExecPath(const TString& guessPath, TString& execPath) { +static inline bool FreeBSDGuessExecPath(const std::string& guessPath, std::string& execPath) { if (NFs::Exists(guessPath)) { // now it should work for real execPath = FreeBSDGetExecPath(); @@ -84,13 +84,13 @@ static inline bool FreeBSDGuessExecPath(const TString& guessPath, TString& execP return false; } -static inline bool FreeBSDGuessExecBasePath(const TString& guessBasePath, TString& execPath) { - return FreeBSDGuessExecPath(TString(guessBasePath) + "/" + getprogname(), execPath); +static inline bool FreeBSDGuessExecBasePath(const std::string& guessBasePath, std::string& execPath) { + return FreeBSDGuessExecPath(std::string(guessBasePath) + "/" + getprogname(), execPath); } #endif -static TString GetExecPathImpl() { +static std::string GetExecPathImpl() { #if defined(_solaris_) return execname(); #elif defined(_darwin_) @@ -118,11 +118,11 @@ static TString GetExecPathImpl() { } } #elif defined(_linux_) || defined(_cygwin_) - TString path("/proc/self/exe"); + std::string path("/proc/self/exe"); return NFs::ReadLink(path); // TODO(yoda): check if the filename ends with " (deleted)" #elif defined(_freebsd_) - TString execPath = FreeBSDGetExecPath(); + std::string execPath = FreeBSDGetExecPath(); if (GoodPath(execPath)) { return execPath; } @@ -145,15 +145,15 @@ static TString GetExecPathImpl() { #endif } -static bool GetPersistentExecPathImpl(TString& to) { +static bool GetPersistentExecPathImpl(std::string& to) { #if defined(_solaris_) - to = TString("/proc/self/object/a.out"); + to = std::string("/proc/self/object/a.out"); return true; #elif defined(_linux_) || defined(_cygwin_) - to = TString("/proc/self/exe"); + to = std::string("/proc/self/exe"); return true; #elif defined(_freebsd_) - to = TString("/proc/curproc/file"); + to = std::string("/proc/curproc/file"); return true; #else // defined(_win_) || defined(_darwin_) or unknown Y_UNUSED(to); @@ -175,15 +175,15 @@ namespace { return SingletonWithPriority(); } - TString ExecPath; - TString PersistentExecPath; + std::string ExecPath; + std::string PersistentExecPath; }; } -const TString& GetExecPath() { +const std::string& GetExecPath() { return TExecPathsHolder::Instance()->ExecPath; } -const TString& GetPersistentExecPath() { +const std::string& GetPersistentExecPath() { return TExecPathsHolder::Instance()->PersistentExecPath; } diff --git a/util/system/execpath.h b/util/system/execpath.h index 4b914b8e851..9375d1556ba 100644 --- a/util/system/execpath.h +++ b/util/system/execpath.h @@ -3,7 +3,7 @@ #include // NOTE: This function has rare sporadic failures (throws exceptions) on FreeBSD. See REVIEW:54297 -const TString& GetExecPath(); +const std::string& GetExecPath(); /** * Get openable path to the binary currently being executed. @@ -14,4 +14,4 @@ const TString& GetExecPath(); * On UNIX variants, utilizes the /proc FS. On Windows, equivalent to * GetExecPath. */ -const TString& GetPersistentExecPath(); +const std::string& GetPersistentExecPath(); diff --git a/util/system/execpath_ut.cpp b/util/system/execpath_ut.cpp index f8ca86d9cf2..bf97fd4d490 100644 --- a/util/system/execpath_ut.cpp +++ b/util/system/execpath_ut.cpp @@ -6,8 +6,8 @@ Y_UNIT_TEST_SUITE(TExecPathTest) { Y_UNIT_TEST(TestIt) { - TString execPath = GetExecPath(); - TString persistentExecPath = GetPersistentExecPath(); + std::string execPath = GetExecPath(); + std::string persistentExecPath = GetPersistentExecPath(); try { UNIT_ASSERT(NFs::Exists(execPath)); diff --git a/util/system/fake_mutex.cpp b/util/system/fake_mutex.cpp new file mode 100644 index 00000000000..bc04bd4a6e3 --- /dev/null +++ b/util/system/fake_mutex.cpp @@ -0,0 +1 @@ +#include "fake_mutex.h" \ No newline at end of file diff --git a/util/system/fake_mutex.h b/util/system/fake_mutex.h new file mode 100644 index 00000000000..b76760f2deb --- /dev/null +++ b/util/system/fake_mutex.h @@ -0,0 +1,28 @@ +#include + +class TFakeMutex: public TNonCopyable { +public: + inline void Acquire() noexcept { + } + + inline bool TryAcquire() noexcept { + return true; + } + + inline void Release() noexcept { + } + + inline void lock() noexcept { + Acquire(); + } + + inline bool try_lock() noexcept { + return TryAcquire(); + } + + inline void unlock() noexcept { + Release(); + } + + ~TFakeMutex() = default; +}; \ No newline at end of file diff --git a/util/system/fasttime.cpp b/util/system/fasttime.cpp index 22b9e9f4665..83378da552d 100644 --- a/util/system/fasttime.cpp +++ b/util/system/fasttime.cpp @@ -19,8 +19,7 @@ ui64 InterpolatedMicroSeconds() { #if defined(_musl_) #include #include - #include - + #include #endif diff --git a/util/system/file.cpp b/util/system/file.cpp index 349e2a667ef..66fec31a089 100644 --- a/util/system/file.cpp +++ b/util/system/file.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -18,7 +19,6 @@ #include #include -#include #include #include @@ -158,7 +158,7 @@ TFileHandle::TFileHandle(const std::filesystem::path& path, EOpenMode oMode) noe } } -TFileHandle::TFileHandle(const TString& fName, EOpenMode oMode) noexcept +TFileHandle::TFileHandle(const std::string& fName, EOpenMode oMode) noexcept : TFileHandle{ // clang-format: off std::filesystem::path( @@ -303,9 +303,9 @@ TFileHandle::TFileHandle(const std::filesystem::path& path, EOpenMode oMode) noe } } -TFileHandle::TFileHandle(const TString& fName, EOpenMode oMode) noexcept +TFileHandle::TFileHandle(const std::string& fName, EOpenMode oMode) noexcept : TFileHandle{ - std::filesystem::path(fName.ConstRef()), + std::filesystem::path(fName), oMode, } { @@ -315,7 +315,7 @@ TFileHandle::TFileHandle(const TString& fName, EOpenMode oMode) noexcept #endif TFileHandle::TFileHandle(const char* fName, EOpenMode oMode) noexcept - : TFileHandle(TString(fName), oMode) + : TFileHandle(std::string(fName), oMode) { } @@ -827,7 +827,7 @@ bool TFileHandle::FlushCache(i64 offset, i64 length, bool wait) noexcept { #endif } -TString DecodeOpenMode(ui32 mode0) { +std::string DecodeOpenMode(ui32 mode0) { ui32 mode = mode0; TStringBuilder r; @@ -835,10 +835,10 @@ TString DecodeOpenMode(ui32 mode0) { #define F(flag) \ if ((mode & flag) == flag) { \ mode &= ~flag; \ - if (r) { \ - r << TStringBuf("|"); \ + if (!r.empty()) { \ + r << std::string_view("|"); \ } \ - r << TStringBuf(#flag); \ + r << std::string_view(#flag); \ } F(RdWr) @@ -879,14 +879,14 @@ TString DecodeOpenMode(ui32 mode0) { #undef F if (mode != 0) { - if (r) { - r << TStringBuf("|"); + if (!r.empty()) { + r << std::string_view("|"); } r << Hex(mode); } - if (!r) { + if (r.empty()) { return "0"; } @@ -895,7 +895,7 @@ TString DecodeOpenMode(ui32 mode0) { class TFile::TImpl: public TAtomicRefCount { public: - inline TImpl(FHANDLE fd, const TString& fname = TString()) + inline TImpl(FHANDLE fd, const std::string& fname = std::string()) : Handle_(fd) , FileName_(fname) { @@ -906,16 +906,16 @@ class TFile::TImpl: public TAtomicRefCount { , FileName_(fName) { if (!Handle_.IsOpen()) { - ythrow TFileError() << "can't open " << FileName_.Quote() << " with mode " << DecodeOpenMode(oMode) << " (" << Hex(oMode.ToBaseType()) << ")"; + ythrow TFileError() << "can't open " << NUtils::Quote(FileName_) << " with mode " << DecodeOpenMode(oMode) << " (" << Hex(oMode.ToBaseType()) << ")"; } } - inline TImpl(const TString& fName, EOpenMode oMode) + inline TImpl(const std::string& fName, EOpenMode oMode) : Handle_(fName, oMode) , FileName_(fName) { if (!Handle_.IsOpen()) { - ythrow TFileError() << "can't open " << FileName_.Quote() << " with mode " << DecodeOpenMode(oMode) << " (" << Hex(oMode.ToBaseType()) << ")"; + ythrow TFileError() << "can't open " << NUtils::Quote(FileName_) << " with mode " << DecodeOpenMode(oMode) << " (" << Hex(oMode.ToBaseType()) << ")"; } } @@ -924,7 +924,7 @@ class TFile::TImpl: public TAtomicRefCount { , FileName_(path.string()) { if (!Handle_.IsOpen()) { - ythrow TFileError() << "can't open " << FileName_.Quote() << " with mode " << DecodeOpenMode(oMode) << " (" << Hex(oMode.ToBaseType()) << ")"; + ythrow TFileError() << "can't open " << NUtils::Quote(FileName_) << " with mode " << DecodeOpenMode(oMode) << " (" << Hex(oMode.ToBaseType()) << ")"; } } @@ -932,15 +932,15 @@ class TFile::TImpl: public TAtomicRefCount { inline void Close() { if (!Handle_.Close()) { - ythrow TFileError() << "can't close " << FileName_.Quote(); + ythrow TFileError() << "can't close " << NUtils::Quote(FileName_); } } - const TString& GetName() const noexcept { + const std::string& GetName() const noexcept { return FileName_; } - void SetName(const TString& newName) { + void SetName(const std::string& newName) { FileName_ = newName; } @@ -951,51 +951,51 @@ class TFile::TImpl: public TAtomicRefCount { i64 Seek(i64 offset, SeekDir origin) { i64 pos = Handle_.Seek(offset, origin); if (pos == -1L) { - ythrow TFileError() << "can't seek " << offset << " bytes in " << FileName_.Quote(); + ythrow TFileError() << "can't seek " << offset << " bytes in " << NUtils::Quote(FileName_); } return pos; } void Resize(i64 length) { if (!Handle_.Resize(length)) { - ythrow TFileError() << "can't resize " << FileName_.Quote() << " to size " << length; + ythrow TFileError() << "can't resize " << NUtils::Quote(FileName_) << " to size " << length; } } void Reserve(i64 length) { if (!Handle_.Reserve(length)) { - ythrow TFileError() << "can't reserve " << length << " for file " << FileName_.Quote(); + ythrow TFileError() << "can't reserve " << length << " for file " << NUtils::Quote(FileName_); } } void FallocateNoResize(i64 length) { if (!Handle_.FallocateNoResize(length)) { - ythrow TFileError() << "can't allocate " << length << "bytes of space for file " << FileName_.Quote(); + ythrow TFileError() << "can't allocate " << length << "bytes of space for file " << NUtils::Quote(FileName_); } } void ShrinkToFit() { if (!Handle_.ShrinkToFit()) { - ythrow TFileError() << "can't shrink " << FileName_.Quote() << " to logical size"; + ythrow TFileError() << "can't shrink " << NUtils::Quote(FileName_) << " to logical size"; } } void Flush() { if (!Handle_.Flush()) { - ythrow TFileError() << "can't flush " << FileName_.Quote(); + ythrow TFileError() << "can't flush " << NUtils::Quote(FileName_); } } void FlushData() { if (!Handle_.FlushData()) { - ythrow TFileError() << "can't flush data " << FileName_.Quote(); + ythrow TFileError() << "can't flush data " << NUtils::Quote(FileName_); } } TFile Duplicate() const { TFileHandle dupH(Handle_.Duplicate()); if (!dupH.IsOpen()) { - ythrow TFileError() << "can't duplicate the handle of " << FileName_.Quote(); + ythrow TFileError() << "can't duplicate the handle of " << NUtils::Quote(FileName_); } TFile res(dupH); dupH.Release(); @@ -1016,7 +1016,7 @@ class TFile::TImpl: public TAtomicRefCount { const i32 reallyRead = RawRead(buf, numBytes); if (reallyRead < 0) { - ythrow TFileError() << "can not read data from " << FileName_.Quote(); + ythrow TFileError() << "can not read data from " << NUtils::Quote(FileName_); } return reallyRead; @@ -1042,7 +1042,7 @@ class TFile::TImpl: public TAtomicRefCount { void Load(void* buf, size_t len) { if (Read(buf, len) != len) { - ythrow TFileError() << "can't read " << len << " bytes from " << FileName_.Quote(); + ythrow TFileError() << "can't read " << len << " bytes from " << NUtils::Quote(FileName_); } } @@ -1059,7 +1059,7 @@ class TFile::TImpl: public TAtomicRefCount { const i32 reallyWritten = Handle_.Write(buf, toWrite); if (reallyWritten < 0) { - ythrow TFileError() << "can't write " << toWrite << " bytes to " << FileName_.Quote(); + ythrow TFileError() << "can't write " << toWrite << " bytes to " << NUtils::Quote(FileName_); } buf += reallyWritten; @@ -1075,7 +1075,7 @@ class TFile::TImpl: public TAtomicRefCount { const i32 reallyRead = RawPread(buf, toRead, offset); if (reallyRead < 0) { - ythrow TFileError() << "can not read data from " << FileName_.Quote(); + ythrow TFileError() << "can not read data from " << NUtils::Quote(FileName_); } if (reallyRead == 0) { @@ -1097,7 +1097,7 @@ class TFile::TImpl: public TAtomicRefCount { void Pload(void* buf, size_t len, i64 offset) const { if (Pread(buf, len, offset) != len) { - ythrow TFileError() << "can't read " << len << " bytes at offset " << offset << " from " << FileName_.Quote(); + ythrow TFileError() << "can't read " << len << " bytes at offset " << offset << " from " << NUtils::Quote(FileName_); } } @@ -1109,7 +1109,7 @@ class TFile::TImpl: public TAtomicRefCount { const i32 reallyWritten = Handle_.Pwrite(buf, toWrite, offset); if (reallyWritten < 0) { - ythrow TFileError() << "can't write " << toWrite << " bytes to " << FileName_.Quote(); + ythrow TFileError() << "can't write " << toWrite << " bytes to " << NUtils::Quote(FileName_); } buf += reallyWritten; @@ -1120,13 +1120,13 @@ class TFile::TImpl: public TAtomicRefCount { void Flock(int op) { if (0 != Handle_.Flock(op)) { - ythrow TFileError() << "can't flock " << FileName_.Quote(); + ythrow TFileError() << "can't flock " << NUtils::Quote(FileName_); } } void SetDirect() { if (!Handle_.SetDirect()) { - ythrow TFileError() << "can't set direct mode for " << FileName_.Quote(); + ythrow TFileError() << "can't set direct mode for " << NUtils::Quote(FileName_); } } @@ -1148,13 +1148,13 @@ class TFile::TImpl: public TAtomicRefCount { void FlushCache(i64 offset, i64 length, bool wait) { if (!Handle_.FlushCache(offset, length, wait)) { - ythrow TFileError() << "can't flush data " << FileName_.Quote(); + ythrow TFileError() << "can't flush data " << NUtils::Quote(FileName_); } } private: TFileHandle Handle_; - TString FileName_; + std::string FileName_; }; TFile::TFile() @@ -1167,7 +1167,7 @@ TFile::TFile(FHANDLE fd) { } -TFile::TFile(FHANDLE fd, const TString& name) +TFile::TFile(FHANDLE fd, const std::string& name) : Impl_(new TImpl(fd, name)) { } @@ -1177,7 +1177,7 @@ TFile::TFile(const char* fName, EOpenMode oMode) { } -TFile::TFile(const TString& fName, EOpenMode oMode) +TFile::TFile(const std::string& fName, EOpenMode oMode) : Impl_(new TImpl(fName, oMode)) { } @@ -1193,7 +1193,7 @@ void TFile::Close() { Impl_->Close(); } -const TString& TFile::GetName() const noexcept { +const std::string& TFile::GetName() const noexcept { return Impl_->GetName(); } @@ -1317,12 +1317,12 @@ void TFile::LinkTo(const TFile& f) const { } } -TFile TFile::Temporary(const TString& prefix) { +TFile TFile::Temporary(const std::string& prefix) { //TODO - handle impossible case of name collision return TFile(prefix + ToString(MicroSeconds()) + "-" + ToString(RandomNumber()), CreateNew | RdWr | Seq | Temp | Transient); } -TFile TFile::ForAppend(const TString& path) { +TFile TFile::ForAppend(const std::string& path) { return TFile(path, OpenAlways | WrOnly | Seq | ::ForAppend); } diff --git a/util/system/file.h b/util/system/file.h index bf5791271df..062d6fa129c 100644 --- a/util/system/file.h +++ b/util/system/file.h @@ -53,7 +53,7 @@ enum EOpenModeFlag { Y_DECLARE_FLAGS(EOpenMode, EOpenModeFlag); Y_DECLARE_OPERATORS_FOR_FLAGS(EOpenMode); -TString DecodeOpenMode(ui32 openMode); +std::string DecodeOpenMode(ui32 openMode); enum SeekDir { sSet = 0, @@ -78,7 +78,7 @@ class TFileHandle: public TNonCopyable { } TFileHandle(const char* fName, EOpenMode oMode) noexcept; - TFileHandle(const TString& fName, EOpenMode oMode) noexcept; + TFileHandle(const std::string& fName, EOpenMode oMode) noexcept; TFileHandle(const std::filesystem::path& path, EOpenMode oMode) noexcept; inline ~TFileHandle() { @@ -152,15 +152,15 @@ class TFile { TFile(); /// Takes ownership of handle, so closes it when the last holder of descriptor dies. explicit TFile(FHANDLE fd); - TFile(FHANDLE fd, const TString& fname); + TFile(FHANDLE fd, const std::string& fname); TFile(const char* fName, EOpenMode oMode); - TFile(const TString& fName, EOpenMode oMode); + TFile(const std::string& fName, EOpenMode oMode); TFile(const std::filesystem::path& path, EOpenMode oMode); ~TFile(); void Close(); - const TString& GetName() const noexcept; + const std::string& GetName() const noexcept; i64 GetPosition() const noexcept; i64 GetLength() const noexcept; bool IsOpen() const noexcept; @@ -216,8 +216,8 @@ class TFile { //flush unwritten data in this range and optionally wait for completion void FlushCache(i64 offset = 0, i64 length = 0, bool wait = true); - static TFile Temporary(const TString& prefix); - static TFile ForAppend(const TString& path); + static TFile Temporary(const std::string& prefix); + static TFile ForAppend(const std::string& path); private: class TImpl; diff --git a/util/system/file_lock.cpp b/util/system/file_lock.cpp index 45d91282c5b..3989180888e 100644 --- a/util/system/file_lock.cpp +++ b/util/system/file_lock.cpp @@ -19,7 +19,7 @@ namespace { } } -TFileLock::TFileLock(const TString& filename, const EFileLockType type) +TFileLock::TFileLock(const std::string& filename, const EFileLockType type) : TFile(filename, OpenAlways | RdOnly) , Type(type) { diff --git a/util/system/file_lock.h b/util/system/file_lock.h index b2aaff5baf8..cefc3dc8a6d 100644 --- a/util/system/file_lock.h +++ b/util/system/file_lock.h @@ -11,7 +11,7 @@ enum class EFileLockType { class TFileLock: public TFile { public: - TFileLock(const TString& filename, const EFileLockType type = EFileLockType::Exclusive); + TFileLock(const std::string& filename, const EFileLockType type = EFileLockType::Exclusive); void Acquire(); bool TryAcquire(); diff --git a/util/system/file_ut.cpp b/util/system/file_ut.cpp index 941e6a50f3d..84e836a70fc 100644 --- a/util/system/file_ut.cpp +++ b/util/system/file_ut.cpp @@ -111,7 +111,7 @@ class TFileTest: public TTestBase { UNIT_ASSERT_EQUAL(file.GetPosition(), 3); } - const TString data = TUnbufferedFileInput(tmp.Name()).ReadAll(); + const std::string data = TUnbufferedFileInput(tmp.Name()).ReadAll(); UNIT_ASSERT_EQUAL(data.length(), 12); UNIT_ASSERT(data.StartsWith("12345")); } @@ -120,7 +120,7 @@ class TFileTest: public TTestBase { UNIT_TEST_SUITE_REGISTRATION(TFileTest); void TFileTest::TestOpen() { - TString res; + std::string res; TFile f1; try { @@ -254,7 +254,7 @@ void TFileTest::TestRawRead() { char buf[7]; i32 reallyRead = file.RawRead(buf, 7); Y_ENSURE(0 <= reallyRead && reallyRead <= 7); - Y_ENSURE(TStringBuf(buf, reallyRead) == TStringBuf("1234567").Head(reallyRead)); + Y_ENSURE(std::string_view(buf, reallyRead) == std::string_view("1234567").Head(reallyRead)); } } @@ -272,12 +272,12 @@ void TFileTest::TestRead() { TFile file(tmp.Name(), OpenExisting | RdOnly); char buf[7]; Y_ENSURE(file.Read(buf, 7) == 7); - Y_ENSURE(TStringBuf(buf, 7) == "1234567"); + Y_ENSURE(std::string_view(buf, 7) == "1234567"); memset(buf, 0, sizeof(buf)); file.Seek(0, sSet); Y_ENSURE(file.Read(buf, 123) == 7); - Y_ENSURE(TStringBuf(buf, 7) == "1234567"); + Y_ENSURE(std::string_view(buf, 7) == "1234567"); } } @@ -296,12 +296,12 @@ void TFileTest::TestRawPread() { char buf[7]; i32 reallyRead = file.RawPread(buf, 3, 1); Y_ENSURE(0 <= reallyRead && reallyRead <= 3); - Y_ENSURE(TStringBuf(buf, reallyRead) == TStringBuf("234").Head(reallyRead)); + Y_ENSURE(std::string_view(buf, reallyRead) == std::string_view("234").Head(reallyRead)); memset(buf, 0, sizeof(buf)); reallyRead = file.RawPread(buf, 2, 5); Y_ENSURE(0 <= reallyRead && reallyRead <= 2); - Y_ENSURE(TStringBuf(buf, reallyRead) == TStringBuf("67").Head(reallyRead)); + Y_ENSURE(std::string_view(buf, reallyRead) == std::string_view("67").Head(reallyRead)); } } @@ -319,11 +319,11 @@ void TFileTest::TestPread() { TFile file(tmp.Name(), OpenExisting | RdOnly); char buf[7]; Y_ENSURE(file.Pread(buf, 3, 1) == 3); - Y_ENSURE(TStringBuf(buf, 3) == "234"); + Y_ENSURE(std::string_view(buf, 3) == "234"); memset(buf, 0, sizeof(buf)); Y_ENSURE(file.Pread(buf, 2, 5) == 2); - Y_ENSURE(TStringBuf(buf, 2) == "67"); + Y_ENSURE(std::string_view(buf, 2) == "67"); } } diff --git a/util/system/filemap.cpp b/util/system/filemap.cpp index 957ad4ab325..2adb27f9dd4 100644 --- a/util/system/filemap.cpp +++ b/util/system/filemap.cpp @@ -70,8 +70,8 @@ namespace { #define GRANULARITY (TSysInfo::Instance().GRANULARITY_) #define PAGE_SIZE (TSysInfo::Instance().PAGE_SIZE_) -const TString& TMemoryMapCommon::UnknownFileName() { - static const TString unknownFileName = "Unknown_file_name"; +const std::string& TMemoryMapCommon::UnknownFileName() { + static const std::string unknownFileName = "Unknown_file_name"; return unknownFileName; } @@ -161,7 +161,7 @@ class TMemoryMap::TImpl: public TAtomicRefCount { } } - inline TImpl(FILE* f, EOpenMode om, TString dbgName) + inline TImpl(FILE* f, EOpenMode om, std::string dbgName) : File_(Duplicate(f)) , DbgName_(std::move(dbgName)) , Length_(File_.GetLength()) @@ -171,7 +171,7 @@ class TMemoryMap::TImpl: public TAtomicRefCount { CreateMapping(); } - inline TImpl(const TString& name, EOpenMode om) + inline TImpl(const std::string& name, EOpenMode om) : File_(name, (om & oRdWr) ? OpenExisting | RdWr : OpenExisting | RdOnly) , DbgName_(name) , Length_(File_.GetLength()) @@ -181,7 +181,7 @@ class TMemoryMap::TImpl: public TAtomicRefCount { CreateMapping(); } - inline TImpl(const TString& name, i64 length, EOpenMode om) + inline TImpl(const std::string& name, i64 length, EOpenMode om) : File_(name, (om & oRdWr) ? OpenExisting | RdWr : OpenExisting | RdOnly) , DbgName_(name) , Length_(length) @@ -196,9 +196,9 @@ class TMemoryMap::TImpl: public TAtomicRefCount { CreateMapping(); } - inline TImpl(const TFile& file, EOpenMode om, const TString& dbgName) + inline TImpl(const TFile& file, EOpenMode om, const std::string& dbgName) : File_(file) - , DbgName_(File_.GetName() ? File_.GetName() : dbgName) + , DbgName_(!File_.GetName().empty() ? File_.GetName() : dbgName) , Length_(File_.GetLength()) , Mode_(om) { @@ -325,7 +325,7 @@ class TMemoryMap::TImpl: public TAtomicRefCount { return File_; } - inline TString GetDbgName() const { + inline std::string GetDbgName() const { return DbgName_; } @@ -335,7 +335,7 @@ class TMemoryMap::TImpl: public TAtomicRefCount { private: TFile File_; - TString DbgName_; // This string is never used to actually open a file, only in exceptions + std::string DbgName_; // This string is never used to actually open a file, only in exceptions i64 Length_; EOpenMode Mode_; @@ -346,37 +346,37 @@ class TMemoryMap::TImpl: public TAtomicRefCount { #endif }; -TMemoryMap::TMemoryMap(const TString& name) +TMemoryMap::TMemoryMap(const std::string& name) : Impl_(new TImpl(name, EOpenModeFlag::oRdOnly)) { } -TMemoryMap::TMemoryMap(const TString& name, EOpenMode om) +TMemoryMap::TMemoryMap(const std::string& name, EOpenMode om) : Impl_(new TImpl(name, om)) { } -TMemoryMap::TMemoryMap(const TString& name, i64 length, EOpenMode om) +TMemoryMap::TMemoryMap(const std::string& name, i64 length, EOpenMode om) : Impl_(new TImpl(name, length, om)) { } -TMemoryMap::TMemoryMap(FILE* f, TString dbgName) +TMemoryMap::TMemoryMap(FILE* f, std::string dbgName) : Impl_(new TImpl(f, EOpenModeFlag::oRdOnly, std::move(dbgName))) { } -TMemoryMap::TMemoryMap(FILE* f, EOpenMode om, TString dbgName) +TMemoryMap::TMemoryMap(FILE* f, EOpenMode om, std::string dbgName) : Impl_(new TImpl(f, om, std::move(dbgName))) { } -TMemoryMap::TMemoryMap(const TFile& file, const TString& dbgName) +TMemoryMap::TMemoryMap(const TFile& file, const std::string& dbgName) : Impl_(new TImpl(file, EOpenModeFlag::oRdOnly, dbgName)) { } -TMemoryMap::TMemoryMap(const TFile& file, EOpenMode om, const TString& dbgName) +TMemoryMap::TMemoryMap(const TFile& file, EOpenMode om, const std::string& dbgName) : Impl_(new TImpl(file, om, dbgName)) { } @@ -444,27 +444,27 @@ TFileMap::TFileMap(const TMemoryMap& map) noexcept { } -TFileMap::TFileMap(const TString& name) +TFileMap::TFileMap(const std::string& name) : Map_(name) { } -TFileMap::TFileMap(const TString& name, EOpenMode om) +TFileMap::TFileMap(const std::string& name, EOpenMode om) : Map_(name, om) { } -TFileMap::TFileMap(const TString& name, i64 length, EOpenMode om) +TFileMap::TFileMap(const std::string& name, i64 length, EOpenMode om) : Map_(name, length, om) { } -TFileMap::TFileMap(FILE* f, EOpenMode om, TString dbgName) +TFileMap::TFileMap(FILE* f, EOpenMode om, std::string dbgName) : Map_(f, om, std::move(dbgName)) { } -TFileMap::TFileMap(const TFile& file, EOpenMode om, const TString& dbgName) +TFileMap::TFileMap(const TFile& file, EOpenMode om, const std::string& dbgName) : Map_(file, om, dbgName) { } diff --git a/util/system/filemap.h b/util/system/filemap.h index 933d169adb7..6515da7254e 100644 --- a/util/system/filemap.h +++ b/util/system/filemap.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include @@ -64,19 +64,19 @@ struct TMemoryMapCommon { * Name that will be printed in exceptions if not specified. * Overridden by name obtained from `TFile` if it's not empty. */ - static const TString& UnknownFileName(); + static const std::string& UnknownFileName(); }; Y_DECLARE_OPERATORS_FOR_FLAGS(TMemoryMapCommon::EOpenMode); class TMemoryMap: public TMemoryMapCommon { public: - explicit TMemoryMap(const TString& name); - explicit TMemoryMap(const TString& name, EOpenMode om); - TMemoryMap(const TString& name, i64 length, EOpenMode om); - TMemoryMap(FILE* f, TString dbgName = UnknownFileName()); - TMemoryMap(FILE* f, EOpenMode om, TString dbgName = UnknownFileName()); - TMemoryMap(const TFile& file, const TString& dbgName = UnknownFileName()); - TMemoryMap(const TFile& file, EOpenMode om, const TString& dbgName = UnknownFileName()); + explicit TMemoryMap(const std::string& name); + explicit TMemoryMap(const std::string& name, EOpenMode om); + TMemoryMap(const std::string& name, i64 length, EOpenMode om); + TMemoryMap(FILE* f, std::string dbgName = UnknownFileName()); + TMemoryMap(FILE* f, EOpenMode om, std::string dbgName = UnknownFileName()); + TMemoryMap(const TFile& file, const std::string& dbgName = UnknownFileName()); + TMemoryMap(const TFile& file, EOpenMode om, const std::string& dbgName = UnknownFileName()); ~TMemoryMap(); @@ -109,11 +109,11 @@ class TMemoryMap: public TMemoryMapCommon { class TFileMap: public TMemoryMapCommon { public: TFileMap(const TMemoryMap& map) noexcept; - TFileMap(const TString& name); - TFileMap(const TString& name, EOpenMode om); - TFileMap(const TString& name, i64 length, EOpenMode om); - TFileMap(FILE* f, EOpenMode om = oRdOnly, TString dbgName = UnknownFileName()); - TFileMap(const TFile& file, EOpenMode om = oRdOnly, const TString& dbgName = UnknownFileName()); + TFileMap(const std::string& name); + TFileMap(const std::string& name, EOpenMode om); + TFileMap(const std::string& name, i64 length, EOpenMode om); + TFileMap(FILE* f, EOpenMode om = oRdOnly, std::string dbgName = UnknownFileName()); + TFileMap(const TFile& file, EOpenMode om = oRdOnly, const std::string& dbgName = UnknownFileName()); TFileMap(const TFileMap& fm) noexcept; ~TFileMap(); @@ -275,11 +275,11 @@ class TFileMappedArray { } private: - void DoInit(const TString& fileName) { + void DoInit(const std::string& fileName) { DataHolder_->Map(0, DataHolder_->Length()); if (DataHolder_->Length() % sizeof(T)) { Term(); - ythrow yexception() << "Incorrect size of file " << fileName.Quote(); + ythrow yexception() << "Incorrect size of file " << NUtils::Quote(fileName); } Ptr_ = (const T*)DataHolder_->Ptr(); Size_ = DataHolder_->Length() / sizeof(T); diff --git a/util/system/filemap_ut.cpp b/util/system/filemap_ut.cpp index f7b153c3960..aab34cc2167 100644 --- a/util/system/filemap_ut.cpp +++ b/util/system/filemap_ut.cpp @@ -301,8 +301,8 @@ Y_UNIT_TEST_SUITE(TFileMapTest) { mappedMem.Map(mappedMem.Length() / 2, mappedMem.Length() + 100); // overflow UNIT_ASSERT(0); // should not go here } catch (yexception& exc) { - TString text = exc.what(); // exception should contain failed file name - UNIT_ASSERT(text.find(TMemoryMapCommon::UnknownFileName()) != TString::npos); + std::string text = exc.what(); // exception should contain failed file name + UNIT_ASSERT(text.find(TMemoryMapCommon::UnknownFileName()) != std::string::npos); fclose(f); } @@ -312,8 +312,8 @@ Y_UNIT_TEST_SUITE(TFileMapTest) { mappedMem.Map(mappedMem.Length() / 2, mappedMem.Length() + 100); // overflow UNIT_ASSERT(0); // should not go here } catch (yexception& exc) { - TString text = exc.what(); // exception should contain failed file name - UNIT_ASSERT(text.find(FileName_) != TString::npos); + std::string text = exc.what(); // exception should contain failed file name + UNIT_ASSERT(text.find(FileName_) != std::string::npos); } NFs::Remove(FileName_); } diff --git a/util/system/flock_ut.cpp b/util/system/flock_ut.cpp index b5f6cb53287..937653765c1 100644 --- a/util/system/flock_ut.cpp +++ b/util/system/flock_ut.cpp @@ -5,6 +5,8 @@ #include +#include + Y_UNIT_TEST_SUITE(TFileLockTest) { Y_UNIT_TEST(TestFlock) { TTempFileHandle tmp("./file"); @@ -21,33 +23,33 @@ Y_UNIT_TEST_SUITE(TFileLockTest) { TFileLock fileLockShared2("./file.locker", EFileLockType::Shared); TFileLock fileLockShared3("./file.locker", EFileLockType::Shared); { - TGuard guard(fileLockExclusive1); + std::lock_guard guard(fileLockExclusive1); } { TTryGuard tryGuard(fileLockExclusive1); UNIT_ASSERT(tryGuard.WasAcquired()); } { - TGuard guard1(fileLockExclusive1); + std::lock_guard guard1(fileLockExclusive1); TTryGuard guard2(fileLockExclusive2); UNIT_ASSERT(!guard2.WasAcquired()); } { - TGuard guard1(fileLockShared1); + std::lock_guard guard1(fileLockShared1); TTryGuard guard2(fileLockShared2); TTryGuard guard3(fileLockShared3); UNIT_ASSERT(guard2.WasAcquired()); UNIT_ASSERT(guard3.WasAcquired()); } { - TGuard guard1(fileLockExclusive1); + std::lock_guard guard1(fileLockExclusive1); TTryGuard guard2(fileLockShared1); TTryGuard guard3(fileLockShared2); UNIT_ASSERT(!guard2.WasAcquired()); UNIT_ASSERT(!guard3.WasAcquired()); } { - TGuard guard1(fileLockShared1); + std::lock_guard guard1(fileLockShared1); TTryGuard guard2(fileLockExclusive1); TTryGuard guard3(fileLockShared2); UNIT_ASSERT(!guard2.WasAcquired()); diff --git a/util/system/fs.cpp b/util/system/fs.cpp index b848abac8a1..b910fec07af 100644 --- a/util/system/fs.cpp +++ b/util/system/fs.cpp @@ -8,6 +8,7 @@ #include #endif +#include #include #include #include @@ -15,7 +16,7 @@ #include #include -bool NFs::Remove(const TString& path) { +bool NFs::Remove(const std::string& path) { #if defined(_win_) return NFsPrivate::WinRemove(path); #else @@ -23,8 +24,8 @@ bool NFs::Remove(const TString& path) { #endif } -void NFs::RemoveRecursive(const TString& path) { - static const TStringBuf errStr = "error while removing "; +void NFs::RemoveRecursive(const std::string& path) { + static const std::string_view errStr = "error while removing "; if (!NFs::Exists(path)) { return; @@ -52,7 +53,7 @@ void NFs::RemoveRecursive(const TString& path) { } } -bool NFs::MakeDirectory(const TString& path, EFilePermissions mode) { +bool NFs::MakeDirectory(const std::string& path, EFilePermissions mode) { #if defined(_win_) Y_UNUSED(mode); return NFsPrivate::WinMakeDirectory(path); @@ -61,7 +62,7 @@ bool NFs::MakeDirectory(const TString& path, EFilePermissions mode) { #endif } -bool NFs::MakeDirectoryRecursive(const TString& path, EFilePermissions mode, bool alwaysCreate) { +bool NFs::MakeDirectoryRecursive(const std::string& path, EFilePermissions mode, bool alwaysCreate) { if (NFs::Exists(path) && TFileStat(path).IsDir()) { if (alwaysCreate) { ythrow TIoException() << "path " << path << " already exists" @@ -83,7 +84,7 @@ bool NFs::MakeDirectoryRecursive(const TString& path, EFilePermissions mode, boo } } -bool NFs::Rename(const TString& oldPath, const TString& newPath) { +bool NFs::Rename(const std::string& oldPath, const std::string& newPath) { #if defined(_win_) return NFsPrivate::WinRename(oldPath, newPath); #else @@ -91,13 +92,13 @@ bool NFs::Rename(const TString& oldPath, const TString& newPath) { #endif } -void NFs::HardLinkOrCopy(const TString& existingPath, const TString& newPath) { +void NFs::HardLinkOrCopy(const std::string& existingPath, const std::string& newPath) { if (!NFs::HardLink(existingPath, newPath)) { Copy(existingPath, newPath); } } -bool NFs::HardLink(const TString& existingPath, const TString& newPath) { +bool NFs::HardLink(const std::string& existingPath, const std::string& newPath) { #if defined(_win_) return NFsPrivate::WinHardLink(existingPath, newPath); #elif defined(_unix_) @@ -105,7 +106,7 @@ bool NFs::HardLink(const TString& existingPath, const TString& newPath) { #endif } -bool NFs::SymLink(const TString& targetPath, const TString& linkPath) { +bool NFs::SymLink(const std::string& targetPath, const std::string& linkPath) { #if defined(_win_) return NFsPrivate::WinSymLink(targetPath, linkPath); #elif defined(_unix_) @@ -113,7 +114,7 @@ bool NFs::SymLink(const TString& targetPath, const TString& linkPath) { #endif } -TString NFs::ReadLink(const TString& path) { +std::string NFs::ReadLink(const std::string& path) { #if defined(_win_) return NFsPrivate::WinReadLink(path); #elif defined(_unix_) @@ -124,28 +125,28 @@ TString NFs::ReadLink(const TString& path) { ythrow yexception() << "can't read link " << path << ", errno = " << errno; } if (r < (ssize_t)buf.Size()) { - return TString(buf.Data(), r); + return std::string(buf.Data(), r); } buf = TTempBuf(buf.Size() * 2); } #endif } -void NFs::Cat(const TString& dstPath, const TString& srcPath) { +void NFs::Cat(const std::string& dstPath, const std::string& srcPath) { TUnbufferedFileInput src(srcPath); TUnbufferedFileOutput dst(TFile(dstPath, ForAppend | WrOnly | Seq)); TransferData(&src, &dst); } -void NFs::Copy(const TString& existingPath, const TString& newPath) { +void NFs::Copy(const std::string& existingPath, const std::string& newPath) { TUnbufferedFileInput src(existingPath); TUnbufferedFileOutput dst(TFile(newPath, CreateAlways | WrOnly | Seq)); TransferData(&src, &dst); } -bool NFs::Exists(const TString& path) { +bool NFs::Exists(const std::string& path) { #if defined(_win_) return NFsPrivate::WinExists(path); #elif defined(_unix_) @@ -153,7 +154,7 @@ bool NFs::Exists(const TString& path) { #endif } -TString NFs::CurrentWorkingDirectory() { +std::string NFs::CurrentWorkingDirectory() { #if defined(_win_) return NFsPrivate::WinCurrentWorkingDirectory(); #elif defined(_unix_) @@ -166,13 +167,13 @@ TString NFs::CurrentWorkingDirectory() { #endif } -void NFs::SetCurrentWorkingDirectory(const TString& path) { +void NFs::SetCurrentWorkingDirectory(const std::string& path) { #ifdef _win_ bool ok = NFsPrivate::WinSetCurrentWorkingDirectory(path); #else bool ok = !chdir(path.data()); #endif if (!ok) { - ythrow TSystemError() << "failed to change directory to " << path.Quote(); + ythrow TSystemError() << "failed to change directory to " << NUtils::Quote(path); } } diff --git a/util/system/fs.h b/util/system/fs.h index a28a6d84fb9..f1801892b5d 100644 --- a/util/system/fs.h +++ b/util/system/fs.h @@ -1,9 +1,10 @@ #pragma once #include -#include #include +#include + namespace NFs { enum EFilePermission { FP_ALL_EXEC = 01, @@ -28,28 +29,28 @@ namespace NFs { /// @param[in] path Path to file or directory /// @returns true on success or false otherwise /// LastSystemError() is set in case of failure - bool Remove(const TString& path); + bool Remove(const std::string& path); /// Remove a file or directory with contents /// Does nothing if path does not exist /// /// @param[in] path Path to file or directory /// @throws - void RemoveRecursive(const TString& path); + void RemoveRecursive(const std::string& path); /// Creates directory /// /// @param[in] path Path to the directory /// @param[in] mode Access permissions field; NOTE: ignored on win /// @returns true on success or false otherwise - bool MakeDirectory(const TString& path, EFilePermissions mode); + bool MakeDirectory(const std::string& path, EFilePermissions mode); /// Creates directory /// /// @param[in] path Path to the directory /// @returns true on success or false otherwise /// NOTE: access permissions is set to 0777 - inline bool MakeDirectory(const TString& path) { + inline bool MakeDirectory(const std::string& path) { return MakeDirectory(path, FP_COMMON_FILE); } @@ -59,14 +60,14 @@ namespace NFs { /// @param[in] mode Access permissions field; NOTE: ignored on win /// @param[in] alwaysCreate Throw if path already exists or failed to create /// @returns true if target path created or exists (and directory) - bool MakeDirectoryRecursive(const TString& path, EFilePermissions mode, bool alwaysCreate); + bool MakeDirectoryRecursive(const std::string& path, EFilePermissions mode, bool alwaysCreate); /// Creates directory and all non-existings parents /// /// @param[in] path Path to be created /// @param[in] mode Access permissions field; NOTE: ignored on win /// @returns true if target path created or exists (and directory) - inline bool MakeDirectoryRecursive(const TString& path, EFilePermissions mode) { + inline bool MakeDirectoryRecursive(const std::string& path, EFilePermissions mode) { return MakeDirectoryRecursive(path, mode, false); } @@ -74,7 +75,7 @@ namespace NFs { /// /// @param[in] path Path to be created /// @returns true if target path created or exists (and directory) - inline bool MakeDirectoryRecursive(const TString& path) { + inline bool MakeDirectoryRecursive(const std::string& path) { return MakeDirectoryRecursive(path, FP_COMMON_FILE, false); } @@ -85,14 +86,14 @@ namespace NFs { /// @param[in] newPath New path of file or directory /// @returns true on success or false otherwise /// LastSystemError() is set in case of failure - bool Rename(const TString& oldPath, const TString& newPath); + bool Rename(const std::string& oldPath, const std::string& newPath); /// Creates a new directory entry for a file /// or creates a new one with the same content /// /// @param[in] existingPath Path to an existing file /// @param[in] newPath New path of file - void HardLinkOrCopy(const TString& existingPath, const TString& newPath); + void HardLinkOrCopy(const std::string& existingPath, const std::string& newPath); /// Creates a new directory entry for a file /// @@ -100,7 +101,7 @@ namespace NFs { /// @param[in] newPath New path of file /// @returns true if new link was created or false otherwise /// LastSystemError() is set in case of failure - bool HardLink(const TString& existingPath, const TString& newPath); + bool HardLink(const std::string& existingPath, const std::string& newPath); /// Creates a symlink to a file /// @@ -108,47 +109,47 @@ namespace NFs { /// @param[in] linkPath Path of symlink /// @returns true if new link was created or false otherwise /// LastSystemError() is set in case of failure - bool SymLink(const TString& targetPath, const TString& linkPath); + bool SymLink(const std::string& targetPath, const std::string& linkPath); /// Reads value of a symbolic link /// /// @param[in] path Path to a symlink /// @returns File path that a symlink points to - TString ReadLink(const TString& path); + std::string ReadLink(const std::string& path); /// Append contents of a file to a new file /// /// @param[in] dstPath Path to a destination file /// @param[in] srcPath Path to a source file - void Cat(const TString& dstPath, const TString& srcPath); + void Cat(const std::string& dstPath, const std::string& srcPath); /// Copy contents of a file to a new file /// /// @param[in] existingPath Path to an existing file /// @param[in] newPath New path of file - void Copy(const TString& existingPath, const TString& newPath); + void Copy(const std::string& existingPath, const std::string& newPath); /// Returns path to the current working directory /// /// Note: is not threadsafe - TString CurrentWorkingDirectory(); + std::string CurrentWorkingDirectory(); /// Changes current working directory /// /// @param[in] path Path for new cwd /// Note: is not threadsafe - void SetCurrentWorkingDirectory(const TString& path); + void SetCurrentWorkingDirectory(const std::string& path); /// Checks if file exists /// /// @param[in] path Path to check - bool Exists(const TString& path); + bool Exists(const std::string& path); /// Ensures that file exists /// /// @param[in] path Path to check /// @returns input argument - inline const TString& EnsureExists(const TString& path) { + inline const std::string& EnsureExists(const std::string& path) { Y_ENSURE_EX(Exists(path), TFileError{} << "Path " << path << " does not exists (checked from cwd:" << NFs::CurrentWorkingDirectory() << ")"); return path; } diff --git a/util/system/fs_ut.cpp b/util/system/fs_ut.cpp index ddee6670a8a..489eeb48951 100644 --- a/util/system/fs_ut.cpp +++ b/util/system/fs_ut.cpp @@ -54,7 +54,7 @@ void TFsTest::TestCreateRemove() { UNIT_ASSERT_EXCEPTION(NFs::MakeDirectoryRecursive(subdir1, NFs::FP_COMMON_FILE, true), TIoException); TFsPath file1 = dir1 / "f1.txt"; - TFsPath file2 = subdir1 + TString("_f2.txt"); + TFsPath file2 = subdir1 + std::string("_f2.txt"); TFsPath file3 = subdir1 / "f2.txt"; Touch(file1); Touch(file2); @@ -68,7 +68,7 @@ void TFsTest::TestCreateRemove() { UNIT_ASSERT(!NFs::MakeDirectoryRecursive(file1 / "subdir1" / "subdir2", NFs::FP_COMMON_FILE, false)); UNIT_ASSERT(!NFs::MakeDirectoryRecursive(file1, NFs::FP_COMMON_FILE, false)); - TString longUtf8Name = ""; + std::string longUtf8Name = ""; while (longUtf8Name.size() < 255) { longUtf8Name = longUtf8Name + "fф"; } @@ -189,7 +189,7 @@ void TFsTest::TestHardlink() { RunHardlinkTest("tempfile_абвг", "hardlinkfile_абвг"); //utf-8 names } -static void RunSymLinkTest(TString fileLocalName, TString symLinkName) { +static void RunSymLinkTest(std::string fileLocalName, std::string symLinkName) { // if previous running was failed TFsPath subDir = "tempsubdir"; TFsPath srcFile = subDir / fileLocalName; @@ -220,8 +220,8 @@ static void RunSymLinkTest(TString fileLocalName, TString symLinkName) { UNIT_ASSERT(NFs::SymLink(subDir, linkD1)); UNIT_ASSERT(NFs::SymLink("../dir2", linkD2)); UNIT_ASSERT(NFs::SymLink("../dir3", dangling)); - UNIT_ASSERT_STRINGS_EQUAL(NFs::ReadLink(linkD2), TString("..") + LOCSLASH_S "dir2"); - UNIT_ASSERT_STRINGS_EQUAL(NFs::ReadLink(dangling), TString("..") + LOCSLASH_S "dir3"); + UNIT_ASSERT_STRINGS_EQUAL(NFs::ReadLink(linkD2), std::string("..") + LOCSLASH_S "dir2"); + UNIT_ASSERT_STRINGS_EQUAL(NFs::ReadLink(dangling), std::string("..") + LOCSLASH_S "dir3"); { TFile file(linkD1 / fileLocalName, OpenExisting | RdOnly); UNIT_ASSERT_VALUES_EQUAL(file.GetLength(), 7); @@ -309,7 +309,7 @@ void TFsTest::TestEnsureExists() { UNIT_ASSERT_EXCEPTION(NFs::EnsureExists(nonExists), TFileError); TStringBuilder expected; - TString got; + std::string got; try { NFs::EnsureExists(nonExists); expected << __LOCATION__; diff --git a/util/system/fs_win.cpp b/util/system/fs_win.cpp index 45d7454b2f6..e2ff40c48e9 100644 --- a/util/system/fs_win.cpp +++ b/util/system/fs_win.cpp @@ -9,7 +9,7 @@ #include namespace NFsPrivate { - static LPCWSTR UTF8ToWCHAR(const TStringBuf str, TUtf16String& wstr) { + static LPCWSTR UTF8ToWCHAR(const std::string_view str, std::u16string& wstr) { wstr.resize(str.size()); size_t written = 0; if (!UTF8ToWide(str.data(), str.size(), wstr.begin(), written)) @@ -19,14 +19,14 @@ namespace NFsPrivate { return (const WCHAR*)wstr.data(); } - static TString WCHARToUTF8(const LPWSTR wstr, size_t len) { + static std::string WCHARToUTF8(const LPWSTR wstr, size_t len) { static_assert(sizeof(WCHAR) == sizeof(wchar16), "expect sizeof(WCHAR) == sizeof(wchar16)"); return WideToUTF8((wchar16*)wstr, len); } - HANDLE CreateFileWithUtf8Name(const TStringBuf fName, ui32 accessMode, ui32 shareMode, ui32 createMode, ui32 attributes, bool inheritHandle) { - TUtf16String wstr; + HANDLE CreateFileWithUtf8Name(const std::string_view fName, ui32 accessMode, ui32 shareMode, ui32 createMode, ui32 attributes, bool inheritHandle) { + std::u16string wstr; LPCWSTR wname = UTF8ToWCHAR(fName, wstr); if (!wname) { ::SetLastError(ERROR_INVALID_NAME); @@ -39,8 +39,8 @@ namespace NFsPrivate { return ::CreateFileW(wname, accessMode, shareMode, &secAttrs, createMode, attributes, nullptr); } - bool WinRename(const TString& oldPath, const TString& newPath) { - TUtf16String op, np; + bool WinRename(const std::string& oldPath, const std::string& newPath) { + std::u16string op, np; LPCWSTR opPtr = UTF8ToWCHAR(oldPath, op); LPCWSTR npPtr = UTF8ToWCHAR(newPath, np); if (!opPtr || !npPtr) { @@ -51,8 +51,8 @@ namespace NFsPrivate { return MoveFileExW(opPtr, npPtr, MOVEFILE_REPLACE_EXISTING) != 0; } - bool WinRemove(const TString& path) { - TUtf16String wstr; + bool WinRemove(const std::string& path) { + std::u16string wstr; LPCWSTR wname = UTF8ToWCHAR(path, wstr); if (!wname) { ::SetLastError(ERROR_INVALID_NAME); @@ -72,16 +72,16 @@ namespace NFsPrivate { return false; } - bool WinSymLink(const TString& targetName, const TString& linkName) { - TString tName(targetName); + bool WinSymLink(const std::string& targetName, const std::string& linkName) { + std::string tName(targetName); { size_t pos; - while ((pos = tName.find('/')) != TString::npos) + while ((pos = tName.find('/')) != std::string::npos) tName.replace(pos, 1, LOCSLASH_S); } - TUtf16String tstr; + std::u16string tstr; LPCWSTR wname = UTF8ToWCHAR(tName, tstr); - TUtf16String lstr; + std::u16string lstr; LPCWSTR lname = UTF8ToWCHAR(linkName, lstr); // we can't create a dangling link to a dir in this way @@ -89,14 +89,14 @@ namespace NFsPrivate { if (attr == INVALID_FILE_ATTRIBUTES) { TTempBuf result; if (GetFullPathNameW(lname, result.Size(), (LPWSTR)result.Data(), 0) != 0) { - TString fullPath = WideToUTF8(TWtringBuf((const wchar16*)result.Data())); - TStringBuf linkDir(fullPath); + std::string fullPath = WideToUTF8(std::u16string_view((const wchar16*)result.Data())); + std::string_view linkDir(fullPath); linkDir.RNextTok('\\'); if (linkDir) { - TString fullTarget(tName); - resolvepath(fullTarget, TString{linkDir}); - TUtf16String fullTargetW; + std::string fullTarget(tName); + resolvepath(fullTarget, std::string{linkDir}); + std::u16string fullTargetW; LPCWSTR ptrFullTarget = UTF8ToWCHAR(fullTarget, fullTargetW); attr = ::GetFileAttributesW(ptrFullTarget); } @@ -105,8 +105,8 @@ namespace NFsPrivate { return 0 != CreateSymbolicLinkW(lname, wname, attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY) ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0); } - bool WinHardLink(const TString& existingPath, const TString& newPath) { - TUtf16String ep, np; + bool WinHardLink(const std::string& existingPath, const std::string& newPath) { + std::u16string ep, np; LPCWSTR epPtr = UTF8ToWCHAR(existingPath, ep); LPCWSTR npPtr = UTF8ToWCHAR(newPath, np); if (!epPtr || !npPtr) { @@ -117,13 +117,13 @@ namespace NFsPrivate { return (CreateHardLinkW(npPtr, epPtr, nullptr) != 0); } - bool WinExists(const TString& path) { - TUtf16String buf; + bool WinExists(const std::string& path) { + std::u16string buf; LPCWSTR ptr = UTF8ToWCHAR(path, buf); return ::GetFileAttributesW(ptr) != INVALID_FILE_ATTRIBUTES; } - TString WinCurrentWorkingDirectory() { + std::string WinCurrentWorkingDirectory() { TTempBuf result; LPWSTR buf = reinterpret_cast(result.Data()); int r = GetCurrentDirectoryW(result.Size() / sizeof(WCHAR), buf); @@ -132,8 +132,8 @@ namespace NFsPrivate { return WCHARToUTF8(buf, r); } - bool WinSetCurrentWorkingDirectory(const TString& path) { - TUtf16String wstr; + bool WinSetCurrentWorkingDirectory(const std::string& path) { + std::u16string wstr; LPCWSTR wname = UTF8ToWCHAR(path, wstr); if (!wname) { ::SetLastError(ERROR_INVALID_NAME); @@ -142,8 +142,8 @@ namespace NFsPrivate { return SetCurrentDirectoryW(wname); } - bool WinMakeDirectory(const TString& path) { - TUtf16String buf; + bool WinMakeDirectory(const std::string& path) { + std::u16string buf; LPCWSTR ptr = UTF8ToWCHAR(path, buf); return CreateDirectoryW(ptr, (LPSECURITY_ATTRIBUTES) nullptr); } @@ -205,7 +205,7 @@ namespace NFsPrivate { } } - TString WinReadLink(const TString& name) { + std::string WinReadLink(const std::string& name) { TFileHandle h = CreateFileWithUtf8Name(name, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, true); TTempBuf buf; @@ -223,7 +223,7 @@ namespace NFsPrivate { return WideToUTF8(str, len); } //this reparse point is unsupported in arcadia - return TString(); + return std::string(); } ULONG WinReadReparseTag(HANDLE h) { diff --git a/util/system/fs_win.h b/util/system/fs_win.h index b91f8691060..97f1b672bc3 100644 --- a/util/system/fs_win.h +++ b/util/system/fs_win.h @@ -3,29 +3,27 @@ #include "winint.h" #include "defaults.h" -#include -#include namespace NFsPrivate { - bool WinRename(const TString& oldPath, const TString& newPath); + bool WinRename(const std::string& oldPath, const std::string& newPath); - bool WinSymLink(const TString& targetName, const TString& linkName); + bool WinSymLink(const std::string& targetName, const std::string& linkName); - bool WinHardLink(const TString& existingPath, const TString& newPath); + bool WinHardLink(const std::string& existingPath, const std::string& newPath); - TString WinReadLink(const TString& path); + std::string WinReadLink(const std::string& path); ULONG WinReadReparseTag(HANDLE h); - HANDLE CreateFileWithUtf8Name(const TStringBuf fName, ui32 accessMode, ui32 shareMode, ui32 createMode, ui32 attributes, bool inheritHandle); + HANDLE CreateFileWithUtf8Name(const std::string_view fName, ui32 accessMode, ui32 shareMode, ui32 createMode, ui32 attributes, bool inheritHandle); - bool WinRemove(const TString& path); + bool WinRemove(const std::string& path); - bool WinExists(const TString& path); + bool WinExists(const std::string& path); - TString WinCurrentWorkingDirectory(); + std::string WinCurrentWorkingDirectory(); - bool WinSetCurrentWorkingDirectory(const TString& path); + bool WinSetCurrentWorkingDirectory(const std::string& path); - bool WinMakeDirectory(const TString& path); + bool WinMakeDirectory(const std::string& path); } diff --git a/util/system/fs_win_ut.cpp b/util/system/fs_win_ut.cpp index 46a8c5f2b0c..5260afe1622 100644 --- a/util/system/fs_win_ut.cpp +++ b/util/system/fs_win_ut.cpp @@ -10,14 +10,13 @@ #include "win_undef.h" #include #include -#include static void Touch(const TFsPath& path) { TFile file(path, CreateAlways | WrOnly); file.Write("1115", 4); } -static LPCWSTR UTF8ToWCHAR(const TStringBuf str, TUtf16String& wstr) { +static LPCWSTR UTF8ToWCHAR(const std::string_view str, std::u16string& wstr) { wstr.resize(str.size()); size_t written = 0; if (!UTF8ToWide(str.data(), str.size(), wstr.begin(), written)) @@ -39,8 +38,8 @@ Y_UNIT_TEST_SUITE(TFsWinTest) { Touch(file1); UNIT_ASSERT(NFsPrivate::WinExists(file1)); { - TUtf16String wstr; - LPCWSTR wname = UTF8ToWCHAR(static_cast(file1), wstr); + std::u16string wstr; + LPCWSTR wname = UTF8ToWCHAR(static_cast(file1), wstr); UNIT_ASSERT(wname); WIN32_FILE_ATTRIBUTE_DATA fad; fad.dwFileAttributes = FILE_ATTRIBUTE_READONLY; @@ -58,8 +57,8 @@ Y_UNIT_TEST_SUITE(TFsWinTest) { UNIT_ASSERT(TFileStat(dir1).IsDir()); { - TUtf16String wstr; - LPCWSTR wname = UTF8ToWCHAR(static_cast(dir1), wstr); + std::u16string wstr; + LPCWSTR wname = UTF8ToWCHAR(static_cast(dir1), wstr); UNIT_ASSERT(wname); WIN32_FILE_ATTRIBUTE_DATA fad; fad.dwFileAttributes = FILE_ATTRIBUTE_READONLY; diff --git a/util/system/fstat.cpp b/util/system/fstat.cpp index 121d34f23ab..f50c8e076f9 100644 --- a/util/system/fstat.cpp +++ b/util/system/fstat.cpp @@ -167,7 +167,7 @@ TFileStat::TFileStat(const TFsPath& fileName, bool nofollow) { MakeFromFileName(fileName.GetPath().data(), nofollow); } -TFileStat::TFileStat(const TString& fileName, bool nofollow) { +TFileStat::TFileStat(const std::string& fileName, bool nofollow) { MakeFromFileName(fileName.data(), nofollow); } @@ -253,6 +253,6 @@ i64 GetFileLength(const char* name) { #endif } -i64 GetFileLength(const TString& name) { +i64 GetFileLength(const std::string& name) { return GetFileLength(name.data()); } diff --git a/util/system/fstat.h b/util/system/fstat.h index b487146e36b..c5d50e74f18 100644 --- a/util/system/fstat.h +++ b/util/system/fstat.h @@ -34,7 +34,7 @@ struct TFileStat { explicit TFileStat(const TFile& f); explicit TFileStat(FHANDLE f); TFileStat(const TFsPath& fileName, bool nofollow = false); - TFileStat(const TString& fileName, bool nofollow = false); + TFileStat(const std::string& fileName, bool nofollow = false); TFileStat(const char* fileName, bool nofollow = false); friend bool operator==(const TFileStat& l, const TFileStat& r) noexcept; @@ -46,4 +46,4 @@ struct TFileStat { i64 GetFileLength(FHANDLE fd); i64 GetFileLength(const char* name); -i64 GetFileLength(const TString& name); +i64 GetFileLength(const std::string& name); diff --git a/util/system/fstat_ut.cpp b/util/system/fstat_ut.cpp index 300f76d7e64..a6cb9544bbc 100644 --- a/util/system/fstat_ut.cpp +++ b/util/system/fstat_ut.cpp @@ -10,7 +10,7 @@ Y_UNIT_TEST_SUITE(TestFileStat) { Y_UNIT_TEST(FileTest) { - TString fileName = "f1.txt"; + std::string fileName = "f1.txt"; TFileStat oFs; { TFile file(fileName.data(), OpenAlways | WrOnly); @@ -176,7 +176,7 @@ Y_UNIT_TEST_SUITE(TestFileStat) { } Y_UNIT_TEST(ChmodTest) { - const TString fileName = "m.txt"; + const std::string fileName = "m.txt"; TFile file(fileName.c_str(), OpenAlways | WrOnly); file.Write("1", 1); file.Close(); diff --git a/util/system/guard.h b/util/system/guard.h index b6ca45fff67..8677dacd8d7 100644 --- a/util/system/guard.h +++ b/util/system/guard.h @@ -6,18 +6,18 @@ template struct TCommonLockOps { static inline void Acquire(T* t) noexcept { - t->Acquire(); + t->lock(); } static inline void Release(T* t) noexcept { - t->Release(); + t->unlock(); } }; template struct TTryLockOps: public TCommonLockOps { static inline bool TryAcquire(T* t) noexcept { - return t->TryAcquire(); + return t->try_lock(); } }; diff --git a/util/system/hostname.cpp b/util/system/hostname.cpp index e377910d5cd..1f3e08098d0 100644 --- a/util/system/hostname.cpp +++ b/util/system/hostname.cpp @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -29,7 +31,7 @@ namespace { HostName = hostNameBuf.Data(); } - TString HostName; + std::string HostName; }; struct TFQDNHostNameHolder { @@ -74,16 +76,16 @@ namespace { ythrow TSystemError() << "can not get FQDN (return code is " << res << ", hostname is \"" << buf << "\")"; } FQDNHostName = ais->ai_canonname; - FQDNHostName.to_lower(); + NUtils::ToLower(FQDNHostName); freeaddrinfo(ais); #endif } - TString FQDNHostName; + std::string FQDNHostName; }; } -const TString& HostName() { +const std::string& HostName() { return (Singleton())->HostName; } @@ -91,7 +93,7 @@ const char* GetHostName() { return HostName().data(); } -const TString& FQDNHostName() { +const std::string& FQDNHostName() { return (Singleton())->FQDNHostName; } @@ -99,9 +101,9 @@ const char* GetFQDNHostName() { return FQDNHostName().data(); } -bool IsFQDN(const TString& name) { - TString absName = name; - if (!absName.EndsWith('.')) { +bool IsFQDN(const std::string& name) { + std::string absName = name; + if (!absName.ends_with('.')) { absName.append("."); } diff --git a/util/system/hostname.h b/util/system/hostname.h index 0839ee2b594..b714f89367b 100644 --- a/util/system/hostname.h +++ b/util/system/hostname.h @@ -3,8 +3,8 @@ #include const char* GetHostName(); -const TString& HostName(); +const std::string& HostName(); const char* GetFQDNHostName(); -const TString& FQDNHostName(); -bool IsFQDN(const TString& name); +const std::string& FQDNHostName(); +bool IsFQDN(const std::string& name); diff --git a/util/system/info.cpp b/util/system/info.cpp index d46c2e7d8d1..cf13f1d207c 100644 --- a/util/system/info.cpp +++ b/util/system/info.cpp @@ -51,7 +51,7 @@ In nanny - Runtime -> Instance spec -> Advanced settings -> Cgroupfs settings: M In deploy - Stage - Edit stage - Box - Cgroupfs settings: Mount mode = Read only */ -static inline double CgroupV1Cpus(const TString& cpuCfsQuotaUsPath, const TString& cfsPeriodUsPath) { +static inline double CgroupV1Cpus(const std::string& cpuCfsQuotaUsPath, const std::string& cfsPeriodUsPath) { try { double q = FromString(StripString(TFileInput(cpuCfsQuotaUsPath).ReadAll())); @@ -82,9 +82,9 @@ Which indicates that the group may consume up to $MAX in each $PERIOD duration. The "max" value could be either the string "max" or a number. In the first case our approximation doesn't work so we can bail out earlier. */ -static inline double CgroupV2Cpus(const TString& cpuMaxPath) { +static inline double CgroupV2Cpus(const std::string& cpuMaxPath) { try { - std::vector cgroupCpuMax = StringSplitter(TFileInput(cpuMaxPath).ReadAll()).Split(' ').Take(2); + std::vector cgroupCpuMax = StringSplitter(TFileInput(cpuMaxPath).ReadAll()).Split(' ').Take(2); double max = FromString(StripString(cgroupCpuMax[0])); double period = FromString(StripString(cgroupCpuMax[1])); @@ -99,9 +99,9 @@ static inline double CgroupV2Cpus(const TString& cpuMaxPath) { } static inline double CgroupCpus() { - static const TString cpuMaxPath("/sys/fs/cgroup/cpu.max"); - static const TString cpuCfsQuotaUsPath("/sys/fs/cgroup/cpu/cpu.cfs_quota_us"); - static const TString cfsPeriodUsPath("/sys/fs/cgroup/cpu/cpu.cfs_period_us"); + static const std::string cpuMaxPath("/sys/fs/cgroup/cpu.max"); + static const std::string cpuCfsQuotaUsPath("/sys/fs/cgroup/cpu/cpu.cfs_quota_us"); + static const std::string cfsPeriodUsPath("/sys/fs/cgroup/cpu/cpu.cfs_period_us"); if (NFs::Exists(cpuMaxPath)) { auto cgroup2Cpus = CgroupV2Cpus(cpuMaxPath); diff --git a/util/system/madvise.cpp b/util/system/madvise.cpp index ad24ac0f1de..6c610a04b1e 100644 --- a/util/system/madvise.cpp +++ b/util/system/madvise.cpp @@ -4,6 +4,8 @@ #include +#include + #if defined(_win_) #include #else @@ -28,13 +30,13 @@ namespace { #if defined(_win_) if (!VirtualFree((LPVOID)begin, size, flag)) { - TString err(LastSystemErrorText()); + std::string err(LastSystemErrorText()); ythrow yexception() << "VirtualFree(" << begin << ", " << size << ", " << flag << ")" << " returned error: " << err; } #else if (-1 == madvise(begin, size, flag)) { - TString err(LastSystemErrorText()); + std::string err(LastSystemErrorText()); ythrow yexception() << "madvise(" << begin << ", " << size << ", " << flag << ")" << " returned error: " << err; } diff --git a/util/system/mem_info.cpp b/util/system/mem_info.cpp index 90ed353a59d..1d7f93dde13 100644 --- a/util/system/mem_info.cpp +++ b/util/system/mem_info.cpp @@ -1,6 +1,7 @@ #include "mem_info.h" -#include +#include + #include #include #include @@ -105,18 +106,18 @@ namespace NMemInfo { #endif #if defined(_linux_) || defined(_cygwin_) - TString path; + std::string path; if (!pid) { path = "/proc/self/statm"; } else { - path = TStringBuilder() << TStringBuf("/proc/") << pid << TStringBuf("/statm"); + path = TStringBuilder() << std::string_view("/proc/") << pid << std::string_view("/statm"); } - const TString stats = TUnbufferedFileInput(path).ReadAll(); + const std::string stats = TUnbufferedFileInput(path).ReadAll(); - TStringBuf statsiter(stats); + std::string_view statsiter(stats); - result.VMS = FromString(statsiter.NextTok(' ')) * pagesize; - result.RSS = FromString(statsiter.NextTok(' ')) * pagesize; + result.VMS = FromString(NUtils::NextTok(statsiter, ' ')) * pagesize; + result.RSS = FromString(NUtils::NextTok(statsiter, ' ')) * pagesize; #if defined(_cygwin_) //cygwin not very accurate @@ -132,7 +133,7 @@ namespace NMemInfo { errno = 0; if (sysctl((int*)mib, 4, &proc, &size, nullptr, 0) == -1) { int err = errno; - TString errtxt = LastSystemErrorText(err); + std::string errtxt = LastSystemErrorText(err); ythrow yexception() << "sysctl({CTL_KERN,KERN_PROC,KERN_PROC_PID,pid},4,proc,&size,NULL,0) returned -1, errno: " << err << " (" << errtxt << ")" << Endl; } @@ -147,7 +148,7 @@ namespace NMemInfo { if (r != sizeof(taskInfo)) { int err = errno; - TString errtxt = LastSystemErrorText(err); + std::string errtxt = LastSystemErrorText(err); ythrow yexception() << "proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &taskInfo, sizeof(taskInfo)) returned " << r << ", errno: " << err << " (" << errtxt << ")" << Endl; } result.VMS = taskInfo.pti_virtual_size; @@ -160,7 +161,7 @@ namespace NMemInfo { const int r = task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&taskInfo, &infoCount); if (r != KERN_SUCCESS) { int err = errno; - TString errtxt = LastSystemErrorText(err); + std::string errtxt = LastSystemErrorText(err); ythrow yexception() << "task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &infoCount) returned" << r << ", errno: " << err << " (" << errtxt << ")" << Endl; } result.VMS = taskInfo.virtual_size; diff --git a/util/system/mktemp.cpp b/util/system/mktemp.cpp index 801451263e5..6af54888862 100644 --- a/util/system/mktemp.cpp +++ b/util/system/mktemp.cpp @@ -14,9 +14,9 @@ extern "C" int mkstemps(char* path, int slen); -TString MakeTempName(const char* wrkDir, const char* prefix, const char* extension) { +std::string MakeTempName(const char* wrkDir, const char* prefix, const char* extension) { #ifndef _win32_ - TString filePath; + std::string filePath; if (wrkDir && *wrkDir) { filePath += wrkDir; diff --git a/util/system/mutex.cpp b/util/system/mutex.cpp deleted file mode 100644 index 66f9a15f294..00000000000 --- a/util/system/mutex.cpp +++ /dev/null @@ -1,146 +0,0 @@ -#include "mutex.h" - -#include -#include - -#if defined(_win_) - #include "winint.h" -#else - #include -#endif - -class TMutex::TImpl { -public: - inline TImpl() { -#if defined(_win_) - InitializeCriticalSection(&Obj); -#else - struct T { - pthread_mutexattr_t Attr; - - inline T() { - int result; - - memset(&Attr, 0, sizeof(Attr)); - result = pthread_mutexattr_init(&Attr); - if (result != 0) { - ythrow yexception() << "mutexattr init failed(" << LastSystemErrorText(result) << ")"; - } - - result = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE); - if (result != 0) { - ythrow yexception() << "mutexattr set type failed(" << LastSystemErrorText(result) << ")"; - } - } - - inline ~T() { - int result = pthread_mutexattr_destroy(&Attr); - Y_ABORT_UNLESS(result == 0, "mutexattr destroy(%s)", LastSystemErrorText(result)); - } - } pma; - - int result = pthread_mutex_init(&Obj, &pma.Attr); - if (result != 0) { - ythrow yexception() << "mutex init failed(" << LastSystemErrorText(result) << ")"; - } -#endif - } - - inline ~TImpl() { -#if defined(_win_) - DeleteCriticalSection(&Obj); -#else - int result = pthread_mutex_destroy(&Obj); - Y_ABORT_UNLESS(result == 0, "mutex destroy failure (%s)", LastSystemErrorText(result)); -#endif - } - - inline void Acquire() noexcept { -#if defined(_win_) - EnterCriticalSection(&Obj); -#else - int result = pthread_mutex_lock(&Obj); - Y_ABORT_UNLESS(result == 0, "mutex lock failure (%s)", LastSystemErrorText(result)); -#endif - } - -#if defined(_win_) - static bool TryEnterCriticalSectionInt(CRITICAL_SECTION* obj) { - #if (_WIN32_WINNT < 0x0400) - if (-1L == ::InterlockedCompareExchange(&obj->LockCount, 0, -1)) { - obj->OwningThread = (HANDLE)(DWORD_PTR)::GetCurrentThreadId(); - obj->RecursionCount = 1; - - return true; - } - - if (obj->OwningThread == (HANDLE)(DWORD_PTR)::GetCurrentThreadId()) { - ::InterlockedIncrement(&obj->LockCount); - ++obj->RecursionCount; - return true; - } - - return false; - #else // _WIN32_WINNT < 0x0400 - return TryEnterCriticalSection(obj); - #endif // _WIN32_WINNT < 0x0400 - } -#endif // _win_ - - inline bool TryAcquire() noexcept { -#if defined(_win_) - return TryEnterCriticalSectionInt(&Obj); -#else - int result = pthread_mutex_trylock(&Obj); - if (result == 0 || result == EBUSY) { - return result == 0; - } - Y_ABORT("mutex trylock failure (%s)", LastSystemErrorText(result)); -#endif - } - - inline void Release() noexcept { -#if defined(_win_) - LeaveCriticalSection(&Obj); -#else - int result = pthread_mutex_unlock(&Obj); - Y_ABORT_UNLESS(result == 0, "mutex unlock failure (%s)", LastSystemErrorText(result)); -#endif - } - - inline void* Handle() const noexcept { - return (void*)&Obj; - } - -private: -#ifdef _win_ - CRITICAL_SECTION Obj; -#else - pthread_mutex_t Obj; -#endif -}; - -TMutex::TMutex() - : Impl_(new TImpl()) -{ -} - -TMutex::TMutex(TMutex&&) noexcept = default; - -TMutex::~TMutex() = default; - -void TMutex::Acquire() noexcept { - Impl_->Acquire(); -} - -bool TMutex::TryAcquire() noexcept { - return Impl_->TryAcquire(); -} - -void TMutex::Release() noexcept { - Impl_->Release(); -} - -void* TMutex::Handle() const noexcept { - return Impl_->Handle(); -} diff --git a/util/system/mutex.h b/util/system/mutex.h deleted file mode 100644 index 2899ed9a50b..00000000000 --- a/util/system/mutex.h +++ /dev/null @@ -1,64 +0,0 @@ -#pragma once - -#include "guard.h" -#include "defaults.h" - -#include -#include - -class TFakeMutex: public TNonCopyable { -public: - inline void Acquire() noexcept { - } - - inline bool TryAcquire() noexcept { - return true; - } - - inline void Release() noexcept { - } - - inline void lock() noexcept { - Acquire(); - } - - inline bool try_lock() noexcept { - return TryAcquire(); - } - - inline void unlock() noexcept { - Release(); - } - - ~TFakeMutex() = default; -}; - -class TMutex { -public: - TMutex(); - TMutex(TMutex&&) noexcept; - ~TMutex(); - - void Acquire() noexcept; - bool TryAcquire() noexcept; - void Release() noexcept; - - inline void lock() noexcept { - Acquire(); - } - - inline bool try_lock() noexcept { - return TryAcquire(); - } - - inline void unlock() noexcept { - Release(); - } - - //return opaque pointer to real handler - void* Handle() const noexcept; - -private: - class TImpl; - THolder Impl_; -}; diff --git a/util/system/mutex_ut.cpp b/util/system/mutex_ut.cpp deleted file mode 100644 index a9c5c45123a..00000000000 --- a/util/system/mutex_ut.cpp +++ /dev/null @@ -1,128 +0,0 @@ -#include "mutex.h" - -#include - -#include -#include - -class TMutexTest: public TTestBase { - UNIT_TEST_SUITE(TMutexTest); - UNIT_TEST(TestBasics) - UNIT_TEST(TestFake) - UNIT_TEST(TestRecursive) - UNIT_TEST_SUITE_END(); - - struct TSharedData { - TSharedData() - : sharedCounter(0) - , failed(false) - { - } - - volatile ui32 sharedCounter; - TMutex mutex; - TFakeMutex fakeMutex; - - bool failed; - }; - - class TThreadTask: public IObjectInQueue { - public: - using PFunc = void (TThreadTask::*)(void); - - TThreadTask(PFunc func, TSharedData& data, size_t id) - : Func_(func) - , Data_(data) - , Id_(id) - { - } - - void Process(void*) override { - THolder This(this); - - (this->*Func_)(); - } - -#define FAIL_ASSERT(cond) \ - if (!(cond)) { \ - Data_.failed = true; \ - } - void RunBasics() { - Data_.mutex.Acquire(); - - ui32 oldCounter = ui32(Data_.sharedCounter + Id_); - Data_.sharedCounter = oldCounter; - usleep(10 + RandomNumber() % 10); - FAIL_ASSERT(Data_.sharedCounter == oldCounter); - - Data_.mutex.Release(); - } - - void RunFakeMutex() { - bool res = Data_.fakeMutex.TryAcquire(); - FAIL_ASSERT(res); - } - - void RunRecursiveMutex() { - for (size_t i = 0; i < Id_ + 1; ++i) { - Data_.mutex.Acquire(); - ++Data_.sharedCounter; - usleep(1); - } - FAIL_ASSERT(Data_.sharedCounter == Id_ + 1); - - bool res = Data_.mutex.TryAcquire(); - FAIL_ASSERT(res); - Data_.mutex.Release(); - - for (size_t i = 0; i < Id_; ++i) { - --Data_.sharedCounter; - Data_.mutex.Release(); - } - FAIL_ASSERT(Data_.sharedCounter == 1); - --Data_.sharedCounter; - Data_.mutex.Release(); - } - -#undef FAIL_ASSERT - - private: - PFunc Func_; - TSharedData& Data_; - size_t Id_; - }; - -private: -#define RUN_CYCLE(what, count) \ - Q_.Start(count); \ - for (size_t i = 0; i < count; ++i) { \ - UNIT_ASSERT(Q_.Add(new TThreadTask(&TThreadTask::what, Data_, i))); \ - } \ - Q_.Stop(); \ - bool b = Data_.failed; \ - Data_.failed = false; \ - UNIT_ASSERT(!b); - - void TestBasics() { - RUN_CYCLE(RunBasics, 5); - - UNIT_ASSERT(Data_.sharedCounter == 10); - Data_.sharedCounter = 0; - } - - void TestFake() { - RUN_CYCLE(RunFakeMutex, 3); - } - - void TestRecursive() { - RUN_CYCLE(RunRecursiveMutex, 4); - } - -#undef RUN_CYCLE - -private: - TSharedData Data_; - TThreadPool Q_; -}; - -UNIT_TEST_SUITE_REGISTRATION(TMutexTest) diff --git a/util/system/progname.cpp b/util/system/progname.cpp index 2c291193203..5466c2f58e6 100644 --- a/util/system/progname.cpp +++ b/util/system/progname.cpp @@ -13,11 +13,11 @@ namespace { { } - TString ProgName; + std::string ProgName; }; } -const TString& GetProgramName() { +const std::string& GetProgramName() { return Singleton()->ProgName; } diff --git a/util/system/progname.h b/util/system/progname.h index e5e2a0eee29..e423774586c 100644 --- a/util/system/progname.h +++ b/util/system/progname.h @@ -9,5 +9,5 @@ void SetProgramName(const char* argv0); SetProgramName(argv[0]); \ } while (0) -/// guaranted return the same immutable instance of TString -const TString& GetProgramName(); +/// guaranted return the same immutable instance of std::string +const std::string& GetProgramName(); diff --git a/util/system/progname_ut.cpp b/util/system/progname_ut.cpp index 11f3d9308b0..12892a8723b 100644 --- a/util/system/progname_ut.cpp +++ b/util/system/progname_ut.cpp @@ -4,11 +4,11 @@ Y_UNIT_TEST_SUITE(TProgramNameTest) { Y_UNIT_TEST(TestIt) { - TString progName = GetProgramName(); + std::string progName = GetProgramName(); try { UNIT_ASSERT( - progName.find("ut_util") != TString::npos || progName.find("util-system_ut") != TString::npos || progName.find("util-system-ut") != TString::npos); + progName.find("ut_util") != std::string::npos || progName.find("util-system_ut") != std::string::npos || progName.find("util-system-ut") != std::string::npos); } catch (...) { Cerr << progName << Endl; diff --git a/util/system/protect.cpp b/util/system/protect.cpp index 6e435e648e3..fb7e550fc1c 100644 --- a/util/system/protect.cpp +++ b/util/system/protect.cpp @@ -1,7 +1,6 @@ #include "protect.h" #include -#include #include "yassert.h" @@ -13,8 +12,8 @@ #include #endif -static TString ModeToString(const EProtectMemory mode) { - TString strMode; +static std::string ModeToString(const EProtectMemory mode) { + std::string strMode; if (mode == PM_NONE) { return "PM_NONE"; } diff --git a/util/system/rwlock.cpp b/util/system/rwlock.cpp index d4664d72d6f..355a2295663 100644 --- a/util/system/rwlock.cpp +++ b/util/system/rwlock.cpp @@ -8,8 +8,8 @@ #endif #if defined(_win_) || defined(_darwin_) - #include "mutex.h" - #include "condvar.h" + #include + #include //darwin rwlocks not recursive class TRWMutex::TImpl { @@ -28,10 +28,10 @@ class TRWMutex::TImpl { void Release() noexcept; private: - TMutex Lock_; + std::mutex Lock_; int State_; - TCondVar ReadCond_; - TCondVar WriteCond_; + std::condition_variable ReadCond_; + std::condition_variable WriteCond_; int BlockedWriters_; }; @@ -47,98 +47,96 @@ TRWMutex::TImpl::~TImpl() { } void TRWMutex::TImpl::AcquireRead() noexcept { - with_lock (Lock_) { + { + std::unique_lock ulock(Lock_); while (BlockedWriters_ || State_ < 0) { - ReadCond_.Wait(Lock_); + ReadCond_.wait(ulock); } ++State_; } - ReadCond_.Signal(); + ReadCond_.notify_one(); } bool TRWMutex::TImpl::TryAcquireRead() noexcept { - with_lock (Lock_) { - if (BlockedWriters_ || State_ < 0) { - return false; - } - - ++State_; + std::lock_guard guard(Lock_); + if (BlockedWriters_ || State_ < 0) { + return false; } + ++State_; + return true; } void TRWMutex::TImpl::ReleaseRead() noexcept { - Lock_.Acquire(); + Lock_.lock(); if (--State_ > 0) { - Lock_.Release(); + Lock_.unlock(); } else if (BlockedWriters_) { - Lock_.Release(); - WriteCond_.Signal(); + Lock_.unlock(); + WriteCond_.notify_one(); } else { - Lock_.Release(); + Lock_.unlock(); } } void TRWMutex::TImpl::AcquireWrite() noexcept { - with_lock (Lock_) { - while (State_ != 0) { - ++BlockedWriters_; - WriteCond_.Wait(Lock_); - --BlockedWriters_; - } - - State_ = -1; + std::unique_lock ulock(Lock_); + while (State_ != 0) { + ++BlockedWriters_; + WriteCond_.wait(ulock); + --BlockedWriters_; } + + State_ = -1; } bool TRWMutex::TImpl::TryAcquireWrite() noexcept { - with_lock (Lock_) { - if (State_ != 0) { - return false; - } - - State_ = -1; + std::lock_guard guard(Lock_); + if (State_ != 0) { + return false; } + State_ = -1; + return true; } void TRWMutex::TImpl::ReleaseWrite() noexcept { - Lock_.Acquire(); + Lock_.lock(); State_ = 0; if (BlockedWriters_) { - Lock_.Release(); - WriteCond_.Signal(); + Lock_.unlock(); + WriteCond_.notify_one(); } else { - Lock_.Release(); - ReadCond_.Signal(); + Lock_.unlock(); + ReadCond_.notify_one(); } } void TRWMutex::TImpl::Release() noexcept { - Lock_.Acquire(); + Lock_.lock(); if (State_ > 0) { if (--State_ > 0) { - Lock_.Release(); + Lock_.unlock(); } else if (BlockedWriters_) { - Lock_.Release(); - WriteCond_.Signal(); + Lock_.unlock(); + WriteCond_.notify_one(); } } else { State_ = 0; if (BlockedWriters_) { - Lock_.Release(); - WriteCond_.Signal(); + Lock_.unlock(); + WriteCond_.notify_one(); } else { - Lock_.Release(); - ReadCond_.Signal(); + Lock_.unlock(); + ReadCond_.notify_one(); } } } diff --git a/util/system/sem.cpp b/util/system/sem.cpp index 1825ac3093c..b1e466ed507 100644 --- a/util/system/sem.cpp +++ b/util/system/sem.cpp @@ -248,10 +248,10 @@ class TFastSemaphore::TImpl: public TPosixSemaphore { } }; #else -class TFastSemaphore::TImpl: public TString, public TSemaphoreImpl { +class TFastSemaphore::TImpl: public std::string, public TSemaphoreImpl { public: inline TImpl(ui32 n) - : TString(ToString(RandomNumber())) + : std::string(ToString(RandomNumber())) , TSemaphoreImpl(c_str(), n) { } diff --git a/util/system/shellcommand.cpp b/util/system/shellcommand.cpp index 0fa2f9af140..ee2101c0f63 100644 --- a/util/system/shellcommand.cpp +++ b/util/system/shellcommand.cpp @@ -3,10 +3,11 @@ #include "nice.h" #include "sigset.h" +#include + #include #include #include -#include #include #include #include @@ -16,6 +17,7 @@ #include #include +#include #if defined(_unix_) #include @@ -189,10 +191,10 @@ using REALPIPEHANDLE = PIPEHANDLE; class TShellCommand::TImpl : public TAtomicRefCount { private: - TString Command; - std::list Arguments; + std::string Command; + std::list Arguments; TShellCommandOptions Options_; - TString WorkDir; + std::string WorkDir; TShellCommandOptions::EHandleMode InputMode = TShellCommandOptions::HANDLE_STREAM; @@ -202,10 +204,10 @@ class TShellCommand::TImpl bool TerminateFlag = false; std::optional ExitCode; - TString CollectedOutput; - TString CollectedError; - TString InternalError; - TMutex TerminateMutex; + std::string CollectedOutput; + std::string CollectedError; + std::string InternalError; + std::mutex TerminateMutex; TFileHandle InputHandle; TFileHandle OutputHandle; TFileHandle ErrorHandle; @@ -253,7 +255,7 @@ class TShellCommand::TImpl IOutputStream* OutputStream; IInputStream* InputStream; std::atomic* ShouldClosePipe; - TString InternalError; + std::string InternalError; }; #if defined(_unix_) @@ -263,7 +265,7 @@ class TShellCommand::TImpl #endif public: - inline TImpl(const TStringBuf cmd, const std::list& args, const TShellCommandOptions& options, const TString& workdir) + inline TImpl(const std::string_view cmd, const std::list& args, const TShellCommandOptions& options, const std::string& workdir) : Command(ToString(cmd)) , Arguments(args) , Options_(options) @@ -282,7 +284,8 @@ class TShellCommand::TImpl inline ~TImpl() { if (WatchThread) { - with_lock (TerminateMutex) { + { + std::lock_guard guard(TerminateMutex); TerminateFlag = true; } @@ -296,28 +299,28 @@ class TShellCommand::TImpl #endif } - inline void AppendArgument(const TStringBuf argument) { + inline void AppendArgument(const std::string_view argument) { if (ExecutionStatus.load(std::memory_order_acquire) == SHELL_RUNNING) { ythrow yexception() << "You cannot change command parameters while process is running"; } Arguments.push_back(ToString(argument)); } - inline const TString& GetOutput() const { + inline const std::string& GetOutput() const { if (ExecutionStatus.load(std::memory_order_acquire) == SHELL_RUNNING) { ythrow yexception() << "You cannot retrieve output while process is running."; } return CollectedOutput; } - inline const TString& GetError() const { + inline const std::string& GetError() const { if (ExecutionStatus.load(std::memory_order_acquire) == SHELL_RUNNING) { ythrow yexception() << "You cannot retrieve output while process is running."; } return CollectedError; } - inline const TString& GetInternalError() const { + inline const std::string& GetInternalError() const { if (ExecutionStatus.load(std::memory_order_acquire) != SHELL_INTERNAL_ERROR) { ythrow yexception() << "Internal error hasn't occured so can't be retrieved."; } @@ -475,7 +478,7 @@ class TShellCommand::TImpl return nullptr; } - TString GetQuotedCommand() const; + std::string GetQuotedCommand() const; }; #if defined(_win_) @@ -521,10 +524,10 @@ void TShellCommand::TImpl::StartProcess(TShellCommand::TImpl::TPipes& pipes) { } PROCESS_INFORMATION process_info; - // TString cmd = "cmd /U" + TUtf16String can be used to read unicode messages from cmd + // std::string cmd = "cmd /U" + std::u16string can be used to read unicode messages from cmd // /A - ansi charset /Q - echo off, /C - command, /Q - special quotes - TString qcmd = GetQuotedCommand(); - TString cmd = Options_.UseShell ? "cmd /A /Q /S /C \"" + qcmd + "\"" : qcmd; + std::string qcmd = GetQuotedCommand(); + std::string cmd = Options_.UseShell ? "cmd /A /Q /S /C \"" + qcmd + "\"" : qcmd; // winapi can modify command text, copy it Y_ENSURE_EX(cmd.size() < MAX_COMMAND_LINE, yexception() << "Command is too long (length=" << cmd.size() << ")"); @@ -540,7 +543,7 @@ void TShellCommand::TImpl::StartProcess(TShellCommand::TImpl::TPipes& pipes) { } void* lpEnvironment = nullptr; - TString env; + std::string env; if (!Options_.Environment.empty()) { for (auto e = Options_.Environment.begin(); e != Options_.Environment.end(); ++e) { env += e->first + '=' + e->second + '\0'; @@ -593,10 +596,10 @@ void TShellCommand::TImpl::StartProcess(TShellCommand::TImpl::TPipes& pipes) { } #endif -void ShellQuoteArg(TString& dst, TStringBuf argument) { +void ShellQuoteArg(std::string& dst, std::string_view argument) { dst.append("\""); - TStringBuf l, r; - while (argument.TrySplit('"', l, r)) { + std::string_view l, r; + while (NUtils::TrySplit(argument, l, r, '"')) { dst.append(l); dst.append("\\\""); argument = r; @@ -605,20 +608,20 @@ void ShellQuoteArg(TString& dst, TStringBuf argument) { dst.append("\""); } -void ShellQuoteArgSp(TString& dst, TStringBuf argument) { - dst.append(' '); +void ShellQuoteArgSp(std::string& dst, std::string_view argument) { + dst.append(" "); ShellQuoteArg(dst, argument); } -bool ArgNeedsQuotes(TStringBuf arg) noexcept { +bool ArgNeedsQuotes(std::string_view arg) noexcept { if (arg.empty()) { return true; } - return arg.find_first_of(" \"\'\t&()*<>\\`^|") != TString::npos; + return arg.find_first_of(" \"\'\t&()*<>\\`^|") != std::string::npos; } -TString TShellCommand::TImpl::GetQuotedCommand() const { - TString quoted = Command; /// @todo command itself should be quoted too +std::string TShellCommand::TImpl::GetQuotedCommand() const { + std::string quoted = Command; /// @todo command itself should be quoted too for (const auto& argument : Arguments) { // Don't add unnecessary quotes. It's especially important for the windows with a 32k command line length limit. if (Options_.QuoteArguments && ArgNeedsQuotes(argument)) { @@ -722,7 +725,7 @@ void TShellCommand::TImpl::OnFork(TPipes& pipes, sigset_t oldmask, char* const* #endif void TShellCommand::TImpl::Run() { - Y_ENSURE(ExecutionStatus.load(std::memory_order_acquire) != SHELL_RUNNING, TStringBuf("Process is already running")); + Y_ENSURE(ExecutionStatus.load(std::memory_order_acquire) != SHELL_RUNNING, std::string_view("Process is already running")); // Prepare I/O streams CollectedOutput.clear(); CollectedError.clear(); @@ -749,7 +752,7 @@ void TShellCommand::TImpl::Run() { } /* arguments holders */ - TString shellArg; + std::string shellArg; std::vector qargv; /* Following "const_cast"s are safe: @@ -773,7 +776,7 @@ void TShellCommand::TImpl::Run() { qargv.push_back(nullptr); - std::vector envHolder; + std::vector envHolder; std::vector envp; if (!Options_.Environment.empty()) { for (auto& env : Options_.Environment) { @@ -895,7 +898,8 @@ void TShellCommand::TImpl::Communicate(TProcessInfo* pi) { while (true) { { - with_lock (pi->Parent->TerminateMutex) { + { + std::lock_guard guard(pi->Parent->TerminateMutex); if (TerminateIsRequired(pi)) { return; } @@ -1087,33 +1091,33 @@ void TShellCommand::TImpl::Communicate(TProcessInfo* pi) { TerminateIsRequired(pi); } -TShellCommand::TShellCommand(const TStringBuf cmd, const std::list& args, const TShellCommandOptions& options, - const TString& workdir) +TShellCommand::TShellCommand(const std::string_view cmd, const std::list& args, const TShellCommandOptions& options, + const std::string& workdir) : Impl(new TImpl(cmd, args, options, workdir)) { } -TShellCommand::TShellCommand(const TStringBuf cmd, const TShellCommandOptions& options, const TString& workdir) - : Impl(new TImpl(cmd, std::list(), options, workdir)) +TShellCommand::TShellCommand(const std::string_view cmd, const TShellCommandOptions& options, const std::string& workdir) + : Impl(new TImpl(cmd, std::list(), options, workdir)) { } TShellCommand::~TShellCommand() = default; -TShellCommand& TShellCommand::operator<<(const TStringBuf argument) { +TShellCommand& TShellCommand::operator<<(const std::string_view argument) { Impl->AppendArgument(argument); return *this; } -const TString& TShellCommand::GetOutput() const { +const std::string& TShellCommand::GetOutput() const { return Impl->GetOutput(); } -const TString& TShellCommand::GetError() const { +const std::string& TShellCommand::GetError() const { return Impl->GetError(); } -const TString& TShellCommand::GetInternalError() const { +const std::string& TShellCommand::GetInternalError() const { return Impl->GetInternalError(); } @@ -1161,6 +1165,6 @@ TShellCommand& TShellCommand::CloseInput() { return *this; } -TString TShellCommand::GetQuotedCommand() const { +std::string TShellCommand::GetQuotedCommand() const { return Impl->GetQuotedCommand(); } diff --git a/util/system/shellcommand.h b/util/system/shellcommand.h index 61e40a5d129..0cd17610e5f 100644 --- a/util/system/shellcommand.h +++ b/util/system/shellcommand.h @@ -1,15 +1,12 @@ #pragma once #include -#include #include -#include #include #include #include "file.h" #include "getpid.h" #include "thread.h" -#include "mutex.h" #include #include #include @@ -31,9 +28,9 @@ class TShellCommandOptions { public: struct TUserOptions { - TString Name; + std::string Name; #if defined(_win_) - TString Password; + std::string Password; #endif #if defined(_unix_) /** @@ -335,7 +332,7 @@ class TShellCommandOptions { IOutputStream* OutputStream; IOutputStream* ErrorStream; TUserOptions User; - THashMap Environment; + THashMap Environment; int Nice = 0; std::function FuncAfterFork = {}; @@ -367,9 +364,9 @@ class TShellCommand: public TNonCopyable { * @param options execution options * @todo store entire options structure */ - TShellCommand(const TStringBuf cmd, const std::list& args, const TShellCommandOptions& options = TShellCommandOptions(), - const TString& workdir = TString()); - TShellCommand(const TStringBuf cmd, const TShellCommandOptions& options = TShellCommandOptions(), const TString& workdir = TString()); + TShellCommand(const std::string_view cmd, const std::list& args, const TShellCommandOptions& options = TShellCommandOptions(), + const std::string& workdir = std::string()); + TShellCommand(const std::string_view cmd, const TShellCommandOptions& options = TShellCommandOptions(), const std::string& workdir = std::string()); ~TShellCommand(); public: @@ -380,7 +377,7 @@ class TShellCommand: public TNonCopyable { * * @return self */ - TShellCommand& operator<<(const TStringBuf argument); + TShellCommand& operator<<(const std::string_view argument); /** * @brief return the collected output from the command. @@ -388,7 +385,7 @@ class TShellCommand: public TNonCopyable { * * @return collected output */ - const TString& GetOutput() const; + const std::string& GetOutput() const; /** * @brief return the collected error output from the command. @@ -396,7 +393,7 @@ class TShellCommand: public TNonCopyable { * * @return collected error output */ - const TString& GetError() const; + const std::string& GetError() const; /** * @brief return the internal error occured while watching @@ -405,7 +402,7 @@ class TShellCommand: public TNonCopyable { * * @return error text */ - const TString& GetInternalError() const; + const std::string& GetInternalError() const; /** * @brief get current status of command execution @@ -483,7 +480,7 @@ class TShellCommand: public TNonCopyable { /** * @brief Get quoted command (for debug/view purposes only!) **/ - TString GetQuotedCommand() const; + std::string GetQuotedCommand() const; private: class TImpl; @@ -492,10 +489,10 @@ class TShellCommand: public TNonCopyable { }; /// Appends to dst: quoted arg -void ShellQuoteArg(TString& dst, TStringBuf arg); +void ShellQuoteArg(std::string& dst, std::string_view arg); /// Appends to dst: space, quoted arg -void ShellQuoteArgSp(TString& dst, TStringBuf arg); +void ShellQuoteArgSp(std::string& dst, std::string_view arg); /// Returns true if arg should be quoted -bool ArgNeedsQuotes(TStringBuf arg) noexcept; +bool ArgNeedsQuotes(std::string_view arg) noexcept; diff --git a/util/system/shellcommand_ut.cpp b/util/system/shellcommand_ut.cpp index 13a5fad75fa..9d8e53aada5 100644 --- a/util/system/shellcommand_ut.cpp +++ b/util/system/shellcommand_ut.cpp @@ -16,6 +16,8 @@ #include #include +#include + #if defined(_win_) #define NL "\r\n" const char catCommand[] = "sort"; // not really cat but ok @@ -32,25 +34,20 @@ class TGuardedStringStream: public IInputStream, public IOutputStream { Stream_.Reserve(100); } - TString Str() const { - with_lock (Lock_) { - return Stream_.Str(); - } - return TString(); // line for compiler + std::string Str() const { + std::lock_guard guard(Lock_); + return Stream_.Str(); } protected: size_t DoRead(void* buf, size_t len) override { - with_lock (Lock_) { - return Stream_.Read(buf, len); - } - return 0; // line for compiler + std::lock_guard guard(Lock_); + return Stream_.Read(buf, len); } void DoWrite(const void* buf, size_t len) override { - with_lock (Lock_) { - return Stream_.Write(buf, len); - } + std::lock_guard guard(Lock_); + return Stream_.Write(buf, len); } private: @@ -60,7 +57,7 @@ class TGuardedStringStream: public IInputStream, public IOutputStream { Y_UNIT_TEST_SUITE(TShellQuoteTest) { Y_UNIT_TEST(TestQuoteArg) { - TString cmd; + std::string cmd; ShellQuoteArg(cmd, "/pr f/krev/prev.exe"); ShellQuoteArgSp(cmd, "-DVal=\"W Quotes\""); ShellQuoteArgSp(cmd, "-DVal=W Space"); @@ -169,8 +166,8 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { } Y_UNIT_TEST(TestQuotes) { TShellCommandOptions options; - TString input = TString("a\"a a"); - TString output; + std::string input = std::string("a\"a a"); + std::string output; TStringOutput outputStream(output); options.SetOutputStream(&outputStream); TShellCommand cmd("echo", options); @@ -201,7 +198,7 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { // 'type con' and 'copy con con' want real console, not stdin, use sort Y_UNIT_TEST(TestInput) { TShellCommandOptions options; - TString input = (TString("a") * 2000).append(NL) * textSize; + std::string input = (std::string("a") * 2000).append(NL) * textSize; TStringInput inputStream(input); options.SetInputStream(&inputStream); TShellCommand cmd(catCommand, options); @@ -211,10 +208,10 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { } Y_UNIT_TEST(TestOutput) { TShellCommandOptions options; - TString input = (TString("a") * 2000).append(NL) * textSize; + std::string input = (std::string("a") * 2000).append(NL) * textSize; TStringInput inputStream(input); options.SetInputStream(&inputStream); - TString output; + std::string output; TStringOutput outputStream(output); options.SetOutputStream(&outputStream); TShellCommand cmd(catCommand, options); @@ -296,7 +293,7 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { #if !defined(_win_) // this ut is unix-only, port to win using %TEMP% Y_UNIT_TEST(TestInterrupt) { - TString tmpfile = TString("shellcommand_ut.interrupt.") + ToString(RandomNumber()); + std::string tmpfile = std::string("shellcommand_ut.interrupt.") + ToString(RandomNumber()); TShellCommandOptions options; options.SetAsync(true); @@ -326,7 +323,7 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { rc = SigProcMask(SIG_SETMASK, &newmask, &oldmask); UNIT_ASSERT(rc == 0); - TString tmpfile = TString("shellcommand_ut.interrupt.") + ToString(RandomNumber()); + std::string tmpfile = std::string("shellcommand_ut.interrupt.") + ToString(RandomNumber()); TShellCommandOptions options; options.SetAsync(true); @@ -380,7 +377,7 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { } #endif Y_UNIT_TEST(TestInternalError) { - TString input = (TString("a") * 2000).append("\n"); + std::string input = (std::string("a") * 2000).append("\n"); TStringInput inputStream(input); TMemoryOutput outputStream(nullptr, 0); TShellCommandOptions options; @@ -397,8 +394,8 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { options.SetOutputStream(&stream); options.SetUseShell(true); - TString input = TString(7000, 'a'); - TString command = TStringBuilder{} << "echo " << input; + std::string input = std::string(7000, 'a'); + std::string command = TStringBuilder{} << "echo " << input; TShellCommand cmd(command, options); cmd.Run().Wait(); @@ -410,8 +407,8 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { options.SetErrorStream(&stream); options.SetUseShell(true); - TString input = TString(7000, 'a'); - TString command = TStringBuilder{} << "echo " << input << ">&2"; + std::string input = std::string(7000, 'a'); + std::string command = TStringBuilder{} << "echo " << input << ">&2"; TShellCommand cmd(command, options); cmd.Run().Wait(); @@ -439,13 +436,13 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { TShellCommandOptions options; options.SetAsync(true); options.PipeOutput(); - constexpr TStringBuf firstMessage = "first message"; - constexpr TStringBuf secondMessage = "second message"; - const TString command = TStringBuilder() << "echo '" << firstMessage << "' && sleep 10 && echo '" << secondMessage << "'"; + constexpr std::string_view firstMessage = "first message"; + constexpr std::string_view secondMessage = "second message"; + const std::string command = TStringBuilder() << "echo '" << firstMessage << "' && sleep 10 && echo '" << secondMessage << "'"; TShellCommand cmd(command, options); cmd.Run(); TUnbufferedFileInput cmdOutput(TFile(cmd.GetOutputHandle().Release())); - TString firstLineOutput, secondLineOutput; + std::string firstLineOutput, secondLineOutput; { Sleep(TDuration::Seconds(5)); firstLineOutput = cmdOutput.ReadLine(); @@ -465,11 +462,11 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { UNIT_ASSERT(options.ErrorMode == TShellCommandOptions::HANDLE_STREAM); } Y_UNIT_TEST(TestForkCallback) { - TString tmpFile = TString("shellcommand_ut.test_for_callback.txt"); + std::string tmpFile = std::string("shellcommand_ut.test_for_callback.txt"); TFsPath cwd(::NFs::CurrentWorkingDirectory()); - const TString tmpFilePath = cwd.Child(tmpFile); + const std::string tmpFilePath = cwd.Child(tmpFile); - const TString text = "test output"; + const std::string text = "test output"; auto afterForkCallback = [&tmpFilePath, &text]() -> void { TFixedBufferFileOutput out(tmpFilePath); out << text; @@ -478,14 +475,14 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { TShellCommandOptions options; options.SetFuncAfterFork(afterForkCallback); - const TString command = "ls"; + const std::string command = "ls"; TShellCommand cmd(command, options); cmd.Run(); UNIT_ASSERT(NFs::Exists(tmpFilePath)); TUnbufferedFileInput fileOutput(tmpFilePath); - TString firstLine = fileOutput.ReadLine(); + std::string firstLine = fileOutput.ReadLine(); UNIT_ASSERT_VALUES_EQUAL(firstLine, text); } diff --git a/util/system/spinlock.h b/util/system/spinlock.h index 9e7f603f491..5ac1a792ed1 100644 --- a/util/system/spinlock.h +++ b/util/system/spinlock.h @@ -85,7 +85,7 @@ class TSpinLock: public TSpinLockBase { /** * TAdaptiveLock almost always should be used instead of TSpinLock. - * It also should be used instead of TMutex for short-term locks. + * It also should be used instead of std::mutex for short-term locks. * This usually means that the locked code should not use syscalls, * since almost every syscall: * - might run unpredictably long and the waiting thread will waste a lot of CPU diff --git a/util/system/src_location.cpp b/util/system/src_location.cpp index d8b1f1ef4a9..fb18bc0f097 100644 --- a/util/system/src_location.cpp +++ b/util/system/src_location.cpp @@ -5,7 +5,7 @@ template <> void Out(IOutputStream& o, const TSourceLocation& t) { #if defined(_win_) - TString file(t.File); + std::string file(t.File); std::replace(file.begin(), file.vend(), '\\', '/'); o << file; #else diff --git a/util/system/src_location.h b/util/system/src_location.h index 12ba6e063e9..46ac62ab38a 100644 --- a/util/system/src_location.h +++ b/util/system/src_location.h @@ -2,24 +2,24 @@ #include "src_root.h" -#include +#include struct TSourceLocation { - constexpr TSourceLocation(const TStringBuf f, int l) noexcept + constexpr TSourceLocation(const std::string_view f, int l) noexcept : File(f) , Line(l) { } - TStringBuf File; + std::string_view File; int Line; }; // __SOURCE_FILE__ should be used instead of __FILE__ #if !defined(__NVCC__) - #define __SOURCE_FILE__ (__SOURCE_FILE_IMPL__.As()) + #define __SOURCE_FILE__ (__SOURCE_FILE_IMPL__.As()) #else - #define __SOURCE_FILE__ (__SOURCE_FILE_IMPL__.template As()) + #define __SOURCE_FILE__ (__SOURCE_FILE_IMPL__.template As()) #endif #define __LOCATION__ ::TSourceLocation(__SOURCE_FILE__, __LINE__) diff --git a/util/system/src_location_ut.cpp b/util/system/src_location_ut.cpp index 5b86cb86efc..e7a7dc4c234 100644 --- a/util/system/src_location_ut.cpp +++ b/util/system/src_location_ut.cpp @@ -4,7 +4,7 @@ #include -static inline TString GenLoc() { +static inline std::string GenLoc() { return TStringBuilder() << __LOCATION__; } diff --git a/util/system/src_root_ut.cpp b/util/system/src_root_ut.cpp index e9a675eb9ab..dd012dc6d71 100644 --- a/util/system/src_root_ut.cpp +++ b/util/system/src_root_ut.cpp @@ -6,14 +6,14 @@ Y_UNIT_TEST_SUITE(TestSourceRoot) { Y_UNIT_TEST(TestStrip) { // Reconstruct() converts "\" -> "/" on Windows - const TString path = TPathSplit(__SOURCE_FILE_IMPL__.As()).Reconstruct(); + const std::string path = TPathSplit(__SOURCE_FILE_IMPL__.As()).Reconstruct(); UNIT_ASSERT_EQUAL(path, "util" LOCSLASH_S "system" LOCSLASH_S "src_root_ut.cpp"); } Y_UNIT_TEST(TestPrivateChopPrefixRoutine) { static constexpr const char str[] = ":\0:\0: It's unlikely that this string has an ARCADIA_ROOT as its prefix :\0:\0:"; static constexpr const auto strStaticBuf = STATIC_BUF(str); - UNIT_ASSERT_VALUES_EQUAL(TStringBuf(str, sizeof(str) - 1), ::NPrivate::StripRoot(strStaticBuf).As()); + UNIT_ASSERT_VALUES_EQUAL(std::string_view(str, sizeof(str) - 1), ::NPrivate::StripRoot(strStaticBuf).As()); UNIT_ASSERT_VALUES_EQUAL(0, ::NPrivate::RootPrefixLength(strStaticBuf)); static_assert(::NPrivate::IsProperPrefix(STATIC_BUF("foo"), STATIC_BUF("foobar")), R"(IsProperPrefix("foo", "foobar") failed)"); diff --git a/util/system/tempfile.cpp b/util/system/tempfile.cpp index a2e9f49eb12..5616c236d35 100644 --- a/util/system/tempfile.cpp +++ b/util/system/tempfile.cpp @@ -6,17 +6,17 @@ TTempFileHandle::TTempFileHandle() { } -TTempFileHandle::TTempFileHandle(const TString& fname) +TTempFileHandle::TTempFileHandle(const std::string& fname) : TTempFile(fname) , TFile(CreateFile()) { } -TTempFileHandle TTempFileHandle::InCurrentDir(const TString& filePrefix, const TString& extension) { +TTempFileHandle TTempFileHandle::InCurrentDir(const std::string& filePrefix, const std::string& extension) { return TTempFileHandle(MakeTempName(".", filePrefix.c_str(), extension.c_str())); } -TTempFileHandle TTempFileHandle::InDir(const TFsPath& dirPath, const TString& filePrefix, const TString& extension) { +TTempFileHandle TTempFileHandle::InDir(const TFsPath& dirPath, const std::string& filePrefix, const std::string& extension) { return TTempFileHandle(MakeTempName(dirPath.c_str(), filePrefix.c_str(), extension.c_str())); } diff --git a/util/system/tempfile.h b/util/system/tempfile.h index ac8bc5c3226..fb4df793442 100644 --- a/util/system/tempfile.h +++ b/util/system/tempfile.h @@ -4,11 +4,10 @@ #include "file.h" #include -#include class TTempFile { public: - inline TTempFile(const TString& fname) + inline TTempFile(const std::string& fname) : Name_(fname) { } @@ -17,21 +16,21 @@ class TTempFile { NFs::Remove(Name()); } - inline const TString& Name() const noexcept { + inline const std::string& Name() const noexcept { return Name_; } private: - const TString Name_; + const std::string Name_; }; class TTempFileHandle: public TTempFile, public TFile { public: TTempFileHandle(); - TTempFileHandle(const TString& fname); + TTempFileHandle(const std::string& fname); - static TTempFileHandle InCurrentDir(const TString& filePrefix = "yandex", const TString& extension = "tmp"); - static TTempFileHandle InDir(const TFsPath& dirPath, const TString& filePrefix = "yandex", const TString& extension = "tmp"); + static TTempFileHandle InCurrentDir(const std::string& filePrefix = "yandex", const std::string& extension = "tmp"); + static TTempFileHandle InDir(const TFsPath& dirPath, const std::string& filePrefix = "yandex", const std::string& extension = "tmp"); private: TFile CreateFile() const; @@ -47,4 +46,4 @@ class TTempFileHandle: public TTempFile, public TFile { * Returned filepath has such format: dir/prefixXXXXXX.extension or dir/prefixXXXXXX * But win32: dir/preXXXX.tmp (prefix is up to 3 characters, extension is always tmp). */ -TString MakeTempName(const char* wrkDir = nullptr, const char* prefix = "yandex", const char* extension = "tmp"); +std::string MakeTempName(const char* wrkDir = nullptr, const char* prefix = "yandex", const char* extension = "tmp"); diff --git a/util/system/tempfile_ut.cpp b/util/system/tempfile_ut.cpp index da43f5a8610..104b128a91f 100644 --- a/util/system/tempfile_ut.cpp +++ b/util/system/tempfile_ut.cpp @@ -10,7 +10,7 @@ Y_UNIT_TEST_SUITE(TTempFileHandle) { Y_UNIT_TEST(Create) { - TString path; + std::string path; { TTempFileHandle tmp; path = tmp.Name(); @@ -23,20 +23,20 @@ Y_UNIT_TEST_SUITE(TTempFileHandle) { Y_UNIT_TEST(InCurrentDir) { #ifndef _win32_ - static const TString TEST_PREFIX = "unique_prefix"; + static const std::string TEST_PREFIX = "unique_prefix"; #else - static const TString TEST_PREFIX = "uni"; + static const std::string TEST_PREFIX = "uni"; #endif - TString path; + std::string path; { TTempFileHandle tmp = TTempFileHandle::InCurrentDir(TEST_PREFIX); path = tmp.Name(); UNIT_ASSERT(NFs::Exists(path)); - std::vector names; + std::vector names; TFsPath(".").ListNames(names); - bool containsFileWithPrefix = std::any_of(names.begin(), names.end(), [&](const TString& name) { + bool containsFileWithPrefix = std::any_of(names.begin(), names.end(), [&](const std::string& name) { return name.Contains(TEST_PREFIX); }); UNIT_ASSERT(containsFileWithPrefix); @@ -45,7 +45,7 @@ Y_UNIT_TEST_SUITE(TTempFileHandle) { } Y_UNIT_TEST(UseExtensionWithoutDot) { - TString path; + std::string path; { TTempFileHandle tmp = TTempFileHandle::InCurrentDir("hello", "world"); path = tmp.Name(); @@ -64,7 +64,7 @@ Y_UNIT_TEST_SUITE(TTempFileHandle) { } Y_UNIT_TEST(UseExtensionWithDot) { - TString path; + std::string path; { TTempFileHandle tmp = TTempFileHandle::InCurrentDir("lorem", ".ipsum"); path = tmp.Name(); @@ -83,7 +83,7 @@ Y_UNIT_TEST_SUITE(TTempFileHandle) { } Y_UNIT_TEST(SafeDestructor) { - TString path; + std::string path; { path = MakeTempName(); UNIT_ASSERT(NFs::Exists(path)); @@ -100,7 +100,7 @@ Y_UNIT_TEST_SUITE(TTempFileHandle) { } Y_UNIT_TEST(RemovesOpen) { - TString path; + std::string path; { TTempFileHandle tmp; path = tmp.Name(); @@ -119,7 +119,7 @@ Y_UNIT_TEST_SUITE(TTempFileHandle) { Y_UNIT_TEST_SUITE(MakeTempName) { Y_UNIT_TEST(Default) { - TString path; + std::string path; { TTempFile tmp(MakeTempName()); path = tmp.Name(); @@ -138,7 +138,7 @@ Y_UNIT_TEST_SUITE(MakeTempName) { } Y_UNIT_TEST(UseNullptr) { - TString path; + std::string path; { TTempFile tmp(MakeTempName(nullptr, nullptr, nullptr)); path = tmp.Name(); diff --git a/util/system/thread.cpp b/util/system/thread.cpp index e25ce5327f0..aa0d591d85d 100644 --- a/util/system/thread.cpp +++ b/util/system/thread.cpp @@ -64,7 +64,7 @@ namespace { inline void SetThrName(const TParams& p) { try { - if (p.Name) { + if (!p.Name.empty()) { TThread::SetCurrentThreadName(p.Name.data()); } } catch (...) { @@ -176,7 +176,7 @@ namespace { { \ const int err_ = x; \ if (err_) { \ - ythrow TSystemError(err_) << TStringBuf(y); \ + ythrow TSystemError(err_) << std::string_view(y); \ } \ } @@ -446,7 +446,7 @@ namespace { Y_ABORT_UNLESS(SUCCEEDED(hr), "SetThreadDescription failed"); } - TString GetDescr() { + std::string GetDescr() { PWSTR wideName; auto hr = GetThreadDescription(GetCurrentThread(), &wideName); Y_ABORT_UNLESS(SUCCEEDED(hr), "GetThreadDescription failed"); @@ -490,7 +490,7 @@ void TThread::SetCurrentThreadName(const char* name) { #endif // OS } -TString TThread::CurrentThreadName() { +std::string TThread::CurrentThreadName() { #if defined(_freebsd_) // TODO: check pthread_get_name_np API availability #elif defined(_linux_) diff --git a/util/system/thread.h b/util/system/thread.h index 830c98a5c30..ca48520ed8a 100644 --- a/util/system/thread.h +++ b/util/system/thread.h @@ -5,7 +5,8 @@ /// @see SystemThreadFactory() #include -#include + +#include #include "defaults.h" #include "progname.h" @@ -28,7 +29,7 @@ class TThread { size_t StackSize; void* StackPointer; // See comments for `SetCurrentThreadName` - TString Name = GetProgramName(); + std::string Name = GetProgramName(); inline TParams() : Proc(nullptr) @@ -54,7 +55,7 @@ class TThread { { } - inline TParams& SetName(const TString& name) noexcept { + inline TParams& SetName(const std::string& name) noexcept { Name = name; return *this; @@ -120,7 +121,7 @@ class TThread { static void SetCurrentThreadName(const char* name); // NOTE: Will return empty string where CanGetCurrentThreadName() returns false. - static TString CurrentThreadName(); + static std::string CurrentThreadName(); // NOTE: Depends on a platform version. // Will return true for Darwin, Linux or fresh Windows 10. diff --git a/util/system/thread_ut.cpp b/util/system/thread_ut.cpp index 24eeee5016d..4e6766b793d 100644 --- a/util/system/thread_ut.cpp +++ b/util/system/thread_ut.cpp @@ -62,7 +62,7 @@ Y_UNIT_TEST_SUITE(TSysThreadTest) { } void* ThreadProc4(void*) { - const TString setName = "ThreadName"; + const std::string setName = "ThreadName"; TThread::SetCurrentThreadName(setName.data()); const auto getName = TThread::CurrentThreadName(); @@ -88,7 +88,7 @@ Y_UNIT_TEST_SUITE(TSysThreadTest) { } void* ThreadProcParent(void*) { - const TString setName = "Parent"; + const std::string setName = "Parent"; TThread::SetCurrentThreadName(setName.data()); TThread thread(&ThreadProcChild, nullptr); diff --git a/util/system/tls.cpp b/util/system/tls.cpp index b97f59104c3..7968c56310f 100644 --- a/util/system/tls.cpp +++ b/util/system/tls.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include @@ -183,7 +182,7 @@ namespace { class TGenericTls: public TGenericTlsBase { public: virtual TPerThreadStorage* MyStorageSlow() { - auto lock = Guard(Lock_); + std::lock_guard guard(Lock_); { TPTSRef& ret = Datas_[TThread::CurrentThreadId()]; @@ -197,9 +196,8 @@ namespace { } inline void Cleanup() noexcept { - with_lock (Lock_) { - Datas_.erase(TThread::CurrentThreadId()); - } + std::lock_guard guard(Lock_); + Datas_.erase(TThread::CurrentThreadId()); } static inline TGenericTls* Instance() { @@ -208,7 +206,7 @@ namespace { private: using TPTSRef = THolder; - TMutex Lock_; + std::mutex Lock_; THashMap Datas_; }; } diff --git a/util/system/type_name.cpp b/util/system/type_name.cpp index 52f47a1ddd6..be18b84622d 100644 --- a/util/system/type_name.cpp +++ b/util/system/type_name.cpp @@ -38,20 +38,20 @@ const char* NPrivate::TCppDemangler::Demangle(const char* name) { #endif } -TString CppDemangle(const TString& name) { +std::string CppDemangle(const std::string& name) { return NPrivate::TCppDemangler().Demangle(name.data()); } -TString TypeName(const std::type_info& typeInfo) { - TString demangled = CppDemangle(typeInfo.name()); // NOLINT(arcadia-typeid-name-restriction) +std::string TypeName(const std::type_info& typeInfo) { + std::string demangled = CppDemangle(typeInfo.name()); // NOLINT(arcadia-typeid-name-restriction) #if defined(_linux_) || defined(_darwin_) SubstGlobal(demangled, STD_ABI_PREFIX, STD_PREFIX); #endif return demangled; } -TString TypeName(const std::type_index& typeIndex) { - TString demangled = CppDemangle(typeIndex.name()); +std::string TypeName(const std::type_index& typeIndex) { + std::string demangled = CppDemangle(typeIndex.name()); #if defined(_linux_) || defined(_darwin_) SubstGlobal(demangled, STD_ABI_PREFIX, STD_PREFIX); #endif diff --git a/util/system/type_name.h b/util/system/type_name.h index b6619aba3fa..d4347369449 100644 --- a/util/system/type_name.h +++ b/util/system/type_name.h @@ -1,23 +1,22 @@ #pragma once -#include #include #include #include // Consider using TypeName function family. -TString CppDemangle(const TString& name); +std::string CppDemangle(const std::string& name); // TypeName function family return human readable type name. -TString TypeName(const std::type_info& typeInfo); -TString TypeName(const std::type_index& typeInfo); +std::string TypeName(const std::type_info& typeInfo); +std::string TypeName(const std::type_index& typeInfo); // Works for types known at compile-time // (thus, does not take any inheritance into account) template -inline TString TypeName() { +inline std::string TypeName() { return TypeName(typeid(T)); } @@ -25,6 +24,6 @@ inline TString TypeName() { // Also, distinguishes between T, T*, T const*, T volatile*, T const volatile*, // but not T and T const. template -inline TString TypeName(const T& t) { +inline std::string TypeName(const T& t) { return TypeName(typeid(t)); } diff --git a/util/system/type_name_ut.cpp b/util/system/type_name_ut.cpp index 86597f4232c..d5167b4362e 100644 --- a/util/system/type_name_ut.cpp +++ b/util/system/type_name_ut.cpp @@ -47,7 +47,7 @@ namespace NUtil::NTypeNameTest { class TFromThis { public: - TString GetTypeName() const { + std::string GetTypeName() const { return TypeName(*this); } }; @@ -78,10 +78,10 @@ Y_UNIT_TEST_SUITE(TypeName) { Y_UNIT_TEST(FromArcadiaTypes) { #ifdef _MSC_VER UNIT_ASSERT_VALUES_EQUAL(TypeName(), "class yexception"); - UNIT_ASSERT_VALUES_EQUAL(TypeName(), "class TBasicString >"); + UNIT_ASSERT_VALUES_EQUAL(TypeName(), "class std::basic_string >"); #else UNIT_ASSERT_VALUES_EQUAL(TypeName(), "yexception"); - UNIT_ASSERT_VALUES_EQUAL(TypeName(), "TBasicString >"); + UNIT_ASSERT_VALUES_EQUAL(TypeName(), "std::basic_string >"); #endif } diff --git a/util/system/user.cpp b/util/system/user.cpp index 05439f8939d..4d74d0ea190 100644 --- a/util/system/user.cpp +++ b/util/system/user.cpp @@ -11,10 +11,10 @@ #include #endif -TString GetUsername() { +std::string GetUsername() { for (const auto& var : {"LOGNAME", "USER", "LNAME", "USERNAME"}) { - TString val = GetEnv(var); - if (val) { + std::string val = GetEnv(var); + if (!val.empty()) { return val; } } @@ -30,22 +30,22 @@ TString GetUsername() { else ythrow TSystemError(err) << " GetUserName failed"; } else { - return TString(nameBuf.Data(), (size_t)(len - 1)); + return std::string(nameBuf.Data(), (size_t)(len - 1)); } #elif defined(_bionic_) const passwd* pwd = getpwuid(geteuid()); if (pwd) { - return TString(pwd->pw_name); + return std::string(pwd->pw_name); } - ythrow TSystemError() << TStringBuf(" getpwuid failed"); + ythrow TSystemError() << std::string_view(" getpwuid failed"); #else passwd pwd; passwd* tmpPwd; int err = getpwuid_r(geteuid(), &pwd, nameBuf.Data(), nameBuf.Size(), &tmpPwd); if (err == 0 && tmpPwd) { - return TString(pwd.pw_name); + return std::string(pwd.pw_name); } else if (err == ERANGE) { nameBuf = TTempBuf(nameBuf.Size() * 2); } else { diff --git a/util/system/user.h b/util/system/user.h index be348d1cee6..e25c808c305 100644 --- a/util/system/user.h +++ b/util/system/user.h @@ -2,4 +2,4 @@ #include -TString GetUsername(); +std::string GetUsername(); diff --git a/util/system/yassert.cpp b/util/system/yassert.cpp index 0f586648b78..cd77dc82a1c 100644 --- a/util/system/yassert.cpp +++ b/util/system/yassert.cpp @@ -7,8 +7,6 @@ #include #include -#include -#include #include #include #include @@ -35,7 +33,7 @@ namespace { }; } namespace NPrivate { - [[noreturn]] Y_NO_INLINE void InternalPanicImpl(int line, const char* function, const char* expr, int, int, int, const TStringBuf file, const char* errorMessage, size_t errorMessageSize) noexcept; + [[noreturn]] Y_NO_INLINE void InternalPanicImpl(int line, const char* function, const char* expr, int, int, int, const std::string_view file, const char* errorMessage, size_t errorMessageSize) noexcept; } void ::NPrivate::Panic(const TStaticBuf& file, int line, const char* function, const char* expr, const char* format, ...) noexcept { @@ -44,7 +42,7 @@ void ::NPrivate::Panic(const TStaticBuf& file, int line, const char* function, c // Otherwise stderr could contain multiple messages and stack traces shuffled auto guard = Guard(*Singleton()); - TString errorMsg; + std::string errorMsg; va_list args; va_start(args, format); // format has " " prefix to mute GCC warning on empty format @@ -52,7 +50,7 @@ void ::NPrivate::Panic(const TStaticBuf& file, int line, const char* function, c va_end(args); constexpr int abiPlaceholder = 0; - ::NPrivate::InternalPanicImpl(line, function, expr, abiPlaceholder, abiPlaceholder, abiPlaceholder, file.As(), errorMsg.c_str(), errorMsg.size()); + ::NPrivate::InternalPanicImpl(line, function, expr, abiPlaceholder, abiPlaceholder, abiPlaceholder, file.As(), errorMsg.c_str(), errorMsg.size()); } catch (...) { // ¯\_(ツ)_/¯ } @@ -61,11 +59,11 @@ void ::NPrivate::Panic(const TStaticBuf& file, int line, const char* function, c } namespace NPrivate { - [[noreturn]] Y_NO_INLINE void InternalPanicImpl(int line, const char* function, const char* expr, int, int, int, const TStringBuf file, const char* errorMessage, size_t errorMessageSize) noexcept try { - TStringBuf errorMsg{errorMessage, errorMessageSize}; - const TString now = TInstant::Now().ToStringLocal(); + [[noreturn]] Y_NO_INLINE void InternalPanicImpl(int line, const char* function, const char* expr, int, int, int, const std::string_view file, const char* errorMessage, size_t errorMessageSize) noexcept try { + std::string_view errorMsg{errorMessage, errorMessageSize}; + const std::string now = TInstant::Now().ToStringLocal(); - TString r; + std::string r; TStringOutput o(r); if (expr) { o << "VERIFY failed (" << now << "): " << errorMsg << Endl; diff --git a/util/thread/fwd.h b/util/thread/fwd.h index 6f1caed21c4..1e7479daab2 100644 --- a/util/thread/fwd.h +++ b/util/thread/fwd.h @@ -2,17 +2,6 @@ #include -struct TDefaultLFCounter; - -template -class TLockFreeQueue; - -template -class TAutoLockFreeQueue; - -template -class TLockFreeStack; - class IThreadFactory; struct IObjectInQueue; diff --git a/util/thread/lfqueue.cpp b/util/thread/lfqueue.cpp deleted file mode 100644 index 5861999b78b..00000000000 --- a/util/thread/lfqueue.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "lfqueue.h" diff --git a/util/thread/lfqueue.h b/util/thread/lfqueue.h deleted file mode 100644 index bfe88013fc2..00000000000 --- a/util/thread/lfqueue.h +++ /dev/null @@ -1,399 +0,0 @@ -#pragma once - -#include "fwd.h" -#include "lfstack.h" - -#include -#include - -#include - -struct TDefaultLFCounter { - template - void IncCount(const T& data) { - (void)data; - } - template - void DecCount(const T& data) { - (void)data; - } -}; - -// @brief lockfree queue -// @tparam T - the queue element, should be movable -// @tparam TCounter, a observer class to count number of items in queue -// be careful, IncCount and DecCount can be called on a moved object and -// it is TCounter class responsibility to check validity of passed object -template -class TLockFreeQueue: public TNonCopyable { - struct TListNode { - template - TListNode(U&& u, TListNode* next) - : Next(next) - , Data(std::forward(u)) - { - } - - template - explicit TListNode(U&& u) - : Data(std::forward(u)) - { - } - - std::atomic Next; - T Data; - }; - - // using inheritance to be able to use 0 bytes for TCounter when we don't need one - struct TRootNode: public TCounter { - std::atomic PushQueue = nullptr; - std::atomic PopQueue = nullptr; - std::atomic ToDelete = nullptr; - std::atomic NextFree = nullptr; - - void CopyCounter(TRootNode* x) { - *(TCounter*)this = *(TCounter*)x; - } - }; - - static void EraseList(TListNode* n) { - while (n) { - TListNode* keepNext = n->Next.load(std::memory_order_acquire); - delete n; - n = keepNext; - } - } - - alignas(64) std::atomic JobQueue; - alignas(64) std::atomic FreememCounter; - alignas(64) std::atomic FreeingTaskCounter; - alignas(64) std::atomic FreePtr; - - void TryToFreeAsyncMemory() { - const auto keepCounter = FreeingTaskCounter.load(); - TRootNode* current = FreePtr.load(std::memory_order_acquire); - if (current == nullptr) - return; - if (FreememCounter.load() == 1) { - // we are the last thread, try to cleanup - // check if another thread have cleaned up - if (keepCounter != FreeingTaskCounter.load()) { - return; - } - if (FreePtr.compare_exchange_strong(current, nullptr)) { - // free list - while (current) { - TRootNode* p = current->NextFree.load(std::memory_order_acquire); - EraseList(current->ToDelete.load(std::memory_order_acquire)); - delete current; - current = p; - } - ++FreeingTaskCounter; - } - } - } - void AsyncRef() { - ++FreememCounter; - } - void AsyncUnref() { - TryToFreeAsyncMemory(); - --FreememCounter; - } - void AsyncDel(TRootNode* toDelete, TListNode* lst) { - toDelete->ToDelete.store(lst, std::memory_order_release); - for (auto freePtr = FreePtr.load();;) { - toDelete->NextFree.store(freePtr, std::memory_order_release); - if (FreePtr.compare_exchange_weak(freePtr, toDelete)) - break; - } - } - void AsyncUnref(TRootNode* toDelete, TListNode* lst) { - TryToFreeAsyncMemory(); - if (--FreememCounter == 0) { - // no other operations in progress, can safely reclaim memory - EraseList(lst); - delete toDelete; - } else { - // Dequeue()s in progress, put node to free list - AsyncDel(toDelete, lst); - } - } - - struct TListInvertor { - TListNode* Copy; - TListNode* Tail; - TListNode* PrevFirst; - - TListInvertor() - : Copy(nullptr) - , Tail(nullptr) - , PrevFirst(nullptr) - { - } - ~TListInvertor() { - EraseList(Copy); - } - void CopyWasUsed() { - Copy = nullptr; - Tail = nullptr; - PrevFirst = nullptr; - } - void DoCopy(TListNode* ptr) { - TListNode* newFirst = ptr; - TListNode* newCopy = nullptr; - TListNode* newTail = nullptr; - while (ptr) { - if (ptr == PrevFirst) { - // short cut, we have copied this part already - Tail->Next.store(newCopy, std::memory_order_release); - newCopy = Copy; - Copy = nullptr; // do not destroy prev try - if (!newTail) - newTail = Tail; // tried to invert same list - break; - } - TListNode* newElem = new TListNode(ptr->Data, newCopy); - newCopy = newElem; - ptr = ptr->Next.load(std::memory_order_acquire); - if (!newTail) - newTail = newElem; - } - EraseList(Copy); // copy was useless - Copy = newCopy; - PrevFirst = newFirst; - Tail = newTail; - } - }; - - void EnqueueImpl(TListNode* head, TListNode* tail) { - TRootNode* newRoot = new TRootNode; - AsyncRef(); - newRoot->PushQueue.store(head, std::memory_order_release); - for (TRootNode* curRoot = JobQueue.load(std::memory_order_acquire);;) { - tail->Next.store(curRoot->PushQueue.load(std::memory_order_acquire), std::memory_order_release); - newRoot->PopQueue.store(curRoot->PopQueue.load(std::memory_order_acquire), std::memory_order_release); - newRoot->CopyCounter(curRoot); - - for (TListNode* node = head;; node = node->Next.load(std::memory_order_acquire)) { - newRoot->IncCount(node->Data); - if (node == tail) - break; - } - - if (JobQueue.compare_exchange_weak(curRoot, newRoot)) { - AsyncUnref(curRoot, nullptr); - break; - } - } - } - - template - static void FillCollection(TListNode* lst, TCollection* res) { - while (lst) { - res->emplace_back(std::move(lst->Data)); - lst = lst->Next.load(std::memory_order_acquire); - } - } - - /** Traverses a given list simultaneously creating its inversed version. - * After that, fills a collection with a reversed version and returns the last visited lst's node. - */ - template - static TListNode* FillCollectionReverse(TListNode* lst, TCollection* res) { - if (!lst) { - return nullptr; - } - - TListNode* newCopy = nullptr; - do { - TListNode* newElem = new TListNode(std::move(lst->Data), newCopy); - newCopy = newElem; - lst = lst->Next.load(std::memory_order_acquire); - } while (lst); - - FillCollection(newCopy, res); - EraseList(newCopy); - - return lst; - } - -public: - TLockFreeQueue() - : JobQueue(new TRootNode) - , FreememCounter(0) - , FreeingTaskCounter(0) - , FreePtr(nullptr) - { - } - ~TLockFreeQueue() { - AsyncRef(); - AsyncUnref(); // should free FreeList - EraseList(JobQueue.load(std::memory_order_relaxed)->PushQueue.load(std::memory_order_relaxed)); - EraseList(JobQueue.load(std::memory_order_relaxed)->PopQueue.load(std::memory_order_relaxed)); - delete JobQueue; - } - template - void Enqueue(U&& data) { - TListNode* newNode = new TListNode(std::forward(data)); - EnqueueImpl(newNode, newNode); - } - void Enqueue(T&& data) { - TListNode* newNode = new TListNode(std::move(data)); - EnqueueImpl(newNode, newNode); - } - void Enqueue(const T& data) { - TListNode* newNode = new TListNode(data); - EnqueueImpl(newNode, newNode); - } - template - void EnqueueAll(const TCollection& data) { - EnqueueAll(data.begin(), data.end()); - } - template - void EnqueueAll(TIter dataBegin, TIter dataEnd) { - if (dataBegin == dataEnd) - return; - - TIter i = dataBegin; - TListNode* node = new TListNode(*i); - TListNode* tail = node; - - for (++i; i != dataEnd; ++i) { - TListNode* nextNode = node; - node = new TListNode(*i, nextNode); - } - EnqueueImpl(node, tail); - } - bool Dequeue(T* data) { - TRootNode* newRoot = nullptr; - TListInvertor listInvertor; - AsyncRef(); - for (TRootNode* curRoot = JobQueue.load(std::memory_order_acquire);;) { - TListNode* tail = curRoot->PopQueue.load(std::memory_order_acquire); - if (tail) { - // has elems to pop - if (!newRoot) - newRoot = new TRootNode; - - newRoot->PushQueue.store(curRoot->PushQueue.load(std::memory_order_acquire), std::memory_order_release); - newRoot->PopQueue.store(tail->Next.load(std::memory_order_acquire), std::memory_order_release); - newRoot->CopyCounter(curRoot); - newRoot->DecCount(tail->Data); - Y_ASSERT(curRoot->PopQueue.load() == tail); - if (JobQueue.compare_exchange_weak(curRoot, newRoot)) { - *data = std::move(tail->Data); - tail->Next.store(nullptr, std::memory_order_release); - AsyncUnref(curRoot, tail); - return true; - } - continue; - } - if (curRoot->PushQueue.load(std::memory_order_acquire) == nullptr) { - delete newRoot; - AsyncUnref(); - return false; // no elems to pop - } - - if (!newRoot) - newRoot = new TRootNode; - newRoot->PushQueue.store(nullptr, std::memory_order_release); - listInvertor.DoCopy(curRoot->PushQueue.load(std::memory_order_acquire)); - newRoot->PopQueue.store(listInvertor.Copy, std::memory_order_release); - newRoot->CopyCounter(curRoot); - Y_ASSERT(curRoot->PopQueue.load() == nullptr); - if (JobQueue.compare_exchange_weak(curRoot, newRoot)) { - AsyncDel(curRoot, curRoot->PushQueue.load(std::memory_order_acquire)); - curRoot = newRoot; - newRoot = nullptr; - listInvertor.CopyWasUsed(); - } else { - newRoot->PopQueue.store(nullptr, std::memory_order_release); - } - } - } - template - void DequeueAll(TCollection* res) { - AsyncRef(); - - TRootNode* newRoot = new TRootNode; - TRootNode* curRoot = JobQueue.load(std::memory_order_acquire); - do { - } while (!JobQueue.compare_exchange_weak(curRoot, newRoot)); - - FillCollection(curRoot->PopQueue, res); - - TListNode* toDeleteHead = curRoot->PushQueue; - TListNode* toDeleteTail = FillCollectionReverse(curRoot->PushQueue, res); - curRoot->PushQueue.store(nullptr, std::memory_order_release); - - if (toDeleteTail) { - toDeleteTail->Next.store(curRoot->PopQueue.load()); - } else { - toDeleteTail = curRoot->PopQueue; - } - curRoot->PopQueue.store(nullptr, std::memory_order_release); - - AsyncUnref(curRoot, toDeleteHead); - } - bool IsEmpty() { - AsyncRef(); - TRootNode* curRoot = JobQueue.load(std::memory_order_acquire); - bool res = curRoot->PushQueue.load(std::memory_order_acquire) == nullptr && - curRoot->PopQueue.load(std::memory_order_acquire) == nullptr; - AsyncUnref(); - return res; - } - TCounter GetCounter() { - AsyncRef(); - TRootNode* curRoot = JobQueue.load(std::memory_order_acquire); - TCounter res = *(TCounter*)curRoot; - AsyncUnref(); - return res; - } -}; - -template -class TAutoLockFreeQueue { -public: - using TRef = THolder; - - inline ~TAutoLockFreeQueue() { - TRef tmp; - - while (Dequeue(&tmp)) { - } - } - - inline bool Dequeue(TRef* t) { - T* res = nullptr; - - if (Queue.Dequeue(&res)) { - t->Reset(res); - - return true; - } - - return false; - } - - inline void Enqueue(TRef& t) { - Queue.Enqueue(t.Get()); - Y_UNUSED(t.Release()); - } - - inline void Enqueue(TRef&& t) { - Queue.Enqueue(t.Get()); - Y_UNUSED(t.Release()); - } - - inline bool IsEmpty() { - return Queue.IsEmpty(); - } - - inline TCounter GetCounter() { - return Queue.GetCounter(); - } - -private: - TLockFreeQueue Queue; -}; diff --git a/util/thread/lfqueue_ut.cpp b/util/thread/lfqueue_ut.cpp deleted file mode 100644 index 08819b70f42..00000000000 --- a/util/thread/lfqueue_ut.cpp +++ /dev/null @@ -1,331 +0,0 @@ -#include -#include - -#include -#include -#include -#include - -#include "lfqueue.h" - -class TMoveTest { -public: - TMoveTest(int marker = 0, int value = 0) - : Marker_(marker) - , Value_(value) - { - } - - TMoveTest(const TMoveTest& other) { - *this = other; - } - - TMoveTest(TMoveTest&& other) { - *this = std::move(other); - } - - TMoveTest& operator=(const TMoveTest& other) { - Value_ = other.Value_; - Marker_ = other.Marker_ + 1024; - return *this; - } - - TMoveTest& operator=(TMoveTest&& other) { - Value_ = other.Value_; - Marker_ = other.Marker_; - other.Marker_ = 0; - return *this; - } - - int Marker() const { - return Marker_; - } - - int Value() const { - return Value_; - } - -private: - int Marker_ = 0; - int Value_ = 0; -}; - -class TOperationsChecker { -public: - TOperationsChecker() { - ++DefaultCtor_; - } - - TOperationsChecker(TOperationsChecker&&) { - ++MoveCtor_; - } - - TOperationsChecker(const TOperationsChecker&) { - ++CopyCtor_; - } - - TOperationsChecker& operator=(TOperationsChecker&&) { - ++MoveAssign_; - return *this; - } - - TOperationsChecker& operator=(const TOperationsChecker&) { - ++CopyAssign_; - return *this; - } - - static void Check(int defaultCtor, int moveCtor, int copyCtor, int moveAssign, int copyAssign) { - UNIT_ASSERT_VALUES_EQUAL(defaultCtor, DefaultCtor_); - UNIT_ASSERT_VALUES_EQUAL(moveCtor, MoveCtor_); - UNIT_ASSERT_VALUES_EQUAL(copyCtor, CopyCtor_); - UNIT_ASSERT_VALUES_EQUAL(moveAssign, MoveAssign_); - UNIT_ASSERT_VALUES_EQUAL(copyAssign, CopyAssign_); - Clear(); - } - -private: - static void Clear() { - DefaultCtor_ = MoveCtor_ = CopyCtor_ = MoveAssign_ = CopyAssign_ = 0; - } - - static int DefaultCtor_; - static int MoveCtor_; - static int CopyCtor_; - static int MoveAssign_; - static int CopyAssign_; -}; - -int TOperationsChecker::DefaultCtor_ = 0; -int TOperationsChecker::MoveCtor_ = 0; -int TOperationsChecker::CopyCtor_ = 0; -int TOperationsChecker::MoveAssign_ = 0; -int TOperationsChecker::CopyAssign_ = 0; - -Y_UNIT_TEST_SUITE(TLockFreeQueueTests) { - Y_UNIT_TEST(TestMoveEnqueue) { - TMoveTest value(0xFF, 0xAA); - TMoveTest tmp; - - TLockFreeQueue queue; - - queue.Enqueue(value); - UNIT_ASSERT_VALUES_EQUAL(value.Marker(), 0xFF); - UNIT_ASSERT(queue.Dequeue(&tmp)); - UNIT_ASSERT_VALUES_UNEQUAL(tmp.Marker(), 0xFF); - UNIT_ASSERT_VALUES_EQUAL(tmp.Value(), 0xAA); - - queue.Enqueue(std::move(value)); - UNIT_ASSERT_VALUES_EQUAL(value.Marker(), 0); - UNIT_ASSERT(queue.Dequeue(&tmp)); - UNIT_ASSERT_VALUES_EQUAL(tmp.Value(), 0xAA); - } - - Y_UNIT_TEST(TestSimpleEnqueueDequeue) { - TLockFreeQueue queue; - - int i = -1; - - UNIT_ASSERT(!queue.Dequeue(&i)); - UNIT_ASSERT_VALUES_EQUAL(i, -1); - - queue.Enqueue(10); - queue.Enqueue(11); - queue.Enqueue(12); - - UNIT_ASSERT(queue.Dequeue(&i)); - UNIT_ASSERT_VALUES_EQUAL(10, i); - UNIT_ASSERT(queue.Dequeue(&i)); - UNIT_ASSERT_VALUES_EQUAL(11, i); - - queue.Enqueue(13); - - UNIT_ASSERT(queue.Dequeue(&i)); - UNIT_ASSERT_VALUES_EQUAL(12, i); - UNIT_ASSERT(queue.Dequeue(&i)); - UNIT_ASSERT_VALUES_EQUAL(13, i); - - UNIT_ASSERT(!queue.Dequeue(&i)); - - const int tmp = 100; - queue.Enqueue(tmp); - UNIT_ASSERT(queue.Dequeue(&i)); - UNIT_ASSERT_VALUES_EQUAL(i, tmp); - } - - Y_UNIT_TEST(TestSimpleEnqueueAllDequeue) { - TLockFreeQueue queue; - - int i = -1; - - UNIT_ASSERT(!queue.Dequeue(&i)); - UNIT_ASSERT_VALUES_EQUAL(i, -1); - - std::vector v; - v.push_back(20); - v.push_back(21); - - queue.EnqueueAll(v); - - v.clear(); - v.push_back(22); - v.push_back(23); - v.push_back(24); - - queue.EnqueueAll(v); - - v.clear(); - queue.EnqueueAll(v); - - v.clear(); - v.push_back(25); - - queue.EnqueueAll(v); - - for (int j = 20; j <= 25; ++j) { - UNIT_ASSERT(queue.Dequeue(&i)); - UNIT_ASSERT_VALUES_EQUAL(j, i); - } - - UNIT_ASSERT(!queue.Dequeue(&i)); - } - - void DequeueAllRunner(TLockFreeQueue& queue, bool singleConsumer) { - size_t threadsNum = 4; - size_t enqueuesPerThread = 10'000; - TThreadPool p; - p.Start(threadsNum, 0); - - std::vector> futures; - - for (size_t i = 0; i < threadsNum; ++i) { - NThreading::TPromise promise = NThreading::NewPromise(); - futures.emplace_back(promise.GetFuture()); - - p.SafeAddFunc([enqueuesPerThread, &queue, promise]() mutable { - for (size_t i = 0; i != enqueuesPerThread; ++i) { - queue.Enqueue(i); - } - - promise.SetValue(); - }); - } - - std::atomic elementsLeft = threadsNum * enqueuesPerThread; - - ui64 numOfConsumers = singleConsumer ? 1 : threadsNum; - - std::vector> dataBuckets(numOfConsumers); - - for (size_t i = 0; i < numOfConsumers; ++i) { - NThreading::TPromise promise = NThreading::NewPromise(); - futures.emplace_back(promise.GetFuture()); - - p.SafeAddFunc([&queue, &elementsLeft, promise, consumerData{&dataBuckets[i]}]() mutable { - std::vector vec; - while (static_cast(elementsLeft.load()) > 0) { - for (size_t i = 0; i != 100; ++i) { - vec.clear(); - queue.DequeueAll(&vec); - - elementsLeft -= vec.size(); - consumerData->insert(consumerData->end(), vec.begin(), vec.end()); - } - } - - promise.SetValue(); - }); - } - - NThreading::WaitExceptionOrAll(futures).GetValueSync(); - p.Stop(); - - std::vector left; - queue.DequeueAll(&left); - - UNIT_ASSERT(left.empty()); - - std::vector data; - for (auto& dataBucket : dataBuckets) { - data.insert(data.end(), dataBucket.begin(), dataBucket.end()); - } - - UNIT_ASSERT_EQUAL(data.size(), threadsNum * enqueuesPerThread); - - size_t threadIdx = 0; - size_t cntValue = 0; - - Sort(data.begin(), data.end()); - for (size_t i = 0; i != data.size(); ++i) { - UNIT_ASSERT_VALUES_EQUAL(cntValue, data[i]); - ++threadIdx; - - if (threadIdx == threadsNum) { - ++cntValue; - threadIdx = 0; - } - } - } - - Y_UNIT_TEST(TestDequeueAllSingleConsumer) { - TLockFreeQueue queue; - DequeueAllRunner(queue, true); - } - - Y_UNIT_TEST(TestDequeueAllMultipleConsumers) { - TLockFreeQueue queue; - DequeueAllRunner(queue, false); - } - - Y_UNIT_TEST(TestDequeueAllEmptyQueue) { - TLockFreeQueue queue; - std::vector vec; - - queue.DequeueAll(&vec); - - UNIT_ASSERT(vec.empty()); - } - - Y_UNIT_TEST(TestDequeueAllQueueOrder) { - TLockFreeQueue queue; - queue.Enqueue(1); - queue.Enqueue(2); - queue.Enqueue(3); - - std::vector v; - queue.DequeueAll(&v); - - UNIT_ASSERT_VALUES_EQUAL(v.size(), 3); - UNIT_ASSERT_VALUES_EQUAL(v[0], 1); - UNIT_ASSERT_VALUES_EQUAL(v[1], 2); - UNIT_ASSERT_VALUES_EQUAL(v[2], 3); - } - - Y_UNIT_TEST(CleanInDestructor) { - TSimpleSharedPtr p(new bool); - UNIT_ASSERT_VALUES_EQUAL(1u, p.RefCount()); - - { - TLockFreeQueue> stack; - - stack.Enqueue(p); - stack.Enqueue(p); - - UNIT_ASSERT_VALUES_EQUAL(3u, p.RefCount()); - } - - UNIT_ASSERT_VALUES_EQUAL(1, p.RefCount()); - } - - Y_UNIT_TEST(CheckOperationsCount) { - TOperationsChecker o; - o.Check(1, 0, 0, 0, 0); - TLockFreeQueue queue; - o.Check(0, 0, 0, 0, 0); - queue.Enqueue(std::move(o)); - o.Check(0, 1, 0, 0, 0); - queue.Enqueue(o); - o.Check(0, 0, 1, 0, 0); - queue.Dequeue(&o); - o.Check(0, 0, 2, 1, 0); - } -} diff --git a/util/thread/lfstack.cpp b/util/thread/lfstack.cpp deleted file mode 100644 index be8b3bdf377..00000000000 --- a/util/thread/lfstack.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "lfstack.h" diff --git a/util/thread/lfstack.h b/util/thread/lfstack.h deleted file mode 100644 index effde7c706d..00000000000 --- a/util/thread/lfstack.h +++ /dev/null @@ -1,192 +0,0 @@ -#pragma once - -#include - -#include -#include -#include - -////////////////////////////// -// lock free lifo stack -template -class TLockFreeStack: TNonCopyable { - struct TNode { - T Value; - std::atomic Next; - - TNode() = default; - - template - explicit TNode(U&& val) - : Value(std::forward(val)) - , Next(nullptr) - { - } - }; - - std::atomic Head = nullptr; - std::atomic FreePtr = nullptr; - std::atomic DequeueCount = 0; - - void TryToFreeMemory() { - TNode* current = FreePtr.load(std::memory_order_acquire); - if (!current) - return; - if (DequeueCount.load() == 1) { - // node current is in free list, we are the last thread so try to cleanup - if (FreePtr.compare_exchange_strong(current, nullptr)) - EraseList(current); - } - } - void EraseList(TNode* p) { - while (p) { - TNode* next = p->Next; - delete p; - p = next; - } - } - void EnqueueImpl(TNode* head, TNode* tail) { - auto headValue = Head.load(std::memory_order_acquire); - for (;;) { - tail->Next.store(headValue, std::memory_order_release); - // NB. See https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange - // The weak forms (1-2) of the functions are allowed to fail spuriously, that is, - // act as if *this != expected even if they are equal. - // When a compare-and-exchange is in a loop, the weak version will yield better - // performance on some platforms. - if (Head.compare_exchange_weak(headValue, head)) - break; - } - } - template - void EnqueueImpl(U&& u) { - TNode* node = new TNode(std::forward(u)); - EnqueueImpl(node, node); - } - -public: - TLockFreeStack() = default; - - ~TLockFreeStack() { - EraseList(Head.load()); - EraseList(FreePtr.load()); - } - - void Enqueue(const T& t) { - EnqueueImpl(t); - } - - void Enqueue(T&& t) { - EnqueueImpl(std::move(t)); - } - - template - void EnqueueAll(const TCollection& data) { - EnqueueAll(data.begin(), data.end()); - } - template - void EnqueueAll(TIter dataBegin, TIter dataEnd) { - if (dataBegin == dataEnd) { - return; - } - TIter i = dataBegin; - TNode* node = new TNode(*i); - TNode* tail = node; - - for (++i; i != dataEnd; ++i) { - TNode* nextNode = node; - node = new TNode(*i); - node->Next.store(nextNode, std::memory_order_release); - } - EnqueueImpl(node, tail); - } - bool Dequeue(T* res) { - ++DequeueCount; - for (TNode* current = Head.load(std::memory_order_acquire); current;) { - if (Head.compare_exchange_weak(current, current->Next.load(std::memory_order_acquire))) { - *res = std::move(current->Value); - // delete current; // ABA problem - // even more complex node deletion - TryToFreeMemory(); - if (--DequeueCount == 0) { - // no other Dequeue()s, can safely reclaim memory - delete current; - } else { - // Dequeue()s in progress, put node to free list - for (TNode* freePtr = FreePtr.load(std::memory_order_acquire);;) { - current->Next.store(freePtr, std::memory_order_release); - if (FreePtr.compare_exchange_weak(freePtr, current)) - break; - } - } - return true; - } - } - TryToFreeMemory(); - --DequeueCount; - return false; - } - // add all elements to *res - // elements are returned in order of dequeue (top to bottom; see example in unittest) - template - void DequeueAll(TCollection* res) { - ++DequeueCount; - for (TNode* current = Head.load(std::memory_order_acquire); current;) { - if (Head.compare_exchange_weak(current, nullptr)) { - for (TNode* x = current; x;) { - res->push_back(std::move(x->Value)); - x = x->Next; - } - // EraseList(current); // ABA problem - // even more complex node deletion - TryToFreeMemory(); - if (--DequeueCount == 0) { - // no other Dequeue()s, can safely reclaim memory - EraseList(current); - } else { - // Dequeue()s in progress, add nodes list to free list - TNode* currentLast = current; - while (currentLast->Next) { - currentLast = currentLast->Next; - } - for (TNode* freePtr = FreePtr.load(std::memory_order_acquire);;) { - currentLast->Next.store(freePtr, std::memory_order_release); - if (FreePtr.compare_exchange_weak(freePtr, current)) - break; - } - } - return; - } - } - TryToFreeMemory(); - --DequeueCount; - } - bool DequeueSingleConsumer(T* res) { - for (TNode* current = Head.load(std::memory_order_acquire); current;) { - if (Head.compare_exchange_weak(current, current->Next)) { - *res = std::move(current->Value); - delete current; // with single consumer thread ABA does not happen - return true; - } - } - return false; - } - // add all elements to *res - // elements are returned in order of dequeue (top to bottom; see example in unittest) - template - void DequeueAllSingleConsumer(TCollection* res) { - for (TNode* head = Head.load(std::memory_order_acquire); head;) { - if (Head.compare_exchange_weak(head, nullptr)) { - for (TNode* x = head; x;) { - res->push_back(std::move(x->Value)); - x = x->Next; - } - EraseList(head); // with single consumer thread ABA does not happen - return; - } - } - } - bool IsEmpty() { - return Head.load() == nullptr; // without lock, so result is approximate - } -}; diff --git a/util/thread/lfstack_ut.cpp b/util/thread/lfstack_ut.cpp deleted file mode 100644 index 6eaed9d9303..00000000000 --- a/util/thread/lfstack_ut.cpp +++ /dev/null @@ -1,347 +0,0 @@ - -#include "lfstack.h" - -#include -#include - -#include -#include - -#include - -Y_UNIT_TEST_SUITE(TLockFreeStackTests) { - class TCountDownLatch { - private: - std::atomic Current_; - TSystemEvent EventObject_; - - public: - TCountDownLatch(unsigned initial) - : Current_(initial) - { - } - - void CountDown() { - if (--Current_ == 0) { - EventObject_.Signal(); - } - } - - void Await() { - EventObject_.Wait(); - } - - bool Await(TDuration timeout) { - return EventObject_.WaitT(timeout); - } - }; - - template - struct TDequeueAllTester { - size_t EnqueueThreads; - size_t DequeueThreads; - - size_t EnqueuesPerThread; - std::atomic LeftToDequeue; - - TCountDownLatch StartLatch; - TLockFreeStack Stack; - - TDequeueAllTester() - : EnqueueThreads(4) - , DequeueThreads(SingleConsumer ? 1 : 3) - , EnqueuesPerThread(100000) - , LeftToDequeue(EnqueueThreads * EnqueuesPerThread) - , StartLatch(EnqueueThreads + DequeueThreads) - { - } - - void Enqueuer() { - StartLatch.CountDown(); - StartLatch.Await(); - - for (size_t i = 0; i < EnqueuesPerThread; ++i) { - Stack.Enqueue(i); - } - } - - void DequeuerAll() { - StartLatch.CountDown(); - StartLatch.Await(); - - std::vector temp; - while (LeftToDequeue.load() > 0) { - size_t dequeued = 0; - for (size_t i = 0; i < 100; ++i) { - temp.clear(); - if (SingleConsumer) { - Stack.DequeueAllSingleConsumer(&temp); - } else { - Stack.DequeueAll(&temp); - } - dequeued += temp.size(); - } - LeftToDequeue -= dequeued; - } - } - - void Run() { - std::vector>> futures; - - for (size_t i = 0; i < EnqueueThreads; ++i) { - futures.push_back(new NThreading::TLegacyFuture<>(std::bind(&TDequeueAllTester::Enqueuer, this))); - } - - for (size_t i = 0; i < DequeueThreads; ++i) { - futures.push_back(new NThreading::TLegacyFuture<>(std::bind(&TDequeueAllTester::DequeuerAll, this))); - } - - // effectively join - futures.clear(); - - UNIT_ASSERT_VALUES_EQUAL(0, int(LeftToDequeue.load())); - - std::vector left; - Stack.DequeueAll(&left); - UNIT_ASSERT(left.empty()); - } - }; - - Y_UNIT_TEST(TestDequeueAll) { - TDequeueAllTester().Run(); - } - - Y_UNIT_TEST(TestDequeueAllSingleConsumer) { - TDequeueAllTester().Run(); - } - - Y_UNIT_TEST(TestDequeueAllEmptyStack) { - TLockFreeStack stack; - - std::vector r; - stack.DequeueAll(&r); - - UNIT_ASSERT(r.empty()); - } - - Y_UNIT_TEST(TestDequeueAllReturnsInReverseOrder) { - TLockFreeStack stack; - - stack.Enqueue(17); - stack.Enqueue(19); - stack.Enqueue(23); - - std::vector r; - - stack.DequeueAll(&r); - - UNIT_ASSERT_VALUES_EQUAL(size_t(3), r.size()); - UNIT_ASSERT_VALUES_EQUAL(23, r.at(0)); - UNIT_ASSERT_VALUES_EQUAL(19, r.at(1)); - UNIT_ASSERT_VALUES_EQUAL(17, r.at(2)); - } - - Y_UNIT_TEST(TestEnqueueAll) { - TLockFreeStack stack; - - std::vector v; - std::vector expected; - - stack.EnqueueAll(v); // add empty - - v.push_back(2); - v.push_back(3); - v.push_back(5); - expected.insert(expected.end(), v.begin(), v.end()); - stack.EnqueueAll(v); - - v.clear(); - - stack.EnqueueAll(v); // add empty - - v.push_back(7); - v.push_back(11); - v.push_back(13); - v.push_back(17); - expected.insert(expected.end(), v.begin(), v.end()); - stack.EnqueueAll(v); - - std::vector actual; - stack.DequeueAll(&actual); - - UNIT_ASSERT_VALUES_EQUAL(expected.size(), actual.size()); - for (size_t i = 0; i < actual.size(); ++i) { - UNIT_ASSERT_VALUES_EQUAL(expected.at(expected.size() - i - 1), actual.at(i)); - } - } - - Y_UNIT_TEST(CleanInDestructor) { - TSimpleSharedPtr p(new bool); - UNIT_ASSERT_VALUES_EQUAL(1u, p.RefCount()); - - { - TLockFreeStack> stack; - - stack.Enqueue(p); - stack.Enqueue(p); - - UNIT_ASSERT_VALUES_EQUAL(3u, p.RefCount()); - } - - UNIT_ASSERT_VALUES_EQUAL(1, p.RefCount()); - } - - Y_UNIT_TEST(NoCopyTest) { - static unsigned copied = 0; - struct TCopyCount { - TCopyCount(int) { - } - TCopyCount(const TCopyCount&) { - ++copied; - } - - TCopyCount(TCopyCount&&) { - } - - TCopyCount& operator=(const TCopyCount&) { - ++copied; - return *this; - } - - TCopyCount& operator=(TCopyCount&&) { - return *this; - } - }; - - TLockFreeStack stack; - stack.Enqueue(TCopyCount(1)); - TCopyCount val(0); - stack.Dequeue(&val); - UNIT_ASSERT_VALUES_EQUAL(0, copied); - } - - Y_UNIT_TEST(MoveOnlyTest) { - TLockFreeStack> stack; - stack.Enqueue(MakeHolder(true)); - THolder val; - stack.Dequeue(&val); - UNIT_ASSERT(val); - UNIT_ASSERT_VALUES_EQUAL(true, *val); - } - - template - struct TMultiThreadTester { - using ThisType = TMultiThreadTester; - - size_t Threads; - size_t OperationsPerThread; - - TCountDownLatch StartLatch; - TLockFreeStack Stack; - - TMultiThreadTester() - : Threads(10) - , OperationsPerThread(100000) - , StartLatch(Threads) - { - } - - void Worker() { - StartLatch.CountDown(); - StartLatch.Await(); - - std::vector unused; - for (size_t i = 0; i < OperationsPerThread; ++i) { - switch (GetCycleCount() % 4) { - case 0: { - TTest::Enqueue(Stack, i); - break; - } - case 1: { - TTest::Dequeue(Stack); - break; - } - case 2: { - TTest::EnqueueAll(Stack); - break; - } - case 3: { - TTest::DequeueAll(Stack); - break; - } - } - } - } - - void Run() { - TDeque> futures; - - for (size_t i = 0; i < Threads; ++i) { - futures.emplace_back(std::bind(&ThisType::Worker, this)); - } - futures.clear(); - TTest::DequeueAll(Stack); - } - }; - - struct TFreeListTest { - using ValueType = int; - - static void Enqueue(TLockFreeStack& stack, size_t i) { - stack.Enqueue(static_cast(i)); - } - - static void Dequeue(TLockFreeStack& stack) { - int value; - stack.Dequeue(&value); - } - - static void EnqueueAll(TLockFreeStack& stack) { - std::vector values(5); - stack.EnqueueAll(values); - } - - static void DequeueAll(TLockFreeStack& stack) { - std::vector value; - stack.DequeueAll(&value); - } - }; - - // Test for catching thread sanitizer problems - Y_UNIT_TEST(TestFreeList) { - TMultiThreadTester().Run(); - } - - struct TMoveTest { - using ValueType = THolder; - - static void Enqueue(TLockFreeStack& stack, size_t i) { - stack.Enqueue(MakeHolder(static_cast(i))); - } - - static void Dequeue(TLockFreeStack& stack) { - ValueType value; - if (stack.Dequeue(&value)) { - UNIT_ASSERT(value); - } - } - - static void EnqueueAll(TLockFreeStack& stack) { - // there is no enqueAll with moving signature in LockFreeStack - Enqueue(stack, 0); - } - - static void DequeueAll(TLockFreeStack& stack) { - std::vector values; - stack.DequeueAll(&values); - for (auto& v : values) { - UNIT_ASSERT(v); - } - } - }; - - // Test for catching thread sanitizer problems - Y_UNIT_TEST(TesMultiThreadMove) { - TMultiThreadTester().Run(); - } -} diff --git a/util/thread/pool.cpp b/util/thread/pool.cpp index c005a760f63..6c9f270242b 100644 --- a/util/thread/pool.cpp +++ b/util/thread/pool.cpp @@ -6,7 +6,6 @@ #include #endif -#include #include #include #include @@ -17,8 +16,6 @@ #include #include -#include -#include #include #include @@ -26,6 +23,9 @@ #include "factory.h" #include "pool.h" +#include +#include + namespace { class TThreadNamer { public: @@ -48,12 +48,12 @@ namespace { } private: - void Set(const TString& name) { + void Set(const std::string& name) { TThread::SetCurrentThreadName(name.c_str()); } private: - TString ThreadName; + std::string ThreadName; bool EnumerateThreads = false; std::atomic Index{0}; }; @@ -108,12 +108,13 @@ class TThreadPool::TImpl: public TIntrusiveListItem, public IThreadFactor return true; } - with_lock (QueueMutex) { + { + std::unique_lock ulock(QueueMutex); while (MaxQueueSize > 0 && Queue.Size() >= MaxQueueSize && !ShouldTerminate.load()) { if (!Blocking) { return false; } - QueuePopCond.Wait(QueueMutex); + QueuePopCond.wait(ulock); } if (ShouldTerminate.load()) { @@ -123,7 +124,7 @@ class TThreadPool::TImpl: public TIntrusiveListItem, public IThreadFactor Queue.Push(obj); } - QueuePushCond.Signal(); + QueuePushCond.notify_one(); return true; } @@ -175,8 +176,9 @@ class TThreadPool::TImpl: public TIntrusiveListItem, public IThreadFactor inline void Stop() { ShouldTerminate.store(true); - with_lock (QueueMutex) { - QueuePopCond.BroadCast(); + { + std::lock_guard guard(QueueMutex); + QueuePopCond.notify_all(); } if (!NeedRestart()) { @@ -189,14 +191,14 @@ class TThreadPool::TImpl: public TIntrusiveListItem, public IThreadFactor } inline void WaitForComplete() noexcept { - with_lock (StopMutex) { - while (ThreadCountReal) { - with_lock (QueueMutex) { - QueuePushCond.Signal(); - } - - StopCond.Wait(StopMutex); + std::unique_lock ulock(StopMutex); + while (ThreadCountReal) { + { + std::lock_guard guard(QueueMutex); + QueuePushCond.notify_one(); } + + StopCond.wait(ulock); } } @@ -210,9 +212,10 @@ class TThreadPool::TImpl: public TIntrusiveListItem, public IThreadFactor while (true) { IObjectInQueue* job = nullptr; - with_lock (QueueMutex) { + { + std::unique_lock ulock(QueueMutex); while (Queue.Empty() && !ShouldTerminate.load()) { - QueuePushCond.Wait(QueueMutex); + QueuePushCond.wait(ulock); } if (ShouldTerminate.load() && Queue.Empty()) { @@ -224,7 +227,7 @@ class TThreadPool::TImpl: public TIntrusiveListItem, public IThreadFactor job = Queue.Pop(); } - QueuePopCond.Signal(); + QueuePopCond.notify_one(); if (Catching) { try { @@ -248,7 +251,7 @@ class TThreadPool::TImpl: public TIntrusiveListItem, public IThreadFactor auto guard = Guard(StopMutex); --ThreadCountReal; - StopCond.Signal(); + StopCond.notify_one(); } private: @@ -256,11 +259,11 @@ class TThreadPool::TImpl: public TIntrusiveListItem, public IThreadFactor const bool Blocking; const bool Catching; TThreadNamer Namer; - mutable TMutex QueueMutex; - mutable TMutex StopMutex; - TCondVar QueuePushCond; - TCondVar QueuePopCond; - TCondVar StopCond; + mutable std::mutex QueueMutex; + mutable std::mutex StopMutex; + std::condition_variable QueuePushCond; + std::condition_variable QueuePopCond; + std::condition_variable StopCond; TJobQueue Queue; std::vector Tharr; std::atomic ShouldTerminate; @@ -305,7 +308,7 @@ class TThreadPool::TImpl: public TIntrusiveListItem, public IThreadFactor } TIntrusiveList RegisteredObjects; - TMutex ActionMutex; + std::mutex ActionMutex; public: inline TAtforkQueueRestarter() { @@ -353,7 +356,7 @@ size_t TThreadPool::GetMaxQueueSize() const noexcept { } bool TThreadPool::Add(IObjectInQueue* obj) { - Y_ENSURE_EX(Impl_.Get(), TThreadPoolException() << TStringBuf("mtp queue not started")); + Y_ENSURE_EX(Impl_.Get(), TThreadPoolException() << "mtp queue not started"); if (Impl_->NeedRestart()) { Start(Impl_->GetThreadCountExpected(), Impl_->GetMaxQueueSize()); @@ -447,9 +450,10 @@ class TAdaptiveThreadPool::TImpl { } inline void Add(IObjectInQueue* obj) { - with_lock (Mutex_) { + { + std::unique_lock ulock(Mutex_); while (Obj_ != nullptr) { - CondFree_.Wait(Mutex_); + CondFree_.wait(ulock); } if (Free_ == 0) { @@ -458,19 +462,18 @@ class TAdaptiveThreadPool::TImpl { Obj_ = obj; - Y_ENSURE_EX(!AllDone_, TThreadPoolException() << TStringBuf("adding to a stopped queue")); + Y_ENSURE_EX(!AllDone_, TThreadPoolException() << "adding to a stopped queue"); } - CondReady_.Signal(); + CondReady_.notify_one(); } inline void AddThreads(size_t n) { - with_lock (Mutex_) { - while (n) { - AddThreadNoLock(); + std::lock_guard guard(Mutex_); + while (n) { + AddThreadNoLock(); - --n; - } + --n; } } @@ -500,37 +503,38 @@ class TAdaptiveThreadPool::TImpl { } inline void Stop() noexcept { - Mutex_.Acquire(); + Mutex_.lock(); AllDone_ = true; while (ThrCount_.load()) { - Mutex_.Release(); - CondReady_.Signal(); - Mutex_.Acquire(); + Mutex_.unlock(); + CondReady_.notify_one(); + Mutex_.lock(); } - Mutex_.Release(); + Mutex_.unlock(); } inline IObjectInQueue* WaitForJob() noexcept { - Mutex_.Acquire(); - - ++Free_; + IObjectInQueue* ret; + { + std::unique_lock ulock(Mutex_); + ++Free_; - while (!Obj_ && !AllDone_) { - if (!CondReady_.WaitT(Mutex_, IdleTime_)) { - break; + while (!Obj_ && !AllDone_) { + if (CondReady_.wait_for(ulock, std::chrono::microseconds(IdleTime_.MicroSeconds())) == std::cv_status::timeout) { + break; + } } - } - IObjectInQueue* ret = Obj_; - Obj_ = nullptr; + ret = Obj_; + Obj_ = nullptr; - --Free_; + --Free_; - Mutex_.Release(); - CondFree_.Signal(); + } + CondFree_.notify_one(); return ret; } @@ -540,9 +544,9 @@ class TAdaptiveThreadPool::TImpl { const bool Catching; TThreadNamer Namer; std::atomic ThrCount_; - TMutex Mutex_; - TCondVar CondReady_; - TCondVar CondFree_; + std::mutex Mutex_; + std::condition_variable CondReady_; + std::condition_variable CondFree_; bool AllDone_; IObjectInQueue* Obj_; size_t Free_; @@ -569,7 +573,7 @@ DEFINE_THREAD_POOL_CTORS(TSimpleThreadPool) TAdaptiveThreadPool::~TAdaptiveThreadPool() = default; bool TAdaptiveThreadPool::Add(IObjectInQueue* obj) { - Y_ENSURE_EX(Impl_.Get(), TThreadPoolException() << TStringBuf("mtp queue not started")); + Y_ENSURE_EX(Impl_.Get(), TThreadPoolException() << "mtp queue not started"); Impl_->Add(obj); @@ -593,7 +597,7 @@ size_t TAdaptiveThreadPool::Size() const noexcept { } void TAdaptiveThreadPool::SetMaxIdleTime(TDuration interval) { - Y_ENSURE_EX(Impl_.Get(), TThreadPoolException() << TStringBuf("mtp queue not started")); + Y_ENSURE_EX(Impl_.Get(), TThreadPoolException() << "mtp queue not started"); Impl_->SetMaxIdleTime(interval); } @@ -607,7 +611,7 @@ TSimpleThreadPool::~TSimpleThreadPool() { } bool TSimpleThreadPool::Add(IObjectInQueue* obj) { - Y_ENSURE_EX(Slave_.Get(), TThreadPoolException() << TStringBuf("mtp queue not started")); + Y_ENSURE_EX(Slave_.Get(), TThreadPoolException() << "mtp queue not started"); return Slave_->Add(obj); } @@ -663,11 +667,11 @@ namespace { } void IThreadPool::SafeAdd(IObjectInQueue* obj) { - Y_ENSURE_EX(Add(obj), TThreadPoolException() << TStringBuf("can not add object to queue")); + Y_ENSURE_EX(Add(obj), TThreadPoolException() << "can not add object to queue"); } void IThreadPool::SafeAddAndOwn(THolder obj) { - Y_ENSURE_EX(AddAndOwn(std::move(obj)), TThreadPoolException() << TStringBuf("can not add to queue and own")); + Y_ENSURE_EX(AddAndOwn(std::move(obj)), TThreadPoolException() << "can not add to queue and own"); } bool IThreadPool::AddAndOwn(THolder obj) { diff --git a/util/thread/pool.h b/util/thread/pool.h index f7422645f96..b7585efc467 100644 --- a/util/thread/pool.h +++ b/util/thread/pool.h @@ -10,6 +10,8 @@ #include #include +#include + class TDuration; struct IObjectInQueue { @@ -84,7 +86,7 @@ struct TThreadPoolParams { bool Catching_ = true; bool Blocking_ = false; IThreadFactory* Factory_ = SystemThreadFactory(); - TString ThreadName_; + std::string ThreadName_; bool EnumerateThreads_ = false; using TSelf = TThreadPoolParams; @@ -97,7 +99,7 @@ struct TThreadPoolParams { { } - TThreadPoolParams(const TString& name) { + TThreadPoolParams(const std::string& name) { SetThreadName(name); } @@ -120,13 +122,13 @@ struct TThreadPoolParams { return *this; } - TSelf& SetThreadName(const TString& name) { + TSelf& SetThreadName(const std::string& name) { ThreadName_ = name; EnumerateThreads_ = false; return *this; } - TSelf& SetThreadNamePrefix(const TString& prefix) { + TSelf& SetThreadNamePrefix(const std::string& prefix) { ThreadName_ = prefix; EnumerateThreads_ = true; return *this; @@ -150,7 +152,7 @@ class IThreadPool: public IThreadFactory, public TNonCopyable { template void SafeAddFunc(T&& func) { - Y_ENSURE_EX(AddFunc(std::forward(func)), TThreadPoolException() << TStringBuf("can not add function to queue")); + Y_ENSURE_EX(AddFunc(std::forward(func)), TThreadPoolException() << "can not add function to queue"); } void SafeAddAndOwn(THolder obj); diff --git a/util/thread/pool_ut.cpp b/util/thread/pool_ut.cpp index 85cbcdf6c97..1110c00d382 100644 --- a/util/thread/pool_ut.cpp +++ b/util/thread/pool_ut.cpp @@ -5,8 +5,9 @@ #include #include #include -#include -#include + +#include +#include struct TThreadPoolTest { TSpinLock Lock; @@ -25,7 +26,7 @@ struct TThreadPoolTest { void Process(void*) override { THolder This(this); - TGuard guard(Test->Lock); + std::lock_guard guard(Test->Lock); Test->R ^= Value; } }; @@ -178,9 +179,9 @@ Y_UNIT_TEST_SUITE(TThreadPoolTest) { queue.Stop(); } - void TestFixedThreadName(IThreadPool& pool, const TString& expectedName) { + void TestFixedThreadName(IThreadPool& pool, const std::string& expectedName) { pool.Start(1); - TString name; + std::string name; pool.SafeAddFunc([&name]() { name = TThread::CurrentThreadName(); }); @@ -192,7 +193,7 @@ Y_UNIT_TEST_SUITE(TThreadPoolTest) { } Y_UNIT_TEST(TestFixedThreadName) { - const TString expectedName = "HelloWorld"; + const std::string expectedName = "HelloWorld"; { TThreadPool pool(TThreadPool::TParams().SetBlocking(true).SetCatching(false).SetThreadName(expectedName)); TestFixedThreadName(pool, expectedName); @@ -203,24 +204,23 @@ Y_UNIT_TEST_SUITE(TThreadPoolTest) { } } - void TestEnumeratedThreadName(IThreadPool& pool, const THashSet& expectedNames) { + void TestEnumeratedThreadName(IThreadPool& pool, const THashSet& expectedNames) { pool.Start(expectedNames.size()); - TMutex lock; - TCondVar allReady; + std::mutex lock; + std::condition_variable allReady; size_t readyCount = 0; - THashSet names; + THashSet names; for (size_t i = 0; i < expectedNames.size(); ++i) { pool.SafeAddFunc([&]() { - with_lock (lock) { - if (++readyCount == expectedNames.size()) { - allReady.BroadCast(); - } else { - while (readyCount != expectedNames.size()) { - allReady.WaitI(lock); - } + std::unique_lock ulock(lock); + if (++readyCount == expectedNames.size()) { + allReady.notify_all(); + } else { + while (readyCount != expectedNames.size()) { + allReady.wait(ulock); } - names.insert(TThread::CurrentThreadName()); } + names.insert(TThread::CurrentThreadName()); }); } pool.Stop(); @@ -230,8 +230,8 @@ Y_UNIT_TEST_SUITE(TThreadPoolTest) { } Y_UNIT_TEST(TestEnumeratedThreadName) { - const TString namePrefix = "HelloWorld"; - const THashSet expectedNames = { + const std::string namePrefix = "HelloWorld"; + const THashSet expectedNames = { "HelloWorld0", "HelloWorld1", "HelloWorld2", diff --git a/util/ysaveload.cpp b/util/ysaveload.cpp index 1ae3acb01ea..cbbcc3a885d 100644 --- a/util/ysaveload.cpp +++ b/util/ysaveload.cpp @@ -13,7 +13,7 @@ void TSerializer::Load(IInputStream* rh, TBuffer& buf) { ::LoadPodArray(rh, buf.Data(), buf.Size()); } -[[noreturn]] void NPrivate::ThrowLoadEOFException(size_t typeSize, size_t realSize, TStringBuf structName) { +[[noreturn]] void NPrivate::ThrowLoadEOFException(size_t typeSize, size_t realSize, std::string_view structName) { ythrow TLoadEOF() << "can not load " << structName << "(" << typeSize << ", " << realSize << " bytes)"; } diff --git a/util/ysaveload.h b/util/ysaveload.h index 623fde0fab5..3629859f6bd 100644 --- a/util/ysaveload.h +++ b/util/ysaveload.h @@ -1,8 +1,6 @@ #pragma once #include -#include -#include #include #include #include @@ -10,6 +8,8 @@ #include #include +#include + #ifndef __NVCC__ // cuda is compiled in C++14 mode at the time #include @@ -57,7 +57,7 @@ static inline void SavePodType(IOutputStream* rh, const T& t) { } namespace NPrivate { - [[noreturn]] void ThrowLoadEOFException(size_t typeSize, size_t realSize, TStringBuf structName); + [[noreturn]] void ThrowLoadEOFException(size_t typeSize, size_t realSize, std::string_view structName); [[noreturn]] void ThrowUnexpectedVariantTagException(ui8 tagIndex); } @@ -66,7 +66,7 @@ static inline void LoadPodType(IInputStream* rh, T& t) { const size_t res = rh->Load(&t, sizeof(T)); if (Y_UNLIKELY(res != sizeof(T))) { - ::NPrivate::ThrowLoadEOFException(sizeof(T), res, TStringBuf("pod type")); + ::NPrivate::ThrowLoadEOFException(sizeof(T), res, "pod type"); } } @@ -81,7 +81,7 @@ static inline void LoadPodArray(IInputStream* rh, T* arr, size_t count) { const size_t res = rh->Load(arr, len); if (Y_UNLIKELY(res != len)) { - ::NPrivate::ThrowLoadEOFException(len, res, TStringBuf("pod array")); + ::NPrivate::ThrowLoadEOFException(len, res, "pod array"); } } @@ -357,11 +357,11 @@ class TSerializer>: public TVectorSerializer> { }; template <> -class TSerializer: public TVectorSerializer { +class TSerializer: public TVectorSerializer { }; template <> -class TSerializer: public TVectorSerializer { +class TSerializer: public TVectorSerializer { }; template diff --git a/util/ysaveload_ut.cpp b/util/ysaveload_ut.cpp index 44828f09d3b..a7c60e0cf30 100644 --- a/util/ysaveload_ut.cpp +++ b/util/ysaveload_ut.cpp @@ -6,10 +6,8 @@ #include #include -#include #include #include -#include #include #include @@ -51,11 +49,11 @@ class TSaveLoadTest: public TTestBase { ::SaveLoad(s, Str); } - TString Str; + std::string Str; }; struct TNewNewStyleHelper { - TString Str; + std::string Str; ui32 Int; Y_SAVELOAD_DEFINE(Str, Int); @@ -67,7 +65,7 @@ class TSaveLoadTest: public TTestBase { private: inline void TestNewNewStyle() { - TString ss; + std::string ss; { TNewNewStyleHelper h; @@ -92,7 +90,7 @@ class TSaveLoadTest: public TTestBase { } inline void TestNewStyle() { - TString ss; + std::string ss; { TNewStyleSaveHelper sh; @@ -160,7 +158,7 @@ class TSaveLoadTest: public TTestBase { } { - TString val("123456"); + std::string val("123456"); Save(&S_, val); } @@ -214,7 +212,7 @@ class TSaveLoadTest: public TTestBase { } { - THashMultiMap mm; + THashMultiMap mm; mm.insert({"one", 1}); mm.insert({"two", 2}); @@ -299,7 +297,7 @@ class TSaveLoadTest: public TTestBase { } { - TString val; + std::string val; Load(&S_, val); UNIT_ASSERT_EQUAL(val, "123456"); @@ -320,9 +318,9 @@ class TSaveLoadTest: public TTestBase { Load(&S_, vec, pool); UNIT_ASSERT_EQUAL(vec.size(), 3); - UNIT_ASSERT_EQUAL(vec[0], TString("1")); - UNIT_ASSERT_EQUAL(vec[1], TString("123")); - UNIT_ASSERT_EQUAL(vec[2], TString("4567")); + UNIT_ASSERT_EQUAL(vec[0], std::string("1")); + UNIT_ASSERT_EQUAL(vec[1], std::string("123")); + UNIT_ASSERT_EQUAL(vec[2], std::string("4567")); } { @@ -364,7 +362,7 @@ class TSaveLoadTest: public TTestBase { } { - THashMultiMap mm; + THashMultiMap mm; Load(&S_, mm); @@ -405,7 +403,7 @@ class TSaveLoadTest: public TTestBase { void TestTuple() { TBufferStream s; - using TTuple = std::tuple; + using TTuple = std::tuple; const TTuple toSave{-10, "qwerty", 15}; Save(&s, toSave); @@ -428,13 +426,13 @@ class TSaveLoadTest: public TTestBase { } void TestVariant() { - std::variant> v(1); + std::variant> v(1); TestVariantImpl(v, 42); TestVariantImpl(v, true); - TestVariantImpl(v, TString("foo")); + TestVariantImpl(v, std::string("foo")); TestVariantImpl(v, std::vector{'b', 'a', 'r'}); - v = TString("baz"); + v = std::string("baz"); TBufferStream s; ::Save(&s, v); @@ -458,7 +456,7 @@ class TSaveLoadTest: public TTestBase { void TestOptional() { TestOptionalImpl(std::optional(42ull)); TestOptionalImpl(std::optional(true)); - TestOptionalImpl(std::optional("abacaba")); + TestOptionalImpl(std::optional("abacaba")); TestOptionalImpl(std::optional(std::nullopt)); } @@ -484,12 +482,12 @@ class TSaveLoadTest: public TTestBase { void TestInheritNonVirtualClass() { struct TBaseNonVirtual { - TString Str1; + std::string Str1; Y_SAVELOAD_DEFINE(Str1); }; struct TDerivedNonVirtual: TBaseNonVirtual { - TString Str2; - TString Str3; + std::string Str2; + std::string Str3; Y_SAVELOAD_DEFINE(TNonVirtualSaver{this}, Str2, Str3); }; TestInheritClassImpl(); @@ -501,12 +499,12 @@ class TSaveLoadTest: public TTestBase { virtual void Load(IInputStream* in) = 0; }; struct TBaseVirtual: IInterface { - TString Str1; + std::string Str1; Y_SAVELOAD_DEFINE_OVERRIDE(Str1); }; struct TDerivedVirtual: TBaseVirtual { - TString Str2; - TString Str3; + std::string Str2; + std::string Str3; Y_SAVELOAD_DEFINE_OVERRIDE(TNonVirtualSaver{this}, Str2, Str3); }; TestInheritClassImpl(); diff --git a/ydb/library/grpc/client/CMakeLists.txt b/ydb/library/grpc/client/CMakeLists.txt index e5a5e63915b..7e1804aeec8 100644 --- a/ydb/library/grpc/client/CMakeLists.txt +++ b/ydb/library/grpc/client/CMakeLists.txt @@ -5,7 +5,6 @@ target_link_libraries(library-grpc-client PUBLIC yutil gRPC::grpc++ cpp-deprecated-atomic - cpp-string_builder ) target_sources(library-grpc-client PRIVATE diff --git a/ydb/library/grpc/client/grpc_client_low.h b/ydb/library/grpc/client/grpc_client_low.h index e4d556b4881..dd7d2ced6f7 100644 --- a/ydb/library/grpc/client/grpc_client_low.h +++ b/ydb/library/grpc/client/grpc_client_low.h @@ -3,7 +3,7 @@ #include "grpc_common.h" #include -#include +#include #include #include @@ -173,8 +173,8 @@ struct TGrpcStatus { return !InternalError && GRpcStatusCode == grpc::StatusCode::OK; } - NUtils::TYdbStringBuilder ToDebugString() const { - NUtils::TYdbStringBuilder ret; + TStringBuilder ToDebugString() const { + TStringBuilder ret; ret << "gRpcStatusCode: " << GRpcStatusCode; if(!Ok()) ret << ", Msg: " << Msg << ", Details: " << Details << ", InternalError: " << InternalError; diff --git a/ydb/library/grpc/client/grpc_common.h b/ydb/library/grpc/client/grpc_common.h index cba2509bf6e..359f17fa298 100644 --- a/ydb/library/grpc/client/grpc_common.h +++ b/ydb/library/grpc/client/grpc_common.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include namespace NYdbGrpc { diff --git a/ydb/library/login/CMakeLists.txt b/ydb/library/login/CMakeLists.txt index 2b252f15069..672d50780d5 100644 --- a/ydb/library/login/CMakeLists.txt +++ b/ydb/library/login/CMakeLists.txt @@ -12,7 +12,6 @@ target_link_libraries(ydb-library-login PUBLIC library-cpp-json cpp-string_utils-base64 library-login-protos - cpp-string_builder ) target_sources(ydb-library-login PRIVATE diff --git a/ydb/library/login/login.cpp b/ydb/library/login/login.cpp index eac2dcfcdd1..6d9d6e2f640 100644 --- a/ydb/library/login/login.cpp +++ b/ydb/library/login/login.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include @@ -164,7 +164,7 @@ TLoginProvider::TBasicResponse TLoginProvider::AddGroupMembership(const TAddGrou TSidRecord& group = itGroupModify->second; if (group.Members.count(request.Member)) { - response.Notice = NUtils::TYdbStringBuilder() << "Role \"" << request.Member << "\" is already a member of role \"" << group.Name << "\""; + response.Notice = TStringBuilder() << "Role \"" << request.Member << "\" is already a member of role \"" << group.Name << "\""; } else { group.Members.insert(request.Member); } @@ -186,7 +186,7 @@ TLoginProvider::TBasicResponse TLoginProvider::RemoveGroupMembership(const TRemo TSidRecord& group = itGroupModify->second; if (!group.Members.count(request.Member)) { - response.Warning = NUtils::TYdbStringBuilder() << "Role \"" << request.Member << "\" is not a member of role \"" << group.Name << "\""; + response.Warning = TStringBuilder() << "Role \"" << request.Member << "\" is not a member of role \"" << group.Name << "\""; } else { group.Members.erase(request.Member); } diff --git a/ydb/library/login/login.h b/ydb/library/login/login.h index 5b4102bca2d..03f777ed85d 100644 --- a/ydb/library/login/login.h +++ b/ydb/library/login/login.h @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include namespace NLogin { diff --git a/ydb/library/persqueue/topic_parser_public/CMakeLists.txt b/ydb/library/persqueue/topic_parser_public/CMakeLists.txt index fa8d0dd7cb2..bed4bd36994 100644 --- a/ydb/library/persqueue/topic_parser_public/CMakeLists.txt +++ b/ydb/library/persqueue/topic_parser_public/CMakeLists.txt @@ -3,7 +3,6 @@ add_library(library-persqueue-topic_parser_public) target_link_libraries(library-persqueue-topic_parser_public PUBLIC contrib-libs-cxxsupp yutil - cpp-string_builder ) target_sources(library-persqueue-topic_parser_public PRIVATE diff --git a/ydb/library/persqueue/topic_parser_public/topic_parser.cpp b/ydb/library/persqueue/topic_parser_public/topic_parser.cpp index 20325cdd73a..a5e760b9140 100644 --- a/ydb/library/persqueue/topic_parser_public/topic_parser.cpp +++ b/ydb/library/persqueue/topic_parser_public/topic_parser.cpp @@ -1,6 +1,6 @@ #include "topic_parser.h" -#include +#include #include @@ -63,7 +63,7 @@ std::string ConvertNewTopicName(const std::string& topic) { auto pos = t.rfind("/"); if (pos == std::string::npos) return t; - NUtils::TYdbStringBuilder res; + TStringBuilder res; for (ui32 i = 0; i < pos; ++i) { if (t[i] == '/') res << '@'; else res << t[i]; @@ -78,7 +78,7 @@ std::string ConvertOldTopicName(const std::string& topic) { auto pos = topic.rfind("--"); if (pos == std::string::npos) return topic; - NUtils::TYdbStringBuilder res; + TStringBuilder res; for (ui32 i = 0; i < pos; ++i) { if (topic[i] == '@') res << '/'; else res << topic[i]; @@ -93,7 +93,7 @@ std::string BuildFullTopicName(const std::string& topicPath, const std::string& } std::string ConvertOldProducerName(const std::string& producer) { - NUtils::TYdbStringBuilder res; + TStringBuilder res; for (ui32 i = 0; i < producer.size(); ++i) { if (producer[i] == '@') res << "/"; else res << producer[i]; @@ -112,7 +112,7 @@ std::string NormalizePath(const std::string& path) { std::string ConvertNewConsumerName(const std::string& consumer) { - NUtils::TYdbStringBuilder res; + TStringBuilder res; ui32 pos = 0; std::string c = NormalizePath(consumer); if (std::string_view{c}.starts_with("shared/")) @@ -125,7 +125,7 @@ std::string ConvertNewConsumerName(const std::string& consumer) { } std::string ConvertNewProducerName(const std::string& producer) { - NUtils::TYdbStringBuilder res; + TStringBuilder res; for (ui32 i = 0; i < producer.size(); ++i) { if (producer[i] == '/') res << "@"; else res << producer[i]; @@ -135,7 +135,7 @@ std::string ConvertNewProducerName(const std::string& producer) { std::string ConvertOldConsumerName(const std::string& consumer) { - NUtils::TYdbStringBuilder res; + TStringBuilder res; bool shared = true; for (ui32 i = 0; i < consumer.size(); ++i) { if (consumer[i] == '@') { @@ -146,7 +146,7 @@ std::string ConvertOldConsumerName(const std::string& consumer) { } } if (shared) - return NUtils::TYdbStringBuilder() << "shared/" << res; + return TStringBuilder() << "shared/" << res; return res; } diff --git a/ydb/library/yql/public/decimal/yql_decimal.h b/ydb/library/yql/public/decimal/yql_decimal.h index 11d08d8ab6f..14822b6fd40 100644 --- a/ydb/library/yql/public/decimal/yql_decimal.h +++ b/ydb/library/yql/public/decimal/yql_decimal.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include "yql_wide_int.h" #include diff --git a/ydb/library/yql/public/issue/yql_issue.h b/ydb/library/yql/public/issue/yql_issue.h index 80b58ebaf6f..398f91613cc 100644 --- a/ydb/library/yql/public/issue/yql_issue.h +++ b/ydb/library/yql/public/issue/yql_issue.h @@ -2,9 +2,9 @@ #include #include -#include -#include -#include +#include +#include +#include #include #include #include diff --git a/ydb/library/yql/utils/utf8.cpp b/ydb/library/yql/utils/utf8.cpp index 4722e422c54..26817dfeae7 100644 --- a/ydb/library/yql/utils/utf8.cpp +++ b/ydb/library/yql/utils/utf8.cpp @@ -208,7 +208,7 @@ std::optional RoundToNearestValidUtf8(const std::string_view& str, std::optional NextValidUtf8(const std::string_view& str) { Y_ENSURE(IsUtf8(str)); - TUtf32String wide = UTF8ToUTF32(str); + auto wide = UTF8ToUTF32(str); bool incremented = false; size_t toDrop = 0; for (auto it = wide.rbegin(); it != wide.rend(); ++it) { diff --git a/ydb/public/lib/json_value/ydb_json_value.cpp b/ydb/public/lib/json_value/ydb_json_value.cpp index b4749b8da90..4af751addb5 100644 --- a/ydb/public/lib/json_value/ydb_json_value.cpp +++ b/ydb/public/lib/json_value/ydb_json_value.cpp @@ -1,6 +1,6 @@ #include "ydb_json_value.h" -#include +#include #include #include #include @@ -200,7 +200,7 @@ namespace NYdb { Writer.WriteString(Parser.GetDyNumber()); break; default: - ThrowFatalError(NUtils::TYdbStringBuilder() << "Unsupported primitive type: " << type); + ThrowFatalError(TStringBuilder() << "Unsupported primitive type: " << type); } } @@ -312,7 +312,7 @@ namespace NYdb { Writer.WriteNull(); break; default: - ThrowFatalError(NUtils::TYdbStringBuilder() << "Unsupported type kind: " << Parser.GetKind()); + ThrowFatalError(TStringBuilder() << "Unsupported type kind: " << Parser.GetKind()); } } @@ -327,7 +327,7 @@ namespace NYdb { str << Base64Encode(s); break; default: - ThrowFatalError(NUtils::TYdbStringBuilder() << "Unknown binary string encode mode: " + ThrowFatalError(TStringBuilder() << "Unknown binary string encode mode: " << static_cast(Encoding)); } str << "\""; @@ -433,7 +433,7 @@ namespace { EnsureType(jsonValue, NJson::JSON_INTEGER); long long intValue = jsonValue.GetInteger(); if (intValue > std::numeric_limits::max() || intValue < std::numeric_limits::min()) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Value \"" << intValue << "\" doesn't fit in Int8 type"); + ThrowFatalError(TStringBuilder() << "Value \"" << intValue << "\" doesn't fit in Int8 type"); } ValueBuilder.Int8(intValue); break; @@ -443,7 +443,7 @@ namespace { EnsureType(jsonValue, NJson::JSON_UINTEGER); unsigned long long intValue = jsonValue.GetUInteger(); if (intValue > std::numeric_limits::max()) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Value \"" << intValue << "\" doesn't fit in UInt8 type"); + ThrowFatalError(TStringBuilder() << "Value \"" << intValue << "\" doesn't fit in UInt8 type"); } ValueBuilder.Uint8(intValue); break; @@ -453,7 +453,7 @@ namespace { EnsureType(jsonValue, NJson::JSON_INTEGER); long long intValue = jsonValue.GetInteger(); if (intValue > std::numeric_limits::max() || intValue < std::numeric_limits::min()) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Value \"" << intValue << "\" doesn't fit in Int16 type"); + ThrowFatalError(TStringBuilder() << "Value \"" << intValue << "\" doesn't fit in Int16 type"); } ValueBuilder.Int16(intValue); break; @@ -463,7 +463,7 @@ namespace { EnsureType(jsonValue, NJson::JSON_UINTEGER); unsigned long long intValue = jsonValue.GetUInteger(); if (intValue > std::numeric_limits::max()) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Value \"" << intValue << "\" doesn't fit in UInt16 type"); + ThrowFatalError(TStringBuilder() << "Value \"" << intValue << "\" doesn't fit in UInt16 type"); } ValueBuilder.Uint16(intValue); break; @@ -473,7 +473,7 @@ namespace { EnsureType(jsonValue, NJson::JSON_INTEGER); long long intValue = jsonValue.GetInteger(); if (intValue > std::numeric_limits::max() || intValue < std::numeric_limits::min()) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Value \"" << intValue << "\" doesn't fit in Int32 type"); + ThrowFatalError(TStringBuilder() << "Value \"" << intValue << "\" doesn't fit in Int32 type"); } ValueBuilder.Int32(intValue); break; @@ -483,7 +483,7 @@ namespace { EnsureType(jsonValue, NJson::JSON_UINTEGER); unsigned long long intValue = jsonValue.GetUInteger(); if (intValue > std::numeric_limits::max()) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Value \"" << intValue << "\" doesn't fit in UInt32 type"); + ThrowFatalError(TStringBuilder() << "Value \"" << intValue << "\" doesn't fit in UInt32 type"); } ValueBuilder.Uint32(intValue); break; @@ -493,7 +493,7 @@ namespace { EnsureType(jsonValue, NJson::JSON_INTEGER); long long intValue = jsonValue.GetInteger(); if (intValue > std::numeric_limits::max() || intValue < std::numeric_limits::min()) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Value \"" << intValue << "\" doesn't fit in Int64 type"); + ThrowFatalError(TStringBuilder() << "Value \"" << intValue << "\" doesn't fit in Int64 type"); } ValueBuilder.Int64(intValue); break; @@ -503,7 +503,7 @@ namespace { EnsureType(jsonValue, NJson::JSON_UINTEGER); unsigned long long intValue = jsonValue.GetUInteger(); if (intValue > std::numeric_limits::max()) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Value \"" << intValue << "\" doesn't fit in UInt64 type"); + ThrowFatalError(TStringBuilder() << "Value \"" << intValue << "\" doesn't fit in UInt64 type"); } ValueBuilder.Uint64(intValue); break; @@ -521,7 +521,7 @@ namespace { EnsureType(jsonValue, NJson::JSON_STRING); TInstant date; if (!TInstant::TryParseIso8601(jsonValue.GetString(), date)) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Can't parse date from string \"" << jsonValue.GetString() << "\""); + ThrowFatalError(TStringBuilder() << "Can't parse date from string \"" << jsonValue.GetString() << "\""); } ValueBuilder.Date(date); break; @@ -531,7 +531,7 @@ namespace { EnsureType(jsonValue, NJson::JSON_STRING); TInstant dateTime; if (!TInstant::TryParseIso8601(jsonValue.GetString(), dateTime)) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Can't parse dateTime from string \"" << jsonValue.GetString() << "\""); + ThrowFatalError(TStringBuilder() << "Can't parse dateTime from string \"" << jsonValue.GetString() << "\""); } ValueBuilder.Datetime(dateTime); break; @@ -541,7 +541,7 @@ namespace { EnsureType(jsonValue, NJson::JSON_STRING); TInstant timestamp; if (!TInstant::TryParseIso8601(jsonValue.GetString(), timestamp)) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Can't parse timestamp from string \"" << jsonValue.GetString() << "\""); + ThrowFatalError(TStringBuilder() << "Can't parse timestamp from string \"" << jsonValue.GetString() << "\""); } ValueBuilder.Timestamp(timestamp); break; @@ -591,7 +591,7 @@ namespace { ValueBuilder.DyNumber(jsonValue.GetString()); break; default: - ThrowFatalError(NUtils::TYdbStringBuilder() << "Unsupported primitive type: " << type); + ThrowFatalError(TStringBuilder() << "Unsupported primitive type: " << type); } } @@ -653,7 +653,7 @@ namespace { typeBuilder.EndDict(); break; default: - ThrowFatalError(NUtils::TYdbStringBuilder() << "Unsupported type kind: " << TypeParser.GetKind()); + ThrowFatalError(TStringBuilder() << "Unsupported type kind: " << TypeParser.GetKind()); } } @@ -686,7 +686,7 @@ namespace { } else { EnsureType(jsonValue, NJson::JSON_ARRAY); if (jsonValue.GetArray().size() != 1) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Pg type should be encoded as array with size 1, but not " << jsonValue.GetArray().size()); + ThrowFatalError(TStringBuilder() << "Pg type should be encoded as array with size 1, but not " << jsonValue.GetArray().size()); } auto& innerJsonValue = jsonValue.GetArray().at(0); EnsureType(innerJsonValue, NJson::JSON_STRING); @@ -746,7 +746,7 @@ namespace { const std::string& memberName = TypeParser.GetMemberName(); const auto it = jsonMap.find(memberName); if (it == jsonMap.end()) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "No member \"" << memberName + ThrowFatalError(TStringBuilder() << "No member \"" << memberName << "\" in the map in json string for YDB struct type"); } ValueBuilder.AddMember(memberName); @@ -820,7 +820,7 @@ namespace { break; default: - ThrowFatalError(NUtils::TYdbStringBuilder() << "Unsupported type kind: " << TypeParser.GetKind()); + ThrowFatalError(TStringBuilder() << "Unsupported type kind: " << TypeParser.GetKind()); } } @@ -854,7 +854,7 @@ namespace { TStringStream str; NJsonWriter::TBuf writer(NJsonWriter::HEM_UNSAFE, &str); writer.WriteJsonValue(&value); - ThrowFatalError(NUtils::TYdbStringBuilder() << "Wrong type for json value \"" << str.Str() + ThrowFatalError(TStringBuilder() << "Wrong type for json value \"" << str.Str() << "\". Expected type: " << type << ", received type: " << value.GetType() << ". "); } } @@ -873,12 +873,12 @@ TValue JsonToYdbValue(const std::string& jsonString, const TType& type, EBinaryS try { if (!NJson::ReadJsonTree(jsonString, &jsonValue, true)) { - ThrowFatalError(NUtils::TYdbStringBuilder() << "Can't parse string \"" << jsonString << "\" as json."); + ThrowFatalError(TStringBuilder() << "Can't parse string \"" << jsonString << "\" as json."); } } catch (std::exception& e) { ThrowFatalError( - NUtils::TYdbStringBuilder() << "Exception while parsing string \"" << jsonString << "\" as json: " << e.what()); + TStringBuilder() << "Exception while parsing string \"" << jsonString << "\" as json: " << e.what()); } return JsonToYdbValue(jsonValue, type, encoding); } diff --git a/ydb/public/lib/operation_id/operation_id.h b/ydb/public/lib/operation_id/operation_id.h index 7b14f5730be..7bdbda6b96f 100644 --- a/ydb/public/lib/operation_id/operation_id.h +++ b/ydb/public/lib/operation_id/operation_id.h @@ -3,8 +3,8 @@ #include #include -#include -#include +#include +#include namespace NKikimr { namespace NOperationId { diff --git a/ydb/public/lib/yson_value/CMakeLists.txt b/ydb/public/lib/yson_value/CMakeLists.txt index c60c03c8d95..e5f343651ec 100644 --- a/ydb/public/lib/yson_value/CMakeLists.txt +++ b/ydb/public/lib/yson_value/CMakeLists.txt @@ -7,8 +7,7 @@ target_link_libraries(public-lib-yson_value PUBLIC cpp-client-ydb_result cpp-client-ydb_value ydb-library-uuid - cpp-string_builder -) + ) if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT HAVE_CUDA) target_link_libraries(public-lib-json_value PUBLIC contrib-libs-linux-headers diff --git a/ydb/public/lib/yson_value/ydb_yson_value.cpp b/ydb/public/lib/yson_value/ydb_yson_value.cpp index 134c59f15f0..0634cab788e 100644 --- a/ydb/public/lib/yson_value/ydb_yson_value.cpp +++ b/ydb/public/lib/yson_value/ydb_yson_value.cpp @@ -1,6 +1,6 @@ #include "ydb_yson_value.h" -#include +#include #include #include @@ -88,7 +88,7 @@ static void PrimitiveValueToYson(EPrimitiveType type, TValueParser& parser, NYso writer.OnStringScalar(parser.GetDyNumber()); break; default: - ThrowFatalError(NUtils::TYdbStringBuilder() << "Unsupported primitive type: " << type); + ThrowFatalError(TStringBuilder() << "Unsupported primitive type: " << type); } } @@ -214,7 +214,7 @@ static void FormatValueYsonInternal(TValueParser& parser, NYson::TYsonWriter& wr break; default: - ThrowFatalError(NUtils::TYdbStringBuilder() << "Unsupported type kind: " << parser.GetKind()); + ThrowFatalError(TStringBuilder() << "Unsupported type kind: " << parser.GetKind()); } }