Bug 1642744 [wpt PR 23920] - [ScrollTimeline] Update compositor timeline from blink...
[gecko.git] / ipc / glue / StringUtil.cpp
blob2eae9a12827bddc779c91f3850bf23205c195a3d
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/. */
7 #include "base/string_util.h"
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.
13 #include "base/sys_string_conversions.h"
15 #include "base/string_piece.h"
16 #include "base/string_util.h"
18 #include "build/build_config.h"
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
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.
32 namespace base {
34 // FIXME/cjones: as its name implies, this function is a hack.
35 template <typename FromType, typename ToType>
36 ToType 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;
45 } // namespace base
47 // Implement functions that were in the chromium ICU library, which
48 // we're not taking.
50 std::string WideToUTF8(const std::wstring& wide) {
51 return base::SysWideToUTF8(wide);
54 std::wstring UTF8ToWide(const StringPiece& utf8) {
55 return base::SysUTF8ToWide(utf8);
58 namespace base {
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.
64 #if !defined(OS_MACOSX) && !defined(OS_WIN)
65 std::string SysWideToUTF8(const std::wstring& wide) {
66 // FIXME/cjones: do this with iconv
67 return GhettoStringConvert<std::wstring, std::string>(wide);
69 #endif
71 #if !defined(OS_MACOSX) && !defined(OS_WIN)
72 std::wstring SysUTF8ToWide(const StringPiece& utf8) {
73 // FIXME/cjones: do this with iconv
74 return GhettoStringConvert<StringPiece, std::wstring>(utf8);
77 std::string SysWideToNativeMB(const std::wstring& wide) {
78 // TODO(evanm): we can't assume Linux is UTF-8.
79 return SysWideToUTF8(wide);
82 std::wstring SysNativeMBToWide(const StringPiece& native_mb) {
83 // TODO(evanm): we can't assume Linux is UTF-8.
84 return SysUTF8ToWide(native_mb);
86 #endif
88 } // namespace base