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