PR c++/63283
[official-gcc.git] / libgo / runtime / go-string-to-int-array.c
blob554688913101acb0021298c28220da0ff3087328
1 /* go-string-to-int-array.c -- convert a string to an array of ints in Go.
3 Copyright 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. */
7 #include "runtime.h"
8 #include "go-alloc.h"
9 #include "go-string.h"
10 #include "array.h"
11 #include "arch.h"
12 #include "malloc.h"
14 struct __go_open_array
15 __go_string_to_int_array (String str)
17 size_t c;
18 const unsigned char *p;
19 const unsigned char *pend;
20 uintptr mem;
21 uint32_t *data;
22 uint32_t *pd;
23 struct __go_open_array ret;
25 c = 0;
26 p = str.str;
27 pend = p + str.len;
28 while (p < pend)
30 int rune;
32 ++c;
33 p += __go_get_rune (p, pend - p, &rune);
36 if (c > MaxMem / sizeof (uint32_t))
37 runtime_throw ("out of memory");
39 mem = runtime_roundupsize (c * sizeof (uint32_t));
40 data = (uint32_t *) runtime_mallocgc (mem, 0, FlagNoScan | FlagNoZero);
41 p = str.str;
42 pd = data;
43 while (p < pend)
45 int rune;
47 p += __go_get_rune (p, pend - p, &rune);
48 *pd++ = rune;
50 if (mem > (uintptr) c * sizeof (uint32_t))
51 __builtin_memset (data + c, 0, mem - (uintptr) c * sizeof (uint32_t));
52 ret.__values = (void *) data;
53 ret.__count = c;
54 ret.__capacity = (intgo) (mem / sizeof (uint32_t));
55 return ret;