MCPcopy Create free account
hub / github.com/GNOME/gjs / gjs_string_to_utf8_n

Function gjs_string_to_utf8_n

gjs/jsapi-util-string.cpp:115–136  ·  view source on GitHub ↗

* gjs_string_to_utf8_n: * @cx: the current #JSContext * @str: string to encode in UTF-8 * @output: (out): return location for the UTF-8-encoded string * @output_len: (out): return location for the length of @output * * Converts a JSString to UTF-8 and puts the char array in @output and its * length in @output_len. * * This function handles the boilerplate for unpacking @str, determining i

Source from the content-addressed store, hash-verified

113 * Returns: false if an exception is pending, otherwise true.
114 */
115bool gjs_string_to_utf8_n(JSContext* cx, JS::HandleString str,
116 JS::UniqueChars* output, size_t* output_len) {
117 JSLinearString* linear = JS_EnsureLinearString(cx, str);
118 if (!linear)
119 return false;
120
121 size_t length = JS::GetDeflatedUTF8StringLength(linear);
122 char* bytes = js_pod_malloc<char>(length + 1);
123 if (!bytes)
124 return false;
125
126 // Append a zero-terminator to the string.
127 bytes[length] = '\0';
128
129 size_t deflated_length [[maybe_unused]] =
130 JS::DeflateStringToUTF8Buffer(linear, mozilla::Span(bytes, length));
131 g_assert(deflated_length == length);
132
133 *output_len = length;
134 *output = JS::UniqueChars(bytes);
135 return true;
136}
137
138/**
139 * gjs_lossy_string_from_utf8:

Callers 2

gjs_string_to_intarrayFunction · 0.85
gjs_encode_to_uint8arrayFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected