1 /* Search character in UTF-8 string.
2 Copyright (C) 1999, 2002, 2006-2007, 2009-2021 Free Software Foundation,
3 Inc.
4 Written by Bruno Haible <bruno@clisp.org>, 2002.
5
6 This file is free software.
7 It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
8 You can redistribute it and/or modify it under either
9 - the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 3, or (at your
11 option) any later version, or
12 - the terms of the GNU General Public License as published by the
13 Free Software Foundation; either version 2, or (at your option)
14 any later version, or
15 - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
16
17 This file is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License and the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License and of the GNU General Public License along with this
25 program. If not, see <https://www.gnu.org/licenses/>. */
26
27 #include <config.h>
28
29 /* Specification. */
30 #include "unistr.h"
31
32 uint8_t *
33 u8_strrchr (const uint8_t *s, ucs4_t uc)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
34 {
35 /* Calling u8_strlen and then searching from the other end would cause more
36 memory accesses. Avoid that, at the cost of a few more comparisons. */
37 uint8_t *result = NULL;
38 uint8_t c[6];
39
40 if (uc < 0x80)
41 {
42 uint8_t c0 = uc;
43
44 for (;; s++)
45 {
46 if (*s == c0)
47 result = (uint8_t *) s;
48 if (*s == 0)
49 break;
50 }
51 }
52 else
53 switch (u8_uctomb_aux (c, uc, 6))
54 {
55 case 2:
56 if (*s)
57 {
58 uint8_t c0 = c[0];
59 uint8_t c1 = c[1];
60
61 /* FIXME: Maybe walking the string via u8_mblen is a win? */
62 for (;; s++)
63 {
64 if (s[1] == 0)
65 break;
66 if (*s == c0 && s[1] == c1)
67 result = (uint8_t *) s;
68 }
69 }
70 break;
71
72 case 3:
73 if (*s && s[1])
74 {
75 uint8_t c0 = c[0];
76 uint8_t c1 = c[1];
77 uint8_t c2 = c[2];
78
79 /* FIXME: Maybe walking the string via u8_mblen is a win? */
80 for (;; s++)
81 {
82 if (s[2] == 0)
83 break;
84 if (*s == c0 && s[1] == c1 && s[2] == c2)
85 result = (uint8_t *) s;
86 }
87 }
88 break;
89
90 case 4:
91 if (*s && s[1] && s[2])
92 {
93 uint8_t c0 = c[0];
94 uint8_t c1 = c[1];
95 uint8_t c2 = c[2];
96 uint8_t c3 = c[3];
97
98 /* FIXME: Maybe walking the string via u8_mblen is a win? */
99 for (;; s++)
100 {
101 if (s[3] == 0)
102 break;
103 if (*s == c0 && s[1] == c1 && s[2] == c2 && s[3] == c3)
104 result = (uint8_t *) s;
105 }
106 }
107 break;
108 }
109 return result;
110 }