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

Unified Diff: base/string_util.cc

Issue 28189: ASCII <-> UTF16 conversion functions. These are just copies of WideToASCII a... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 10 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/string_util.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/string_util.cc
===================================================================
--- base/string_util.cc (revision 10468)
+++ base/string_util.cc (working copy)
@@ -450,6 +450,16 @@
return std::wstring(ascii.begin(), ascii.end());
}
+std::string UTF16ToASCII(const string16& utf16) {
+ DCHECK(IsStringASCII(utf16));
+ return std::string(utf16.begin(), utf16.end());
+}
+
+string16 ASCIIToUTF16(const std::string& ascii) {
+ DCHECK(IsStringASCII(ascii));
+ return string16(ascii.begin(), ascii.end());
+}
+
// Latin1 is just the low range of Unicode, so we can copy directly to convert.
bool WideToLatin1(const std::wstring& wide, std::string* latin1) {
std::string output;
@@ -472,20 +482,28 @@
return true;
}
-bool IsStringASCII(const std::wstring& str) {
+template<class STR>
+static bool DoIsStringASCII(const STR& str) {
for (size_t i = 0; i < str.length(); i++) {
- if (str[i] > 0x7F)
+ typename ToUnsigned<typename STR::value_type>::Unsigned c = str[i];
+ if (c > 0x7F)
return false;
}
return true;
}
+bool IsStringASCII(const std::wstring& str) {
+ return DoIsStringASCII(str);
+}
+
+#if !defined(WCHAR_T_IS_UTF16)
+bool IsStringASCII(const string16& str) {
+ return DoIsStringASCII(str);
+}
+#endif
+
bool IsStringASCII(const std::string& str) {
- for (size_t i = 0; i < str.length(); i++) {
- if (static_cast<unsigned char>(str[i]) > 0x7F)
- return false;
- }
- return true;
+ return DoIsStringASCII(str);
}
// Helper functions that determine whether the given character begins a
« no previous file with comments | « base/string_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698