2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "PlatformString.h"
32 #include <wtf/text/AtomicString.h>
35 #include <CoreFoundation/CoreFoundation.h>
47 BString::BString(const wchar_t* characters)
52 m_bstr = SysAllocString(characters);
55 BString::BString(const wchar_t* characters, size_t length)
60 m_bstr = SysAllocStringLen(characters, length);
63 BString::BString(const String& s)
68 m_bstr = SysAllocStringLen(s.characters(), s.length());
71 BString::BString(const KURL& url)
76 m_bstr = SysAllocStringLen(url.string().characters(), url.string().length());
79 BString::BString(const AtomicString& s)
84 m_bstr = SysAllocStringLen(s.characters(), s.length());
87 BString::BString(const UString& s)
92 m_bstr = SysAllocStringLen(s.characters(), s.length());
96 BString::BString(CFStringRef cfstr)
102 const UniChar* uniChars = CFStringGetCharactersPtr(cfstr);
104 m_bstr = SysAllocStringLen((LPCWSTR)uniChars, CFStringGetLength(cfstr));
108 CFIndex length = CFStringGetLength(cfstr);
109 m_bstr = SysAllocStringLen(0, length);
110 CFStringGetCharacters(cfstr, CFRangeMake(0, length), (UniChar*)m_bstr);
117 SysFreeString(m_bstr);
120 BString::BString(const BString& other)
125 m_bstr = SysAllocString(other.m_bstr);
128 void BString::adoptBSTR(BSTR bstr)
131 SysFreeString(m_bstr);
135 BString& BString::operator=(const BString& other)
138 *this = other.m_bstr;
142 BString& BString::operator=(const BSTR& other)
144 if (other != m_bstr) {
145 SysFreeString(m_bstr);
146 m_bstr = other ? SysAllocString(other) : 0;
152 bool operator ==(const BString& a, const BString& b)
154 if (SysStringLen((BSTR)a) != SysStringLen((BSTR)b))
156 if (!(BSTR)a && !(BSTR)b)
158 if (!(BSTR)a || !(BSTR)b)
160 return !wcscmp((BSTR)a, (BSTR)b);
163 bool operator !=(const BString& a, const BString& b)
168 bool operator ==(const BString& a, BSTR b)
170 if (SysStringLen((BSTR)a) != SysStringLen(b))
176 return !wcscmp((BSTR)a, b);
179 bool operator !=(const BString& a, BSTR b)
184 bool operator ==(BSTR a, const BString& b)
186 if (SysStringLen(a) != SysStringLen((BSTR)b))
192 return !wcscmp(a, (BSTR)b);
195 bool operator !=(BSTR a, const BString& b)