1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/glue/StringUtil.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,91 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "base/string_util.h" 1.9 + 1.10 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.11 +// Use of this source code is governed by a BSD-style license that can be 1.12 +// found in the LICENSE file. 1.13 + 1.14 +#include "base/sys_string_conversions.h" 1.15 + 1.16 +#include "base/string_piece.h" 1.17 +#include "base/string_util.h" 1.18 + 1.19 +#include "build/build_config.h" 1.20 + 1.21 +// FIXME/cjones: these really only pertain to the linux sys string 1.22 +// converters. 1.23 +#ifdef WCHAR_T_IS_UTF16 1.24 +# define ICONV_WCHAR_T_ENCODING "UTF-16" 1.25 +#else 1.26 +# define ICONV_WCHAR_T_ENCODING "WCHAR_T" 1.27 +#endif 1.28 + 1.29 +// FIXME/cjones: BIG assumption here that std::string is a good 1.30 +// container of UTF8-encoded strings. this is probably wrong, as its 1.31 +// API doesn't really make sense for UTF8. 1.32 + 1.33 +namespace base { 1.34 + 1.35 +// FIXME/cjones: as its name implies, this function is a hack. 1.36 +template<typename FromType, typename ToType> 1.37 +ToType 1.38 +GhettoStringConvert(const FromType& in) 1.39 +{ 1.40 + // FIXME/cjones: assumes no non-ASCII characters in |in| 1.41 + ToType out; 1.42 + out.resize(in.length()); 1.43 + for (int i = 0; i < static_cast<int>(in.length()); ++i) 1.44 + out[i] = static_cast<typename ToType::value_type>(in[i]); 1.45 + return out; 1.46 +} 1.47 +} 1.48 + 1.49 +// Implement functions that were in the chromium ICU library, which 1.50 +// we're not taking. 1.51 + 1.52 +std::string 1.53 +WideToUTF8(const std::wstring& wide) 1.54 +{ 1.55 + return base::SysWideToUTF8(wide); 1.56 +} 1.57 + 1.58 +std::wstring 1.59 +UTF8ToWide(const StringPiece& utf8) 1.60 +{ 1.61 + return base::SysUTF8ToWide(utf8); 1.62 +} 1.63 + 1.64 +namespace base { 1.65 + 1.66 +// FIXME/cjones: here we're entirely replacing the linux string 1.67 +// converters, and implementing the one that doesn't exist for OS X 1.68 +// and Windows. 1.69 + 1.70 +#if !defined(OS_MACOSX) && !defined(OS_WIN) 1.71 +std::string SysWideToUTF8(const std::wstring& wide) { 1.72 + // FIXME/cjones: do this with iconv 1.73 + return GhettoStringConvert<std::wstring, std::string>(wide); 1.74 +} 1.75 +#endif 1.76 + 1.77 +#if !defined(OS_MACOSX) && !defined(OS_WIN) 1.78 +std::wstring SysUTF8ToWide(const StringPiece& utf8) { 1.79 + // FIXME/cjones: do this with iconv 1.80 + return GhettoStringConvert<StringPiece, std::wstring>(utf8); 1.81 +} 1.82 + 1.83 +std::string SysWideToNativeMB(const std::wstring& wide) { 1.84 + // TODO(evanm): we can't assume Linux is UTF-8. 1.85 + return SysWideToUTF8(wide); 1.86 +} 1.87 + 1.88 +std::wstring SysNativeMBToWide(const StringPiece& native_mb) { 1.89 + // TODO(evanm): we can't assume Linux is UTF-8. 1.90 + return SysUTF8ToWide(native_mb); 1.91 +} 1.92 +#endif 1.93 + 1.94 +} // namespace base