Blame gnulib-tests/uniwidth/test-uc_width2.c

Packit 709fb3
/* Test of uc_width() function.
Packit 709fb3
   Copyright (C) 2007-2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
#include "uniwidth.h"
Packit 709fb3
Packit 709fb3
#include <stdio.h>
Packit 709fb3
Packit 709fb3
#include "macros.h"
Packit 709fb3
Packit 709fb3
/* One of 0, '0', '1', 'A', '2'.  */
Packit 709fb3
static char current_width;
Packit 709fb3
/* The interval for which the current_width holds.  */
Packit 709fb3
static ucs4_t current_start;
Packit 709fb3
static ucs4_t current_end;
Packit 709fb3
Packit 709fb3
static void
Packit 709fb3
finish_interval (void)
Packit 709fb3
{
Packit 709fb3
  if (current_width != 0)
Packit 709fb3
    {
Packit 709fb3
      if (current_start == current_end)
Packit 709fb3
        printf ("%04X\t\t%c\n", (unsigned) current_start, current_width);
Packit 709fb3
      else
Packit 709fb3
        printf ("%04X..%04X\t%c\n", (unsigned) current_start,
Packit 709fb3
                (unsigned) current_end, current_width);
Packit 709fb3
      current_width = 0;
Packit 709fb3
    }
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
static void
Packit 709fb3
add_to_interval (ucs4_t uc, char width)
Packit 709fb3
{
Packit 709fb3
  if (current_width == width && uc == current_end + 1)
Packit 709fb3
    current_end = uc;
Packit 709fb3
  else
Packit 709fb3
    {
Packit 709fb3
      finish_interval ();
Packit 709fb3
      current_width = width;
Packit 709fb3
      current_start = current_end = uc;
Packit 709fb3
    }
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
main ()
Packit 709fb3
{
Packit 709fb3
  ucs4_t uc;
Packit 709fb3
Packit 709fb3
  for (uc = 0; uc < 0x110000; uc++)
Packit 709fb3
    {
Packit 709fb3
      int w1 = uc_width (uc, "UTF-8");
Packit 709fb3
      int w2 = uc_width (uc, "GBK");
Packit 709fb3
      char width =
Packit 709fb3
        (w1 == 0 && w2 == 0 ? '0' :
Packit 709fb3
         w1 == 1 && w2 == 1 ? '1' :
Packit 709fb3
         w1 == 1 && w2 == 2 ? 'A' :
Packit 709fb3
         w1 == 2 && w2 == 2 ? '2' :
Packit 709fb3
         0);
Packit 709fb3
      if (width == 0)
Packit 709fb3
        {
Packit 709fb3
          /* uc must be a control character.  */
Packit 709fb3
          ASSERT (w1 < 0 && w2 < 0);
Packit 709fb3
        }
Packit 709fb3
      else
Packit 709fb3
        add_to_interval (uc, width);
Packit 709fb3
    }
Packit 709fb3
  finish_interval ();
Packit 709fb3
Packit 709fb3
  return 0;
Packit 709fb3
}