0

Add defines for the size of wchar_t to build_config.h. Use this in places where we currently have an OS-specific check.

Remove all WIN32 ifdefs from base and replace them with proper defined(OS...).

I also fixed random style bits when I encountered them. I made major style fixes to string16.

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@524 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
brettw@google.com
2008-08-07 18:31:40 +00:00
parent a6b4a18565
commit 39be42426e
14 changed files with 221 additions and 219 deletions

@ -653,6 +653,10 @@
RelativePath="..\string_util_win.h"
>
</File>
<File
RelativePath="..\sys_string_conversions.h"
>
</File>
<File
RelativePath="..\sys_string_conversions_win.cc"
>

@ -31,11 +31,13 @@
// line argument. When the command line argument is detected, it invokes the
// debugger, if no system-wide debugger is registered, a debug break is done.
#ifndef BASE_DEBUG_ON_START_H__
#define BASE_DEBUG_ON_START_H__
#ifndef BASE_DEBUG_ON_START_H_
#define BASE_DEBUG_ON_START_H_
#include "base/basictypes.h"
// This only works on Windows.
#ifdef _WIN32
#if defined(OS_WIN)
#ifndef DECLSPEC_SELECTANY
#define DECLSPEC_SELECTANY __declspec(selectany)
@ -85,6 +87,6 @@ DECLSPEC_SELECTANY DebugOnStart::PIFV debug_on_start = &DebugOnStart::Init;
#pragma data_seg(pop)
#endif // _WIN64
#endif // _WIN32
#endif // defined(OS_WIN)
#endif // BASE_DEBUG_ON_START_H__
#endif // BASE_DEBUG_ON_START_H_

@ -30,14 +30,15 @@
// This file contains utility functions for dealing with the local
// filesystem.
#ifndef BASE_FILE_UTIL_H__
#define BASE_FILE_UTIL_H__
#ifndef BASE_FILE_UTIL_H_
#define BASE_FILE_UTIL_H_
#include "build/build_config.h"
#ifdef OS_WIN
#if defined(OS_WIN)
#include <windows.h>
#endif
#include <stack>
#include <string>
@ -120,13 +121,13 @@ void ReplaceExtension(std::wstring* file_name, const std::wstring& extension);
//-----------------------------------------------------------------------------
// Functions that involve filesystem access or modification:
#ifdef OS_WIN
#if defined(OS_WIN)
// Returns the number of files matching the current path that were
// created on or after the given FILETIME. Doesn't count ".." or ".".
// Filetime is UTC filetime, not LocalFiletime.
int CountFilesCreatedAfter(const std::wstring& path,
const FILETIME& file_time);
#endif
#endif // defined(OS_WIN)
// Deletes the given path, whether it's a file or a directory.
// If it's a directory, it's perfectly happy to delete all of the
@ -162,7 +163,7 @@ bool PathExists(const std::wstring& path);
// Returns true if the given path is writable by the user, false otherwise.
bool PathIsWritable(const std::wstring& path);
#ifdef OS_WIN
#if defined(OS_WIN)
// Gets the creation time of the given file (expressed in the local timezone),
// and returns it via the creation_time parameter. Returns true if successful,
// false otherwise.
@ -172,7 +173,7 @@ bool GetFileCreationLocalTime(const std::wstring& filename,
// Same as above, but takes a previously-opened file handle instead of a name.
bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle,
LPSYSTEMTIME creation_time);
#endif
#endif // defined(OS_WIN)
// Returns true if the contents of the two files given are equal, false
// otherwise. If either file can't be read, returns false.
@ -299,10 +300,10 @@ class FileEnumerator {
// enumerate in the breadth-first search.
std::stack<std::wstring> pending_paths_;
#ifdef OS_WIN
#if defined(OS_WIN)
WIN32_FIND_DATA find_data_;
HANDLE find_handle_;
#endif
#endif // defined(OS_WIN)
DISALLOW_EVIL_CONSTRUCTORS(FileEnumerator);
};
@ -315,4 +316,4 @@ bool RenameFileAndResetSecurityDescriptor(
} // namespace file_util
#endif // BASE_FILE_UTIL_H__
#endif // BASE_FILE_UTIL_H_

