summaryrefslogtreecommitdiff
path: root/libgo/runtime/go-rune.c
diff options
context:
space:
mode:
authorupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
committerupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
commit554fd8c5195424bdbcabf5de30fdc183aba391bd (patch)
tree976dc5ab7fddf506dadce60ae936f43f58787092 /libgo/runtime/go-rune.c
downloadcbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.bz2
cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.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.c77
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;
+}