|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "base/string_util.h" |
|
6 |
|
7 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
|
8 // Use of this source code is governed by a BSD-style license that can be |
|
9 // found in the LICENSE file. |
|
10 |
|
11 #include "base/sys_string_conversions.h" |
|
12 |
|
13 #include "base/string_piece.h" |
|
14 #include "base/string_util.h" |
|
15 |
|
16 #include "build/build_config.h" |
|
17 |
|
18 // FIXME/cjones: these really only pertain to the linux sys string |
|
19 // converters. |
|
20 #ifdef WCHAR_T_IS_UTF16 |
|
21 # define ICONV_WCHAR_T_ENCODING "UTF-16" |
|
22 #else |
|
23 # define ICONV_WCHAR_T_ENCODING "WCHAR_T" |
|
24 #endif |
|
25 |
|
26 // FIXME/cjones: BIG assumption here that std::string is a good |
|
27 // container of UTF8-encoded strings. this is probably wrong, as its |
|
28 // API doesn't really make sense for UTF8. |
|
29 |
|
30 namespace base { |
|
31 |
|
32 // FIXME/cjones: as its name implies, this function is a hack. |
|
33 template<typename FromType, typename ToType> |
|
34 ToType |
|
35 GhettoStringConvert(const FromType& in) |
|
36 { |
|
37 // FIXME/cjones: assumes no non-ASCII characters in |in| |
|
38 ToType out; |
|
39 out.resize(in.length()); |
|
40 for (int i = 0; i < static_cast<int>(in.length()); ++i) |
|
41 out[i] = static_cast<typename ToType::value_type>(in[i]); |
|
42 return out; |
|
43 } |
|
44 } |
|
45 |
|
46 // Implement functions that were in the chromium ICU library, which |
|
47 // we're not taking. |
|
48 |
|
49 std::string |
|
50 WideToUTF8(const std::wstring& wide) |
|
51 { |
|
52 return base::SysWideToUTF8(wide); |
|
53 } |
|
54 |
|
55 std::wstring |
|
56 UTF8ToWide(const StringPiece& utf8) |
|
57 { |
|
58 return base::SysUTF8ToWide(utf8); |
|
59 } |
|
60 |
|
61 namespace base { |
|
62 |
|
63 // FIXME/cjones: here we're entirely replacing the linux string |
|
64 // converters, and implementing the one that doesn't exist for OS X |
|
65 // and Windows. |
|
66 |
|
67 #if !defined(OS_MACOSX) && !defined(OS_WIN) |
|
68 std::string SysWideToUTF8(const std::wstring& wide) { |
|
69 // FIXME/cjones: do this with iconv |
|
70 return GhettoStringConvert<std::wstring, std::string>(wide); |
|
71 } |
|
72 #endif |
|
73 |
|
74 #if !defined(OS_MACOSX) && !defined(OS_WIN) |
|
75 std::wstring SysUTF8ToWide(const StringPiece& utf8) { |
|
76 // FIXME/cjones: do this with iconv |
|
77 return GhettoStringConvert<StringPiece, std::wstring>(utf8); |
|
78 } |
|
79 |
|
80 std::string SysWideToNativeMB(const std::wstring& wide) { |
|
81 // TODO(evanm): we can't assume Linux is UTF-8. |
|
82 return SysWideToUTF8(wide); |
|
83 } |
|
84 |
|
85 std::wstring SysNativeMBToWide(const StringPiece& native_mb) { |
|
86 // TODO(evanm): we can't assume Linux is UTF-8. |
|
87 return SysUTF8ToWide(native_mb); |
|
88 } |
|
89 #endif |
|
90 |
|
91 } // namespace base |