maint: update copyright
[gnulib.git] / lib / unigbrk / uc-is-grapheme-break.c
1 /* Grapheme cluster break function.
2    Copyright (C) 2010-2014 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@cs.stanford.edu>, 2010.
4
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.
9
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.
14
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/>.  */
17
18 #include <config.h>
19
20 /* Specification.  */
21 #include "unigbrk.h"
22
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. */                                         \
29                                                                         \
30    /* GB3 */                                                            \
31    (A) == GBP_CR && (B) == GBP_LF ? false :                             \
32                                                                         \
33    /* GB4 */                                                            \
34    (A) == GBP_CONTROL || (A) == GBP_CR || (A) == GBP_LF ? true :        \
35                                                                         \
36    /* GB5 */                                                            \
37    (B) == GBP_CONTROL || (B) == GBP_CR || (B) == GBP_LF ? true :        \
38                                                                         \
39    /* GB6 */                                                            \
40    (A) == GBP_L && ((B) == GBP_L || (B) == GBP_V                        \
41                     || (B) == GBP_LV || (B) == GBP_LVT) ? false :       \
42                                                                         \
43    /* GB7 */                                                            \
44    ((A) == GBP_LV || (A) == GBP_V)                                      \
45    && ((B) == GBP_V || (B) == GBP_T) ? false :                          \
46                                                                         \
47    /* GB8 */                                                            \
48    ((A) == GBP_LVT || (A) == GBP_T) && (B) == GBP_T ? false :           \
49                                                                         \
50    /* GB9 */                                                            \
51    (B) == GBP_EXTEND ? false :                                          \
52                                                                         \
53    /* GB9a */                                                           \
54    (B) == GBP_SPACINGMARK ? false :                                     \
55                                                                         \
56    /* GB9b */                                                           \
57    (A) == GBP_PREPEND ? false                                           \
58                                                                         \
59    /* GB10 */                                                           \
60    : true)
61
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))
75
76 static const unsigned short int gb_table[12] =
77   {
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 */
90   };
91
92 bool
93 uc_is_grapheme_break (ucs4_t a, ucs4_t b)
94 {
95   int a_gcp, b_gcp;
96
97   if ((a | b) < 0x300)
98     {
99       /* GB3 is the only relevant rule for this case. */
100       return a != '\r' || b != '\n';
101     }
102
103   a_gcp = uc_graphemeclusterbreak_property (a);
104   b_gcp = uc_graphemeclusterbreak_property (b);
105   return (gb_table[a_gcp] >> b_gcp) & 1;
106 }