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

Side by Side Diff: base/string_util.cc

Issue 3076013: Move ASCIIToWide and ASCIIToUTF16 to utf_string_conversions.h. I've found it... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 4 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/string_util.h" 5 #include "base/string_util.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #include <ctype.h> 9 #include <ctype.h>
10 #include <errno.h> 10 #include <errno.h>
11 #include <math.h> 11 #include <math.h>
12 #include <stdarg.h> 12 #include <stdarg.h>
13 #include <stdio.h> 13 #include <stdio.h>
14 #include <stdlib.h> 14 #include <stdlib.h>
15 #include <string.h> 15 #include <string.h>
16 #include <time.h> 16 #include <time.h>
17 #include <wchar.h> 17 #include <wchar.h>
18 #include <wctype.h> 18 #include <wctype.h>
19 19
20 #include <algorithm> 20 #include <algorithm>
21 #include <vector> 21 #include <vector>
22 22
23 #include "base/basictypes.h" 23 #include "base/basictypes.h"
24 #include "base/logging.h" 24 #include "base/logging.h"
25 #include "base/singleton.h" 25 #include "base/singleton.h"
26 #include "base/third_party/dmg_fp/dmg_fp.h" 26 #include "base/third_party/dmg_fp/dmg_fp.h"
27 #include "base/utf_string_conversion_utils.h" 27 #include "base/utf_string_conversion_utils.h"
28 #include "base/utf_string_conversions.h"
28 #include "base/third_party/icu/icu_utf.h" 29 #include "base/third_party/icu/icu_utf.h"
29 30
30 namespace { 31 namespace {
31 32
32 // Force the singleton used by Empty[W]String[16] to be a unique type. This 33 // Force the singleton used by Empty[W]String[16] to be a unique type. This
33 // prevents other code that might accidentally use Singleton<string> from 34 // prevents other code that might accidentally use Singleton<string> from
34 // getting our internal one. 35 // getting our internal one.
35 struct EmptyStrings { 36 struct EmptyStrings {
36 EmptyStrings() {} 37 EmptyStrings() {}
37 const std::string s; 38 const std::string s;
(...skipping 608 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 bool ContainsOnlyChars(const std::string& input, 647 bool ContainsOnlyChars(const std::string& input,
647 const std::string& characters) { 648 const std::string& characters) {
648 return ContainsOnlyCharsT(input, characters); 649 return ContainsOnlyCharsT(input, characters);
649 } 650 }
650 651
651 std::string WideToASCII(const std::wstring& wide) { 652 std::string WideToASCII(const std::wstring& wide) {
652 DCHECK(IsStringASCII(wide)) << wide; 653 DCHECK(IsStringASCII(wide)) << wide;
653 return std::string(wide.begin(), wide.end()); 654 return std::string(wide.begin(), wide.end());
654 } 655 }
655 656
656 std::wstring ASCIIToWide(const base::StringPiece& ascii) {
657 DCHECK(IsStringASCII(ascii)) << ascii;
658 return std::wstring(ascii.begin(), ascii.end());
659 }
660
661 std::string UTF16ToASCII(const string16& utf16) { 657 std::string UTF16ToASCII(const string16& utf16) {
662 DCHECK(IsStringASCII(utf16)) << utf16; 658 DCHECK(IsStringASCII(utf16)) << utf16;
663 return std::string(utf16.begin(), utf16.end()); 659 return std::string(utf16.begin(), utf16.end());
664 } 660 }
665 661
666 string16 ASCIIToUTF16(const base::StringPiece& ascii) {
667 DCHECK(IsStringASCII(ascii)) << ascii;
668 return string16(ascii.begin(), ascii.end());
669 }
670
671 // Latin1 is just the low range of Unicode, so we can copy directly to convert. 662 // Latin1 is just the low range of Unicode, so we can copy directly to convert.
672 bool WideToLatin1(const std::wstring& wide, std::string* latin1) { 663 bool WideToLatin1(const std::wstring& wide, std::string* latin1) {
673 std::string output; 664 std::string output;
674 output.resize(wide.size()); 665 output.resize(wide.size());
675 latin1->clear(); 666 latin1->clear();
676 for (size_t i = 0; i < wide.size(); i++) { 667 for (size_t i = 0; i < wide.size(); i++) {
677 if (wide[i] > 255) 668 if (wide[i] > 255)
678 return false; 669 return false;
679 output[i] = static_cast<char>(wide[i]); 670 output[i] = static_cast<char>(wide[i]);
680 } 671 }
(...skipping 1229 matching lines...) Expand 10 before | Expand all | Expand 10 after
1910 // Each input byte creates two output hex characters. 1901 // Each input byte creates two output hex characters.
1911 std::string ret(size * 2, '\0'); 1902 std::string ret(size * 2, '\0');
1912 1903
1913 for (size_t i = 0; i < size; ++i) { 1904 for (size_t i = 0; i < size; ++i) {
1914 char b = reinterpret_cast<const char*>(bytes)[i]; 1905 char b = reinterpret_cast<const char*>(bytes)[i];
1915 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; 1906 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf];
1916 ret[(i * 2) + 1] = kHexChars[b & 0xf]; 1907 ret[(i * 2) + 1] = kHexChars[b & 0xf];
1917 } 1908 }
1918 return ret; 1909 return ret;
1919 } 1910 }
OLDNEW
« no previous file with comments | « base/string_util.h ('k') | base/utf_string_conversions.h » ('j') | base/utf_string_conversions.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698