* 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
| 113 | * Returns: false if an exception is pending, otherwise true. |
| 114 | */ |
| 115 | bool 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: |
no outgoing calls
no test coverage detected