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#include <CoreFoundation/CoreFoundation.h>
27#include <wtf/RetainPtr.h>
28
29namespace WTF {
30
31String::String(CFStringRef str)
32{
33    if (!str)
34        return;
35
36    CFIndex size = CFStringGetLength(str);
37    if (size == 0)
38        m_impl = StringImpl::empty();
39    else {
40        Vector<LChar, 1024> lcharBuffer(size);
41        CFIndex usedBufLen;
42        CFIndex convertedsize = CFStringGetBytes(str, CFRangeMake(0, size), kCFStringEncodingISOLatin1, 0, false, lcharBuffer.data(), size, &usedBufLen);
43        if ((convertedsize == size) && (usedBufLen == size)) {
44            m_impl = StringImpl::create(lcharBuffer.data(), size);
45            return;
46        }
47
48        Vector<UChar, 1024> buffer(size);
49        CFStringGetCharacters(str, CFRangeMake(0, size), (UniChar*)buffer.data());
50        m_impl = StringImpl::create(buffer.data(), size);
51    }
52}
53
54RetainPtr<CFStringRef> String::createCFString() const
55{
56    if (!m_impl)
57        return CFSTR("");
58
59    return m_impl->createCFString();
60}
61
62}
63
64#endif // USE(CF)
65