source: webkit/trunk/JavaScriptCore/kjs/ustring.h@ 37240

Last change on this file since 37240 was 37089, checked in by [email protected], 17 years ago

2008-09-29 Maciej Stachowiak <[email protected]>

Reviewed by Darin Adler.



It's pretty common in real-world code (and on some of the v8
benchmarks) to append a number to a string, so I made this one of
the fast cases, and also added support to UString to do it
directly without allocating a temporary UString.


~1% speedup on v8 benchmark.

  • VM/Machine.cpp: (JSC::jsAddSlowCase): Make this NEVER_INLINE because somehow otherwise the change is a regression. (JSC::jsAdd): Handle number + string special case. (JSC::Machine::cti_op_add): Integrate much of the logic of jsAdd to avoid exception check in the str + str, num + num and str + num cases.
  • kjs/ustring.cpp: (JSC::expandedSize): Make this a non-member function, since it needs to be called in non-member functions but not outside this file. (JSC::expandCapacity): Ditto. (JSC::UString::expandCapacity): Call the non-member version. (JSC::createRep): Helper to make a rep from a char*. (JSC::UString::UString): Use above helper. (JSC::concatenate): Guts of concatenating constructor for cases where first item is a UString::Rep, and second is a UChar* and length, or a char*. (JSC::UString::append): Implement for cases where first item is a UString::Rep, and second is an int or double. Sadly duplicates logic of UString::from(int) and UString::from(double).
  • kjs/ustring.h:
  • Property svn:eol-style set to native
