Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / text / StringCF.cpp
blobeb55a91424cbb11ee15ac31ccdc22ff2707c5074
1 /**
2 * Copyright (C) 2006, 2012 Apple Computer, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "config.h"
23 #include "wtf/text/WTFString.h"
25 #if USE(CF)
27 #include "wtf/RetainPtr.h"
28 #include <CoreFoundation/CoreFoundation.h>
30 namespace WTF {
32 String::String(CFStringRef str)
34 if (!str)
35 return;
37 CFIndex size = CFStringGetLength(str);
38 if (!size) {
39 m_impl = StringImpl::empty();
40 } else {
41 Vector<LChar, 1024> lcharBuffer(size);
42 CFIndex usedBufLen;
43 CFIndex convertedsize = CFStringGetBytes(str, CFRangeMake(0, size), kCFStringEncodingISOLatin1, 0, false, lcharBuffer.data(), size, &usedBufLen);
44 if ((convertedsize == size) && (usedBufLen == size)) {
45 m_impl = StringImpl::create(lcharBuffer.data(), size);
46 return;
49 Vector<UChar, 1024> buffer(size);
50 CFStringGetCharacters(str, CFRangeMake(0, size), (UniChar*)buffer.data());
51 m_impl = StringImpl::create(buffer.data(), size);
55 RetainPtr<CFStringRef> String::createCFString() const
57 if (!m_impl)
58 return CFSTR("");
60 return m_impl->createCFString();
65 #endif // USE(CF)