1 /* go-rune.c -- rune functions for Go.
3 Copyright 2009, 2010 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
10 #include "go-string.h"
12 /* Get a character from the UTF-8 string STR, of length LEN. Store
13 the Unicode character, if any, in *RUNE. Return the number of
14 characters used from STR. */
17 __go_get_rune (const unsigned char *str
, size_t len
, int32
*rune
)
21 /* Default to the "replacement character". */
38 if ((c
& 0xe0) == 0xc0
39 && (c1
& 0xc0) == 0x80)
41 l
= (((c
& 0x1f) << 6) + (c1
& 0x3f));
52 if ((c
& 0xf0) == 0xe0
53 && (c1
& 0xc0) == 0x80
54 && (c2
& 0xc0) == 0x80)
56 l
= (((c
& 0xf) << 12)
63 if (l
>= 0xd800 && l
< 0xe000)
65 /* Invalid surrogate half; return replace character. */
78 if ((c
& 0xf8) == 0xf0
79 && (c1
& 0xc0) == 0x80
80 && (c2
& 0xc0) == 0x80
81 && (c3
& 0xc0) == 0x80)
83 l
= (((c
& 0x7) << 18)
88 if (l
<= 0xffff || l
> 0x10ffff)
95 /* Invalid encoding. Return 1 so that we advance. */