JS::DeflateStringToUTF8Buffer 编辑

This article covers features introduced in SpiderMonkey 38

Encode the given string as UTF8 into given buffer.

Syntax

// New in JSAPI 52
void
DeflateStringToUTF8Buffer(JSFlatString* src, mozilla::RangedPtr<char> dst,
                          size_t* dstlenp = nullptr, size_t* numcharsp = nullptr);

// Obsolete in SpiderMonkey 49
void
DeflateStringToUTF8Buffer(JSFlatString* src, mozilla::RangedPtr<char> dst);
NameTypeDescription
srcJSFlatString *The pointer to the string to deflate.
dstmozilla::RangedPtr<char>The ranged pointer to the buffer.
dstlenpsize_t*The pointer to receive the number of bytes written to the buffer.
numcharspsize_t*The pointer to receive the number of Unicode characters written to the buffer.

Description

JS::DeflateStringToUTF8Buffer encodes src as UTF8. The caller must either ensure dst has enough space to encode the entire string, or pass the length of the buffer as dstlenp. In which case, the function will encode characters from the string until the buffer is exhausted. Does not write the null terminator.

If dstlenp is provided, it will be updated to hold the number of bytes written to the buffer. If numcharsp is provided, it will be updated to hold the number of Unicode characters written to the buffer. This can be less than the length of the string, if the buffer is exhausted before the string is fully encoded.

Examples

char16_t uchars[] = { 0xD83E, 0xDD8A, 0 };

JS::RootedString str(cx, JS_NewUCStringCopyN(cx, uchars, 2));
if (!str) return false;

JS::Rooted<JSFlatString*> flatStr(cx, JS_FlattenString(cx, str));
if (!flatStr) return false;

size_t length = JS::GetDeflatedUTF8StringLength(flatStr);

char* buffer = static_cast<char*>(JS_malloc(cx, length + 1));
if (!buffer) return false;

JS::DeflateStringToUTF8Buffer(flatStr, mozilla::RangedPtr<char>(buffer, length));
buffer[length] = '\0';

printf("utf8: [%s]\n", buffer);

JS_free(cx, buffer);

See Also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:139 次

字数:3473

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文