wcwidth.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Determine the number of screen columns needed for a character.
  2. Copyright (C) 2006-2007, 2010-2020 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include <wchar.h>
  16. /* Get iswprint. */
  17. #include <wctype.h>
  18. #include "localcharset.h"
  19. #include "streq.h"
  20. #include "uniwidth.h"
  21. /* Returns 1 if the current locale is an UTF-8 locale, 0 otherwise. */
  22. static inline int
  23. is_locale_utf8 (void)
  24. {
  25. const char *encoding = locale_charset ();
  26. return STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0);
  27. }
  28. #if GNULIB_WCHAR_SINGLE
  29. /* When we know that the locale does not change, provide a speedup by
  30. caching the value of is_locale_utf8. */
  31. static int cached_is_locale_utf8 = -1;
  32. static inline int
  33. is_locale_utf8_cached (void)
  34. {
  35. if (cached_is_locale_utf8 < 0)
  36. cached_is_locale_utf8 = is_locale_utf8 ();
  37. return cached_is_locale_utf8;
  38. }
  39. #else
  40. /* By default, don't make assumptions, hence no caching. */
  41. # define is_locale_utf8_cached is_locale_utf8
  42. #endif
  43. int
  44. wcwidth (wchar_t wc)
  45. #undef wcwidth
  46. {
  47. /* In UTF-8 locales, use a Unicode aware width function. */
  48. if (is_locale_utf8_cached ())
  49. {
  50. /* We assume that in a UTF-8 locale, a wide character is the same as a
  51. Unicode character. */
  52. return uc_width (wc, "UTF-8");
  53. }
  54. else
  55. {
  56. /* Otherwise, fall back to the system's wcwidth function. */
  57. #if HAVE_WCWIDTH
  58. return wcwidth (wc);
  59. #else
  60. return wc == 0 ? 0 : iswprint (wc) ? 1 : -1;
  61. #endif
  62. }
  63. }