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.
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.
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.
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/>. */
28 #include "c-strcaseeq.h"
29 #include "localcharset.h"
33 is_utf8_encoding (const char *encoding)
35 if (STRCASEEQ (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
41 /* Assume that every ASCII character starts a new grapheme, which is often
42 true, except that CR-LF is a single grapheme. */
44 ascii_grapheme_breaks (const char *s, size_t n, char *p)
49 for (i = 1; i < n; i++)
51 bool is_ascii = c_isprint (s[i]) || c_isspace (s[i]);
52 p[i] = is_ascii && (s[i] != '\n' || s[i - 1] != '\r');
57 /* Grapheme boundaries in a string in an arbitrary encoding.
59 We convert the input string to Unicode.
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. */
71 ulc_grapheme_breaks (const char *s, size_t n, char *p)
75 const char *encoding = locale_charset ();
77 if (is_utf8_encoding (encoding))
78 u8_grapheme_breaks ((const uint8_t *) s, n, p);
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));
90 t = u8_conv_from_encoding (encoding, iconveh_question_mark,
91 s, n, offsets, NULL, &m);
94 char *q = (char *) (m > 0 ? malloc (m) : NULL);
96 if (m == 0 || q != NULL)
100 /* Determine the grapheme breaks of the UTF-8 string. */
101 u8_grapheme_breaks (t, m, q);
103 /* Translate the result back to the original string. */
105 for (i = 0; i < n; i++)
106 if (offsets[i] != (size_t)(-1))
107 p[i] = q[offsets[i]];
119 /* Impossible to convert. */
121 /* Fall back to ASCII as best we can. */
122 ascii_grapheme_breaks (s, n, p);
124 /* We cannot make any assumptions. */
126 memset (p + 1, 0, n - 1);