Last active
December 13, 2015 17:09
-
-
Save EchoAbstract/4946156 to your computer and use it in GitHub Desktop.
Simple utility to compute the *Display Width* of a UTF-8 string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <string.h> | |
| #include <wchar.h> | |
| #include <stdlib.h> | |
| #include <locale.h> | |
| #include <assert.h> | |
| // Tested with: | |
| // Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn) | |
| // Target: x86_64-apple-darwin12.2.0 | |
| // Thread model: posix | |
| // on: | |
| // Darwin zen-approach.local 12.2.0 Darwin Kernel Version 12.2.0: Sat Aug 25 00:48:52 PDT 2012; root:xnu-2050.18.24~1/RELEASE_X86_64 x86_64 | |
| // Also tested with GCC on Debian Testing (compiling with -W -Wall generates one warning, but doesn't impact the code) | |
| size_t getStringLen(char *str); | |
| size_t | |
| getStringLen(char *str){ | |
| size_t totalStringLength = 0; | |
| size_t maxBytes = strlen(str); // May be bigger than needed | |
| wchar_t *output = malloc(sizeof(wchar_t) * maxBytes + 1); | |
| size_t bytesToConvert = mbstowcs(output, str, maxBytes); | |
| assert(bytesToConvert <= maxBytes); | |
| if (output){ | |
| totalStringLength = (size_t)wcswidth(output, maxBytes); | |
| free(output); | |
| } | |
| return totalStringLength; | |
| } | |
| int | |
| main(int argc, char **argv){ | |
| // Hack, should only use this if no default locale is set | |
| setlocale(LC_ALL, "en_US.UTF-8"); | |
| for (int i = 1; i < argc; i++){ | |
| printf("%5zd, %s\n", getStringLen(argv[i]), argv[i]); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment