2010-12-03 05:34:57 +01:00
|
|
|
/* go-string-to-byte-array.c -- convert a string to an array of bytes in Go.
|
|
|
|
|
|
|
|
Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
Use of this source code is governed by a BSD-style
|
|
|
|
license that can be found in the LICENSE file. */
|
|
|
|
|
|
|
|
#include "runtime.h"
|
2012-11-01 04:02:13 +01:00
|
|
|
#include "array.h"
|
2011-10-27 01:57:58 +02:00
|
|
|
#include "arch.h"
|
2010-12-03 05:34:57 +01:00
|
|
|
#include "malloc.h"
|
|
|
|
|
|
|
|
struct __go_open_array
|
2012-11-01 04:02:13 +01:00
|
|
|
__go_string_to_byte_array (String str)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
2014-06-07 00:37:27 +02:00
|
|
|
uintptr cap;
|
2010-12-03 05:34:57 +01:00
|
|
|
unsigned char *data;
|
|
|
|
struct __go_open_array ret;
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
cap = runtime_roundupsize (str.len);
|
|
|
|
data = (unsigned char *) runtime_mallocgc (cap, 0, FlagNoScan | FlagNoZero);
|
2012-11-01 04:02:13 +01:00
|
|
|
__builtin_memcpy (data, str.str, str.len);
|
2014-06-07 00:37:27 +02:00
|
|
|
if (cap != (uintptr) str.len)
|
|
|
|
__builtin_memset (data + str.len, 0, cap - (uintptr) str.len);
|
2010-12-03 05:34:57 +01:00
|
|
|
ret.__values = (void *) data;
|
2012-11-01 04:02:13 +01:00
|
|
|
ret.__count = str.len;
|
2014-06-07 00:37:27 +02:00
|
|
|
ret.__capacity = (intgo) cap;
|
2010-12-03 05:34:57 +01:00
|
|
|
return ret;
|
|
|
|
}
|