File size: 12.7 KB
Line 
1/*
2 * Copyright (C) 1999-2000 Harri Porten ([email protected])
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifndef _KJS_USTRING_H_
23#define _KJS_USTRING_H_
24
25#include "collector.h"
26#include <stdint.h>
27#include <string.h>
28#include <wtf/Assertions.h>
29#include <wtf/FastMalloc.h>
30#include <wtf/PassRefPtr.h>
31#include <wtf/RefPtr.h>
32#include <wtf/Vector.h>
33#include <wtf/unicode/Unicode.h>
34
35namespace JSC {
36
37 using WTF::PlacementNewAdoptType;
38 using WTF::PlacementNewAdopt;
39
40 class IdentifierTable;
41
42 class CString {
43 public:
44 CString()
45 : m_length(0)
46 , m_data(0)
47 {
48 }
49
50 CString(const char*);
51 CString(const char*, size_t);
52 CString(const CString&);
53
54 ~CString();
55
56 static CString adopt(char*, size_t); // buffer should be allocated with new[].
57
58 CString& append(const CString&);
59 CString& operator=(const char* c);
60 CString& operator=(const CString&);
61 CString& operator+=(const CString& c) { return append(c); }
62
63 size_t size() const { return m_length; }
64 const char* c_str() const { return m_data; }
65
66 private:
67 size_t m_length;
68 char* m_data;
69 };
70
71 typedef Vector<char, 32> CStringBuffer;
72
73 class UString {
74 friend class CTI;
75
76 public:
77 struct Rep {
78 friend class CTI;
79
80 static PassRefPtr<Rep> create(UChar*, int);
81 static PassRefPtr<Rep> createCopying(const UChar*, int);
82 static PassRefPtr<Rep> create(PassRefPtr<Rep> base, int offset, int length);
83
84 // Constructs a string from a UTF-8 string, using strict conversion (see comments in UTF8.h).
85 // Returns UString::Rep::null for null input or conversion failure.
86 static PassRefPtr<Rep> createFromUTF8(const char*);
87
88 void destroy();
89
90 bool baseIsSelf() const { return baseString == this; }
91 UChar* data() const { return baseString->buf + baseString->preCapacity + offset; }
92 int size() const { return len; }
93
94 unsigned hash() const { if (_hash == 0) _hash = computeHash(data(), len); return _hash; }
95 unsigned computedHash() const { ASSERT(_hash); return _hash; } // fast path for Identifiers
96
97 static unsigned computeHash(const UChar*, int length);
98 static unsigned computeHash(const char*, int length);
99 static unsigned computeHash(const char* s) { return computeHash(s, strlen(s)); }
100
101 IdentifierTable* identifierTable() const { return reinterpret_cast<IdentifierTable*>(m_identifierTable & ~static_cast<uintptr_t>(1)); }
102 void setIdentifierTable(IdentifierTable* table) { ASSERT(!isStatic()); m_identifierTable = reinterpret_cast<intptr_t>(table); }
103
104 bool isStatic() const { return m_identifierTable & 1; }
105 void setStatic(bool v) { ASSERT(!identifierTable()); m_identifierTable = v; }
106
107 Rep* ref() { ++rc; return this; }
108 ALWAYS_INLINE void deref() { if (--rc == 0) destroy(); }
109
110 void checkConsistency() const;
111
112 // unshared data
113 int offset;
114 int len;
115 int rc; // For null and empty static strings, this field does not reflect a correct count, because ref/deref are not thread-safe. A special case in destroy() guarantees that these do not get deleted.
116 mutable unsigned _hash;
117 intptr_t m_identifierTable; // A pointer to identifier table. The lowest bit is used to indicate whether the string is static (null or empty).
118 UString::Rep* baseString;
119 size_t reportedCost;
120
121 // potentially shared data. 0 if backed up by a base string.
122 UChar* buf;
123 int usedCapacity;
124 int capacity;
125 int usedPreCapacity;
126 int preCapacity;
127
128 static Rep null;
129 static Rep empty;
130 };
131
132 public:
133 UString();
134 UString(const char*);
135 UString(const UChar*, int length);
136 UString(UChar*, int length, bool copy);
137
138 UString(const UString& s)
139 : m_rep(s.m_rep)
140 {
141 }
142
143 UString(const Vector<UChar>& buffer);
144
145 ~UString()
146 {
147 }
148
149 // Special constructor for cases where we overwrite an object in place.
150 UString(PlacementNewAdoptType)
151 : m_rep(PlacementNewAdopt)
152 {
153 }
154
155 static UString from(int);
156 static UString from(unsigned int);
157 static UString from(long);
158 static UString from(double);
159
160 struct Range {
161 public:
162 Range(int pos, int len)
163 : position(pos)
164 , length(len)
165 {
166 }
167
168 Range()
169 {
170 }
171
172 int position;
173 int length;
174 };
175
176 UString spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const;
177
178 UString& append(const UString&);
179 UString& append(const char*);
180 UString& append(UChar);
181 UString& append(char c) { return append(static_cast<UChar>(static_cast<unsigned char>(c))); }
182 UString& append(const UChar*, int size);
183
184 bool getCString(CStringBuffer&) const;
185
186 // NOTE: This method should only be used for *debugging* purposes as it
187 // is neither Unicode safe nor free from side effects nor thread-safe.
188 char* ascii() const;
189
190 /**
191 * Convert the string to UTF-8, assuming it is UTF-16 encoded.
192 * In non-strict mode, this function is tolerant of badly formed UTF-16, it
193 * can create UTF-8 strings that are invalid because they have characters in
194 * the range U+D800-U+DDFF, U+FFFE, or U+FFFF, but the UTF-8 string is
195 * guaranteed to be otherwise valid.
196 * In strict mode, error is returned as null CString.
197 */
198 CString UTF8String(bool strict = false) const;
199
200 UString& operator=(const char*c);
201
202 UString& operator+=(const UString& s) { return append(s); }
203 UString& operator+=(const char* s) { return append(s); }
204
205 const UChar* data() const { return m_rep->data(); }
206
207 bool isNull() const { return (m_rep == &Rep::null); }
208 bool isEmpty() const { return (!m_rep->len); }
209
210 bool is8Bit() const;
211
212 int size() const { return m_rep->size(); }
213
214 UChar operator[](int pos) const;
215
216 double toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const;
217 double toDouble(bool tolerateTrailingJunk) const;
218 double toDouble() const;
219
220 uint32_t toUInt32(bool* ok = 0) const;
221 uint32_t toUInt32(bool* ok, bool tolerateEmptyString) const;
222 uint32_t toStrictUInt32(bool* ok = 0) const;
223
224 unsigned toArrayIndex(bool* ok = 0) const;
225
226 int find(const UString& f, int pos = 0) const;
227 int find(UChar, int pos = 0) const;
228 int rfind(const UString& f, int pos) const;
229 int rfind(UChar, int pos) const;
230
231 UString substr(int pos = 0, int len = -1) const;
232
233 static const UString& null();
234
235 Rep* rep() const { return m_rep.get(); }
236 static Rep* nullRep();
237
238 UString(PassRefPtr<Rep> r)
239 : m_rep(r)
240 {
241 ASSERT(m_rep);
242 }
243
244 size_t cost() const;
245
246 private:
247 int usedCapacity() const;
248 int usedPreCapacity() const;
249 void expandCapacity(int requiredLength);
250 void expandPreCapacity(int requiredPreCap);
251 void makeNull();
252
253 RefPtr<Rep> m_rep;
254
255 friend bool operator==(const UString&, const UString&);
256 friend PassRefPtr<Rep> concatenate(Rep*, Rep*); // returns 0 if out of memory
257 };
258 PassRefPtr<UString::Rep> concatenate(UString::Rep*, UString::Rep*);
259 PassRefPtr<UString::Rep> concatenate(UString::Rep*, int);
260 PassRefPtr<UString::Rep> concatenate(UString::Rep*, double);
261
262 bool operator==(const UString&, const UString&);
263
264 inline bool operator!=(const UString& s1, const UString& s2)
265 {
266 return !JSC::operator==(s1, s2);
267 }
268
269 bool operator<(const UString& s1, const UString& s2);
270 bool operator>(const UString& s1, const UString& s2);
271
272 bool operator==(const UString& s1, const char* s2);
273
274 inline bool operator!=(const UString& s1, const char* s2)
275 {
276 return !JSC::operator==(s1, s2);
277 }
278
279 inline bool operator==(const char *s1, const UString& s2)
280 {
281 return operator==(s2, s1);
282 }
283
284 inline bool operator!=(const char *s1, const UString& s2)
285 {
286 return !JSC::operator==(s1, s2);
287 }
288
289 bool operator==(const CString&, const CString&);
290
291 inline UString operator+(const UString& s1, const UString& s2)
292 {
293 RefPtr<UString::Rep> result = concatenate(s1.rep(), s2.rep());
294 return UString(result ? result.release() : UString::nullRep());
295 }
296
297 int compare(const UString&, const UString&);
298
299 bool equal(const UString::Rep*, const UString::Rep*);
300
301#ifdef NDEBUG
302 inline void UString::Rep::checkConsistency() const
303 {
304 }
305#endif
306
307 inline UString::UString()
308 : m_rep(&Rep::null)
309 {
310 }
311
312 // Rule from ECMA 15.2 about what an array index is.
313 // Must exactly match string form of an unsigned integer, and be less than 2^32 - 1.
314 inline unsigned UString::toArrayIndex(bool* ok) const
315 {
316 unsigned i = toStrictUInt32(ok);
317 if (ok && i >= 0xFFFFFFFFU)
318 *ok = false;
319 return i;
320 }
321
322 // We'd rather not do shared substring append for small strings, since
323 // this runs too much risk of a tiny initial string holding down a
324 // huge buffer.
325 // FIXME: this should be size_t but that would cause warnings until we
326 // fix UString sizes to be size_t instead of int
327 static const int minShareSize = Heap::minExtraCostSize / sizeof(UChar);
328
329 inline size_t UString::cost() const
330 {
331 size_t capacity = (m_rep->baseString->capacity + m_rep->baseString->preCapacity) * sizeof(UChar);
332 size_t reportedCost = m_rep->baseString->reportedCost;
333 ASSERT(capacity >= reportedCost);
334
335 size_t capacityDelta = capacity - reportedCost;
336
337 if (capacityDelta < static_cast<size_t>(minShareSize))
338 return 0;
339
340 m_rep->baseString->reportedCost = capacity;
341
342 return capacityDelta;
343 }
344
345 struct IdentifierRepHash : PtrHash<RefPtr<JSC::UString::Rep> > {
346 static unsigned hash(const RefPtr<JSC::UString::Rep>& key) { return key->computedHash(); }
347 static unsigned hash(JSC::UString::Rep* key) { return key->computedHash(); }
348 };
349
350} // namespace JSC
351
352namespace WTF {
353
354 template<typename T> struct DefaultHash;
355 template<typename T> struct StrHash;
356
357 template<> struct StrHash<JSC::UString::Rep*> {
358 static unsigned hash(const JSC::UString::Rep* key) { return key->hash(); }
359 static bool equal(const JSC::UString::Rep* a, const JSC::UString::Rep* b) { return JSC::equal(a, b); }
360 static const bool safeToCompareToEmptyOrDeleted = false;
361 };
362
363 template<> struct StrHash<RefPtr<JSC::UString::Rep> > : public StrHash<JSC::UString::Rep*> {
364 using StrHash<JSC::UString::Rep*>::hash;
365 static unsigned hash(const RefPtr<JSC::UString::Rep>& key) { return key->hash(); }
366 using StrHash<JSC::UString::Rep*>::equal;
367 static bool equal(const RefPtr<JSC::UString::Rep>& a, const RefPtr<JSC::UString::Rep>& b) { return JSC::equal(a.get(), b.get()); }
368 static bool equal(const JSC::UString::Rep* a, const RefPtr<JSC::UString::Rep>& b) { return JSC::equal(a, b.get()); }
369 static bool equal(const RefPtr<JSC::UString::Rep>& a, const JSC::UString::Rep* b) { return JSC::equal(a.get(), b); }
370
371 static const bool safeToCompareToEmptyOrDeleted = false;
372 };
373
374 template<> struct DefaultHash<JSC::UString::Rep*> {
375 typedef StrHash<JSC::UString::Rep*> Hash;
376 };
377
378 template<> struct DefaultHash<RefPtr<JSC::UString::Rep> > {
379 typedef StrHash<RefPtr<JSC::UString::Rep> > Hash;
380
381 };
382
383} // namespace WTF
384
385#endif
Note: See TracBrowser for help on using the repository browser.