1 /* Grapheme cluster break function.
2 Copyright (C) 2010-2018 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
6 modify it under the terms of either:
7
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 or
13
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at your
16 option) any later version.
17
18 or both in parallel, as here.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
23
24 You should have received a copy of the GNU Lesser General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>. */
26
27 void
FUNC(const UNIT * s,size_t n,char * p)28 FUNC (const UNIT *s, size_t n, char *p)
29 {
30 if (n > 0)
31 {
32 const UNIT *s_end = s + n;
33
34 /* Grapheme Cluster break property of the last character.
35 -1 at the very beginning of the string. */
36 int last_char_prop = -1;
37
38 /* Grapheme Cluster break property of the last complex character.
39 -1 at the very beginning of the string. */
40 int last_compchar_prop = -1;
41
42 size_t ri_count = 0;
43
44 /* Don't break inside multibyte characters. */
45 memset (p, 0, n);
46
47 while (s < s_end)
48 {
49 ucs4_t uc;
50 int count = U_MBTOUC (&uc, s, s_end - s);
51 int prop = uc_graphemeclusterbreak_property (uc);
52
53 /* Break at the start of the string (GB1). */
54 if (last_char_prop < 0)
55 *p = 1;
56 else
57 {
58 /* No break between CR and LF (GB3). */
59 if (last_char_prop == GBP_CR && prop == GBP_LF)
60 /* *p = 0 */;
61 /* Break before and after newlines (GB4, GB5). */
62 else if ((last_char_prop == GBP_CR
63 || last_char_prop == GBP_LF
64 || last_char_prop == GBP_CONTROL)
65 || (prop == GBP_CR
66 || prop == GBP_LF
67 || prop == GBP_CONTROL))
68 *p = 1;
69 /* No break between Hangul syllable sequences (GB6, GB7, GB8). */
70 else if ((last_char_prop == GBP_L
71 && (prop == GBP_L
72 || prop == GBP_V
73 || prop == GBP_LV
74 || prop == GBP_LVT))
75 || ((last_char_prop == GBP_LV
76 || last_char_prop == GBP_V)
77 && (prop == GBP_V
78 || prop == GBP_T))
79 || ((last_char_prop == GBP_LVT
80 || last_char_prop == GBP_T)
81 && prop == GBP_T))
82 /* *p = 0 */;
83 /* No break before extending characters or ZWJ (GB9). */
84 else if (prop == GBP_EXTEND || prop == GBP_ZWJ)
85 /* *p = 0 */;
86 /* No break before SpacingMarks (GB9a). */
87 else if (prop == GBP_SPACINGMARK)
88 /* *p = 0 */;
89 /* No break after Prepend characters (GB9b). */
90 else if (last_char_prop == GBP_PREPEND)
91 /* *p = 0 */;
92 /* No break within emoji modifier sequences (GB10). */
93 else if ((last_compchar_prop == GBP_EB
94 || last_compchar_prop == GBP_EBG)
95 && prop == GBP_EM)
96 /* *p = 0 */;
97 /* No break within emoji zwj sequences (GB11). */
98 else if (last_char_prop == GBP_ZWJ
99 && (prop == GBP_GAZ
100 || prop == GBP_EBG))
101 /* *p = 0 */;
102 /* No break between RI if there is an odd number of RI
103 characters before (GB12, GB13). */
104 else if (prop == GBP_RI)
105 {
106 if (ri_count % 2 == 0)
107 *p = 1;
108 /* else *p = 0; */
109 }
110 /* Break everywhere (GBP999). */
111 else
112 *p = 1;
113 }
114
115 last_char_prop = prop;
116
117 if (!(prop == GBP_EXTEND
118 && (last_compchar_prop == GBP_EB
119 || last_compchar_prop == GBP_EBG)))
120 last_compchar_prop = prop;
121
122 if (prop == GBP_RI)
123 ri_count++;
124 else
125 ri_count = 0;
126
127 s += count;
128 p += count;
129 }
130 }
131 }
132