diff options
author | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
---|---|---|
committer | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
commit | 554fd8c5195424bdbcabf5de30fdc183aba391bd (patch) | |
tree | 976dc5ab7fddf506dadce60ae936f43f58787092 /libgo/runtime/go-rune.c | |
download | cbb-gcc-4.6.4-15d2061ac0796199866debe9ac87130894b0cdd3.tar.bz2 cbb-gcc-4.6.4-15d2061ac0796199866debe9ac87130894b0cdd3.tar.xz |
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig;
imported gcc-4.6.4 source tree from verified upstream tarball.
downloading a git-generated archive based on the 'upstream' tag
should provide you with a source tree that is binary identical
to the one extracted from the above tarball.
if you have obtained the source via the command 'git clone',
however, do note that line-endings of files in your working
directory might differ from line-endings of the respective
files in the upstream repository.
Diffstat (limited to 'libgo/runtime/go-rune.c')
-rw-r--r-- | libgo/runtime/go-rune.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/libgo/runtime/go-rune.c b/libgo/runtime/go-rune.c new file mode 100644 index 000000000..7e31eb8d6 --- /dev/null +++ b/libgo/runtime/go-rune.c @@ -0,0 +1,77 @@ +/* go-rune.c -- rune functions for Go. + + Copyright 2009, 2010 The Go Authors. All rights reserved. + Use of this source code is governed by a BSD-style + license that can be found in the LICENSE file. */ + +#include <stddef.h> + +#include "go-string.h" + +/* Get a character from the UTF-8 string STR, of length LEN. Store + the Unicode character, if any, in *RUNE. Return the number of + characters used from STR. */ + +int +__go_get_rune (const unsigned char *str, size_t len, int *rune) +{ + int c, c1, c2, c3; + + /* Default to the "replacement character". */ + *rune = 0xfffd; + + if (len <= 0) + return 1; + + c = *str; + if (c <= 0x7f) + { + *rune = c; + return 1; + } + + if (len <= 1) + return 1; + + c1 = str[1]; + if ((c & 0xe0) == 0xc0 + && (c1 & 0xc0) == 0x80) + { + *rune = (((c & 0x1f) << 6) + + (c1 & 0x3f)); + return 2; + } + + if (len <= 2) + return 1; + + c2 = str[2]; + if ((c & 0xf0) == 0xe0 + && (c1 & 0xc0) == 0x80 + && (c2 & 0xc0) == 0x80) + { + *rune = (((c & 0xf) << 12) + + ((c1 & 0x3f) << 6) + + (c2 & 0x3f)); + return 3; + } + + if (len <= 3) + return 1; + + c3 = str[3]; + if ((c & 0xf8) == 0xf0 + && (c1 & 0xc0) == 0x80 + && (c2 & 0xc0) == 0x80 + && (c3 & 0xc0) == 0x80) + { + *rune = (((c & 0x7) << 18) + + ((c1 & 0x3f) << 12) + + ((c2 & 0x3f) << 6) + + (c3 & 0x3f)); + return 4; + } + + /* Invalid encoding. Return 1 so that we advance. */ + return 1; +} |