Blame lib/wcwidth.c

Packit 8f70b4
/* Determine the number of screen columns needed for a character.
Packit 8f70b4
   Copyright (C) 2006-2007, 2010-2018 Free Software Foundation, Inc.
Packit 8f70b4
Packit 8f70b4
   This program is free software: you can redistribute it and/or modify
Packit 8f70b4
   it under the terms of the GNU General Public License as published by
Packit 8f70b4
   the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
   (at your option) any later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
   GNU General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License
Packit 8f70b4
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
/* Specification.  */
Packit 8f70b4
#include <wchar.h>
Packit 8f70b4
Packit 8f70b4
/* Get iswprint.  */
Packit 8f70b4
#include <wctype.h>
Packit 8f70b4
Packit 8f70b4
#include "localcharset.h"
Packit 8f70b4
#include "streq.h"
Packit 8f70b4
#include "uniwidth.h"
Packit 8f70b4
Packit 8f70b4
/* Returns 1 if the current locale is an UTF-8 locale, 0 otherwise.  */
Packit 8f70b4
static inline int
Packit 8f70b4
is_locale_utf8 (void)
Packit 8f70b4
{
Packit 8f70b4
  const char *encoding = locale_charset ();
Packit 8f70b4
  return STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0);
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
#if GNULIB_WCHAR_SINGLE
Packit 8f70b4
/* When we know that the locale does not change, provide a speedup by
Packit 8f70b4
   caching the value of is_locale_utf8.  */
Packit 8f70b4
static int cached_is_locale_utf8 = -1;
Packit 8f70b4
static inline int
Packit 8f70b4
is_locale_utf8_cached (void)
Packit 8f70b4
{
Packit 8f70b4
  if (cached_is_locale_utf8 < 0)
Packit 8f70b4
    cached_is_locale_utf8 = is_locale_utf8 ();
Packit 8f70b4
  return cached_is_locale_utf8;
Packit 8f70b4
}
Packit 8f70b4
#else
Packit 8f70b4
/* By default, don't make assumptions, hence no caching.  */
Packit 8f70b4
# define is_locale_utf8_cached is_locale_utf8
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
wcwidth (wchar_t wc)
Packit 8f70b4
#undef wcwidth
Packit 8f70b4
{
Packit 8f70b4
  /* In UTF-8 locales, use a Unicode aware width function.  */
Packit 8f70b4
  if (is_locale_utf8_cached ())
Packit 8f70b4
    {
Packit 8f70b4
      /* We assume that in a UTF-8 locale, a wide character is the same as a
Packit 8f70b4
         Unicode character.  */
Packit 8f70b4
      return uc_width1 (wc);
Packit 8f70b4
    }
Packit 8f70b4
  else
Packit 8f70b4
    {
Packit 8f70b4
      /* Otherwise, fall back to the system's wcwidth function.  */
Packit 8f70b4
#if HAVE_WCWIDTH
Packit 8f70b4
      return wcwidth (wc);
Packit 8f70b4
#else
Packit 8f70b4
      return wc == 0 ? 0 : iswprint (wc) ? 1 : -1;
Packit 8f70b4
#endif
Packit 8f70b4
    }
Packit 8f70b4
}