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

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