From 07760fc2b6013dfa23abf257d9bc4e6b0e34dc99 Mon Sep 17 00:00:00 2001 From: Aleksandr Orefkov Date: Fri, 23 Jan 2026 17:30:44 +0300 Subject: [PATCH] Update description. Improve signed integer to string conversion. --- include/simstr/strexpr.h | 38 ++++++++++++-------------------------- readme.md | 22 +++++++++++++++++++++- readme_ru.md | 22 +++++++++++++++++++++- 3 files changed, 54 insertions(+), 28 deletions(-) diff --git a/include/simstr/strexpr.h b/include/simstr/strexpr.h index 161c1c6..1344c2c 100644 --- a/include/simstr/strexpr.h +++ b/include/simstr/strexpr.h @@ -1402,10 +1402,8 @@ concept ToIntNumber = FromIntNumber || is_one_of_type::value; template struct need_sign { // NOLINT bool negate; - need_sign(T& t) : negate(t < 0) { - if (negate && t != std::numeric_limits::min()) - t = -t; - } + std::make_unsigned_t val; + need_sign(T t) : negate(t < 0), val(t < 0 ? std::make_unsigned_t{} - t : t) {} void after(K*& ptr) { if (negate) *--ptr = '-'; @@ -1414,7 +1412,8 @@ struct need_sign { // NOLINT template struct need_sign { - need_sign(T&) {} + T val; + need_sign(T t) : val(t){} void after(K*&) {} }; @@ -1427,35 +1426,22 @@ constexpr size_t fromInt(K* bufEnd, T val) { "6061626364656667686970717273747576777879" "8081828384858687888990919293949596979899"; if (val) { - need_sign, T> sign(val); + need_sign, T> store(val); K* itr = bufEnd; - // Когда у нас минимальное отрицательное число, оно не меняется и остается меньше нуля - // When we have a minimum negative number, it does not change and remains less than zero - if constexpr (std::is_signed_v) { - if (val < 0) { - // Возьмем две последние цифры - // Take the last two digits - const char* ptr = twoDigit - (val % 100) * 2; - *--itr = static_cast(ptr[1]); - *--itr = static_cast(ptr[0]); - val /= 100; - val = -val; - } - } - while (val >= 100) { - const char* ptr = twoDigit + (val % 100) * 2; + while (store.val >= 100) { + const char* ptr = twoDigit + (store.val % 100) * 2; *--itr = static_cast(ptr[1]); *--itr = static_cast(ptr[0]); - val /= 100; + store.val /= 100; } - if (val < 10) { - *--itr = static_cast('0' + val); + if (store.val < 10) { + *--itr = static_cast('0' + store.val); } else { - const char* ptr = twoDigit + val * 2; + const char* ptr = twoDigit + store.val * 2; *--itr = static_cast(ptr[1]); *--itr = static_cast(ptr[0]); } - sign.after(itr); + store.after(itr); return size_t(bufEnd - itr); } bufEnd[-1] = '0'; diff --git a/readme.md b/readme.md index 14fed39..9ec19ed 100644 --- a/readme.md +++ b/readme.md @@ -288,7 +288,27 @@ You can connect as a CMake project via `add_subdirectory` (the `simstr` library) you can simply include the files in your project. Building also requires [simdutf](https://github.com/simdutf/simdutf) (when using CMake it is downloaded automatically). -The library is included in [vcpkg](https://vcpkg.io), connected as `orefkov-simstr`. +### Using FetchContent +``` +function(add_simstr) + set(SIMSTR_BUILD_TESTS OFF) + set(SIMSTR_BENCHMARKS OFF) + FetchContent_Declare( + simstr + GIT_REPOSITORY https://github.com/orefkov/simstr.git + GIT_SHALLOW TRUE + GIT_TAG tags/rel1.4.0 # Укажите нужный релиз + FIND_PACKAGE_ARGS NAMES simstr 1.4.0 + ) + FetchContent_MakeAvailable(simstr) +endfunction() + +add_simstr() + +target_link_libraries( PUBLIC simstr::simstr) +``` + +The library is also included in [vcpkg](https://vcpkg.io), connected as `orefkov-simstr`. `simstr` requires a compiler of at least the C++20 standard - concepts and std::format are used. The work was tested under Windows on MSVC-19 and Clang-19, under Linux - on GCC-13 and Clang-21. diff --git a/readme_ru.md b/readme_ru.md index 88939e1..1acf215 100644 --- a/readme_ru.md +++ b/readme_ru.md @@ -289,7 +289,27 @@ int split_and_calc_total_sim(ssa numbers, ssa delimiter) { можно просто включить файлы в свой проект. Для сборки также требуется [simdutf](https://github.com/simdutf/simdutf) (при использовании CMake скачивается автоматически). -Библиотека включена в [vcpkg](https://vcpkg.io), подключается как `orefkov-simstr`. +### Подключение через FetchContent +``` +function(add_simstr) + set(SIMSTR_BUILD_TESTS OFF) + set(SIMSTR_BENCHMARKS OFF) + FetchContent_Declare( + simstr + GIT_REPOSITORY https://github.com/orefkov/simstr.git + GIT_SHALLOW TRUE + GIT_TAG tags/rel1.4.0 # Укажите нужный релиз + FIND_PACKAGE_ARGS NAMES simstr 1.4.0 + ) + FetchContent_MakeAvailable(simstr) +endfunction() + +add_simstr() + +target_link_libraries( PUBLIC simstr::simstr) +``` + +Библиотека также включена в [vcpkg](https://vcpkg.io), подключается как `orefkov-simstr`. Для работы `simstr` требуется компилятор стандарта не ниже С++20 - используются концепты и std::format. Работа проверялась под Windows на MSVC-19 и Clang-19, под Linux - на GCC-13 и Clang-21.