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

Last change on this file since 28479 was 27859, checked in by [email protected], 18 years ago

2007-11-16 Mark Rowe <[email protected]>

Reviewed by Eric.

Replace strings, identifier, buffer8 and buffer16 members of Lexer with vectors.
SunSpider claims this is a 0.7% speedup.

  • kjs/lexer.cpp: (KJS::Lexer::Lexer): (KJS::Lexer::lex): (KJS::Lexer::record8): (KJS::Lexer::record16): (KJS::Lexer::scanRegExp): (KJS::Lexer::clear): (KJS::Lexer::makeIdentifier): (KJS::Lexer::makeUString):
  • kjs/lexer.h:
  • kjs/ustring.cpp: (KJS::UString::UString): Add a convenience constructor that takes a const Vector<UChar>&.
  • kjs/ustring.h:
  • Property svn:eol-style set to native
File size: 14.1 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * Copyright (C) 1999-2000 Harri Porten ([email protected])
4 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#ifndef _KJS_USTRING_H_
24#define _KJS_USTRING_H_
25
26#include "JSLock.h"
27#include "collector.h"
28#include <stdint.h>
29#include <wtf/Assertions.h>
30#include <wtf/FastMalloc.h>
31#include <wtf/PassRefPtr.h>
32#include <wtf/RefPtr.h>
33#include <wtf/Vector.h>
34
35/* On some ARM platforms GCC won't pack structures by default so sizeof(UChar)
36 will end up being != 2 which causes crashes since the code depends on that. */
37#if COMPILER(GCC) && PLATFORM(FORCE_PACK)
38#define PACK_STRUCT __attribute__((packed))
39#else
40#define PACK_STRUCT
41#endif
42
43/**
44 * @internal
45 */
46namespace DOM {
47 class DOMString;
48 class AtomicString;
49}
50class KJScript;
51
52namespace KJS {
53
54 using WTF::PlacementNewAdoptType;
55 using WTF::PlacementNewAdopt;
56
57 class UString;
58
59 /**
60 * @short Unicode character.
61 *
62 * UChar represents a 16 bit Unicode character. It's internal data
63 * representation is compatible to XChar2b and QChar. It's therefore
64 * possible to exchange data with X and Qt with shallow copies.
65 */
66 struct UChar {
67 /**
68 * Construct a character with uninitialized value.
69 */
70 UChar();
71 /**
72 * Construct a character with the value denoted by the arguments.
73 * @param h higher byte
74 * @param l lower byte
75 */
76 UChar(unsigned char h , unsigned char l);
77 /**
78 * Construct a character with the given value.
79 * @param u 16 bit Unicode value
80 */
81 UChar(char u);
82 UChar(unsigned char u);
83 UChar(unsigned short u);
84 /**
85 * @return The higher byte of the character.
86 */
87 unsigned char high() const { return static_cast<unsigned char>(uc >> 8); }
88 /**
89 * @return The lower byte of the character.
90 */
91 unsigned char low() const { return static_cast<unsigned char>(uc); }
92 /**
93 * @return the 16 bit Unicode value of the character
94 */
95 unsigned short unicode() const { return uc; }
96
97 unsigned short uc;
98 } PACK_STRUCT;
99
100 inline UChar::UChar() { }
101 inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
102 inline UChar::UChar(char u) : uc((unsigned char)u) { }
103 inline UChar::UChar(unsigned char u) : uc(u) { }
104 inline UChar::UChar(unsigned short u) : uc(u) { }
105
106 /**
107 * @short 8 bit char based string class
108 */
109 class CString {
110 public:
111 CString() : data(0), length(0) { }
112 CString(const char *c);
113 CString(const char *c, size_t len);
114 CString(const CString &);
115
116 ~CString();
117
118 CString &append(const CString &);
119 CString &operator=(const char *c);
120 CString &operator=(const CString &);
121 CString &operator+=(const CString &c) { return append(c); }
122
123 size_t size() const { return length; }
124 const char *c_str() const { return data; }
125 private:
126 char *data;
127 size_t length;
128 };
129
130 /**
131 * @short Unicode string class
132 */
133 class UString {
134 friend bool operator==(const UString&, const UString&);
135
136 public:
137 /**
138 * @internal
139 */
140 struct Rep {
141
142 static PassRefPtr<Rep> create(UChar *d, int l);
143 static PassRefPtr<Rep> createCopying(const UChar *d, int l);
144 static PassRefPtr<Rep> create(PassRefPtr<Rep> base, int offset, int length);
145
146 void destroy();
147
148 bool baseIsSelf() const { return baseString == this; }
149 UChar* data() const { return baseString->buf + baseString->preCapacity + offset; }
150 int size() const { return len; }
151
152 unsigned hash() const { if (_hash == 0) _hash = computeHash(data(), len); return _hash; }
153 unsigned computedHash() const { ASSERT(_hash); return _hash; } // fast path for Identifiers
154
155 static unsigned computeHash(const UChar *, int length);
156 static unsigned computeHash(const char *);
157
158 Rep* ref() { ASSERT(JSLock::lockCount() > 0); ++rc; return this; }
159 ALWAYS_INLINE void deref() { ASSERT(JSLock::lockCount() > 0); if (--rc == 0) destroy(); }
160
161 // unshared data
162 int offset;
163 int len;
164 int rc;
165 mutable unsigned _hash;
166 bool isIdentifier;
167 UString::Rep* baseString;
168
169 // potentially shared data
170 UChar *buf;
171 int usedCapacity;
172 int capacity;
173 int usedPreCapacity;
174 int preCapacity;
175
176 static Rep null;
177 static Rep empty;
178 };
179
180 public:
181
182 /**
183 * Constructs a null string.
184 */
185 UString();
186 /**
187 * Constructs a string from a classical zero-terminated char string.
188 */
189 UString(const char *c);
190 /**
191 * Constructs a string from an array of Unicode characters of the specified
192 * length.
193 */
194 UString(const UChar *c, int length);
195 /**
196 * If copy is false the string data will be adopted.
197 * That means that the data will NOT be copied and the pointer will
198 * be deleted when the UString object is modified or destroyed.
199 * Behaviour defaults to a deep copy if copy is true.
200 */
201 UString(UChar *c, int length, bool copy);
202 /**
203 * Copy constructor. Makes a shallow copy only.
204 */
205 UString(const UString &s) : m_rep(s.m_rep) {}
206
207 UString(const Vector<UChar>& buffer);
208
209 /**
210 * Convenience declaration only ! You'll be on your own to write the
211 * implementation for a construction from DOM::DOMString.
212 *
213 * Note: feel free to contact me if you want to see a dummy header for
214 * your favorite FooString class here !
215 */
216 UString(const DOM::DOMString&);
217 /**
218 * Convenience declaration only ! See UString(const DOM::DOMString&).
219 */
220 UString(const DOM::AtomicString&);
221
222 /**
223 * Concatenation constructor. Makes operator+ more efficient.
224 */
225 UString(const UString &, const UString &);
226 /**
227 * Destructor.
228 */
229 ~UString() {}
230
231 // Special constructor for cases where we overwrite an object in place.
232 UString(PlacementNewAdoptType) : m_rep(PlacementNewAdopt) { }
233
234 /**
235 * Constructs a string from an int.
236 */
237 static UString from(int i);
238 /**
239 * Constructs a string from an unsigned int.
240 */
241 static UString from(unsigned int u);
242 /**
243 * Constructs a string from a long int.
244 */
245 static UString from(long u);
246 /**
247 * Constructs a string from a double.
248 */
249 static UString from(double d);
250
251 struct Range {
252 public:
253 Range(int pos, int len) : position(pos), length(len) {}
254 Range() {}
255 int position;
256 int length;
257 };
258
259 UString spliceSubstringsWithSeparators(const Range *substringRanges, int rangeCount, const UString *separators, int separatorCount) const;
260
261 /**
262 * Append another string.
263 */
264 UString &append(const UString &);
265 UString &append(const char *);
266 UString &append(unsigned short);
267 UString &append(char c) { return append(static_cast<unsigned short>(static_cast<unsigned char>(c))); }
268 UString &append(UChar c) { return append(c.uc); }
269
270 /**
271 * @return The string converted to the 8-bit string type CString().
272 * This method is not Unicode safe and shouldn't be used unless the string
273 * is known to be ASCII.
274 */
275 CString cstring() const;
276 /**
277 * Convert the Unicode string to plain ASCII chars chopping of any higher
278 * bytes. This method should only be used for *debugging* purposes as it
279 * is neither Unicode safe nor free from side effects. In order not to
280 * waste any memory the char buffer is static and *shared* by all UString
281 * instances.
282 */
283 char *ascii() const;
284
285 /**
286 * Convert the string to UTF-8, assuming it is UTF-16 encoded.
287 * In non-strict mode, this function is tolerant of badly formed UTF-16, it
288 * can create UTF-8 strings that are invalid because they have characters in
289 * the range U+D800-U+DDFF, U+FFFE, or U+FFFF, but the UTF-8 string is
290 * guaranteed to be otherwise valid.
291 * In strict mode, error is returned as null CString.
292 */
293 CString UTF8String(bool strict = false) const;
294
295 /**
296 * @see UString(const DOM::DOMString&).
297 */
298 DOM::DOMString domString() const;
299
300 /**
301 * Assignment operator.
302 */
303 UString &operator=(const char *c);
304 /**
305 * Appends the specified string.
306 */
307 UString &operator+=(const UString &s) { return append(s); }
308 UString &operator+=(const char *s) { return append(s); }
309
310 /**
311 * @return A pointer to the internal Unicode data.
312 */
313 const UChar* data() const { return m_rep->data(); }
314 /**
315 * @return True if null.
316 */
317 bool isNull() const { return (m_rep == &Rep::null); }
318 /**
319 * @return True if null or zero length.
320 */
321 bool isEmpty() const { return (!m_rep->len); }
322 /**
323 * Use this if you want to make sure that this string is a plain ASCII
324 * string. For example, if you don't want to lose any information when
325 * using cstring() or ascii().
326 *
327 * @return True if the string doesn't contain any non-ASCII characters.
328 */
329 bool is8Bit() const;
330 /**
331 * @return The length of the string.
332 */
333 int size() const { return m_rep->size(); }
334 /**
335 * Const character at specified position.
336 */
337 const UChar operator[](int pos) const;
338
339 /**
340 * Attempts an conversion to a number. Apart from floating point numbers,
341 * the algorithm will recognize hexadecimal representations (as
342 * indicated by a 0x or 0X prefix) and +/- Infinity.
343 * Returns NaN if the conversion failed.
344 * @param tolerateTrailingJunk if true, toDouble can tolerate garbage after the number.
345 * @param tolerateEmptyString if false, toDouble will turn an empty string into NaN rather than 0.
346 */
347 double toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const;
348 double toDouble(bool tolerateTrailingJunk) const;
349 double toDouble() const;
350
351 /**
352 * Attempts an conversion to a 32-bit integer. ok will be set
353 * according to the success.
354 * @param tolerateEmptyString if false, toUInt32 will return false for *ok for an empty string.
355 */
356 uint32_t toUInt32(bool *ok = 0) const;
357 uint32_t toUInt32(bool *ok, bool tolerateEmptyString) const;
358 uint32_t toStrictUInt32(bool *ok = 0) const;
359
360 /**
361 * Attempts an conversion to an array index. The "ok" boolean will be set
362 * to true if it is a valid array index according to the rule from
363 * ECMA 15.2 about what an array index is. It must exactly match the string
364 * form of an unsigned integer, and be less than 2^32 - 1.
365 */
366 unsigned toArrayIndex(bool *ok = 0) const;
367
368 /**
369 * @return Position of first occurrence of f starting at position pos.
370 * -1 if the search was not successful.
371 */
372 int find(const UString &f, int pos = 0) const;
373 int find(UChar, int pos = 0) const;
374 /**
375 * @return Position of first occurrence of f searching backwards from
376 * position pos.
377 * -1 if the search was not successful.
378 */
379 int rfind(const UString &f, int pos) const;
380 int rfind(UChar, int pos) const;
381 /**
382 * @return The sub string starting at position pos and length len.
383 */
384 UString substr(int pos = 0, int len = -1) const;
385 /**
386 * Static instance of a null string.
387 */
388 static const UString &null();
389
390 Rep* rep() const { return m_rep.get(); }
391 UString(PassRefPtr<Rep> r) : m_rep(r) { ASSERT(m_rep); }
392
393 size_t cost() const;
394
395 private:
396 size_t expandedSize(size_t size, size_t otherSize) const;
397 int usedCapacity() const;
398 int usedPreCapacity() const;
399 void expandCapacity(int requiredLength);
400 void expandPreCapacity(int requiredPreCap);
401
402 RefPtr<Rep> m_rep;
403 };
404
405 inline bool operator==(const UChar &c1, const UChar &c2) {
406 return (c1.uc == c2.uc);
407 }
408 bool operator==(const UString& s1, const UString& s2);
409 inline bool operator!=(const UString& s1, const UString& s2) {
410 return !KJS::operator==(s1, s2);
411 }
412 bool operator<(const UString& s1, const UString& s2);
413 bool operator==(const UString& s1, const char *s2);
414 inline bool operator!=(const UString& s1, const char *s2) {
415 return !KJS::operator==(s1, s2);
416 }
417 inline bool operator==(const char *s1, const UString& s2) {
418 return operator==(s2, s1);
419 }
420 inline bool operator!=(const char *s1, const UString& s2) {
421 return !KJS::operator==(s1, s2);
422 }
423 bool operator==(const CString& s1, const CString& s2);
424 inline UString operator+(const UString& s1, const UString& s2) {
425 return UString(s1, s2);
426 }
427
428 int compare(const UString &, const UString &);
429
430inline UString::UString()
431 : m_rep(&Rep::null)
432{
433}
434
435// Rule from ECMA 15.2 about what an array index is.
436// Must exactly match string form of an unsigned integer, and be less than 2^32 - 1.
437inline unsigned UString::toArrayIndex(bool *ok) const
438{
439 unsigned i = toStrictUInt32(ok);
440 if (ok && i >= 0xFFFFFFFFU)
441 *ok = false;
442 return i;
443}
444
445inline size_t UString::cost() const
446{
447 // If this string is sharing with a base, then don't count any cost. We will never share
448 // with a base that wasn't already big enough to register extra cost, so a string holding that
449 // buffer has already paid extra cost at some point; and if we just
450 // enlarged it by a huge amount, it must have been by appending a string
451 // that itself paid extra cost, or a huge number of small strings. Either way, GC will come
452 // relatively soon.
453
454 // If we didn't do this, the shared substring optimization would result
455 // in constantly garbage collecting when sharing with one big string.
456
457 if (!m_rep->baseIsSelf())
458 return 0;
459
460 return (m_rep->capacity + m_rep->preCapacity) * sizeof(UChar);
461}
462
463} // namespace
464
465#endif
Note: See TracBrowser for help on using the repository browser.