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> GhettoStringConvert(const FromType & in)36ToType GhettoStringConvert(const FromType& in) { 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 } // namespace base 46 47 // Implement functions that were in the chromium ICU library, which 48 // we're not taking. 49 WideToUTF8(const std::wstring & wide)50std::string WideToUTF8(const std::wstring& wide) { 51 return base::SysWideToUTF8(wide); 52 } 53 UTF8ToWide(const StringPiece & utf8)54std::wstring UTF8ToWide(const StringPiece& utf8) { 55 return base::SysUTF8ToWide(utf8); 56 } 57 58 namespace base { 59 60 // FIXME/cjones: here we're entirely replacing the linux string 61 // converters, and implementing the one that doesn't exist for OS X 62 // and Windows. 63 64 #if !defined(OS_MACOSX) && !defined(OS_WIN) SysWideToUTF8(const std::wstring & wide)65std::string SysWideToUTF8(const std::wstring& wide) { 66 // FIXME/cjones: do this with iconv 67 return GhettoStringConvert<std::wstring, std::string>(wide); 68 } 69 #endif 70 71 #if !defined(OS_MACOSX) && !defined(OS_WIN) SysUTF8ToWide(const StringPiece & utf8)72std::wstring SysUTF8ToWide(const StringPiece& utf8) { 73 // FIXME/cjones: do this with iconv 74 return GhettoStringConvert<StringPiece, std::wstring>(utf8); 75 } 76 SysWideToNativeMB(const std::wstring & wide)77std::string SysWideToNativeMB(const std::wstring& wide) { 78 // TODO(evanm): we can't assume Linux is UTF-8. 79 return SysWideToUTF8(wide); 80 } 81 SysNativeMBToWide(const StringPiece & native_mb)82std::wstring SysNativeMBToWide(const StringPiece& native_mb) { 83 // TODO(evanm): we can't assume Linux is UTF-8. 84 return SysUTF8ToWide(native_mb); 85 } 86 #endif 87 88 } // namespace base 89