1 /* 2 * Copyright (C) 2007 Vaclav Slavik, Kevin Ollivier <kevino (at) theolliviers.com> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "PlatformString.h" 28 29 #include "CString.h" 30 #include "unicode/ustring.h" 31 32 #include <wx/defs.h> 33 #include <wx/string.h> 34 35 namespace WebCore { 36 37 // String conversions 38 String::String(const wxString& wxstr) 39 { 40 #if !wxUSE_UNICODE 41 #error "This code only works in Unicode build of wxWidgets" 42 #endif 43 44 // ICU's UChar is 16bit wide, UTF-16, and the code below counts on it, so 45 // it would break if the definition changed: 46 wxCOMPILE_TIME_ASSERT(sizeof(UChar) == 2, UCharSizeMustBe16Bit); 47 48 #if SIZEOF_WCHAR_T == 2 // wchar_t==UChar 49 50 const UChar* str = wxstr.wc_str(); 51 const size_t len = wxstr.length(); 52 53 #else // SIZEOF_WCHAR_T == 4 54 55 // NB: we can't simply use wxstr.mb_str(wxMBConvUTF16()) here because 56 // the number of characters in UTF-16 encoding of the string may differ 57 // from the number of UTF-32 values and we can't get the length from 58 // returned buffer: 59 60 #if defined(wxUSE_UNICODE_UTF8) && wxUSE_UNICODE_UTF8 61 // in wx3's UTF8 mode, wc_str() returns a buffer, not raw pointer 62 wxWCharBuffer widestr(wxstr.wc_str()); 63 #else 64 const wxChar *widestr = wxstr.wc_str(); 65 #endif 66 const size_t widelen = wxstr.length(); 67 68 // allocate buffer for the UTF-16 string: 69 wxMBConvUTF16 conv; 70 const size_t utf16bufLen = conv.FromWChar(NULL, 0, widestr, widelen); 71 wxCharBuffer utf16buf(utf16bufLen); 72 73 // and convert wxString to UTF-16 (=UChar*): 74 const UChar* str = (const UChar*)utf16buf.data(); 75 size_t len = conv.FromWChar(utf16buf.data(), utf16bufLen, widestr, widelen) / 2; 76 77 #endif // SIZEOF_WCHAR_T == 4 78 79 // conversion to UTF-16 or getting internal buffer isn't supposed to fail: 80 wxASSERT_MSG(str != NULL, _T("failed string conversion?")); 81 82 m_impl = StringImpl::create(str, len); 83 } 84 85 String::operator wxString() const 86 { 87 return wxString(utf8().data(), wxConvUTF8); 88 } 89 90 } 91 92 // vim: ts=4 sw=4 et 93