maint: update copyright
[gnulib.git] / lib / unigbrk / ulc-grapheme-breaks.c
1 /* Grapheme cluster breaks function.
2    Copyright (C) 2001-2003, 2006-2014 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@cs.stanford.edu>, 2010,
4    based on code written by Bruno Haible <bruno@clisp.org>, 2009.
5
6    This program is free software: you can redistribute it and/or modify it
7    under the terms of the GNU Lesser General Public License as published
8    by the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include "unigbrk.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "c-ctype.h"
28 #include "c-strcaseeq.h"
29 #include "localcharset.h"
30 #include "uniconv.h"
31
32 static int
33 is_utf8_encoding (const char *encoding)
34 {
35   if (STRCASEEQ (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
36     return 1;
37   return 0;
38 }
39
40 #if C_CTYPE_ASCII
41 /* Assume that every ASCII character starts a new grapheme, which is often
42    true, except that CR-LF is a single grapheme. */
43 static void
44 ascii_grapheme_breaks (const char *s, size_t n, char *p)
45 {
46   size_t i;
47
48   p[0] = 1;
49   for (i = 1; i < n; i++)
50     {
51       bool is_ascii = c_isprint (s[i]) || c_isspace (s[i]);
52       p[i] = is_ascii && (s[i] != '\n' || s[i - 1] != '\r');
53     }
54 }
55 #endif
56
57 /* Grapheme boundaries in a string in an arbitrary encoding.
58
59    We convert the input string to Unicode.
60
61    The standardized Unicode encodings are UTF-8, UCS-2, UCS-4, UTF-16,
62    UTF-16BE, UTF-16LE, UTF-7.  UCS-2 supports only characters up to
63    \U0000FFFF.  UTF-16 and variants support only characters up to
64    \U0010FFFF.  UTF-7 is way too complex and not supported by glibc-2.1.
65    UCS-4 specification leaves doubts about endianness and byte order mark.
66    glibc currently interprets it as big endian without byte order mark,
67    but this is not backed by an RFC.  So we use UTF-8. It supports
68    characters up to \U7FFFFFFF and is unambiguously defined.  */
69
70 void
71 ulc_grapheme_breaks (const char *s, size_t n, char *p)
72 {
73   if (n > 0)
74     {
75       const char *encoding = locale_charset ();
76
77       if (is_utf8_encoding (encoding))
78         u8_grapheme_breaks ((const uint8_t *) s, n, p);
79       else
80         {
81           /* Convert the string to UTF-8 and build a translation table
82              from offsets into s to offsets into the translated string.  */
83           size_t *offsets = (size_t *) malloc (n * sizeof (size_t));
84
85           if (offsets != NULL)
86             {
87               uint8_t *t;
88               size_t m;
89
90               t = u8_conv_from_encoding (encoding, iconveh_question_mark,
91                                          s, n, offsets, NULL, &m);
92               if (t != NULL)
93                 {
94                   char *q = (char *) (m > 0 ? malloc (m) : NULL);
95
96                   if (m == 0 || q != NULL)
97                     {
98                       size_t i;
99
100                       /* Determine the grapheme breaks of the UTF-8 string.  */
101                       u8_grapheme_breaks (t, m, q);
102
103                       /* Translate the result back to the original string.  */
104                       memset (p, 0, n);
105                       for (i = 0; i < n; i++)
106                         if (offsets[i] != (size_t)(-1))
107                           p[i] = q[offsets[i]];
108
109                       free (q);
110                       free (t);
111                       free (offsets);
112                       return;
113                     }
114                   free (t);
115                 }
116               free (offsets);
117             }
118
119           /* Impossible to convert. */
120 #if C_CTYPE_ASCII
121           /* Fall back to ASCII as best we can. */
122           ascii_grapheme_breaks (s, n, p);
123 #else
124           /* We cannot make any assumptions. */
125           p[0] = 1;
126           memset (p + 1, 0, n - 1);
127 #endif
128         }
129     }
130 }