source: webkit/trunk/JavaScriptCore/runtime/UString.h@ 52047

Last change on this file since 52047 was 52047, checked in by [email protected], 15 years ago

Rolled out my last patch because the bots were crashing

  • Property svn:eol-style set to native
File size: 28.6 KB
Line 
1/*
2 * Copyright (C) 1999-2000 Harri Porten ([email protected])
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4 * Copyright (C) 2009 Google 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 UString_h
24#define UString_h
25
26#include "Collector.h"
27#include <stdint.h>
28#include <string.h>
29#include <wtf/Assertions.h>
30#include <wtf/CrossThreadRefCounted.h>
31#include <wtf/OwnFastMallocPtr.h>
32#include <wtf/PassRefPtr.h>
33#include <wtf/PtrAndFlags.h>
34#include <wtf/RefPtr.h>
35#include <wtf/Vector.h>
36#include <wtf/unicode/Unicode.h>
37
38namespace JSC {
39
40 using WTF::PlacementNewAdoptType;
41 using WTF::PlacementNewAdopt;
42
43 class IdentifierTable;
44
45 class CString {
46 public:
47 CString()
48 : m_length(0)
49 , m_data(0)
50 {
51 }
52
53 CString(const char*);
54 CString(const char*, size_t);
55 CString(const CString&);
56
57 ~CString();
58
59 static CString adopt(char*, size_t); // buffer should be allocated with new[].
60
61 CString& append(const CString&);
62 CString& operator=(const char* c);
63 CString& operator=(const CString&);
64 CString& operator+=(const CString& c) { return append(c); }
65
66 size_t size() const { return m_length; }
67 const char* c_str() const { return m_data; }
68
69 private:
70 size_t m_length;
71 char* m_data;
72 };
73
74 typedef Vector<char, 32> CStringBuffer;
75
76 class UString {
77 friend class JIT;
78
79 public:
80 typedef CrossThreadRefCounted<OwnFastMallocPtr<UChar> > SharedUChar;
81 struct BaseString;
82 struct Rep : Noncopyable {
83 friend class JIT;
84
85 static PassRefPtr<Rep> create(UChar* buffer, int length)
86 {
87 return adoptRef(new BaseString(buffer, length));
88 }
89
90 static PassRefPtr<Rep> createEmptyBuffer(size_t size)
91 {
92 // Guard against integer overflow
93 if (size < (std::numeric_limits<size_t>::max() / sizeof(UChar))) {
94 void* buf = 0;
95 if (tryFastMalloc(size * sizeof(UChar)).getValue(buf))
96 return adoptRef(new BaseString(static_cast<UChar*>(buf), 0, size));
97 }
98 return adoptRef(new BaseString(0, 0, 0));
99 }
100
101 static PassRefPtr<Rep> createCopying(const UChar*, int);
102 static PassRefPtr<Rep> create(PassRefPtr<Rep> base, int offset, int length);
103
104 // Constructs a string from a UTF-8 string, using strict conversion (see comments in UTF8.h).
105 // Returns UString::Rep::null for null input or conversion failure.
106 static PassRefPtr<Rep> createFromUTF8(const char*);
107
108 // Uses SharedUChar to have joint ownership over the UChar*.
109 static PassRefPtr<Rep> create(UChar*, int, PassRefPtr<SharedUChar>);
110
111 SharedUChar* sharedBuffer();
112 void destroy();
113
114 bool baseIsSelf() const { return m_identifierTableAndFlags.isFlagSet(BaseStringFlag); }
115 UChar* data() const;
116 int size() const { return len; }
117
118 unsigned hash() const { if (_hash == 0) _hash = computeHash(data(), len); return _hash; }
119 unsigned computedHash() const { ASSERT(_hash); return _hash; } // fast path for Identifiers
120
121 static unsigned computeHash(const UChar*, int length);
122 static unsigned computeHash(const char*, int length);
123 static unsigned computeHash(const char* s) { return computeHash(s, strlen(s)); }
124
125 IdentifierTable* identifierTable() const { return m_identifierTableAndFlags.get(); }
126 void setIdentifierTable(IdentifierTable* table) { ASSERT(!isStatic()); m_identifierTableAndFlags.set(table); }
127
128 bool isStatic() const { return m_identifierTableAndFlags.isFlagSet(StaticFlag); }
129 void setStatic(bool);
130 void setBaseString(PassRefPtr<BaseString>);
131 BaseString* baseString();
132 const BaseString* baseString() const;
133
134 Rep* ref() { ++rc; return this; }
135 ALWAYS_INLINE void deref() { if (--rc == 0) destroy(); }
136
137 void checkConsistency() const;
138 enum UStringFlags {
139 StaticFlag,
140 BaseStringFlag
141 };
142
143 // unshared data
144 int offset;
145 int len;
146 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.
147 mutable unsigned _hash;
148 PtrAndFlags<IdentifierTable, UStringFlags> m_identifierTableAndFlags;
149
150 static BaseString& null() { return *nullBaseString; }
151 static BaseString& empty() { return *emptyBaseString; }
152
153 bool reserveCapacity(int capacity);
154
155 protected:
156 // Constructor for use by BaseString subclass; they use the union with m_baseString for another purpose.
157 Rep(int length)
158 : offset(0)
159 , len(length)
160 , rc(1)
161 , _hash(0)
162 , m_baseString(0)
163 {
164 }
165
166 Rep(PassRefPtr<BaseString> base, int offsetInBase, int length)
167 : offset(offsetInBase)
168 , len(length)
169 , rc(1)
170 , _hash(0)
171 , m_baseString(base.releaseRef())
172 {
173 checkConsistency();
174 }
175
176 union {
177 // If !baseIsSelf()
178 BaseString* m_baseString;
179 // If baseIsSelf()
180 SharedUChar* m_sharedBuffer;
181 };
182
183 private:
184 // For SmallStringStorage which allocates an array and does initialization manually.
185 Rep() { }
186
187 friend class SmallStringsStorage;
188 friend void initializeUString();
189 JS_EXPORTDATA static BaseString* nullBaseString;
190 JS_EXPORTDATA static BaseString* emptyBaseString;
191 };
192
193
194 struct BaseString : public Rep {
195 bool isShared() { return rc != 1 || isBufferReadOnly(); }
196 void setSharedBuffer(PassRefPtr<SharedUChar>);
197
198 bool isBufferReadOnly()
199 {
200 if (!m_sharedBuffer)
201 return false;
202 return slowIsBufferReadOnly();
203 }
204
205 // potentially shared data.
206 UChar* buf;
207 int preCapacity;
208 int usedPreCapacity;
209 int capacity;
210 int usedCapacity;
211
212 size_t reportedCost;
213
214 private:
215 BaseString(UChar* buffer, int length, int additionalCapacity = 0)
216 : Rep(length)
217 , buf(buffer)
218 , preCapacity(0)
219 , usedPreCapacity(0)
220 , capacity(length + additionalCapacity)
221 , usedCapacity(length)
222 , reportedCost(0)
223 {
224 m_identifierTableAndFlags.setFlag(BaseStringFlag);
225 checkConsistency();
226 }
227
228 SharedUChar* sharedBuffer();
229 bool slowIsBufferReadOnly();
230
231 friend struct Rep;
232 friend class SmallStringsStorage;
233 friend void initializeUString();
234 };
235
236 public:
237 UString();
238 // Constructor for null-terminated ASCII string.
239 UString(const char*);
240 // Constructor for non-null-terminated ASCII string.
241 UString(const char*, int length);
242 UString(const UChar*, int length);
243 UString(UChar*, int length, bool copy);
244
245 UString(const UString& s)
246 : m_rep(s.m_rep)
247 {
248 }
249
250 UString(const Vector<UChar>& buffer);
251
252 ~UString()
253 {
254 }
255
256 // Special constructor for cases where we overwrite an object in place.
257 UString(PlacementNewAdoptType)
258 : m_rep(PlacementNewAdopt)
259 {
260 }
261
262 static UString from(int);
263 static UString from(long long);
264 static UString from(unsigned int);
265 static UString from(long);
266 static UString from(double);
267
268 struct Range {
269 public:
270 Range(int pos, int len)
271 : position(pos)
272 , length(len)
273 {
274 }
275
276 Range()
277 {
278 }
279
280 int position;
281 int length;
282 };
283
284 UString spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const;
285
286 UString replaceRange(int rangeStart, int RangeEnd, const UString& replacement) const;
287
288 UString& append(const UString&);
289 UString& append(const char*);
290 UString& append(UChar);
291 UString& append(char c) { return append(static_cast<UChar>(static_cast<unsigned char>(c))); }
292 UString& append(const UChar*, int size);
293
294 bool getCString(CStringBuffer&) const;
295
296 // NOTE: This method should only be used for *debugging* purposes as it
297 // is neither Unicode safe nor free from side effects nor thread-safe.
298 char* ascii() const;
299
300 /**
301 * Convert the string to UTF-8, assuming it is UTF-16 encoded.
302 * In non-strict mode, this function is tolerant of badly formed UTF-16, it
303 * can create UTF-8 strings that are invalid because they have characters in
304 * the range U+D800-U+DDFF, U+FFFE, or U+FFFF, but the UTF-8 string is
305 * guaranteed to be otherwise valid.
306 * In strict mode, error is returned as null CString.
307 */
308 CString UTF8String(bool strict = false) const;
309
310 UString& operator=(const char*c);
311
312 UString& operator+=(const UString& s) { return append(s); }
313 UString& operator+=(const char* s) { return append(s); }
314
315 const UChar* data() const { return m_rep->data(); }
316
317 bool isNull() const { return (m_rep == &Rep::null()); }
318 bool isEmpty() const { return (!m_rep->len); }
319
320 bool is8Bit() const;
321
322 int size() const { return m_rep->size(); }
323
324 UChar operator[](int pos) const;
325
326 double toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const;
327 double toDouble(bool tolerateTrailingJunk) const;
328 double toDouble() const;
329
330 uint32_t toUInt32(bool* ok = 0) const;
331 uint32_t toUInt32(bool* ok, bool tolerateEmptyString) const;
332 uint32_t toStrictUInt32(bool* ok = 0) const;
333
334 unsigned toArrayIndex(bool* ok = 0) const;
335
336 int find(const UString& f, int pos = 0) const;
337 int find(UChar, int pos = 0) const;
338 int rfind(const UString& f, int pos) const;
339 int rfind(UChar, int pos) const;
340
341 UString substr(int pos = 0, int len = -1) const;
342
343 static const UString& null() { return *nullUString; }
344
345 Rep* rep() const { return m_rep.get(); }
346 static Rep* nullRep();
347
348 UString(PassRefPtr<Rep> r)
349 : m_rep(r)
350 {
351 ASSERT(m_rep);
352 }
353
354 size_t cost() const;
355
356 // Attempt to grow this string such that it can grow to a total length of 'capacity'
357 // without reallocation. This may fail a number of reasons - if the BasicString is
358 // shared and another string is using part of the capacity beyond our end point, if
359 // the realloc fails, or if this string is empty and has no storage.
360 //
361 // This method returns a boolean indicating success.
362 bool reserveCapacity(int capacity)
363 {
364 return m_rep->reserveCapacity(capacity);
365 }
366
367 private:
368 void expandCapacity(int requiredLength);
369 void expandPreCapacity(int requiredPreCap);
370 void makeNull();
371
372 RefPtr<Rep> m_rep;
373 static UString* nullUString;
374
375 friend void initializeUString();
376 friend bool operator==(const UString&, const UString&);
377 friend PassRefPtr<Rep> concatenate(Rep*, Rep*); // returns 0 if out of memory
378 };
379 PassRefPtr<UString::Rep> concatenate(UString::Rep*, UString::Rep*);
380 PassRefPtr<UString::Rep> concatenate(UString::Rep*, int);
381 PassRefPtr<UString::Rep> concatenate(UString::Rep*, double);
382
383 inline bool operator==(const UString& s1, const UString& s2)
384 {
385 int size = s1.size();
386 switch (size) {
387 case 0:
388 return !s2.size();
389 case 1:
390 return s2.size() == 1 && s1.data()[0] == s2.data()[0];
391 case 2: {
392 if (s2.size() != 2)
393 return false;
394 const UChar* d1 = s1.data();
395 const UChar* d2 = s2.data();
396 return (d1[0] == d2[0]) & (d1[1] == d2[1]);
397 }
398 default:
399 return s2.size() == size && memcmp(s1.data(), s2.data(), size * sizeof(UChar)) == 0;
400 }
401 }
402
403
404 inline bool operator!=(const UString& s1, const UString& s2)
405 {
406 return !JSC::operator==(s1, s2);
407 }
408
409 bool operator<(const UString& s1, const UString& s2);
410 bool operator>(const UString& s1, const UString& s2);
411
412 bool operator==(const UString& s1, const char* s2);
413
414 inline bool operator!=(const UString& s1, const char* s2)
415 {
416 return !JSC::operator==(s1, s2);
417 }
418
419 inline bool operator==(const char *s1, const UString& s2)
420 {
421 return operator==(s2, s1);
422 }
423
424 inline bool operator!=(const char *s1, const UString& s2)
425 {
426 return !JSC::operator==(s1, s2);
427 }
428
429 bool operator==(const CString&, const CString&);
430
431 inline UString operator+(const UString& s1, const UString& s2)
432 {
433 RefPtr<UString::Rep> result = concatenate(s1.rep(), s2.rep());
434 return UString(result ? result.release() : UString::nullRep());
435 }
436
437 int compare(const UString&, const UString&);
438
439 bool equal(const UString::Rep*, const UString::Rep*);
440
441 inline PassRefPtr<UString::Rep> UString::Rep::create(PassRefPtr<UString::Rep> rep, int offset, int length)
442 {
443 ASSERT(rep);
444 rep->checkConsistency();
445
446 int repOffset = rep->offset;
447
448 PassRefPtr<BaseString> base = rep->baseString();
449
450 ASSERT(-(offset + repOffset) <= base->usedPreCapacity);
451 ASSERT(offset + repOffset + length <= base->usedCapacity);
452
453 // Steal the single reference this Rep was created with.
454 return adoptRef(new Rep(base, repOffset + offset, length));
455 }
456
457 inline UChar* UString::Rep::data() const
458 {
459 const BaseString* base = baseString();
460 return base->buf + base->preCapacity + offset;
461 }
462
463 inline void UString::Rep::setStatic(bool v)
464 {
465 ASSERT(!identifierTable());
466 if (v)
467 m_identifierTableAndFlags.setFlag(StaticFlag);
468 else
469 m_identifierTableAndFlags.clearFlag(StaticFlag);
470 }
471
472 inline void UString::Rep::setBaseString(PassRefPtr<BaseString> base)
473 {
474 ASSERT(base != this);
475 ASSERT(!baseIsSelf());
476 m_baseString = base.releaseRef();
477 }
478
479 inline UString::BaseString* UString::Rep::baseString()
480 {
481 return !baseIsSelf() ? m_baseString : reinterpret_cast<BaseString*>(this) ;
482 }
483
484 inline const UString::BaseString* UString::Rep::baseString() const
485 {
486 return const_cast<Rep*>(this)->baseString();
487 }
488
489#ifdef NDEBUG
490 inline void UString::Rep::checkConsistency() const
491 {
492 }
493#endif
494
495 inline UString::UString()
496 : m_rep(&Rep::null())
497 {
498 }
499
500 // Rule from ECMA 15.2 about what an array index is.
501 // Must exactly match string form of an unsigned integer, and be less than 2^32 - 1.
502 inline unsigned UString::toArrayIndex(bool* ok) const
503 {
504 unsigned i = toStrictUInt32(ok);
505 if (ok && i >= 0xFFFFFFFFU)
506 *ok = false;
507 return i;
508 }
509
510 // We'd rather not do shared substring append for small strings, since
511 // this runs too much risk of a tiny initial string holding down a
512 // huge buffer.
513 // FIXME: this should be size_t but that would cause warnings until we
514 // fix UString sizes to be size_t instead of int
515 static const int minShareSize = Heap::minExtraCostSize / sizeof(UChar);
516
517 inline size_t UString::cost() const
518 {
519 BaseString* base = m_rep->baseString();
520 size_t capacity = (base->capacity + base->preCapacity) * sizeof(UChar);
521 size_t reportedCost = base->reportedCost;
522 ASSERT(capacity >= reportedCost);
523
524 size_t capacityDelta = capacity - reportedCost;
525
526 if (capacityDelta < static_cast<size_t>(minShareSize))
527 return 0;
528
529 base->reportedCost = capacity;
530
531 return capacityDelta;
532 }
533
534 struct IdentifierRepHash : PtrHash<RefPtr<JSC::UString::Rep> > {
535 static unsigned hash(const RefPtr<JSC::UString::Rep>& key) { return key->computedHash(); }
536 static unsigned hash(JSC::UString::Rep* key) { return key->computedHash(); }
537 };
538
539 void initializeUString();
540
541 template<typename StringType>
542 class StringTypeAdapter {
543 };
544
545 template<>
546 class StringTypeAdapter<char*>
547 {
548 public:
549 StringTypeAdapter<char*>(char* buffer)
550 : m_buffer((unsigned char*)buffer)
551 , m_length(strlen(buffer))
552 {
553 }
554
555 unsigned length() { return m_length; }
556
557 void writeTo(UChar* destination)
558 {
559 for (unsigned i = 0; i < m_length; ++i)
560 destination[i] = m_buffer[i];
561 }
562
563 private:
564 const unsigned char* m_buffer;
565 unsigned m_length;
566 };
567
568 template<>
569 class StringTypeAdapter<const char*>
570 {
571 public:
572 StringTypeAdapter<const char*>(const char* buffer)
573 : m_buffer((unsigned char*)buffer)
574 , m_length(strlen(buffer))
575 {
576 }
577
578 unsigned length() { return m_length; }
579
580 void writeTo(UChar* destination)
581 {
582 for (unsigned i = 0; i < m_length; ++i)
583 destination[i] = m_buffer[i];
584 }
585
586 private:
587 const unsigned char* m_buffer;
588 unsigned m_length;
589 };
590
591 template<>
592 class StringTypeAdapter<UString>
593 {
594 public:
595 StringTypeAdapter<UString>(UString& string)
596 : m_data(string.data())
597 , m_length(string.size())
598 {
599 }
600
601 unsigned length() { return m_length; }
602
603 void writeTo(UChar* destination)
604 {
605 for (unsigned i = 0; i < m_length; ++i)
606 destination[i] = m_data[i];
607 }
608
609 private:
610 const UChar* m_data;
611 unsigned m_length;
612 };
613
614 template<typename StringType1, typename StringType2>
615 UString makeString(StringType1 string1, StringType2 string2)
616 {
617 StringTypeAdapter<StringType1> adapter1(string1);
618 StringTypeAdapter<StringType2> adapter2(string2);
619
620 UChar* buffer;
621 unsigned length = adapter1.length() + adapter2.length();
622 if (!tryFastMalloc(length * sizeof(UChar)).getValue(buffer))
623 return UString();
624
625 UChar* result = buffer;
626 adapter1.writeTo(result);
627 result += adapter1.length();
628 adapter2.writeTo(result);
629
630 return UString(buffer, length, false);
631 }
632
633 template<typename StringType1, typename StringType2, typename StringType3>
634 UString makeString(StringType1 string1, StringType2 string2, StringType3 string3)
635 {
636 StringTypeAdapter<StringType1> adapter1(string1);
637 StringTypeAdapter<StringType2> adapter2(string2);
638 StringTypeAdapter<StringType3> adapter3(string3);
639
640 UChar* buffer;
641 unsigned length = adapter1.length() + adapter2.length() + adapter3.length();
642 if (!tryFastMalloc(length * sizeof(UChar)).getValue(buffer))
643 return UString();
644
645 UChar* result = buffer;
646 adapter1.writeTo(result);
647 result += adapter1.length();
648 adapter2.writeTo(result);
649 result += adapter2.length();
650 adapter3.writeTo(result);
651
652 return UString(buffer, length, false);
653 }
654
655 template<typename StringType1, typename StringType2, typename StringType3, typename StringType4>
656 UString makeString(StringType1 string1, StringType2 string2, StringType3 string3, StringType4 string4)
657 {
658 StringTypeAdapter<StringType1> adapter1(string1);
659 StringTypeAdapter<StringType2> adapter2(string2);
660 StringTypeAdapter<StringType3> adapter3(string3);
661 StringTypeAdapter<StringType4> adapter4(string4);
662
663 UChar* buffer;
664 unsigned length = adapter1.length() + adapter2.length() + adapter3.length() + adapter4.length();
665 if (!tryFastMalloc(length * sizeof(UChar)).getValue(buffer))
666 return UString();
667
668 UChar* result = buffer;
669 adapter1.writeTo(result);
670 result += adapter1.length();
671 adapter2.writeTo(result);
672 result += adapter2.length();
673 adapter3.writeTo(result);
674 result += adapter3.length();
675 adapter4.writeTo(result);
676
677 return UString(buffer, length, false);
678 }
679
680 template<typename StringType1, typename StringType2, typename StringType3, typename StringType4, typename StringType5>
681 UString makeString(StringType1 string1, StringType2 string2, StringType3 string3, StringType4 string4, StringType5 string5)
682 {
683 StringTypeAdapter<StringType1> adapter1(string1);
684 StringTypeAdapter<StringType2> adapter2(string2);
685 StringTypeAdapter<StringType3> adapter3(string3);
686 StringTypeAdapter<StringType4> adapter4(string4);
687 StringTypeAdapter<StringType5> adapter5(string5);
688
689 UChar* buffer;
690 unsigned length = adapter1.length() + adapter2.length() + adapter3.length() + adapter4.length() + adapter5.length();
691 if (!tryFastMalloc(length * sizeof(UChar)).getValue(buffer))
692 return UString();
693
694 UChar* result = buffer;
695 adapter1.writeTo(result);
696 result += adapter1.length();
697 adapter2.writeTo(result);
698 result += adapter2.length();
699 adapter3.writeTo(result);
700 result += adapter3.length();
701 adapter4.writeTo(result);
702 result += adapter4.length();
703 adapter5.writeTo(result);
704
705 return UString(buffer, length, false);
706 }
707
708 template<typename StringType1, typename StringType2, typename StringType3, typename StringType4, typename StringType5, typename StringType6>
709 UString makeString(StringType1 string1, StringType2 string2, StringType3 string3, StringType4 string4, StringType5 string5, StringType6 string6)
710 {
711 StringTypeAdapter<StringType1> adapter1(string1);
712 StringTypeAdapter<StringType2> adapter2(string2);
713 StringTypeAdapter<StringType3> adapter3(string3);
714 StringTypeAdapter<StringType4> adapter4(string4);
715 StringTypeAdapter<StringType5> adapter5(string5);
716 StringTypeAdapter<StringType6> adapter6(string6);
717
718 UChar* buffer;
719 unsigned length = adapter1.length() + adapter2.length() + adapter3.length() + adapter4.length() + adapter5.length() + adapter6.length();
720 if (!tryFastMalloc(length * sizeof(UChar)).getValue(buffer))
721 return UString();
722
723 UChar* result = buffer;
724 adapter1.writeTo(result);
725 result += adapter1.length();
726 adapter2.writeTo(result);
727 result += adapter2.length();
728 adapter3.writeTo(result);
729 result += adapter3.length();
730 adapter4.writeTo(result);
731 result += adapter4.length();
732 adapter5.writeTo(result);
733 result += adapter5.length();
734 adapter6.writeTo(result);
735
736 return UString(buffer, length, false);
737 }
738
739 template<typename StringType1, typename StringType2, typename StringType3, typename StringType4, typename StringType5, typename StringType6, typename StringType7>
740 UString makeString(StringType1 string1, StringType2 string2, StringType3 string3, StringType4 string4, StringType5 string5, StringType6 string6, StringType7 string7)
741 {
742 StringTypeAdapter<StringType1> adapter1(string1);
743 StringTypeAdapter<StringType2> adapter2(string2);
744 StringTypeAdapter<StringType3> adapter3(string3);
745 StringTypeAdapter<StringType4> adapter4(string4);
746 StringTypeAdapter<StringType5> adapter5(string5);
747 StringTypeAdapter<StringType6> adapter6(string6);
748 StringTypeAdapter<StringType7> adapter7(string7);
749
750 UChar* buffer;
751 unsigned length = adapter1.length() + adapter2.length() + adapter3.length() + adapter4.length() + adapter5.length() + adapter6.length() + adapter7.length();
752 if (!tryFastMalloc(length * sizeof(UChar)).getValue(buffer))
753 return UString();
754
755 UChar* result = buffer;
756 adapter1.writeTo(result);
757 result += adapter1.length();
758 adapter2.writeTo(result);
759 result += adapter2.length();
760 adapter3.writeTo(result);
761 result += adapter3.length();
762 adapter4.writeTo(result);
763 result += adapter4.length();
764 adapter5.writeTo(result);
765 result += adapter5.length();
766 adapter6.writeTo(result);
767 result += adapter6.length();
768 adapter7.writeTo(result);
769
770 return UString(buffer, length, false);
771 }
772
773 template<typename StringType1, typename StringType2, typename StringType3, typename StringType4, typename StringType5, typename StringType6, typename StringType7, typename StringType8>
774 UString makeString(StringType1 string1, StringType2 string2, StringType3 string3, StringType4 string4, StringType5 string5, StringType6 string6, StringType7 string7, StringType8 string8)
775 {
776 StringTypeAdapter<StringType1> adapter1(string1);
777 StringTypeAdapter<StringType2> adapter2(string2);
778 StringTypeAdapter<StringType3> adapter3(string3);
779 StringTypeAdapter<StringType4> adapter4(string4);
780 StringTypeAdapter<StringType5> adapter5(string5);
781 StringTypeAdapter<StringType6> adapter6(string6);
782 StringTypeAdapter<StringType7> adapter7(string7);
783 StringTypeAdapter<StringType8> adapter8(string8);
784
785 UChar* buffer;
786 unsigned length = adapter1.length() + adapter2.length() + adapter3.length() + adapter4.length() + adapter5.length() + adapter6.length() + adapter7.length() + adapter8.length();
787 if (!tryFastMalloc(length * sizeof(UChar)).getValue(buffer))
788 return UString();
789
790 UChar* result = buffer;
791 adapter1.writeTo(result);
792 result += adapter1.length();
793 adapter2.writeTo(result);
794 result += adapter2.length();
795 adapter3.writeTo(result);
796 result += adapter3.length();
797 adapter4.writeTo(result);
798 result += adapter4.length();
799 adapter5.writeTo(result);
800 result += adapter5.length();
801 adapter6.writeTo(result);
802 result += adapter6.length();
803 adapter7.writeTo(result);
804 result += adapter7.length();
805 adapter8.writeTo(result);
806
807 return UString(buffer, length, false);
808 }
809
810} // namespace JSC
811
812namespace WTF {
813
814 template<typename T> struct DefaultHash;
815 template<typename T> struct StrHash;
816
817 template<> struct StrHash<JSC::UString::Rep*> {
818 static unsigned hash(const JSC::UString::Rep* key) { return key->hash(); }
819 static bool equal(const JSC::UString::Rep* a, const JSC::UString::Rep* b) { return JSC::equal(a, b); }
820 static const bool safeToCompareToEmptyOrDeleted = false;
821 };
822
823 template<> struct StrHash<RefPtr<JSC::UString::Rep> > : public StrHash<JSC::UString::Rep*> {
824 using StrHash<JSC::UString::Rep*>::hash;
825 static unsigned hash(const RefPtr<JSC::UString::Rep>& key) { return key->hash(); }
826 using StrHash<JSC::UString::Rep*>::equal;
827 static bool equal(const RefPtr<JSC::UString::Rep>& a, const RefPtr<JSC::UString::Rep>& b) { return JSC::equal(a.get(), b.get()); }
828 static bool equal(const JSC::UString::Rep* a, const RefPtr<JSC::UString::Rep>& b) { return JSC::equal(a, b.get()); }
829 static bool equal(const RefPtr<JSC::UString::Rep>& a, const JSC::UString::Rep* b) { return JSC::equal(a.get(), b); }
830
831 static const bool safeToCompareToEmptyOrDeleted = false;
832 };
833
834 template<> struct DefaultHash<JSC::UString::Rep*> {
835 typedef StrHash<JSC::UString::Rep*> Hash;
836 };
837
838 template<> struct DefaultHash<RefPtr<JSC::UString::Rep> > {
839 typedef StrHash<RefPtr<JSC::UString::Rep> > Hash;
840
841 };
842
843} // namespace WTF
844
845#endif
Note: See TracBrowser for help on using the repository browser.