Blame lib/wcwidth.c

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