1 /* go-string-to-int-array.c -- convert a string to an array of ints in Go.
2 
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.  */
6 
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"
13 
14 struct __go_open_array
__go_string_to_int_array(String str)15 __go_string_to_int_array (String str)
16 {
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;
24 
25   c = 0;
26   p = str.str;
27   pend = p + str.len;
28   while (p < pend)
29     {
30       int rune;
31 
32       ++c;
33       p += __go_get_rune (p, pend - p, &rune);
34     }
35 
36   if (c > MaxMem / sizeof (uint32_t))
37     runtime_throw ("out of memory");
38 
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)
44     {
45       int rune;
46 
47       p += __go_get_rune (p, pend - p, &rune);
48       *pd++ = rune;
49     }
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;
56 }
57