OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2013 Apple Inc. All rights | |
3 * reserved. | |
4 * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com> | |
5 * Copyright (C) 2012 Google Inc. All rights reserved. | |
6 * | |
7 * This library is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Library General Public | |
9 * License as published by the Free Software Foundation; either | |
10 * version 2 of the License, or (at your option) any later version. | |
11 * | |
12 * This library is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Library General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Library General Public License | |
18 * along with this library; see the file COPYING.LIB. If not, write to | |
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
20 * Boston, MA 02110-1301, USA. | |
21 * | |
22 */ | |
23 | |
24 #include "wtf/text/AtomicString.h" | |
25 | |
26 #include "wtf/dtoa.h" | |
27 #include "wtf/text/AtomicStringTable.h" | |
28 #include "wtf/text/IntegerToStringConversion.h" | |
29 #include "wtf/text/StringImpl.h" | |
30 | |
31 namespace WTF { | |
32 | |
33 static_assert(sizeof(AtomicString) == sizeof(String), | |
34 "AtomicString and String must be same size"); | |
35 | |
36 AtomicString::AtomicString(const LChar* chars, unsigned length) | |
37 : m_string(AtomicStringTable::instance().add(chars, length)) {} | |
38 | |
39 AtomicString::AtomicString(const UChar* chars, unsigned length) | |
40 : m_string(AtomicStringTable::instance().add(chars, length)) {} | |
41 | |
42 AtomicString::AtomicString(const UChar* chars) | |
43 : m_string(AtomicStringTable::instance().add( | |
44 chars, | |
45 chars ? lengthOfNullTerminatedString(chars) : 0)) {} | |
46 | |
47 PassRefPtr<StringImpl> AtomicString::addSlowCase(StringImpl* string) { | |
48 DCHECK(!string->isAtomic()); | |
49 return AtomicStringTable::instance().add(string); | |
50 } | |
51 | |
52 AtomicString AtomicString::fromUTF8(const char* chars, size_t length) { | |
53 if (!chars) | |
54 return nullAtom; | |
55 if (!length) | |
56 return emptyAtom; | |
57 return AtomicString( | |
58 AtomicStringTable::instance().addUTF8(chars, chars + length)); | |
59 } | |
60 | |
61 AtomicString AtomicString::fromUTF8(const char* chars) { | |
62 if (!chars) | |
63 return nullAtom; | |
64 if (!*chars) | |
65 return emptyAtom; | |
66 return AtomicString(AtomicStringTable::instance().addUTF8(chars, nullptr)); | |
67 } | |
68 | |
69 AtomicString AtomicString::lower() const { | |
70 // Note: This is a hot function in the Dromaeo benchmark. | |
71 StringImpl* impl = this->impl(); | |
72 if (UNLIKELY(!impl)) | |
73 return *this; | |
74 RefPtr<StringImpl> newImpl = impl->lower(); | |
75 if (LIKELY(newImpl == impl)) | |
76 return *this; | |
77 return AtomicString(newImpl.release()); | |
78 } | |
79 | |
80 AtomicString AtomicString::lowerASCII() const { | |
81 StringImpl* impl = this->impl(); | |
82 if (UNLIKELY(!impl)) | |
83 return *this; | |
84 RefPtr<StringImpl> newImpl = impl->lowerASCII(); | |
85 if (LIKELY(newImpl == impl)) | |
86 return *this; | |
87 return AtomicString(newImpl.release()); | |
88 } | |
89 | |
90 AtomicString AtomicString::upperASCII() const { | |
91 StringImpl* impl = this->impl(); | |
92 if (UNLIKELY(!impl)) | |
93 return *this; | |
94 return AtomicString(impl->upperASCII()); | |
95 } | |
96 | |
97 template <typename IntegerType> | |
98 static AtomicString integerToAtomicString(IntegerType input) { | |
99 IntegerToStringConverter<IntegerType> converter(input); | |
100 return AtomicString(converter.characters8(), converter.length()); | |
101 } | |
102 | |
103 AtomicString AtomicString::number(int number) { | |
104 return integerToAtomicString(number); | |
105 } | |
106 | |
107 AtomicString AtomicString::number(unsigned number) { | |
108 return integerToAtomicString(number); | |
109 } | |
110 | |
111 AtomicString AtomicString::number(long number) { | |
112 return integerToAtomicString(number); | |
113 } | |
114 | |
115 AtomicString AtomicString::number(unsigned long number) { | |
116 return integerToAtomicString(number); | |
117 } | |
118 | |
119 AtomicString AtomicString::number(long long number) { | |
120 return integerToAtomicString(number); | |
121 } | |
122 | |
123 AtomicString AtomicString::number(unsigned long long number) { | |
124 return integerToAtomicString(number); | |
125 } | |
126 | |
127 AtomicString AtomicString::number(double number, unsigned precision) { | |
128 NumberToStringBuffer buffer; | |
129 return AtomicString(numberToFixedPrecisionString(number, precision, buffer)); | |
130 } | |
131 | |
132 std::ostream& operator<<(std::ostream& out, const AtomicString& s) { | |
133 return out << s.getString(); | |
134 } | |
135 | |
136 #ifndef NDEBUG | |
137 void AtomicString::show() const { | |
138 m_string.show(); | |
139 } | |
140 #endif | |
141 | |
142 } // namespace WTF | |
OLD | NEW |