KJS

ustring.h
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 1999-2000 Harri Porten ([email protected])
4  * Copyright (C) 2004 Apple Computer, Inc.
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 "kjs/global.h"
27 
28 #include <wtf/AlwaysInline.h>
29 #include <wtf/FastMalloc.h>
30 #include <wtf/RefPtr.h>
31 #include <wtf/PassRefPtr.h>
32 #include <wtf/Vector.h>
33 
34 #include <assert.h>
35 #include "collector.h"
36 #if HAVE_STDINT_H
37 #include <stdint.h>
38 #endif
39 
40 /* On some ARM platforms GCC won't pack structures by default so sizeof(UChar)
41  will end up being != 2 which causes crashes since the code depends on that. */
42 #if defined(WTF_COMPILER_GCC) && PLATFORM(FORCE_PACK)
43 #define PACK_STRUCT __attribute__((packed))
44 #else
45 #define PACK_STRUCT
46 #endif
47 
48 /**
49  * @internal
50  */
51 namespace DOM
52 {
53 class DOMString;
54 }
55 class QString;
56 class QConstString;
57 
58 namespace KJS
59 {
60 
61 class UString;
62 
63 /**
64  * @short Unicode character.
65  *
66  * UChar represents a 16 bit Unicode character. Its internal data
67  * representation is compatible to XChar2b and QChar. It's therefore
68  * possible to exchange data with X and Qt with shallow copies.
69  */
70 struct KJS_EXPORT UChar {
71  /**
72  * Construct a character with uninitialized value.
73  */
74  UChar();
75  /**
76  * Construct a character with the value denoted by the arguments.
77  * @param h higher byte
78  * @param l lower byte
79  */
80  UChar(unsigned char h, unsigned char l);
81  /**
82  * Construct a character with the given value.
83  * @param u 16 bit Unicode value
84  */
85  UChar(char u);
86  UChar(unsigned char u);
87  UChar(unsigned short u);
88  /**
89  * @return The higher byte of the character.
90  */
91  unsigned char high() const
92  {
93  return static_cast<unsigned char>(uc >> 8);
94  }
95 
96  /**
97  * @return The lower byte of the character.
98  */
99  unsigned char low() const
100  {
101  return static_cast<unsigned char>(uc);
102  }
103 
104  /**
105  * @return the 16 bit Unicode value of the character
106  */
107  unsigned short unicode() const
108  {
109  return uc;
110  }
111 
112  unsigned short uc;
113 } PACK_STRUCT;
114 
115 inline UChar::UChar() { }
116 inline UChar::UChar(unsigned char h, unsigned char l) : uc(h << 8 | l) { }
117 inline UChar::UChar(char u) : uc((unsigned char)u) { }
118 inline UChar::UChar(unsigned char u) : uc(u) { }
119 inline UChar::UChar(unsigned short u) : uc(u) { }
120 
121 /**
122  * @short 8 bit char based string class
123  */
124 class KJS_EXPORT CString
125 {
126 public:
127  CString() : data(nullptr), length(0) { }
128  CString(const char *c);
129  CString(const char *c, size_t len);
130  CString(const CString &);
131 
132  ~CString();
133 
134  CString &operator=(const char *c);
135  CString &operator=(const CString &);
136 
137  size_t size() const
138  {
139  return length;
140  }
141  const char *c_str() const
142  {
143  return data;
144  }
145 private:
146  char *data;
147  size_t length;
148 };
149 
150 /**
151  * @short Unicode string class
152  */
153 class KJS_EXPORT UString
154 {
155  KJS_EXPORT friend bool operator==(const UString &, const UString &);
156 
157 public:
158  /**
159  * @internal
160  */
161  struct KJS_EXPORT Rep {
162 
163  static PassRefPtr<Rep> create(UChar *d, int l);
164  static PassRefPtr<Rep> createCopying(const UChar *d, int l);
165  static PassRefPtr<Rep> create(PassRefPtr<Rep> base, int offset, int length);
166 
167  void destroy();
168 
169  bool baseIsSelf() const
170  {
171  return baseString == this;
172  }
173  UChar *data() const
174  {
175  return baseString->buf + baseString->preCapacity + offset;
176  }
177  int size() const
178  {
179  return len;
180  }
181 
182  unsigned hash() const
183  {
184  if (_hash == 0) {
185  _hash = computeHash(data(), len);
186  } return _hash;
187  }
188  unsigned computedHash() const
189  {
190  assert(_hash); // fast path for Identifiers
191  return _hash;
192  }
193  static unsigned computeHash(const UChar *, int length);
194  static unsigned computeHash(const char *s, int length);
195  static unsigned computeHash(const char *);
196 
197  Rep *ref()
198  {
199  ++rc;
200  return this;
201  }
202  ALWAYS_INLINE void deref()
203  {
204  if (--rc == 0) {
205  destroy();
206  }
207  }
208 
209  // unshared data
210  int offset;
211  int len;
212  int rc;
213  mutable unsigned _hash;
214  bool isIdentifier;
215  UString::Rep *baseString;
216  size_t reportedCost;
217 
218  // potentially shared data
219  UChar *buf;
220  int usedCapacity;
221  int capacity;
222  int usedPreCapacity;
223  int preCapacity;
224 
225  static Rep null;
226  static Rep empty;
227  };
228 
229 public:
230  /**
231  * Constructs a null string.
232  */
233  UString();
234  /**
235  * Constructs an empty string.
236  */
237  enum Empty { empty };
238  UString(Empty);
239  /**
240  * Constructs a string from the single character c.
241  */
242  explicit UString(char c);
243  /**
244  * Constructs a string from a classical zero determined char string.
245  */
246  UString(const char *c);
247  UString(const char *c, size_t length);
248  /**
249  * Constructs a string from an array of Unicode characters of the specified
250  * length.
251  */
252  UString(const UChar *c, int length);
253  /**
254  * If copy is false the string data will be adopted.
255  * That means that the data will NOT be copied and the pointer will
256  * be deleted when the UString object is modified or destroyed.
257  * Behaviour defaults to a deep copy if copy is true.
258  */
259  UString(UChar *c, int length, bool copy);
260  /**
261  * Copy constructor. Makes a shallow copy only.
262  */
263  UString(const UString &s) : m_rep(s.m_rep) {}
264  UString &operator=(const UString &s) { m_rep = s.m_rep; return *this; }
265 
266  UString(const Vector<UChar> &buffer);
267 
268  /**
269  * Convenience declaration only ! You'll be on your own to write the
270  * implementation for a construction from QString.
271  *
272  * Note: feel free to contact me if you want to see a dummy header for
273  * your favorite FooString class here !
274  */
275  KJS_EXTERNAL_EXPORT UString(const QString &);
276  /**
277  * Convenience declaration only ! See UString(const QString&).
278  */
279  KJS_EXTERNAL_EXPORT UString(const DOM::DOMString &);
280 
281  /**
282  * Concatenation constructor. Makes operator+ more efficient.
283  */
284  UString(const UString &, const UString &);
285  /**
286  * Destructor.
287  */
288  ~UString() {}
289 
290  /**
291  * Constructs a string from an int.
292  */
293  static UString from(int i);
294  /**
295  * Constructs a string from an unsigned int.
296  */
297  static UString from(unsigned int u);
298  /**
299  * Constructs a string from a long int.
300  */
301  static UString from(long u);
302  /**
303  * Constructs a string from a double.
304  */
305  static UString from(double d);
306 
307  static bool equal(const UString::Rep *a, const UString::Rep *b);
308 
309  struct Range {
310  public:
311  Range(int pos, int len) : position(pos), length(len) {}
312  Range() {}
313  int position;
314  int length;
315  };
316 
317  UString spliceSubstringsWithSeparators(const Range *substringRanges, int rangeCount, const UString *separators, int separatorCount) const;
318 
319  /**
320  * Append another string.
321  */
322  UString &append(const UString &subStr, int subPos, int subLength = -1);
323  UString &append(const UString &t);
324  UString &append(const char *t);
325  UString &append(const char *t, int tSize);
326  UString &append(unsigned short);
327  UString &append(char c)
328  {
329  return append(static_cast<unsigned short>(static_cast<unsigned char>(c)));
330  }
331  UString &append(UChar c)
332  {
333  return append(c.uc);
334  }
335 
336  /**
337  * @return The string converted to the 8-bit string type CString().
338  */
339  CString cstring() const;
340  /**
341  * Convert the Unicode string to plain ASCII chars chopping of any higher
342  * bytes. This method should only be used for *debugging* purposes as it
343  * is neither Unicode safe nor free from side effects. In order not to
344  * waste any memory the char buffer is static and *shared* by all UString
345  * instances.
346  */
347  char *ascii() const;
348 
349  /**
350  * Convert the string to UTF-8, assuming it is UTF-16 encoded.
351  * Since this function is tolerant of badly formed UTF-16, it can create UTF-8
352  * strings that are invalid because they have characters in the range
353  * U+D800-U+DDFF, U+FFFE, or U+FFFF, but the UTF-8 string is guaranteed to
354  * be otherwise valid.
355  */
356  CString UTF8String() const;
357 
358  /**
359  * @see UString(const QString&).
360  */
361  KJS_EXTERNAL_EXPORT DOM::DOMString domString() const;
362  /**
363  * @see UString(const QString&).
364  */
365  KJS_EXTERNAL_EXPORT QString qstring() const;
366  /**
367  * @see UString(const QString&).
368  */
369  KJS_EXTERNAL_EXPORT QConstString qconststring() const;
370 
371  /**
372  * Assignment operator.
373  */
374  UString &operator=(const char *c);
375  UString &operator=(Empty);
376  /**
377  * Appends the specified string.
378  */
379  UString &operator+=(const UString &s)
380  {
381  return append(s);
382  }
383  UString &operator+=(const char *s)
384  {
385  return append(s);
386  }
387 
388  /**
389  * @return A pointer to the internal Unicode data.
390  */
391  const UChar *data() const
392  {
393  return m_rep->data();
394  }
395  /**
396  * @return True if null.
397  */
398  bool isNull() const
399  {
400  return (m_rep == &Rep::null);
401  }
402  /**
403  * @return True if null or zero length.
404  */
405  bool isEmpty() const
406  {
407  return (!m_rep->len);
408  }
409  /**
410  * Use this if you want to make sure that this string is a plain ASCII
411  * string. For example, if you don't want to lose any information when
412  * using cstring() or ascii().
413  *
414  * @return True if the string doesn't contain any non-ASCII characters.
415  */
416  bool is8Bit() const;
417  /**
418  * @return The length of the string.
419  */
420  int size() const
421  {
422  return m_rep->size();
423  }
424  /**
425  * Const character at specified position.
426  */
427  const UChar operator[](int pos) const;
428  /**
429  * Attempts an conversion to a number. Apart from floating point numbers,
430  * the algorithm will recognize hexadecimal representations (as
431  * indicated by a 0x or 0X prefix) and +/- Infinity.
432  * Returns NaN if the conversion failed.
433  * @param tolerateTrailingJunk if true, toDouble can tolerate garbage after the number.
434  * @param tolerateEmptyString if false, toDouble will turn an empty string into NaN rather than 0.
435  */
436  double toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const;
437  double toDouble(bool tolerateTrailingJunk) const;
438  double toDouble() const;
439 
440  /**
441  * Attempts an conversion to a 32-bit integer. ok will be set
442  * according to the success.
443  */
444  uint32_t toStrictUInt32(bool *ok = nullptr) const;
445 
446  /**
447  * Attempts an conversion to an array index. The "ok" boolean will be set
448  * to true if it is a valid array index according to the rule from
449  * ECMA 15.2 about what an array index is. It must exactly match the string
450  * form of an unsigned integer, and be less than 2^32 - 1.
451  */
452  unsigned toArrayIndex(bool *ok = nullptr) const;
453 
454  /**
455  * @return Position of first occurrence of f starting at position pos.
456  * -1 if the search was not successful.
457  */
458  int find(const UString &f, int pos = 0) const;
459  int find(UChar, int pos = 0) const;
460  /**
461  * @return Position of first occurrence of f searching backwards from
462  * position pos.
463  * -1 if the search was not successful.
464  */
465  int rfind(const UString &f, int pos) const;
466  int rfind(UChar, int pos) const;
467  /**
468  * @return The sub string starting at position pos and length len.
469  */
470  UString substr(int pos = 0, int len = -1) const;
471  /**
472  * Static instance of a null string.
473  */
474  static const UString &null();
475 
476  /**
477  * Maximum permitted string length
478  * @since 5.20
479  */
480  static size_t maxUChars();
481 
482  Rep *rep() const
483  {
484  return m_rep.get();
485  }
486  UString(PassRefPtr<Rep> r) : m_rep(r)
487  {
488  assert(m_rep);
489  }
490  void copyForWriting();
491 
492  size_t cost() const;
493 private:
494  size_t expandedSize(size_t size, size_t otherSize) const;
495  int usedCapacity() const;
496  int usedPreCapacity() const;
497  void expandCapacity(int requiredLength);
498  void expandPreCapacity(int requiredPreCap);
499  void set(const char *c, int len);
500 
501  RefPtr<Rep> m_rep;
502 };
503 
504 KJS_EXPORT inline bool operator==(const UChar &c1, const UChar &c2)
505 {
506  return (c1.uc == c2.uc);
507 }
508 KJS_EXPORT bool operator==(const UString &s1, const UString &s2);
509 KJS_EXPORT inline bool operator!=(const UString &s1, const UString &s2)
510 {
511  return !KJS::operator==(s1, s2);
512 }
513 KJS_EXPORT bool operator<(const UString &s1, const UString &s2);
514 KJS_EXPORT bool operator==(const UString &s1, const char *s2);
515 KJS_EXPORT inline bool operator!=(const UString &s1, const char *s2)
516 {
517  return !KJS::operator==(s1, s2);
518 }
519 KJS_EXPORT inline bool operator==(const char *s1, const UString &s2)
520 {
521  return operator==(s2, s1);
522 }
523 KJS_EXPORT inline bool operator!=(const char *s1, const UString &s2)
524 {
525  return !KJS::operator==(s1, s2);
526 }
527 KJS_EXPORT bool operator==(const CString &s1, const CString &s2);
528 KJS_EXPORT inline UString operator+(const UString &s1, const UString &s2)
529 {
530  return UString(s1, s2);
531 }
532 
533 KJS_EXPORT int compare(const UString &, const UString &);
534 
535 // Given a first byte, gives the length of the UTF-8 sequence it begins.
536 // Returns 0 for bytes that are not legal starts of UTF-8 sequences.
537 // Only allows sequences of up to 4 bytes, since that works for all Unicode characters (U-00000000 to U-0010FFFF).
538 int UTF8SequenceLength(char);
539 
540 // Takes a null-terminated C-style string with a UTF-8 sequence in it and converts it to a character.
541 // Only allows Unicode characters (U-00000000 to U-0010FFFF).
542 // Returns -1 if the sequence is not valid (including presence of extra bytes).
543 int decodeUTF8Sequence(const char *);
544 
545 KJS_EXPORT inline UString::UString()
546  : m_rep(&Rep::null)
547 {
548 }
549 
550 // Rule from ECMA 15.2 about what an array index is.
551 // Must exactly match string form of an unsigned integer, and be less than 2^32 - 1.
552 inline unsigned UString::toArrayIndex(bool *ok) const
553 {
554  unsigned i = toStrictUInt32(ok);
555  if (ok && i >= 0xFFFFFFFFU) {
556  *ok = false;
557  }
558  return i;
559 }
560 
561 // We'd rather not do shared substring append for small strings, since
562 // this runs too much risk of a tiny initial string holding down a
563 // huge buffer.
564 // FIXME: this should be size_t but that would cause warnings until we
565 // fix UString sizes to be size_t instead of int
566 static const int minShareSize = Collector::minExtraCostSize / sizeof(UChar);
567 
568 inline size_t UString::cost() const
569 {
570  size_t capacity = (m_rep->baseString->capacity + m_rep->baseString->preCapacity) * sizeof(UChar);
571  size_t reportedCost = m_rep->baseString->reportedCost;
572  ASSERT(capacity >= reportedCost);
573 
574  size_t capacityDelta = capacity - reportedCost;
575 
576  if (capacityDelta < static_cast<size_t>(minShareSize)) {
577  return 0;
578  }
579 
580  m_rep->baseString->reportedCost = capacity;
581  return capacityDelta;
582 }
583 
584 } // namespace
585 
586 namespace WTF
587 {
588 
589 template<typename T> struct DefaultHash;
590 template<typename T> struct StrHash;
591 
592 template<> struct StrHash<KJS::UString::Rep *> {
593  static unsigned hash(const KJS::UString::Rep *key)
594  {
595  return key->hash();
596  }
597  static bool equal(const KJS::UString::Rep *a, const KJS::UString::Rep *b)
598  {
599  return KJS::UString::equal(a, b);
600  }
601  static const bool safeToCompareToEmptyOrDeleted = false;
602 };
603 
604 template<> struct DefaultHash<KJS::UString::Rep *> {
605  typedef StrHash<KJS::UString::Rep *> Hash;
606 };
607 } // namespace WTF
608 
609 #endif
Empty
Constructs an empty string.
Definition: ustring.h:237
uint32_t toStrictUInt32(bool *ok=nullptr) const
Attempts an conversion to a 32-bit integer.
Definition: ustring.cpp:1131
unsigned short unicode() const
Definition: ustring.h:107
UString & operator+=(const UString &s)
Appends the specified string.
Definition: ustring.h:379
int size() const
Definition: ustring.h:420
8 bit char based string class
Definition: ustring.h:124
UString(const UString &s)
Copy constructor.
Definition: ustring.h:263
unsigned char high() const
Definition: ustring.h:91
unsigned char low() const
Definition: ustring.h:99
Unicode string class.
Definition: ustring.h:153
~UString()
Destructor.
Definition: ustring.h:288
UChar()
Construct a character with uninitialized value.
Definition: ustring.h:115
Unicode character.
Definition: ustring.h:70
bool isNull() const
Definition: ustring.h:398
unsigned toArrayIndex(bool *ok=nullptr) const
Attempts an conversion to an array index.
Definition: ustring.h:552
QCA_EXPORT const SecureArray operator+(const SecureArray &a, const SecureArray &b)
UString()
Constructs a null string.
Definition: ustring.h:545
bool isEmpty() const
Definition: ustring.h:405
const UChar * data() const
Definition: ustring.h:391
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Mar 8 2021 22:59:37 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.