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 
51 namespace DOM
52 {
53 class DOMString;
54 }
55 class QString;
56 class QConstString;
57 
58 namespace KJS
59 {
60 
61 class UString;
62 
70 struct KJS_EXPORT UChar {
74  UChar();
80  UChar(unsigned char h, unsigned char l);
85  UChar(char u);
86  UChar(unsigned char u);
87  UChar(unsigned short u);
91  unsigned char high() const
92  {
93  return static_cast<unsigned char>(uc >> 8);
94  }
95 
99  unsigned char low() const
100  {
101  return static_cast<unsigned char>(uc);
102  }
103 
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 
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 
153 class KJS_EXPORT UString
154 {
155  KJS_EXPORT friend bool operator==(const UString &, const UString &);
156 
157 public:
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:
233  UString();
237  enum Empty { empty };
238  UString(Empty);
242  explicit UString(char c);
246  UString(const char *c);
247  UString(const char *c, size_t length);
252  UString(const UChar *c, int length);
259  UString(UChar *c, int length, bool copy);
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 
275  KJS_EXTERNAL_EXPORT UString(const QString &);
279  KJS_EXTERNAL_EXPORT UString(const DOM::DOMString &);
280 
284  UString(const UString &, const UString &);
288  ~UString() {}
289 
293  static UString from(int i);
297  static UString from(unsigned int u);
301  static UString from(long u);
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 
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 
339  CString cstring() const;
347  char *ascii() const;
348 
356  CString UTF8String() const;
357 
361  KJS_EXTERNAL_EXPORT DOM::DOMString domString() const;
365  KJS_EXTERNAL_EXPORT QString qstring() const;
369  KJS_EXTERNAL_EXPORT QConstString qconststring() const;
370 
374  UString &operator=(const char *c);
375  UString &operator=(Empty);
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 
391  const UChar *data() const
392  {
393  return m_rep->data();
394  }
398  bool isNull() const
399  {
400  return (m_rep == &Rep::null);
401  }
405  bool isEmpty() const
406  {
407  return (!m_rep->len);
408  }
416  bool is8Bit() const;
420  int size() const
421  {
422  return m_rep->size();
423  }
427  const UChar operator[](int pos) const;
436  double toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const;
437  double toDouble(bool tolerateTrailingJunk) const;
438  double toDouble() const;
439 
444  uint32_t toStrictUInt32(bool *ok = nullptr) const;
445 
452  unsigned toArrayIndex(bool *ok = nullptr) const;
453 
458  int find(const UString &f, int pos = 0) const;
459  int find(UChar, int pos = 0) const;
465  int rfind(const UString &f, int pos) const;
466  int rfind(UChar, int pos) const;
470  UString substr(int pos = 0, int len = -1) const;
474  static const UString &null();
475 
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:1134
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-2020 The KDE developers.
Generated on Mon Aug 10 2020 23:04:04 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.