2012-01-13 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / go-int-array-to-string.c
blob1a37879f312c47642093d7d66a2efd1045b1892f
1 /* go-int-array-to-string.c -- convert an array of ints to a string in Go.
3 Copyright 2009 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. */
7 #include "go-assert.h"
8 #include "go-string.h"
9 #include "runtime.h"
10 #include "arch.h"
11 #include "malloc.h"
13 struct __go_string
14 __go_int_array_to_string (const void* p, int len)
16 const int *ints;
17 int slen;
18 int i;
19 unsigned char *retdata;
20 struct __go_string ret;
21 unsigned char *s;
23 ints = (const int *) p;
25 slen = 0;
26 for (i = 0; i < len; ++i)
28 int v;
30 v = ints[i];
32 if (v > 0x10ffff)
33 v = 0xfffd;
35 if (v <= 0x7f)
36 slen += 1;
37 else if (v <= 0x7ff)
38 slen += 2;
39 else if (v <= 0xffff)
40 slen += 3;
41 else
42 slen += 4;
45 retdata = runtime_mallocgc (slen, FlagNoPointers, 1, 0);
46 ret.__data = retdata;
47 ret.__length = slen;
49 s = retdata;
50 for (i = 0; i < len; ++i)
52 int v;
54 v = ints[i];
56 /* If V is out of range for UTF-8, substitute the replacement
57 character. */
58 if (v > 0x10ffff)
59 v = 0xfffd;
61 if (v <= 0x7f)
62 *s++ = v;
63 else if (v <= 0x7ff)
65 *s++ = 0xc0 | ((v >> 6) & 0x1f);
66 *s++ = 0x80 | (v & 0x3f);
68 else if (v <= 0xffff)
70 *s++ = 0xe0 | ((v >> 12) & 0xf);
71 *s++ = 0x80 | ((v >> 6) & 0x3f);
72 *s++ = 0x80 | (v & 0x3f);
74 else
76 *s++ = 0xf0 | ((v >> 18) & 0x7);
77 *s++ = 0x80 | ((v >> 12) & 0x3f);
78 *s++ = 0x80 | ((v >> 6) & 0x3f);
79 *s++ = 0x80 | (v & 0x3f);
83 __go_assert (s - retdata == slen);
85 return ret;