warnings: fix compilation with old autoconf
[gnulib/ericb.git] / lib / unigbrk / uc-is-grapheme-break.c
blob108df0faa850936164f3e40b815537f475e7bb27
1 /* Grapheme cluster break function.
2 Copyright (C) 2010-2017 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/>. */
18 #include <config.h>
20 /* Specification. */
21 #include "unigbrk.h"
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. */ \
30 /* GB3 */ \
31 (A) == GBP_CR && (B) == GBP_LF ? false : \
33 /* GB4 */ \
34 (A) == GBP_CONTROL || (A) == GBP_CR || (A) == GBP_LF ? true : \
36 /* GB5 */ \
37 (B) == GBP_CONTROL || (B) == GBP_CR || (B) == GBP_LF ? true : \
39 /* GB6 */ \
40 (A) == GBP_L && ((B) == GBP_L || (B) == GBP_V \
41 || (B) == GBP_LV || (B) == GBP_LVT) ? false : \
43 /* GB7 */ \
44 ((A) == GBP_LV || (A) == GBP_V) \
45 && ((B) == GBP_V || (B) == GBP_T) ? false : \
47 /* GB8 */ \
48 ((A) == GBP_LVT || (A) == GBP_T) && (B) == GBP_T ? false : \
50 /* GB8a */ \
51 (A) == GBP_RI && (B) == GBP_RI ? false : \
53 /* GB9 */ \
54 (B) == GBP_EXTEND ? false : \
56 /* GB9a */ \
57 (B) == GBP_SPACINGMARK ? false : \
59 /* GB9b */ \
60 (A) == GBP_PREPEND ? false \
62 /* GB10 */ \
63 : true)
65 #define UC_GRAPHEME_BREAKS_FOR(A) \
66 ( (UC_IS_GRAPHEME_BREAK(A, GBP_OTHER) << GBP_OTHER) \
67 | (UC_IS_GRAPHEME_BREAK(A, GBP_CR) << GBP_CR) \
68 | (UC_IS_GRAPHEME_BREAK(A, GBP_LF) << GBP_LF) \
69 | (UC_IS_GRAPHEME_BREAK(A, GBP_CONTROL) << GBP_CONTROL) \
70 | (UC_IS_GRAPHEME_BREAK(A, GBP_EXTEND) << GBP_EXTEND) \
71 | (UC_IS_GRAPHEME_BREAK(A, GBP_PREPEND) << GBP_PREPEND) \
72 | (UC_IS_GRAPHEME_BREAK(A, GBP_SPACINGMARK) << GBP_SPACINGMARK) \
73 | (UC_IS_GRAPHEME_BREAK(A, GBP_L) << GBP_L) \
74 | (UC_IS_GRAPHEME_BREAK(A, GBP_V) << GBP_V) \
75 | (UC_IS_GRAPHEME_BREAK(A, GBP_T) << GBP_T) \
76 | (UC_IS_GRAPHEME_BREAK(A, GBP_LV) << GBP_LV) \
77 | (UC_IS_GRAPHEME_BREAK(A, GBP_LVT) << GBP_LVT) \
78 | (UC_IS_GRAPHEME_BREAK(A, GBP_RI) << GBP_RI))
80 static const unsigned short int gb_table[13] =
82 UC_GRAPHEME_BREAKS_FOR(0), /* GBP_OTHER */
83 UC_GRAPHEME_BREAKS_FOR(1), /* GBP_CR */
84 UC_GRAPHEME_BREAKS_FOR(2), /* GBP_LF */
85 UC_GRAPHEME_BREAKS_FOR(3), /* GBP_CONTROL */
86 UC_GRAPHEME_BREAKS_FOR(4), /* GBP_EXTEND */
87 UC_GRAPHEME_BREAKS_FOR(5), /* GBP_PREPEND */
88 UC_GRAPHEME_BREAKS_FOR(6), /* GBP_SPACINGMARK */
89 UC_GRAPHEME_BREAKS_FOR(7), /* GBP_L */
90 UC_GRAPHEME_BREAKS_FOR(8), /* GBP_V */
91 UC_GRAPHEME_BREAKS_FOR(9), /* GBP_T */
92 UC_GRAPHEME_BREAKS_FOR(10), /* GBP_LV */
93 UC_GRAPHEME_BREAKS_FOR(11), /* GBP_LVT */
94 UC_GRAPHEME_BREAKS_FOR(12), /* GBP_RI */
97 bool
98 uc_is_grapheme_break (ucs4_t a, ucs4_t b)
100 int a_gcp, b_gcp;
102 if ((a | b) < 0x300)
104 /* GB3 is the only relevant rule for this case. */
105 return a != '\r' || b != '\n';
108 a_gcp = uc_graphemeclusterbreak_property (a);
109 b_gcp = uc_graphemeclusterbreak_property (b);
110 return (gb_table[a_gcp] >> b_gcp) & 1;