go-string-to-int-array.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* go-string-to-int-array.c -- convert a string to an array of ints in Go.
  2. Copyright 2010 The Go Authors. All rights reserved.
  3. Use of this source code is governed by a BSD-style
  4. license that can be found in the LICENSE file. */
  5. #include "runtime.h"
  6. #include "go-alloc.h"
  7. #include "go-string.h"
  8. #include "array.h"
  9. #include "arch.h"
  10. #include "malloc.h"
  11. struct __go_open_array
  12. __go_string_to_int_array (String str)
  13. {
  14. size_t c;
  15. const unsigned char *p;
  16. const unsigned char *pend;
  17. uintptr mem;
  18. uint32_t *data;
  19. uint32_t *pd;
  20. struct __go_open_array ret;
  21. c = 0;
  22. p = str.str;
  23. pend = p + str.len;
  24. while (p < pend)
  25. {
  26. int rune;
  27. ++c;
  28. p += __go_get_rune (p, pend - p, &rune);
  29. }
  30. if (c > MaxMem / sizeof (uint32_t))
  31. runtime_throw ("out of memory");
  32. mem = runtime_roundupsize (c * sizeof (uint32_t));
  33. data = (uint32_t *) runtime_mallocgc (mem, 0, FlagNoScan | FlagNoZero);
  34. p = str.str;
  35. pd = data;
  36. while (p < pend)
  37. {
  38. int rune;
  39. p += __go_get_rune (p, pend - p, &rune);
  40. *pd++ = rune;
  41. }
  42. if (mem > (uintptr) c * sizeof (uint32_t))
  43. __builtin_memset (data + c, 0, mem - (uintptr) c * sizeof (uint32_t));
  44. ret.__values = (void *) data;
  45. ret.__count = c;
  46. ret.__capacity = (intgo) (mem / sizeof (uint32_t));
  47. return ret;
  48. }