@ -27,8 +27,8 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef BASE_LOGGING_H__
#define BASE_LOGGING_H__
#ifndef BASE_LOGGING_H_
#define BASE_LOGGING_H_
#include <string>
#include <cstring>
@ -143,10 +143,10 @@ enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE };
// The default log file is initialized to "debug.log" in the application
// directory. You probably don't want this, especially since the program
// directory may not be writable on an enduser's system.
#if defined(WIN32)
#if defined(OS_WIN)
void InitLogging(const wchar_t* log_file, LoggingDestination logging_dest,
LogLockingState lock_log, OldFileDeletionState delete_old);
#else
#elif defined(OS_POSIX)
// TODO(avi): do we want to do a unification of character types here?
void InitLogging(const char* log_file, LoggingDestination logging_dest,
LogLockingState lock_log, OldFileDeletionState delete_old);
@ -483,7 +483,7 @@ class LogMessage {
size_t message_start_; // Offset of the start of the message (past prefix
// info).
DISALLOW_EVIL_CONSTRUCTORS(LogMessage);
DISALLOW_COPY_AND_ASSIGN(LogMessage);
};
// A non-macro interface to the log facility; (useful
@ -509,16 +509,17 @@ class LogMessageVoidify {
// after this call.
void CloseLogFile();
} // namespace Logging
} // namespace logging
// These functions are provided as a convenience for logging, which is where we
// use streams (it is against Google style to use streams in other places). It
// is designed to allow you to emit non-ASCII Unicode strings to the log file,
// which is normally ASCII. It is relatively slow, so try not to use it for
// common cases. Non-ASCII characters will be converted to UTF-8 by these operators.
// common cases. Non-ASCII characters will be converted to UTF-8 by these
// operators.
std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
return out << wstr.c_str();
}
#endif // BASE_LOGGING_H__
#endif // BASE_LOGGING_H_

