2 * Copyright © 2011,2012,2014 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod
27 #ifndef HB_UTF_PRIVATE_HH
28 #define HB_UTF_PRIVATE_HH
30 #include "hb-private.hh"
35 typedef uint8_t codepoint_t
;
37 static inline const uint8_t *
38 next (const uint8_t *text
,
40 hb_codepoint_t
*unicode
,
41 hb_codepoint_t replacement
)
43 /* Written to only accept well-formed sequences.
44 * Based on ideas from ICU's U8_NEXT.
45 * Generates one "replacement" for each ill-formed byte. */
47 hb_codepoint_t c
= *text
++;
51 if (hb_in_range (c
, 0xC2u
, 0xDFu
)) /* Two-byte */
54 if (likely (text
< end
&&
55 (t1
= text
[0] - 0x80u
) <= 0x3Fu
))
57 c
= ((c
&0x1Fu
)<<6) | t1
;
63 else if (hb_in_range (c
, 0xE0u
, 0xEFu
)) /* Three-byte */
66 if (likely (1 < end
- text
&&
67 (t1
= text
[0] - 0x80u
) <= 0x3Fu
&&
68 (t2
= text
[1] - 0x80u
) <= 0x3Fu
))
70 c
= ((c
&0xFu
)<<12) | (t1
<<6) | t2
;
71 if (unlikely (c
< 0x0800u
|| hb_in_range (c
, 0xD800u
, 0xDFFFu
)))
78 else if (hb_in_range (c
, 0xF0u
, 0xF4u
)) /* Four-byte */
80 unsigned int t1
, t2
, t3
;
81 if (likely (2 < end
- text
&&
82 (t1
= text
[0] - 0x80u
) <= 0x3Fu
&&
83 (t2
= text
[1] - 0x80u
) <= 0x3Fu
&&
84 (t3
= text
[2] - 0x80u
) <= 0x3Fu
))
86 c
= ((c
&0x7u
)<<18) | (t1
<<12) | (t2
<<6) | t3
;
87 if (unlikely (!hb_in_range (c
, 0x10000u
, 0x10FFFFu
)))
102 *unicode
= replacement
;
106 static inline const uint8_t *
107 prev (const uint8_t *text
,
108 const uint8_t *start
,
109 hb_codepoint_t
*unicode
,
110 hb_codepoint_t replacement
)
112 const uint8_t *end
= text
--;
113 while (start
< text
&& (*text
& 0xc0) == 0x80 && end
- text
< 4)
116 if (likely (next (text
, end
, unicode
, replacement
) == end
))
119 *unicode
= replacement
;
123 static inline unsigned int
124 strlen (const uint8_t *text
)
126 return ::strlen ((const char *) text
);
133 typedef uint16_t codepoint_t
;
135 static inline const uint16_t *
136 next (const uint16_t *text
,
138 hb_codepoint_t
*unicode
,
139 hb_codepoint_t replacement
)
141 hb_codepoint_t c
= *text
++;
143 if (likely (!hb_in_range (c
, 0xD800u
, 0xDFFFu
)))
149 if (likely (hb_in_range (c
, 0xD800u
, 0xDBFFu
)))
151 /* High-surrogate in c */
153 if (text
< end
&& ((l
= *text
), likely (hb_in_range (l
, 0xDC00u
, 0xDFFFu
))))
155 /* Low-surrogate in l */
156 *unicode
= (c
<< 10) + l
- ((0xD800u
<< 10) - 0x10000u
+ 0xDC00u
);
162 /* Lonely / out-of-order surrogate. */
163 *unicode
= replacement
;
167 static inline const uint16_t *
168 prev (const uint16_t *text
,
169 const uint16_t *start
,
170 hb_codepoint_t
*unicode
,
171 hb_codepoint_t replacement
)
173 const uint16_t *end
= text
--;
174 hb_codepoint_t c
= *text
;
176 if (likely (!hb_in_range (c
, 0xD800u
, 0xDFFFu
)))
182 if (likely (start
< text
&& hb_in_range (c
, 0xDC00u
, 0xDFFFu
)))
185 if (likely (next (text
, end
, unicode
, replacement
) == end
))
188 *unicode
= replacement
;
193 static inline unsigned int
194 strlen (const uint16_t *text
)
203 template <bool validate
=true>
206 typedef uint32_t codepoint_t
;
208 static inline const uint32_t *
209 next (const uint32_t *text
,
210 const uint32_t *end HB_UNUSED
,
211 hb_codepoint_t
*unicode
,
212 hb_codepoint_t replacement
)
214 hb_codepoint_t c
= *text
++;
215 if (validate
&& unlikely (c
> 0x10FFFFu
|| hb_in_range (c
, 0xD800u
, 0xDFFFu
)))
221 *unicode
= replacement
;
225 static inline const uint32_t *
226 prev (const uint32_t *text
,
227 const uint32_t *start HB_UNUSED
,
228 hb_codepoint_t
*unicode
,
229 hb_codepoint_t replacement
)
231 next (text
- 1, text
, unicode
, replacement
);
235 static inline unsigned int
236 strlen (const uint32_t *text
)
247 typedef uint8_t codepoint_t
;
249 static inline const uint8_t *
250 next (const uint8_t *text
,
251 const uint8_t *end HB_UNUSED
,
252 hb_codepoint_t
*unicode
,
253 hb_codepoint_t replacement HB_UNUSED
)
259 static inline const uint8_t *
260 prev (const uint8_t *text
,
261 const uint8_t *start HB_UNUSED
,
262 hb_codepoint_t
*unicode
,
263 hb_codepoint_t replacement
)
269 static inline unsigned int
270 strlen (const uint8_t *text
)
278 #endif /* HB_UTF_PRIVATE_HH */