Blame lib/wcwidth.c

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