go-string-to-byte-array.c 821 B

1234567891011121314151617181920212223242526272829
  1. /* go-string-to-byte-array.c -- convert a string to an array of bytes 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 "array.h"
  7. #include "arch.h"
  8. #include "malloc.h"
  9. struct __go_open_array
  10. __go_string_to_byte_array (String str)
  11. {
  12. uintptr cap;
  13. unsigned char *data;
  14. struct __go_open_array ret;
  15. cap = runtime_roundupsize (str.len);
  16. data = (unsigned char *) runtime_mallocgc (cap, 0, FlagNoScan | FlagNoZero);
  17. __builtin_memcpy (data, str.str, str.len);
  18. if (cap != (uintptr) str.len)
  19. __builtin_memset (data + str.len, 0, cap - (uintptr) str.len);
  20. ret.__values = (void *) data;
  21. ret.__count = str.len;
  22. ret.__capacity = (intgo) cap;
  23. return ret;
  24. }