1/*
2 * Copyright 2014 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7#include "config.h"
8#include "StringConcatenate.h"
9
10// This macro is helpful for testing how many intermediate Strings are created while evaluating an
11// expression containing operator+.
12#ifndef WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING
13#define WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING() ((void)0)
14#endif
15
16void WTF::StringTypeAdapter<char*>::writeTo(LChar* destination)
17{
18    for (unsigned i = 0; i < m_length; ++i)
19        destination[i] = static_cast<LChar>(m_buffer[i]);
20}
21
22void WTF::StringTypeAdapter<char*>::writeTo(UChar* destination)
23{
24    for (unsigned i = 0; i < m_length; ++i) {
25        unsigned char c = m_buffer[i];
26        destination[i] = c;
27    }
28}
29
30WTF::StringTypeAdapter<LChar*>::StringTypeAdapter(LChar* buffer)
31    : m_buffer(buffer)
32    , m_length(strlen(reinterpret_cast<char*>(buffer)))
33{
34}
35
36void WTF::StringTypeAdapter<LChar*>::writeTo(LChar* destination)
37{
38    memcpy(destination, m_buffer, m_length * sizeof(LChar));
39}
40
41void WTF::StringTypeAdapter<LChar*>::writeTo(UChar* destination)
42{
43    StringImpl::copyChars(destination, m_buffer, m_length);
44}
45
46WTF::StringTypeAdapter<const UChar*>::StringTypeAdapter(const UChar* buffer)
47    : m_buffer(buffer)
48{
49    size_t len = 0;
50    while (m_buffer[len] != UChar(0))
51        ++len;
52
53    RELEASE_ASSERT(len <= std::numeric_limits<unsigned>::max());
54
55    m_length = len;
56}
57
58void WTF::StringTypeAdapter<const UChar*>::writeTo(UChar* destination)
59{
60    memcpy(destination, m_buffer, m_length * sizeof(UChar));
61}
62
63WTF::StringTypeAdapter<const char*>::StringTypeAdapter(const char* buffer)
64: m_buffer(buffer)
65, m_length(strlen(buffer))
66{
67}
68
69void WTF::StringTypeAdapter<const char*>::writeTo(LChar* destination)
70{
71    memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LChar));
72}
73
74void WTF::StringTypeAdapter<const char*>::writeTo(UChar* destination)
75{
76    for (unsigned i = 0; i < m_length; ++i) {
77        unsigned char c = m_buffer[i];
78        destination[i] = c;
79    }
80}
81
82WTF::StringTypeAdapter<const LChar*>::StringTypeAdapter(const LChar* buffer)
83: m_buffer(buffer)
84, m_length(strlen(reinterpret_cast<const char*>(buffer)))
85{
86}
87
88void WTF::StringTypeAdapter<const LChar*>::writeTo(LChar* destination)
89{
90    memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LChar));
91}
92
93void WTF::StringTypeAdapter<const LChar*>::writeTo(UChar* destination)
94{
95    StringImpl::copyChars(destination, m_buffer, m_length);
96}
97
98void WTF::StringTypeAdapter<Vector<char> >::writeTo(LChar* destination)
99{
100    for (size_t i = 0; i < m_buffer.size(); ++i)
101        destination[i] = static_cast<unsigned char>(m_buffer[i]);
102}
103
104void WTF::StringTypeAdapter<Vector<char> >::writeTo(UChar* destination)
105{
106    for (size_t i = 0; i < m_buffer.size(); ++i)
107        destination[i] = static_cast<unsigned char>(m_buffer[i]);
108}
109
110void WTF::StringTypeAdapter<Vector<LChar> >::writeTo(LChar* destination)
111{
112    for (size_t i = 0; i < m_buffer.size(); ++i)
113        destination[i] = m_buffer[i];
114}
115
116void WTF::StringTypeAdapter<Vector<LChar> >::writeTo(UChar* destination)
117{
118    for (size_t i = 0; i < m_buffer.size(); ++i)
119        destination[i] = m_buffer[i];
120}
121
122void WTF::StringTypeAdapter<String>::writeTo(LChar* destination)
123{
124    unsigned length = m_buffer.length();
125
126    ASSERT(is8Bit());
127    const LChar* data = m_buffer.characters8();
128    for (unsigned i = 0; i < length; ++i)
129        destination[i] = data[i];
130
131    WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING();
132}
133
134void WTF::StringTypeAdapter<String>::writeTo(UChar* destination)
135{
136    unsigned length = m_buffer.length();
137
138    if (is8Bit()) {
139        const LChar* data = m_buffer.characters8();
140        for (unsigned i = 0; i < length; ++i)
141            destination[i] = data[i];
142    } else {
143        const UChar* data = m_buffer.characters16();
144        for (unsigned i = 0; i < length; ++i)
145            destination[i] = data[i];
146    }
147
148    WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING();
149}
150
151