ext/jsonnet/helpers.c in jsonnet-0.5.3 vs ext/jsonnet/helpers.c in jsonnet-0.6.0
- old
+ new
@@ -32,16 +32,42 @@
return enc;
}
/**
* Allocates a C string whose content is equal to \c str with jsonnet_realloc.
+ *
+ * Note that this function does not allow NUL characters in the string.
+ * You should use rubyjsonnet_str_to_ptr() if you want to handle NUL characters.
+ *
+ * @param[in] vm a Jsonnet VM
+ * @param[in] str a String-like object
+ * @return the allocated C string
*/
char *
rubyjsonnet_str_to_cstr(struct JsonnetVm *vm, VALUE str)
{
const char *const cstr = StringValueCStr(str);
char *const buf = jsonnet_realloc(vm, NULL, strlen(cstr));
strcpy(buf, cstr);
+ return buf;
+}
+
+/**
+ * Allocates a byte sequence whose content is equal to \c str with jsonnet_realloc.
+ *
+ * @param[in] vm a Jsonnet VM
+ * @param[in] str a String-like object
+ * @param[out] buflen the length of the allocated buffer
+ * @return the allocated buffer
+ */
+char *
+rubyjsonnet_str_to_ptr(struct JsonnetVm *vm, VALUE str, size_t *buflen)
+{
+ StringValue(str);
+ size_t len = RSTRING_LEN(str);
+ char *buf = jsonnet_realloc(vm, NULL, len);
+ memcpy(buf, RSTRING_PTR(str), len);
+ *buflen = len;
return buf;
}
/**
* @return a human readable string which contains the class name of the