1/**
2 * Copyright (C) 2006, 2012 Apple Computer, Inc.
3 *
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.
8 *
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.
13 *
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.
18 *
19 */
20
21#include "config.h"
22
23#include "wtf/text/WTFString.h"
24
25#if USE(CF)
26
27#include "wtf/RetainPtr.h"
28#include <CoreFoundation/CoreFoundation.h>
29
30namespace WTF {
31
32String::String(CFStringRef str)
33{
34    if (!str)
35        return;
36
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;
47        }
48
49        Vector<UChar, 1024> buffer(size);
50        CFStringGetCharacters(str, CFRangeMake(0, size), (UniChar*)buffer.data());
51        m_impl = StringImpl::create(buffer.data(), size);
52    }
53}
54
55RetainPtr<CFStringRef> String::createCFString() const
56{
57    if (!m_impl)
58        return CFSTR("");
59
60    return m_impl->createCFString();
61}
62
63}
64
65#endif // USE(CF)
66