OLD | NEW |
1 /* | 1 /* |
2 * Copyright © 2011,2012,2014 Google, Inc. | 2 * Copyright © 2011,2012,2014 Google, Inc. |
3 * | 3 * |
4 * This is part of HarfBuzz, a text shaping library. | 4 * This is part of HarfBuzz, a text shaping library. |
5 * | 5 * |
6 * Permission is hereby granted, without written agreement and without | 6 * Permission is hereby granted, without written agreement and without |
7 * license or royalty fees, to use, copy, modify, and distribute this | 7 * license or royalty fees, to use, copy, modify, and distribute this |
8 * software and its documentation for any purpose, provided that the | 8 * software and its documentation for any purpose, provided that the |
9 * above copyright notice and the following two paragraphs appear in | 9 * above copyright notice and the following two paragraphs appear in |
10 * all copies of this software. | 10 * all copies of this software. |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 hb_codepoint_t replacement) | 139 hb_codepoint_t replacement) |
140 { | 140 { |
141 hb_codepoint_t c = *text++; | 141 hb_codepoint_t c = *text++; |
142 | 142 |
143 if (likely (!hb_in_range (c, 0xD800u, 0xDFFFu))) | 143 if (likely (!hb_in_range (c, 0xD800u, 0xDFFFu))) |
144 { | 144 { |
145 *unicode = c; | 145 *unicode = c; |
146 return text; | 146 return text; |
147 } | 147 } |
148 | 148 |
149 if (likely (hb_in_range (c, 0xD800u, 0xDBFFu))) | 149 if (likely (c <= 0xDBFFu && text < end)) |
150 { | 150 { |
151 /* High-surrogate in c */ | 151 /* High-surrogate in c */ |
152 hb_codepoint_t l; | 152 hb_codepoint_t l = *text; |
153 if (text < end && ((l = *text), likely (hb_in_range (l, 0xDC00u, 0xDFFFu))
)) | 153 if (likely (hb_in_range (l, 0xDC00u, 0xDFFFu))) |
154 { | 154 { |
155 /* Low-surrogate in l */ | 155 /* Low-surrogate in l */ |
156 *unicode = (c << 10) + l - ((0xD800u << 10) - 0x10000u + 0xDC00u); | 156 *unicode = (c << 10) + l - ((0xD800u << 10) - 0x10000u + 0xDC00u); |
157 text++; | 157 text++; |
158 return text; | 158 return text; |
159 } | 159 } |
160 } | 160 } |
161 | 161 |
162 /* Lonely / out-of-order surrogate. */ | 162 /* Lonely / out-of-order surrogate. */ |
163 *unicode = replacement; | 163 *unicode = replacement; |
164 return text; | 164 return text; |
165 } | 165 } |
166 | 166 |
167 static inline const uint16_t * | 167 static inline const uint16_t * |
168 prev (const uint16_t *text, | 168 prev (const uint16_t *text, |
169 const uint16_t *start, | 169 const uint16_t *start, |
170 hb_codepoint_t *unicode, | 170 hb_codepoint_t *unicode, |
171 hb_codepoint_t replacement) | 171 hb_codepoint_t replacement) |
172 { | 172 { |
173 const uint16_t *end = text--; | 173 hb_codepoint_t c = *--text; |
174 hb_codepoint_t c = *text; | |
175 | 174 |
176 if (likely (!hb_in_range (c, 0xD800u, 0xDFFFu))) | 175 if (likely (!hb_in_range (c, 0xD800u, 0xDFFFu))) |
177 { | 176 { |
178 *unicode = c; | 177 *unicode = c; |
179 return text; | 178 return text; |
180 } | 179 } |
181 | 180 |
182 if (likely (start < text && hb_in_range (c, 0xDC00u, 0xDFFFu))) | 181 if (likely (c >= 0xDC00u && start < text)) |
183 text--; | 182 { |
| 183 /* Low-surrogate in c */ |
| 184 hb_codepoint_t h = text[-1]; |
| 185 if (likely (hb_in_range (h, 0xD800u, 0xDBFFu))) |
| 186 { |
| 187 /* High-surrogate in h */ |
| 188 *unicode = (h << 10) + c - ((0xD800u << 10) - 0x10000u + 0xDC00u); |
| 189 text--; |
| 190 return text; |
| 191 } |
| 192 } |
184 | 193 |
185 if (likely (next (text, end, unicode, replacement) == end)) | 194 /* Lonely / out-of-order surrogate. */ |
186 return text; | |
187 | |
188 *unicode = replacement; | 195 *unicode = replacement; |
189 return end - 1; | 196 return text; |
190 } | 197 } |
191 | 198 |
192 | 199 |
193 static inline unsigned int | 200 static inline unsigned int |
194 strlen (const uint16_t *text) | 201 strlen (const uint16_t *text) |
195 { | 202 { |
196 unsigned int l = 0; | 203 unsigned int l = 0; |
197 while (*text++) l++; | 204 while (*text++) l++; |
198 return l; | 205 return l; |
199 } | 206 } |
200 }; | 207 }; |
201 | 208 |
202 | 209 |
203 template <bool validate=true> | 210 template <bool validate=true> |
204 struct hb_utf32_t | 211 struct hb_utf32_t |
205 { | 212 { |
206 typedef uint32_t codepoint_t; | 213 typedef uint32_t codepoint_t; |
207 | 214 |
208 static inline const uint32_t * | 215 static inline const uint32_t * |
209 next (const uint32_t *text, | 216 next (const uint32_t *text, |
210 const uint32_t *end HB_UNUSED, | 217 const uint32_t *end HB_UNUSED, |
211 hb_codepoint_t *unicode, | 218 hb_codepoint_t *unicode, |
212 hb_codepoint_t replacement) | 219 hb_codepoint_t replacement) |
213 { | 220 { |
214 hb_codepoint_t c = *text++; | 221 hb_codepoint_t c = *unicode = *text++; |
215 if (validate && unlikely (c > 0x10FFFFu || hb_in_range (c, 0xD800u, 0xDFFFu)
)) | 222 if (validate && unlikely (c >= 0xD800u && (c <= 0xDFFFu || c > 0x10FFFFu))) |
216 goto error; | 223 *unicode = replacement; |
217 *unicode = c; | |
218 return text; | |
219 | |
220 error: | |
221 *unicode = replacement; | |
222 return text; | 224 return text; |
223 } | 225 } |
224 | 226 |
225 static inline const uint32_t * | 227 static inline const uint32_t * |
226 prev (const uint32_t *text, | 228 prev (const uint32_t *text, |
227 const uint32_t *start HB_UNUSED, | 229 const uint32_t *start HB_UNUSED, |
228 hb_codepoint_t *unicode, | 230 hb_codepoint_t *unicode, |
229 hb_codepoint_t replacement) | 231 hb_codepoint_t replacement) |
230 { | 232 { |
231 next (text - 1, text, unicode, replacement); | 233 hb_codepoint_t c = *unicode = *--text; |
232 return text - 1; | 234 if (validate && unlikely (c >= 0xD800u && (c <= 0xDFFFu || c > 0x10FFFFu))) |
| 235 *unicode = replacement; |
| 236 return text; |
233 } | 237 } |
234 | 238 |
235 static inline unsigned int | 239 static inline unsigned int |
236 strlen (const uint32_t *text) | 240 strlen (const uint32_t *text) |
237 { | 241 { |
238 unsigned int l = 0; | 242 unsigned int l = 0; |
239 while (*text++) l++; | 243 while (*text++) l++; |
240 return l; | 244 return l; |
241 } | 245 } |
242 }; | 246 }; |
(...skipping 26 matching lines...) Expand all Loading... |
269 static inline unsigned int | 273 static inline unsigned int |
270 strlen (const uint8_t *text) | 274 strlen (const uint8_t *text) |
271 { | 275 { |
272 unsigned int l = 0; | 276 unsigned int l = 0; |
273 while (*text++) l++; | 277 while (*text++) l++; |
274 return l; | 278 return l; |
275 } | 279 } |
276 }; | 280 }; |
277 | 281 |
278 #endif /* HB_UTF_PRIVATE_HH */ | 282 #endif /* HB_UTF_PRIVATE_HH */ |
OLD | NEW |