michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "base/string_util.h" michael@0: michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/sys_string_conversions.h" michael@0: michael@0: #include "base/string_piece.h" michael@0: #include "base/string_util.h" michael@0: michael@0: #include "build/build_config.h" michael@0: michael@0: // FIXME/cjones: these really only pertain to the linux sys string michael@0: // converters. michael@0: #ifdef WCHAR_T_IS_UTF16 michael@0: # define ICONV_WCHAR_T_ENCODING "UTF-16" michael@0: #else michael@0: # define ICONV_WCHAR_T_ENCODING "WCHAR_T" michael@0: #endif michael@0: michael@0: // FIXME/cjones: BIG assumption here that std::string is a good michael@0: // container of UTF8-encoded strings. this is probably wrong, as its michael@0: // API doesn't really make sense for UTF8. michael@0: michael@0: namespace base { michael@0: michael@0: // FIXME/cjones: as its name implies, this function is a hack. michael@0: template michael@0: ToType michael@0: GhettoStringConvert(const FromType& in) michael@0: { michael@0: // FIXME/cjones: assumes no non-ASCII characters in |in| michael@0: ToType out; michael@0: out.resize(in.length()); michael@0: for (int i = 0; i < static_cast(in.length()); ++i) michael@0: out[i] = static_cast(in[i]); michael@0: return out; michael@0: } michael@0: } michael@0: michael@0: // Implement functions that were in the chromium ICU library, which michael@0: // we're not taking. michael@0: michael@0: std::string michael@0: WideToUTF8(const std::wstring& wide) michael@0: { michael@0: return base::SysWideToUTF8(wide); michael@0: } michael@0: michael@0: std::wstring michael@0: UTF8ToWide(const StringPiece& utf8) michael@0: { michael@0: return base::SysUTF8ToWide(utf8); michael@0: } michael@0: michael@0: namespace base { michael@0: michael@0: // FIXME/cjones: here we're entirely replacing the linux string michael@0: // converters, and implementing the one that doesn't exist for OS X michael@0: // and Windows. michael@0: michael@0: #if !defined(OS_MACOSX) && !defined(OS_WIN) michael@0: std::string SysWideToUTF8(const std::wstring& wide) { michael@0: // FIXME/cjones: do this with iconv michael@0: return GhettoStringConvert(wide); michael@0: } michael@0: #endif michael@0: michael@0: #if !defined(OS_MACOSX) && !defined(OS_WIN) michael@0: std::wstring SysUTF8ToWide(const StringPiece& utf8) { michael@0: // FIXME/cjones: do this with iconv michael@0: return GhettoStringConvert(utf8); michael@0: } michael@0: michael@0: std::string SysWideToNativeMB(const std::wstring& wide) { michael@0: // TODO(evanm): we can't assume Linux is UTF-8. michael@0: return SysWideToUTF8(wide); michael@0: } michael@0: michael@0: std::wstring SysNativeMBToWide(const StringPiece& native_mb) { michael@0: // TODO(evanm): we can't assume Linux is UTF-8. michael@0: return SysUTF8ToWide(native_mb); michael@0: } michael@0: #endif michael@0: michael@0: } // namespace base