1 /* Grapheme cluster break function.
2 Copyright (C) 2010-2011 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@cs.stanford.edu>, 2010.
5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* Evaluates to true if there is an extended grapheme cluster break between
24 code points with GBP_* values A and B, false if there is not. The comments
25 are the grapheme cluster boundary rules from in UAX #29. */
26 #define UC_IS_GRAPHEME_BREAK(A, B) \
27 (/* GB1 and GB2 are covered--just use a GBP_CONTROL character, such \
28 as 0, for sot and eot. */ \
31 (A) == GBP_CR && (B) == GBP_LF ? false : \
34 (A) == GBP_CONTROL || (A) == GBP_CR || (A) == GBP_LF ? true : \
37 (B) == GBP_CONTROL || (B) == GBP_CR || (B) == GBP_LF ? true : \
40 (A) == GBP_L && ((B) == GBP_L || (B) == GBP_V \
41 || (B) == GBP_LV || (B) == GBP_LVT) ? false : \
44 ((A) == GBP_LV || (A) == GBP_V) \
45 && ((B) == GBP_V || (B) == GBP_T) ? false : \
48 ((A) == GBP_LVT || (A) == GBP_T) && (B) == GBP_T ? false : \
51 (B) == GBP_EXTEND ? false : \
54 (B) == GBP_SPACINGMARK ? false : \
57 (A) == GBP_PREPEND ? false \
62 #define UC_GRAPHEME_BREAKS_FOR(A) \
63 ( (UC_IS_GRAPHEME_BREAK(A, GBP_OTHER) << GBP_OTHER) \
64 | (UC_IS_GRAPHEME_BREAK(A, GBP_CR) << GBP_CR) \
65 | (UC_IS_GRAPHEME_BREAK(A, GBP_LF) << GBP_LF) \
66 | (UC_IS_GRAPHEME_BREAK(A, GBP_CONTROL) << GBP_CONTROL) \
67 | (UC_IS_GRAPHEME_BREAK(A, GBP_EXTEND) << GBP_EXTEND) \
68 | (UC_IS_GRAPHEME_BREAK(A, GBP_PREPEND) << GBP_PREPEND) \
69 | (UC_IS_GRAPHEME_BREAK(A, GBP_SPACINGMARK) << GBP_SPACINGMARK) \
70 | (UC_IS_GRAPHEME_BREAK(A, GBP_L) << GBP_L) \
71 | (UC_IS_GRAPHEME_BREAK(A, GBP_V) << GBP_V) \
72 | (UC_IS_GRAPHEME_BREAK(A, GBP_T) << GBP_T) \
73 | (UC_IS_GRAPHEME_BREAK(A, GBP_LV) << GBP_LV) \
74 | (UC_IS_GRAPHEME_BREAK(A, GBP_LVT) << GBP_LVT))
76 static const unsigned short int gb_table[12] =
78 UC_GRAPHEME_BREAKS_FOR(0), /* GBP_OTHER */
79 UC_GRAPHEME_BREAKS_FOR(1), /* GBP_CR */
80 UC_GRAPHEME_BREAKS_FOR(2), /* GBP_LF */
81 UC_GRAPHEME_BREAKS_FOR(3), /* GBP_CONTROL */
82 UC_GRAPHEME_BREAKS_FOR(4), /* GBP_EXTEND */
83 UC_GRAPHEME_BREAKS_FOR(5), /* GBP_PREPEND */
84 UC_GRAPHEME_BREAKS_FOR(6), /* GBP_SPACINGMARK */
85 UC_GRAPHEME_BREAKS_FOR(7), /* GBP_L */
86 UC_GRAPHEME_BREAKS_FOR(8), /* GBP_V */
87 UC_GRAPHEME_BREAKS_FOR(9), /* GBP_T */
88 UC_GRAPHEME_BREAKS_FOR(10), /* GBP_LV */
89 UC_GRAPHEME_BREAKS_FOR(11), /* GBP_LVT */
93 uc_is_grapheme_break (ucs4_t a, ucs4_t b)
99 /* GB3 is the only relevant rule for this case. */
100 return a != '\r' || b != '\n';
103 a_gcp = uc_graphemeclusterbreak_property (a);
104 b_gcp = uc_graphemeclusterbreak_property (b);
105 return (gb_table[a_gcp] >> b_gcp) & 1;