diff options
author | melkov <melkov@yandex-team.ru> | 2022-02-10 16:48:14 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:14 +0300 |
commit | 2c532b38e6aeb4fd88531027c7335690fd34c4e5 (patch) | |
tree | b222e5ac2e2e98872661c51ccceee5da0d291e13 | |
parent | 438546c8737d5c1fdeb31157dcf999717d930eec (diff) | |
download | ydb-2c532b38e6aeb4fd88531027c7335690fd34c4e5.tar.gz |
Restoring authorship annotation for <melkov@yandex-team.ru>. Commit 2 of 2.
85 files changed, 1571 insertions, 1571 deletions
diff --git a/build/scripts/writer.py b/build/scripts/writer.py index cb49a9ea21..21bb3006e5 100644 --- a/build/scripts/writer.py +++ b/build/scripts/writer.py @@ -17,12 +17,12 @@ def parse_args(): return parser.parse_args(args) -def smart_shell_quote(v): - if v is None: - return None - if ' ' in v or '"' in v or "'" in v: - return "\"{0}\"".format(v.replace('"', '\\"')) - return v +def smart_shell_quote(v): + if v is None: + return None + if ' ' in v or '"' in v or "'" in v: + return "\"{0}\"".format(v.replace('"', '\\"')) + return v if __name__ == '__main__': args = parse_args() @@ -32,9 +32,9 @@ if __name__ == '__main__': if args.quote: content = [smart_shell_quote(ln) for ln in content] if content is not None else None content = '\n'.join(content) - + with open(args.file_path, open_type) as f: if args.addspace: - f.write(' ') - if content is not None: - f.write(content) + f.write(' ') + if content is not None: + f.write(content) diff --git a/build/ymake.core.conf b/build/ymake.core.conf index cb1039ac40..081833998b 100644 --- a/build/ymake.core.conf +++ b/build/ymake.core.conf @@ -996,7 +996,7 @@ YIELD=$YMAKE_PYTHON ${input:"build/scripts/yield_line.py"} -- ${BINDIR}/__args XARGS=$YMAKE_PYTHON ${input:"build/scripts/xargs.py"} -- ${BINDIR}/__args RESPFILE_CMD=$YMAKE_PYTHON ${input:"build/scripts/writer.py"} - + FS_TOOLS=$YMAKE_PYTHON ${input:"build/scripts/fs_tools.py"} COPY_CMD=$FS_TOOLS copy @@ -6962,7 +6962,7 @@ SCHEEME2_STRUCT_INFO_FLAGS=-f "const static ui32 RecordSig" -u "RecordSig" --gcc macro GEN_SCHEEME2(ScheemeName, FromFile) { .CMD=$CXX_COMPILER $C_FLAGS_PLATFORM -c ${tmp;stdout:FromFile.cph} $SCHEEME2_CFLAGS ${input:FromFile} ${pre=-I:_C__INCLUDE} $CXXFLAGS -Wno-error && ${tool:"tools/structparser"} -o ${output:ScheemeName.inc} -n N${ScheemeName}SchemeInfo $SCHEEME2_STRUCT_INFO_FLAGS $DATAWORK_SCHEEME_EXPORT_FLAGS ${tmp:FromFile.cph} ${output;stdout;noauto:ScheemeName.inc.log} ${kv;hide:"p SH"} ${kv;hide:"pc yellow"} } - + ### @usage: SYMLINK(from to) ### Add symlink macro SYMLINK(From, To) { @@ -7134,10 +7134,10 @@ macro NO_DEBUG_INFO() { ### Generate File.varnames.h using contrib/libs/ctemplate/make_tpl_varnames_h ### ### Documentation: https://a.yandex-team.ru/arc/trunk/arcadia/contrib/libs/ctemplate/README.md -macro CTEMPLATE_VARNAMES(File) { +macro CTEMPLATE_VARNAMES(File) { .CMD=${tool:"contrib/libs/ctemplate/make_tpl_varnames_h"} -f ${output;addincl;nopath;noallext:File.varnames.h} ${input:File} -} - +} + LLVM_OPTS= CLANG_ROOT=$CLANG_RESOURCE_GLOBAL diff --git a/build/ymake_conf.py b/build/ymake_conf.py index a7dc9a8678..30219eb85e 100755 --- a/build/ymake_conf.py +++ b/build/ymake_conf.py @@ -173,14 +173,14 @@ class Platform(object): if self._parse_os(key) == self.os: return dict_[key] return default - + @property def os_compat(self): if self.is_macos: return 'DARWIN' else: return self.os.upper() - + def exe(self, *paths): if self.is_windows: return ntpath.join(*itertools.chain(paths[:-1], (paths[-1] + '.exe',))) @@ -189,24 +189,24 @@ class Platform(object): def __str__(self): return '{name}-{os}-{arch}'.format(name=self.name, os=self.os, arch=self.arch) - + def __cmp__(self, other): return cmp((self.name, self.os, self.arch), (other.name, other.os, other.arch)) - + def __hash__(self): return hash((self.name, self.os, self.arch)) - + @staticmethod def _parse_os(os): os = os.lower() - + if os == 'darwin': return 'macos' if os in ('win', 'win32', 'win64'): return 'windows' if os.startswith('cygwin'): return 'cygwin' - + return os @@ -417,16 +417,16 @@ class Options(object): self.options, self.arguments = parser.parse_args(argv) - argv = self.arguments - if len(argv) < 4: + argv = self.arguments + if len(argv) < 4: print('Usage: ArcRoot, --BuildType--, Verbosity, [Path to local.ymake]', file=sys.stderr) - sys.exit(1) - + sys.exit(1) + self.arcadia_root = argv[1] init_logger(argv[3] == 'verbose') # Эти переменные не надо использоваться напрямую. Их значения уже разбираются в других местах. - self.build_type = argv[2].lower() + self.build_type = argv[2].lower() self.local_distbuild = self.options.local_distbuild self.toolchain_params = self.options.toolchain_params @@ -435,12 +435,12 @@ class Options(object): Instance = None - + def opts(): if Options.Instance is None: Options.Instance = Options(sys.argv) return Options.Instance - + class Profiler(object): Generic = 'generic' @@ -476,7 +476,7 @@ class Build(object): if force_ignore_local_files: self.ignore_local_files = True - + if self.is_ide_build_type(self.build_type): self.ignore_local_files = True @@ -517,7 +517,7 @@ class Build(object): def _print_build_settings(self): emit('BUILD_TYPE', self.build_type.upper()) emit('BT_' + self.build_type.upper().replace('-', '_'), 'yes') - + if self.build_system == 'distbuild': emit('DISTBUILD', 'yes') elif self.build_system != 'ymake': @@ -615,7 +615,7 @@ class Build(object): host = self.host emit('USE_LOCAL_TOOLS', 'no' if self.ignore_local_files else 'yes') - + ragel = Ragel() ragel.configure_toolchain(self, compiler) ragel.print_variables() @@ -733,7 +733,7 @@ class YMake(object): def _find_core_conf(self): return self._find_conf('ymake.core.conf') - + class System(object): def __init__(self, platform): @@ -801,18 +801,18 @@ when (($USEMPROF == "yes") || ($USE_MPROF == "yes")) { emit('HOST_PLATFORM', self.platform.os_compat) if not self.platform.is_windows: self.print_nix_host_const() - + for variable in itertools.chain(self.platform.os_variables, self.platform.arch_variables): emit('HOST_{var}'.format(var=variable), 'yes') - - + + class CompilerDetector(object): def __init__(self): self.type = None self.c_compiler = None self.cxx_compiler = None self.version_list = None - + @staticmethod def preprocess_source(compiler, source): # noinspection PyBroadException @@ -825,7 +825,7 @@ class CompilerDetector(object): finally: os.remove(path) return stdout, code - + except Exception as e: logger.debug('Preprocessing failed: %s', e) return None, None @@ -834,13 +834,13 @@ class CompilerDetector(object): def get_compiler_vars(compiler, names): prefix = '____YA_VAR_' source = '\n'.join(['{prefix}{name}={name}\n'.format(prefix=prefix, name=n) for n in names]) - + # Некоторые препроцессоры возвращают ненулевой код возврата. Поэтому его проверять нельзя. # Мы можем только удостовериться после разбора stdout, что в нём # присутствовала хотя бы одна подставленная переменная. # TODO(somov): Исследовать, можно ли проверять ограниченный набор кодов возврата. stdout, _ = CompilerDetector.preprocess_source(compiler, source) - + if stdout is None: return None @@ -852,14 +852,14 @@ class CompilerDetector(object): if value == name: continue # Preprocessor variable was not substituted vars_[name] = value - + return vars_ - + def detect(self, c_compiler=None, cxx_compiler=None): c_compiler = c_compiler or os.environ.get('CC') cxx_compiler = cxx_compiler or os.environ.get('CXX') or c_compiler c_compiler = c_compiler or cxx_compiler - + logger.debug('e=%s', os.environ) if c_compiler is None: raise ConfigureError('Custom compiler was requested but not specified') @@ -872,10 +872,10 @@ class CompilerDetector(object): apple_var = '__apple_build_version__' compiler_vars = self.get_compiler_vars(c_compiler_path, clang_vars + [apple_var] + gcc_vars + msvc_vars) - + if not compiler_vars: raise ConfigureError('Could not determine custom compiler version: {}'.format(c_compiler)) - + def version(version_names): def iter_version(): for name in version_names: @@ -906,12 +906,12 @@ class CompilerDetector(object): self.type = 'msvc' else: raise ConfigureError('Could not determine custom compiler type: {}'.format(c_compiler)) - + self.version_list = clang_version or gcc_version or msvc_version self.c_compiler = c_compiler_path self.cxx_compiler = cxx_compiler and which(cxx_compiler) or c_compiler_path - + class ToolchainOptions(object): def __init__(self, build, detector): @@ -929,20 +929,20 @@ class ToolchainOptions(object): self.params = tc_json['params'] self._name = tc_json.get('name', 'theyknow') - + if detector: self.type = detector.type self.from_arcadia = False - + self.c_compiler = detector.c_compiler self.cxx_compiler = detector.cxx_compiler self.compiler_version_list = detector.version_list self.compiler_version = '.'.join(map(lambda part: six.ensure_str(str(part)), self.compiler_version_list)) - else: + else: self.type = self.params['type'] self.from_arcadia = True - + self.c_compiler = self.params['c_compiler'] self.cxx_compiler = self.params['cxx_compiler'] @@ -1071,15 +1071,15 @@ class Compiler(object): def __init__(self, tc, compiler_variable): self.compiler_variable = compiler_variable self.tc = tc - + def print_compiler(self): # CLANG and CLANG_VER variables emit(self.compiler_variable, 'yes') emit('{}_VER'.format(self.compiler_variable), self.tc.compiler_version) if self.tc.is_xcode: emit('XCODE', 'yes') - - + + class GnuToolchain(Toolchain): def __init__(self, tc, build): """ @@ -1302,7 +1302,7 @@ class GnuToolchain(Toolchain): emit('C_FLAGS_PLATFORM', self.c_flags_platform) emit('SWIFT_FLAGS_PLATFORM', self.swift_flags_platform) emit('SWIFT_LD_FLAGS', '-L{}'.format(self.swift_lib_path) if self.swift_lib_path else '') - + emit('PERL_SDK', preset('OS_SDK') or self.tc.os_sdk) if preset('OS_SDK') is None: emit('OS_SDK', self.tc.os_sdk) @@ -1323,8 +1323,8 @@ class GnuCompiler(Compiler): self.build = build self.host = self.build.host self.target = self.build.target - self.tc = tc - + self.tc = tc + self.c_foptions = [ # Enable C++ exceptions (and allow them to be throw through pure C code) '-fexceptions', @@ -1404,7 +1404,7 @@ class GnuCompiler(Compiler): self.c_flags += self.tc.arch_opt + ['-pipe'] self.sfdl_flags = ['-E', '-C', '-x', 'c++'] - + if self.target.is_x86: self.c_flags.append('-m32') if self.target.is_x86_64: @@ -1801,10 +1801,10 @@ class LD(Linker): self.build = build self.host = self.build.host self.target = self.build.target - self.tc = tc + self.tc = tc target = self.target - + self.ar = preset('AR') or self.tc.ar self.ar_plugin = self.tc.ar_plugin self.strip = self.tc.strip @@ -2088,7 +2088,7 @@ class LD(Linker): ld_env_style, ) emit('REAL_LINK_EXE', '$REAL_LINK_EXE_IMPL($_WHOLE_ARCHIVE_PEERS_VALUE)') - + # Executable Shared Library emit('REAL_LINK_EXEC_DYN_LIB_CMDLINE', @@ -2110,7 +2110,7 @@ class LD(Linker): emit('REAL_LINK_EXEC_DYN_LIB', '$REAL_LINK_EXEC_DYN_LIB_IMPL($_WHOLE_ARCHIVE_PEERS_VALUE)') # Shared Library - + emit('LINK_DYN_LIB_FLAGS') emit('REAL_LINK_DYN_LIB_CMDLINE', '$YMAKE_PYTHON ${input:"build/scripts/link_dyn_lib.py"}', @@ -2128,7 +2128,7 @@ class LD(Linker): exe_flags, ld_env_style) emit('REAL_LINK_DYN_LIB', '$REAL_LINK_DYN_LIB_IMPL($_WHOLE_ARCHIVE_PEERS_VALUE)') - + if self.dwarf_command is None or self.target.is_ios: emit('DWARF_COMMAND') else: @@ -2169,7 +2169,7 @@ class LD(Linker): emit_link_fat_obj('LINK_FAT_OBJECT', True, '--obj=$TARGET', '--lib=${output:REALPRJNAME.a}') emit_link_fat_obj('LINK_RECURSIVE_LIBRARY', False, '--lib=$TARGET', '--with-own-obj', '--with-global-srcs') emit_link_fat_obj('LINK_FAT_OBJECT_LIBRARY', False, '--lib=$TARGET', '$FAT_OBJECT_ARGS', '$FAT_OBJECT_OUTS') - + emit('LIBRT', '-lrt') emit('MD5LIB', '-lcrypt') emit('LIBRESOLV', self.libresolv) @@ -2631,7 +2631,7 @@ class MSVCCompiler(MSVC, Compiler): masm_io + '${requirements;hide:CC_REQUIREMENTS} ${kv;hide:"p AS"} ${kv;hide:"pc yellow"}' ) - + class MSVCLinker(MSVC, Linker): def __init__(self, tc, build): MSVC.__init__(self, tc, build) diff --git a/contrib/tools/bison/gnulib/platform/win64/config.h b/contrib/tools/bison/gnulib/platform/win64/config.h index 316f866ca1..cf3d366e18 100644 --- a/contrib/tools/bison/gnulib/platform/win64/config.h +++ b/contrib/tools/bison/gnulib/platform/win64/config.h @@ -964,7 +964,7 @@ /* Define to 1 if you have the `strsignal' function. */ /* #undef HAVE_STRSIGNAL */ -char *strsignal (int signum); +char *strsignal (int signum); /* Define to 1 if `decimal_point' is a member of `struct lconv'. */ #define HAVE_STRUCT_LCONV_DECIMAL_POINT 1 diff --git a/contrib/tools/bison/gnulib/platform/win64/math.h b/contrib/tools/bison/gnulib/platform/win64/math.h index a37cb0c3e6..b2a0023d11 100644 --- a/contrib/tools/bison/gnulib/platform/win64/math.h +++ b/contrib/tools/bison/gnulib/platform/win64/math.h @@ -33,7 +33,7 @@ #ifndef _GL_M4_MATH_H #define _GL_M4_MATH_H -#if _MSC_VER >= 1800 // [ +#if _MSC_VER >= 1800 // [ #else // ] _MSC_VER >= 1600 [ #ifndef _GL_INLINE_HEADER_BEGIN diff --git a/contrib/tools/bison/m4/src/m4.c b/contrib/tools/bison/m4/src/m4.c index 78b830916b..2c03154471 100644 --- a/contrib/tools/bison/m4/src/m4.c +++ b/contrib/tools/bison/m4/src/m4.c @@ -364,9 +364,9 @@ process_file (const char *name) int main (int argc, char *const *argv) { -#if !defined(_WIN32) && !defined(_WIN64) +#if !defined(_WIN32) && !defined(_WIN64) struct sigaction act; -#endif +#endif macro_definition *head; /* head of deferred argument list */ macro_definition *tail; macro_definition *defn; @@ -402,9 +402,9 @@ main (int argc, char *const *argv) signal_message[SIGFPE] = xstrdup (strsignal (SIGFPE)); if (SIGBUS != SIGILL && SIGBUS != SIGSEGV) signal_message[SIGBUS] = xstrdup (strsignal (SIGBUS)); -#if !defined(_WIN32) && !defined(_WIN64) - // No such signals on Windows - sigemptyset(&act.sa_mask); +#if !defined(_WIN32) && !defined(_WIN64) + // No such signals on Windows + sigemptyset(&act.sa_mask); /* One-shot - if we fault while handling a fault, we want to revert to default signal behavior. */ act.sa_flags = SA_NODEFER | SA_RESETHAND; diff --git a/contrib/tools/protoc/plugins/cpp_styleguide/cpp_styleguide.cpp b/contrib/tools/protoc/plugins/cpp_styleguide/cpp_styleguide.cpp index d5816b6bce..cd96aa0565 100644 --- a/contrib/tools/protoc/plugins/cpp_styleguide/cpp_styleguide.cpp +++ b/contrib/tools/protoc/plugins/cpp_styleguide/cpp_styleguide.cpp @@ -213,11 +213,11 @@ namespace NPlugins { "inline $type$* Add$rname$() { return add_$name$(); }\n" "inline const $type$& get_idx_$name$(int _index) const { return $name$(_index); }\n" "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n" - " get_arr_$name$() const { return $name$(); }\n" + " get_arr_$name$() const { return $name$(); }\n" "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n" - " Get$rname$() const { return $name$(); }\n" + " Get$rname$() const { return $name$(); }\n" "inline ::google::protobuf::RepeatedPtrField< $type$ >*\n" - " Mutable$rname$() { return mutable_$name$(); }\n"); + " Mutable$rname$() { return mutable_$name$(); }\n"); if (Variables_.end() != Variables_.find("RName")) printer->Print(Variables_, @@ -225,9 +225,9 @@ namespace NPlugins { "inline $type$* Mutable$RName$(size_t _index) {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); return mutable_$name$(int(_index)); }\n" "inline $type$* Add$RName$() { return add_$name$(); }\n" "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n" - " Get$RName$() const { return $name$(); }\n" + " Get$RName$() const { return $name$(); }\n" "inline ::google::protobuf::RepeatedPtrField< $type$ >*\n" - " Mutable$RName$() { return mutable_$name$(); }\n" + " Mutable$RName$() { return mutable_$name$(); }\n" ); } @@ -457,20 +457,20 @@ namespace NPlugins { "inline void Add$rname$($type$ value) { add_$name$(value); }\n" "inline $type$ get_idx_$name$(int _index) const { return $name$(_index); }\n" "inline const ::google::protobuf::RepeatedField< $type$ >&\n" - " get_arr_$name$() const { return $name$(); }\n" + " get_arr_$name$() const { return $name$(); }\n" "inline const ::google::protobuf::RepeatedField< $type$ >&\n" - " Get$rname$() const { return $name$(); }\n" + " Get$rname$() const { return $name$(); }\n" "inline ::google::protobuf::RepeatedField< $type$ >*\n" - " Mutable$rname$() { return mutable_$name$(); }\n"); + " Mutable$rname$() { return mutable_$name$(); }\n"); if (Variables_.end() != Variables_.find("RName")) printer->Print(Variables_, "inline $type$ Get$RName$(size_t _index) const {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); return $name$(_index); }\n" "inline void Set$RName$(size_t _index, $type$ value) {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); set_$name$(_index, value); }\n" "inline void Add$RName$($type$ value) { add_$name$(value); }\n" "inline const ::google::protobuf::RepeatedField< $type$ >&\n" - " Get$RName$() const { return $name$(); }\n" + " Get$RName$() const { return $name$(); }\n" "inline ::google::protobuf::RepeatedField< $type$ >*\n" - " Mutable$RName$() { return mutable_$name$(); }\n" + " Mutable$RName$() { return mutable_$name$(); }\n" ); } diff --git a/contrib/tools/python/pyconfig.inc b/contrib/tools/python/pyconfig.inc index c07dab5d5e..01da7d4566 100644 --- a/contrib/tools/python/pyconfig.inc +++ b/contrib/tools/python/pyconfig.inc @@ -17,10 +17,10 @@ CFLAGS( ) IF (NOT WIN32) - EXTRALIBS(-lutil) + EXTRALIBS(-lutil) IF (LINUX) - EXTRALIBS(-ldl) + EXTRALIBS(-ldl) CFLAGS( -DUNQUOTED_PYTHONPATH=:plat-linux2 diff --git a/contrib/tools/python/src/config_init.c b/contrib/tools/python/src/config_init.c index 91f4d970b3..2fa47000b8 100644 --- a/contrib/tools/python/src/config_init.c +++ b/contrib/tools/python/src/config_init.c @@ -84,7 +84,7 @@ extern void initpyexpat(void); extern void initselect(void); extern void initmsvcrt(void); extern void init_subprocess(void); -extern void init_winreg(void); +extern void init_winreg(void); #endif #if !defined(_CYGWIN_) diff --git a/contrib/tools/python/src/config_map.c b/contrib/tools/python/src/config_map.c index 9e1869bd08..6bfe125ddd 100644 --- a/contrib/tools/python/src/config_map.c +++ b/contrib/tools/python/src/config_map.c @@ -84,7 +84,7 @@ {"select", initselect}, {"msvcrt", initmsvcrt}, {"_subprocess", init_subprocess}, -{"_winreg", init_winreg}, +{"_winreg", init_winreg}, #endif #if defined(_x86_) && !defined(_CYGWIN_) || defined(__powerpc__) || defined(__aarch64__) diff --git a/library/cpp/cgiparam/cgiparam.cpp b/library/cpp/cgiparam/cgiparam.cpp index 3b1e3c7661..f3277b8e4b 100644 --- a/library/cpp/cgiparam/cgiparam.cpp +++ b/library/cpp/cgiparam/cgiparam.cpp @@ -227,47 +227,47 @@ bool TCgiParameters::Has(const TStringBuf name, const TStringBuf value) const no return false; } - -TQuickCgiParam::TQuickCgiParam(const TStringBuf cgiParamStr) { + +TQuickCgiParam::TQuickCgiParam(const TStringBuf cgiParamStr) { UnescapeBuf.reserve(CgiUnescapeBufLen(cgiParamStr.size())); - char* buf = UnescapeBuf.begin(); - - auto f = [this, &buf](const TStringBuf key, const TStringBuf val) { - TStringBuf name = CgiUnescapeBuf(buf, key); + char* buf = UnescapeBuf.begin(); + + auto f = [this, &buf](const TStringBuf key, const TStringBuf val) { + TStringBuf name = CgiUnescapeBuf(buf, key); buf += name.size() + 1; - TStringBuf value = CgiUnescapeBuf(buf, val); + TStringBuf value = CgiUnescapeBuf(buf, val); buf += value.size() + 1; Y_ASSERT(buf <= UnescapeBuf.begin() + UnescapeBuf.capacity() + 1 /*trailing zero*/); - emplace(name, value); - }; - - DoScan<false>(cgiParamStr, f); - - if (buf != UnescapeBuf.begin()) { - UnescapeBuf.ReserveAndResize(buf - UnescapeBuf.begin() - 1 /*trailing zero*/); - } -} - + emplace(name, value); + }; + + DoScan<false>(cgiParamStr, f); + + if (buf != UnescapeBuf.begin()) { + UnescapeBuf.ReserveAndResize(buf - UnescapeBuf.begin() - 1 /*trailing zero*/); + } +} + const TStringBuf& TQuickCgiParam::Get(const TStringBuf name, size_t pos) const noexcept { - const auto pair = equal_range(name); - - for (auto it = pair.first; it != pair.second; ++it, --pos) { - if (0 == pos) { - return it->second; - } - } - - return Default<TStringBuf>(); -} - + const auto pair = equal_range(name); + + for (auto it = pair.first; it != pair.second; ++it, --pos) { + if (0 == pos) { + return it->second; + } + } + + return Default<TStringBuf>(); +} + bool TQuickCgiParam::Has(const TStringBuf name, const TStringBuf value) const noexcept { - const auto pair = equal_range(name); - - for (auto it = pair.first; it != pair.second; ++it) { - if (value == it->second) { - return true; - } - } - - return false; -} + const auto pair = equal_range(name); + + for (auto it = pair.first; it != pair.second; ++it) { + if (value == it->second) { + return true; + } + } + + return false; +} diff --git a/library/cpp/cgiparam/cgiparam.h b/library/cpp/cgiparam/cgiparam.h index c7102bc40f..87d1ab0ad4 100644 --- a/library/cpp/cgiparam/cgiparam.h +++ b/library/cpp/cgiparam/cgiparam.h @@ -155,30 +155,30 @@ void TCgiParameters::ReplaceUnescaped(const TStringBuf key, TIter valuesBegin, c } } } - -/** TQuickCgiParam is a faster non-editable version of TCgiParameters. - * Care should be taken when replacing: - * - note that the result of Get() is invalidated when TQuickCgiParam object is destroyed. - */ - -class TQuickCgiParam: public TMultiMap<TStringBuf, TStringBuf> { -public: + +/** TQuickCgiParam is a faster non-editable version of TCgiParameters. + * Care should be taken when replacing: + * - note that the result of Get() is invalidated when TQuickCgiParam object is destroyed. + */ + +class TQuickCgiParam: public TMultiMap<TStringBuf, TStringBuf> { +public: TQuickCgiParam() = default; - - explicit TQuickCgiParam(const TStringBuf cgiParamStr); - + + explicit TQuickCgiParam(const TStringBuf cgiParamStr); + Y_PURE_FUNCTION bool Has(const TStringBuf name, const TStringBuf value) const noexcept; Y_PURE_FUNCTION bool Has(const TStringBuf name) const noexcept { - const auto pair = equal_range(name); - return pair.first != pair.second; - } - + const auto pair = equal_range(name); + return pair.first != pair.second; + } + Y_PURE_FUNCTION const TStringBuf& Get(const TStringBuf name, size_t numOfValue = 0) const noexcept; - -private: - TString UnescapeBuf; -}; + +private: + TString UnescapeBuf; +}; diff --git a/library/cpp/cgiparam/cgiparam_ut.cpp b/library/cpp/cgiparam/cgiparam_ut.cpp index eb185b3370..a562342084 100644 --- a/library/cpp/cgiparam/cgiparam_ut.cpp +++ b/library/cpp/cgiparam/cgiparam_ut.cpp @@ -17,27 +17,27 @@ Y_UNIT_TEST_SUITE(TCgiParametersTest) { UNIT_ASSERT(!C.Has("zzzzzz")); } - Y_UNIT_TEST(TestQuick) { - TQuickCgiParam C("aaa=b%62b&ccc=ddd&ag0="); - UNIT_ASSERT_EQUAL(C.Get("aaa") == "bbb", true); - UNIT_ASSERT(C.Has("ccc", "ddd")); - UNIT_ASSERT(C.Has("ag0", "")); - UNIT_ASSERT(!C.Has("a", "bbb")); - UNIT_ASSERT(!C.Has("aaa", "bb")); - - UNIT_ASSERT(C.Has("ccc")); - UNIT_ASSERT(!C.Has("zzzzzz")); - - TQuickCgiParam D = std::move(C); - UNIT_ASSERT(D.Has("aaa")); - - TQuickCgiParam E(""); - UNIT_ASSERT(!E.Has("aaa")); - - C = std::move(E); - UNIT_ASSERT(!C.Has("aaa")); - } - + Y_UNIT_TEST(TestQuick) { + TQuickCgiParam C("aaa=b%62b&ccc=ddd&ag0="); + UNIT_ASSERT_EQUAL(C.Get("aaa") == "bbb", true); + UNIT_ASSERT(C.Has("ccc", "ddd")); + UNIT_ASSERT(C.Has("ag0", "")); + UNIT_ASSERT(!C.Has("a", "bbb")); + UNIT_ASSERT(!C.Has("aaa", "bb")); + + UNIT_ASSERT(C.Has("ccc")); + UNIT_ASSERT(!C.Has("zzzzzz")); + + TQuickCgiParam D = std::move(C); + UNIT_ASSERT(D.Has("aaa")); + + TQuickCgiParam E(""); + UNIT_ASSERT(!E.Has("aaa")); + + C = std::move(E); + UNIT_ASSERT(!C.Has("aaa")); + } + Y_UNIT_TEST(TestScan2) { const TString parsee("=000&aaa=bbb&ag0=&ccc=ddd"); TCgiParameters c; diff --git a/library/cpp/charset/codepage.cpp b/library/cpp/charset/codepage.cpp index 537a4f9f79..0431bef31b 100644 --- a/library/cpp/charset/codepage.cpp +++ b/library/cpp/charset/codepage.cpp @@ -72,7 +72,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 { @@ -258,8 +258,8 @@ void DoDecodeUnknownPlane(TxChar* str, TxChar*& ee, const ECharset enc) { for (TxChar* s = str; s < e; s++) { if (Hi8(Lo16(*s)) == 0xF0) *s = (TxChar)cp->unicode[Lo8(Lo16(*s))]; // NOT mb compliant - } - } else if (enc == CODES_UTF8) { + } + } else if (enc == CODES_UTF8) { TxChar* s; TxChar* d; @@ -272,13 +272,13 @@ void DoDecodeUnknownPlane(TxChar* str, TxChar*& ee, const ECharset enc) { *d++ = BROKEN_RUNE; ++s; } - } - e = d; + } + e = d; } else if (enc == CODES_UNKNOWN) { for (TxChar* s = str; s < e; s++) { if (Hi8(Lo16(*s)) == 0xF0) *s = Lo8(Lo16(*s)); - } + } } else { Y_ASSERT(!SingleByteCodepage(enc)); @@ -307,13 +307,13 @@ void DoDecodeUnknownPlane(TxChar* str, TxChar*& ee, const ECharset enc) { *d++ = *s; } } - } - ee = e; -} - + } + ee = e; +} + void DecodeUnknownPlane(wchar16* str, wchar16*& ee, const ECharset enc) { DoDecodeUnknownPlane(str, ee, enc); -} +} void DecodeUnknownPlane(wchar32* str, wchar32*& ee, const ECharset enc) { DoDecodeUnknownPlane(str, ee, enc); } diff --git a/library/cpp/charset/codepage.h b/library/cpp/charset/codepage.h index 7b7d4ee48c..30a02a4610 100644 --- a/library/cpp/charset/codepage.h +++ b/library/cpp/charset/codepage.h @@ -215,8 +215,8 @@ struct Encoder { void Tr(const wchar32* in, char* out, size_t len) const; void Tr(const wchar32* in, char* out) const; char* DefaultPlane; -}; - +}; + /*****************************************************************\ * struct Recoder * \*****************************************************************/ @@ -287,10 +287,10 @@ inline unsigned char CodePage::ToTitle(unsigned char ch) const { extern const CodePage& csYandex; /// these functions change (lowers) [end] position in case of utf-8 -/// null character is NOT assumed or written at [*end] +/// null character is NOT assumed or written at [*end] void DecodeUnknownPlane(wchar16* start, wchar16*& end, const ECharset enc4unk); void DecodeUnknownPlane(wchar32* start, wchar32*& end, const ECharset enc4unk); - + inline void ToLower(char* s, size_t n, const CodePage& cp = csYandex) { char* const e = s + n; for (; s != e; ++s) diff --git a/library/cpp/deprecated/mapped_file/mapped_file.cpp b/library/cpp/deprecated/mapped_file/mapped_file.cpp index 10032d8aab..b0e4511299 100644 --- a/library/cpp/deprecated/mapped_file/mapped_file.cpp +++ b/library/cpp/deprecated/mapped_file/mapped_file.cpp @@ -25,8 +25,8 @@ void TMappedFile::precharge(size_t off, size_t size) const { return; Map_->Precharge(off, size); -} - +} + void TMappedFile::init(const TString& name) { THolder<TFileMap> map(new TFileMap(name)); TMappedFile newFile(map.Get(), name.data()); diff --git a/library/cpp/deprecated/mapped_file/mapped_file.h b/library/cpp/deprecated/mapped_file/mapped_file.h index 32bd169c57..45859ed65a 100644 --- a/library/cpp/deprecated/mapped_file/mapped_file.h +++ b/library/cpp/deprecated/mapped_file/mapped_file.h @@ -25,11 +25,11 @@ public: TMappedFile() { Map_ = nullptr; } - + ~TMappedFile() { term(); } - + explicit TMappedFile(const TString& name) { Map_ = nullptr; init(name, TFileMap::oRdOnly); @@ -54,16 +54,16 @@ public: Map_ = nullptr; } } - + size_t getSize() const { return (Map_ ? Map_->MappedSize() : 0); } - + void* getData(size_t pos = 0) const { Y_ASSERT(!Map_ || (pos <= getSize())); return (Map_ ? (void*)((unsigned char*)Map_->Ptr() + pos) : nullptr); } - + void precharge(size_t pos = 0, size_t size = (size_t)-1) const; void swap(TMappedFile& file) noexcept { diff --git a/library/cpp/digest/md5/md5.cpp b/library/cpp/digest/md5/md5.cpp index 198afaabd9..24a5b69eef 100644 --- a/library/cpp/digest/md5/md5.cpp +++ b/library/cpp/digest/md5/md5.cpp @@ -192,15 +192,15 @@ char* MD5::End(char* buf) { char* MD5::End_b64(char* buf) { ui8 digest[16]; if (!buf) - buf = (char*)malloc(25); + buf = (char*)malloc(25); if (!buf) return nullptr; - Final(digest); + Final(digest); Base64Encode(buf, digest, 16); buf[24] = '\0'; - return buf; -} - + return buf; +} + ui64 MD5::EndHalfMix() { ui8 digest[16]; Final(digest); diff --git a/library/cpp/getopt/small/opt.cpp b/library/cpp/getopt/small/opt.cpp index a78f8adc9d..744501765c 100644 --- a/library/cpp/getopt/small/opt.cpp +++ b/library/cpp/getopt/small/opt.cpp @@ -116,4 +116,4 @@ int opt_get_number(int& argc, char* argv[]) { } } return num; -} +} diff --git a/library/cpp/getopt/small/opt2.cpp b/library/cpp/getopt/small/opt2.cpp index eba042e3a6..0cdc774e78 100644 --- a/library/cpp/getopt/small/opt2.cpp +++ b/library/cpp/getopt/small/opt2.cpp @@ -4,63 +4,63 @@ #include <util/generic/utility.h> #include <util/generic/yexception.h> #include <util/str_stl.h> - + #include <stdio.h> #include <errno.h> #include <ctype.h> -void Opt2::Clear() { - Specs.clear(); - memset(SpecsMap, 0, sizeof(SpecsMap)); - Pos.clear(); -} - +void Opt2::Clear() { + Specs.clear(); + memset(SpecsMap, 0, sizeof(SpecsMap)); + Pos.clear(); +} + void Opt2::Init(int argc, char* const* argv, const char* optspec, IntRange free_args_num, const char* long_alias) { - Clear(); + Clear(); Argc = argc; - Argv = argv; - HasErrors = false, BadPosCount = false, UnknownOption = 0, OptionMissingArg = 0; + Argv = argv; + HasErrors = false, BadPosCount = false, UnknownOption = 0, OptionMissingArg = 0; UnknownLongOption = nullptr; - OptionWrongArg = 0, RequiredOptionMissing = 0; - EatArgv(optspec, long_alias); - MinArgs = Min<int>(free_args_num.Left, free_args_num.Right); - MaxArgs = Max<int>(free_args_num.Left, free_args_num.Right); - if (!HasErrors && MinArgs != -1 && ((int)Pos.size() < MinArgs || (int)Pos.size() > MaxArgs)) - BadPosCount = HasErrors = true; -} - + OptionWrongArg = 0, RequiredOptionMissing = 0; + EatArgv(optspec, long_alias); + MinArgs = Min<int>(free_args_num.Left, free_args_num.Right); + MaxArgs = Max<int>(free_args_num.Left, free_args_num.Right); + if (!HasErrors && MinArgs != -1 && ((int)Pos.size() < MinArgs || (int)Pos.size() > MaxArgs)) + BadPosCount = HasErrors = true; +} + void Opt2::EatArgv(const char* optspec, const char* long_alias) { - // some flags - bool require_order = false; - if (*optspec == '+') { - require_order = true; - optspec++; - } - if (*optspec == '-') + // some flags + bool require_order = false; + if (*optspec == '+') { + require_order = true; + optspec++; + } + if (*optspec == '-') ythrow yexception() << "Flag '-' can not be used in Opt2's optspec"; - // step 1 - parse optspec + // step 1 - parse optspec for (const char* s = optspec; *s; s++) { - if (SpecsMap[(ui8)*s]) + if (SpecsMap[(ui8)*s]) ythrow yexception() << "Symbol '" << *s << "' is met twice in Opt2's optspec"; - if (*s == '?' || *s == '-') + if (*s == '?' || *s == '-') ythrow yexception() << "Opt2: Symbol '" << *s << "' can not be used in optspec because it is reserved"; - Specs.push_back(Opt2Param()); + Specs.push_back(Opt2Param()); SpecsMap[(ui8)*s] = (ui8)Specs.size(); // actual index + 1 - Specs.back().opt = *s; - if (s[1] == ':') { - Specs.back().HasArg = true; - if (s[2] == ':') + Specs.back().opt = *s; + if (s[1] == ':') { + Specs.back().HasArg = true; + if (s[2] == ':') ythrow yexception() << "Opt2 does not accept optional parameters (e.g. \"a::\") in optspec"; - s++; - } - } - // long_alias has a form "long-name1=A,long-name2=B", etc. - // This implementation is limited to aliasing a single long option - // with single short option (extend it if you really need). + s++; + } + } + // long_alias has a form "long-name1=A,long-name2=B", etc. + // This implementation is limited to aliasing a single long option + // with single short option (extend it if you really need). THashMap<const char*, char> long2short; - long2short["help"] = '?'; + long2short["help"] = '?'; long_alias = long_alias ? long_alias : ""; - alias_copy = long_alias; + alias_copy = long_alias; for (char* s = alias_copy.begin(); s && *s;) { char* eq = strchr(s, '='); char* comma = strchr(s, ','); @@ -68,180 +68,180 @@ void Opt2::EatArgv(const char* optspec, const char* long_alias) { *comma = 0; if (!eq || (comma && comma < eq)) ythrow yexception() << "Opt2, long_alias: '=' is expected after " << s; - *eq++ = 0; - if (!*eq || eq[1]) + *eq++ = 0; + if (!*eq || eq[1]) ythrow yexception() << "Opt2, long_alias: single letter must be assigned to " << s; - if (!SpecsMap[(ui8)*eq]) + if (!SpecsMap[(ui8)*eq]) ythrow yexception() << "Opt2, long_alias: trying to assign unknown option '" << *eq << "' to " << s; Opt2Param& p = Specs[SpecsMap[(ui8)*eq] - 1]; - // If several long options aliased to some letter, only last one is shown in usage - p.LongOptName = s; - if (long2short.find(s) != long2short.end()) + // If several long options aliased to some letter, only last one is shown in usage + p.LongOptName = s; + if (long2short.find(s) != long2short.end()) ythrow yexception() << "Opt2, long_alias: " << s << " specified twice"; - long2short[s] = *eq; + long2short[s] = *eq; s = comma ? comma + 1 : nullptr; - } - - if (Argc < 1) { - HasErrors = true; - return; - } - - // step 2 - parse argv - int ind = 1; - for (; ind != Argc; ind++) { - if (*Argv[ind] != '-') { - if (require_order) // everything now goes to Pos - break; - Pos.push_back(Argv[ind]); - continue; - } + } + + if (Argc < 1) { + HasErrors = true; + return; + } + + // step 2 - parse argv + int ind = 1; + for (; ind != Argc; ind++) { + if (*Argv[ind] != '-') { + if (require_order) // everything now goes to Pos + break; + Pos.push_back(Argv[ind]); + continue; + } const char* s = Argv[ind] + 1; - - if (*s == '-') { - if (!*++s) { // `--' terminates the list of options - ind++; - break; - } - // long option always spans one argv (--switch or --option-name=value) + + if (*s == '-') { + if (!*++s) { // `--' terminates the list of options + ind++; + break; + } + // long option always spans one argv (--switch or --option-name=value) const char* eq = strchr(s, '='); TString lname(s, eq ? (size_t)(eq - s) : (size_t)strlen(s)); THashMap<const char*, char>::iterator i = long2short.find(lname.data()); - if (i == long2short.end()) { + if (i == long2short.end()) { UnknownLongOption = strdup(lname.data()); // free'd in AutoUsage() - HasErrors = true; - return; - } - if (i->second == '?') { - UnknownOption = '?'; - HasErrors = true; - continue; - } + HasErrors = true; + return; + } + if (i->second == '?') { + UnknownOption = '?'; + HasErrors = true; + continue; + } Opt2Param& p = Specs[SpecsMap[(ui8)i->second] - 1]; - p.IsFound = true; - if (p.HasArg && !eq) { - HasErrors = true; - OptionMissingArg = p.opt; // short option, indeed - return; - } - if (!p.HasArg && eq) { - HasErrors = true; - OptionWrongArg = p.opt; // short option, indeed - return; - } - if (eq) - p.ActualValue.push_back(eq + 1); - continue; - } - - for (; *s; s++) { - if (!SpecsMap[(ui8)*s]) { - UnknownOption = *s; - HasErrors = true; - if (*s == '?') - continue; - return; - } + p.IsFound = true; + if (p.HasArg && !eq) { + HasErrors = true; + OptionMissingArg = p.opt; // short option, indeed + return; + } + if (!p.HasArg && eq) { + HasErrors = true; + OptionWrongArg = p.opt; // short option, indeed + return; + } + if (eq) + p.ActualValue.push_back(eq + 1); + continue; + } + + for (; *s; s++) { + if (!SpecsMap[(ui8)*s]) { + UnknownOption = *s; + HasErrors = true; + if (*s == '?') + continue; + return; + } Opt2Param& p = Specs[SpecsMap[(ui8)*s] - 1]; - p.IsFound = true; - if (p.HasArg) { + p.IsFound = true; + if (p.HasArg) { if (s[1]) p.ActualValue.push_back(s + 1); - else { - ind++; - if (ind == Argc) { - HasErrors = true; - OptionMissingArg = *s; - p.IsFound = false; - return; - } - p.ActualValue.push_back(Argv[ind]); - } - break; - } - } - } - for (; ind != Argc; ind++) - Pos.push_back(Argv[ind]); -} - + else { + ind++; + if (ind == Argc) { + HasErrors = true; + OptionMissingArg = *s; + p.IsFound = false; + return; + } + p.ActualValue.push_back(Argv[ind]); + } + break; + } + } + } + for (; ind != Argc; ind++) + Pos.push_back(Argv[ind]); +} + Opt2Param& Opt2::GetInternal(char opt, const char* defValue, const char* helpUsage, bool requred) { - if (!SpecsMap[(ui8)opt]) + if (!SpecsMap[(ui8)opt]) ythrow yexception() << "Unspecified option character '" << opt << "' asked from Opt2::Get"; Opt2Param& p = Specs[SpecsMap[(ui8)opt] - 1]; - p.DefValue = defValue; - p.HelpUsage = helpUsage; - p.IsRequired = requred; - if (!p.IsFound && requred && !HasErrors) { - RequiredOptionMissing = opt; - HasErrors = true; - } - return p; -} - -// For options with parameters + p.DefValue = defValue; + p.HelpUsage = helpUsage; + p.IsRequired = requred; + if (!p.IsFound && requred && !HasErrors) { + RequiredOptionMissing = opt; + HasErrors = true; + } + return p; +} + +// For options with parameters const char* Opt2::Arg(char opt, const char* help, const char* def, bool required) { Opt2Param& p = GetInternal(opt, def, help, required); - if (!p.HasArg) + if (!p.HasArg) ythrow yexception() << "Opt2::Arg called for '" << opt << "' which is an option without argument"; return p.IsFound ? p.ActualValue.empty() ? nullptr : p.ActualValue.back() : def; -} - -// For options with parameters +} + +// For options with parameters const char* Opt2::Arg(char opt, const char* help, TString def, bool required) { Opt2Param& p = GetInternal(opt, nullptr, help, required); - if (!p.HasArg) + if (!p.HasArg) ythrow yexception() << "Opt2::Arg called for '" << opt << "' which is an option without argument"; - p.DefValueStr = def; - p.DefValue = p.DefValueStr.begin(); + p.DefValueStr = def; + p.DefValue = p.DefValueStr.begin(); return p.IsFound ? p.ActualValue.empty() ? nullptr : p.ActualValue.back() : p.DefValue; -} - -// Options with parameters that can be specified several times +} + +// Options with parameters that can be specified several times const TVector<const char*>& Opt2::MArg(char opt, const char* help) { Opt2Param& p = GetInternal(opt, nullptr, help, false); - p.MultipleUse = true; - if (!p.HasArg) + p.MultipleUse = true; + if (!p.HasArg) ythrow yexception() << "Opt2::Arg called for '" << opt << "' which is an option without argument"; - return p.ActualValue; -} - -/// For options w/o parameters + return p.ActualValue; +} + +/// For options w/o parameters bool Opt2::Has(char opt, const char* help) { Opt2Param& p = GetInternal(opt, nullptr, help, false); - if (p.HasArg) + if (p.HasArg) ythrow yexception() << "Opt2::Has called for '" << opt << "' which is an option with argument"; - return p.IsFound; -} - -// Get() + strtol, may set up HasErrors + return p.IsFound; +} + +// Get() + strtol, may set up HasErrors long Opt2::Int(char opt, const char* help, long def, bool required) { Opt2Param& p = GetInternal(opt, (char*)(uintptr_t)def, help, required); - if (!p.HasArg) + if (!p.HasArg) ythrow yexception() << "Opt2::Int called for '" << opt << "' which is an option without argument"; - p.IsNumeric = true; - if (!p.IsFound || p.ActualValue.empty() || !p.ActualValue.back()) - return def; + p.IsNumeric = true; + if (!p.IsFound || p.ActualValue.empty() || !p.ActualValue.back()) + return def; char* e; - long rv = strtol(p.ActualValue.back(), &e, 10); - if (e == p.ActualValue.back() || *e) { - OptionWrongArg = opt; - HasErrors = true; - } - return rv; -} - + long rv = strtol(p.ActualValue.back(), &e, 10); + if (e == p.ActualValue.back() || *e) { + OptionWrongArg = opt; + HasErrors = true; + } + return rv; +} + // Get() + strtoul, may set up HasErrors unsigned long Opt2::UInt(char opt, const char* help, unsigned long def, bool required) { Opt2Param& p = GetInternal(opt, (char*)(uintptr_t)def, help, required); if (!p.HasArg) ythrow yexception() << "Opt2::UInt called for '" << opt << "' which is an option without argument"; p.IsNumeric = true; - if (!p.IsFound || p.ActualValue.empty() || !p.ActualValue.back()) + if (!p.IsFound || p.ActualValue.empty() || !p.ActualValue.back()) return def; char* e; - unsigned long rv = strtoul(p.ActualValue.back(), &e, 10); - if (e == p.ActualValue.back() || *e) { + unsigned long rv = strtoul(p.ActualValue.back(), &e, 10); + if (e == p.ActualValue.back() || *e) { OptionWrongArg = opt; HasErrors = true; } @@ -256,17 +256,17 @@ void Opt2::AddError(const char* message) { } int Opt2::AutoUsage(const char* free_arg_names) { - if (!HasErrors) - return 0; + if (!HasErrors) + return 0; FILE* where = UnknownOption == '?' ? stdout : stderr; - char req_str[256], nreq_str[256]; - int req = 0, nreq = 0; - for (int n = 0; n < (int)Specs.size(); n++) - if (Specs[n].IsRequired) - req_str[req++] = Specs[n].opt; - else - nreq_str[nreq++] = Specs[n].opt; - req_str[req] = 0, nreq_str[nreq] = 0; + char req_str[256], nreq_str[256]; + int req = 0, nreq = 0; + for (int n = 0; n < (int)Specs.size(); n++) + if (Specs[n].IsRequired) + req_str[req++] = Specs[n].opt; + else + nreq_str[nreq++] = Specs[n].opt; + req_str[req] = 0, nreq_str[nreq] = 0; const char* prog = strrchr(Argv[0], LOCSLASH_C); prog = prog ? prog + 1 : Argv[0]; fprintf(where, "Usage: %s%s%s%s%s%s%s%s\n", prog, req ? " -" : "", req_str, @@ -278,107 +278,107 @@ int Opt2::AutoUsage(const char* free_arg_names) { fprintf(where, " -%c %s\n", spec.opt, hlp); else if (!spec.IsNumeric) fprintf(where, " -%c %s [Default: %s]\n", spec.opt, hlp, spec.DefValue); - else + else fprintf(where, " -%c %s [Def.val: %li]\n", spec.opt, hlp, (long)(uintptr_t)spec.DefValue); if (spec.LongOptName) fprintf(where, " --%s%s - same as -%c\n", spec.LongOptName, spec.HasArg ? "=<argument>" : "", spec.opt); - } - if (OptionMissingArg) - fprintf(where, " *** Option '%c' is missing required argument\n", OptionMissingArg); - if (OptionWrongArg) - fprintf(where, " *** Incorrect argument for option '%c'\n", OptionWrongArg); - if (UnknownOption && UnknownOption != '?') - fprintf(where, " *** Unknown option '%c'\n", UnknownOption); - if (UnknownLongOption) { - fprintf(where, " *** Unknown long option '%s'\n", UnknownLongOption); - free(UnknownLongOption); + } + if (OptionMissingArg) + fprintf(where, " *** Option '%c' is missing required argument\n", OptionMissingArg); + if (OptionWrongArg) + fprintf(where, " *** Incorrect argument for option '%c'\n", OptionWrongArg); + if (UnknownOption && UnknownOption != '?') + fprintf(where, " *** Unknown option '%c'\n", UnknownOption); + if (UnknownLongOption) { + fprintf(where, " *** Unknown long option '%s'\n", UnknownLongOption); + free(UnknownLongOption); UnknownLongOption = nullptr; - } - if (RequiredOptionMissing) - fprintf(where, " *** Required option '%c' missing\n", RequiredOptionMissing); - if (BadPosCount && MinArgs != MaxArgs) - fprintf(where, " *** %i free argument(s) supplied, expected %i to %i\n", (int)Pos.size(), MinArgs, MaxArgs); - if (BadPosCount && MinArgs == MaxArgs) - fprintf(where, " *** %i free argument(s) supplied, expected %i\n", (int)Pos.size(), MinArgs); + } + if (RequiredOptionMissing) + fprintf(where, " *** Required option '%c' missing\n", RequiredOptionMissing); + if (BadPosCount && MinArgs != MaxArgs) + fprintf(where, " *** %i free argument(s) supplied, expected %i to %i\n", (int)Pos.size(), MinArgs, MaxArgs); + if (BadPosCount && MinArgs == MaxArgs) + fprintf(where, " *** %i free argument(s) supplied, expected %i\n", (int)Pos.size(), MinArgs); for (const auto& userErrorMessage : UserErrorMessages) fprintf(where, " *** %s\n", userErrorMessage.data()); - return UnknownOption == '?' ? 1 : 2; -} - + return UnknownOption == '?' ? 1 : 2; +} + void Opt2::AutoUsageErr(const char* free_arg_names) { - if (AutoUsage(free_arg_names)) - exit(1); -} - -#ifdef OPT2_TEST -// TODO: convert it to unittest - -bool opt2_ut_fail = false, opt_ut_verbose = false; + if (AutoUsage(free_arg_names)) + exit(1); +} + +#ifdef OPT2_TEST +// TODO: convert it to unittest + +bool opt2_ut_fail = false, opt_ut_verbose = false; const char* ut_optspec; int ut_real(TString args, bool err_exp, const char* A_exp, int b_exp, bool a_exp, const char* p1_exp, const char* p2_exp) { char* argv[32]; - int argc = sf(' ', argv, args.begin()); - Opt2 opt(argc, argv, ut_optspec, 2, "option-1=A,option-2=a,"); + int argc = sf(' ', argv, args.begin()); + Opt2 opt(argc, argv, ut_optspec, 2, "option-1=A,option-2=a,"); const char* A = opt.Arg('A', "<qqq> - blah"); int b = opt.Int('b', "<rrr> - blah", 2); bool a = opt.Has('a', "- blah"); /*const char *C = */ opt.Arg('C', "<ccc> - blah", 0); - - if (opt_ut_verbose) - opt.AutoUsage(""); - if (opt.HasErrors != err_exp) - return 1; - if (err_exp) - return false; - if (!A && A_exp || A && !A_exp || A && A_exp && strcmp(A, A_exp)) - return 2; - if (b != b_exp) - return 3; - if (a != a_exp) - return 4; - if (strcmp(opt.Pos[0], p1_exp)) - return 5; - if (strcmp(opt.Pos[1], p2_exp)) - return 6; - return false; -} - + + if (opt_ut_verbose) + opt.AutoUsage(""); + if (opt.HasErrors != err_exp) + return 1; + if (err_exp) + return false; + if (!A && A_exp || A && !A_exp || A && A_exp && strcmp(A, A_exp)) + return 2; + if (b != b_exp) + return 3; + if (a != a_exp) + return 4; + if (strcmp(opt.Pos[0], p1_exp)) + return 5; + if (strcmp(opt.Pos[1], p2_exp)) + return 6; + return false; +} + void ut(const char* args, bool err_exp, const char* A_exp, int b_exp, bool a_exp, const char* p1_exp, const char* p2_exp) { - if (opt_ut_verbose) - fprintf(stderr, "Testing: %s\n", args); - if (int rv = ut_real(args, err_exp, A_exp, b_exp, a_exp, p1_exp, p2_exp)) { - opt2_ut_fail = true; - fprintf(stderr, "Test %i failed for: %s\n", rv, args); - } else { - if (opt_ut_verbose) - fprintf(stderr, "OK\n"); - } -} - + if (opt_ut_verbose) + fprintf(stderr, "Testing: %s\n", args); + if (int rv = ut_real(args, err_exp, A_exp, b_exp, a_exp, p1_exp, p2_exp)) { + opt2_ut_fail = true; + fprintf(stderr, "Test %i failed for: %s\n", rv, args); + } else { + if (opt_ut_verbose) + fprintf(stderr, "OK\n"); + } +} + int main(int argc, char* argv[]) { - Opt2 opt(argc, argv, "v", 0); - opt_ut_verbose = opt.Has('v', "- some verboseness"); - opt.AutoUsageErr(""); - ut_optspec = "A:ab:C:"; - ut("prog -A argA -a -b 22 -C argC Pos1 Pos2", false, "argA", 22, true, "Pos1", "Pos2"); - ut("prog Pos1 -A argA -a -C argC Pos2", false, "argA", 2, true, "Pos1", "Pos2"); - ut("prog -A argA Pos1 -b22 Pos2 -C argC", false, "argA", 22, false, "Pos1", "Pos2"); - ut("prog -A argA Pos1 -b 22 Pos2 -C", true, "argA", 22, false, "Pos1", "Pos2"); - ut("prog -A argA -a -b 22 -C Pos1 Pos2", true, "argA", 22, true, "Pos1", "Pos2"); - ut("prog -A argA -a -b two -C argC Pos1 Pos2", true, "argA", 2, true, "Pos1", "Pos2"); - ut("prog -a -b 22 -C argC Pos1 Pos2", true, "argA", 22, true, "Pos1", "Pos2"); - ut("prog Pos1 --option-1=argA -a -C argC Pos2", false, "argA", 2, true, "Pos1", "Pos2"); - ut("prog Pos1 -A argA --option-1 -a -C argC Pos2", true, "argA", 2, true, "Pos1", "Pos2"); - ut("prog -A argA --option-2 -b -22 -C argC Pos1 Pos2", false, "argA", -22, true, "Pos1", "Pos2"); - ut("prog -A argA --option-2 -b -22 -- -C argC", false, "argA", -22, true, "-C", "argC"); - ut("prog -A argA --option-2=1 -b -22 -C argC Pos1 Pos2", true, "argA", -22, true, "Pos1", "Pos2"); - - ut_optspec = "+A:ab:C:"; - ut("prog -A argA --option-2 v1 -C", false, "argA", 2, true, "v1", "-C"); - ut("prog -A argA --option-2 v1 -C argC", true, "argA", 2, true, "v1", "-C"); - if (!opt2_ut_fail) - fprintf(stderr, "All OK\n"); - return opt2_ut_fail; -} - -#endif // OPT2_TEST + Opt2 opt(argc, argv, "v", 0); + opt_ut_verbose = opt.Has('v', "- some verboseness"); + opt.AutoUsageErr(""); + ut_optspec = "A:ab:C:"; + ut("prog -A argA -a -b 22 -C argC Pos1 Pos2", false, "argA", 22, true, "Pos1", "Pos2"); + ut("prog Pos1 -A argA -a -C argC Pos2", false, "argA", 2, true, "Pos1", "Pos2"); + ut("prog -A argA Pos1 -b22 Pos2 -C argC", false, "argA", 22, false, "Pos1", "Pos2"); + ut("prog -A argA Pos1 -b 22 Pos2 -C", true, "argA", 22, false, "Pos1", "Pos2"); + ut("prog -A argA -a -b 22 -C Pos1 Pos2", true, "argA", 22, true, "Pos1", "Pos2"); + ut("prog -A argA -a -b two -C argC Pos1 Pos2", true, "argA", 2, true, "Pos1", "Pos2"); + ut("prog -a -b 22 -C argC Pos1 Pos2", true, "argA", 22, true, "Pos1", "Pos2"); + ut("prog Pos1 --option-1=argA -a -C argC Pos2", false, "argA", 2, true, "Pos1", "Pos2"); + ut("prog Pos1 -A argA --option-1 -a -C argC Pos2", true, "argA", 2, true, "Pos1", "Pos2"); + ut("prog -A argA --option-2 -b -22 -C argC Pos1 Pos2", false, "argA", -22, true, "Pos1", "Pos2"); + ut("prog -A argA --option-2 -b -22 -- -C argC", false, "argA", -22, true, "-C", "argC"); + ut("prog -A argA --option-2=1 -b -22 -C argC Pos1 Pos2", true, "argA", -22, true, "Pos1", "Pos2"); + + ut_optspec = "+A:ab:C:"; + ut("prog -A argA --option-2 v1 -C", false, "argA", 2, true, "v1", "-C"); + ut("prog -A argA --option-2 v1 -C argC", true, "argA", 2, true, "v1", "-C"); + if (!opt2_ut_fail) + fprintf(stderr, "All OK\n"); + return opt2_ut_fail; +} + +#endif // OPT2_TEST diff --git a/library/cpp/getopt/small/opt2.h b/library/cpp/getopt/small/opt2.h index 51800d0f6e..4d9d943237 100644 --- a/library/cpp/getopt/small/opt2.h +++ b/library/cpp/getopt/small/opt2.h @@ -3,33 +3,33 @@ #include <util/system/defaults.h> #include <util/generic/string.h> #include <util/generic/vector.h> - -// simplified options parser -// No 'optional argument' (e.g. "a::" in spec.) support; -// Supports '+' switch (see opt.h), does not support '-'; - -/** Typical use - Opt2 opt(argc, argv, "A:b:c", 3); <- 3 more arguments expected, opt.Pos[0], etc. - ** Usage description for options is provided through functions that query values ** - const char *a = opt.Arg('A', "<var_name> - usage of -A"); <- This option is required - int b = opt.Int('b', "<var_name> - usage of -b", 2); <- This option has default value, not required - bool c = opt.Has('c', "- usage of -c"); <- switches are always optional - - ** Additional argument names are provided in AutoUsage call ** - ** AutoUsage generages 'USAGE' text automatically ** - if (opt.AutoUsage("<L> <M>")) <- Returns 1 if there was any error in getopt - return 1; - OR: opt.AutoUsageErr("<L> <M>"); <- Will terminate program for you :) -*/ - -// Note: struct Opt2Param can be moved to cpp-file -struct Opt2Param { - char opt; - bool HasArg; - bool IsFound; - bool IsNumeric; - bool IsRequired; - bool MultipleUse; + +// simplified options parser +// No 'optional argument' (e.g. "a::" in spec.) support; +// Supports '+' switch (see opt.h), does not support '-'; + +/** Typical use + Opt2 opt(argc, argv, "A:b:c", 3); <- 3 more arguments expected, opt.Pos[0], etc. + ** Usage description for options is provided through functions that query values ** + const char *a = opt.Arg('A', "<var_name> - usage of -A"); <- This option is required + int b = opt.Int('b', "<var_name> - usage of -b", 2); <- This option has default value, not required + bool c = opt.Has('c', "- usage of -c"); <- switches are always optional + + ** Additional argument names are provided in AutoUsage call ** + ** AutoUsage generages 'USAGE' text automatically ** + if (opt.AutoUsage("<L> <M>")) <- Returns 1 if there was any error in getopt + return 1; + OR: opt.AutoUsageErr("<L> <M>"); <- Will terminate program for you :) +*/ + +// Note: struct Opt2Param can be moved to cpp-file +struct Opt2Param { + char opt; + bool HasArg; + bool IsFound; + bool IsNumeric; + bool IsRequired; + bool MultipleUse; const char* DefValue; TString DefValueStr; TString HelpUsage; @@ -45,10 +45,10 @@ struct Opt2Param { , LongOptName(nullptr) { } -}; - -struct IntRange { - int Left, Right; +}; + +struct IntRange { + int Left, Right; IntRange() = delete; IntRange(int both) : Left(both) @@ -61,77 +61,77 @@ struct IntRange { , Right(right) { } -}; - -class Opt2 { -public: +}; + +class Opt2 { +public: Opt2() = default; Opt2(int argc, char* const* argv, const char* optspec, IntRange free_args_num = -1, const char* long_alias = nullptr) { - Init(argc, argv, optspec, free_args_num, long_alias); - } - - // Init throws exception only in case of incorrect optspec. - // In other cases, consult HasErrors or call AutoUsage() + Init(argc, argv, optspec, free_args_num, long_alias); + } + + // Init throws exception only in case of incorrect optspec. + // In other cases, consult HasErrors or call AutoUsage() void Init(int argc, char* const* argv, const char* optspec, IntRange free_args_num = -1, const char* long_alias = nullptr); - - // In case of incorrect options, constructs and prints Usage text, - // usually to stderr (however, to stdout if '-?' switch was used), and returns 1. + + // In case of incorrect options, constructs and prints Usage text, + // usually to stderr (however, to stdout if '-?' switch was used), and returns 1. int AutoUsage(const char* free_arg_names = ""); - - // same as AutoUsage but calls exit(1) instead of error code + + // same as AutoUsage but calls exit(1) instead of error code void AutoUsageErr(const char* free_arg_names = ""); - - // For options with parameters + + // For options with parameters const char* Arg(char opt, const char* helpUsage, const char* defValue, bool required = false); const char* Arg(char opt, const char* helpUsage) { return Arg(opt, helpUsage, nullptr, true); - } + } const char* Arg(char opt, const char* helpUsage, TString defValue, bool required = false); - - // Options with parameters that can be specified several times + + // Options with parameters that can be specified several times const TVector<const char*>& MArg(char opt, const char* helpUsage); - - // Get() + strtol, may set up HasErrors + + // Get() + strtol, may set up HasErrors long Int(char opt, const char* helpUsage, long defValue, bool required = false); long Int(char opt, const char* helpUsage) { - return Int(opt, helpUsage, 0, true); - } - + return Int(opt, helpUsage, 0, true); + } + // Get() + strtoul, may set up HasErrors unsigned long UInt(char opt, const char* helpUsage, unsigned long defValue, bool required = false); unsigned long UInt(char opt, const char* helpUsage) { return UInt(opt, helpUsage, 0, true); } - // For options w/o parameters + // For options w/o parameters bool Has(char opt, const char* helpUsage); - + // Add user defined error message and set error flag void AddError(const char* message = nullptr); -public: +public: // non-option args TVector<char*> Pos; - bool HasErrors; + bool HasErrors; private: - bool BadPosCount; - char UnknownOption; + bool BadPosCount; + char UnknownOption; char* UnknownLongOption; - char OptionMissingArg; - char OptionWrongArg; - char RequiredOptionMissing; + char OptionMissingArg; + char OptionWrongArg; + char RequiredOptionMissing; TVector<TString> UserErrorMessages; - -protected: - int Argc; - char* const* Argv; - int MinArgs, MaxArgs; - ui8 SpecsMap[256]; + +protected: + int Argc; + char* const* Argv; + int MinArgs, MaxArgs; + ui8 SpecsMap[256]; TVector<Opt2Param> Specs; TString alias_copy; void EatArgv(const char* optspec, const char* long_alias); - void Clear(); + void Clear(); Opt2Param& GetInternal(char opt, const char* defValue, const char* helpUsage, bool required); -}; +}; diff --git a/library/cpp/getopt/ut/opt2_ut.cpp b/library/cpp/getopt/ut/opt2_ut.cpp index 01ec1c8173..0e7464747c 100644 --- a/library/cpp/getopt/ut/opt2_ut.cpp +++ b/library/cpp/getopt/ut/opt2_ut.cpp @@ -2,11 +2,11 @@ #include <library/cpp/testing/unittest/registar.h> -//using namespace NLastGetopt; +//using namespace NLastGetopt; Y_UNIT_TEST_SUITE(Opt2Test) { Y_UNIT_TEST(TestSimple) { - int argc = 8; + int argc = 8; char* argv[] = { (char*)"cmd", (char*)"--aaaa=aaaa", @@ -18,7 +18,7 @@ Y_UNIT_TEST_SUITE(Opt2Test) { (char*)"ww", }; - Opt2 opt(argc, argv, "A:b:cd:e:x:", 2, "aaaa=A"); + Opt2 opt(argc, argv, "A:b:cd:e:x:", 2, "aaaa=A"); const char* edef = "edef"; const char* a = opt.Arg('A', "<var_name> - usage of -A"); @@ -33,31 +33,31 @@ Y_UNIT_TEST_SUITE(Opt2Test) { UNIT_ASSERT_VALUES_EQUAL(2, b); UNIT_ASSERT(c); UNIT_ASSERT_VALUES_EQUAL(8, d); - UNIT_ASSERT_VALUES_EQUAL((void*)edef, e); + UNIT_ASSERT_VALUES_EQUAL((void*)edef, e); UNIT_ASSERT_VALUES_EQUAL(2u, opt.Pos.size()); UNIT_ASSERT_STRINGS_EQUAL("zz", opt.Pos.at(0)); - UNIT_ASSERT_VALUES_EQUAL((void*)argv[2], opt.Pos.at(0)); + UNIT_ASSERT_VALUES_EQUAL((void*)argv[2], opt.Pos.at(0)); UNIT_ASSERT_STRINGS_EQUAL("ww", opt.Pos.at(1)); - UNIT_ASSERT_STRINGS_EQUAL("1", x.at(0)); - UNIT_ASSERT_STRINGS_EQUAL("2", x.at(1)); + UNIT_ASSERT_STRINGS_EQUAL("1", x.at(0)); + UNIT_ASSERT_STRINGS_EQUAL("2", x.at(1)); } - + Y_UNIT_TEST(TestErrors1) { - int argc = 4; - char* argv[] = { + int argc = 4; + char* argv[] = { (char*)"cmd", (char*)"zz", (char*)"-c", (char*)"-e", - }; - - Opt2 opt(argc, argv, "ce:", 2); - + }; + + Opt2 opt(argc, argv, "ce:", 2); + const char* edef = "edef"; bool c = opt.Has('c', "usage of -c"); const char* e = opt.Arg('e', "<unused> - only default is really used", edef); - UNIT_ASSERT(c); - UNIT_ASSERT_VALUES_EQUAL((void*)edef, e); - } + UNIT_ASSERT(c); + UNIT_ASSERT_VALUES_EQUAL((void*)edef, e); + } } diff --git a/library/cpp/packedtypes/packedfloat.cpp b/library/cpp/packedtypes/packedfloat.cpp index d965d5a290..6039d78969 100644 --- a/library/cpp/packedtypes/packedfloat.cpp +++ b/library/cpp/packedtypes/packedfloat.cpp @@ -1,18 +1,18 @@ -#include "packedfloat.h" - -#include <util/stream/output.h> - +#include "packedfloat.h" + +#include <util/stream/output.h> + #define OUT_IMPL(T) \ template <> \ void Out<T>(IOutputStream & os, TTypeTraits<T>::TFuncParam val) { \ os << (float)val; \ - } - -OUT_IMPL(f16) -OUT_IMPL(uf16) -OUT_IMPL(f8) -OUT_IMPL(uf8) -OUT_IMPL(f8d) -OUT_IMPL(uf8d) + } + +OUT_IMPL(f16) +OUT_IMPL(uf16) +OUT_IMPL(f8) +OUT_IMPL(uf8) +OUT_IMPL(f8d) +OUT_IMPL(uf8d) #undef OUT_IMPL diff --git a/library/cpp/string_utils/base64/base64.cpp b/library/cpp/string_utils/base64/base64.cpp index 73ff81904a..05c201f0de 100644 --- a/library/cpp/string_utils/base64/base64.cpp +++ b/library/cpp/string_utils/base64/base64.cpp @@ -150,11 +150,11 @@ char* Base64EncodeUrl(char* outstr, const unsigned char* instr, size_t len) { } inline void uudecode_1(char* dst, unsigned char* src) { - dst[0] = char((base64_bkw[src[0]] << 2) | (base64_bkw[src[1]] >> 4)); - dst[1] = char((base64_bkw[src[1]] << 4) | (base64_bkw[src[2]] >> 2)); - dst[2] = char((base64_bkw[src[2]] << 6) | base64_bkw[src[3]]); -} - + dst[0] = char((base64_bkw[src[0]] << 2) | (base64_bkw[src[1]] >> 4)); + dst[1] = char((base64_bkw[src[1]] << 4) | (base64_bkw[src[2]] >> 2)); + dst[2] = char((base64_bkw[src[2]] << 6) | base64_bkw[src[3]]); +} + static size_t Base64DecodePlain(void* dst, const char* b, const char* e) { size_t n = 0; while (b < e) { @@ -174,8 +174,8 @@ static size_t Base64DecodePlain(void* dst, const char* b, const char* e) { } } - return n; -} + return n; +} // Table for Base64StrictDecode static const char base64_bkw_strict[] = diff --git a/library/cpp/string_utils/url/url.cpp b/library/cpp/string_utils/url/url.cpp index a7f42f0360..85f4ac5d69 100644 --- a/library/cpp/string_utils/url/url.cpp +++ b/library/cpp/string_utils/url/url.cpp @@ -16,37 +16,37 @@ #include <cstdlib> namespace { - struct TUncheckedSize { + struct TUncheckedSize { static bool Has(size_t) { - return true; - } - }; - - struct TKnownSize { - size_t MySize; + return true; + } + }; + + struct TKnownSize { + size_t MySize; explicit TKnownSize(size_t sz) - : MySize(sz) + : MySize(sz) { } - bool Has(size_t sz) const { - return sz <= MySize; - } - }; - - template <typename TChar1, typename TChar2> - int Compare1Case2(const TChar1* s1, const TChar2* s2, size_t n) { - for (size_t i = 0; i < n; ++i) { + bool Has(size_t sz) const { + return sz <= MySize; + } + }; + + template <typename TChar1, typename TChar2> + int Compare1Case2(const TChar1* s1, const TChar2* s2, size_t n) { + for (size_t i = 0; i < n; ++i) { if ((TChar1)ToLower(s1[i]) != s2[i]) return (TChar1)ToLower(s1[i]) < s2[i] ? -1 : 1; - } - return 0; - } - + } + return 0; + } + template <typename TChar, typename TBounds> inline size_t GetHttpPrefixSizeImpl(const TChar* url, const TBounds& urlSize, bool ignorehttps) { const TChar httpPrefix[] = {'h', 't', 't', 'p', ':', '/', '/', 0}; const TChar httpsPrefix[] = {'h', 't', 't', 'p', 's', ':', '/', '/', 0}; - if (urlSize.Has(7) && Compare1Case2(url, httpPrefix, 7) == 0) + if (urlSize.Has(7) && Compare1Case2(url, httpPrefix, 7) == 0) return 7; if (!ignorehttps && urlSize.Has(8) && Compare1Case2(url, httpsPrefix, 8) == 0) return 8; @@ -113,8 +113,8 @@ size_t GetSchemePrefixSize(const TStringBuf url) noexcept { } return n + 3 - url.begin(); -} - +} + TStringBuf GetSchemePrefix(const TStringBuf url) noexcept { return url.Head(GetSchemePrefixSize(url)); } @@ -236,7 +236,7 @@ void GetSchemeHostAndPort(const TStringBuf url, TStringBuf& scheme, TStringBuf& } TStringBuf GetOnlyHost(const TStringBuf url) noexcept { - return GetHost(CutSchemePrefix(url)); + return GetHost(CutSchemePrefix(url)); } TStringBuf GetPathAndQuery(const TStringBuf url, bool trimFragment) noexcept { @@ -248,19 +248,19 @@ TStringBuf GetPathAndQuery(const TStringBuf url, bool trimFragment) noexcept { return trimFragment ? path.Before('#') : path; } -// this strange creature returns 2nd level domain, possibly with port +// this strange creature returns 2nd level domain, possibly with port TStringBuf GetDomain(const TStringBuf host) noexcept { const char* c = !host ? host.data() : host.end() - 1; for (bool wasPoint = false; c != host.data(); --c) { - if (*c == '.') { - if (wasPoint) { + if (*c == '.') { + if (wasPoint) { ++c; break; - } + } wasPoint = true; } } - return TStringBuf(c, host.end()); + return TStringBuf(c, host.end()); } TStringBuf GetParentDomain(const TStringBuf host, size_t level) noexcept { diff --git a/library/cpp/string_utils/url/url.h b/library/cpp/string_utils/url/url.h index 47b9f6465b..84137ccc57 100644 --- a/library/cpp/string_utils/url/url.h +++ b/library/cpp/string_utils/url/url.h @@ -35,10 +35,10 @@ Y_PURE_FUNCTION size_t GetHttpPrefixSize(const TWtringBuf url, bool ignorehttps = false) noexcept; /** BEWARE of TStringBuf! You can not use operator ~ or c_str() like in TString - !!!!!!!!!!!! */ + !!!!!!!!!!!! */ Y_PURE_FUNCTION size_t GetSchemePrefixSize(const TStringBuf url) noexcept; - + Y_PURE_FUNCTION TStringBuf GetSchemePrefix(const TStringBuf url) noexcept; diff --git a/library/cpp/string_utils/url/url_ut.cpp b/library/cpp/string_utils/url/url_ut.cpp index 66c76a63ff..1588013893 100644 --- a/library/cpp/string_utils/url/url_ut.cpp +++ b/library/cpp/string_utils/url/url_ut.cpp @@ -45,7 +45,7 @@ Y_UNIT_TEST_SUITE(TUtilUrlTest) { UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetDomain("a.b.ya.ru")); UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetDomain("ya.ru")); UNIT_ASSERT_VALUES_EQUAL("ya", GetDomain("ya")); - UNIT_ASSERT_VALUES_EQUAL("", GetDomain("")); + UNIT_ASSERT_VALUES_EQUAL("", GetDomain("")); } Y_UNIT_TEST(TestGetParentDomain) { @@ -88,27 +88,27 @@ Y_UNIT_TEST_SUITE(TUtilUrlTest) { } Y_UNIT_TEST(TestSchemeCut) { - UNIT_ASSERT_VALUES_EQUAL("ya.ru/bebe", CutSchemePrefix("http://ya.ru/bebe")); - UNIT_ASSERT_VALUES_EQUAL("yaru", CutSchemePrefix("yaru")); - UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutSchemePrefix("yaru://ya.ru://zzz")); - UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutSchemePrefix("ya.ru://zzz")); - UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutSchemePrefix("ftp://ya.ru://zzz")); - UNIT_ASSERT_VALUES_EQUAL("", CutSchemePrefix("https://")); // is that right? - - UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru", CutHttpPrefix("ftp://ya.ru")); - UNIT_ASSERT_VALUES_EQUAL("ya.ru/zzz", CutHttpPrefix("http://ya.ru/zzz")); + UNIT_ASSERT_VALUES_EQUAL("ya.ru/bebe", CutSchemePrefix("http://ya.ru/bebe")); + UNIT_ASSERT_VALUES_EQUAL("yaru", CutSchemePrefix("yaru")); + UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutSchemePrefix("yaru://ya.ru://zzz")); + UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutSchemePrefix("ya.ru://zzz")); + UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutSchemePrefix("ftp://ya.ru://zzz")); + UNIT_ASSERT_VALUES_EQUAL("", CutSchemePrefix("https://")); // is that right? + + UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru", CutHttpPrefix("ftp://ya.ru")); + UNIT_ASSERT_VALUES_EQUAL("ya.ru/zzz", CutHttpPrefix("http://ya.ru/zzz")); UNIT_ASSERT_VALUES_EQUAL("ya.ru/zzz", CutHttpPrefix("http://ya.ru/zzz", true)); - UNIT_ASSERT_VALUES_EQUAL("ya.ru/zzz", CutHttpPrefix("https://ya.ru/zzz")); + UNIT_ASSERT_VALUES_EQUAL("ya.ru/zzz", CutHttpPrefix("https://ya.ru/zzz")); UNIT_ASSERT_VALUES_EQUAL("https://ya.ru/zzz", CutHttpPrefix("https://ya.ru/zzz", true)); UNIT_ASSERT_VALUES_EQUAL("", CutHttpPrefix("https://")); // is that right? UNIT_ASSERT_VALUES_EQUAL("https://", CutHttpPrefix("https://", true)); // is that right? - } - + } + Y_UNIT_TEST(TestMisc) { - UNIT_ASSERT_VALUES_EQUAL("", CutWWWPrefix("www.")); - UNIT_ASSERT_VALUES_EQUAL("", CutWWWPrefix("WwW.")); - UNIT_ASSERT_VALUES_EQUAL("www", CutWWWPrefix("www")); - UNIT_ASSERT_VALUES_EQUAL("ya.ru", CutWWWPrefix("www.ya.ru")); + UNIT_ASSERT_VALUES_EQUAL("", CutWWWPrefix("www.")); + UNIT_ASSERT_VALUES_EQUAL("", CutWWWPrefix("WwW.")); + UNIT_ASSERT_VALUES_EQUAL("www", CutWWWPrefix("www")); + UNIT_ASSERT_VALUES_EQUAL("ya.ru", CutWWWPrefix("www.ya.ru")); UNIT_ASSERT_VALUES_EQUAL("", CutWWWNumberedPrefix("www.")); UNIT_ASSERT_VALUES_EQUAL("www", CutWWWNumberedPrefix("www")); @@ -125,7 +125,7 @@ Y_UNIT_TEST_SUITE(TUtilUrlTest) { UNIT_ASSERT_VALUES_EQUAL("", CutMPrefix("M.")); UNIT_ASSERT_VALUES_EQUAL("m", CutMPrefix("m")); UNIT_ASSERT_VALUES_EQUAL("ya.ru", CutMPrefix("m.ya.ru")); - } + } Y_UNIT_TEST(TestSplitUrlToHostAndPath) { TStringBuf host, path; diff --git a/library/cpp/testing/unittest/utmain.cpp b/library/cpp/testing/unittest/utmain.cpp index a29773e5a0..305bc6b40f 100644 --- a/library/cpp/testing/unittest/utmain.cpp +++ b/library/cpp/testing/unittest/utmain.cpp @@ -577,13 +577,13 @@ public: SetConsoleOutputCP(CP_UTF8); _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); - - if (!IsDebuggerPresent()) { - _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); - _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); - _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); - _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR); - } + + if (!IsDebuggerPresent()) { + _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); + _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); + _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); + _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR); + } } ~TWinEnvironment() { if (!IsDebuggerPresent()) { diff --git a/library/cpp/yson/node/node.h b/library/cpp/yson/node/node.h index 63229b5934..5f90f95df0 100644 --- a/library/cpp/yson/node/node.h +++ b/library/cpp/yson/node/node.h @@ -131,9 +131,9 @@ public: template<typename T> bool IsOfType() const noexcept; - // Int64, Uint64, Double, or Bool - bool IsArithmetic() const; - + // Int64, Uint64, Double, or Bool + bool IsArithmetic() const; + bool Empty() const; size_t Size() const; @@ -290,10 +290,10 @@ bool operator!=(const TNode& lhs, const TNode& rhs); bool GetBool(const TNode& node); -inline bool TNode::IsArithmetic() const { - return IsInt64() || IsUint64() || IsDouble() || IsBool(); -} - +inline bool TNode::IsArithmetic() const { + return IsInt64() || IsUint64() || IsDouble() || IsBool(); +} + template<typename T> inline T TNode::IntCast() const { if constexpr (std::is_integral<T>::value) { diff --git a/util/datetime/base.cpp b/util/datetime/base.cpp index 8238cc66a2..38ecc3ab96 100644 --- a/util/datetime/base.cpp +++ b/util/datetime/base.cpp @@ -287,9 +287,9 @@ TString DateToString(time_t when, long* sec) { TString 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; + char buf[16]; + sprintf(buf, "%04d", theTm.tm_year + 1900); + return buf; } TString YearToString(time_t when) { diff --git a/util/datetime/base_ut.cpp b/util/datetime/base_ut.cpp index d1b7f399f5..afc3f802eb 100644 --- a/util/datetime/base_ut.cpp +++ b/util/datetime/base_ut.cpp @@ -186,7 +186,7 @@ Y_UNIT_TEST_SUITE(TDateTimeTest) { << a.tm_mon << ", " << a.tm_year << ", " << a.tm_wday << ", " -#if !defined(_musl_) && !defined(_win_) +#if !defined(_musl_) && !defined(_win_) << a.tm_yday #endif << ")"; diff --git a/util/datetime/cputimer.h b/util/datetime/cputimer.h index c6559c8de2..7d38d5bdb3 100644 --- a/util/datetime/cputimer.h +++ b/util/datetime/cputimer.h @@ -34,25 +34,25 @@ public: class TProfileTimer { TDuration T; -public: +public: TProfileTimer() { - Reset(); - } + Reset(); + } TDuration Get() const { return TRusage::Get().Utime - T; - } + } TDuration Step() { TRusage r; r.Fill(); TDuration d = r.Utime - T; T = r.Utime; - return d; - } + return d; + } void Reset() { T = TRusage::Get().Utime; - } -}; - + } +}; + /// Return cached processor cycle count per second. Method takes 1 second at first invocation. /// Note, on older systems cycle rate may change during program lifetime, /// so returned value may be incorrect. Modern Intel and AMD processors keep constant TSC rate. @@ -115,7 +115,7 @@ private: time_t Begin; ui64 BeginCycles; -public: +public: TTimeLogger(const TString& message, bool verbose = true); ~TTimeLogger(); diff --git a/util/datetime/strptime.cpp b/util/datetime/strptime.cpp index 2e999879a1..f0d4ec333e 100644 --- a/util/datetime/strptime.cpp +++ b/util/datetime/strptime.cpp @@ -1,83 +1,83 @@ -// ATTN! this is port of FreeBSD LIBC code to Win32 - just for convenience. -// Locale is ignored!!! - -/* - * Powerdog Industries kindly requests feedback from anyone modifying - * this function: - * +// ATTN! this is port of FreeBSD LIBC code to Win32 - just for convenience. +// Locale is ignored!!! + +/* + * Powerdog Industries kindly requests feedback from anyone modifying + * this function: + * * Date: Thu, 05 Jun 1997 23:17:17 -0400 - * From: Kevin Ruddy <kevin.ruddy@powerdog.com> - * To: James FitzGibbon <james@nexis.net> - * Subject: Re: Use of your strptime(3) code (fwd) + * From: Kevin Ruddy <kevin.ruddy@powerdog.com> + * To: James FitzGibbon <james@nexis.net> + * Subject: Re: Use of your strptime(3) code (fwd) * - * The reason for the "no mod" clause was so that modifications would + * The reason for the "no mod" clause was so that modifications would * come back and we could integrate them and reissue so that a wider * audience could use it (thereby spreading the wealth). This has - * made it possible to get strptime to work on many operating systems. - * I'm not sure why that's "plain unacceptable" to the FreeBSD team. + * made it possible to get strptime to work on many operating systems. + * I'm not sure why that's "plain unacceptable" to the FreeBSD team. * - * Anyway, you can change it to "with or without modification" as + * Anyway, you can change it to "with or without modification" as * you see fit. Enjoy. * - * Kevin Ruddy - * Powerdog Industries, Inc. - */ -/* - * Copyright (c) 1994 Powerdog Industries. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * 3. All advertising materials mentioning features or use of this - * software must display the following acknowledgement: - * This product includes software developed by Powerdog Industries. - * 4. The name of Powerdog Industries may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY POWERDOG INDUSTRIES ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE POWERDOG INDUSTRIES BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - + * Kevin Ruddy + * Powerdog Industries, Inc. + */ +/* + * Copyright (c) 1994 Powerdog Industries. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgement: + * This product includes software developed by Powerdog Industries. + * 4. The name of Powerdog Industries may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY POWERDOG INDUSTRIES ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE POWERDOG INDUSTRIES BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + #include <util/system/compat.h> #include "systime.h" -#ifdef _win32_ +#ifdef _win32_ #ifndef lint #ifndef NOID -static char copyright[] = +static char copyright[] = "@(#) Copyright (c) 1994 Powerdog Industries. All rights reserved."; static char sccsid[] = "@(#)strptime.c 0.1 (Powerdog) 94/03/27"; #endif /* !defined NOID */ #endif /* not lint */ //__FBSDID("$FreeBSD: src/lib/libc/stdtime/strptime.c,v 1.35 2003/11/17 04:19:15 nectar Exp $"); - + //#include "namespace.h" #include <time.h> #include <ctype.h> #include <errno.h> #include <stdlib.h> #include <string.h> -//#include <pthread.h> -//#include "un-namespace.h" -//#include "libc_private.h" - -// ******************* #include "timelocal.h" ********************* -struct lc_time_T { +//#include <pthread.h> +//#include "un-namespace.h" +//#include "libc_private.h" + +// ******************* #include "timelocal.h" ********************* +struct lc_time_T { const char* mon[12]; const char* month[12]; const char* wday[7]; @@ -91,14 +91,14 @@ struct lc_time_T { const char* alt_month[12]; const char* md_order; const char* ampm_fmt; -}; - -// ******************* timelocal.c ****************** -/*- - * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org> - * Copyright (c) 1997 FreeBSD Inc. - * All rights reserved. -*/ +}; + +// ******************* timelocal.c ****************** +/*- + * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org> + * Copyright (c) 1997 FreeBSD Inc. + * All rights reserved. +*/ static const struct lc_time_T _C_time_locale = { {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}, @@ -108,85 +108,85 @@ static const struct lc_time_T _C_time_locale = { "Thu", "Fri", "Sat"}, {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}, - + /* X_fmt */ "%H:%M:%S", - + /* - * x_fmt - * Since the C language standard calls for - * "date, using locale's date format," anything goes. - * Using just numbers (as here) makes Quakers happier; - * it's also compatible with SVR4. - */ + * x_fmt + * Since the C language standard calls for + * "date, using locale's date format," anything goes. + * Using just numbers (as here) makes Quakers happier; + * it's also compatible with SVR4. + */ "%m/%d/%y", - + /* - * c_fmt - */ + * c_fmt + */ "%a %b %e %H:%M:%S %Y", - + /* am */ "AM", - + /* pm */ "PM", - + /* date_fmt */ "%a %b %e %H:%M:%S %Z %Y", - + /* alt_month - * Standalone months forms for %OB - */ + * Standalone months forms for %OB + */ { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}, - + /* md_order - * Month / day order in dates - */ + * Month / day order in dates + */ "md", - + /* ampm_fmt - * To determine 12-hour clock format time (empty, if N/A) - */ + * To determine 12-hour clock format time (empty, if N/A) + */ "%I:%M:%S %p"}; - + struct lc_time_T* -__get_current_time_locale(void) -{ +__get_current_time_locale(void) +{ return /*(_time_using_locale - ? &_time_locale + ? &_time_locale :*/ (struct lc_time_T*)&_C_time_locale /*)*/; -} - -// ******************* strptime.c ******************* +} + +// ******************* strptime.c ******************* static char* _strptime(const char*, const char*, struct tm*, int*); - + #define asizeof(a) (sizeof(a) / sizeof((a)[0])) - + #if defined(_MSC_VER) && (_MSC_VER >= 1900) #define tzname _tzname #endif static char* _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) -{ +{ char c; const char* ptr; int i; size_t len = 0; int Ealternative, Oalternative; struct lc_time_T* tptr = __get_current_time_locale(); - + ptr = fmt; while (*ptr != 0) { if (*buf == 0) break; - + c = *ptr++; - + if (c != '%') { if (isspace((unsigned char)c)) while (*buf != 0 && isspace((unsigned char)*buf)) @@ -195,7 +195,7 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) return 0; continue; } - + Ealternative = 0; Oalternative = 0; label: @@ -206,17 +206,17 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) if (*buf++ != '%') return 0; break; - + case '+': buf = _strptime(buf, tptr->date_fmt, tm, GMTp); if (buf == 0) return 0; break; - + case 'C': if (!isdigit((unsigned char)*buf)) return 0; - + /* XXX This will break for 3-digit centuries. */ len = 2; for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { @@ -226,74 +226,74 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) } if (i < 19) return 0; - + tm->tm_year = i * 100 - 1900; break; - + case 'c': buf = _strptime(buf, tptr->c_fmt, tm, GMTp); if (buf == 0) return 0; break; - + case 'D': buf = _strptime(buf, "%m/%d/%y", tm, GMTp); if (buf == 0) return 0; break; - + case 'E': if (Ealternative || Oalternative) break; ++Ealternative; goto label; - + case 'O': if (Ealternative || Oalternative) break; ++Oalternative; goto label; - + case 'F': buf = _strptime(buf, "%Y-%m-%d", tm, GMTp); if (buf == 0) return 0; break; - + case 'R': buf = _strptime(buf, "%H:%M", tm, GMTp); if (buf == 0) return 0; break; - + case 'r': buf = _strptime(buf, tptr->ampm_fmt, tm, GMTp); if (buf == 0) return 0; break; - + case 'T': buf = _strptime(buf, "%H:%M:%S", tm, GMTp); if (buf == 0) return 0; break; - + case 'X': buf = _strptime(buf, tptr->X_fmt, tm, GMTp); if (buf == 0) return 0; break; - + case 'x': buf = _strptime(buf, tptr->x_fmt, tm, GMTp); if (buf == 0) return 0; break; - + case 'j': if (!isdigit((unsigned char)*buf)) return 0; - + len = 3; for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { i *= 10; @@ -305,22 +305,22 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) tm->tm_yday = i - 1; break; - + case 'M': case 'S': if (*buf == 0 || isspace((unsigned char)*buf)) break; - + if (!isdigit((unsigned char)*buf)) return 0; - + len = 2; for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { i *= 10; i += *buf - '0'; --len; } - + if (c == 'M') { if (i > 59) return 0; @@ -350,7 +350,7 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) */ if (!isdigit((unsigned char)*buf)) return 0; - + len = 2; for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { i *= 10; @@ -362,14 +362,14 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) return 0; } else if (i > 12) return 0; - + tm->tm_hour = i; - + if (*buf != 0 && isspace((unsigned char)*buf)) while (*ptr != 0 && !isspace((unsigned char)*ptr)) ++ptr; break; - + case 'p': /* * XXX This is bogus if parsed before hour-related @@ -411,11 +411,11 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) } if (i == asizeof(tptr->weekday)) return 0; - + tm->tm_wday = i; buf += len; break; - + case 'U': case 'W': /* @@ -426,7 +426,7 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) */ if (!isdigit((unsigned char)*buf)) return 0; - + len = 2; for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { i *= 10; @@ -435,27 +435,27 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) } if (i > 53) return 0; - + if (*buf != 0 && isspace((unsigned char)*buf)) while (*ptr != 0 && !isspace((unsigned char)*ptr)) ++ptr; break; - + case 'w': if (!isdigit((unsigned char)*buf)) return 0; - + i = *buf - '0'; if (i > 6) return 0; - + tm->tm_wday = i; - + if (*buf != 0 && isspace((unsigned char)*buf)) while (*ptr != 0 && !isspace((unsigned char)*ptr)) ++ptr; break; - + case 'd': case 'e': /* @@ -468,7 +468,7 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) */ if (!isdigit((unsigned char)*buf)) return 0; - + len = 2; for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { i *= 10; @@ -477,14 +477,14 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) } if (i > 31) return 0; - + tm->tm_mday = i; - + if (*buf != 0 && isspace((unsigned char)*buf)) while (*ptr != 0 && !isspace((unsigned char)*ptr)) ++ptr; break; - + case 'B': case 'b': case 'h': @@ -510,15 +510,15 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) } if (i == asizeof(tptr->month)) return 0; - + tm->tm_mon = i; buf += len; break; - + case 'm': if (!isdigit((unsigned char)*buf)) return 0; - + len = 2; for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { i *= 10; @@ -527,20 +527,20 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) } if (i < 1 || i > 12) return 0; - + tm->tm_mon = i - 1; - + if (*buf != 0 && isspace((unsigned char)*buf)) while (*ptr != 0 && !isspace((unsigned char)*ptr)) ++ptr; break; - + case 's': { char* cp; int sverrno; long n; time_t t; - + sverrno = errno; errno = 0; n = strtol(buf, &cp, 10); @@ -553,15 +553,15 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) GmTimeR(&t, tm); *GMTp = 1; } break; - + case 'Y': case 'y': if (*buf == 0 || isspace((unsigned char)*buf)) break; - + if (!isdigit((unsigned char)*buf)) return 0; - + len = (c == 'Y') ? 4 : 2; for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { i *= 10; @@ -574,18 +574,18 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) i += 100; if (i < 0) return 0; - + tm->tm_year = i; - + if (*buf != 0 && isspace((unsigned char)*buf)) while (*ptr != 0 && !isspace((unsigned char)*ptr)) ++ptr; break; - + case 'Z': { const char* cp; char* zonestr; - + for (cp = buf; *cp && isupper((unsigned char)*cp); ++cp) { /*empty*/ } if (cp - buf) { @@ -608,20 +608,20 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) } } return (char*)buf; -} - +} + char* strptime(const char* buf, const char* fmt, struct tm* tm) -{ +{ char* ret; int gmt; - + gmt = 0; ret = _strptime(buf, fmt, tm, &gmt); if (ret && gmt) { time_t t = timegm(tm); localtime_r(&t, tm); } - + return (ret); -} -#endif //_win32_ +} +#endif //_win32_ diff --git a/util/datetime/systime.cpp b/util/datetime/systime.cpp index 1f3b1f5d29..6ee7e8fc6e 100644 --- a/util/datetime/systime.cpp +++ b/util/datetime/systime.cpp @@ -6,7 +6,7 @@ #ifdef _win_ void FileTimeToTimeval(const FILETIME* ft, timeval* tv) { - const i64 NANOINTERVAL = LL(116444736000000000); // Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 + const i64 NANOINTERVAL = LL(116444736000000000); // Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 union { ui64 ft_scalar; FILETIME ft_struct; @@ -38,14 +38,14 @@ tm* gmtime_r(const time_t* clock, tm* result) { } char* ctime_r(const time_t* clock, char* buf) { - char* res = ctime(clock); - if (res) { - memcpy(buf, res, 26); - return buf; - } - return 0; -} - + char* res = ctime(clock); + if (res) { + memcpy(buf, res, 26); + return buf; + } + return 0; +} + #endif /* _win_ */ #define YEAR0 1900 diff --git a/util/datetime/systime.h b/util/datetime/systime.h index 937518b2ac..491d36e802 100644 --- a/util/datetime/systime.h +++ b/util/datetime/systime.h @@ -24,7 +24,7 @@ int gettimeofday(struct timeval* tp, void*); tm* localtime_r(const time_t* clock, tm* result); tm* gmtime_r(const time_t* clock, tm* result); char* ctime_r(const time_t* clock, char* buf); - + inline time_t timegm(struct tm* t) { return TimeGM(t); } diff --git a/util/folder/dirut.h b/util/folder/dirut.h index 67525acb45..2537027b12 100644 --- a/util/folder/dirut.h +++ b/util/folder/dirut.h @@ -23,7 +23,7 @@ extern "C" int mkstemps(char* path, int slen); char* mkdtemp(char* path); -#else +#else #ifdef _sun_ #include <alloca.h> @@ -55,7 +55,7 @@ void MakePathIfNotExist(const char* path, int mode = 0777); void SlashFolderLocal(TString& folder); bool correctpath(TString& filename); bool resolvepath(TString& folder, const TString& home); - + char GetDirectorySeparator(); const char* GetDirectorySeparatorS(); diff --git a/util/generic/bitmap.h b/util/generic/bitmap.h index 1c9c2c41b1..f77d182460 100644 --- a/util/generic/bitmap.h +++ b/util/generic/bitmap.h @@ -1,5 +1,5 @@ #pragma once - + #include "fwd.h" #include "ptr.h" #include "bitops.h" diff --git a/util/generic/hash.h b/util/generic/hash.h index ed183431bd..e46db21fa9 100644 --- a/util/generic/hash.h +++ b/util/generic/hash.h @@ -2,7 +2,7 @@ #include "fwd.h" #include "mapfindptr.h" - + #include <util/memory/alloc.h> #include <util/system/type_name.h> #include <util/system/yassert.h> @@ -28,7 +28,7 @@ struct TSelect1st { } }; -template <class Value> +template <class Value> struct __yhashtable_node { /** If the first bit is not set, then this is a pointer to the next node in * the list of nodes for the current bucket. Otherwise this is a pointer of @@ -42,37 +42,37 @@ struct __yhashtable_node { Value val; __yhashtable_node& operator=(const __yhashtable_node&) = delete; -}; - -template <class Value, class Key, class HashFcn, - class ExtractKey, class EqualKey, class Alloc> +}; + +template <class Value, class Key, class HashFcn, + class ExtractKey, class EqualKey, class Alloc> class THashTable; - + template <class Key, class T, class HashFcn, class EqualKey, typename size_type_f> class sthash; -template <class Value> +template <class Value> struct __yhashtable_iterator; - -template <class Value> + +template <class Value> struct __yhashtable_const_iterator; - -template <class Value> + +template <class Value> struct __yhashtable_iterator { using iterator = __yhashtable_iterator<Value>; using const_iterator = __yhashtable_const_iterator<Value>; using node = __yhashtable_node<Value>; - + using iterator_category = std::forward_iterator_tag; using value_type = Value; using difference_type = ptrdiff_t; using size_type = size_t; using reference = Value&; using pointer = Value*; - + node* cur; - + explicit __yhashtable_iterator(node* n) : cur(n) { @@ -99,23 +99,23 @@ struct __yhashtable_iterator { Y_FORCE_INLINE explicit operator bool() const noexcept { return cur != nullptr; } -}; - -template <class Value> +}; + +template <class Value> struct __yhashtable_const_iterator { using iterator = __yhashtable_iterator<Value>; using const_iterator = __yhashtable_const_iterator<Value>; using node = __yhashtable_node<Value>; - + using iterator_category = std::forward_iterator_tag; using value_type = Value; using difference_type = ptrdiff_t; using size_type = size_t; using reference = const Value&; using pointer = const Value*; - + const node* cur; - + explicit __yhashtable_const_iterator(const node* n) : cur(n) { @@ -146,8 +146,8 @@ struct __yhashtable_const_iterator { Y_FORCE_INLINE explicit operator bool() const noexcept { return cur != nullptr; } -}; - +}; + /** * This class saves some space in allocator-based containers for the most common * use case of empty allocators. This is achieved thanks to the application of @@ -447,7 +447,7 @@ struct _yhashtable_traits { }; extern const void* const _yhashtable_empty_data[]; - + template <class Value, class Key, class HashFcn, class ExtractKey, class EqualKey, class Alloc> class THashTable: private _yhashtable_traits<Value, Key, HashFcn, ExtractKey, EqualKey, Alloc>::base_type { using traits_type = _yhashtable_traits<Value, Key, HashFcn, ExtractKey, EqualKey, Alloc>; @@ -457,7 +457,7 @@ class THashTable: private _yhashtable_traits<Value, Key, HashFcn, ExtractKey, Eq using buckets_type = _yhashtable_buckets<node*, nodep_allocator_type>; using TBucketDivisor = ::NPrivate::THashDivisor; -public: +public: using key_type = Key; using value_type = Value; using hasher = HashFcn; @@ -465,14 +465,14 @@ public: using key_extract = ExtractKey; using allocator_type = Alloc; using node_allocator_type = typename traits_type::node_allocator_type; - + using size_type = size_t; using difference_type = ptrdiff_t; using pointer = value_type*; using const_pointer = const value_type*; using reference = value_type&; using const_reference = const value_type&; - + node_allocator_type& GetNodeAllocator() { return this->_get_alloc(); } @@ -485,13 +485,13 @@ public: hasher hash_function() const { return this->_get_hash_fun(); } - -private: + +private: template <class KeyL, class KeyR> bool equals(const KeyL& l, const KeyR& r) const { return this->_get_key_eq()(l, r); } - + /* This method is templated to postpone instantiation of key extraction functor. */ template <class ValueL> auto get_key(const ValueL& value) const -> decltype(ExtractKey()(value)) { @@ -509,16 +509,16 @@ private: buckets_type buckets; size_type num_elements; - -public: + +public: using iterator = __yhashtable_iterator<Value>; using const_iterator = __yhashtable_const_iterator<Value>; using insert_ctx = node**; - + friend struct __yhashtable_iterator<Value>; friend struct __yhashtable_const_iterator<Value>; - -public: + +public: THashTable() : base_type(HashFcn(), ExtractKey(), EqualKey(), node_allocator_type()) , buckets(nodep_allocator_type()) @@ -534,7 +534,7 @@ public: { initialize_buckets(buckets, n); } - + THashTable(size_type n, const HashFcn& hf, const EqualKey& eql) : base_type(hf, ExtractKey(), eql, node_allocator_type()) , buckets(nodep_allocator_type()) @@ -542,7 +542,7 @@ public: { initialize_buckets(buckets, n); } - + template <class TAllocParam> THashTable(size_type n, const HashFcn& hf, const EqualKey& eql, TAllocParam* allocParam) : base_type(hf, ExtractKey(), eql, allocParam) @@ -551,7 +551,7 @@ public: { initialize_buckets(buckets, n); } - + THashTable(const THashTable& ht) : base_type(ht._get_hash_fun(), ht._get_key_extract(), ht._get_key_eq(), ht._get_alloc()) , buckets(ht.buckets.get_allocator()) @@ -572,8 +572,8 @@ public: { initialize_buckets(buckets, 0); this->swap(ht); - } - + } + THashTable& operator=(const THashTable& ht) { if (&ht != this) { basic_clear(); @@ -609,12 +609,12 @@ public: return *this; } - + ~THashTable() { basic_clear(); deinitialize_buckets(buckets); } - + size_type size() const noexcept { return num_elements; } @@ -625,24 +625,24 @@ public: Y_PURE_FUNCTION bool empty() const noexcept { return size() == 0; } - + void swap(THashTable& ht) { base_type::swap(ht); buckets.swap(ht.buckets); DoSwap(num_elements, ht.num_elements); } - + iterator begin() { for (size_type n = 0; n < buckets.size(); ++n) /*y*/ if (buckets[n]) return iterator(buckets[n]); /*y*/ return end(); } - + iterator end() { return iterator(nullptr); } /*y*/ - + const_iterator begin() const { for (size_type n = 0; n < buckets.size(); ++n) /*y*/ if (buckets[n]) @@ -654,11 +654,11 @@ public: return const_iterator(nullptr); } /*y*/ -public: +public: size_type bucket_count() const { return buckets.size(); } /*y*/ - + size_type bucket_size(size_type bucket) const { size_type result = 0; if (const node* cur = buckets[bucket]) @@ -666,19 +666,19 @@ public: result += 1; return result; } - + template <class OtherValue> std::pair<iterator, bool> insert_unique(const OtherValue& obj) { reserve(num_elements + 1); return insert_unique_noresize(obj); } - + template <class OtherValue> iterator insert_equal(const OtherValue& obj) { reserve(num_elements + 1); return emplace_equal_noresize(obj); } - + template <typename... Args> iterator emplace_equal(Args&&... args) { reserve(num_elements + 1); @@ -702,7 +702,7 @@ public: ++num_elements; return iterator(tmp); } - + template <typename... Args> std::pair<iterator, bool> emplace_unique(Args&&... args) { reserve(num_elements + 1); @@ -717,29 +717,29 @@ public: template <typename... Args> iterator emplace_equal_noresize(Args&&... args); - + template <class InputIterator> void insert_unique(InputIterator f, InputIterator l) { insert_unique(f, l, typename std::iterator_traits<InputIterator>::iterator_category()); } - + template <class InputIterator> void insert_equal(InputIterator f, InputIterator l) { insert_equal(f, l, typename std::iterator_traits<InputIterator>::iterator_category()); } - + template <class InputIterator> void insert_unique(InputIterator f, InputIterator l, std::input_iterator_tag) { for (; f != l; ++f) insert_unique(*f); } - + template <class InputIterator> void insert_equal(InputIterator f, InputIterator l, std::input_iterator_tag) { for (; f != l; ++f) insert_equal(*f); } - + template <class ForwardIterator> void insert_unique(ForwardIterator f, ForwardIterator l, std::forward_iterator_tag) { difference_type n = std::distance(f, l); @@ -748,7 +748,7 @@ public: for (; n > 0; --n, ++f) insert_unique_noresize(*f); } - + template <class ForwardIterator> void insert_equal(ForwardIterator f, ForwardIterator l, std::forward_iterator_tag) { difference_type n = std::distance(f, l); @@ -757,10 +757,10 @@ public: for (; n > 0; --n, ++f) emplace_equal_noresize(*f); } - + template <class OtherValue> reference find_or_insert(const OtherValue& v); - + template <class OtherKey> iterator find(const OtherKey& key) { size_type n = bkt_num_key(key); @@ -784,25 +784,25 @@ public: } return const_iterator(first); /*y*/ } - + template <class OtherKey> iterator find_i(const OtherKey& key, insert_ctx& ins); - + template <class OtherKey> size_type count(const OtherKey& key) const { const size_type n = bkt_num_key(key); size_type result = 0; - + if (const node* cur = buckets[n]) for (; !((uintptr_t)cur & 1); cur = cur->next) if (equals(get_key(cur->val), key)) ++result; return result; } - + template <class OtherKey> std::pair<iterator, iterator> equal_range(const OtherKey& key); - + template <class OtherKey> std::pair<const_iterator, const_iterator> equal_range(const OtherKey& key) const; @@ -815,13 +815,13 @@ public: // void (instead of iterator) is intended, see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2023.pdf void erase(const iterator& it); void erase(iterator first, iterator last); - + void erase(const const_iterator& it); void erase(const_iterator first, const_iterator last); - + bool reserve(size_type num_elements_hint); void basic_clear(); - + /** * Clears the hashtable without deallocating the nodes. * @@ -840,10 +840,10 @@ public: // implemented in save_stl.h template <class KeySaver> int save_for_st(IOutputStream* stream, KeySaver& ks, sthash<int, int, THash<int>, TEqualTo<int>, typename KeySaver::TSizeType>* stHash = nullptr) const; - + void clear(size_type downsize) { basic_clear(); - + if (downsize < buckets.size()) { const TBucketDivisor newSize = HashBucketCountExt(downsize); if (newSize() < buckets.size()) { @@ -875,9 +875,9 @@ public: void clear() { if (num_elements) clear((num_elements * 2 + buckets.size()) / 3); - } - -private: + } + +private: static void initialize_buckets(buckets_type& buckets, size_type sizeHint) { if (sizeHint == 0) { buckets.initialize_static(reinterpret_cast<node**>(const_cast<void**>(_yhashtable_empty_data)) + 1, TBucketDivisor::One()); @@ -888,7 +888,7 @@ private: initialize_buckets_dynamic(buckets, size); } } - + static void initialize_buckets_dynamic(buckets_type& buckets, TBucketDivisor size) { buckets.initialize_dynamic(size); memset(buckets.data(), 0, size() * sizeof(*buckets.data())); @@ -911,24 +911,24 @@ private: size_type bkt_num_key(const OtherKey& key) const { return bkt_num_key(key, buckets.ExtSize()); } - + template <class OtherValue> size_type bkt_num(const OtherValue& obj) const { return bkt_num_key(get_key(obj)); } - + template <class OtherKey> size_type bkt_num_key(const OtherKey& key, TBucketDivisor n) const { const size_type bucket = n.Remainder(this->_get_hash_fun()(key)); Y_ASSERT((0 <= bucket) && (bucket < n())); return bucket; } - + template <class OtherValue> size_type bkt_num(const OtherValue& obj, TBucketDivisor n) const { return bkt_num_key(get_key(obj), n); } - + template <typename... Args> node* new_node(Args&&... val) { node* n = get_node(); @@ -947,56 +947,56 @@ private: //n->next = (node*) 0xDeadBeeful; put_node(n); } - + void erase_bucket(const size_type n, node* first, node* last); void erase_bucket(const size_type n, node* last); - + void copy_from_dynamic(const THashTable& ht); -}; - -template <class V> +}; + +template <class V> __yhashtable_iterator<V>& __yhashtable_iterator<V>::operator++() { Y_ASSERT(cur); - cur = cur->next; + cur = cur->next; if ((uintptr_t)cur & 1) { node** bucket = (node**)((uintptr_t)cur & ~1); while (*bucket == nullptr) ++bucket; Y_ASSERT(*bucket != nullptr); cur = (node*)((uintptr_t)*bucket & ~1); - } - return *this; -} - -template <class V> + } + return *this; +} + +template <class V> inline __yhashtable_iterator<V> __yhashtable_iterator<V>::operator++(int) { iterator tmp = *this; ++*this; return tmp; -} - -template <class V> +} + +template <class V> __yhashtable_const_iterator<V>& __yhashtable_const_iterator<V>::operator++() { Y_ASSERT(cur); - cur = cur->next; + cur = cur->next; if ((uintptr_t)cur & 1) { node** bucket = (node**)((uintptr_t)cur & ~1); while (*bucket == nullptr) ++bucket; Y_ASSERT(*bucket != nullptr); cur = (node*)((uintptr_t)*bucket & ~1); - } - return *this; -} - -template <class V> + } + return *this; +} + +template <class V> inline __yhashtable_const_iterator<V> __yhashtable_const_iterator<V>::operator++(int) { const_iterator tmp = *this; ++*this; return tmp; -} - -template <class V, class K, class HF, class Ex, class Eq, class A> +} + +template <class V, class K, class HF, class Ex, class Eq, class A> template <typename... Args> std::pair<typename THashTable<V, K, HF, Ex, Eq, A>::iterator, bool> THashTable<V, K, HF, Ex, Eq, A>::emplace_unique_noresize(Args&&... args) { auto deleter = [&](node* tmp) { delete_node(tmp); }; @@ -1023,20 +1023,20 @@ template <class OtherValue> std::pair<typename THashTable<V, K, HF, Ex, Eq, A>::iterator, bool> THashTable<V, K, HF, Ex, Eq, A>::insert_unique_noresize(const OtherValue& obj) { const size_type n = bkt_num(obj); node* first = buckets[n]; - + if (first) /*y*/ for (node* cur = first; !((uintptr_t)cur & 1); cur = cur->next) /*y*/ if (equals(get_key(cur->val), get_key(obj))) return std::pair<iterator, bool>(iterator(cur), false); /*y*/ - + node* tmp = new_node(obj); tmp->next = first ? first : (node*)((uintptr_t)&buckets[n + 1] | 1); /*y*/ buckets[n] = tmp; ++num_elements; return std::pair<iterator, bool>(iterator(tmp), true); /*y*/ -} - -template <class V, class K, class HF, class Ex, class Eq, class A> +} + +template <class V, class K, class HF, class Ex, class Eq, class A> template <typename... Args> __yhashtable_iterator<V> THashTable<V, K, HF, Ex, Eq, A>::emplace_equal_noresize(Args&&... args) { auto deleter = [&](node* tmp) { delete_node(tmp); }; @@ -1044,7 +1044,7 @@ __yhashtable_iterator<V> THashTable<V, K, HF, Ex, Eq, A>::emplace_equal_noresize std::unique_ptr<node, decltype(deleter)> guard(tmp, deleter); const size_type n = bkt_num(tmp->val); node* first = buckets[n]; - + if (first) /*y*/ for (node* cur = first; !((uintptr_t)cur & 1); cur = cur->next) /*y*/ if (equals(get_key(cur->val), get_key(tmp->val))) { @@ -1054,55 +1054,55 @@ __yhashtable_iterator<V> THashTable<V, K, HF, Ex, Eq, A>::emplace_equal_noresize ++num_elements; return iterator(tmp); /*y*/ } - + guard.release(); tmp->next = first ? first : (node*)((uintptr_t)&buckets[n + 1] | 1); /*y*/ buckets[n] = tmp; ++num_elements; return iterator(tmp); /*y*/ -} - -template <class V, class K, class HF, class Ex, class Eq, class A> +} + +template <class V, class K, class HF, class Ex, class Eq, class A> template <class OtherValue> typename THashTable<V, K, HF, Ex, Eq, A>::reference THashTable<V, K, HF, Ex, Eq, A>::find_or_insert(const OtherValue& v) { reserve(num_elements + 1); - + size_type n = bkt_num_key(get_key(v)); node* first = buckets[n]; - + if (first) /*y*/ for (node* cur = first; !((uintptr_t)cur & 1); cur = cur->next) /*y*/ if (equals(get_key(cur->val), get_key(v))) return cur->val; - + node* tmp = new_node(v); tmp->next = first ? first : (node*)((uintptr_t)&buckets[n + 1] | 1); /*y*/ buckets[n] = tmp; ++num_elements; return tmp->val; -} - -template <class V, class K, class HF, class Ex, class Eq, class A> +} + +template <class V, class K, class HF, class Ex, class Eq, class A> template <class OtherKey> __yhashtable_iterator<V> THashTable<V, K, HF, Ex, Eq, A>::find_i(const OtherKey& key, insert_ctx& ins) { size_type n = bkt_num_key(key); ins = &buckets[n]; node* first = buckets[n]; - + if (first) /*y*/ for (node* cur = first; !((uintptr_t)cur & 1); cur = cur->next) /*y*/ if (equals(get_key(cur->val), key)) return iterator(cur); /*y*/ return end(); -} - -template <class V, class K, class HF, class Ex, class Eq, class A> +} + +template <class V, class K, class HF, class Ex, class Eq, class A> template <class OtherKey> std::pair<__yhashtable_iterator<V>, __yhashtable_iterator<V>> THashTable<V, K, HF, Ex, Eq, A>::equal_range(const OtherKey& key) { using pii = std::pair<iterator, iterator>; const size_type n = bkt_num_key(key); node* first = buckets[n]; - + if (first) /*y*/ for (; !((uintptr_t)first & 1); first = first->next) { /*y*/ if (equals(get_key(first->val), key)) { @@ -1117,15 +1117,15 @@ std::pair<__yhashtable_iterator<V>, __yhashtable_iterator<V>> THashTable<V, K, H } } return pii(end(), end()); -} - -template <class V, class K, class HF, class Ex, class Eq, class A> +} + +template <class V, class K, class HF, class Ex, class Eq, class A> template <class OtherKey> std::pair<__yhashtable_const_iterator<V>, __yhashtable_const_iterator<V>> THashTable<V, K, HF, Ex, Eq, A>::equal_range(const OtherKey& key) const { using pii = std::pair<const_iterator, const_iterator>; const size_type n = bkt_num_key(key); const node* first = buckets[n]; - + if (first) /*y*/ for (; !((uintptr_t)first & 1); first = first->next) { /*y*/ if (equals(get_key(first->val), key)) { @@ -1141,15 +1141,15 @@ std::pair<__yhashtable_const_iterator<V>, __yhashtable_const_iterator<V>> THashT } } return pii(end(), end()); -} - -template <class V, class K, class HF, class Ex, class Eq, class A> +} + +template <class V, class K, class HF, class Ex, class Eq, class A> template <class OtherKey> typename THashTable<V, K, HF, Ex, Eq, A>::size_type THashTable<V, K, HF, Ex, Eq, A>::erase(const OtherKey& key) { const size_type n = bkt_num_key(key); node* first = buckets[n]; size_type erased = 0; - + if (first) { node* cur = first; node* next = cur->next; @@ -1171,11 +1171,11 @@ typename THashTable<V, K, HF, Ex, Eq, A>::size_type THashTable<V, K, HF, Ex, Eq, --num_elements; delete_node(first); } - } + } return erased; -} - -template <class V, class K, class HF, class Ex, class Eq, class A> +} + +template <class V, class K, class HF, class Ex, class Eq, class A> template <class OtherKey> typename THashTable<V, K, HF, Ex, Eq, A>::size_type THashTable<V, K, HF, Ex, Eq, A>::erase_one(const OtherKey& key) { const size_type n = bkt_num_key(key); @@ -1210,7 +1210,7 @@ void THashTable<V, K, HF, Ex, Eq, A>::erase(const iterator& it) { if (node* const p = it.cur) { const size_type n = bkt_num(p->val); node* cur = buckets[n]; - + if (cur == p) { buckets[n] = ((uintptr_t)cur->next & 1) ? nullptr : cur->next; /*y*/ delete_node(cur); @@ -1228,15 +1228,15 @@ void THashTable<V, K, HF, Ex, Eq, A>::erase(const iterator& it) { next = cur->next; } } - } - } -} - -template <class V, class K, class HF, class Ex, class Eq, class A> + } + } +} + +template <class V, class K, class HF, class Ex, class Eq, class A> void THashTable<V, K, HF, Ex, Eq, A>::erase(iterator first, iterator last) { size_type f_bucket = first.cur ? bkt_num(first.cur->val) : buckets.size(); /*y*/ size_type l_bucket = last.cur ? bkt_num(last.cur->val) : buckets.size(); /*y*/ - + if (first.cur == last.cur) return; else if (f_bucket == l_bucket) @@ -1248,20 +1248,20 @@ void THashTable<V, K, HF, Ex, Eq, A>::erase(iterator first, iterator last) { if (l_bucket != buckets.size()) /*y*/ erase_bucket(l_bucket, last.cur); } -} - -template <class V, class K, class HF, class Ex, class Eq, class A> -inline void +} + +template <class V, class K, class HF, class Ex, class Eq, class A> +inline void THashTable<V, K, HF, Ex, Eq, A>::erase(const_iterator first, const_iterator last) { erase(iterator(const_cast<node*>(first.cur)), iterator(const_cast<node*>(last.cur))); -} - -template <class V, class K, class HF, class Ex, class Eq, class A> +} + +template <class V, class K, class HF, class Ex, class Eq, class A> inline void THashTable<V, K, HF, Ex, Eq, A>::erase(const const_iterator& it) { erase(iterator(const_cast<node*>(it.cur))); -} - -template <class V, class K, class HF, class Ex, class Eq, class A> +} + +template <class V, class K, class HF, class Ex, class Eq, class A> bool THashTable<V, K, HF, Ex, Eq, A>::reserve(size_type num_elements_hint) { const size_type old_n = buckets.size(); /*y*/ if (num_elements_hint + 1 > old_n) { @@ -1304,13 +1304,13 @@ bool THashTable<V, K, HF, Ex, Eq, A>::reserve(size_type num_elements_hint) { throw; } #endif /* __STL_USE_EXCEPTIONS */ - } - } + } + } return false; -} - -template <class V, class K, class HF, class Ex, class Eq, class A> +} + +template <class V, class K, class HF, class Ex, class Eq, class A> void THashTable<V, K, HF, Ex, Eq, A>::erase_bucket(const size_type n, node* first, node* last) { node* cur = buckets[n]; if (cur == first) @@ -1325,10 +1325,10 @@ void THashTable<V, K, HF, Ex, Eq, A>::erase_bucket(const size_type n, node* firs next = ((uintptr_t)cur->next & 1) ? nullptr : cur->next; /*y*/ --num_elements; } - } -} - -template <class V, class K, class HF, class Ex, class Eq, class A> + } +} + +template <class V, class K, class HF, class Ex, class Eq, class A> void THashTable<V, K, HF, Ex, Eq, A>::erase_bucket(const size_type n, node* last) { node* cur = buckets[n]; while (cur != last) { @@ -1338,9 +1338,9 @@ void THashTable<V, K, HF, Ex, Eq, A>::erase_bucket(const size_type n, node* last buckets[n] = cur; --num_elements; } -} - -template <class V, class K, class HF, class Ex, class Eq, class A> +} + +template <class V, class K, class HF, class Ex, class Eq, class A> void THashTable<V, K, HF, Ex, Eq, A>::basic_clear() { if (!num_elements) { return; @@ -1360,9 +1360,9 @@ void THashTable<V, K, HF, Ex, Eq, A>::basic_clear() { } } num_elements = 0; -} - -template <class V, class K, class HF, class Ex, class Eq, class A> +} + +template <class V, class K, class HF, class Ex, class Eq, class A> void THashTable<V, K, HF, Ex, Eq, A>::copy_from_dynamic(const THashTable& ht) { Y_ASSERT(buckets.size() == ht.buckets.size() && !ht.empty()); @@ -1373,23 +1373,23 @@ void THashTable<V, K, HF, Ex, Eq, A>::copy_from_dynamic(const THashTable& ht) { if (const node* cur = ht.buckets[i]) { node* copy = new_node(cur->val); buckets[i] = copy; - + for (node* next = cur->next; !((uintptr_t)next & 1); cur = next, next = cur->next) { copy->next = new_node(next->val); copy = copy->next; } copy->next = (node*)((uintptr_t)&buckets[i + 1] | 1); /*y*/ } - } + } num_elements = ht.num_elements; #ifdef __STL_USE_EXCEPTIONS } catch (...) { basic_clear(); throw; - } + } #endif /* __STL_USE_EXCEPTIONS */ -} - +} + namespace NPrivate { template <class Key> inline TString MapKeyToString(const Key&) { @@ -1423,11 +1423,11 @@ namespace NPrivate { template <class Key, class T, class HashFcn, class EqualKey, class Alloc> class THashMap: public TMapOps<THashMap<Key, T, HashFcn, EqualKey, Alloc>> { -private: +private: using ht = THashTable<std::pair<const Key, T>, Key, HashFcn, TSelect1st, EqualKey, Alloc>; ht rep; - -public: + +public: using key_type = typename ht::key_type; using value_type = typename ht::value_type; using hasher = typename ht::hasher; @@ -1435,26 +1435,26 @@ public: using allocator_type = typename ht::allocator_type; using node_allocator_type = typename ht::node_allocator_type; using mapped_type = T; - + using size_type = typename ht::size_type; using difference_type = typename ht::difference_type; using pointer = typename ht::pointer; using const_pointer = typename ht::const_pointer; using reference = typename ht::reference; using const_reference = typename ht::const_reference; - + using iterator = typename ht::iterator; using const_iterator = typename ht::const_iterator; using insert_ctx = typename ht::insert_ctx; - + hasher hash_function() const { return rep.hash_function(); } key_equal key_eq() const { return rep.key_eq(); } - -public: + +public: THashMap() : rep(0, hasher(), key_equal()) { @@ -1507,7 +1507,7 @@ public: { rep.insert_unique(f, l); } - + THashMap(const std::initializer_list<std::pair<Key, T>>& list) : rep(list.size(), hasher(), key_equal()) { @@ -1520,7 +1520,7 @@ public: // because its implementation is backed by THashTable. // See hash_ut.cpp -public: +public: size_type size() const noexcept { return rep.size(); } @@ -1540,7 +1540,7 @@ public: void swap(THashMap& hs) { rep.swap(hs.rep); } - + iterator begin() { return rep.begin(); } @@ -1559,8 +1559,8 @@ public: const_iterator cend() const { return rep.end(); } - -public: + +public: template <class InputIterator> void insert(InputIterator f, InputIterator l) { rep.insert_unique(f, l); @@ -1588,7 +1588,7 @@ public: iterator insert_direct(const TheObj& obj, const insert_ctx& ins) { return rep.insert_direct(obj, ins); } - + template <typename... Args> iterator emplace_direct(const insert_ctx& ins, Args&&... args) { return rep.emplace_direct(ins, std::forward<Args>(args)...); @@ -1646,7 +1646,7 @@ public: return rep.emplace_direct(ctx, std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple())->second; } - + template <class TheKey> const T& at(const TheKey& key) const { using namespace ::NPrivate; @@ -1680,7 +1680,7 @@ public: std::pair<iterator, iterator> equal_range(const TKey& key) { return rep.equal_range(key); } - + template <class TKey> std::pair<const_iterator, const_iterator> equal_range(const TKey& key) const { return rep.equal_range(key); @@ -1690,7 +1690,7 @@ public: size_type erase(const TKey& key) { return rep.erase_one(key); } - + void erase(iterator it) { rep.erase(it); } @@ -1715,8 +1715,8 @@ public: int save_for_st(IOutputStream* stream, KeySaver& ks, sthash<int, int, THash<int>, TEqualTo<int>, typename KeySaver::TSizeType>* stHash = nullptr) const { return rep.template save_for_st<KeySaver>(stream, ks, stHash); } - -public: + +public: void reserve(size_type hint) { rep.reserve(hint); } @@ -1732,9 +1732,9 @@ public: const node_allocator_type& GetNodeAllocator() const { return rep.GetNodeAllocator(); } -}; - -template <class Key, class T, class HashFcn, class EqualKey, class Alloc> +}; + +template <class Key, class T, class HashFcn, class EqualKey, class Alloc> inline bool operator==(const THashMap<Key, T, HashFcn, EqualKey, Alloc>& hm1, const THashMap<Key, T, HashFcn, EqualKey, Alloc>& hm2) { if (hm1.size() != hm2.size()) { return false; @@ -1746,8 +1746,8 @@ inline bool operator==(const THashMap<Key, T, HashFcn, EqualKey, Alloc>& hm1, co } } return true; -} - +} + template <class Key, class T, class HashFcn, class EqualKey, class Alloc> inline bool operator!=(const THashMap<Key, T, HashFcn, EqualKey, Alloc>& hm1, const THashMap<Key, T, HashFcn, EqualKey, Alloc>& hm2) { return !(hm1 == hm2); @@ -1755,37 +1755,37 @@ inline bool operator!=(const THashMap<Key, T, HashFcn, EqualKey, Alloc>& hm1, co template <class Key, class T, class HashFcn, class EqualKey, class Alloc> class THashMultiMap { -private: +private: using ht = THashTable<std::pair<const Key, T>, Key, HashFcn, TSelect1st, EqualKey, Alloc>; ht rep; - -public: + +public: using key_type = typename ht::key_type; using value_type = typename ht::value_type; using hasher = typename ht::hasher; using key_equal = typename ht::key_equal; using mapped_type = T; using allocator_type = typename ht::allocator_type; - + using size_type = typename ht::size_type; using difference_type = typename ht::difference_type; using pointer = typename ht::pointer; using const_pointer = typename ht::const_pointer; using reference = typename ht::reference; using const_reference = typename ht::const_reference; - + using iterator = typename ht::iterator; using const_iterator = typename ht::const_iterator; using insert_ctx = typename ht::insert_ctx; - + hasher hash_function() const { return rep.hash_function(); } key_equal key_eq() const { return rep.key_eq(); } - -public: + +public: THashMultiMap() : rep(0, hasher(), key_equal()) { @@ -1807,7 +1807,7 @@ public: : rep(n, hf, eql) { } - + template <class InputIterator> THashMultiMap(InputIterator f, InputIterator l) : rep(0, hasher(), key_equal()) @@ -1832,7 +1832,7 @@ public: { rep.insert_equal(f, l); } - + THashMultiMap(std::initializer_list<std::pair<Key, T>> list) : rep(list.size(), hasher(), key_equal()) { @@ -1845,7 +1845,7 @@ public: // because its implementation is backed by THashTable. // See hash_ut.cpp -public: +public: size_type size() const { return rep.size(); } @@ -1865,7 +1865,7 @@ public: void swap(THashMultiMap& hs) { rep.swap(hs.rep); } - + iterator begin() { return rep.begin(); } @@ -1884,8 +1884,8 @@ public: const_iterator cend() const { return rep.end(); } - -public: + +public: template <class InputIterator> void insert(InputIterator f, InputIterator l) { rep.insert_equal(f, l); @@ -1903,7 +1903,7 @@ public: iterator insert_noresize(const value_type& obj) { return rep.emplace_equal_noresize(obj); } - + template <typename... Args> iterator emplace_noresize(Args&&... args) { return rep.emplace_equal_noresize(std::forward<Args>(args)...); @@ -1923,12 +1923,12 @@ public: const_iterator find(const TKey& key) const { return rep.find(key); } - + template <class TKey> iterator find(const TKey& key) { return rep.find(key); } - + template <class TheKey> iterator find(const TheKey& key, insert_ctx& ins) { return rep.find_i(key, ins); @@ -1953,7 +1953,7 @@ public: std::pair<const_iterator, const_iterator> equal_range(const TKey& key) const { return rep.equal_range(key); } - + size_type erase(const key_type& key) { return rep.erase(key); } @@ -1975,14 +1975,14 @@ public: void release_nodes() { rep.release_nodes(); } - + // if (stHash != NULL) bucket_count() must be equal to stHash->bucket_count() template <class KeySaver> int save_for_st(IOutputStream* stream, KeySaver& ks, sthash<int, int, THash<int>, TEqualTo<int>, typename KeySaver::TSizeType>* stHash = nullptr) const { return rep.template save_for_st<KeySaver>(stream, ks, stHash); } -public: +public: void reserve(size_type hint) { rep.reserve(hint); } @@ -1992,9 +1992,9 @@ public: size_type bucket_size(size_type n) const { return rep.bucket_size(n); } -}; - -template <class Key, class T, class HF, class EqKey, class Alloc> +}; + +template <class Key, class T, class HF, class EqKey, class Alloc> inline bool operator==(const THashMultiMap<Key, T, HF, EqKey, Alloc>& hm1, const THashMultiMap<Key, T, HF, EqKey, Alloc>& hm2) { // NOTE: copy-pasted from // contrib/libs/cxxsupp/libcxx/include/unordered_map @@ -2015,8 +2015,8 @@ inline bool operator==(const THashMultiMap<Key, T, HF, EqKey, Alloc>& hm1, const it = eq1.second; } return true; -} - +} + template <class Key, class T, class HF, class EqKey, class Alloc> inline bool operator!=(const THashMultiMap<Key, T, HF, EqKey, Alloc>& hm1, const THashMultiMap<Key, T, HF, EqKey, Alloc>& hm2) { return !(hm1 == hm2); diff --git a/util/generic/hash_set.h b/util/generic/hash_set.h index bb27ee90d6..e8088cf23b 100644 --- a/util/generic/hash_set.h +++ b/util/generic/hash_set.h @@ -6,43 +6,43 @@ #include <initializer_list> #include <utility> -#undef value_type - +#undef value_type + template <class Value, class HashFcn, class EqualKey, class Alloc> class THashSet { -private: +private: using ht = THashTable<Value, Value, HashFcn, ::TIdentity, EqualKey, Alloc>; ht rep; - + using mutable_iterator = typename ht::iterator; -public: +public: using key_type = typename ht::key_type; using value_type = typename ht::value_type; using hasher = typename ht::hasher; using key_equal = typename ht::key_equal; using allocator_type = typename ht::allocator_type; using node_allocator_type = typename ht::node_allocator_type; - + using size_type = typename ht::size_type; using difference_type = typename ht::difference_type; using pointer = typename ht::const_pointer; using const_pointer = typename ht::const_pointer; using reference = typename ht::const_reference; using const_reference = typename ht::const_reference; - + using iterator = typename ht::const_iterator; using const_iterator = typename ht::const_iterator; using insert_ctx = typename ht::insert_ctx; - + hasher hash_function() const { return rep.hash_function(); } key_equal key_eq() const { return rep.key_eq(); } - -public: + +public: THashSet() { } template <class TT> @@ -62,7 +62,7 @@ public: : rep(n, hf, eql) { } - + THashSet(std::initializer_list<value_type> list) : rep(list.size(), hasher(), key_equal()) { @@ -110,12 +110,12 @@ public: { rep.insert_unique(f, l); } - + // THashSet has implicit copy/move constructors and copy-/move-assignment operators // because its implementation is backed by THashTable. // See hash_ut.cpp -public: +public: size_type size() const { return rep.size(); } @@ -132,7 +132,7 @@ public: void swap(THashSet& hs) { rep.swap(hs.rep); } - + iterator begin() const { return rep.begin(); } @@ -145,8 +145,8 @@ public: iterator cend() const { return rep.end(); } - -public: + +public: void insert(std::initializer_list<value_type> ilist) { insert(ilist.begin(), ilist.end()); } @@ -159,7 +159,7 @@ public: std::pair<iterator, bool> insert(const value_type& obj) { std::pair<mutable_iterator, bool> p = rep.insert_unique(obj); return std::pair<iterator, bool>(p.first, p.second); - } + } template <typename... Args> std::pair<iterator, bool> emplace(Args&&... args) { std::pair<mutable_iterator, bool> p = rep.emplace_unique(std::forward<Args>(args)...); @@ -189,7 +189,7 @@ public: iterator emplace_direct(const insert_ctx& ins, Args&&... args) { return rep.emplace_direct(ins, std::forward<Args>(args)...); } - + template <class TheKey> const_iterator find(const TheKey& key) const { return rep.find(key); @@ -212,12 +212,12 @@ public: size_type count(const TKey& key) const { return rep.count(key); } - + template <class TKey> std::pair<iterator, iterator> equal_range(const TKey& key) const { return rep.equal_range(key); } - + size_type erase(const key_type& key) { return rep.erase(key); } @@ -239,13 +239,13 @@ public: void release_nodes() { rep.release_nodes(); } - + template <class KeySaver> int save_for_st(IOutputStream* stream, KeySaver& ks) const { return rep.template save_for_st<KeySaver>(stream, ks); } - -public: + +public: void reserve(size_type hint) { rep.reserve(hint); } @@ -258,9 +258,9 @@ public: node_allocator_type& GetNodeAllocator() { return rep.GetNodeAllocator(); } -}; - -template <class Value, class HashFcn, class EqualKey, class Alloc> +}; + +template <class Value, class HashFcn, class EqualKey, class Alloc> inline bool operator==(const THashSet<Value, HashFcn, EqualKey, Alloc>& hs1, const THashSet<Value, HashFcn, EqualKey, Alloc>& hs2) { if (hs1.size() != hs2.size()) { return false; @@ -271,8 +271,8 @@ inline bool operator==(const THashSet<Value, HashFcn, EqualKey, Alloc>& hs1, con } } return true; -} - +} + template <class Value, class HashFcn, class EqualKey, class Alloc> inline bool operator!=(const THashSet<Value, HashFcn, EqualKey, Alloc>& hs1, const THashSet<Value, HashFcn, EqualKey, Alloc>& hs2) { return !(hs1 == hs2); @@ -280,36 +280,36 @@ inline bool operator!=(const THashSet<Value, HashFcn, EqualKey, Alloc>& hs1, con template <class Value, class HashFcn, class EqualKey, class Alloc> class THashMultiSet { -private: +private: using ht = THashTable<Value, Value, HashFcn, ::TIdentity, EqualKey, Alloc>; ht rep; - -public: + +public: using key_type = typename ht::key_type; using value_type = typename ht::value_type; using hasher = typename ht::hasher; using key_equal = typename ht::key_equal; using allocator_type = typename ht::allocator_type; using node_allocator_type = typename ht::node_allocator_type; - + using size_type = typename ht::size_type; using difference_type = typename ht::difference_type; using pointer = typename ht::const_pointer; using const_pointer = typename ht::const_pointer; using reference = typename ht::const_reference; using const_reference = typename ht::const_reference; - + using iterator = typename ht::const_iterator; using const_iterator = typename ht::const_iterator; - + hasher hash_function() const { return rep.hash_function(); } key_equal key_eq() const { return rep.key_eq(); } - -public: + +public: THashMultiSet() : rep(0, hasher(), key_equal()) { @@ -326,7 +326,7 @@ public: : rep(n, hf, eql) { } - + template <class InputIterator> THashMultiSet(InputIterator f, InputIterator l) : rep(0, hasher(), key_equal()) @@ -353,7 +353,7 @@ public: { rep.insert_equal(f, l); } - + THashMultiSet(std::initializer_list<value_type> list) : rep(list.size(), hasher(), key_equal()) { @@ -364,7 +364,7 @@ public: // because its implementation is backed by THashTable. // See hash_ut.cpp -public: +public: size_type size() const { return rep.size(); } @@ -381,7 +381,7 @@ public: void swap(THashMultiSet& hs) { rep.swap(hs.rep); } - + iterator begin() const { return rep.begin(); } @@ -394,8 +394,8 @@ public: iterator cend() const { return rep.end(); } - -public: + +public: iterator insert(const value_type& obj) { return rep.insert_equal(obj); } @@ -410,22 +410,22 @@ public: iterator insert_noresize(const value_type& obj) { return rep.insert_equal_noresize(obj); } - + template <class TKey> iterator find(const TKey& key) const { return rep.find(key); } - + template <class TKey> size_type count(const TKey& key) const { return rep.count(key); } - + template <class TKey> std::pair<iterator, iterator> equal_range(const TKey& key) const { return rep.equal_range(key); } - + size_type erase(const key_type& key) { return rep.erase(key); } @@ -447,8 +447,8 @@ public: void release_nodes() { rep.release_nodes(); } - -public: + +public: void reserve(size_type hint) { rep.reserve(hint); } @@ -461,9 +461,9 @@ public: node_allocator_type& GetNodeAllocator() { return rep.GetNodeAllocator(); } -}; - -template <class Val, class HashFcn, class EqualKey, class Alloc> +}; + +template <class Val, class HashFcn, class EqualKey, class Alloc> inline bool operator==(const THashMultiSet<Val, HashFcn, EqualKey, Alloc>& hs1, const THashMultiSet<Val, HashFcn, EqualKey, Alloc>& hs2) { if (hs1.size() != hs2.size()) { return false; @@ -480,7 +480,7 @@ inline bool operator==(const THashMultiSet<Val, HashFcn, EqualKey, Alloc>& hs1, } } return true; -} +} template <class Val, class HashFcn, class EqualKey, class Alloc> inline bool operator!=(const THashMultiSet<Val, HashFcn, EqualKey, Alloc>& hs1, const THashMultiSet<Val, HashFcn, EqualKey, Alloc>& hs2) { diff --git a/util/generic/hash_ut.cpp b/util/generic/hash_ut.cpp index 9bf1a3b9b6..0551d58770 100644 --- a/util/generic/hash_ut.cpp +++ b/util/generic/hash_ut.cpp @@ -346,13 +346,13 @@ void THashTest::TestHMMap1() { hmap.insert(HMapType::value_type(23, 0)); UNIT_ASSERT(hmap.count(12325) == 2); - - UNIT_ASSERT(hmap.bucket_count() > 3000); - for (size_t n = 0; n < 10; n++) { - hmap.clear(); - hmap.insert(HMapType::value_type(1, 2)); - } - UNIT_ASSERT(hmap.bucket_count() < 30); + + UNIT_ASSERT(hmap.bucket_count() > 3000); + for (size_t n = 0; n < 10; n++) { + hmap.clear(); + hmap.insert(HMapType::value_type(1, 2)); + } + UNIT_ASSERT(hmap.bucket_count() < 30); } void THashTest::TestHMMapHas() { diff --git a/util/generic/strbase.h b/util/generic/strbase.h index 119f4eb832..ab39fc7537 100644 --- a/util/generic/strbase.h +++ b/util/generic/strbase.h @@ -326,7 +326,7 @@ public: return !empty() && TTraits::eq(Ptr()[Len() - 1], ch); } - template <typename TDerived2, typename TTraits2> + template <typename TDerived2, typename TTraits2> bool operator==(const TStringBase<TDerived2, TChar, TTraits2>& s2) const noexcept { return equal(*this, s2); } @@ -344,7 +344,7 @@ public: return equal(pc, s); } - template <typename TDerived2, typename TTraits2> + template <typename TDerived2, typename TTraits2> friend bool operator!=(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { return !(s1 == s2); } @@ -362,7 +362,7 @@ public: } #endif - template <typename TDerived2, typename TTraits2> + template <typename TDerived2, typename TTraits2> friend bool operator<(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { return compare(s1, s2) < 0; } @@ -379,7 +379,7 @@ public: return compare(pc, s) < 0; } - template <typename TDerived2, typename TTraits2> + template <typename TDerived2, typename TTraits2> friend bool operator<=(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { return compare(s1, s2) <= 0; } @@ -396,7 +396,7 @@ public: return compare(pc, s) <= 0; } - template <typename TDerived2, typename TTraits2> + template <typename TDerived2, typename TTraits2> friend bool operator>(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { return compare(s1, s2) > 0; } @@ -413,7 +413,7 @@ public: return compare(pc, s) > 0; } - template <typename TDerived2, typename TTraits2> + template <typename TDerived2, typename TTraits2> friend bool operator>=(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept { return compare(s1, s2) >= 0; } @@ -573,11 +573,11 @@ public: return TDerived(Ptr(), Len()); } - // ~~~ Partial copy ~~~~ + // ~~~ Partial copy ~~~~ TDerived substr(size_t pos, size_t n = npos) const Y_WARN_UNUSED_RESULT { - return TDerived(*This(), pos, n); - } - + return TDerived(*This(), pos, n); + } + private: using GenericFinder = const TCharType* (*)(const TCharType*, size_t, const TCharType*, size_t); diff --git a/util/generic/strbuf.h b/util/generic/strbuf.h index 03d1a3397c..70b9360d58 100644 --- a/util/generic/strbuf.h +++ b/util/generic/strbuf.h @@ -20,7 +20,7 @@ private: 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; @@ -116,7 +116,7 @@ public: { } - template <typename D, typename T> + template <typename D, typename T> inline TBasicStringBuf(const TStringBase<D, TCharType, T>& str) noexcept : TStringView(str.data(), str.size()) { @@ -147,10 +147,10 @@ public: 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) { diff --git a/util/generic/strfcpy.cpp b/util/generic/strfcpy.cpp index d9dd21eb8e..19b4da493e 100644 --- a/util/generic/strfcpy.cpp +++ b/util/generic/strfcpy.cpp @@ -1,13 +1,13 @@ /* $OpenBSD: strlcpy.c,v 1.16 2019/01/25 00:19:25 millert Exp $ */ -/* +/* * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org> - * - * + * + * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. - * + * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR @@ -15,23 +15,23 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - + */ + /* * Modified version is * Copyright (c) Alexey Galakhov */ -#include "strfcpy.h" - -/* +#include "strfcpy.h" + +/* * Copy string src to buffer dst of size dsize. At most dsize-1 * chars will be copied. Always NUL terminates (unless dsize == 0). - */ + */ void strfcpy(char* dst, const char* src, size_t dsize) { size_t nleft = dsize; - + /* Copy as many bytes as will fit. */ if (nleft != 0) { while (--nleft != 0) { @@ -40,11 +40,11 @@ void strfcpy(char* dst, const char* src, size_t dsize) } } } - + /* Not enough room in dst, add NUL */ if (nleft == 0) { if (dsize != 0) { *dst = '\0'; /* NUL-terminate dst */ } } -} +} diff --git a/util/generic/string_ut.h b/util/generic/string_ut.h index 688687593e..44bb10bdeb 100644 --- a/util/generic/string_ut.h +++ b/util/generic/string_ut.h @@ -794,16 +794,16 @@ public: void TestOperatorsCI() { TStringType s(Data.ABCD()); - UNIT_ASSERT(s > Data.abc0123456xyz()); - UNIT_ASSERT(s == Data.abcd()); - + UNIT_ASSERT(s > Data.abc0123456xyz()); + UNIT_ASSERT(s == Data.abcd()); + using TCIStringBuf = TBasicStringBuf<char_type, traits_type>; UNIT_ASSERT(s > TCIStringBuf(Data.abc0123456xyz())); UNIT_ASSERT(TCIStringBuf(Data.abc0123456xyz()) < s); UNIT_ASSERT(s == TCIStringBuf(Data.abcd())); - } - + } + void TestMulOperators() { { TStringType s(Data._0()); diff --git a/util/generic/utility.h b/util/generic/utility.h index 304a1b4e33..43b98eeafc 100644 --- a/util/generic/utility.h +++ b/util/generic/utility.h @@ -101,14 +101,14 @@ template <class T> static inline void DoSwap(T& l, T& r) noexcept(noexcept(NSwapCheck::TSwapSelector<T>::Swap(l, r))) { NSwapCheck::TSwapSelector<T>::Swap(l, r); } - -template <bool b> -struct TNullTmpl { + +template <bool b> +struct TNullTmpl { template <class T> - operator T() const { - return (T)0; - } -}; + operator T() const { + return (T)0; + } +}; using TNull = TNullTmpl<0>; diff --git a/util/memory/segmented_string_pool.h b/util/memory/segmented_string_pool.h index fa7effb433..a40aa408f5 100644 --- a/util/memory/segmented_string_pool.h +++ b/util/memory/segmented_string_pool.h @@ -1,11 +1,11 @@ #pragma once - + #include <util/system/align.h> #include <util/system/yassert.h> #include <util/system/defaults.h> #include <util/generic/noncopyable.h> #include <util/generic/vector.h> -#include <util/generic/strbuf.h> +#include <util/generic/strbuf.h> #include <memory> #include <cstdio> @@ -18,7 +18,7 @@ template <class T, class Alloc = std::allocator<T>> class segmented_pool: TNonCopyable { protected: Alloc seg_allocator; - struct seg_inf { + struct seg_inf { T* data; // allocated chunk size_t _size; // size of allocated chunk in sizeof(T)-units size_t freepos; // offset to free chunk's memory in bytes @@ -34,7 +34,7 @@ protected: , freepos(0) { } - }; + }; using seg_container = TVector<seg_inf>; using seg_iterator = typename seg_container::iterator; using seg_const_iterator = typename seg_container::const_iterator; @@ -72,7 +72,7 @@ public: } ~segmented_pool() { clear(); - } + } /* src - array of objects, len - count of elements in array */ T* append(const T* src, size_t len) { check_capacity(len); @@ -82,12 +82,12 @@ public: memcpy(rv, src, last_ins_size); curseg->freepos += last_ins_size, last_free -= len; return (T*)rv; - } + } T* append() { T* obj = get_raw(); new (obj) T(); return obj; - } + } T* get_raw() { // append(0, 1) check_capacity(1); ui8* rv = (ui8*)curseg->data + curseg->freepos; @@ -114,38 +114,38 @@ public: return segs.size() * segment_size * sizeof(T); } void restart() { - if (curseg != segs.end()) - ++curseg; - for (seg_iterator i = segs.begin(); i != curseg; ++i) + if (curseg != segs.end()) + ++curseg; + for (seg_iterator i = segs.begin(); i != curseg; ++i) i->freepos = 0; curseg = segs.begin(); last_free = 0; last_ins_size = 0; } - void clear() { + void clear() { for (seg_iterator i = segs.begin(); i != segs.end(); ++i) seg_allocator.deallocate(i->data, i->_size); - segs.clear(); - curseg = segs.begin(); - last_free = 0; - last_ins_size = 0; - } - void undo_last_append() { + segs.clear(); + curseg = segs.begin(); + last_free = 0; + last_ins_size = 0; + } + void undo_last_append() { Y_ASSERT(curseg != segs.end()); // do not use before append() if (last_ins_size) { Y_ASSERT(last_ins_size <= curseg->freepos); curseg->freepos -= last_ins_size; last_free += last_ins_size / sizeof(T); - last_ins_size = 0; - } - } + last_ins_size = 0; + } + } void alloc_first_seg() { Y_ASSERT(capacity() == 0); check_capacity(segment_size); Y_ASSERT(capacity() == segment_size * sizeof(T)); } -}; - +}; + class segmented_string_pool: public segmented_pool<char> { private: using _Base = segmented_pool<char>; @@ -166,14 +166,14 @@ public: } char* append(const char* src, size_t len) { char* rv = _Base::append(nullptr, len + 1); - if (src) - memcpy(rv, src, len); - rv[len] = 0; - return rv; - } + if (src) + memcpy(rv, src, len); + rv[len] = 0; + return rv; + } char* Append(const TStringBuf s) { return append(s.data(), s.size()); - } + } void align_4() { size_t t = (curseg->freepos + 3) & ~3; last_free -= t - curseg->freepos; @@ -184,11 +184,11 @@ public: } }; -template <typename T, typename C> +template <typename T, typename C> inline T* pool_push(segmented_pool<C>& pool, const T* v) { static_assert(sizeof(C) == 1, "only char type supported"); - size_t len = SizeOf(v); + size_t len = SizeOf(v); C* buf = pool.append(nullptr, AlignUp(len)); - memcpy(buf, v, len); - return (T*)buf; -} + memcpy(buf, v, len); + return (T*)buf; +} diff --git a/util/memory/segpool_alloc.h b/util/memory/segpool_alloc.h index e8d39b64fa..1a83b7a543 100644 --- a/util/memory/segpool_alloc.h +++ b/util/memory/segpool_alloc.h @@ -1,35 +1,35 @@ #pragma once - + #include "segmented_string_pool.h" - -/** - This is a partially implemented allocator class that uses segmented_pool - as its allocatior base. - In fact, segpool_alloc can only allocate, it can't deallocate. Therefore, - it should be used with add-only containers that are just clear()'ed in the - end of their life cycle. - - External segmented_pool object must be cleared explicitly after all - containers use it in their allocator are cleared. - Single segmented_pool object may be used for several containers. - - Use segpool_alloc_vt as allocator template parameter for container. - Example: + +/** + This is a partially implemented allocator class that uses segmented_pool + as its allocatior base. + In fact, segpool_alloc can only allocate, it can't deallocate. Therefore, + it should be used with add-only containers that are just clear()'ed in the + end of their life cycle. + + External segmented_pool object must be cleared explicitly after all + containers use it in their allocator are cleared. + Single segmented_pool object may be used for several containers. + + Use segpool_alloc_vt as allocator template parameter for container. + Example: using TSomething = THashMap<A, B, hash<A>, TEqualTo<A>, segpool_alloc_vt>; - ... - TSomething something; - something.GetNodeAllocator().pool = &alloc_pool; - - Some containers may provide no way to access their allocator. In that case, - if you want to use them with segpool_alloc_vt, you should first either - hack them, or fix segpool_alloc so that it would create ref-counted - segmented_pool object on it's own - + ... + TSomething something; + something.GetNodeAllocator().pool = &alloc_pool; + + Some containers may provide no way to access their allocator. In that case, + if you want to use them with segpool_alloc_vt, you should first either + hack them, or fix segpool_alloc so that it would create ref-counted + segmented_pool object on it's own + ! Only tested with THashMap -*/ - -template <class _Tp> -struct segpool_alloc { +*/ + +template <class _Tp> +struct segpool_alloc { using pool_type = segmented_pool<char>; pool_type* pool; using pointer = _Tp*; @@ -39,62 +39,62 @@ struct segpool_alloc { using size_type = size_t; using difference_type = ptrdiff_t; using value_type = _Tp; -#ifndef NDEBUG - ui64 pool_count, malloc_count, pool_free_count, malloc_free_count; -#endif +#ifndef NDEBUG + ui64 pool_count, malloc_count, pool_free_count, malloc_free_count; +#endif segpool_alloc() : pool(nullptr) { Y_IF_DEBUG(pool_count = malloc_count = pool_free_count = malloc_free_count = 0); - } + } segpool_alloc(pool_type* p) : pool(p) { Y_IF_DEBUG(pool_count = malloc_count = pool_free_count = malloc_free_count = 0); } - segpool_alloc(const segpool_alloc& a) + segpool_alloc(const segpool_alloc& a) : pool(a.pool) { Y_IF_DEBUG(pool_count = malloc_count = pool_free_count = malloc_free_count = 0); - } - template <class _Tp1> - segpool_alloc(const segpool_alloc<_Tp1>& a) + } + template <class _Tp1> + segpool_alloc(const segpool_alloc<_Tp1>& a) : pool(a.pool) { Y_IF_DEBUG(pool_count = malloc_count = pool_free_count = malloc_free_count = 0); } _Tp* allocate(size_t __n) { - if (!pool) { - _Tp* data = (_Tp*)malloc(__n * sizeof(_Tp)); + if (!pool) { + _Tp* data = (_Tp*)malloc(__n * sizeof(_Tp)); Y_IF_DEBUG(if (data) malloc_count++); - return data; - } + return data; + } _Tp* data = (_Tp*)pool->append(nullptr, __n * sizeof(_Tp)); Y_IF_DEBUG(pool_count++); - return data; - } - void deallocate(pointer __p, size_t /*__n*/) { - if (!pool) { + return data; + } + void deallocate(pointer __p, size_t /*__n*/) { + if (!pool) { Y_IF_DEBUG(malloc_free_count++); - free(__p); + free(__p); } else { Y_IF_DEBUG(pool_free_count++); ; } - } - ~segpool_alloc() { + } + ~segpool_alloc() { //assert(pool_count == pool_free_count && malloc_count == malloc_free_count); <- uncomment when swap() problem is solved //printf("in ~segpool_alloc: size = %u, pool_count = %" PRId64 ", malloc_count = %" PRId64 ", pool_free_count = %" PRId64 ", malloc_free_count = %" PRId64 "\n", - // sizeof(_Tp), pool_count, malloc_count, pool_free_count, malloc_free_count); + // sizeof(_Tp), pool_count, malloc_count, pool_free_count, malloc_free_count); //fflush(stdout); - } + } template <class _Tp1> struct rebind { using other = segpool_alloc<_Tp1>; - }; - size_type max_size() const { - return size_type(-1) / sizeof(_Tp); - } + }; + size_type max_size() const { + return size_type(-1) / sizeof(_Tp); + } void construct(pointer __p, const _Tp& __val) { new (__p) _Tp(__val); } @@ -102,17 +102,17 @@ struct segpool_alloc { (void)__p; /* Make MSVC happy. */ __p->~_Tp(); } -}; +}; -template <class _Tp> +template <class _Tp> inline bool operator==(const segpool_alloc<_Tp>& a1, const segpool_alloc<_Tp>& a2) { return a1.pool == a2.pool; -} - -template <class _Tp> +} + +template <class _Tp> inline bool operator!=(const segpool_alloc<_Tp>& a1, const segpool_alloc<_Tp>& a2) { return a1.pool != a2.pool; -} - +} + // Any type since it is supposed to be rebound anyway. using segpool_alloc_vt = segpool_alloc<int>; diff --git a/util/str_stl.h b/util/str_stl.h index 97f0816c0d..f1e137181d 100644 --- a/util/str_stl.h +++ b/util/str_stl.h @@ -25,7 +25,7 @@ namespace std { } 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; }; } @@ -65,17 +65,17 @@ template <typename T> struct hash<const T*> { inline size_t operator()(const T* t) const noexcept { return NumericHash(t); - } -}; - + } +}; + template <class T> struct hash<T*>: public ::hash<const T*> { }; template <> struct hash<const char*>: ::NHashPrivate::TStringHash<char> { -}; - +}; + template <> struct THash<TStringBuf>: ::NHashPrivate::TStringHash<char> { }; @@ -154,7 +154,7 @@ namespace NHashPrivate { public: template <class T> - inline size_t operator()(const T& pair) const { + inline size_t operator()(const T& pair) const { return CombineHashes(FirstHash(pair.first), SecondHash(pair.second)); } }; @@ -167,7 +167,7 @@ namespace NHashPrivate { template <class TFirst, class TSecond> struct TPairHash<TFirst, TSecond, true> { template <class T> - inline size_t operator()(const T& pair) const { + inline size_t operator()(const T& pair) const { // maps have TFirst = const TFoo, which would make for an undefined specialization using TFirstClean = std::remove_cv_t<TFirst>; using TSecondClean = std::remove_cv_t<TSecond>; @@ -184,12 +184,12 @@ template <class T> struct TEqualTo: public std::equal_to<T> { }; -template <> +template <> struct TEqualTo<TString>: public TEqualTo<TStringBuf> { using is_transparent = void; -}; - -template <> +}; + +template <> struct TEqualTo<TUtf16String>: public TEqualTo<TWtringBuf> { using is_transparent = void; }; @@ -200,14 +200,14 @@ struct TEqualTo<TUtf32String>: public TEqualTo<TUtf32StringBuf> { }; template <class TFirst, class TSecond> -struct TEqualTo<std::pair<TFirst, TSecond>> { +struct TEqualTo<std::pair<TFirst, TSecond>> { template <class TOther> - inline bool operator()(const std::pair<TFirst, TSecond>& a, const TOther& b) const { - return TEqualTo<TFirst>()(a.first, b.first) && TEqualTo<TSecond>()(a.second, b.second); - } - using is_transparent = void; -}; - + inline bool operator()(const std::pair<TFirst, TSecond>& a, const TOther& b) const { + return TEqualTo<TFirst>()(a.first, b.first) && TEqualTo<TSecond>()(a.second, b.second); + } + using is_transparent = void; +}; + template <class T> struct TCIEqualTo { }; @@ -240,8 +240,8 @@ struct TLess: public std::less<T> { template <> struct TLess<TString>: public TLess<TStringBuf> { using is_transparent = void; -}; - +}; + template <> struct TLess<TUtf16String>: public TLess<TWtringBuf> { using is_transparent = void; diff --git a/util/stream/direct_io_ut.cpp b/util/stream/direct_io_ut.cpp index 29f7858ecf..01d09db232 100644 --- a/util/stream/direct_io_ut.cpp +++ b/util/stream/direct_io_ut.cpp @@ -8,8 +8,8 @@ #include "direct_io.h" Y_UNIT_TEST_SUITE(TDirectIOTests) { - // Decrease numBufToWrite further if tests continue to time out - static void Y_NO_INLINE Test(EOpenMode mode, size_t numBufToWrite) { + // Decrease numBufToWrite further if tests continue to time out + static void Y_NO_INLINE Test(EOpenMode mode, size_t numBufToWrite) { const char TEMPLATE[] = "qwertyuiopQWERTYUIOPasdfghjklASD"; const auto TEMPLATE_SIZE = Y_ARRAY_SIZE(TEMPLATE) - 1; static_assert(TEMPLATE_SIZE > 0, "must be greater than zero"); @@ -33,7 +33,7 @@ Y_UNIT_TEST_SUITE(TDirectIOTests) { auto&& directIOBuffer = TDirectIOBufferedFile{fileName, RdWr | CreateAlways | mode}; { auto&& output = TRandomAccessFileOutput{directIOBuffer}; - for (size_t i = 0; i < numBufToWrite; ++i) { + for (size_t i = 0; i < numBufToWrite; ++i) { output.Write(buffer.Data(), BUFFER_SIZE); } } @@ -54,18 +54,18 @@ Y_UNIT_TEST_SUITE(TDirectIOTests) { } } - UNIT_ASSERT_VALUES_EQUAL(bytesRead, numBufToWrite * BUFFER_SIZE); + UNIT_ASSERT_VALUES_EQUAL(bytesRead, numBufToWrite * BUFFER_SIZE); } Y_UNIT_TEST(ReadWriteTest) { - Test(0, 100 * 32); + Test(0, 100 * 32); } Y_UNIT_TEST(ReadWriteDirectTest) { - Test(Direct, 100 * 4); + Test(Direct, 100 * 4); } Y_UNIT_TEST(ReadWriteDirectSeqTest) { - Test(Direct | Seq, 100 * 4); + Test(Direct | Seq, 100 * 4); } } diff --git a/util/stream/input_ut.cpp b/util/stream/input_ut.cpp index 20469cf551..4a93f5458e 100644 --- a/util/stream/input_ut.cpp +++ b/util/stream/input_ut.cpp @@ -6,10 +6,10 @@ #include <util/system/file.h> #include <util/system/yassert.h> -#ifdef _win_ +#ifdef _win_ #include <io.h> -#endif - +#endif + class TMockStdIn { public: TMockStdIn() @@ -28,7 +28,7 @@ public: tempFile.Seek(0, sSet); TFileHandle tempFh(tempFile.GetHandle()); - tempFh.Duplicate2Posix(0); + tempFh.Duplicate2Posix(0); tempFh.Release(); func(); diff --git a/util/string/escape.h b/util/string/escape.h index ac52685f31..b01be65b0e 100644 --- a/util/string/escape.h +++ b/util/string/escape.h @@ -1,7 +1,7 @@ #pragma once #include <util/generic/string.h> -#include <util/generic/strbuf.h> +#include <util/generic/strbuf.h> template <class TChar> TBasicString<TChar>& EscapeCImpl(const TChar* str, size_t len, TBasicString<TChar>&); @@ -26,9 +26,9 @@ static inline TBasicString<TChar> EscapeC(const TChar* str, size_t len) { template <typename TChar> static inline TBasicString<TChar> EscapeC(const TBasicStringBuf<TChar>& str) { return EscapeC(str.data(), str.size()); -} - -template <typename TChar> +} + +template <typename TChar> static inline TBasicString<TChar>& UnescapeC(const TChar* str, size_t len, TBasicString<TChar>& s) { return UnescapeCImpl(str, len, s); } diff --git a/util/string/split.cpp b/util/string/split.cpp index e3fc1444f5..7d26857cc7 100644 --- a/util/string/split.cpp +++ b/util/string/split.cpp @@ -8,7 +8,7 @@ inline size_t Split(const char* ptr, const char* delim, TVector<TValue>& values) if (ptr && *ptr) { size_t epos = strcspn(ptr, delim); assert(epos); - values.push_back(TValue(ptr, epos)); + values.push_back(TValue(ptr, epos)); ptr += epos; } } @@ -17,8 +17,8 @@ inline size_t Split(const char* ptr, const char* delim, TVector<TValue>& values) size_t Split(const char* ptr, const char* delim, TVector<TString>& values) { return Split<TString>(ptr, delim, values); -} - +} + size_t Split(const TString& in, const TString& delim, TVector<TString>& res) { return Split(in.data(), delim.data(), res); } diff --git a/util/string/split.h b/util/string/split.h index 50aa32210e..bc46d9e64c 100644 --- a/util/string/split.h +++ b/util/string/split.h @@ -408,21 +408,21 @@ static inline void Split(char* buf, char ch, T* res) { } /// Split string into res vector. Res vector is cleared before split. -/// Old good slow split function. -/// Field delimter is any number of symbols specified in delim (no empty strings in res vector) +/// 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, TVector<TString>& res); size_t Split(const TString& in, const TString& delim, TVector<TString>& res); - -/// Old split reimplemented for TStringBuf using the new code -/// Note that delim can be constructed from char* automatically (it is not cheap though) + +/// Old split reimplemented for TStringBuf 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<const char>& delim, TVector<TStringBuf>& res) { - res.clear(); + res.clear(); TContainerConsumer<TVector<TStringBuf>> res1(&res); TSkipEmptyTokens<TContainerConsumer<TVector<TStringBuf>>> consumer(&res1); SplitString(s.data(), s.data() + s.size(), delim, consumer); - return res.size(); -} + return res.size(); +} template <class P, class D> void GetNext(TStringBuf& s, D delim, P& param) { diff --git a/util/string/split_ut.cpp b/util/string/split_ut.cpp index 54c9b4c833..43e59f2d75 100644 --- a/util/string/split_ut.cpp +++ b/util/string/split_ut.cpp @@ -245,10 +245,10 @@ Y_UNIT_TEST_SUITE(SplitStringTest) { TVector<TString> test; Split(data, delim, test); Cmp(good, test); - + TVector<TStringBuf> test1; Split(data, delim.data(), test1); - Cmp(good, test1); + Cmp(good, test1); } Y_UNIT_TEST(ConvenientSplitTest) { diff --git a/util/string/util.cpp b/util/string/util.cpp index 41dd7db624..b14f20bf75 100644 --- a/util/string/util.cpp +++ b/util/string/util.cpp @@ -10,44 +10,44 @@ int a2i(const TString& s) { return atoi(s.c_str()); } -//============================== span ===================================== - +//============================== span ===================================== + void str_spn::init(const char* charset, bool extended) { // chars_table_1 is necessary to avoid some unexpected // multi-threading issues - ui8 chars_table_1[256]; - memset(chars_table_1, 0, sizeof(chars_table_1)); - if (extended) { + ui8 chars_table_1[256]; + memset(chars_table_1, 0, sizeof(chars_table_1)); + if (extended) { for (const char* cs = charset; *cs; cs++) { - if (cs[1] == '-' && cs[2] != 0) { + if (cs[1] == '-' && cs[2] != 0) { for (int c = (ui8)*cs; c <= (ui8)cs[2]; c++) { - chars_table_1[c] = 1; + chars_table_1[c] = 1; } - cs += 2; - continue; - } - chars_table_1[(ui8)*cs] = 1; - } - } else { + cs += 2; + continue; + } + chars_table_1[(ui8)*cs] = 1; + } + } else { for (; *charset; charset++) { - chars_table_1[(ui8)*charset] = 1; + chars_table_1[(ui8)*charset] = 1; } - } - memcpy(chars_table, chars_table_1, 256); + } + memcpy(chars_table, chars_table_1, 256); chars_table_1[0] = 1; for (int n = 0; n < 256; n++) { c_chars_table[n] = !chars_table_1[n]; } -} - +} + Tr::Tr(const char* from, const char* to) { for (size_t n = 0; n < 256; n++) { - Map[n] = (char)n; + Map[n] = (char)n; } for (; *from && *to; from++, to++) { - Map[(ui8)*from] = *to; + Map[(ui8)*from] = *to; } -} +} size_t Tr::FindFirstChangePosition(const TString& str) const { for (auto it = str.begin(); it != str.end(); ++it) { diff --git a/util/string/util.h b/util/string/util.h index 0c41a3459e..0d77a5042b 100644 --- a/util/string/util.h +++ b/util/string/util.h @@ -53,24 +53,24 @@ inline void addIfAbsent(TString& s, char lastCh1, char lastCh2) { * * ------------------------------------------------------------------ */ -struct ui8_256 { +struct ui8_256 { // forward chars table - ui8 chars_table[256]; + ui8 chars_table[256]; // reverse (for c* functions) chars table ui8 c_chars_table[256]; -}; - +}; + class str_spn: public ui8_256 { public: explicit str_spn(const char* charset, bool extended = false) { - // exteneded: if true, treat charset string more like - // interior of brackets [ ], e.g. "a-z0-9" - init(charset, extended); - } - - /// Return first character in table, like strpbrk() - /// That is, skip all characters not in table - /// [DIFFERENCE FOR NOT_FOUND CASE: Returns end of string, not NULL] + // exteneded: if true, treat charset string more like + // interior of brackets [ ], e.g. "a-z0-9" + init(charset, extended); + } + + /// Return first character in table, like strpbrk() + /// That is, skip all characters not in table + /// [DIFFERENCE FOR NOT_FOUND CASE: Returns end of string, not NULL] const char* brk(const char* s) const { while (c_chars_table[(ui8)*s]) ++s; @@ -83,13 +83,13 @@ public: return s; } - /// Return first character not in table, like strpbrk() for inverted table. - /// That is, skip all characters in table + /// Return first character not in table, like strpbrk() for inverted table. + /// That is, skip all characters in table const char* cbrk(const char* s) const { while (chars_table[(ui8)*s]) ++s; - return s; - } + return s; + } const char* cbrk(const char* s, const char* e) const { while (s < e && chars_table[(ui8)*s]) @@ -97,7 +97,7 @@ public: return s; } - /// Offset of the first character not in table, like strspn(). + /// Offset of the first character not in table, like strspn(). size_t spn(const char* s) const { return cbrk(s) - s; } @@ -106,7 +106,7 @@ public: return cbrk(s, e) - s; } - /// Offset of the first character in table, like strcspn(). + /// Offset of the first character in table, like strcspn(). size_t cspn(const char* s) const { return brk(s) - s; } @@ -123,35 +123,35 @@ public: return const_cast<char*>(cbrk((const char*)s)); } - /// See strsep [BUT argument is *&, not **] + /// See strsep [BUT argument is *&, not **] char* sep(char*& s) const { - char sep_char; // unused; - return sep(s, sep_char); - } - - /// strsep + remember character that was destroyed + char sep_char; // unused; + return sep(s, sep_char); + } + + /// strsep + remember character that was destroyed char* sep(char*& s, char& sep_char) const { - if (!s) + if (!s) return nullptr; char* ret = s; char* next = brk(ret); - if (*next) { - sep_char = *next; - *next = 0; - s = next + 1; - } else { - sep_char = 0; + if (*next) { + sep_char = *next; + *next = 0; + s = next + 1; + } else { + sep_char = 0; s = nullptr; - } - return ret; - } - + } + return ret; + } + protected: void init(const char* charset, bool extended); str_spn() = default; -}; - -// an analogue of tr/$from/$to/ +}; + +// an analogue of tr/$from/$to/ class Tr { public: Tr(const char* from, const char* to); @@ -161,14 +161,14 @@ public: } void Do(char* s) const { - for (; *s; s++) + for (; *s; s++) *s = ConvertChar(*s); - } + } void Do(const char* src, char* dst) const { - for (; *src; src++) + for (; *src; src++) *dst++ = ConvertChar(*src); - *dst = 0; - } + *dst = 0; + } void Do(char* s, size_t l) const { for (size_t i = 0; i < l && s[i]; i++) s[i] = ConvertChar(s[i]); @@ -179,7 +179,7 @@ private: char Map[256]; size_t FindFirstChangePosition(const TString& str) const; -}; +}; // Removes all occurrences of given character from string template <typename TStringType> diff --git a/util/string/util_ut.cpp b/util/string/util_ut.cpp index 5c6e003132..18a2d8e195 100644 --- a/util/string/util_ut.cpp +++ b/util/string/util_ut.cpp @@ -10,8 +10,8 @@ class TStrUtilTest: public TTestBase { public: void TestSpn() { - str_spn rul("a-z", true); - char s[] = "!@#$ab%^&c+-"; + str_spn rul("a-z", true); + char s[] = "!@#$ab%^&c+-"; UNIT_ASSERT_EQUAL(rul.brk(s), s + 4); UNIT_ASSERT_EQUAL(rul.brk(s + 4), s + 4); UNIT_ASSERT_EQUAL(rul.brk(s + 10), s + 12); @@ -21,7 +21,7 @@ public: UNIT_ASSERT_EQUAL(strcmp(rul.sep(s1), "%^&"), 0); UNIT_ASSERT_EQUAL(strcmp(rul.sep(s1), "+-"), 0); UNIT_ASSERT_EQUAL(rul.sep(s1), nullptr); - } + } void TestRemoveAll() { static const struct T { diff --git a/util/system/align.h b/util/system/align.h index e5e8a217f8..ea0bbc5b46 100644 --- a/util/system/align.h +++ b/util/system/align.h @@ -21,9 +21,9 @@ template <class T> static inline T AlignUpSpace(T len, T align) noexcept { Y_ASSERT(IsPowerOf2(align)); // align should be power of 2 return ((T)0 - len) & (align - 1); // AlignUp(len, align) - len; -} - -template <class T> +} + +template <class T> static inline T* AlignUp(T* ptr, size_t align) noexcept { return (T*)AlignUp((uintptr_t)ptr, align); } diff --git a/util/system/atomic.h b/util/system/atomic.h index 676fa3fb91..80265babfd 100644 --- a/util/system/atomic.h +++ b/util/system/atomic.h @@ -1,5 +1,5 @@ #pragma once - + #include "defaults.h" using TAtomicBase = intptr_t; @@ -11,8 +11,8 @@ using TAtomic = volatile TAtomicBase; #include "atomic_win.h" #else #error unsupported platform -#endif - +#endif + #if !defined(ATOMIC_COMPILER_BARRIER) #define ATOMIC_COMPILER_BARRIER() #endif diff --git a/util/system/atomic_gcc.h b/util/system/atomic_gcc.h index 8838e3919c..ed8dc2bdc5 100644 --- a/util/system/atomic_gcc.h +++ b/util/system/atomic_gcc.h @@ -1,5 +1,5 @@ #pragma once - + #define ATOMIC_COMPILER_BARRIER() __asm__ __volatile__("" \ : \ : \ @@ -61,7 +61,7 @@ static inline intptr_t AtomicSwap(TAtomic* p, intptr_t v) { __atomic_exchange(p, &v, &ret, __ATOMIC_SEQ_CST); return ret; } - + static inline bool AtomicCas(TAtomic* a, intptr_t exchange, intptr_t compare) { (void)a; // disable strange 'parameter set but not used' warning on gcc return __atomic_compare_exchange(a, &compare, &exchange, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); diff --git a/util/system/atomic_win.h b/util/system/atomic_win.h index 084c4c8859..65c290e6cc 100644 --- a/util/system/atomic_win.h +++ b/util/system/atomic_win.h @@ -1,5 +1,5 @@ #pragma once - + #include <intrin.h> #define USE_GENERIC_SETGET @@ -14,7 +14,7 @@ static inline intptr_t AtomicIncrement(TAtomic& a) { return _InterlockedIncrement((volatile long*)&a); -} +} static inline intptr_t AtomicGetAndIncrement(TAtomic& a) { return _InterlockedIncrement((volatile long*)&a) - 1; @@ -22,7 +22,7 @@ static inline intptr_t AtomicGetAndIncrement(TAtomic& a) { static inline intptr_t AtomicDecrement(TAtomic& a) { return _InterlockedDecrement((volatile long*)&a); -} +} static inline intptr_t AtomicGetAndDecrement(TAtomic& a) { return _InterlockedDecrement((volatile long*)&a) + 1; @@ -30,7 +30,7 @@ static inline intptr_t AtomicGetAndDecrement(TAtomic& a) { static inline intptr_t AtomicAdd(TAtomic& a, intptr_t b) { return _InterlockedExchangeAdd((volatile long*)&a, b) + b; -} +} static inline intptr_t AtomicGetAndAdd(TAtomic& a, intptr_t b) { return _InterlockedExchangeAdd((volatile long*)&a, b); @@ -39,7 +39,7 @@ static inline intptr_t AtomicGetAndAdd(TAtomic& a, intptr_t b) { static inline intptr_t AtomicSwap(TAtomic* a, intptr_t b) { return _InterlockedExchange((volatile long*)a, b); } - + static inline bool AtomicCas(TAtomic* a, intptr_t exchange, intptr_t compare) { return _InterlockedCompareExchange((volatile long*)a, exchange, compare) == compare; } diff --git a/util/system/compat.h b/util/system/compat.h index b85591b0d1..c53dbcca17 100644 --- a/util/system/compat.h +++ b/util/system/compat.h @@ -44,7 +44,7 @@ extern "C" { void vwarnx(const char* format, va_list ap); void vwarn(const char* format, va_list ap); void verrx(int status, const char* format, va_list ap); -#else +#else #include <err.h> #endif } diff --git a/util/system/err.cpp b/util/system/err.cpp index 891d7c0cb6..5573ea1ee9 100644 --- a/util/system/err.cpp +++ b/util/system/err.cpp @@ -4,20 +4,20 @@ #include "error.h" #include <util/generic/scope.h> - + #include <util/stream/printf.h> #include <util/stream/output.h> - + void vwarnx(const char* fmt, va_list args) { Cerr << GetProgramName() << ": "; - + if (fmt) { Printf(Cerr, fmt, args); } - + Cerr << '\n'; } - + void vwarn(const char* fmt, va_list args) { int curErrNo = errno; auto curErrText = LastSystemErrorText(); @@ -25,55 +25,55 @@ void vwarn(const char* fmt, va_list args) { Y_DEFER { errno = curErrNo; }; - + Cerr << GetProgramName() << ": "; - + if (fmt) { Printf(Cerr, fmt, args); Cerr << ": "; } Cerr << curErrText << '\n'; -} - +} + void warn(const char* fmt, ...) { va_list args; - + va_start(args, fmt); vwarn(fmt, args); va_end(args); -} - +} + void warnx(const char* fmt, ...) { va_list args; - + va_start(args, fmt); vwarnx(fmt, args); va_end(args); -} - +} + void verr(int status, const char* fmt, va_list args) { vwarn(fmt, args); exit(status); -} - +} + void err(int status, const char* fmt, ...) { va_list args; va_start(args, fmt); verr(status, fmt, args); va_end(args); -} - +} + void verrx(int status, const char* fmt, va_list args) { vwarnx(fmt, args); exit(status); -} - +} + void errx(int status, const char* fmt, ...) { va_list args; va_start(args, fmt); verrx(status, fmt, args); va_end(args); -} +} diff --git a/util/system/file.cpp b/util/system/file.cpp index ec7cc69272..4a261d020c 100644 --- a/util/system/file.cpp +++ b/util/system/file.cpp @@ -49,7 +49,7 @@ #include <sys/sendfile.h> #define HAVE_POSIX_FADVISE 1 #define HAVE_SYNC_FILE_RANGE 1 -#elif defined(__FreeBSD__) && !defined(WITH_VALGRIND) +#elif defined(__FreeBSD__) && !defined(WITH_VALGRIND) #include <sys/param.h> #define HAVE_POSIX_FADVISE (__FreeBSD_version >= 900501) #define HAVE_SYNC_FILE_RANGE 0 @@ -565,38 +565,38 @@ FHANDLE TFileHandle::Duplicate() const noexcept { #endif } -int TFileHandle::Duplicate2Posix(int dstHandle) const noexcept { - if (!IsOpen()) { - return -1; - } -#if defined(_win_) - FHANDLE dupHandle = Duplicate(); - if (dupHandle == INVALID_FHANDLE) { - _set_errno(EMFILE); - return -1; - } - int posixHandle = _open_osfhandle((intptr_t)dupHandle, 0); - if (posixHandle == -1) { - CloseHandle(dupHandle); - return -1; - } - if (dup2(posixHandle, dstHandle) == -1) { - dstHandle = -1; - } - _close(posixHandle); - return dstHandle; -#elif defined(_unix_) - while (dup2(Fd_, dstHandle) == -1) { - if (errno != EINTR) { - return -1; - } - } - return dstHandle; -#else +int TFileHandle::Duplicate2Posix(int dstHandle) const noexcept { + if (!IsOpen()) { + return -1; + } +#if defined(_win_) + FHANDLE dupHandle = Duplicate(); + if (dupHandle == INVALID_FHANDLE) { + _set_errno(EMFILE); + return -1; + } + int posixHandle = _open_osfhandle((intptr_t)dupHandle, 0); + if (posixHandle == -1) { + CloseHandle(dupHandle); + return -1; + } + if (dup2(posixHandle, dstHandle) == -1) { + dstHandle = -1; + } + _close(posixHandle); + return dstHandle; +#elif defined(_unix_) + while (dup2(Fd_, dstHandle) == -1) { + if (errno != EINTR) { + return -1; + } + } + return dstHandle; +#else #error unsupported platform -#endif -} - +#endif +} + bool TFileHandle::LinkTo(const TFileHandle& fh) const noexcept { #if defined(_unix_) while (dup2(fh.Fd_, Fd_) == -1) { diff --git a/util/system/file.h b/util/system/file.h index caec596d9e..9502e159b6 100644 --- a/util/system/file.h +++ b/util/system/file.h @@ -120,7 +120,7 @@ public: int Flock(int op) noexcept; FHANDLE Duplicate() const noexcept; - int Duplicate2Posix(int dstHandle) const noexcept; + int Duplicate2Posix(int dstHandle) const noexcept; //dup2 - like semantics, return true on success bool LinkTo(const TFileHandle& fh) const noexcept; diff --git a/util/system/filemap.cpp b/util/system/filemap.cpp index a0864cc980..7454a4cb94 100644 --- a/util/system/filemap.cpp +++ b/util/system/filemap.cpp @@ -60,7 +60,7 @@ namespace { return sysInfo.dwAllocationGranularity; #else return NSystemInfo::GetPageSize(); -#endif +#endif } const size_t GRANULARITY_; @@ -188,7 +188,7 @@ public: , Mode_(om) { CheckFile(); - + if (File_.GetLength() < Length_) { File_.Resize(Length_); } @@ -220,15 +220,15 @@ public: inline TMapResult Map(i64 offset, size_t size) { assert(File_.IsOpen()); - + if (offset > Length_) { ythrow yexception() << "Can't map something at offset " << offset << " of '" << DbgName_ << "' with length " << Length_; } - + if (offset + (i64)size > Length_) { ythrow yexception() << "Can't map " << (unsigned long)size << " bytes at offset " << offset << " of '" << DbgName_ << "' with length " << Length_; } - + TMapResult result; i64 base = DownToGranularity(offset); diff --git a/util/system/filemap.h b/util/system/filemap.h index 014b46c41d..11be64bff4 100644 --- a/util/system/filemap.h +++ b/util/system/filemap.h @@ -52,7 +52,7 @@ struct TMemoryMapCommon { oRdOnly = 1, oRdWr = 2, oCopyOnWr = 4, - + oAccessMask = 7, oNotGreedy = 8, oPrecharge = 16, @@ -77,12 +77,12 @@ public: TMemoryMap(FILE* f, EOpenMode om, TString dbgName = UnknownFileName()); TMemoryMap(const TFile& file, TString dbgName = UnknownFileName()); TMemoryMap(const TFile& file, EOpenMode om, TString dbgName = UnknownFileName()); - + ~TMemoryMap(); - + TMapResult Map(i64 offset, size_t size); bool Unmap(TMapResult region); - + void ResizeAndReset(i64 size); TMapResult ResizeAndRemap(i64 offset, size_t size); @@ -91,7 +91,7 @@ public: bool IsWritable() const noexcept; EOpenMode GetMode() const noexcept; TFile GetFile() const noexcept; - + void SetSequential(); void Evict(void* ptr, size_t len); void Evict(); @@ -100,7 +100,7 @@ public: * deprecated */ bool Unmap(void* ptr, size_t size); - + private: class TImpl; TSimpleIntrusivePtr<TImpl> Impl_; @@ -165,7 +165,7 @@ public: TFile GetFile() const noexcept { return Map_.GetFile(); } - + void Precharge(size_t pos = 0, size_t size = (size_t)-1) const; void SetSequential() { @@ -203,11 +203,11 @@ public: ~TFileMappedArray() { Ptr_ = nullptr; End_ = nullptr; - } + } void Init(const char* name) { DataHolder_.Reset(new TFileMap(name)); DoInit(name); - } + } void Init(const TFileMap& fileMap) { DataHolder_.Reset(new TFileMap(fileMap)); DoInit(fileMap.GetFile().GetName()); @@ -217,18 +217,18 @@ public: Ptr_ = nullptr; Size_ = 0; End_ = nullptr; - } + } void Precharge() { DataHolder_->Precharge(); } const T& operator[](size_t pos) const { Y_ASSERT(pos < size()); return Ptr_[pos]; - } + } /// for STL compatibility only, Size() usage is recommended - size_t size() const { + size_t size() const { return Size_; - } + } size_t Size() const { return Size_; } @@ -236,11 +236,11 @@ public: if (pos < Size_) return Ptr_[pos]; return Dummy(); - } + } void SetDummy(const T& n_Dummy) { Dummy_.Destroy(); Dummy_.Reset(new (DummyData()) T(n_Dummy)); - } + } inline char* DummyData() const noexcept { return AlignUp((char*)DummyData_); } @@ -285,8 +285,8 @@ private: Size_ = DataHolder_->Length() / sizeof(T); End_ = Ptr_ + Size_; } -}; - +}; + class TMappedAllocation: TMoveOnly { public: TMappedAllocation(size_t size = 0, bool shared = false, void* addr = nullptr); @@ -343,12 +343,12 @@ public: T* Create(size_t siz) { Y_ASSERT(MappedSize() == 0 && Ptr() == nullptr); T* arr = (T*)Alloc((sizeof(T) * siz)); - if (!arr) + if (!arr) return nullptr; Y_ASSERT(MappedSize() == sizeof(T) * siz); for (size_t n = 0; n < siz; n++) new (&arr[n]) T(); - return arr; + return arr; } void Destroy() { T* arr = (T*)Ptr(); diff --git a/util/system/fs_win.cpp b/util/system/fs_win.cpp index 52ecf3ed7f..a410ccac06 100644 --- a/util/system/fs_win.cpp +++ b/util/system/fs_win.cpp @@ -6,7 +6,7 @@ #include <util/charset/wide.h> #include "file.h" -#include <winioctl.h> +#include <winioctl.h> namespace NFsPrivate { static LPCWSTR UTF8ToWCHAR(const TStringBuf str, TUtf16String& wstr) { diff --git a/util/system/guard.h b/util/system/guard.h index fa8da37f27..efc091d5f8 100644 --- a/util/system/guard.h +++ b/util/system/guard.h @@ -1,5 +1,5 @@ #pragma once - + #include <util/generic/noncopyable.h> template <class T> diff --git a/util/system/mktemp.cpp b/util/system/mktemp.cpp index 4b332255af..505b7b4a4b 100644 --- a/util/system/mktemp.cpp +++ b/util/system/mktemp.cpp @@ -10,7 +10,7 @@ #ifdef _win32_ #include "winint.h" #include <io.h> -#else +#else #include <unistd.h> #include <stdlib.h> #endif diff --git a/util/system/mutex.h b/util/system/mutex.h index 2187380e65..032630d134 100644 --- a/util/system/mutex.h +++ b/util/system/mutex.h @@ -17,7 +17,7 @@ public: inline void Release() noexcept { } - + inline void lock() noexcept { Acquire(); } diff --git a/util/system/shellcommand.cpp b/util/system/shellcommand.cpp index 4200491e2e..b1989b5c8c 100644 --- a/util/system/shellcommand.cpp +++ b/util/system/shellcommand.cpp @@ -628,22 +628,22 @@ void TShellCommand::TImpl::StartProcess(TShellCommand::TImpl::TPipes& pipes) { #endif void ShellQuoteArg(TString& dst, TStringBuf argument) { - dst.append("\""); - TStringBuf l, r; + dst.append("\""); + TStringBuf l, r; while (argument.TrySplit('"', l, r)) { - dst.append(l); - dst.append("\\\""); + dst.append(l); + dst.append("\\\""); argument = r; - } + } dst.append(argument); - dst.append("\""); -} - + dst.append("\""); +} + void ShellQuoteArgSp(TString& dst, TStringBuf argument) { - dst.append(' '); + dst.append(' '); ShellQuoteArg(dst, argument); -} - +} + bool ArgNeedsQuotes(TStringBuf arg) noexcept { if (arg.empty()) { return true; diff --git a/util/system/shellcommand.h b/util/system/shellcommand.h index d3d1488d3a..8730627fe5 100644 --- a/util/system/shellcommand.h +++ b/util/system/shellcommand.h @@ -474,11 +474,11 @@ private: using TImplRef = TSimpleIntrusivePtr<TImpl>; TImplRef Impl; }; - -/// Appends to dst: quoted arg + +/// Appends to dst: quoted arg void ShellQuoteArg(TString& dst, TStringBuf arg); - -/// Appends to dst: space, quoted arg + +/// Appends to dst: space, quoted arg void ShellQuoteArgSp(TString& dst, TStringBuf arg); /// Returns true if arg should be quoted diff --git a/util/system/shellcommand_ut.cpp b/util/system/shellcommand_ut.cpp index ffd7fd738d..9d849279d2 100644 --- a/util/system/shellcommand_ut.cpp +++ b/util/system/shellcommand_ut.cpp @@ -62,14 +62,14 @@ private: Y_UNIT_TEST_SUITE(TShellQuoteTest) { Y_UNIT_TEST(TestQuoteArg) { TString cmd; - ShellQuoteArg(cmd, "/pr f/krev/prev.exe"); - ShellQuoteArgSp(cmd, "-DVal=\"W Quotes\""); - ShellQuoteArgSp(cmd, "-DVal=W Space"); - ShellQuoteArgSp(cmd, "-DVal=Blah"); - UNIT_ASSERT_STRINGS_EQUAL(cmd, "\"/pr f/krev/prev.exe\" \"-DVal=\\\"W Quotes\\\"\" \"-DVal=W Space\" \"-DVal=Blah\""); - } -} - + ShellQuoteArg(cmd, "/pr f/krev/prev.exe"); + ShellQuoteArgSp(cmd, "-DVal=\"W Quotes\""); + ShellQuoteArgSp(cmd, "-DVal=W Space"); + ShellQuoteArgSp(cmd, "-DVal=Blah"); + UNIT_ASSERT_STRINGS_EQUAL(cmd, "\"/pr f/krev/prev.exe\" \"-DVal=\\\"W Quotes\\\"\" \"-DVal=W Space\" \"-DVal=Blah\""); + } +} + Y_UNIT_TEST_SUITE(TShellCommandTest) { Y_UNIT_TEST(TestNoQuotes) { TShellCommandOptions options; diff --git a/util/system/spin_wait.h b/util/system/spin_wait.h index c13559bce6..91dd423e33 100644 --- a/util/system/spin_wait.h +++ b/util/system/spin_wait.h @@ -1,5 +1,5 @@ #pragma once - + struct TSpinWait { TSpinWait() noexcept; diff --git a/util/system/spinlock.h b/util/system/spinlock.h index a560d83821..af2630890a 100644 --- a/util/system/spinlock.h +++ b/util/system/spinlock.h @@ -1,5 +1,5 @@ #pragma once - + #include "atomic.h" #include "spin_wait.h" diff --git a/util/system/thread.cpp b/util/system/thread.cpp index f1fe4a24b4..6236746c2d 100644 --- a/util/system/thread.cpp +++ b/util/system/thread.cpp @@ -8,7 +8,7 @@ #include <util/generic/yexception.h> #include "yassert.h" #include <utility> - + #if defined(_glibc_) #if !__GLIBC_PREREQ(2, 30) #include <sys/syscall.h> @@ -82,7 +82,7 @@ namespace { #endif { } - + inline bool Running() const noexcept { return Handle != 0; } @@ -231,7 +231,7 @@ namespace { #undef PCHECK using TThreadBase = TPosixThread; -#endif +#endif template <class T> static inline typename T::TValueType* Impl(T& t, const char* op, bool check = true) { @@ -403,48 +403,48 @@ static void WindowsCurrentSetThreadName(DWORD dwThreadID, const char* threadName } #endif -#if defined(_win_) -namespace { - struct TWinThreadDescrAPI { - TWinThreadDescrAPI() +#if defined(_win_) +namespace { + struct TWinThreadDescrAPI { + TWinThreadDescrAPI() : Kernel32Dll("kernel32.dll") - , SetThreadDescription((TSetThreadDescription)Kernel32Dll.SymOptional("SetThreadDescription")) - , GetThreadDescription((TGetThreadDescription)Kernel32Dll.SymOptional("GetThreadDescription")) - { - } - - // This API is for Windows 10+ only: - // https://msdn.microsoft.com/en-us/library/windows/desktop/mt774972(v=vs.85).aspx - bool HasAPI() noexcept { - return SetThreadDescription && GetThreadDescription; - } - - // Should always succeed, unless something very strange is passed in `descr' - void SetDescr(const char* descr) { - auto hr = SetThreadDescription(GetCurrentThread(), (const WCHAR*)UTF8ToWide(descr).data()); - Y_VERIFY(SUCCEEDED(hr), "SetThreadDescription failed"); - } - - TString GetDescr() { - PWSTR wideName; - auto hr = GetThreadDescription(GetCurrentThread(), &wideName); - Y_VERIFY(SUCCEEDED(hr), "GetThreadDescription failed"); - Y_DEFER { - LocalFree(wideName); - }; - return WideToUTF8((const wchar16*)wideName); - } - + , SetThreadDescription((TSetThreadDescription)Kernel32Dll.SymOptional("SetThreadDescription")) + , GetThreadDescription((TGetThreadDescription)Kernel32Dll.SymOptional("GetThreadDescription")) + { + } + + // This API is for Windows 10+ only: + // https://msdn.microsoft.com/en-us/library/windows/desktop/mt774972(v=vs.85).aspx + bool HasAPI() noexcept { + return SetThreadDescription && GetThreadDescription; + } + + // Should always succeed, unless something very strange is passed in `descr' + void SetDescr(const char* descr) { + auto hr = SetThreadDescription(GetCurrentThread(), (const WCHAR*)UTF8ToWide(descr).data()); + Y_VERIFY(SUCCEEDED(hr), "SetThreadDescription failed"); + } + + TString GetDescr() { + PWSTR wideName; + auto hr = GetThreadDescription(GetCurrentThread(), &wideName); + Y_VERIFY(SUCCEEDED(hr), "GetThreadDescription failed"); + Y_DEFER { + LocalFree(wideName); + }; + return WideToUTF8((const wchar16*)wideName); + } + typedef HRESULT(__cdecl* TSetThreadDescription)(HANDLE hThread, PCWSTR lpThreadDescription); typedef HRESULT(__cdecl* TGetThreadDescription)(HANDLE hThread, PWSTR* ppszThreadDescription); - - TDynamicLibrary Kernel32Dll; - TSetThreadDescription SetThreadDescription; - TGetThreadDescription GetThreadDescription; - }; -} -#endif // _win_ - + + TDynamicLibrary Kernel32Dll; + TSetThreadDescription SetThreadDescription; + TGetThreadDescription GetThreadDescription; + }; +} +#endif // _win_ + void TThread::SetCurrentThreadName(const char* name) { (void)name; @@ -455,15 +455,15 @@ void TThread::SetCurrentThreadName(const char* name) { prctl(PR_SET_NAME, name, 0, 0, 0); #elif defined(_darwin_) pthread_setname_np(name); -#elif defined(_win_) - auto api = Singleton<TWinThreadDescrAPI>(); - if (api->HasAPI()) { - api->SetDescr(name); - } else { +#elif defined(_win_) + auto api = Singleton<TWinThreadDescrAPI>(); + if (api->HasAPI()) { + api->SetDescr(name); + } else { #if defined(_MSC_VER) - WindowsCurrentSetThreadName(DWORD(-1), name); + WindowsCurrentSetThreadName(DWORD(-1), name); #endif - } + } #else // no idea #endif // OS @@ -471,7 +471,7 @@ void TThread::SetCurrentThreadName(const char* name) { TString TThread::CurrentThreadName() { #if defined(_freebsd_) -// TODO: check pthread_get_name_np API availability +// TODO: check pthread_get_name_np API availability #elif defined(_linux_) // > The buffer should allow space for up to 16 bytes; the returned string will be // > null-terminated. @@ -487,12 +487,12 @@ TString TThread::CurrentThreadName() { memset(name, 0, sizeof(name)); Y_VERIFY(pthread_getname_np(thread, name, sizeof(name)) == 0, "pthread_getname_np failed: %s", strerror(errno)); return name; -#elif defined(_win_) - auto api = Singleton<TWinThreadDescrAPI>(); - if (api->HasAPI()) { - return api->GetDescr(); - } - return {}; +#elif defined(_win_) + auto api = Singleton<TWinThreadDescrAPI>(); + if (api->HasAPI()) { + return api->GetDescr(); + } + return {}; #else // no idea #endif // OS @@ -500,16 +500,16 @@ TString TThread::CurrentThreadName() { return {}; } -bool TThread::CanGetCurrentThreadName() { -#if defined(_linux_) || defined(_darwin_) - return true; -#elif defined(_win_) - return Singleton<TWinThreadDescrAPI>()->HasAPI(); -#else - return false; -#endif // OS -} - +bool TThread::CanGetCurrentThreadName() { +#if defined(_linux_) || defined(_darwin_) + return true; +#elif defined(_win_) + return Singleton<TWinThreadDescrAPI>()->HasAPI(); +#else + return false; +#endif // OS +} + TCurrentThreadLimits::TCurrentThreadLimits() noexcept : StackBegin(nullptr) , StackLength(0) diff --git a/util/system/thread.h b/util/system/thread.h index 8ce6ab3016..a6e8abdb5b 100644 --- a/util/system/thread.h +++ b/util/system/thread.h @@ -1,5 +1,5 @@ #pragma once - + /// This code should not be used directly unless you really understand what you do. /// If you need threads, use thread pool functionality in <util/thread/factory.h> /// @see SystemThreadFactory() @@ -9,7 +9,7 @@ #include "defaults.h" #include "progname.h" - + bool SetHighestThreadPriority(); class TThread { @@ -71,7 +71,7 @@ public: return *this; } }; - + TThread(const TParams& params); TThread(TThreadProc threadProc, void* param); @@ -113,18 +113,18 @@ public: // NOTE: Content of `name` will be copied. // // NOTE: On Linux thread name is limited to 15 symbols which is probably the smallest one among - // all platforms. If you provide a name longer than 15 symbols it will be cut. So if you expect + // all platforms. If you provide a name longer than 15 symbols it will be cut. So if you expect // `CurrentThreadName` to return the same name as `name` make sure it's not longer than 15 // symbols. static void SetCurrentThreadName(const char* name); - // NOTE: Will return empty string where CanGetCurrentThreadName() returns false. + // NOTE: Will return empty string where CanGetCurrentThreadName() returns false. static TString CurrentThreadName(); - // NOTE: Depends on a platform version. - // Will return true for Darwin, Linux or fresh Windows 10. - static bool CanGetCurrentThreadName(); - + // NOTE: Depends on a platform version. + // Will return true for Darwin, Linux or fresh Windows 10. + static bool CanGetCurrentThreadName(); + private: struct TCallableBase { virtual ~TCallableBase() = default; @@ -155,7 +155,7 @@ private: private: class TImpl; THolder<TImpl> Impl_; -}; +}; class ISimpleThread: public TThread { public: diff --git a/util/system/thread.i b/util/system/thread.i index ee18b5efee..8cba505473 100644 --- a/util/system/thread.i +++ b/util/system/thread.i @@ -23,7 +23,7 @@ #endif #include <util/digest/numeric.h> - + static inline size_t SystemCurrentThreadIdImpl() noexcept { #if defined(_unix_) return (size_t)pthread_self(); diff --git a/util/system/thread_ut.cpp b/util/system/thread_ut.cpp index 8d0b023da0..d820898fd5 100644 --- a/util/system/thread_ut.cpp +++ b/util/system/thread_ut.cpp @@ -66,11 +66,11 @@ Y_UNIT_TEST_SUITE(TSysThreadTest) { TThread::SetCurrentThreadName(setName.data()); const auto getName = TThread::CurrentThreadName(); - if (TThread::CanGetCurrentThreadName()) { - UNIT_ASSERT_VALUES_EQUAL(setName, getName); - } else { - UNIT_ASSERT_VALUES_EQUAL("", getName); - } + if (TThread::CanGetCurrentThreadName()) { + UNIT_ASSERT_VALUES_EQUAL(setName, getName); + } else { + UNIT_ASSERT_VALUES_EQUAL("", getName); + } return nullptr; } @@ -97,11 +97,11 @@ Y_UNIT_TEST_SUITE(TSysThreadTest) { thread.Join(); const auto getName = TThread::CurrentThreadName(); - if (TThread::CanGetCurrentThreadName()) { - UNIT_ASSERT_VALUES_EQUAL(setName, getName); - } else { - UNIT_ASSERT_VALUES_EQUAL("", getName); - } + if (TThread::CanGetCurrentThreadName()) { + UNIT_ASSERT_VALUES_EQUAL(setName, getName); + } else { + UNIT_ASSERT_VALUES_EQUAL("", getName); + } return nullptr; } diff --git a/util/system/type_name.cpp b/util/system/type_name.cpp index 16efdbe353..0377da4212 100644 --- a/util/system/type_name.cpp +++ b/util/system/type_name.cpp @@ -1,7 +1,7 @@ #include "platform.h" #include "demangle_impl.h" -#ifdef __GNUC__ +#ifdef __GNUC__ #include <stdexcept> #include <cxxabi.h> #endif @@ -25,7 +25,7 @@ namespace { } // anonymous namespace const char* NPrivate::TCppDemangler::Demangle(const char* name) { -#ifndef __GNUC__ +#ifndef __GNUC__ return name; #else int status; diff --git a/util/system/unaligned_mem.h b/util/system/unaligned_mem.h index 4af20371f2..4b84686f2f 100644 --- a/util/system/unaligned_mem.h +++ b/util/system/unaligned_mem.h @@ -40,7 +40,7 @@ public: } inline T Cur() const noexcept { - Y_ASSERT(C_ < L_ || sizeof(T) < Align); + Y_ASSERT(C_ < L_ || sizeof(T) < Align); return ::ReadUnaligned<T>(C_); } diff --git a/util/system/yassert.cpp b/util/system/yassert.cpp index 816e9e8448..0f586648b7 100644 --- a/util/system/yassert.cpp +++ b/util/system/yassert.cpp @@ -78,7 +78,7 @@ namespace NPrivate { } else { o << " " << function << "() failed" << Endl; } - Cerr << r << Flush; + Cerr << r << Flush; #ifndef WITH_VALGRIND PrintBackTrace(); #endif diff --git a/util/thread/pool.cpp b/util/thread/pool.cpp index 506743da09..05fad02e9b 100644 --- a/util/thread/pool.cpp +++ b/util/thread/pool.cpp @@ -662,7 +662,7 @@ namespace { void IThreadPool::SafeAdd(IObjectInQueue* obj) { Y_ENSURE_EX(Add(obj), TThreadPoolException() << TStringBuf("can not add object to queue")); } - + void IThreadPool::SafeAddAndOwn(THolder<IObjectInQueue> obj) { Y_ENSURE_EX(AddAndOwn(std::move(obj)), TThreadPoolException() << TStringBuf("can not add to queue and own")); } diff --git a/util/thread/pool_ut.cpp b/util/thread/pool_ut.cpp index 628db06fed..893770d0c4 100644 --- a/util/thread/pool_ut.cpp +++ b/util/thread/pool_ut.cpp @@ -186,10 +186,10 @@ Y_UNIT_TEST_SUITE(TThreadPoolTest) { name = TThread::CurrentThreadName(); }); pool.Stop(); - if (TThread::CanGetCurrentThreadName()) { - UNIT_ASSERT_EQUAL(name, expectedName); - UNIT_ASSERT_UNEQUAL(TThread::CurrentThreadName(), expectedName); - } + if (TThread::CanGetCurrentThreadName()) { + UNIT_ASSERT_EQUAL(name, expectedName); + UNIT_ASSERT_UNEQUAL(TThread::CurrentThreadName(), expectedName); + } } Y_UNIT_TEST(TestFixedThreadName) { @@ -225,9 +225,9 @@ Y_UNIT_TEST_SUITE(TThreadPoolTest) { }); } pool.Stop(); - if (TThread::CanGetCurrentThreadName()) { - UNIT_ASSERT_EQUAL(names, expectedNames); - } + if (TThread::CanGetCurrentThreadName()) { + UNIT_ASSERT_EQUAL(names, expectedNames); + } } Y_UNIT_TEST(TestEnumeratedThreadName) { |