timespec_get: New module.
[gnulib.git] / lib / unigbrk / uc-is-grapheme-break.c
bloba2902af7e59f40c33eb3e3ea616ec6e9e577306f
1 /* Grapheme cluster break function.
2 Copyright (C) 2010-2021 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 <https://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 /* GB9 */ \
51 (B) == GBP_EXTEND || (B) == GBP_ZWJ ? false : \
53 /* GB9a */ \
54 (B) == GBP_SPACINGMARK ? false : \
56 /* GB9b */ \
57 (A) == GBP_PREPEND ? false : \
59 /* GB10 -- incomplete */ \
60 ((A) == GBP_EB || (A) == GBP_EBG) && (B) == GBP_EM ? false : \
62 /* GB11 */ \
63 (A) == GBP_ZWJ && ((B) == GBP_GAZ || (B) == GBP_EBG) ? false \
65 /* GB999 */ \
66 : true)
68 #define UC_GRAPHEME_BREAKS_FOR(A) \
69 ( (UC_IS_GRAPHEME_BREAK(A, GBP_OTHER) << GBP_OTHER) \
70 | (UC_IS_GRAPHEME_BREAK(A, GBP_CR) << GBP_CR) \
71 | (UC_IS_GRAPHEME_BREAK(A, GBP_LF) << GBP_LF) \
72 | (UC_IS_GRAPHEME_BREAK(A, GBP_CONTROL) << GBP_CONTROL) \
73 | (UC_IS_GRAPHEME_BREAK(A, GBP_EXTEND) << GBP_EXTEND) \
74 | (UC_IS_GRAPHEME_BREAK(A, GBP_PREPEND) << GBP_PREPEND) \
75 | (UC_IS_GRAPHEME_BREAK(A, GBP_SPACINGMARK) << GBP_SPACINGMARK) \
76 | (UC_IS_GRAPHEME_BREAK(A, GBP_L) << GBP_L) \
77 | (UC_IS_GRAPHEME_BREAK(A, GBP_V) << GBP_V) \
78 | (UC_IS_GRAPHEME_BREAK(A, GBP_T) << GBP_T) \
79 | (UC_IS_GRAPHEME_BREAK(A, GBP_LV) << GBP_LV) \
80 | (UC_IS_GRAPHEME_BREAK(A, GBP_LVT) << GBP_LVT) \
81 | (UC_IS_GRAPHEME_BREAK(A, GBP_RI) << GBP_RI) \
82 | (UC_IS_GRAPHEME_BREAK(A, GBP_ZWJ) << GBP_ZWJ) \
83 | (UC_IS_GRAPHEME_BREAK(A, GBP_EB) << GBP_EB) \
84 | (UC_IS_GRAPHEME_BREAK(A, GBP_EM) << GBP_EM) \
85 | (UC_IS_GRAPHEME_BREAK(A, GBP_GAZ) << GBP_GAZ) \
86 | (UC_IS_GRAPHEME_BREAK(A, GBP_EBG) << GBP_EBG))
88 static const unsigned long int gb_table[18] =
90 UC_GRAPHEME_BREAKS_FOR(0), /* GBP_OTHER */
91 UC_GRAPHEME_BREAKS_FOR(1), /* GBP_CR */
92 UC_GRAPHEME_BREAKS_FOR(2), /* GBP_LF */
93 UC_GRAPHEME_BREAKS_FOR(3), /* GBP_CONTROL */
94 UC_GRAPHEME_BREAKS_FOR(4), /* GBP_EXTEND */
95 UC_GRAPHEME_BREAKS_FOR(5), /* GBP_PREPEND */
96 UC_GRAPHEME_BREAKS_FOR(6), /* GBP_SPACINGMARK */
97 UC_GRAPHEME_BREAKS_FOR(7), /* GBP_L */
98 UC_GRAPHEME_BREAKS_FOR(8), /* GBP_V */
99 UC_GRAPHEME_BREAKS_FOR(9), /* GBP_T */
100 UC_GRAPHEME_BREAKS_FOR(10), /* GBP_LV */
101 UC_GRAPHEME_BREAKS_FOR(11), /* GBP_LVT */
102 UC_GRAPHEME_BREAKS_FOR(12), /* GBP_RI */
103 UC_GRAPHEME_BREAKS_FOR(13), /* GBP_ZWJ */
104 UC_GRAPHEME_BREAKS_FOR(14), /* GBP_EB */
105 UC_GRAPHEME_BREAKS_FOR(15), /* GBP_EM */
106 UC_GRAPHEME_BREAKS_FOR(16), /* GBP_GAZ */
107 UC_GRAPHEME_BREAKS_FOR(17), /* GBP_EBG */
110 bool
111 uc_is_grapheme_break (ucs4_t a, ucs4_t b)
113 int a_gcp, b_gcp;
115 if ((a | b) < 0x300)
117 /* GB3 is the only relevant rule for this case. */
118 return a != '\r' || b != '\n';
121 a_gcp = uc_graphemeclusterbreak_property (a);
122 b_gcp = uc_graphemeclusterbreak_property (b);
123 return (gb_table[a_gcp] >> b_gcp) & 1;