runtime: runtime.Caller should succeed even without debug info.
[gcc.git] / libgo / runtime / go-int-array-to-string.c
1 /* go-int-array-to-string.c -- convert an array of ints to a string in Go.
2
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. */
6
7 #include "go-assert.h"
8 #include "go-string.h"
9 #include "runtime.h"
10 #include "arch.h"
11 #include "malloc.h"
12
13 struct __go_string
14 __go_int_array_to_string (const void* p, int len)
15 {
16 const int *ints;
17 int slen;
18 int i;
19 unsigned char *retdata;
20 struct __go_string ret;
21 unsigned char *s;
22
23 ints = (const int *) p;
24
25 slen = 0;
26 for (i = 0; i < len; ++i)
27 {
28 int v;
29
30 v = ints[i];
31
32 if (v > 0x10ffff)
33 v = 0xfffd;
34
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;
43 }
44
45 retdata = runtime_mallocgc (slen, FlagNoPointers, 1, 0);
46 ret.__data = retdata;
47 ret.__length = slen;
48
49 s = retdata;
50 for (i = 0; i < len; ++i)
51 {
52 int v;
53
54 v = ints[i];
55
56 /* If V is out of range for UTF-8, substitute the replacement
57 character. */
58 if (v > 0x10ffff)
59 v = 0xfffd;
60
61 if (v <= 0x7f)
62 *s++ = v;
63 else if (v <= 0x7ff)
64 {
65 *s++ = 0xc0 | ((v >> 6) & 0x1f);
66 *s++ = 0x80 | (v & 0x3f);
67 }
68 else if (v <= 0xffff)
69 {
70 *s++ = 0xe0 | ((v >> 12) & 0xf);
71 *s++ = 0x80 | ((v >> 6) & 0x3f);
72 *s++ = 0x80 | (v & 0x3f);
73 }
74 else
75 {
76 *s++ = 0xf0 | ((v >> 18) & 0x7);
77 *s++ = 0x80 | ((v >> 12) & 0x3f);
78 *s++ = 0x80 | ((v >> 6) & 0x3f);
79 *s++ = 0x80 | (v & 0x3f);
80 }
81 }
82
83 __go_assert (s - retdata == slen);
84
85 return ret;
86 }