@ -27,19 +27,19 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef WIN32
#include "base/platform_thread.h"
#if defined(OS_POSIX)
#include <sched.h>
#endif
#include "base/platform_thread.h"
// static
PlatformThread PlatformThread::Current() {
PlatformThread thread;
#ifdef WIN32
#if defined(OS_WIN)
thread.thread_ = GetCurrentThread();
#else
#elif defined(OS_POSIX)
thread.thread_ = pthread_self();
#endif
@ -48,17 +48,17 @@ PlatformThread PlatformThread::Current() {
// static
void PlatformThread::YieldCurrentThread() {
#ifdef WIN32
#if defined(OS_WIN)
::Sleep(0);
#else
#elif defined(OS_POSIX)
sched_yield();
#endif
}
bool PlatformThread::operator==(const PlatformThread& other_thread) {
#ifdef WIN32
#if defined(OS_WIN)
return thread_ == other_thread.thread_;
#else
#elif defined(OS_POSIX)
return pthread_equal(thread_, other_thread.thread_);
#endif
}

@ -27,16 +27,22 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef BASE_PLATFORM_THREAD_H__
#define BASE_PLATFORM_THREAD_H__
#ifndef BASE_PLATFORM_THREAD_H_
#define BASE_PLATFORM_THREAD_H_
#include "build/build_config.h"
#if defined(OS_WIN)
#ifdef WIN32
#include <windows.h>
typedef HANDLE PlatformThreadHandle;
#else
#elif defined(OS_POSIX)
#include <pthread.h>
typedef pthread_t PlatformThreadHandle;
#endif
#endif // defined(OS_POSIX)
class PlatformThread {
public:
@ -49,8 +55,7 @@ class PlatformThread {
bool operator==(const PlatformThread& other_thread);
private:
PlatformThreadHandle thread_;
};
#endif // BASE_PLATFORM_THREAD_H__
#endif // BASE_PLATFORM_THREAD_H_

@ -27,17 +27,18 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef BASE_SHARED_MEMORY_H__
#define BASE_SHARED_MEMORY_H__
#ifndef BASE_SHARED_MEMORY_H_
#define BASE_SHARED_MEMORY_H_
#include "base/basictypes.h"
#include "base/process_util.h"
// SharedMemoryHandle is a platform specific type which represents
// the underlying OS handle to a shared memory segment.
#ifdef WIN32
#if defined(OS_WIN)
typedef HANDLE SharedMemoryHandle;
typedef HANDLE SharedMemoryLock;
#else
#elif defined(OS_POSIX)
typedef int SharedMemoryHandle;
typedef int SharedMemoryLock;
#endif
@ -166,4 +167,4 @@ class SharedMemoryAutoLock {
};
#endif // BASE_SHARED_MEMORY_H__
#endif // BASE_SHARED_MEMORY_H_

@ -26,30 +26,10 @@
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#ifndef BASE_STRING16_H_
#define BASE_STRING16_H_
// WHAT:
// A version of std::basic_string that works even on Linux when 2-byte wchar_t
// values (-fshort-wchar) are used. You can access this class as std::string16.
@ -69,77 +49,81 @@
// for us, std::wstring uses mostly *inline* wchar_t-based functions (like
// wmemcmp) that are defined in .h files and do not need to be overridden.
#ifndef BASE_STRING16_H__
#define BASE_STRING16_H__
#include <string>
#ifdef WIN32
#include "build/build_config.h"
#ifdef WCHAR_T_IS_UTF16
typedef wchar_t char16;
namespace std {
typedef wstring string16;
typedef wstring string16;
}
#else
#else // !WCHAR_T_IS_UTF16
typedef unsigned short char16;
namespace std {
typedef basic_string<char16> string16;
typedef basic_string<char16> string16;
}
// Define char16 versions of functions required below in char_traits<char16>
extern "C" {
inline char16 *char16_wmemmove(char16 *s1, const char16 *s2, size_t n) {
return (char16 *)memmove(s1, s2, n * sizeof(char16));
}
inline char16 *char16_wmemmove(char16 *s1, const char16 *s2, size_t n) {
return reinterpret_cast<char16*>(memmove(s1, s2, n * sizeof(char16)));
}
inline char16 *char16_wmemcpy(char16 *s1, const char16 *s2, size_t n) {
return (char16 *)memcpy(s1, s2, n * sizeof(char16));
}
inline char16 *char16_wmemcpy(char16 *s1, const char16 *s2, size_t n) {
return reinterpret_cast<char16*>(memcpy(s1, s2, n * sizeof(char16)));
}
inline int char16_wmemcmp(const char16 *s1, const char16 *s2, size_t n) {
// we cannot call memcmp because that changes the semantics.
while (n > 0) {
if (*s1 != *s2) {
// we cannot use (*s1 - *s2) because char16 is unsigned
return ((*s1 < *s2) ? -1 : 1);
}
++s1; ++s2; --n;
inline int char16_wmemcmp(const char16 *s1, const char16 *s2, size_t n) {
// We cannot call memcmp because that changes the semantics.
while (n > 0) {
if (*s1 != *s2) {
// We cannot use (*s1 - *s2) because char16 is unsigned.
return ((*s1 < *s2) ? -1 : 1);
}
return 0;
++s1;
++s2;
--n;
}
return 0;
}
inline const char16 *char16_wmemchr(const char16 *s, char16 c, size_t n) {
while (n > 0) {
if (*s == c) {
return s;
}
++s; --n;
inline const char16 *char16_wmemchr(const char16 *s, char16 c, size_t n) {
while (n > 0) {
if (*s == c) {
return s;
}
return 0;
++s;
--n;
}
return 0;
}
inline char16 *char16_wmemset(char16 *s, char16 c, size_t n) {
char16 *s_orig = s;
while (n > 0) {
*s = c;
++s; --n;
}
return s_orig;
inline char16 *char16_wmemset(char16 *s, char16 c, size_t n) {
char16 *s_orig = s;
while (n > 0) {
*s = c;
++s;
--n;
}
return s_orig;
}
inline size_t char16_wcslen(const char16 *s) {
const char16 *s_orig = s;
while (*s) { ++s; }
return (s - s_orig);
}
inline size_t char16_wcslen(const char16 *s) {
const char16 *s_orig = s;
while (*s)
++s;
return (s - s_orig);
}
} // END: extern "C"
} // extern "C"
// Definition of char_traits<char16>, which enables basic_string<char16>
@ -147,71 +131,69 @@ extern "C" {
// This is a slightly modified version of char_traits<wchar_t> from gcc 3.2.2
namespace std {
template<>
struct char_traits<char16>
{
typedef char16 char_type;
typedef wint_t int_type;
typedef streamoff off_type;
typedef wstreampos pos_type;
typedef mbstate_t state_type;
template<> struct char_traits<char16> {
typedef char16 char_type;
typedef wint_t int_type;
typedef streamoff off_type;
typedef wstreampos pos_type;
typedef mbstate_t state_type;
static void
assign(char_type& __c1, const char_type& __c2)
{ __c1 = __c2; }
static void assign(char_type& c1, const char_type& c2) {
c1 = c2;
}
static bool
eq(const char_type& __c1, const char_type& __c2)
{ return __c1 == __c2; }
static bool eq(const char_type& c1, const char_type& c2) {
return c1 == c2;
}
static bool lt(const char_type& c1, const char_type& c2) {
return c1 < c2;
}
static bool
lt(const char_type& __c1, const char_type& __c2)
{ return __c1 < __c2; }
static int compare(const char_type* s1, const char_type* s2, size_t n) {
return char16_wmemcmp(s1, s2, n);
}
static int
compare(const char_type* __s1, const char_type* __s2, size_t __n)
{ return char16_wmemcmp(__s1, __s2, __n); }
static size_t length(const char_type* s) {
return char16_wcslen(s);
}
static size_t
length(const char_type* __s)
{ return char16_wcslen(__s); }
static const char_type* find(const char_type* s, size_t n,
const char_type& a) {
return char16_wmemchr(s, a, n);
}
static const char_type*
find(const char_type* __s, size_t __n, const char_type& __a)
{ return char16_wmemchr(__s, __a, __n); }
static char_type* move(char_type* s1, const char_type* s2, int_type n) {
return char16_wmemmove(s1, s2, n);
}
static char_type*
move(char_type* __s1, const char_type* __s2, int_type __n)
{ return char16_wmemmove(__s1, __s2, __n); }
static char_type* copy(char_type* s1, const char_type* s2, size_t n) {
return char16_wmemcpy(s1, s2, n);
}
static char_type*
copy(char_type* __s1, const char_type* __s2, size_t __n)
{ return char16_wmemcpy(__s1, __s2, __n); }
static char_type* assign(char_type* s, size_t n, char_type a) {
return char16_wmemset(s, a, n);
}
static char_type*
assign(char_type* __s, size_t __n, char_type __a)
{ return char16_wmemset(__s, __a, __n); }
static char_type to_char_type(const int_type& c) {
return char_type(c);
}
static int_type to_int_type(const char_type& c) {
return int_type(c);
}
static bool eq_int_type(const int_type& c1, const int_type& c2) {
return c1 == c2;
}
static char_type
to_char_type(const int_type& __c) { return char_type(__c); }
static int_type eof() {
return static_cast<int_type>(WEOF);
}
static int_type not_eof(const int_type& c) {
return eq_int_type(c, eof()) ? 0 : c;
}
};
static int_type
to_int_type(const char_type& __c) { return int_type(__c); }
} // namespace std
static bool
eq_int_type(const int_type& __c1, const int_type& __c2)
{ return __c1 == __c2; }
#endif // !WCHAR_T_IS_UTF16
static int_type
eof() { return static_cast<int_type>(WEOF); }
static int_type
not_eof(const int_type& __c)
{ return eq_int_type(__c, eof()) ? 0 : __c; }
};
} // END: namespace std
#endif // END: WIN32
#endif // END: BASE_STRING16_H__
#endif // BASE_STRING16_H_

@ -29,8 +29,8 @@
//
// This file defines utility functions for working with strings.
#ifndef BASE_STRING_UTIL_H__
#define BASE_STRING_UTIL_H__
#ifndef BASE_STRING_UTIL_H_
#define BASE_STRING_UTIL_H_
#include <string>
#include <vector>
@ -76,9 +76,9 @@ int SWPrintF(wchar_t* buffer, size_t size, const wchar_t* format, ...);
// Some of these implementations need to be inlined.
#if defined(WIN32)
#if defined(OS_WIN)
#include "base/string_util_win.h"
#elif defined(__APPLE__)
#elif defined(OS_MACOSX)
#include "base/string_util_mac.h"
#else
#error Define string operations appropriately for your platform
@ -532,4 +532,4 @@ std::wstring ReplaceStringPlaceholders(const std::wstring& format_string,
bool MatchPattern(const std::wstring& string, const std::wstring& pattern);
bool MatchPattern(const std::string& string, const std::string& pattern);
#endif // BASE_STRING_UTIL_H__
#endif // BASE_STRING_UTIL_H_

@ -62,9 +62,8 @@ bool ReadUnicodeCharacter(const char* src, int32 src_len,
return U_IS_UNICODE_CHAR(*code_point);
}
#ifdef WIN32
// Reads a UTF-16 character for Windows. The usage is the same as the 8-bit
// version above.
#if defined(WCHAR_T_IS_UTF16)
// Reads a UTF-16 character. The usage is the same as the 8-bit version above.
bool ReadUnicodeCharacter(const wchar_t* src, int32 src_len,
int32* char_index, uint32* code_point) {
if (U16_IS_SURROGATE(src[*char_index])) {
@ -86,10 +85,9 @@ bool ReadUnicodeCharacter(const wchar_t* src, int32 src_len,
return U_IS_UNICODE_CHAR(*code_point);
}
#else
// Reads a 32-bit character for Mac and Linux systems. The usage is the same as
// the 8-bit version above.
bool ReadUnicodeCharacter(const wchar_t* src, in32 src_len,
#elif defined(WCHAR_T_IS_UTF32)
// Reads UTF-32 character. The usage is the same as the 8-bit version above.
bool ReadUnicodeCharacter(const wchar_t* src, int32 src_len,
int32* char_index, uint32* code_point) {
// Conversion is easy since the source is 32-bit.
*code_point = src[*char_index];
@ -97,7 +95,7 @@ bool ReadUnicodeCharacter(const wchar_t* src, in32 src_len,
// Validate the value.
return U_IS_UNICODE_CHAR(*code_point);
}
#endif
#endif // defined(WCHAR_T_IS_UTF32)
// WriteUnicodeCharacter -------------------------------------------------------
@ -120,9 +118,8 @@ void WriteUnicodeCharacter(uint32 code_point, std::basic_string<char>* output) {
output->resize(char_offset);
}
#ifdef WIN32
// Appends the given code point as a UTF-16 character to the STL string. On
// Windows, wchar_t is UTF-16.
#if defined(WCHAR_T_IS_UTF16)
// Appends the given code point as a UTF-16 character to the STL string.
void WriteUnicodeCharacter(uint32 code_point,
std::basic_string<wchar_t>* output) {
if (U16_LENGTH(code_point) == 1) {
@ -135,15 +132,14 @@ void WriteUnicodeCharacter(uint32 code_point,
U16_APPEND_UNSAFE(&(*output)[0], char_offset, code_point);
}
}
#else
// Appends the given UCS-4 character to the given 32-bit string for Linux and
// Mac where wchar_t is UCS-4.
#elif defined(WCHAR_T_IS_UTF32)
// Appends the given UTF-32 character to the given 32-bit string.
inline void WriteUnicodeCharacter(uint32 code_point,
std::basic_string<wchar_t>* output) {
// This is the easy case, just append the character.
output->push_back(code_point);
}
#endif
#endif // defined(WCHAR_T_IS_UTF32)
// Generalized Unicode converter -----------------------------------------------
@ -245,10 +241,10 @@ bool WideToCodepage(const std::wstring& wide,
const UChar* uchar_src;
int uchar_len;
#ifdef U_WCHAR_IS_UTF16
#if defined(WCHAR_T_IS_UTF16)
uchar_src = wide.c_str();
uchar_len = static_cast<int>(wide.length());
#else // U_WCHAR_IS_UTF16
#elif defined(WCHAR_T_IS_UTF32)
// When wchar_t is wider than UChar (16 bits), transform |wide| into a
// UChar* string. Size the UChar* buffer to be large enough to hold twice
// as many UTF-16 code points as there are UCS-4 characters, in case each
@ -259,7 +255,7 @@ bool WideToCodepage(const std::wstring& wide,
wide.c_str(), wide.length(), &status);
uchar_src = &wide_uchar[0];
DCHECK(U_SUCCESS(status)) << "failed to convert wstring to UChar*";
#endif // U_WCHAR_IS_UTF16
#endif // defined(WCHAR_T_IS_UTF32)
int encoded_max_length = UCNV_GET_MAX_BYTES_FOR_STRING(uchar_len,
ucnv_getMaxCharSize(converter));
@ -307,14 +303,14 @@ bool CodepageToWide(const std::string& encoded,
size_t uchar_max_length = encoded.length() * 2 + 1;
UChar* uchar_dst;
#ifdef U_WCHAR_IS_UTF16
#if defined(WCHAR_T_IS_UTF16)
uchar_dst = WriteInto(wide, uchar_max_length);
#else
#elif defined(WCHAR_T_IS_UTF32)
// When wchar_t is wider than UChar (16 bits), convert into a temporary
// UChar* buffer.
std::vector<UChar> wide_uchar(uchar_max_length);
uchar_dst = &wide_uchar[0];
#endif // U_WCHAR_IS_UTF16
#endif // defined(WCHAR_T_IS_UTF32)
// Setup our error handler.
switch (on_error) {
@ -342,7 +338,7 @@ bool CodepageToWide(const std::string& encoded,
return false;
}
#ifndef U_WCHAR_IS_UTF16
#ifdef WCHAR_T_IS_UTF32
// When wchar_t is wider than UChar (16 bits), it's not possible to wind up
// with any more wchar_t elements than UChar elements. ucnv_toUChars
// returns the number of UChar elements not including the NUL terminator, so
@ -350,7 +346,7 @@ bool CodepageToWide(const std::string& encoded,
u_strToWCS(WriteInto(wide, actual_size + 1), actual_size + 1, &actual_size,
uchar_dst, actual_size, &status);
DCHECK(U_SUCCESS(status)) << "failed to convert UChar* to wstring";
#endif // U_WCHAR_IS_UTF16
#endif // WCHAR_T_IS_UTF32
wide->resize(actual_size);
return true;
@ -372,10 +368,10 @@ std::wstring FormatNumber(int64 number) {
UnicodeString ustr;
number_format->format(number, ustr);
#ifdef U_WCHAR_IS_UTF16
#if defined(WCHAR_T_IS_UTF16)
return std::wstring(ustr.getBuffer(),
static_cast<std::wstring::size_type>(ustr.length()));
#else // U_WCHAR_IS_UTF16
#elif defined(WCHAR_T_IS_UTF32)
wchar_t buffer[64]; // A int64 is less than 20 chars long, so 64 chars
// leaves plenty of room for formating stuff.
int length = 0;
@ -387,5 +383,5 @@ std::wstring FormatNumber(int64 number) {
return StringPrintf(L"%lld", number);
}
return std::wstring(buffer, static_cast<std::wstring::size_type>(length));
#endif // U_WCHAR_IS_UTF16
#endif // defined(WCHAR_T_IS_UTF32)
}

@ -153,9 +153,9 @@ static const wchar_t* const kConvertRoundtripCases[] = {
// Test a character that takes more than 16-bits. This will depend on whether
// wchar_t is 16 or 32 bits.
#ifdef WIN32
#if defined(WCHAR_T_IS_UTF16)
L"\xd800\xdf00",
#else
#elif defined(WCHAR_T_IS_UTF32)
"\x10300,
#endif
};
@ -207,9 +207,9 @@ TEST(StringUtilTest, ConvertUTF8ToWide) {
// This UTF-8 character decodes to a UTF-16 surrogate, which is illegal.
{"\xed\xb0\x80", L"", false},
// Non-BMP character. The result will either be in UTF-16 or UCS-4.
#ifdef WIN32
#if defined(WCHAR_T_IS_UTF16)
{"A\xF0\x90\x8C\x80z", L"A\xd800\xdf00z", true},
#else
#elif defined(WCHAR_T_IS_UTF32)
{"A\xF0\x90\x8C\x80z", L"A\x10300z", true},
#endif
};
@ -238,7 +238,7 @@ TEST(StringUtilTest, ConvertUTF8ToWide) {
EXPECT_EQ('B', converted[0]);
}
#ifdef WIN32
#if defined(WCHAR_T_IS_UTf16)
// This test is only valid when wchar_t == UTF-16.
TEST(StringUtilTest, ConvertUTF16ToUTF8) {
struct UTF16ToUTF8Case {
@ -269,7 +269,7 @@ TEST(StringUtilTest, ConvertUTF16ToUTF8) {
}
}
#else
#elif defined(WCHAR_T_IS_UTF32)
// This test is only valid when wchar_t == UCS-4.
TEST(StringUtilTest, ConvertUCS4ToUTF8) {
struct UTF8ToWideCase {
@ -298,7 +298,7 @@ TEST(StringUtilTest, ConvertUCS4ToUTF8) {
EXPECT_EQ(expected, converted);
}
}
#endif
#endif // defined(WCHAR_T_IS_UTF32)
TEST(StringUtilTest, ConvertMultiString) {
static wchar_t wmulti[] = {
@ -467,7 +467,7 @@ TEST(StringUtilTest, ConvertBetweenCodepageAndWide) {
OnStringUtilConversionError::SKIP, &encoded));
EXPECT_STREQ("Chinese", encoded.c_str());
#ifdef WIN32
#if defined(WCHAR_T_IS_UTF16)
// When we're in UTF-16 mode, test an invalid UTF-16 character in the input.
EXPECT_FALSE(WideToCodepage(L"a\xd800z", "iso-8859-1",
OnStringUtilConversionError::FAIL, &encoded));
@ -475,7 +475,7 @@ TEST(StringUtilTest, ConvertBetweenCodepageAndWide) {
EXPECT_TRUE(WideToCodepage(L"a\xd800z", "iso-8859-1",
OnStringUtilConversionError::SKIP, &encoded));
EXPECT_STREQ("az", encoded.c_str());
#endif
#endif // WCHAR_T_IS_UTf16
// Invalid characters should fail.
EXPECT_TRUE(WideToCodepage(L"a\xffffz", "iso-8859-1",

@ -46,13 +46,13 @@ std::wstring SysUTF8ToWide(const std::string& utf8);
// Converts between wide and the system multi-byte representations of a string.
// DANGER: This will lose information and can change (on Windows, this can
// change between reboots),
// change between reboots).
std::string SysWideToNativeMB(const std::wstring& wide);
std::wstring SysNativeMBToWide(const std::string& native_mb);
// Windows-specific ------------------------------------------------------------
#ifdef WIN32
#if defined(OS_WIN)
// Converts between an 8-bit string into a wide string, using the given
// code page. The code page identifier is one accepted by the Windows function
@ -60,7 +60,7 @@ std::wstring SysNativeMBToWide(const std::string& native_mb);
std::wstring SysMultiByteToWide(const std::string& mb, uint32 code_page);
std::string SysWideToMultiByte(const std::wstring& wide, uint32 code_page);
#endif // WIN32
#endif // defined(OS_WIN)
} // namespace base

@ -44,19 +44,19 @@
// These classes are represented as only a 64-bit value, so they can be
// efficiently passed by value.
#ifndef BASE_TIME_H__
#define BASE_TIME_H__
#ifdef WIN32
// For FILETIME in FromFileTime, until it moves to a new converter class.
// See TODO(iyengar) below.
#include <windows.h>
#endif
#ifndef BASE_TIME_H_
#define BASE_TIME_H_
#include <time.h>
#include "base/basictypes.h"
#include "testing/gtest/include/gtest/gtest_prod.h"
#if defined(OS_WIN)
// For FILETIME in FromFileTime, until it moves to a new converter class.
// See TODO(iyengar) below.
#include <windows.h>
#endif
class Time;
class TimeTicks;
@ -236,7 +236,7 @@ class Time {
// (Jan 1, 1970). Webkit uses this format to represent time.
double ToDoubleT() const;
#ifdef WIN32
#if defined(OS_WIN)
static Time FromFileTime(FILETIME ft);
FILETIME ToFileTime() const;
#endif
@ -455,7 +455,7 @@ class TimeTicks {
// Tick count in microseconds.
int64 ticks_;
#ifdef WIN32
#if defined(OS_WIN)
// The function to use for counting ticks.
typedef int (__stdcall *TickFunction)(void);
static TickFunction tick_function_;
@ -466,4 +466,4 @@ inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
return TimeTicks(t.ticks_ + delta_);
}
#endif // BASE_TIME_H__
#endif // BASE_TIME_H_

@ -80,4 +80,14 @@
#error Please add support for your architecture in build/build_config.h
#endif
// Type detection for wchar_t.
#if defined(OS_WIN)
#define WCHAR_T_IS_UTF16
#elif defined(OS_POSIX) && defined(COMPILER_GCC) && \
defined(__WCHAR_MAX__) && __WCHAR_MAX__ == 0x7fffffff
#define WCHAR_T_IS_UTF32
#else
#error Please add support for your compiler in build/build_config.h
#endif
#endif // BUILD_BUILD_CONFIG_H_