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