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

Last change on this file since 2736 was 2736, checked in by darin, 23 years ago
  • fix worst speed problems on the sort page of the iBench JavaScript test

Sped up JavaScript iBench by 70%, the sort page by 88%.

  • kjs/array_object.h: Add array-specific sort functions.
  • kjs/array_object.cpp: (compareByStringForQSort): Added. (ArrayInstanceImp::sort): Added. (compareWithCompareFunctionForQSort): Added. (ArrayProtoFuncImp::call): Use ArrayInstanceImp::sort if the object being sorted is actually an array.
  • kjs/object.h: Add argumentsPropertyName.
  • kjs/object.cpp: Add argumentsPropertyName.
  • kjs/function.cpp: (FunctionImp::FunctionImp): Use argumentsPropertyName to avoid making a UString. (FunctionImp::call): Ditto. (ActivationImp::ActivationImp): Ditto.
  • kjs/function_object.cpp: (FunctionObjectImp::construct): Ditto.
  • kjs/ustring.h: Added compare function for -1/0/+1 comparison.
  • kjs/ustring.cpp: (KJS::compare): Added.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.2 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2000 Harri Porten ([email protected])
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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 *
21 */
22
23#ifndef _KJS_USTRING_H_
24#define _KJS_USTRING_H_
25
26#if APPLE_CHANGES
27#include <sys/types.h>
28#ifndef KWQ_UNSIGNED_TYPES_DEFINED
29#define KWQ_UNSIGNED_TYPES_DEFINED
30typedef unsigned char uchar;
31typedef unsigned long ulong;
32#endif
33#endif
34
35/**
36 * @internal
37 */
38namespace DOM {
39 class DOMString;
40};
41class KJScript;
42class QString;
43class QConstString;
44
45namespace KJS {
46
47 class UCharReference;
48 class UString;
49
50 /**
51 * @short Unicode character.
52 *
53 * UChar represents a 16 bit Unicode character. It's internal data
54 * representation is compatible to XChar2b and QChar. It's therefore
55 * possible to exchange data with X and Qt with shallow copies.
56 */
57 struct UChar {
58 /**
59 * Construct a character with uninitialized value.
60 */
61 UChar();
62 /**
63 * Construct a character with the value denoted by the arguments.
64 * @param h higher byte
65 * @param l lower byte
66 */
67 UChar(unsigned char h , unsigned char l);
68 /**
69 * Construct a character with the given value.
70 * @param u 16 bit Unicode value
71 */
72 UChar(char u);
73 UChar(unsigned char u);
74 UChar(unsigned short u);
75 UChar(const UCharReference &c);
76 /**
77 * @return The higher byte of the character.
78 */
79 unsigned char high() const { return uc >> 8; }
80 /**
81 * @return The lower byte of the character.
82 */
83 unsigned char low() const { return uc; }
84 /**
85 * @return the 16 bit Unicode value of the character
86 */
87 unsigned short unicode() const { return uc; }
88 public:
89 /**
90 * @return The character converted to lower case.
91 */
92 UChar toLower() const;
93 /**
94 * @return The character converted to upper case.
95 */
96 UChar toUpper() const;
97 /**
98 * A static instance of UChar(0).
99 */
100 static UChar null;
101 private:
102 friend class UCharReference;
103 friend class UString;
104 friend bool operator==(const UChar &c1, const UChar &c2);
105 friend bool operator==(const UString& s1, const char *s2);
106 friend bool operator<(const UString& s1, const UString& s2);
107
108 unsigned short uc;
109 };
110
111 inline UChar::UChar() { }
112 inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
113 inline UChar::UChar(char u) : uc((unsigned char)u) { }
114 inline UChar::UChar(unsigned char u) : uc(u) { }
115 inline UChar::UChar(unsigned short u) : uc(u) { }
116
117 /**
118 * @short Dynamic reference to a string character.
119 *
120 * UCharReference is the dynamic counterpart of @ref UChar. It's used when
121 * characters retrieved via index from a @ref UString are used in an
122 * assignment expression (and therefore can't be treated as being const):
123 * <pre>
124 * UString s("hello world");
125 * s[0] = 'H';
126 * </pre>
127 *
128 * If that sounds confusing your best bet is to simply forget about the
129 * existance of this class and treat is as being identical to @ref UChar.
130 */
131 class UCharReference {
132 friend class UString;
133 UCharReference(UString *s, unsigned int off) : str(s), offset(off) { }
134 public:
135 /**
136 * Set the referenced character to c.
137 */
138 UCharReference& operator=(UChar c);
139 /**
140 * Same operator as above except the argument that it takes.
141 */
142 UCharReference& operator=(char c) { return operator=(UChar(c)); }
143 /**
144 * @return Unicode value.
145 */
146 unsigned short unicode() const { return ref().unicode(); }
147 /**
148 * @return Lower byte.
149 */
150 unsigned char low() const { return ref().uc & 0xFF; }
151 /**
152 * @return Higher byte.
153 */
154 unsigned char high() const { return ref().uc >> 8; }
155 /**
156 * @return Character converted to lower case.
157 */
158 UChar toLower() const { return ref().toLower(); }
159 /**
160 * @return Character converted to upper case.
161 */
162 UChar toUpper() const { return ref().toUpper(); }
163 private:
164 // not implemented, can only be constructed from UString
165 UCharReference();
166
167 UChar& ref() const;
168 UString *str;
169 int offset;
170 };
171
172 inline UChar::UChar(const UCharReference &c) : uc(c.unicode()) { }
173
174 /**
175 * @short 8 bit char based string class
176 */
177 class CString {
178 public:
179 CString() : data(0L) { }
180 CString(const char *c);
181 CString(const CString &);
182
183 ~CString();
184
185 CString &append(const CString &);
186 CString &operator=(const char *c);
187 CString &operator=(const CString &);
188 CString &operator+=(const CString &c) { return append(c); }
189
190 int size() const;
191 const char *c_str() const { return data; }
192 private:
193 char *data;
194 };
195
196 /**
197 * @short Unicode string class
198 */
199 class UString {
200 friend bool operator==(const UString&, const UString&);
201 friend class UCharReference;
202 /**
203 * @internal
204 */
205 struct Rep {
206 friend class UString;
207 friend bool operator==(const UString&, const UString&);
208 static Rep *create(UChar *d, int l);
209 inline UChar *data() const { return dat; }
210 inline int size() const { return len; }
211
212 inline void ref() { rc++; }
213 inline int deref() { return --rc; }
214
215 UChar *dat;
216 int len;
217 int capacity;
218 int rc;
219 static Rep null;
220 static Rep empty;
221 };
222
223 public:
224 /**
225 * Constructs a null string.
226 */
227 UString();
228 /**
229 * Constructs a string from the single character c.
230 */
231 explicit UString(char c);
232 /**
233 * Constructs a string from a classical zero determined char string.
234 */
235 UString(const char *c);
236 /**
237 * Constructs a string from an array of Unicode characters of the specified
238 * length.
239 */
240 UString(const UChar *c, int length);
241 /**
242 * If copy is false the string data will be adopted.
243 * That means that the data will NOT be copied and the pointer will
244 * be deleted when the UString object is modified or destroyed.
245 * Behaviour defaults to a deep copy if copy is true.
246 */
247 UString(UChar *c, int length, bool copy);
248 /**
249 * Copy constructor. Makes a shallow copy only.
250 */
251 UString(const UString &);
252 /**
253 * Convenience declaration only ! You'll be on your own to write the
254 * implementation for a construction from QString.
255 *
256 * Note: feel free to contact me if you want to see a dummy header for
257 * your favourite FooString class here !
258 */
259 UString(const QString &);
260 /**
261 * Convenience declaration only ! See @ref UString(const QString&).
262 */
263 UString(const DOM::DOMString &);
264 /**
265 * Concatenation constructor. Makes operator+ more efficient.
266 */
267 UString(const UString &, const UString &);
268 /**
269 * Destructor. If this handle was the only one holding a reference to the
270 * string the data will be freed.
271 */
272 ~UString() { release(); }
273
274 /**
275 * Constructs a string from an int.
276 */
277 static UString from(int i);
278 /**
279 * Constructs a string from an unsigned int.
280 */
281 static UString from(unsigned int u);
282 /**
283 * Constructs a string from a long.
284 */
285 static UString from(long l);
286 /**
287 * Constructs a string from a double.
288 */
289 static UString from(double d);
290
291 /**
292 * Append another string.
293 */
294 UString &append(const UString &);
295
296 /**
297 * @return The string converted to the 8-bit string type @ref CString().
298 */
299 CString cstring() const;
300 /**
301 * Convert the Unicode string to plain ASCII chars chopping of any higher
302 * bytes. This method should only be used for *debugging* purposes as it
303 * is neither Unicode safe nor free from side effects. In order not to
304 * waste any memory the char buffer is static and *shared* by all UString
305 * instances.
306 */
307 char *ascii() const;
308 /**
309 * @see UString(const QString&).
310 */
311 DOM::DOMString string() const;
312 /**
313 * @see UString(const QString&).
314 */
315 QString qstring() const;
316 /**
317 * @see UString(const QString&).
318 */
319 QConstString qconststring() const;
320
321 /**
322 * Assignment operator.
323 */
324 UString &operator=(const char *c);
325 UString &operator=(const UString &);
326 /**
327 * Appends the specified string.
328 */
329 UString &operator+=(const UString &s) { return append(s); }
330
331 /**
332 * @return A pointer to the internal Unicode data.
333 */
334 const UChar* data() const { return rep->data(); }
335 /**
336 * @return True if null.
337 */
338 bool isNull() const { return (rep == &Rep::null); }
339 /**
340 * @return True if null or zero length.
341 */
342 bool isEmpty() const { return (!rep->len); }
343 /**
344 * Use this if you want to make sure that this string is a plain ASCII
345 * string. For example, if you don't want to lose any information when
346 * using @ref cstring() or @ref ascii().
347 *
348 * @return True if the string doesn't contain any non-ASCII characters.
349 */
350 bool is8Bit() const;
351 /**
352 * @return The length of the string.
353 */
354 int size() const { return rep->size(); }
355 /**
356 * Const character at specified position.
357 */
358 UChar operator[](int pos) const;
359 /**
360 * Writable reference to character at specified position.
361 */
362 UCharReference operator[](int pos);
363
364 /**
365 * Attempts an conversion to a number. Apart from floating point numbers,
366 * the algorithm will recognize hexadecimal representations (as
367 * indicated by a 0x or 0X prefix) and +/- Infinity.
368 * Returns NaN if the conversion failed.
369 * @param tolerant if true, toDouble can tolerate garbage after the number.
370 */
371 double toDouble(bool tolerant=false) const;
372 /**
373 * Attempts an conversion to an unsigned long integer. ok will be set
374 * according to the success.
375 */
376 unsigned long toULong(bool *ok = 0L) const;
377 /**
378 * @return Position of first occurence of f starting at position pos.
379 * -1 if the search was not successful.
380 */
381 int find(const UString &f, int pos = 0) const;
382 int find(UChar, int pos = 0) const;
383 /**
384 * @return Position of first occurence of f searching backwards from
385 * position pos.
386 * -1 if the search was not successful.
387 */
388 int rfind(const UString &f, int pos) const;
389 int rfind(UChar, int pos) const;
390 /**
391 * @return The sub string starting at position pos and length len.
392 */
393 UString substr(int pos = 0, int len = -1) const;
394 /**
395 * Static instance of a null string.
396 */
397 static UString null;
398#ifdef KJS_DEBUG_MEM
399 /**
400 * Clear statically allocated resources.
401 */
402 static void globalClear();
403#endif
404 private:
405 void attach(Rep *r);
406 void detach();
407 void release();
408 Rep *rep;
409 };
410
411 inline bool operator==(const UChar &c1, const UChar &c2) {
412 return (c1.uc == c2.uc);
413 }
414 bool operator==(const UString& s1, const UString& s2);
415 inline bool operator!=(const UString& s1, const UString& s2) {
416 return !KJS::operator==(s1, s2);
417 }
418 bool operator<(const UString& s1, const UString& s2);
419 bool operator==(const UString& s1, const char *s2);
420 inline bool operator!=(const UString& s1, const char *s2) {
421 return !KJS::operator==(s1, s2);
422 }
423 inline bool operator==(const char *s1, const UString& s2) {
424 return operator==(s2, s1);
425 }
426 inline bool operator!=(const char *s1, const UString& s2) {
427 return !KJS::operator==(s1, s2);
428 }
429 bool operator==(const CString& s1, const CString& s2);
430 inline UString operator+(const UString& s1, const UString& s2) {
431 return UString(s1, s2);
432 }
433
434 int compare(const UString &, const UString &);
435
436}; // namespace
437
438#endif
Note: See TracBrowser for help on using the repository browser.