blob: bfacf1ed7ca90c814eb87d1c668f1b92a28a4213 [file] [log] [blame]
Kenny Rootba0165b2010-11-09 14:37:23 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Sergio Giro570dbfb2016-06-28 18:02:29 +010017#include <log/log.h>
Kenny Rootba0165b2010-11-09 14:37:23 -080018#include <utils/Unicode.h>
19
20#include <stddef.h>
Adam Vartanianf0a43de2017-08-14 15:51:29 +010021#include <limits.h>
Kenny Rootba0165b2010-11-09 14:37:23 -080022
Elliott Hughesadbf4422015-07-29 17:45:24 -070023#if defined(_WIN32)
Kenny Rootba0165b2010-11-09 14:37:23 -080024# undef nhtol
25# undef htonl
26# undef nhtos
27# undef htons
28
Elliott Hughes97ac0e12014-11-21 23:01:59 -080029# define ntohl(x) ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) )
30# define htonl(x) ntohl(x)
31# define ntohs(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) )
32# define htons(x) ntohs(x)
Kenny Rootba0165b2010-11-09 14:37:23 -080033#else
34# include <netinet/in.h>
35#endif
36
37extern "C" {
38
39static const char32_t kByteMask = 0x000000BF;
40static const char32_t kByteMark = 0x00000080;
41
42// Surrogates aren't valid for UTF-32 characters, so define some
43// constants that will let us screen them out.
44static const char32_t kUnicodeSurrogateHighStart = 0x0000D800;
Andreas Gampea53c8152014-11-24 09:42:07 -080045// Unused, here for completeness:
46// static const char32_t kUnicodeSurrogateHighEnd = 0x0000DBFF;
47// static const char32_t kUnicodeSurrogateLowStart = 0x0000DC00;
Kenny Rootba0165b2010-11-09 14:37:23 -080048static const char32_t kUnicodeSurrogateLowEnd = 0x0000DFFF;
49static const char32_t kUnicodeSurrogateStart = kUnicodeSurrogateHighStart;
50static const char32_t kUnicodeSurrogateEnd = kUnicodeSurrogateLowEnd;
51static const char32_t kUnicodeMaxCodepoint = 0x0010FFFF;
52
53// Mask used to set appropriate bits in first byte of UTF-8 sequence,
54// indexed by number of bytes in the sequence.
55// 0xxxxxxx
56// -> (00-7f) 7bit. Bit mask for the first byte is 0x00000000
57// 110yyyyx 10xxxxxx
58// -> (c0-df)(80-bf) 11bit. Bit mask is 0x000000C0
59// 1110yyyy 10yxxxxx 10xxxxxx
60// -> (e0-ef)(80-bf)(80-bf) 16bit. Bit mask is 0x000000E0
61// 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx
62// -> (f0-f7)(80-bf)(80-bf)(80-bf) 21bit. Bit mask is 0x000000F0
63static const char32_t kFirstByteMark[] = {
64 0x00000000, 0x00000000, 0x000000C0, 0x000000E0, 0x000000F0
65};
66
67// --------------------------------------------------------------------------
68// UTF-32
69// --------------------------------------------------------------------------
70
71/**
72 * Return number of UTF-8 bytes required for the character. If the character
73 * is invalid, return size of 0.
74 */
75static inline size_t utf32_codepoint_utf8_length(char32_t srcChar)
76{
77 // Figure out how many bytes the result will require.
78 if (srcChar < 0x00000080) {
79 return 1;
80 } else if (srcChar < 0x00000800) {
81 return 2;
82 } else if (srcChar < 0x00010000) {
83 if ((srcChar < kUnicodeSurrogateStart) || (srcChar > kUnicodeSurrogateEnd)) {
84 return 3;
85 } else {
86 // Surrogates are invalid UTF-32 characters.
87 return 0;
88 }
89 }
90 // Max code point for Unicode is 0x0010FFFF.
91 else if (srcChar <= kUnicodeMaxCodepoint) {
92 return 4;
93 } else {
94 // Invalid UTF-32 character.
95 return 0;
96 }
97}
98
99// Write out the source character to <dstP>.
100
101static inline void utf32_codepoint_to_utf8(uint8_t* dstP, char32_t srcChar, size_t bytes)
102{
103 dstP += bytes;
104 switch (bytes)
105 { /* note: everything falls through. */
106 case 4: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
107 case 3: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
108 case 2: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
109 case 1: *--dstP = (uint8_t)(srcChar | kFirstByteMark[bytes]);
110 }
111}
112
113size_t strlen32(const char32_t *s)
114{
115 const char32_t *ss = s;
116 while ( *ss )
117 ss++;
118 return ss-s;
119}
120
121size_t strnlen32(const char32_t *s, size_t maxlen)
122{
123 const char32_t *ss = s;
124 while ((maxlen > 0) && *ss) {
125 ss++;
126 maxlen--;
127 }
128 return ss-s;
129}
130
131static inline int32_t utf32_at_internal(const char* cur, size_t *num_read)
132{
133 const char first_char = *cur;
134 if ((first_char & 0x80) == 0) { // ASCII
135 *num_read = 1;
136 return *cur;
137 }
138 cur++;
139 char32_t mask, to_ignore_mask;
140 size_t num_to_read = 0;
141 char32_t utf32 = first_char;
142 for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0xFFFFFF80;
143 (first_char & mask);
144 num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
145 // 0x3F == 00111111
146 utf32 = (utf32 << 6) + (*cur++ & 0x3F);
147 }
148 to_ignore_mask |= mask;
149 utf32 &= ~(to_ignore_mask << (6 * (num_to_read - 1)));
150
151 *num_read = num_to_read;
152 return static_cast<int32_t>(utf32);
153}
154
155int32_t utf32_from_utf8_at(const char *src, size_t src_len, size_t index, size_t *next_index)
156{
157 if (index >= src_len) {
158 return -1;
159 }
160 size_t dummy_index;
161 if (next_index == NULL) {
162 next_index = &dummy_index;
163 }
164 size_t num_read;
165 int32_t ret = utf32_at_internal(src + index, &num_read);
166 if (ret >= 0) {
167 *next_index = index + num_read;
168 }
169
170 return ret;
171}
172
173ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len)
174{
175 if (src == NULL || src_len == 0) {
176 return -1;
177 }
178
179 size_t ret = 0;
180 const char32_t *end = src + src_len;
181 while (src < end) {
Adam Vartanianf0a43de2017-08-14 15:51:29 +0100182 size_t char_len = utf32_codepoint_utf8_length(*src++);
183 if (SSIZE_MAX - char_len < ret) {
184 // If this happens, we would overflow the ssize_t type when
185 // returning from this function, so we cannot express how
186 // long this string is in an ssize_t.
187 android_errorWriteLog(0x534e4554, "37723026");
188 return -1;
189 }
190 ret += char_len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800191 }
192 return ret;
193}
194
Sergio Giro570dbfb2016-06-28 18:02:29 +0100195void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst, size_t dst_len)
Kenny Rootba0165b2010-11-09 14:37:23 -0800196{
197 if (src == NULL || src_len == 0 || dst == NULL) {
198 return;
199 }
200
201 const char32_t *cur_utf32 = src;
202 const char32_t *end_utf32 = src + src_len;
203 char *cur = dst;
204 while (cur_utf32 < end_utf32) {
205 size_t len = utf32_codepoint_utf8_length(*cur_utf32);
Sergio Giro570dbfb2016-06-28 18:02:29 +0100206 LOG_ALWAYS_FATAL_IF(dst_len < len, "%zu < %zu", dst_len, len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800207 utf32_codepoint_to_utf8((uint8_t *)cur, *cur_utf32++, len);
208 cur += len;
Sergio Giro570dbfb2016-06-28 18:02:29 +0100209 dst_len -= len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800210 }
Sergio Giro570dbfb2016-06-28 18:02:29 +0100211 LOG_ALWAYS_FATAL_IF(dst_len < 1, "dst_len < 1: %zu < 1", dst_len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800212 *cur = '\0';
213}
214
215// --------------------------------------------------------------------------
216// UTF-16
217// --------------------------------------------------------------------------
218
219int strcmp16(const char16_t *s1, const char16_t *s2)
220{
221 char16_t ch;
222 int d = 0;
223
224 while ( 1 ) {
225 d = (int)(ch = *s1++) - (int)*s2++;
226 if ( d || !ch )
227 break;
228 }
229
230 return d;
231}
232
233int strncmp16(const char16_t *s1, const char16_t *s2, size_t n)
234{
235 char16_t ch;
236 int d = 0;
237
Michael Wright5bacef32016-05-09 14:43:31 +0100238 if (n == 0) {
239 return 0;
Kenny Rootba0165b2010-11-09 14:37:23 -0800240 }
241
Michael Wright5bacef32016-05-09 14:43:31 +0100242 do {
243 d = (int)(ch = *s1++) - (int)*s2++;
244 if ( d || !ch ) {
245 break;
246 }
247 } while (--n);
248
Kenny Rootba0165b2010-11-09 14:37:23 -0800249 return d;
250}
251
252char16_t *strcpy16(char16_t *dst, const char16_t *src)
253{
254 char16_t *q = dst;
255 const char16_t *p = src;
256 char16_t ch;
257
258 do {
259 *q++ = ch = *p++;
260 } while ( ch );
261
262 return dst;
263}
264
265size_t strlen16(const char16_t *s)
266{
267 const char16_t *ss = s;
268 while ( *ss )
269 ss++;
270 return ss-s;
271}
272
273
274char16_t *strncpy16(char16_t *dst, const char16_t *src, size_t n)
275{
276 char16_t *q = dst;
277 const char16_t *p = src;
278 char ch;
279
280 while (n) {
281 n--;
282 *q++ = ch = *p++;
283 if ( !ch )
284 break;
285 }
286
287 *q = 0;
288
289 return dst;
290}
291
292size_t strnlen16(const char16_t *s, size_t maxlen)
293{
294 const char16_t *ss = s;
295
296 /* Important: the maxlen test must precede the reference through ss;
297 since the byte beyond the maximum may segfault */
298 while ((maxlen > 0) && *ss) {
299 ss++;
300 maxlen--;
301 }
302 return ss-s;
303}
304
Michael Wright5bacef32016-05-09 14:43:31 +0100305char16_t* strstr16(const char16_t* src, const char16_t* target)
306{
307 const char16_t needle = *target++;
Michael Wright0fd60eb2016-05-16 21:23:07 +0100308 const size_t target_len = strlen16(target);
Michael Wright5bacef32016-05-09 14:43:31 +0100309 if (needle != '\0') {
310 do {
311 do {
312 if (*src == '\0') {
313 return nullptr;
314 }
315 } while (*src++ != needle);
Michael Wright0fd60eb2016-05-16 21:23:07 +0100316 } while (strncmp16(src, target, target_len) != 0);
Michael Wright5bacef32016-05-09 14:43:31 +0100317 src--;
318 }
319
320 return (char16_t*)src;
321}
322
323
Kenny Rootba0165b2010-11-09 14:37:23 -0800324int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2)
325{
326 const char16_t* e1 = s1+n1;
327 const char16_t* e2 = s2+n2;
328
329 while (s1 < e1 && s2 < e2) {
330 const int d = (int)*s1++ - (int)*s2++;
331 if (d) {
332 return d;
333 }
334 }
335
336 return n1 < n2
337 ? (0 - (int)*s2)
338 : (n1 > n2
339 ? ((int)*s1 - 0)
340 : 0);
341}
342
343int strzcmp16_h_n(const char16_t *s1H, size_t n1, const char16_t *s2N, size_t n2)
344{
345 const char16_t* e1 = s1H+n1;
346 const char16_t* e2 = s2N+n2;
347
348 while (s1H < e1 && s2N < e2) {
349 const char16_t c2 = ntohs(*s2N);
350 const int d = (int)*s1H++ - (int)c2;
351 s2N++;
352 if (d) {
353 return d;
354 }
355 }
356
357 return n1 < n2
358 ? (0 - (int)ntohs(*s2N))
359 : (n1 > n2
360 ? ((int)*s1H - 0)
361 : 0);
362}
363
Sergio Giro570dbfb2016-06-28 18:02:29 +0100364void utf16_to_utf8(const char16_t* src, size_t src_len, char* dst, size_t dst_len)
Kenny Rootba0165b2010-11-09 14:37:23 -0800365{
366 if (src == NULL || src_len == 0 || dst == NULL) {
367 return;
368 }
369
370 const char16_t* cur_utf16 = src;
371 const char16_t* const end_utf16 = src + src_len;
372 char *cur = dst;
373 while (cur_utf16 < end_utf16) {
374 char32_t utf32;
375 // surrogate pairs
Cylen Yao72299bf2014-06-04 19:11:27 +0800376 if((*cur_utf16 & 0xFC00) == 0xD800 && (cur_utf16 + 1) < end_utf16
377 && (*(cur_utf16 + 1) & 0xFC00) == 0xDC00) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800378 utf32 = (*cur_utf16++ - 0xD800) << 10;
379 utf32 |= *cur_utf16++ - 0xDC00;
380 utf32 += 0x10000;
381 } else {
382 utf32 = (char32_t) *cur_utf16++;
383 }
384 const size_t len = utf32_codepoint_utf8_length(utf32);
Sergio Giro570dbfb2016-06-28 18:02:29 +0100385 LOG_ALWAYS_FATAL_IF(dst_len < len, "%zu < %zu", dst_len, len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800386 utf32_codepoint_to_utf8((uint8_t*)cur, utf32, len);
387 cur += len;
Sergio Giro570dbfb2016-06-28 18:02:29 +0100388 dst_len -= len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800389 }
Sergio Giro570dbfb2016-06-28 18:02:29 +0100390 LOG_ALWAYS_FATAL_IF(dst_len < 1, "%zu < 1", dst_len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800391 *cur = '\0';
392}
393
394// --------------------------------------------------------------------------
395// UTF-8
396// --------------------------------------------------------------------------
397
398ssize_t utf8_length(const char *src)
399{
400 const char *cur = src;
401 size_t ret = 0;
402 while (*cur != '\0') {
403 const char first_char = *cur++;
404 if ((first_char & 0x80) == 0) { // ASCII
405 ret += 1;
406 continue;
407 }
408 // (UTF-8's character must not be like 10xxxxxx,
409 // but 110xxxxx, 1110xxxx, ... or 1111110x)
410 if ((first_char & 0x40) == 0) {
411 return -1;
412 }
413
414 int32_t mask, to_ignore_mask;
415 size_t num_to_read = 0;
416 char32_t utf32 = 0;
417 for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80;
418 num_to_read < 5 && (first_char & mask);
419 num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
420 if ((*cur & 0xC0) != 0x80) { // must be 10xxxxxx
421 return -1;
422 }
423 // 0x3F == 00111111
424 utf32 = (utf32 << 6) + (*cur++ & 0x3F);
425 }
426 // "first_char" must be (110xxxxx - 11110xxx)
427 if (num_to_read == 5) {
428 return -1;
429 }
430 to_ignore_mask |= mask;
431 utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1));
432 if (utf32 > kUnicodeMaxCodepoint) {
433 return -1;
434 }
435
436 ret += num_to_read;
437 }
438 return ret;
439}
440
441ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len)
442{
443 if (src == NULL || src_len == 0) {
444 return -1;
445 }
446
447 size_t ret = 0;
448 const char16_t* const end = src + src_len;
449 while (src < end) {
Adam Vartanianf0a43de2017-08-14 15:51:29 +0100450 size_t char_len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800451 if ((*src & 0xFC00) == 0xD800 && (src + 1) < end
Sergio Giro570dbfb2016-06-28 18:02:29 +0100452 && (*(src + 1) & 0xFC00) == 0xDC00) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800453 // surrogate pairs are always 4 bytes.
Adam Vartanianf0a43de2017-08-14 15:51:29 +0100454 char_len = 4;
Sergio Giro570dbfb2016-06-28 18:02:29 +0100455 src += 2;
Kenny Rootba0165b2010-11-09 14:37:23 -0800456 } else {
Adam Vartanianf0a43de2017-08-14 15:51:29 +0100457 char_len = utf32_codepoint_utf8_length((char32_t)*src++);
Kenny Rootba0165b2010-11-09 14:37:23 -0800458 }
Adam Vartanianf0a43de2017-08-14 15:51:29 +0100459 if (SSIZE_MAX - char_len < ret) {
460 // If this happens, we would overflow the ssize_t type when
461 // returning from this function, so we cannot express how
462 // long this string is in an ssize_t.
463 android_errorWriteLog(0x534e4554, "37723026");
464 return -1;
465 }
466 ret += char_len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800467 }
468 return ret;
469}
470
471/**
472 * Returns 1-4 based on the number of leading bits.
473 *
474 * 1111 -> 4
475 * 1110 -> 3
476 * 110x -> 2
477 * 10xx -> 1
478 * 0xxx -> 1
479 */
480static inline size_t utf8_codepoint_len(uint8_t ch)
481{
482 return ((0xe5000000 >> ((ch >> 3) & 0x1e)) & 3) + 1;
483}
484
485static inline void utf8_shift_and_mask(uint32_t* codePoint, const uint8_t byte)
486{
487 *codePoint <<= 6;
488 *codePoint |= 0x3F & byte;
489}
490
491size_t utf8_to_utf32_length(const char *src, size_t src_len)
492{
493 if (src == NULL || src_len == 0) {
494 return 0;
495 }
496 size_t ret = 0;
497 const char* cur;
498 const char* end;
499 size_t num_to_skip;
500 for (cur = src, end = src + src_len, num_to_skip = 1;
501 cur < end;
502 cur += num_to_skip, ret++) {
503 const char first_char = *cur;
504 num_to_skip = 1;
505 if ((first_char & 0x80) == 0) { // ASCII
506 continue;
507 }
508 int32_t mask;
509
510 for (mask = 0x40; (first_char & mask); num_to_skip++, mask >>= 1) {
511 }
512 }
513 return ret;
514}
515
516void utf8_to_utf32(const char* src, size_t src_len, char32_t* dst)
517{
518 if (src == NULL || src_len == 0 || dst == NULL) {
519 return;
520 }
521
522 const char* cur = src;
523 const char* const end = src + src_len;
524 char32_t* cur_utf32 = dst;
525 while (cur < end) {
526 size_t num_read;
527 *cur_utf32++ = static_cast<char32_t>(utf32_at_internal(cur, &num_read));
528 cur += num_read;
529 }
530 *cur_utf32 = 0;
531}
532
533static inline uint32_t utf8_to_utf32_codepoint(const uint8_t *src, size_t length)
534{
535 uint32_t unicode;
536
537 switch (length)
538 {
539 case 1:
540 return src[0];
541 case 2:
542 unicode = src[0] & 0x1f;
543 utf8_shift_and_mask(&unicode, src[1]);
544 return unicode;
545 case 3:
546 unicode = src[0] & 0x0f;
547 utf8_shift_and_mask(&unicode, src[1]);
548 utf8_shift_and_mask(&unicode, src[2]);
549 return unicode;
550 case 4:
551 unicode = src[0] & 0x07;
552 utf8_shift_and_mask(&unicode, src[1]);
553 utf8_shift_and_mask(&unicode, src[2]);
554 utf8_shift_and_mask(&unicode, src[3]);
555 return unicode;
556 default:
557 return 0xffff;
558 }
559
560 //printf("Char at %p: len=%d, utf-16=%p\n", src, length, (void*)result);
561}
562
563ssize_t utf8_to_utf16_length(const uint8_t* u8str, size_t u8len)
564{
565 const uint8_t* const u8end = u8str + u8len;
566 const uint8_t* u8cur = u8str;
567
568 /* Validate that the UTF-8 is the correct len */
569 size_t u16measuredLen = 0;
570 while (u8cur < u8end) {
571 u16measuredLen++;
572 int u8charLen = utf8_codepoint_len(*u8cur);
573 uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8charLen);
574 if (codepoint > 0xFFFF) u16measuredLen++; // this will be a surrogate pair in utf16
575 u8cur += u8charLen;
576 }
577
578 /**
579 * Make sure that we ended where we thought we would and the output UTF-16
580 * will be exactly how long we were told it would be.
581 */
582 if (u8cur != u8end) {
583 return -1;
584 }
585
586 return u16measuredLen;
587}
588
Jeff Brownaa983c92011-10-07 13:28:18 -0700589char16_t* utf8_to_utf16_no_null_terminator(const uint8_t* u8str, size_t u8len, char16_t* u16str)
Kenny Rootba0165b2010-11-09 14:37:23 -0800590{
591 const uint8_t* const u8end = u8str + u8len;
592 const uint8_t* u8cur = u8str;
593 char16_t* u16cur = u16str;
594
595 while (u8cur < u8end) {
596 size_t u8len = utf8_codepoint_len(*u8cur);
597 uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8len);
598
599 // Convert the UTF32 codepoint to one or more UTF16 codepoints
600 if (codepoint <= 0xFFFF) {
601 // Single UTF16 character
602 *u16cur++ = (char16_t) codepoint;
603 } else {
604 // Multiple UTF16 characters with surrogates
605 codepoint = codepoint - 0x10000;
606 *u16cur++ = (char16_t) ((codepoint >> 10) + 0xD800);
607 *u16cur++ = (char16_t) ((codepoint & 0x3FF) + 0xDC00);
608 }
609
610 u8cur += u8len;
611 }
Jeff Brownaa983c92011-10-07 13:28:18 -0700612 return u16cur;
613}
614
615void utf8_to_utf16(const uint8_t* u8str, size_t u8len, char16_t* u16str) {
616 char16_t* end = utf8_to_utf16_no_null_terminator(u8str, u8len, u16str);
617 *end = 0;
Kenny Rootba0165b2010-11-09 14:37:23 -0800618}
619
Dianne Hackborn0f10d0a2013-07-31 16:04:39 -0700620char16_t* utf8_to_utf16_n(const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen) {
621 const uint8_t* const u8end = src + srcLen;
622 const uint8_t* u8cur = src;
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700623 const char16_t* const u16end = dst + dstLen;
Dianne Hackborn0f10d0a2013-07-31 16:04:39 -0700624 char16_t* u16cur = dst;
625
626 while (u8cur < u8end && u16cur < u16end) {
627 size_t u8len = utf8_codepoint_len(*u8cur);
628 uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8len);
629
630 // Convert the UTF32 codepoint to one or more UTF16 codepoints
631 if (codepoint <= 0xFFFF) {
632 // Single UTF16 character
633 *u16cur++ = (char16_t) codepoint;
634 } else {
635 // Multiple UTF16 characters with surrogates
636 codepoint = codepoint - 0x10000;
637 *u16cur++ = (char16_t) ((codepoint >> 10) + 0xD800);
638 if (u16cur >= u16end) {
639 // Ooops... not enough room for this surrogate pair.
640 return u16cur-1;
641 }
642 *u16cur++ = (char16_t) ((codepoint & 0x3FF) + 0xDC00);
643 }
644
645 u8cur += u8len;
646 }
647 return u16cur;
648}
649
Kenny Rootba0165b2010-11-09 14:37:23 -0800650}