diff options
author | Philip Wittamore <philip@wittamore.com> | 2025-06-08 22:00:43 +0200 |
---|---|---|
committer | Philip Wittamore <philip@wittamore.com> | 2025-06-08 22:00:43 +0200 |
commit | 81757c235ff8e112b4baabdd1ff23409426e9c98 (patch) | |
tree | ef213566ac3c17bf3d7795b0597f254791bd219e /src/util.c | |
download | dwmblocks-async-81757c235ff8e112b4baabdd1ff23409426e9c98.tar.gz dwmblocks-async-81757c235ff8e112b4baabdd1ff23409426e9c98.tar.bz2 dwmblocks-async-81757c235ff8e112b4baabdd1ff23409426e9c98.zip |
update
Diffstat (limited to 'src/util.c')
-rw-r--r-- | src/util.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..10485db --- /dev/null +++ b/src/util.c @@ -0,0 +1,49 @@ +#include "util.h" + +#define UTF8_MULTIBYTE_BIT BIT(7) + +unsigned int gcd(unsigned int a, unsigned int b) { + while (b > 0) { + const unsigned int temp = a % b; + a = b; + b = temp; + } + + return a; +} + +size_t truncate_utf8_string(char* const buffer, const size_t size, + const size_t char_limit) { + size_t char_count = 0; + size_t i = 0; + while (char_count < char_limit) { + char ch = buffer[i]; + if (ch == '\0') { + break; + } + + unsigned short skip = 1; + + // Multibyte unicode character. + if ((ch & UTF8_MULTIBYTE_BIT) != 0) { + // Skip continuation bytes. + ch <<= 1; + while ((ch & UTF8_MULTIBYTE_BIT) != 0) { + ch <<= 1; + ++skip; + } + } + + // Avoid buffer overflow. + if (i + skip >= size) { + break; + } + + ++char_count; + i += skip; + } + + buffer[i] = '\0'; + + return i + 1; +} |