Blame gettext-runtime/gnulib-lib/wcwidth.c

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