diff options
| author | Mikhail Romanko <me@blankhex.com> | 2025-04-05 12:48:29 +0300 |
|---|---|---|
| committer | Mikhail Romanko <me@blankhex.com> | 2025-04-05 13:56:00 +0300 |
| commit | 6aee5a83aa009c5e2cd5be5278c0b3b1fdb1325d (patch) | |
| tree | 4920c7efbfc2b6627e6a97ba48f4054709b4e3af /src/String/Inline/Signed.h | |
| parent | f9ebeabb18397f0ec6eba6223c556c70183c3fef (diff) | |
| download | bhlib-6aee5a83aa009c5e2cd5be5278c0b3b1fdb1325d.tar.gz | |
Refactor string functions, add unicode support
Refactored existing functions. Added Unicode support and UTF-8, UTF-16,
and UTF-32 encoding/decoding.
Diffstat (limited to 'src/String/Inline/Signed.h')
| -rw-r--r-- | src/String/Inline/Signed.h | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/String/Inline/Signed.h b/src/String/Inline/Signed.h new file mode 100644 index 0000000..f6ba991 --- /dev/null +++ b/src/String/Inline/Signed.h @@ -0,0 +1,37 @@ +char tmp[sizeof(value) * CHAR_BIT + 2]; +char *current, *end; +int sign; + +end = tmp + sizeof(tmp); +current = end; +sign = 0; + +/* Check for sign */ +if (value < 0) +{ + sign = 1; + value = -value; +} + +/* Fill buffer from the end */ +*(--current) = 0; +while (value) +{ + *(--current) = digits[value % base]; + value /= base; +} + +/* Append sign */ +if (sign) + *(--current) = '-'; + +/* Check that string have space for the result */ +if (size < (size_t)(end - current)) + return BH_ERROR; + +/* Copy data */ +memcpy(string, current, end - current); +if (actual) + *actual = end - current; + +return BH_OK; |
