1 /* Case-mapping context of suffix UTF-8/UTF-16/UTF-32 string.
2 Copyright (C) 2009-2021 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2009.
4
5 This file is free software.
6 It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
7 You can redistribute it and/or modify it under either
8 - the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version, or
11 - the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option)
13 any later version, or
14 - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
15
16 This file is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License and the GNU General Public License
20 for more details.
21
22 You should have received a copy of the GNU Lesser General Public
23 License and of the GNU General Public License along with this
24 program. If not, see <https://www.gnu.org/licenses/>. */
25
26 casing_suffix_context_t
27 FUNC1 (const UNIT *s, size_t n)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
28 {
29 return FUNC2 (s, n, unicase_empty_suffix_context);
30 }
31
32 casing_suffix_context_t
33 FUNC2 (const UNIT *s, size_t n, casing_suffix_context_t a_context)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
34 {
35 casing_suffix_context_t context;
36 /* Evaluate all three conditions in a single pass through the string S.
37 The three variables are -1 as long as the value of the condition has
38 not been determined. */
39 ucs4_t first_char_except_ignorable = (ucs4_t)(-1);
40 int scc_MORE_ABOVE = -1;
41 int scc_BEFORE_DOT = -1;
42 const UNIT *s_end = s + n;
43
44 while (s < s_end)
45 {
46 ucs4_t uc;
47 int count = U_MBTOUC_UNSAFE (&uc, s, s_end - s);
48
49 if (first_char_except_ignorable == (ucs4_t)(-1))
50 {
51 if (!uc_is_case_ignorable (uc))
52 first_char_except_ignorable = uc;
53 }
54
55 if (scc_MORE_ABOVE < 0)
56 {
57 int ccc = uc_combining_class (uc);
58 if (ccc == UC_CCC_A)
59 scc_MORE_ABOVE = SCC_MORE_ABOVE_MASK;
60 else if (ccc == UC_CCC_NR)
61 scc_MORE_ABOVE = 0;
62 }
63
64 if (scc_BEFORE_DOT < 0)
65 {
66 if (uc == 0x0307) /* COMBINING DOT ABOVE */
67 scc_BEFORE_DOT = SCC_BEFORE_DOT_MASK;
68 else
69 {
70 int ccc = uc_combining_class (uc);
71 if (ccc == UC_CCC_A || ccc == UC_CCC_NR)
72 scc_BEFORE_DOT = 0;
73 }
74 }
75
76 if (first_char_except_ignorable != (ucs4_t)(-1)
77 && (scc_MORE_ABOVE | scc_BEFORE_DOT) >= 0)
78 /* All conditions have been determined. */
79 break;
80
81 s += count;
82 }
83
84 /* For those conditions that have not been determined so far, use the
85 value from the argument context. */
86 context.first_char_except_ignorable =
87 (first_char_except_ignorable != (ucs4_t)(-1)
88 ? first_char_except_ignorable
89 : a_context.first_char_except_ignorable);
90 context.bits =
91 (scc_MORE_ABOVE >= 0
92 ? scc_MORE_ABOVE
93 : a_context.bits & SCC_MORE_ABOVE_MASK)
94 | (scc_BEFORE_DOT >= 0
95 ? scc_BEFORE_DOT
96 : a_context.bits & SCC_BEFORE_DOT_MASK);
97 return context;
98 }