Update description.

Improve signed integer to string conversion.
This commit is contained in:
Aleksandr Orefkov 2026-01-23 17:30:44 +03:00
parent 9b668edf24
commit 07760fc2b6
3 changed files with 54 additions and 28 deletions

View File

@ -1402,10 +1402,8 @@ concept ToIntNumber = FromIntNumber<T> || is_one_of_type<T, int8_t>::value;
template<typename K, bool I, typename T> template<typename K, bool I, typename T>
struct need_sign { // NOLINT struct need_sign { // NOLINT
bool negate; bool negate;
need_sign(T& t) : negate(t < 0) { std::make_unsigned_t<T> val;
if (negate && t != std::numeric_limits<T>::min()) need_sign(T t) : negate(t < 0), val(t < 0 ? std::make_unsigned_t<T>{} - t : t) {}
t = -t;
}
void after(K*& ptr) { void after(K*& ptr) {
if (negate) if (negate)
*--ptr = '-'; *--ptr = '-';
@ -1414,7 +1412,8 @@ struct need_sign { // NOLINT
template<typename K, typename T> template<typename K, typename T>
struct need_sign<K, false, T> { struct need_sign<K, false, T> {
need_sign(T&) {} T val;
need_sign(T t) : val(t){}
void after(K*&) {} void after(K*&) {}
}; };
@ -1427,35 +1426,22 @@ constexpr size_t fromInt(K* bufEnd, T val) {
"6061626364656667686970717273747576777879" "6061626364656667686970717273747576777879"
"8081828384858687888990919293949596979899"; "8081828384858687888990919293949596979899";
if (val) { if (val) {
need_sign<K, std::is_signed_v<T>, T> sign(val); need_sign<K, std::is_signed_v<T>, T> store(val);
K* itr = bufEnd; K* itr = bufEnd;
// Когда у нас минимальное отрицательное число, оно не меняется и остается меньше нуля while (store.val >= 100) {
// When we have a minimum negative number, it does not change and remains less than zero const char* ptr = twoDigit + (store.val % 100) * 2;
if constexpr (std::is_signed_v<T>) {
if (val < 0) {
// Возьмем две последние цифры
// Take the last two digits
const char* ptr = twoDigit - (val % 100) * 2;
*--itr = static_cast<K>(ptr[1]); *--itr = static_cast<K>(ptr[1]);
*--itr = static_cast<K>(ptr[0]); *--itr = static_cast<K>(ptr[0]);
val /= 100; store.val /= 100;
val = -val;
} }
} if (store.val < 10) {
while (val >= 100) { *--itr = static_cast<K>('0' + store.val);
const char* ptr = twoDigit + (val % 100) * 2;
*--itr = static_cast<K>(ptr[1]);
*--itr = static_cast<K>(ptr[0]);
val /= 100;
}
if (val < 10) {
*--itr = static_cast<K>('0' + val);
} else { } else {
const char* ptr = twoDigit + val * 2; const char* ptr = twoDigit + store.val * 2;
*--itr = static_cast<K>(ptr[1]); *--itr = static_cast<K>(ptr[1]);
*--itr = static_cast<K>(ptr[0]); *--itr = static_cast<K>(ptr[0]);
} }
sign.after(itr); store.after(itr);
return size_t(bufEnd - itr); return size_t(bufEnd - itr);
} }
bufEnd[-1] = '0'; bufEnd[-1] = '0';

View File

@ -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 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). 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(<your target> 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. `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. The work was tested under Windows on MSVC-19 and Clang-19, under Linux - on GCC-13 and Clang-21.

View File

@ -289,7 +289,27 @@ int split_and_calc_total_sim(ssa numbers, ssa delimiter) {
можно просто включить файлы в свой проект. Для сборки также требуется [simdutf](https://github.com/simdutf/simdutf) (при использовании CMake можно просто включить файлы в свой проект. Для сборки также требуется [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(<your target> PUBLIC simstr::simstr)
```
Библиотека также включена в [vcpkg](https://vcpkg.io), подключается как `orefkov-simstr`.
Для работы `simstr` требуется компилятор стандарта не ниже С++20 - используются концепты и std::format. Для работы `simstr` требуется компилятор стандарта не ниже С++20 - используются концепты и std::format.
Работа проверялась под Windows на MSVC-19 и Clang-19, под Linux - на GCC-13 и Clang-21. Работа проверялась под Windows на MSVC-19 и Clang-19, под Linux - на GCC-13 и Clang-21.