Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4018)

Unified Diff: base/strings/utf_string_conversions.cc

Issue 543043002: Implement fast path in UTF8ToUTF16 for pure ASCII strings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/strings/string_util_unittest.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/strings/utf_string_conversions.cc
diff --git a/base/strings/utf_string_conversions.cc b/base/strings/utf_string_conversions.cc
index f13ed1bb9c61fc6c2211a488bd3568047c5e3fc1..9d520fa5ecf0841a4379a4062de65767bf22e9fd 100644
--- a/base/strings/utf_string_conversions.cc
+++ b/base/strings/utf_string_conversions.cc
@@ -56,13 +56,23 @@ std::string WideToUTF8(const std::wstring& wide) {
}
bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) {
- PrepareForUTF16Or32Output(src, src_len, output);
- return ConvertUnicode(src, src_len, output);
+ if (IsStringASCII(StringPiece(src, src_len))) {
+ output->assign(src, src + src_len);
+ return true;
+ } else {
+ PrepareForUTF16Or32Output(src, src_len, output);
+ return ConvertUnicode(src, src_len, output);
+ }
}
std::wstring UTF8ToWide(const StringPiece& utf8) {
+ if (IsStringASCII(utf8)) {
+ return std::wstring(utf8.begin(), utf8.end());
+ }
+
std::wstring ret;
- UTF8ToWide(utf8.data(), utf8.length(), &ret);
+ PrepareForUTF16Or32Output(utf8.data(), utf8.length(), &ret);
+ ConvertUnicode(utf8.data(), utf8.length(), &ret);
return ret;
}
@@ -126,15 +136,25 @@ std::wstring UTF16ToWide(const string16& utf16) {
#if defined(WCHAR_T_IS_UTF32)
bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) {
- PrepareForUTF16Or32Output(src, src_len, output);
- return ConvertUnicode(src, src_len, output);
+ if (IsStringASCII(StringPiece(src, src_len))) {
+ output->assign(src, src + src_len);
+ return true;
+ } else {
+ PrepareForUTF16Or32Output(src, src_len, output);
+ return ConvertUnicode(src, src_len, output);
+ }
}
string16 UTF8ToUTF16(const StringPiece& utf8) {
+ if (IsStringASCII(utf8)) {
+ return string16(utf8.begin(), utf8.end());
+ }
+
string16 ret;
+ PrepareForUTF16Or32Output(utf8.data(), utf8.length(), &ret);
// Ignore the success flag of this call, it will do the best it can for
// invalid input, which is what we want here.
- UTF8ToUTF16(utf8.data(), utf8.length(), &ret);
+ ConvertUnicode(utf8.data(), utf8.length(), &ret);
return ret;
}
« no previous file with comments | « base/strings/string_util_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698