root/maint/gnulib/lib/uniwbrk/ulc-wordbreaks.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ulc_wordbreaks
  2. read_file
  3. main

   1 /* Word breaks in strings.
   2    Copyright (C) 2001-2003, 2006-2021 Free Software Foundation, Inc.
   3    Written by Bruno Haible <bruno@clisp.org>, 2009.
   4 
   5    This file is free software.
   6    It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
   7    You can redistribute it and/or modify it under either
   8      - the terms of the GNU Lesser General Public License as published
   9        by the Free Software Foundation; either version 3, or (at your
  10        option) any later version, or
  11      - the terms of the GNU General Public License as published by the
  12        Free Software Foundation; either version 2, or (at your option)
  13        any later version, or
  14      - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
  15 
  16    This file is distributed in the hope that it will be useful,
  17    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19    Lesser General Public License and the GNU General Public License
  20    for more details.
  21 
  22    You should have received a copy of the GNU Lesser General Public
  23    License and of the GNU General Public License along with this
  24    program.  If not, see <https://www.gnu.org/licenses/>.  */
  25 
  26 #include <config.h>
  27 
  28 /* Specification.  */
  29 #include "uniwbrk.h"
  30 
  31 #include <stdlib.h>
  32 #include <string.h>
  33 
  34 #include "c-ctype.h"
  35 #include "localcharset.h"
  36 #include "uniconv.h"
  37 #include "unilbrk/ulc-common.h"
  38 
  39 /* Word breaks of a string in an arbitrary encoding.
  40 
  41    We convert the input string to Unicode.
  42 
  43    The standardized Unicode encodings are UTF-8, UCS-2, UCS-4, UTF-16,
  44    UTF-16BE, UTF-16LE, UTF-7.  UCS-2 supports only characters up to
  45    \U0000FFFF.  UTF-16 and variants support only characters up to
  46    \U0010FFFF.  UTF-7 is way too complex and not supported by glibc-2.1.
  47    UCS-4 specification leaves doubts about endianness and byte order mark.
  48    glibc currently interprets it as big endian without byte order mark,
  49    but this is not backed by an RFC.  So we use UTF-8. It supports
  50    characters up to \U7FFFFFFF and is unambiguously defined.  */
  51 
  52 void
  53 ulc_wordbreaks (const char *s, size_t n, char *p)
     /* [previous][next][first][last][top][bottom][index][help] */
  54 {
  55   if (n > 0)
  56     {
  57       const char *encoding = locale_charset ();
  58 
  59       if (is_utf8_encoding (encoding))
  60         u8_wordbreaks ((const uint8_t *) s, n, p);
  61       else
  62         {
  63           /* Convert the string to UTF-8 and build a translation table
  64              from offsets into s to offsets into the translated string.  */
  65           size_t *offsets = (size_t *) malloc (n * sizeof (size_t));
  66 
  67           if (offsets != NULL)
  68             {
  69               uint8_t *t;
  70               size_t m;
  71 
  72               t = u8_conv_from_encoding (encoding, iconveh_question_mark,
  73                                          s, n, offsets, NULL, &m);
  74               if (t != NULL)
  75                 {
  76                   char *q = (char *) (m > 0 ? malloc (m) : NULL);
  77 
  78                   if (m == 0 || q != NULL)
  79                     {
  80                       size_t i;
  81 
  82                       /* Determine the word breaks of the UTF-8 string.  */
  83                       u8_wordbreaks (t, m, q);
  84 
  85                       /* Translate the result back to the original string.  */
  86                       memset (p, 0, n);
  87                       for (i = 0; i < n; i++)
  88                         if (offsets[i] != (size_t)(-1))
  89                           p[i] = q[offsets[i]];
  90 
  91                       free (q);
  92                       free (t);
  93                       free (offsets);
  94                       return;
  95                     }
  96                   free (t);
  97                 }
  98               free (offsets);
  99             }
 100 
 101           /* Impossible to convert.  */
 102 #if C_CTYPE_ASCII
 103           if (is_all_ascii (s, n))
 104             {
 105               /* ASCII is a subset of UTF-8.  */
 106               u8_wordbreaks ((const uint8_t *) s, n, p);
 107               return;
 108             }
 109 #endif
 110           /* We have a non-ASCII string and cannot convert it.
 111              Don't produce any word breaks.  */
 112           memset (p, 0, n);
 113         }
 114     }
 115 }
 116 
 117 
 118 #ifdef TEST
 119 
 120 #include <locale.h>
 121 #include <stdio.h>
 122 #include <stdlib.h>
 123 
 124 /* Read the contents of an input stream, and return it, terminated with a NUL
 125    byte. */
 126 char *
 127 read_file (FILE *stream)
     /* [previous][next][first][last][top][bottom][index][help] */
 128 {
 129 #define BUFSIZE 4096
 130   char *buf = NULL;
 131   int alloc = 0;
 132   int size = 0;
 133   int count;
 134 
 135   while (! feof (stream))
 136     {
 137       if (size + BUFSIZE > alloc)
 138         {
 139           alloc = alloc + alloc / 2;
 140           if (alloc < size + BUFSIZE)
 141             alloc = size + BUFSIZE;
 142           buf = realloc (buf, alloc);
 143           if (buf == NULL)
 144             {
 145               fprintf (stderr, "out of memory\n");
 146               exit (1);
 147             }
 148         }
 149       count = fread (buf + size, 1, BUFSIZE, stream);
 150       if (count == 0)
 151         {
 152           if (ferror (stream))
 153             {
 154               perror ("fread");
 155               exit (1);
 156             }
 157         }
 158       else
 159         size += count;
 160     }
 161   buf = realloc (buf, size + 1);
 162   if (buf == NULL)
 163     {
 164       fprintf (stderr, "out of memory\n");
 165       exit (1);
 166     }
 167   buf[size] = '\0';
 168   return buf;
 169 #undef BUFSIZE
 170 }
 171 
 172 int
 173 main (int argc, char * argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
 174 {
 175   setlocale (LC_CTYPE, "");
 176   if (argc == 1)
 177     {
 178       /* Display all the word breaks in the input string.  */
 179       char *input = read_file (stdin);
 180       int length = strlen (input);
 181       char *breaks = malloc (length);
 182       int i;
 183 
 184       ulc_wordbreaks (input, length, breaks);
 185 
 186       for (i = 0; i < length; i++)
 187         {
 188           switch (breaks[i])
 189             {
 190             case 1:
 191               putc ('|', stdout);
 192               break;
 193             case 0:
 194               break;
 195             default:
 196               abort ();
 197             }
 198           putc (input[i], stdout);
 199         }
 200 
 201       free (breaks);
 202 
 203       return 0;
 204     }
 205   else
 206     return 1;
 207 }
 208 
 209 #endif /* TEST */

/* [previous][next][first][last][top][bottom][index][help] */