Bug 1736875: Add FormatBuffer::toAsciiString(). r=allstarschh

This avoids an unnecessary call to `JS::FindSmallestEncoding()` in `NewStringCopyUTF8N()`
when the string is guaranteed to be ASCII-only.

Differential Revision: https://phabricator.services.mozilla.com/D129053
This commit is contained in:
André Bargull 2021-10-22 08:49:11 +00:00
Родитель 8ff93952a4
Коммит fd5b205acc
3 изменённых файлов: 16 добавлений и 4 удалений

Просмотреть файл

@ -9,6 +9,7 @@
#include "mozilla/Assertions.h"
#include "mozilla/Span.h"
#include "mozilla/TextUtils.h"
#include <stddef.h>
#include <stdint.h>
@ -97,6 +98,17 @@ class FormatBuffer {
}
}
/**
* Copies the buffer's data to a JSString. The buffer must contain only
* ASCII characters.
*/
JSLinearString* toAsciiString(JSContext* cx) const {
static_assert(std::is_same_v<CharT, char>);
MOZ_ASSERT(mozilla::IsAscii(buffer_));
return NewStringCopyN<CanGC>(cx, buffer_.begin(), buffer_.length());
}
/**
* Extract this buffer's content as a null-terminated string.
*/

Просмотреть файл

@ -377,7 +377,7 @@ bool js::intl_supportedLocaleOrFallback(JSContext* cx, unsigned argc,
return false;
}
candidate = buffer.toString(cx);
candidate = buffer.toAsciiString(cx);
if (!candidate) {
return false;
}

Просмотреть файл

@ -113,7 +113,7 @@ static LocaleObject* CreateLocaleObject(JSContext* cx, HandleObject prototype,
return nullptr;
}
RootedString tagStr(cx, buffer.toString(cx));
RootedString tagStr(cx, buffer.toAsciiString(cx));
if (!tagStr) {
return nullptr;
}
@ -1367,7 +1367,7 @@ bool js::intl_ValidateAndCanonicalizeLanguageTag(JSContext* cx, unsigned argc,
return false;
}
JSString* resultStr = buffer.toString(cx);
JSString* resultStr = buffer.toAsciiString(cx);
if (!resultStr) {
return false;
}
@ -1424,7 +1424,7 @@ bool js::intl_TryValidateAndCanonicalizeLanguageTag(JSContext* cx,
return false;
}
JSString* resultStr = buffer.toString(cx);
JSString* resultStr = buffer.toAsciiString(cx);
if (!resultStr) {
return false;
}