wcwidth.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Determine the number of screen columns needed for a character.
  2. Copyright (C) 2006-2007, 2010-2013 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 <http://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. int
  22. wcwidth (wchar_t wc)
  23. #undef wcwidth
  24. {
  25. /* In UTF-8 locales, use a Unicode aware width function. */
  26. const char *encoding = locale_charset ();
  27. if (STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0 ,0))
  28. {
  29. /* We assume that in a UTF-8 locale, a wide character is the same as a
  30. Unicode character. */
  31. return uc_width (wc, encoding);
  32. }
  33. else
  34. {
  35. /* Otherwise, fall back to the system's wcwidth function. */
  36. #if HAVE_WCWIDTH
  37. return wcwidth (wc);
  38. #else
  39. return wc == 0 ? 0 : iswprint (wc) ? 1 : -1;
  40. #endif
  41. }
  42. }