mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 1 | // -*- c-basic-offset: 2 -*- |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 2 | /* |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 3 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) |
darin@apple.com | 3beb300 | 2008-04-28 05:59:07 +0000 | [diff] [blame] | 4 | * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
darin | 5769f31 | 2007-07-18 02:25:38 +0000 | [diff] [blame] | 5 | * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Library General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Library General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Library General Public License |
| 18 | * along with this library; see the file COPYING.LIB. If not, write to |
mjs | cdff33b | 2006-01-23 21:41:36 +0000 | [diff] [blame] | 19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
ggaren | 07d4ce6 | 2005-07-14 18:27:04 +0000 | [diff] [blame] | 20 | * Boston, MA 02110-1301, USA. |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 21 | * |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
darin | 6e70e6a | 2006-01-12 15:25:58 +0000 | [diff] [blame] | 24 | #include "config.h" |
| 25 | #include "ustring.h" |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 26 | |
ggaren | ff59958 | 2007-03-07 04:25:49 +0000 | [diff] [blame] | 27 | #include "JSLock.h" |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 28 | #include "collector.h" |
ggaren | ff59958 | 2007-03-07 04:25:49 +0000 | [diff] [blame] | 29 | #include "dtoa.h" |
darin@apple.com | 3dcb636 | 2008-06-16 04:00:19 +0000 | [diff] [blame] | 30 | #include "JSFunction.h" |
ggaren | ff59958 | 2007-03-07 04:25:49 +0000 | [diff] [blame] | 31 | #include "identifier.h" |
| 32 | #include "operations.h" |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 33 | #include <ctype.h> |
ggaren | ff59958 | 2007-03-07 04:25:49 +0000 | [diff] [blame] | 34 | #include <float.h> |
hausmann | 0437311 | 2007-10-10 11:12:20 +0000 | [diff] [blame] | 35 | #include <limits.h> |
ggaren | ff59958 | 2007-03-07 04:25:49 +0000 | [diff] [blame] | 36 | #include <math.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
ggaren | 7213ee0 | 2007-10-16 23:25:33 +0000 | [diff] [blame] | 39 | #include <wtf/Assertions.h> |
darin | 185ca85 | 2007-10-16 20:53:21 +0000 | [diff] [blame] | 40 | #include <wtf/ASCIICType.h> |
mrowe@apple.com | 915de55 | 2007-10-29 02:52:04 +0000 | [diff] [blame] | 41 | #include <wtf/MathExtras.h> |
ggaren | ff59958 | 2007-03-07 04:25:49 +0000 | [diff] [blame] | 42 | #include <wtf/Vector.h> |
alp@webkit.org | f4917c6 | 2007-11-13 09:49:50 +0000 | [diff] [blame] | 43 | #include <wtf/unicode/UTF8.h> |
ggaren | ff59958 | 2007-03-07 04:25:49 +0000 | [diff] [blame] | 44 | |
mjs | 3bfb61b | 2006-03-02 09:12:06 +0000 | [diff] [blame] | 45 | #if HAVE(STRING_H) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 46 | #include <string.h> |
| 47 | #endif |
mjs | 3bfb61b | 2006-03-02 09:12:06 +0000 | [diff] [blame] | 48 | #if HAVE(STRINGS_H) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 49 | #include <strings.h> |
| 50 | #endif |
| 51 | |
darin | eb0d86d | 2007-10-16 20:13:24 +0000 | [diff] [blame] | 52 | using namespace WTF; |
alp@webkit.org | f4917c6 | 2007-11-13 09:49:50 +0000 | [diff] [blame] | 53 | using namespace WTF::Unicode; |
darin | eb0d86d | 2007-10-16 20:13:24 +0000 | [diff] [blame] | 54 | using namespace std; |
darin | e3385e9 | 2004-08-10 18:43:51 +0000 | [diff] [blame] | 55 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 56 | namespace KJS { |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 57 | |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 58 | extern const double NaN; |
| 59 | extern const double Inf; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 60 | |
darin | 6238e2a | 2007-08-14 22:19:04 +0000 | [diff] [blame] | 61 | static inline const size_t overflowIndicator() { return std::numeric_limits<size_t>::max(); } |
| 62 | static inline const size_t maxUChars() { return std::numeric_limits<size_t>::max() / sizeof(UChar); } |
darin | 0d66de7 | 2007-08-08 00:45:39 +0000 | [diff] [blame] | 63 | |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 64 | static inline UChar* allocChars(size_t length) |
| 65 | { |
darin | 0d66de7 | 2007-08-08 00:45:39 +0000 | [diff] [blame] | 66 | ASSERT(length); |
darin | 6238e2a | 2007-08-14 22:19:04 +0000 | [diff] [blame] | 67 | if (length > maxUChars()) |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 68 | return 0; |
| 69 | return static_cast<UChar*>(fastMalloc(sizeof(UChar) * length)); |
| 70 | } |
| 71 | |
darin | 0d66de7 | 2007-08-08 00:45:39 +0000 | [diff] [blame] | 72 | static inline UChar* reallocChars(UChar* buffer, size_t length) |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 73 | { |
darin | 0d66de7 | 2007-08-08 00:45:39 +0000 | [diff] [blame] | 74 | ASSERT(length); |
darin | 6238e2a | 2007-08-14 22:19:04 +0000 | [diff] [blame] | 75 | if (length > maxUChars()) |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 76 | return 0; |
| 77 | return static_cast<UChar*>(fastRealloc(buffer, sizeof(UChar) * length)); |
| 78 | } |
| 79 | |
kjk | 67f5d5a | 2007-02-17 09:07:39 +0000 | [diff] [blame] | 80 | COMPILE_ASSERT(sizeof(UChar) == 2, uchar_is_2_bytes) |
| 81 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 82 | CString::CString(const char *c) |
| 83 | { |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 84 | length = strlen(c); |
| 85 | data = new char[length+1]; |
darin | 43d061b | 2003-10-27 04:21:15 +0000 | [diff] [blame] | 86 | memcpy(data, c, length + 1); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 87 | } |
| 88 | |
ggaren | df6e53c | 2006-06-05 16:37:59 +0000 | [diff] [blame] | 89 | CString::CString(const char *c, size_t len) |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 90 | { |
| 91 | length = len; |
| 92 | data = new char[len+1]; |
| 93 | memcpy(data, c, len); |
| 94 | data[len] = 0; |
| 95 | } |
| 96 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 97 | CString::CString(const CString &b) |
| 98 | { |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 99 | length = b.length; |
eseidel | b3ff607 | 2006-03-15 22:26:37 +0000 | [diff] [blame] | 100 | if (b.data) { |
rjw | c8b0fe8 | 2004-01-23 00:42:34 +0000 | [diff] [blame] | 101 | data = new char[length+1]; |
| 102 | memcpy(data, b.data, length + 1); |
| 103 | } |
eseidel | b3ff607 | 2006-03-15 22:26:37 +0000 | [diff] [blame] | 104 | else |
rjw | c8b0fe8 | 2004-01-23 00:42:34 +0000 | [diff] [blame] | 105 | data = 0; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | CString::~CString() |
| 109 | { |
| 110 | delete [] data; |
| 111 | } |
| 112 | |
ap@webkit.org | 11f3871 | 2008-04-07 06:33:20 +0000 | [diff] [blame] | 113 | CString CString::adopt(char* c, size_t len) |
| 114 | { |
| 115 | CString s; |
| 116 | s.data = c; |
| 117 | s.length = len; |
| 118 | |
| 119 | return s; |
| 120 | } |
| 121 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 122 | CString &CString::append(const CString &t) |
| 123 | { |
| 124 | char *n; |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 125 | n = new char[length+t.length+1]; |
| 126 | if (length) |
| 127 | memcpy(n, data, length); |
| 128 | if (t.length) |
| 129 | memcpy(n+length, t.data, t.length); |
| 130 | length += t.length; |
| 131 | n[length] = 0; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 132 | |
| 133 | delete [] data; |
| 134 | data = n; |
| 135 | |
| 136 | return *this; |
| 137 | } |
| 138 | |
| 139 | CString &CString::operator=(const char *c) |
| 140 | { |
| 141 | if (data) |
| 142 | delete [] data; |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 143 | length = strlen(c); |
| 144 | data = new char[length+1]; |
darin | 43d061b | 2003-10-27 04:21:15 +0000 | [diff] [blame] | 145 | memcpy(data, c, length + 1); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 146 | |
| 147 | return *this; |
| 148 | } |
| 149 | |
| 150 | CString &CString::operator=(const CString &str) |
| 151 | { |
| 152 | if (this == &str) |
| 153 | return *this; |
| 154 | |
| 155 | if (data) |
| 156 | delete [] data; |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 157 | length = str.length; |
eseidel | b3ff607 | 2006-03-15 22:26:37 +0000 | [diff] [blame] | 158 | if (str.data) { |
rjw | c8b0fe8 | 2004-01-23 00:42:34 +0000 | [diff] [blame] | 159 | data = new char[length + 1]; |
| 160 | memcpy(data, str.data, length + 1); |
| 161 | } |
eseidel | b3ff607 | 2006-03-15 22:26:37 +0000 | [diff] [blame] | 162 | else |
rjw | c8b0fe8 | 2004-01-23 00:42:34 +0000 | [diff] [blame] | 163 | data = 0; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 164 | |
| 165 | return *this; |
| 166 | } |
| 167 | |
darin | 04b93fa | 2005-05-16 18:57:29 +0000 | [diff] [blame] | 168 | bool operator==(const CString& c1, const CString& c2) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 169 | { |
ggaren | df6e53c | 2006-06-05 16:37:59 +0000 | [diff] [blame] | 170 | size_t len = c1.size(); |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 171 | return len == c2.size() && (len == 0 || memcmp(c1.c_str(), c2.c_str(), len) == 0); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 172 | } |
| 173 | |
ap@webkit.org | 11f3871 | 2008-04-07 06:33:20 +0000 | [diff] [blame] | 174 | // These static strings are immutable, except for rc, whose initial value is chosen to reduce the possibility of it becoming zero due to ref/deref not being thread-safe. |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 175 | static UChar sharedEmptyChar; |
ap@webkit.org | 0ea9a69 | 2008-04-18 19:46:13 +0000 | [diff] [blame] | 176 | UString::Rep UString::Rep::null = { 0, 0, INT_MAX / 2, 0, 0, &UString::Rep::null, true, 0, 0, 0, 0, 0, 0 }; |
| 177 | UString::Rep UString::Rep::empty = { 0, 0, INT_MAX / 2, 0, 0, &UString::Rep::empty, true, 0, &sharedEmptyChar, 0, 0, 0, 0 }; |
ap@webkit.org | 11f3871 | 2008-04-07 06:33:20 +0000 | [diff] [blame] | 178 | |
| 179 | static char* statBuffer = 0; // Only used for debugging via UString::ascii(). |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 180 | |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 181 | PassRefPtr<UString::Rep> UString::Rep::createCopying(const UChar *d, int l) |
mjs | 80a4d8e | 2005-04-15 01:26:26 +0000 | [diff] [blame] | 182 | { |
| 183 | int sizeInBytes = l * sizeof(UChar); |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 184 | UChar *copyD = static_cast<UChar *>(fastMalloc(sizeInBytes)); |
mjs | 80a4d8e | 2005-04-15 01:26:26 +0000 | [diff] [blame] | 185 | memcpy(copyD, d, sizeInBytes); |
| 186 | |
| 187 | return create(copyD, l); |
| 188 | } |
| 189 | |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 190 | PassRefPtr<UString::Rep> UString::Rep::create(UChar *d, int l) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 191 | { |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 192 | Rep* r = new Rep; |
mjs | 7a882e8 | 2004-04-16 18:54:21 +0000 | [diff] [blame] | 193 | r->offset = 0; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 194 | r->len = l; |
mjs | 0ee0df7 | 2005-12-08 18:19:28 +0000 | [diff] [blame] | 195 | r->rc = 1; |
darin | 5721aa9 | 2002-11-20 01:23:02 +0000 | [diff] [blame] | 196 | r->_hash = 0; |
ap@webkit.org | 0ea9a69 | 2008-04-18 19:46:13 +0000 | [diff] [blame] | 197 | r->identifierTable = 0; |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 198 | r->baseString = r; |
ap@webkit.org | 0ea9a69 | 2008-04-18 19:46:13 +0000 | [diff] [blame] | 199 | r->isStatic = false; |
kmccullough@apple.com | 9f7c99e | 2008-01-18 21:05:16 +0000 | [diff] [blame] | 200 | r->reportedCost = 0; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 201 | r->buf = d; |
| 202 | r->usedCapacity = l; |
| 203 | r->capacity = l; |
mjs | 3752117 | 2004-04-19 21:26:40 +0000 | [diff] [blame] | 204 | r->usedPreCapacity = 0; |
| 205 | r->preCapacity = 0; |
mjs | 03a1087 | 2005-12-20 20:12:50 +0000 | [diff] [blame] | 206 | |
mjs | 0ee0df7 | 2005-12-08 18:19:28 +0000 | [diff] [blame] | 207 | // steal the single reference this Rep was created with |
mjs | 1aec358 | 2005-12-25 09:22:35 +0000 | [diff] [blame] | 208 | return adoptRef(r); |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 209 | } |
| 210 | |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 211 | PassRefPtr<UString::Rep> UString::Rep::create(PassRefPtr<Rep> base, int offset, int length) |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 212 | { |
ggaren | ff59958 | 2007-03-07 04:25:49 +0000 | [diff] [blame] | 213 | ASSERT(base); |
mjs | 7a882e8 | 2004-04-16 18:54:21 +0000 | [diff] [blame] | 214 | |
| 215 | int baseOffset = base->offset; |
| 216 | |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 217 | base = base->baseString; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 218 | |
ggaren | 7213ee0 | 2007-10-16 23:25:33 +0000 | [diff] [blame] | 219 | ASSERT(-(offset + baseOffset) <= base->usedPreCapacity); |
| 220 | ASSERT(offset + baseOffset + length <= base->usedCapacity); |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 221 | |
| 222 | Rep *r = new Rep; |
mjs | 7a882e8 | 2004-04-16 18:54:21 +0000 | [diff] [blame] | 223 | r->offset = baseOffset + offset; |
| 224 | r->len = length; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 225 | r->rc = 1; |
| 226 | r->_hash = 0; |
ap@webkit.org | 0ea9a69 | 2008-04-18 19:46:13 +0000 | [diff] [blame] | 227 | r->identifierTable = 0; |
darin | e4d34c6 | 2006-10-29 06:48:02 +0000 | [diff] [blame] | 228 | r->baseString = base.releaseRef(); |
ap@webkit.org | 0ea9a69 | 2008-04-18 19:46:13 +0000 | [diff] [blame] | 229 | r->isStatic = false; |
kmccullough@apple.com | 9f7c99e | 2008-01-18 21:05:16 +0000 | [diff] [blame] | 230 | r->reportedCost = 0; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 231 | r->buf = 0; |
| 232 | r->usedCapacity = 0; |
| 233 | r->capacity = 0; |
mjs | 3752117 | 2004-04-19 21:26:40 +0000 | [diff] [blame] | 234 | r->usedPreCapacity = 0; |
| 235 | r->preCapacity = 0; |
mjs | 03a1087 | 2005-12-20 20:12:50 +0000 | [diff] [blame] | 236 | |
mjs | 0ee0df7 | 2005-12-08 18:19:28 +0000 | [diff] [blame] | 237 | // steal the single reference this Rep was created with |
mjs | 1aec358 | 2005-12-25 09:22:35 +0000 | [diff] [blame] | 238 | return adoptRef(r); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 239 | } |
| 240 | |
ap@webkit.org | a1251b3 | 2008-05-13 09:46:47 +0000 | [diff] [blame] | 241 | PassRefPtr<UString::Rep> UString::Rep::createFromUTF8(const char* string) |
| 242 | { |
| 243 | if (!string) |
| 244 | return &UString::Rep::null; |
| 245 | |
| 246 | size_t length = strlen(string); |
| 247 | Vector<UChar, 1024> buffer(length); |
| 248 | UChar* p = buffer.data(); |
| 249 | if (conversionOK != convertUTF8ToUTF16(&string, string + length, &p, p + length)) |
| 250 | return &UString::Rep::null; |
| 251 | |
| 252 | return UString::Rep::createCopying(buffer.data(), p - buffer.data()); |
| 253 | } |
| 254 | |
darin | 5721aa9 | 2002-11-20 01:23:02 +0000 | [diff] [blame] | 255 | void UString::Rep::destroy() |
| 256 | { |
ap@webkit.org | 11f3871 | 2008-04-07 06:33:20 +0000 | [diff] [blame] | 257 | // Static null and empty strings can never be destroyed, but we cannot rely on reference counting, because ref/deref are not thread-safe. |
| 258 | if (!isStatic) { |
ap@webkit.org | 0ea9a69 | 2008-04-18 19:46:13 +0000 | [diff] [blame] | 259 | if (identifierTable) |
ap@webkit.org | 11f3871 | 2008-04-07 06:33:20 +0000 | [diff] [blame] | 260 | Identifier::remove(this); |
| 261 | if (baseString == this) |
| 262 | fastFree(buf); |
| 263 | else |
| 264 | baseString->deref(); |
ggaren | ff59958 | 2007-03-07 04:25:49 +0000 | [diff] [blame] | 265 | |
ap@webkit.org | 11f3871 | 2008-04-07 06:33:20 +0000 | [diff] [blame] | 266 | delete this; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 267 | } |
darin | 5721aa9 | 2002-11-20 01:23:02 +0000 | [diff] [blame] | 268 | } |
| 269 | |
mjs | a518384 | 2003-03-04 01:13:26 +0000 | [diff] [blame] | 270 | // Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's |
| 271 | // or anything like that. |
| 272 | const unsigned PHI = 0x9e3779b9U; |
| 273 | |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 274 | // Paul Hsieh's SuperFastHash |
| 275 | // http://www.azillionmonkeys.com/qed/hash.html |
| 276 | unsigned UString::Rep::computeHash(const UChar *s, int len) |
darin | 5721aa9 | 2002-11-20 01:23:02 +0000 | [diff] [blame] | 277 | { |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 278 | unsigned l = len; |
| 279 | uint32_t hash = PHI; |
| 280 | uint32_t tmp; |
darin | 5721aa9 | 2002-11-20 01:23:02 +0000 | [diff] [blame] | 281 | |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 282 | int rem = l & 1; |
| 283 | l >>= 1; |
mjs | a518384 | 2003-03-04 01:13:26 +0000 | [diff] [blame] | 284 | |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 285 | // Main loop |
| 286 | for (; l > 0; l--) { |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 287 | hash += s[0]; |
| 288 | tmp = (s[1] << 11) ^ hash; |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 289 | hash = (hash << 16) ^ tmp; |
| 290 | s += 2; |
| 291 | hash += hash >> 11; |
| 292 | } |
mjs | a518384 | 2003-03-04 01:13:26 +0000 | [diff] [blame] | 293 | |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 294 | // Handle end case |
| 295 | if (rem) { |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 296 | hash += s[0]; |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 297 | hash ^= hash << 11; |
| 298 | hash += hash >> 17; |
| 299 | } |
mjs | a518384 | 2003-03-04 01:13:26 +0000 | [diff] [blame] | 300 | |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 301 | // Force "avalanching" of final 127 bits |
| 302 | hash ^= hash << 3; |
| 303 | hash += hash >> 5; |
| 304 | hash ^= hash << 2; |
| 305 | hash += hash >> 15; |
| 306 | hash ^= hash << 10; |
| 307 | |
| 308 | // this avoids ever returning a hash code of 0, since that is used to |
| 309 | // signal "hash not computed yet", using a value that is likely to be |
| 310 | // effectively the same as 0 when the low bits are masked |
| 311 | if (hash == 0) |
| 312 | hash = 0x80000000; |
| 313 | |
| 314 | return hash; |
darin | 55be989 | 2002-11-20 02:35:01 +0000 | [diff] [blame] | 315 | } |
| 316 | |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 317 | // Paul Hsieh's SuperFastHash |
| 318 | // http://www.azillionmonkeys.com/qed/hash.html |
darin | 55be989 | 2002-11-20 02:35:01 +0000 | [diff] [blame] | 319 | unsigned UString::Rep::computeHash(const char *s) |
| 320 | { |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 321 | // This hash is designed to work on 16-bit chunks at a time. But since the normal case |
| 322 | // (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they |
| 323 | // were 16-bit chunks, which should give matching results |
darin | 55be989 | 2002-11-20 02:35:01 +0000 | [diff] [blame] | 324 | |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 325 | uint32_t hash = PHI; |
| 326 | uint32_t tmp; |
darin | b847b44 | 2006-10-27 16:48:28 +0000 | [diff] [blame] | 327 | size_t l = strlen(s); |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 328 | |
darin | b847b44 | 2006-10-27 16:48:28 +0000 | [diff] [blame] | 329 | size_t rem = l & 1; |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 330 | l >>= 1; |
mjs | a518384 | 2003-03-04 01:13:26 +0000 | [diff] [blame] | 331 | |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 332 | // Main loop |
| 333 | for (; l > 0; l--) { |
| 334 | hash += (unsigned char)s[0]; |
| 335 | tmp = ((unsigned char)s[1] << 11) ^ hash; |
| 336 | hash = (hash << 16) ^ tmp; |
| 337 | s += 2; |
| 338 | hash += hash >> 11; |
| 339 | } |
mjs | a518384 | 2003-03-04 01:13:26 +0000 | [diff] [blame] | 340 | |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 341 | // Handle end case |
| 342 | if (rem) { |
| 343 | hash += (unsigned char)s[0]; |
| 344 | hash ^= hash << 11; |
| 345 | hash += hash >> 17; |
| 346 | } |
mjs | a518384 | 2003-03-04 01:13:26 +0000 | [diff] [blame] | 347 | |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 348 | // Force "avalanching" of final 127 bits |
| 349 | hash ^= hash << 3; |
| 350 | hash += hash >> 5; |
| 351 | hash ^= hash << 2; |
| 352 | hash += hash >> 15; |
| 353 | hash ^= hash << 10; |
| 354 | |
| 355 | // this avoids ever returning a hash code of 0, since that is used to |
| 356 | // signal "hash not computed yet", using a value that is likely to be |
| 357 | // effectively the same as 0 when the low bits are masked |
| 358 | if (hash == 0) |
| 359 | hash = 0x80000000; |
mjs | a518384 | 2003-03-04 01:13:26 +0000 | [diff] [blame] | 360 | |
mjs | bcd9d06 | 2005-06-28 00:02:08 +0000 | [diff] [blame] | 361 | return hash; |
darin | 5721aa9 | 2002-11-20 01:23:02 +0000 | [diff] [blame] | 362 | } |
| 363 | |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 364 | // put these early so they can be inlined |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 365 | inline size_t UString::expandedSize(size_t size, size_t otherSize) const |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 366 | { |
darin | 0d66de7 | 2007-08-08 00:45:39 +0000 | [diff] [blame] | 367 | // Do the size calculation in two parts, returning overflowIndicator if |
| 368 | // we overflow the maximum value that we can handle. |
| 369 | |
darin | 6238e2a | 2007-08-14 22:19:04 +0000 | [diff] [blame] | 370 | if (size > maxUChars()) |
| 371 | return overflowIndicator(); |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 372 | |
| 373 | size_t expandedSize = ((size + 10) / 10 * 11) + 1; |
darin | 6238e2a | 2007-08-14 22:19:04 +0000 | [diff] [blame] | 374 | if (maxUChars() - expandedSize < otherSize) |
| 375 | return overflowIndicator(); |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 376 | |
| 377 | return expandedSize + otherSize; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | inline int UString::usedCapacity() const |
| 381 | { |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 382 | return m_rep->baseString->usedCapacity; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 383 | } |
| 384 | |
mjs | 3752117 | 2004-04-19 21:26:40 +0000 | [diff] [blame] | 385 | inline int UString::usedPreCapacity() const |
| 386 | { |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 387 | return m_rep->baseString->usedPreCapacity; |
mjs | 3752117 | 2004-04-19 21:26:40 +0000 | [diff] [blame] | 388 | } |
| 389 | |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 390 | void UString::expandCapacity(int requiredLength) |
| 391 | { |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 392 | Rep* r = m_rep->baseString; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 393 | |
| 394 | if (requiredLength > r->capacity) { |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 395 | size_t newCapacity = expandedSize(requiredLength, r->preCapacity); |
| 396 | UChar* oldBuf = r->buf; |
| 397 | r->buf = reallocChars(r->buf, newCapacity); |
| 398 | if (!r->buf) { |
| 399 | r->buf = oldBuf; |
| 400 | m_rep = &Rep::null; |
| 401 | return; |
| 402 | } |
mjs | 3752117 | 2004-04-19 21:26:40 +0000 | [diff] [blame] | 403 | r->capacity = newCapacity - r->preCapacity; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 404 | } |
| 405 | if (requiredLength > r->usedCapacity) { |
| 406 | r->usedCapacity = requiredLength; |
| 407 | } |
| 408 | } |
| 409 | |
mjs | 3752117 | 2004-04-19 21:26:40 +0000 | [diff] [blame] | 410 | void UString::expandPreCapacity(int requiredPreCap) |
| 411 | { |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 412 | Rep* r = m_rep->baseString; |
mjs | 3752117 | 2004-04-19 21:26:40 +0000 | [diff] [blame] | 413 | |
| 414 | if (requiredPreCap > r->preCapacity) { |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 415 | size_t newCapacity = expandedSize(requiredPreCap, r->capacity); |
mjs | 3752117 | 2004-04-19 21:26:40 +0000 | [diff] [blame] | 416 | int delta = newCapacity - r->capacity - r->preCapacity; |
| 417 | |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 418 | UChar* newBuf = allocChars(newCapacity); |
| 419 | if (!newBuf) { |
| 420 | m_rep = &Rep::null; |
| 421 | return; |
| 422 | } |
mjs | 3752117 | 2004-04-19 21:26:40 +0000 | [diff] [blame] | 423 | memcpy(newBuf + delta, r->buf, (r->capacity + r->preCapacity) * sizeof(UChar)); |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 424 | fastFree(r->buf); |
mjs | 3752117 | 2004-04-19 21:26:40 +0000 | [diff] [blame] | 425 | r->buf = newBuf; |
| 426 | |
| 427 | r->preCapacity = newCapacity - r->capacity; |
| 428 | } |
| 429 | if (requiredPreCap > r->usedPreCapacity) { |
| 430 | r->usedPreCapacity = requiredPreCap; |
| 431 | } |
| 432 | } |
| 433 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 434 | UString::UString(const char *c) |
| 435 | { |
darin | 2dbbd42 | 2002-08-10 04:31:50 +0000 | [diff] [blame] | 436 | if (!c) { |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 437 | m_rep = &Rep::null; |
darin | 2dbbd42 | 2002-08-10 04:31:50 +0000 | [diff] [blame] | 438 | return; |
| 439 | } |
ggaren | 699f4d4 | 2007-10-27 19:20:25 +0000 | [diff] [blame] | 440 | |
| 441 | if (!c[0]) { |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 442 | m_rep = &Rep::empty; |
darin | 07f7cce | 2002-08-12 20:14:02 +0000 | [diff] [blame] | 443 | return; |
| 444 | } |
ggaren | 699f4d4 | 2007-10-27 19:20:25 +0000 | [diff] [blame] | 445 | |
| 446 | size_t length = strlen(c); |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 447 | UChar *d = allocChars(length); |
| 448 | if (!d) |
| 449 | m_rep = &Rep::null; |
| 450 | else { |
| 451 | for (size_t i = 0; i < length; i++) |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 452 | d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 453 | m_rep = Rep::create(d, static_cast<int>(length)); |
| 454 | } |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | UString::UString(const UChar *c, int length) |
| 458 | { |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 459 | if (length == 0) |
| 460 | m_rep = &Rep::empty; |
| 461 | else |
| 462 | m_rep = Rep::createCopying(c, length); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | UString::UString(UChar *c, int length, bool copy) |
| 466 | { |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 467 | if (length == 0) |
| 468 | m_rep = &Rep::empty; |
| 469 | else if (copy) |
| 470 | m_rep = Rep::createCopying(c, length); |
| 471 | else |
| 472 | m_rep = Rep::create(c, length); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 473 | } |
| 474 | |
mrowe@apple.com | f458fb7 | 2007-11-16 22:43:36 +0000 | [diff] [blame] | 475 | UString::UString(const Vector<UChar>& buffer) |
| 476 | { |
| 477 | if (!buffer.size()) |
| 478 | m_rep = &Rep::empty; |
| 479 | else |
| 480 | m_rep = Rep::createCopying(buffer.data(), buffer.size()); |
| 481 | } |
| 482 | |
| 483 | |
darin | 2dbbd42 | 2002-08-10 04:31:50 +0000 | [diff] [blame] | 484 | UString::UString(const UString &a, const UString &b) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 485 | { |
darin | 2dbbd42 | 2002-08-10 04:31:50 +0000 | [diff] [blame] | 486 | int aSize = a.size(); |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 487 | int aOffset = a.m_rep->offset; |
darin | 2dbbd42 | 2002-08-10 04:31:50 +0000 | [diff] [blame] | 488 | int bSize = b.size(); |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 489 | int bOffset = b.m_rep->offset; |
darin | 07f7cce | 2002-08-12 20:14:02 +0000 | [diff] [blame] | 490 | int length = aSize + bSize; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 491 | |
| 492 | // possible cases: |
| 493 | |
| 494 | if (aSize == 0) { |
| 495 | // a is empty |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 496 | m_rep = b.m_rep; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 497 | } else if (bSize == 0) { |
| 498 | // b is empty |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 499 | m_rep = a.m_rep; |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 500 | } else if (aOffset + aSize == a.usedCapacity() && aSize >= minShareSize && 4 * aSize >= bSize && |
darin | ae58eff | 2006-07-30 00:04:22 +0000 | [diff] [blame] | 501 | (-bOffset != b.usedPreCapacity() || aSize >= bSize)) { |
mjs | 3752117 | 2004-04-19 21:26:40 +0000 | [diff] [blame] | 502 | // - a reaches the end of its buffer so it qualifies for shared append |
| 503 | // - also, it's at least a quarter the length of b - appending to a much shorter |
| 504 | // string does more harm than good |
| 505 | // - however, if b qualifies for prepend and is longer than a, we'd rather prepend |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 506 | UString x(a); |
mjs | 7a882e8 | 2004-04-16 18:54:21 +0000 | [diff] [blame] | 507 | x.expandCapacity(aOffset + length); |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 508 | if (a.data() && x.data()) { |
aliceli1 | 96d1c4d | 2007-01-27 02:31:28 +0000 | [diff] [blame] | 509 | memcpy(const_cast<UChar *>(a.data() + aSize), b.data(), bSize * sizeof(UChar)); |
| 510 | m_rep = Rep::create(a.m_rep, 0, length); |
| 511 | } else |
| 512 | m_rep = &Rep::null; |
kmccullough@apple.com | 9f7c99e | 2008-01-18 21:05:16 +0000 | [diff] [blame] | 513 | } else if (-bOffset == b.usedPreCapacity() && bSize >= minShareSize && 4 * bSize >= aSize) { |
mjs | 3752117 | 2004-04-19 21:26:40 +0000 | [diff] [blame] | 514 | // - b reaches the beginning of its buffer so it qualifies for shared prepend |
| 515 | // - also, it's at least a quarter the length of a - prepending to a much shorter |
| 516 | // string does more harm than good |
| 517 | UString y(b); |
| 518 | y.expandPreCapacity(-bOffset + aSize); |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 519 | if (b.data() && y.data()) { |
aliceli1 | 96d1c4d | 2007-01-27 02:31:28 +0000 | [diff] [blame] | 520 | memcpy(const_cast<UChar *>(b.data() - aSize), a.data(), aSize * sizeof(UChar)); |
| 521 | m_rep = Rep::create(b.m_rep, -aSize, length); |
| 522 | } else |
| 523 | m_rep = &Rep::null; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 524 | } else { |
mjs | 3752117 | 2004-04-19 21:26:40 +0000 | [diff] [blame] | 525 | // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 526 | size_t newCapacity = expandedSize(length, 0); |
| 527 | UChar* d = allocChars(newCapacity); |
| 528 | if (!d) |
| 529 | m_rep = &Rep::null; |
| 530 | else { |
aliceli1 | 96d1c4d | 2007-01-27 02:31:28 +0000 | [diff] [blame] | 531 | memcpy(d, a.data(), aSize * sizeof(UChar)); |
| 532 | memcpy(d + aSize, b.data(), bSize * sizeof(UChar)); |
| 533 | m_rep = Rep::create(d, length); |
| 534 | m_rep->capacity = newCapacity; |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 535 | } |
darin | 07f7cce | 2002-08-12 20:14:02 +0000 | [diff] [blame] | 536 | } |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 537 | } |
| 538 | |
ggaren | ff59958 | 2007-03-07 04:25:49 +0000 | [diff] [blame] | 539 | const UString& UString::null() |
darin | 8aede13 | 2003-04-29 18:26:29 +0000 | [diff] [blame] | 540 | { |
ap@webkit.org | 11f3871 | 2008-04-07 06:33:20 +0000 | [diff] [blame] | 541 | static UString* n = new UString; // Should be called from main thread at least once to be safely initialized. |
ggaren | ff59958 | 2007-03-07 04:25:49 +0000 | [diff] [blame] | 542 | return *n; |
darin | 8aede13 | 2003-04-29 18:26:29 +0000 | [diff] [blame] | 543 | } |
| 544 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 545 | UString UString::from(int i) |
| 546 | { |
adele | 704e8bb | 2005-09-16 22:42:30 +0000 | [diff] [blame] | 547 | UChar buf[1 + sizeof(i) * 3]; |
| 548 | UChar *end = buf + sizeof(buf) / sizeof(UChar); |
| 549 | UChar *p = end; |
| 550 | |
| 551 | if (i == 0) { |
| 552 | *--p = '0'; |
| 553 | } else if (i == INT_MIN) { |
| 554 | char minBuf[1 + sizeof(i) * 3]; |
| 555 | sprintf(minBuf, "%d", INT_MIN); |
| 556 | return UString(minBuf); |
| 557 | } else { |
| 558 | bool negative = false; |
| 559 | if (i < 0) { |
| 560 | negative = true; |
| 561 | i = -i; |
| 562 | } |
| 563 | while (i) { |
| 564 | *--p = (unsigned short)((i % 10) + '0'); |
| 565 | i /= 10; |
| 566 | } |
| 567 | if (negative) { |
| 568 | *--p = '-'; |
| 569 | } |
| 570 | } |
| 571 | |
darin | b847b44 | 2006-10-27 16:48:28 +0000 | [diff] [blame] | 572 | return UString(p, static_cast<int>(end - p)); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | UString UString::from(unsigned int u) |
| 576 | { |
adele | 704e8bb | 2005-09-16 22:42:30 +0000 | [diff] [blame] | 577 | UChar buf[sizeof(u) * 3]; |
| 578 | UChar *end = buf + sizeof(buf) / sizeof(UChar); |
darin | ff56afa0 | 2002-11-19 22:25:11 +0000 | [diff] [blame] | 579 | UChar *p = end; |
darin | 451d435 | 2002-11-19 08:10:03 +0000 | [diff] [blame] | 580 | |
| 581 | if (u == 0) { |
| 582 | *--p = '0'; |
| 583 | } else { |
| 584 | while (u) { |
| 585 | *--p = (unsigned short)((u % 10) + '0'); |
| 586 | u /= 10; |
| 587 | } |
| 588 | } |
| 589 | |
darin | b847b44 | 2006-10-27 16:48:28 +0000 | [diff] [blame] | 590 | return UString(p, static_cast<int>(end - p)); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 591 | } |
| 592 | |
mjs | da18fd0 | 2002-08-15 12:02:07 +0000 | [diff] [blame] | 593 | UString UString::from(long l) |
| 594 | { |
adele | 704e8bb | 2005-09-16 22:42:30 +0000 | [diff] [blame] | 595 | UChar buf[1 + sizeof(l) * 3]; |
| 596 | UChar *end = buf + sizeof(buf) / sizeof(UChar); |
darin | ff56afa0 | 2002-11-19 22:25:11 +0000 | [diff] [blame] | 597 | UChar *p = end; |
darin | 451d435 | 2002-11-19 08:10:03 +0000 | [diff] [blame] | 598 | |
| 599 | if (l == 0) { |
| 600 | *--p = '0'; |
| 601 | } else if (l == LONG_MIN) { |
adele | 704e8bb | 2005-09-16 22:42:30 +0000 | [diff] [blame] | 602 | char minBuf[1 + sizeof(l) * 3]; |
darin | 451d435 | 2002-11-19 08:10:03 +0000 | [diff] [blame] | 603 | sprintf(minBuf, "%ld", LONG_MIN); |
| 604 | return UString(minBuf); |
| 605 | } else { |
| 606 | bool negative = false; |
| 607 | if (l < 0) { |
| 608 | negative = true; |
| 609 | l = -l; |
| 610 | } |
| 611 | while (l) { |
| 612 | *--p = (unsigned short)((l % 10) + '0'); |
| 613 | l /= 10; |
| 614 | } |
| 615 | if (negative) { |
| 616 | *--p = '-'; |
| 617 | } |
| 618 | } |
| 619 | |
darin | b847b44 | 2006-10-27 16:48:28 +0000 | [diff] [blame] | 620 | return UString(p, static_cast<int>(end - p)); |
mjs | da18fd0 | 2002-08-15 12:02:07 +0000 | [diff] [blame] | 621 | } |
| 622 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 623 | UString UString::from(double d) |
| 624 | { |
mjs | f8fa72e | 2006-03-17 22:03:39 +0000 | [diff] [blame] | 625 | // avoid ever printing -NaN, in JS conceptually there is only one NaN value |
mrowe@apple.com | 915de55 | 2007-10-29 02:52:04 +0000 | [diff] [blame] | 626 | if (isnan(d)) |
mjs | f8fa72e | 2006-03-17 22:03:39 +0000 | [diff] [blame] | 627 | return "NaN"; |
| 628 | |
mjs | 7c33c6f | 2002-12-03 22:51:39 +0000 | [diff] [blame] | 629 | char buf[80]; |
| 630 | int decimalPoint; |
| 631 | int sign; |
mjs | f273a9f | 2002-12-15 03:33:10 +0000 | [diff] [blame] | 632 | |
weinig@apple.com | 843df6f3 | 2008-04-16 18:41:54 +0000 | [diff] [blame] | 633 | char *result = dtoa(d, 0, &decimalPoint, &sign, NULL); |
darin | b847b44 | 2006-10-27 16:48:28 +0000 | [diff] [blame] | 634 | int length = static_cast<int>(strlen(result)); |
mjs | 7c33c6f | 2002-12-03 22:51:39 +0000 | [diff] [blame] | 635 | |
| 636 | int i = 0; |
| 637 | if (sign) { |
| 638 | buf[i++] = '-'; |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 639 | } |
mjs | 7c33c6f | 2002-12-03 22:51:39 +0000 | [diff] [blame] | 640 | |
mjs | f273a9f | 2002-12-15 03:33:10 +0000 | [diff] [blame] | 641 | if (decimalPoint <= 0 && decimalPoint > -6) { |
mjs | 957a1b3 | 2002-12-15 00:37:56 +0000 | [diff] [blame] | 642 | buf[i++] = '0'; |
mjs | 7c33c6f | 2002-12-03 22:51:39 +0000 | [diff] [blame] | 643 | buf[i++] = '.'; |
mjs | 957a1b3 | 2002-12-15 00:37:56 +0000 | [diff] [blame] | 644 | for (int j = decimalPoint; j < 0; j++) { |
| 645 | buf[i++] = '0'; |
| 646 | } |
mjs | 7c33c6f | 2002-12-03 22:51:39 +0000 | [diff] [blame] | 647 | strcpy(buf + i, result); |
mjs | f273a9f | 2002-12-15 03:33:10 +0000 | [diff] [blame] | 648 | } else if (decimalPoint <= 21 && decimalPoint > 0) { |
| 649 | if (length <= decimalPoint) { |
| 650 | strcpy(buf + i, result); |
| 651 | i += length; |
| 652 | for (int j = 0; j < decimalPoint - length; j++) { |
darin | ae58eff | 2006-07-30 00:04:22 +0000 | [diff] [blame] | 653 | buf[i++] = '0'; |
mjs | f273a9f | 2002-12-15 03:33:10 +0000 | [diff] [blame] | 654 | } |
| 655 | buf[i] = '\0'; |
| 656 | } else { |
| 657 | strncpy(buf + i, result, decimalPoint); |
| 658 | i += decimalPoint; |
| 659 | buf[i++] = '.'; |
| 660 | strcpy(buf + i, result + decimalPoint); |
| 661 | } |
| 662 | } else if (result[0] < '0' || result[0] > '9') { |
mjs | 7c33c6f | 2002-12-03 22:51:39 +0000 | [diff] [blame] | 663 | strcpy(buf + i, result); |
| 664 | } else { |
mjs | f273a9f | 2002-12-15 03:33:10 +0000 | [diff] [blame] | 665 | buf[i++] = result[0]; |
| 666 | if (length > 1) { |
| 667 | buf[i++] = '.'; |
| 668 | strcpy(buf + i, result + 1); |
| 669 | i += length - 1; |
| 670 | } |
| 671 | |
| 672 | buf[i++] = 'e'; |
| 673 | buf[i++] = (decimalPoint >= 0) ? '+' : '-'; |
| 674 | // decimalPoint can't be more than 3 digits decimal given the |
| 675 | // nature of float representation |
| 676 | int exponential = decimalPoint - 1; |
darin | b847b44 | 2006-10-27 16:48:28 +0000 | [diff] [blame] | 677 | if (exponential < 0) |
| 678 | exponential = -exponential; |
| 679 | if (exponential >= 100) |
| 680 | buf[i++] = static_cast<char>('0' + exponential / 100); |
| 681 | if (exponential >= 10) |
| 682 | buf[i++] = static_cast<char>('0' + (exponential % 100) / 10); |
| 683 | buf[i++] = static_cast<char>('0' + exponential % 10); |
mjs | f273a9f | 2002-12-15 03:33:10 +0000 | [diff] [blame] | 684 | buf[i++] = '\0'; |
mjs | 7c33c6f | 2002-12-03 22:51:39 +0000 | [diff] [blame] | 685 | } |
weinig@apple.com | 843df6f3 | 2008-04-16 18:41:54 +0000 | [diff] [blame] | 686 | |
| 687 | freedtoa(result); |
| 688 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 689 | return UString(buf); |
| 690 | } |
| 691 | |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 692 | UString UString::spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const |
mjs | 5d90e06 | 2004-09-15 00:16:34 +0000 | [diff] [blame] | 693 | { |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 694 | if (rangeCount == 1 && separatorCount == 0) { |
| 695 | int thisSize = size(); |
| 696 | int position = substringRanges[0].position; |
| 697 | int length = substringRanges[0].length; |
| 698 | if (position <= 0 && length >= thisSize) |
| 699 | return *this; |
| 700 | return UString::Rep::create(m_rep, max(0, position), min(thisSize, length)); |
| 701 | } |
| 702 | |
mjs | 5d90e06 | 2004-09-15 00:16:34 +0000 | [diff] [blame] | 703 | int totalLength = 0; |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 704 | for (int i = 0; i < rangeCount; i++) |
mjs | 5d90e06 | 2004-09-15 00:16:34 +0000 | [diff] [blame] | 705 | totalLength += substringRanges[i].length; |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 706 | for (int i = 0; i < separatorCount; i++) |
mjs | 5d90e06 | 2004-09-15 00:16:34 +0000 | [diff] [blame] | 707 | totalLength += separators[i].size(); |
mjs | 5d90e06 | 2004-09-15 00:16:34 +0000 | [diff] [blame] | 708 | |
darin | 0d66de7 | 2007-08-08 00:45:39 +0000 | [diff] [blame] | 709 | if (totalLength == 0) |
| 710 | return ""; |
| 711 | |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 712 | UChar* buffer = allocChars(totalLength); |
| 713 | if (!buffer) |
| 714 | return null(); |
mjs | 5d90e06 | 2004-09-15 00:16:34 +0000 | [diff] [blame] | 715 | |
darin | d589360 | 2005-08-01 05:02:13 +0000 | [diff] [blame] | 716 | int maxCount = max(rangeCount, separatorCount); |
mjs | 5d90e06 | 2004-09-15 00:16:34 +0000 | [diff] [blame] | 717 | int bufferPos = 0; |
| 718 | for (int i = 0; i < maxCount; i++) { |
| 719 | if (i < rangeCount) { |
| 720 | memcpy(buffer + bufferPos, data() + substringRanges[i].position, substringRanges[i].length * sizeof(UChar)); |
| 721 | bufferPos += substringRanges[i].length; |
| 722 | } |
| 723 | if (i < separatorCount) { |
| 724 | memcpy(buffer + bufferPos, separators[i].data(), separators[i].size() * sizeof(UChar)); |
| 725 | bufferPos += separators[i].size(); |
| 726 | } |
| 727 | } |
| 728 | |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 729 | return UString::Rep::create(buffer, totalLength); |
mjs | 5d90e06 | 2004-09-15 00:16:34 +0000 | [diff] [blame] | 730 | } |
| 731 | |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 732 | UString& UString::append(const UString &t) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 733 | { |
mjs | 7a882e8 | 2004-04-16 18:54:21 +0000 | [diff] [blame] | 734 | int thisSize = size(); |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 735 | int thisOffset = m_rep->offset; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 736 | int tSize = t.size(); |
| 737 | int length = thisSize + tSize; |
| 738 | |
| 739 | // possible cases: |
| 740 | if (thisSize == 0) { |
| 741 | // this is empty |
| 742 | *this = t; |
| 743 | } else if (tSize == 0) { |
| 744 | // t is empty |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 745 | } else if (m_rep->baseIsSelf() && m_rep->rc == 1) { |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 746 | // this is direct and has refcount of 1 (so we can just alter it directly) |
mjs | 7a882e8 | 2004-04-16 18:54:21 +0000 | [diff] [blame] | 747 | expandCapacity(thisOffset + length); |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 748 | if (data()) { |
| 749 | memcpy(const_cast<UChar*>(data() + thisSize), t.data(), tSize * sizeof(UChar)); |
| 750 | m_rep->len = length; |
| 751 | m_rep->_hash = 0; |
| 752 | } |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 753 | } else if (thisOffset + thisSize == usedCapacity() && thisSize >= minShareSize) { |
| 754 | // this reaches the end of the buffer - extend it if it's long enough to append to |
darin | 08f6b3a | 2004-08-10 03:05:37 +0000 | [diff] [blame] | 755 | expandCapacity(thisOffset + length); |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 756 | if (data()) { |
| 757 | memcpy(const_cast<UChar*>(data() + thisSize), t.data(), tSize * sizeof(UChar)); |
| 758 | m_rep = Rep::create(m_rep, 0, length); |
| 759 | } |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 760 | } else { |
| 761 | // this is shared with someone using more capacity, gotta make a whole new string |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 762 | size_t newCapacity = expandedSize(length, 0); |
| 763 | UChar* d = allocChars(newCapacity); |
| 764 | if (!d) |
| 765 | m_rep = &Rep::null; |
| 766 | else { |
| 767 | memcpy(d, data(), thisSize * sizeof(UChar)); |
| 768 | memcpy(const_cast<UChar*>(d + thisSize), t.data(), tSize * sizeof(UChar)); |
| 769 | m_rep = Rep::create(d, length); |
| 770 | m_rep->capacity = newCapacity; |
| 771 | } |
darin | 0c2dc3f | 2002-06-04 22:33:26 +0000 | [diff] [blame] | 772 | } |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 773 | |
| 774 | return *this; |
| 775 | } |
| 776 | |
darin@apple.com | 59c4d4e | 2008-06-27 02:53:42 +0000 | [diff] [blame^] | 777 | UString& UString::append(const UChar* tData, int tSize) |
| 778 | { |
| 779 | int thisSize = size(); |
| 780 | int thisOffset = m_rep->offset; |
| 781 | int length = thisSize + tSize; |
| 782 | |
| 783 | // possible cases: |
| 784 | if (tSize == 0) { |
| 785 | // t is empty |
| 786 | } else if (thisSize == 0) { |
| 787 | // this is empty |
| 788 | m_rep = Rep::createCopying(tData, tSize); |
| 789 | } else if (m_rep->baseIsSelf() && m_rep->rc == 1) { |
| 790 | // this is direct and has refcount of 1 (so we can just alter it directly) |
| 791 | expandCapacity(thisOffset + length); |
| 792 | if (data()) { |
| 793 | memcpy(const_cast<UChar*>(data() + thisSize), tData, tSize * sizeof(UChar)); |
| 794 | m_rep->len = length; |
| 795 | m_rep->_hash = 0; |
| 796 | } |
| 797 | } else if (thisOffset + thisSize == usedCapacity() && thisSize >= minShareSize) { |
| 798 | // this reaches the end of the buffer - extend it if it's long enough to append to |
| 799 | expandCapacity(thisOffset + length); |
| 800 | if (data()) { |
| 801 | memcpy(const_cast<UChar*>(data() + thisSize), tData, tSize * sizeof(UChar)); |
| 802 | m_rep = Rep::create(m_rep, 0, length); |
| 803 | } |
| 804 | } else { |
| 805 | // this is shared with someone using more capacity, gotta make a whole new string |
| 806 | size_t newCapacity = expandedSize(length, 0); |
| 807 | UChar* d = allocChars(newCapacity); |
| 808 | if (!d) |
| 809 | m_rep = &Rep::null; |
| 810 | else { |
| 811 | memcpy(d, data(), thisSize * sizeof(UChar)); |
| 812 | memcpy(const_cast<UChar*>(d + thisSize), tData, tSize * sizeof(UChar)); |
| 813 | m_rep = Rep::create(d, length); |
| 814 | m_rep->capacity = newCapacity; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | return *this; |
| 819 | } |
| 820 | |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 821 | UString& UString::append(const char *t) |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 822 | { |
mjs | 7a882e8 | 2004-04-16 18:54:21 +0000 | [diff] [blame] | 823 | int thisSize = size(); |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 824 | int thisOffset = m_rep->offset; |
darin | b847b44 | 2006-10-27 16:48:28 +0000 | [diff] [blame] | 825 | int tSize = static_cast<int>(strlen(t)); |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 826 | int length = thisSize + tSize; |
| 827 | |
| 828 | // possible cases: |
| 829 | if (thisSize == 0) { |
| 830 | // this is empty |
| 831 | *this = t; |
| 832 | } else if (tSize == 0) { |
| 833 | // t is empty, we'll just return *this below. |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 834 | } else if (m_rep->baseIsSelf() && m_rep->rc == 1) { |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 835 | // this is direct and has refcount of 1 (so we can just alter it directly) |
mjs | 7a882e8 | 2004-04-16 18:54:21 +0000 | [diff] [blame] | 836 | expandCapacity(thisOffset + length); |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 837 | UChar *d = const_cast<UChar *>(data()); |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 838 | if (d) { |
| 839 | for (int i = 0; i < tSize; ++i) |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 840 | d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 841 | m_rep->len = length; |
| 842 | m_rep->_hash = 0; |
| 843 | } |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 844 | } else if (thisOffset + thisSize == usedCapacity() && thisSize >= minShareSize) { |
mjs | 7a882e8 | 2004-04-16 18:54:21 +0000 | [diff] [blame] | 845 | // this string reaches the end of the buffer - extend it |
| 846 | expandCapacity(thisOffset + length); |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 847 | UChar *d = const_cast<UChar *>(data()); |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 848 | if (d) { |
| 849 | for (int i = 0; i < tSize; ++i) |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 850 | d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 851 | m_rep = Rep::create(m_rep, 0, length); |
| 852 | } |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 853 | } else { |
| 854 | // this is shared with someone using more capacity, gotta make a whole new string |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 855 | size_t newCapacity = expandedSize(length, 0); |
| 856 | UChar* d = allocChars(newCapacity); |
| 857 | if (!d) |
| 858 | m_rep = &Rep::null; |
| 859 | else { |
| 860 | memcpy(d, data(), thisSize * sizeof(UChar)); |
| 861 | for (int i = 0; i < tSize; ++i) |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 862 | d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 863 | m_rep = Rep::create(d, length); |
| 864 | m_rep->capacity = newCapacity; |
| 865 | } |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 866 | } |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 867 | |
| 868 | return *this; |
| 869 | } |
| 870 | |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 871 | UString& UString::append(UChar c) |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 872 | { |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 873 | int thisOffset = m_rep->offset; |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 874 | int length = size(); |
| 875 | |
| 876 | // possible cases: |
| 877 | if (length == 0) { |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 878 | // this is empty - must make a new m_rep because we don't want to pollute the shared empty one |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 879 | size_t newCapacity = expandedSize(1, 0); |
| 880 | UChar* d = allocChars(newCapacity); |
| 881 | if (!d) |
| 882 | m_rep = &Rep::null; |
| 883 | else { |
| 884 | d[0] = c; |
| 885 | m_rep = Rep::create(d, 1); |
| 886 | m_rep->capacity = newCapacity; |
| 887 | } |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 888 | } else if (m_rep->baseIsSelf() && m_rep->rc == 1) { |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 889 | // this is direct and has refcount of 1 (so we can just alter it directly) |
mjs | 7a882e8 | 2004-04-16 18:54:21 +0000 | [diff] [blame] | 890 | expandCapacity(thisOffset + length + 1); |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 891 | UChar *d = const_cast<UChar *>(data()); |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 892 | if (d) { |
| 893 | d[length] = c; |
| 894 | m_rep->len = length + 1; |
| 895 | m_rep->_hash = 0; |
| 896 | } |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 897 | } else if (thisOffset + length == usedCapacity() && length >= minShareSize) { |
mjs | 7a882e8 | 2004-04-16 18:54:21 +0000 | [diff] [blame] | 898 | // this reaches the end of the string - extend it and share |
| 899 | expandCapacity(thisOffset + length + 1); |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 900 | UChar *d = const_cast<UChar *>(data()); |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 901 | if (d) { |
| 902 | d[length] = c; |
| 903 | m_rep = Rep::create(m_rep, 0, length + 1); |
| 904 | } |
mjs | f9c3795 | 2004-04-14 02:33:59 +0000 | [diff] [blame] | 905 | } else { |
| 906 | // this is shared with someone using more capacity, gotta make a whole new string |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 907 | size_t newCapacity = expandedSize(length + 1, 0); |
| 908 | UChar* d = allocChars(newCapacity); |
| 909 | if (!d) |
| 910 | m_rep = &Rep::null; |
| 911 | else { |
| 912 | memcpy(d, data(), length * sizeof(UChar)); |
| 913 | d[length] = c; |
| 914 | m_rep = Rep::create(d, length + 1); |
| 915 | m_rep->capacity = newCapacity; |
| 916 | } |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 917 | } |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 918 | |
| 919 | return *this; |
| 920 | } |
| 921 | |
darin@apple.com | 3beb300 | 2008-04-28 05:59:07 +0000 | [diff] [blame] | 922 | bool UString::getCString(CStringBuffer& buffer) const |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 923 | { |
darin@apple.com | 3beb300 | 2008-04-28 05:59:07 +0000 | [diff] [blame] | 924 | int length = size(); |
| 925 | int neededSize = length + 1; |
| 926 | buffer.resize(neededSize); |
| 927 | char* buf = buffer.data(); |
ap@webkit.org | 11f3871 | 2008-04-07 06:33:20 +0000 | [diff] [blame] | 928 | |
darin@apple.com | 3beb300 | 2008-04-28 05:59:07 +0000 | [diff] [blame] | 929 | UChar ored = 0; |
| 930 | const UChar* p = data(); |
| 931 | char* q = buf; |
| 932 | const UChar* limit = p + length; |
| 933 | while (p != limit) { |
| 934 | UChar c = p[0]; |
| 935 | ored |= c; |
| 936 | *q = static_cast<char>(c); |
| 937 | ++p; |
| 938 | ++q; |
| 939 | } |
| 940 | *q = '\0'; |
ap@webkit.org | 11f3871 | 2008-04-07 06:33:20 +0000 | [diff] [blame] | 941 | |
darin@apple.com | 3beb300 | 2008-04-28 05:59:07 +0000 | [diff] [blame] | 942 | return !(ored & 0xFF00); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | char *UString::ascii() const |
| 946 | { |
darin | 2dbbd42 | 2002-08-10 04:31:50 +0000 | [diff] [blame] | 947 | int length = size(); |
| 948 | int neededSize = length + 1; |
ap@webkit.org | 11f3871 | 2008-04-07 06:33:20 +0000 | [diff] [blame] | 949 | delete[] statBuffer; |
| 950 | statBuffer = new char[neededSize]; |
darin | 2dbbd42 | 2002-08-10 04:31:50 +0000 | [diff] [blame] | 951 | |
| 952 | const UChar *p = data(); |
| 953 | char *q = statBuffer; |
| 954 | const UChar *limit = p + length; |
| 955 | while (p != limit) { |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 956 | *q = static_cast<char>(p[0]); |
darin | 2dbbd42 | 2002-08-10 04:31:50 +0000 | [diff] [blame] | 957 | ++p; |
| 958 | ++q; |
| 959 | } |
| 960 | *q = '\0'; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 961 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 962 | return statBuffer; |
| 963 | } |
| 964 | |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 965 | UString& UString::operator=(const char *c) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 966 | { |
darin | 0d66de7 | 2007-08-08 00:45:39 +0000 | [diff] [blame] | 967 | if (!c) { |
| 968 | m_rep = &Rep::null; |
| 969 | return *this; |
| 970 | } |
| 971 | |
ggaren | 699f4d4 | 2007-10-27 19:20:25 +0000 | [diff] [blame] | 972 | if (!c[0]) { |
darin | 0d66de7 | 2007-08-08 00:45:39 +0000 | [diff] [blame] | 973 | m_rep = &Rep::empty; |
| 974 | return *this; |
| 975 | } |
| 976 | |
ggaren | 699f4d4 | 2007-10-27 19:20:25 +0000 | [diff] [blame] | 977 | int l = static_cast<int>(strlen(c)); |
darin | 0c2dc3f | 2002-06-04 22:33:26 +0000 | [diff] [blame] | 978 | UChar *d; |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 979 | if (m_rep->rc == 1 && l <= m_rep->capacity && m_rep->baseIsSelf() && m_rep->offset == 0 && m_rep->preCapacity == 0) { |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 980 | d = m_rep->buf; |
| 981 | m_rep->_hash = 0; |
mjs | 0753376 | 2007-07-26 01:50:54 +0000 | [diff] [blame] | 982 | m_rep->len = l; |
darin | 0c2dc3f | 2002-06-04 22:33:26 +0000 | [diff] [blame] | 983 | } else { |
bdash | 41705de | 2007-08-02 09:33:22 +0000 | [diff] [blame] | 984 | d = allocChars(l); |
| 985 | if (!d) { |
| 986 | m_rep = &Rep::null; |
| 987 | return *this; |
| 988 | } |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 989 | m_rep = Rep::create(d, l); |
darin | 0c2dc3f | 2002-06-04 22:33:26 +0000 | [diff] [blame] | 990 | } |
| 991 | for (int i = 0; i < l; i++) |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 992 | d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 993 | |
| 994 | return *this; |
| 995 | } |
| 996 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 997 | bool UString::is8Bit() const |
| 998 | { |
| 999 | const UChar *u = data(); |
darin | 2dbbd42 | 2002-08-10 04:31:50 +0000 | [diff] [blame] | 1000 | const UChar *limit = u + size(); |
| 1001 | while (u < limit) { |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 1002 | if (u[0] > 0xFF) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1003 | return false; |
darin | 2dbbd42 | 2002-08-10 04:31:50 +0000 | [diff] [blame] | 1004 | ++u; |
| 1005 | } |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1006 | |
| 1007 | return true; |
| 1008 | } |
| 1009 | |
mrowe@apple.com | 59de0d0 | 2008-04-19 01:31:29 +0000 | [diff] [blame] | 1010 | UChar UString::operator[](int pos) const |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1011 | { |
| 1012 | if (pos >= size()) |
darin | 8aede13 | 2003-04-29 18:26:29 +0000 | [diff] [blame] | 1013 | return '\0'; |
| 1014 | return data()[pos]; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
darin | 0a2c7b8 | 2003-12-11 04:55:12 +0000 | [diff] [blame] | 1017 | double UString::toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1018 | { |
| 1019 | double d; |
| 1020 | |
darin | 0a2c7b8 | 2003-12-11 04:55:12 +0000 | [diff] [blame] | 1021 | // FIXME: If tolerateTrailingJunk is true, then we want to tolerate non-8-bit junk |
darin@apple.com | 3beb300 | 2008-04-28 05:59:07 +0000 | [diff] [blame] | 1022 | // after the number, so this is too strict a check. |
| 1023 | CStringBuffer s; |
| 1024 | if (!getCString(s)) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1025 | return NaN; |
darin@apple.com | 3beb300 | 2008-04-28 05:59:07 +0000 | [diff] [blame] | 1026 | const char* c = s.data(); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1027 | |
| 1028 | // skip leading white space |
darin | eb0d86d | 2007-10-16 20:13:24 +0000 | [diff] [blame] | 1029 | while (isASCIISpace(*c)) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1030 | c++; |
| 1031 | |
| 1032 | // empty string ? |
| 1033 | if (*c == '\0') |
darin | 0a2c7b8 | 2003-12-11 04:55:12 +0000 | [diff] [blame] | 1034 | return tolerateEmptyString ? 0.0 : NaN; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1035 | |
| 1036 | // hex number ? |
| 1037 | if (*c == '0' && (*(c+1) == 'x' || *(c+1) == 'X')) { |
darin | 5769f31 | 2007-07-18 02:25:38 +0000 | [diff] [blame] | 1038 | const char* firstDigitPosition = c + 2; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1039 | c++; |
| 1040 | d = 0.0; |
| 1041 | while (*(++c)) { |
| 1042 | if (*c >= '0' && *c <= '9') |
darin | ae58eff | 2006-07-30 00:04:22 +0000 | [diff] [blame] | 1043 | d = d * 16.0 + *c - '0'; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1044 | else if ((*c >= 'A' && *c <= 'F') || (*c >= 'a' && *c <= 'f')) |
darin | ae58eff | 2006-07-30 00:04:22 +0000 | [diff] [blame] | 1045 | d = d * 16.0 + (*c & 0xdf) - 'A' + 10.0; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1046 | else |
darin | ae58eff | 2006-07-30 00:04:22 +0000 | [diff] [blame] | 1047 | break; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1048 | } |
darin | 5769f31 | 2007-07-18 02:25:38 +0000 | [diff] [blame] | 1049 | |
| 1050 | if (d >= mantissaOverflowLowerBound) |
| 1051 | d = parseIntOverflow(firstDigitPosition, c - firstDigitPosition, 16); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1052 | } else { |
| 1053 | // regular number ? |
| 1054 | char *end; |
weinig@apple.com | 843df6f3 | 2008-04-16 18:41:54 +0000 | [diff] [blame] | 1055 | d = strtod(c, &end); |
darin | 5769f31 | 2007-07-18 02:25:38 +0000 | [diff] [blame] | 1056 | if ((d != 0.0 || end != c) && d != Inf && d != -Inf) { |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1057 | c = end; |
| 1058 | } else { |
darin | 5769f31 | 2007-07-18 02:25:38 +0000 | [diff] [blame] | 1059 | double sign = 1.0; |
| 1060 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1061 | if (*c == '+') |
darin | ae58eff | 2006-07-30 00:04:22 +0000 | [diff] [blame] | 1062 | c++; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1063 | else if (*c == '-') { |
darin | 5769f31 | 2007-07-18 02:25:38 +0000 | [diff] [blame] | 1064 | sign = -1.0; |
darin | ae58eff | 2006-07-30 00:04:22 +0000 | [diff] [blame] | 1065 | c++; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1066 | } |
darin | 5769f31 | 2007-07-18 02:25:38 +0000 | [diff] [blame] | 1067 | |
| 1068 | // We used strtod() to do the conversion. However, strtod() handles |
| 1069 | // infinite values slightly differently than JavaScript in that it |
| 1070 | // converts the string "inf" with any capitalization to infinity, |
| 1071 | // whereas the ECMA spec requires that it be converted to NaN. |
| 1072 | |
ggaren@apple.com | 2cffc2c | 2007-11-13 08:05:03 +0000 | [diff] [blame] | 1073 | if (c[0] == 'I' && c[1] == 'n' && c[2] == 'f' && c[3] == 'i' && c[4] == 'n' && c[5] == 'i' && c[6] == 't' && c[7] == 'y') { |
darin | 5769f31 | 2007-07-18 02:25:38 +0000 | [diff] [blame] | 1074 | d = sign * Inf; |
| 1075 | c += 8; |
| 1076 | } else if ((d == Inf || d == -Inf) && *c != 'I' && *c != 'i') |
| 1077 | c = end; |
| 1078 | else |
darin | ae58eff | 2006-07-30 00:04:22 +0000 | [diff] [blame] | 1079 | return NaN; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | // allow trailing white space |
darin | eb0d86d | 2007-10-16 20:13:24 +0000 | [diff] [blame] | 1084 | while (isASCIISpace(*c)) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1085 | c++; |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 1086 | // don't allow anything after - unless tolerant=true |
darin | 0a2c7b8 | 2003-12-11 04:55:12 +0000 | [diff] [blame] | 1087 | if (!tolerateTrailingJunk && *c != '\0') |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1088 | d = NaN; |
| 1089 | |
| 1090 | return d; |
| 1091 | } |
| 1092 | |
darin | 0a2c7b8 | 2003-12-11 04:55:12 +0000 | [diff] [blame] | 1093 | double UString::toDouble(bool tolerateTrailingJunk) const |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1094 | { |
darin | 0a2c7b8 | 2003-12-11 04:55:12 +0000 | [diff] [blame] | 1095 | return toDouble(tolerateTrailingJunk, true); |
| 1096 | } |
| 1097 | |
| 1098 | double UString::toDouble() const |
| 1099 | { |
| 1100 | return toDouble(false, true); |
| 1101 | } |
| 1102 | |
adele | 704e8bb | 2005-09-16 22:42:30 +0000 | [diff] [blame] | 1103 | uint32_t UString::toUInt32(bool *ok) const |
darin | 0a2c7b8 | 2003-12-11 04:55:12 +0000 | [diff] [blame] | 1104 | { |
adele | 704e8bb | 2005-09-16 22:42:30 +0000 | [diff] [blame] | 1105 | double d = toDouble(); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1106 | bool b = true; |
| 1107 | |
ggaren | f52a5ed | 2005-10-06 01:13:18 +0000 | [diff] [blame] | 1108 | if (d != static_cast<uint32_t>(d)) { |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1109 | b = false; |
| 1110 | d = 0; |
| 1111 | } |
| 1112 | |
| 1113 | if (ok) |
| 1114 | *ok = b; |
| 1115 | |
adele | 704e8bb | 2005-09-16 22:42:30 +0000 | [diff] [blame] | 1116 | return static_cast<uint32_t>(d); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
adele | 704e8bb | 2005-09-16 22:42:30 +0000 | [diff] [blame] | 1119 | uint32_t UString::toUInt32(bool *ok, bool tolerateEmptyString) const |
darin | 0a2c7b8 | 2003-12-11 04:55:12 +0000 | [diff] [blame] | 1120 | { |
adele | 704e8bb | 2005-09-16 22:42:30 +0000 | [diff] [blame] | 1121 | double d = toDouble(false, tolerateEmptyString); |
mjs | 1a9f113 | 2002-11-24 07:49:26 +0000 | [diff] [blame] | 1122 | bool b = true; |
| 1123 | |
ggaren | f52a5ed | 2005-10-06 01:13:18 +0000 | [diff] [blame] | 1124 | if (d != static_cast<uint32_t>(d)) { |
mjs | 1a9f113 | 2002-11-24 07:49:26 +0000 | [diff] [blame] | 1125 | b = false; |
| 1126 | d = 0; |
| 1127 | } |
| 1128 | |
| 1129 | if (ok) |
| 1130 | *ok = b; |
| 1131 | |
| 1132 | return static_cast<uint32_t>(d); |
| 1133 | } |
| 1134 | |
darin | 7fdc25c | 2003-07-12 16:01:36 +0000 | [diff] [blame] | 1135 | uint32_t UString::toStrictUInt32(bool *ok) const |
| 1136 | { |
| 1137 | if (ok) |
| 1138 | *ok = false; |
| 1139 | |
| 1140 | // Empty string is not OK. |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 1141 | int len = m_rep->len; |
darin | 7fdc25c | 2003-07-12 16:01:36 +0000 | [diff] [blame] | 1142 | if (len == 0) |
| 1143 | return 0; |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 1144 | const UChar *p = m_rep->data(); |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 1145 | unsigned short c = p[0]; |
darin | 7fdc25c | 2003-07-12 16:01:36 +0000 | [diff] [blame] | 1146 | |
| 1147 | // If the first digit is 0, only 0 itself is OK. |
| 1148 | if (c == '0') { |
| 1149 | if (len == 1 && ok) |
| 1150 | *ok = true; |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | // Convert to UInt32, checking for overflow. |
| 1155 | uint32_t i = 0; |
| 1156 | while (1) { |
| 1157 | // Process character, turning it into a digit. |
| 1158 | if (c < '0' || c > '9') |
| 1159 | return 0; |
| 1160 | const unsigned d = c - '0'; |
| 1161 | |
darin | 08e4b21 | 2003-07-13 21:40:45 +0000 | [diff] [blame] | 1162 | // Multiply by 10, checking for overflow out of 32 bits. |
| 1163 | if (i > 0xFFFFFFFFU / 10) |
darin | 7fdc25c | 2003-07-12 16:01:36 +0000 | [diff] [blame] | 1164 | return 0; |
darin | 7fdc25c | 2003-07-12 16:01:36 +0000 | [diff] [blame] | 1165 | i *= 10; |
darin | 08e4b21 | 2003-07-13 21:40:45 +0000 | [diff] [blame] | 1166 | |
| 1167 | // Add in the digit, checking for overflow out of 32 bits. |
| 1168 | const unsigned max = 0xFFFFFFFFU - d; |
| 1169 | if (i > max) |
| 1170 | return 0; |
darin | 7fdc25c | 2003-07-12 16:01:36 +0000 | [diff] [blame] | 1171 | i += d; |
| 1172 | |
| 1173 | // Handle end of string. |
| 1174 | if (--len == 0) { |
| 1175 | if (ok) |
| 1176 | *ok = true; |
| 1177 | return i; |
| 1178 | } |
| 1179 | |
| 1180 | // Get next character. |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 1181 | c = *(++p); |
darin | 7fdc25c | 2003-07-12 16:01:36 +0000 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1185 | int UString::find(const UString &f, int pos) const |
| 1186 | { |
darin | fe98588 | 2002-08-21 05:23:31 +0000 | [diff] [blame] | 1187 | int sz = size(); |
| 1188 | int fsz = f.size(); |
| 1189 | if (sz < fsz) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1190 | return -1; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1191 | if (pos < 0) |
| 1192 | pos = 0; |
darin | c2ded48 | 2002-10-11 04:07:07 +0000 | [diff] [blame] | 1193 | if (fsz == 0) |
| 1194 | return pos; |
darin | fe98588 | 2002-08-21 05:23:31 +0000 | [diff] [blame] | 1195 | const UChar *end = data() + sz - fsz; |
adele | 704e8bb | 2005-09-16 22:42:30 +0000 | [diff] [blame] | 1196 | int fsizeminusone = (fsz - 1) * sizeof(UChar); |
darin | fe98588 | 2002-08-21 05:23:31 +0000 | [diff] [blame] | 1197 | const UChar *fdata = f.data(); |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 1198 | unsigned short fchar = fdata[0]; |
darin | 140be37 | 2005-04-20 10:14:35 +0000 | [diff] [blame] | 1199 | ++fdata; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1200 | for (const UChar *c = data() + pos; c <= end; c++) |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 1201 | if (c[0] == fchar && !memcmp(c + 1, fdata, fsizeminusone)) |
darin | b847b44 | 2006-10-27 16:48:28 +0000 | [diff] [blame] | 1202 | return static_cast<int>(c - data()); |
darin | 07f7cce | 2002-08-12 20:14:02 +0000 | [diff] [blame] | 1203 | |
| 1204 | return -1; |
| 1205 | } |
| 1206 | |
| 1207 | int UString::find(UChar ch, int pos) const |
| 1208 | { |
| 1209 | if (pos < 0) |
| 1210 | pos = 0; |
| 1211 | const UChar *end = data() + size(); |
| 1212 | for (const UChar *c = data() + pos; c < end; c++) |
| 1213 | if (*c == ch) |
darin | b847b44 | 2006-10-27 16:48:28 +0000 | [diff] [blame] | 1214 | return static_cast<int>(c - data()); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1215 | |
| 1216 | return -1; |
| 1217 | } |
| 1218 | |
| 1219 | int UString::rfind(const UString &f, int pos) const |
| 1220 | { |
darin | fe98588 | 2002-08-21 05:23:31 +0000 | [diff] [blame] | 1221 | int sz = size(); |
| 1222 | int fsz = f.size(); |
| 1223 | if (sz < fsz) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1224 | return -1; |
darin | fe98588 | 2002-08-21 05:23:31 +0000 | [diff] [blame] | 1225 | if (pos < 0) |
| 1226 | pos = 0; |
darin | c2ded48 | 2002-10-11 04:07:07 +0000 | [diff] [blame] | 1227 | if (pos > sz - fsz) |
| 1228 | pos = sz - fsz; |
| 1229 | if (fsz == 0) |
| 1230 | return pos; |
adele | 704e8bb | 2005-09-16 22:42:30 +0000 | [diff] [blame] | 1231 | int fsizeminusone = (fsz - 1) * sizeof(UChar); |
darin | fe98588 | 2002-08-21 05:23:31 +0000 | [diff] [blame] | 1232 | const UChar *fdata = f.data(); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1233 | for (const UChar *c = data() + pos; c >= data(); c--) { |
darin | fe98588 | 2002-08-21 05:23:31 +0000 | [diff] [blame] | 1234 | if (*c == *fdata && !memcmp(c + 1, fdata + 1, fsizeminusone)) |
darin | b847b44 | 2006-10-27 16:48:28 +0000 | [diff] [blame] | 1235 | return static_cast<int>(c - data()); |
darin | 07f7cce | 2002-08-12 20:14:02 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | return -1; |
| 1239 | } |
| 1240 | |
| 1241 | int UString::rfind(UChar ch, int pos) const |
| 1242 | { |
| 1243 | if (isEmpty()) |
| 1244 | return -1; |
| 1245 | if (pos + 1 >= size()) |
| 1246 | pos = size() - 1; |
| 1247 | for (const UChar *c = data() + pos; c >= data(); c--) { |
| 1248 | if (*c == ch) |
darin | b847b44 | 2006-10-27 16:48:28 +0000 | [diff] [blame] | 1249 | return static_cast<int>(c-data()); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | return -1; |
| 1253 | } |
| 1254 | |
| 1255 | UString UString::substr(int pos, int len) const |
| 1256 | { |
darin | bceb27f | 2004-08-12 17:21:29 +0000 | [diff] [blame] | 1257 | int s = size(); |
| 1258 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1259 | if (pos < 0) |
| 1260 | pos = 0; |
darin | bceb27f | 2004-08-12 17:21:29 +0000 | [diff] [blame] | 1261 | else if (pos >= s) |
| 1262 | pos = s; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1263 | if (len < 0) |
darin | bceb27f | 2004-08-12 17:21:29 +0000 | [diff] [blame] | 1264 | len = s; |
| 1265 | if (pos + len >= s) |
| 1266 | len = s - pos; |
| 1267 | |
| 1268 | if (pos == 0 && len == s) |
| 1269 | return *this; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1270 | |
mjs | 1aec358 | 2005-12-25 09:22:35 +0000 | [diff] [blame] | 1271 | return UString(Rep::create(m_rep, pos, len)); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
darin | 04b93fa | 2005-05-16 18:57:29 +0000 | [diff] [blame] | 1274 | bool operator==(const UString& s1, const UString& s2) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1275 | { |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 1276 | if (s1.m_rep->len != s2.m_rep->len) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1277 | return false; |
| 1278 | |
mjs | 97e2b18 | 2005-12-06 09:21:15 +0000 | [diff] [blame] | 1279 | return (memcmp(s1.m_rep->data(), s2.m_rep->data(), |
darin | ae58eff | 2006-07-30 00:04:22 +0000 | [diff] [blame] | 1280 | s1.m_rep->len * sizeof(UChar)) == 0); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
darin | 04b93fa | 2005-05-16 18:57:29 +0000 | [diff] [blame] | 1283 | bool operator==(const UString& s1, const char *s2) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1284 | { |
darin | 07f7cce | 2002-08-12 20:14:02 +0000 | [diff] [blame] | 1285 | if (s2 == 0) { |
| 1286 | return s1.isEmpty(); |
| 1287 | } |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1288 | |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1289 | const UChar *u = s1.data(); |
darin | fe98588 | 2002-08-21 05:23:31 +0000 | [diff] [blame] | 1290 | const UChar *uend = u + s1.size(); |
| 1291 | while (u != uend && *s2) { |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 1292 | if (u[0] != (unsigned char)*s2) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1293 | return false; |
| 1294 | s2++; |
| 1295 | u++; |
| 1296 | } |
| 1297 | |
darin | fe98588 | 2002-08-21 05:23:31 +0000 | [diff] [blame] | 1298 | return u == uend && *s2 == 0; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
darin | 04b93fa | 2005-05-16 18:57:29 +0000 | [diff] [blame] | 1301 | bool operator<(const UString& s1, const UString& s2) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1302 | { |
darin | b70665a | 2002-04-15 23:43:21 +0000 | [diff] [blame] | 1303 | const int l1 = s1.size(); |
| 1304 | const int l2 = s2.size(); |
| 1305 | const int lmin = l1 < l2 ? l1 : l2; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1306 | const UChar *c1 = s1.data(); |
| 1307 | const UChar *c2 = s2.data(); |
| 1308 | int l = 0; |
darin | b70665a | 2002-04-15 23:43:21 +0000 | [diff] [blame] | 1309 | while (l < lmin && *c1 == *c2) { |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1310 | c1++; |
| 1311 | c2++; |
| 1312 | l++; |
| 1313 | } |
darin | b70665a | 2002-04-15 23:43:21 +0000 | [diff] [blame] | 1314 | if (l < lmin) |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 1315 | return (c1[0] < c2[0]); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1316 | |
darin | b70665a | 2002-04-15 23:43:21 +0000 | [diff] [blame] | 1317 | return (l1 < l2); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1318 | } |
darin | 401641e | 2002-11-18 21:55:23 +0000 | [diff] [blame] | 1319 | |
kmccullough@apple.com | c74bed1 | 2008-05-20 20:59:16 +0000 | [diff] [blame] | 1320 | bool operator>(const UString& s1, const UString& s2) |
| 1321 | { |
| 1322 | const int l1 = s1.size(); |
| 1323 | const int l2 = s2.size(); |
| 1324 | const int lmin = l1 < l2 ? l1 : l2; |
| 1325 | const UChar *c1 = s1.data(); |
| 1326 | const UChar *c2 = s2.data(); |
| 1327 | int l = 0; |
| 1328 | while (l < lmin && *c1 == *c2) { |
| 1329 | c1++; |
| 1330 | c2++; |
| 1331 | l++; |
| 1332 | } |
| 1333 | if (l < lmin) |
| 1334 | return (c1[0] > c2[0]); |
| 1335 | |
| 1336 | return (l1 > l2); |
| 1337 | } |
| 1338 | |
darin | 04b93fa | 2005-05-16 18:57:29 +0000 | [diff] [blame] | 1339 | int compare(const UString& s1, const UString& s2) |
darin | 401641e | 2002-11-18 21:55:23 +0000 | [diff] [blame] | 1340 | { |
| 1341 | const int l1 = s1.size(); |
| 1342 | const int l2 = s2.size(); |
| 1343 | const int lmin = l1 < l2 ? l1 : l2; |
| 1344 | const UChar *c1 = s1.data(); |
| 1345 | const UChar *c2 = s2.data(); |
| 1346 | int l = 0; |
| 1347 | while (l < lmin && *c1 == *c2) { |
| 1348 | c1++; |
| 1349 | c2++; |
| 1350 | l++; |
| 1351 | } |
ggaren | 39a1c9a | 2006-03-15 01:09:15 +0000 | [diff] [blame] | 1352 | |
darin | 401641e | 2002-11-18 21:55:23 +0000 | [diff] [blame] | 1353 | if (l < lmin) |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 1354 | return (c1[0] > c2[0]) ? 1 : -1; |
darin | 401641e | 2002-11-18 21:55:23 +0000 | [diff] [blame] | 1355 | |
ggaren | 39a1c9a | 2006-03-15 01:09:15 +0000 | [diff] [blame] | 1356 | if (l1 == l2) |
darin | 401641e | 2002-11-18 21:55:23 +0000 | [diff] [blame] | 1357 | return 0; |
ggaren | 39a1c9a | 2006-03-15 01:09:15 +0000 | [diff] [blame] | 1358 | |
| 1359 | return (l1 > l2) ? 1 : -1; |
darin | 401641e | 2002-11-18 21:55:23 +0000 | [diff] [blame] | 1360 | } |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 1361 | |
ap@webkit.org | faaccdb | 2008-06-04 18:10:15 +0000 | [diff] [blame] | 1362 | bool equal(const UString::Rep* r, const UString::Rep* b) |
| 1363 | { |
| 1364 | int length = r->len; |
| 1365 | if (length != b->len) |
| 1366 | return false; |
| 1367 | const UChar* d = r->data(); |
| 1368 | const UChar* s = b->data(); |
| 1369 | for (int i = 0; i != length; ++i) |
| 1370 | if (d[i] != s[i]) |
| 1371 | return false; |
| 1372 | return true; |
| 1373 | } |
| 1374 | |
ap@webkit.org | f263e7d | 2007-11-13 07:12:55 +0000 | [diff] [blame] | 1375 | CString UString::UTF8String(bool strict) const |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 1376 | { |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 1377 | // Allocate a buffer big enough to hold all the characters. |
| 1378 | const int length = size(); |
mjs | a09640f | 2006-01-29 04:12:59 +0000 | [diff] [blame] | 1379 | Vector<char, 1024> buffer(length * 3); |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 1380 | |
| 1381 | // Convert to runs of 8-bit characters. |
ap@webkit.org | f263e7d | 2007-11-13 07:12:55 +0000 | [diff] [blame] | 1382 | char* p = buffer.data(); |
eric@webkit.org | 87d855b | 2008-03-10 22:06:44 +0000 | [diff] [blame] | 1383 | const UChar* d = reinterpret_cast<const UChar*>(&data()[0]); |
ap@webkit.org | d3895dc | 2007-11-15 05:59:40 +0000 | [diff] [blame] | 1384 | ConversionResult result = convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), strict); |
ap@webkit.org | f263e7d | 2007-11-13 07:12:55 +0000 | [diff] [blame] | 1385 | if (result != conversionOK) |
| 1386 | return CString(); |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 1387 | |
ap@webkit.org | f263e7d | 2007-11-13 07:12:55 +0000 | [diff] [blame] | 1388 | return CString(buffer.data(), p - buffer.data()); |
darin@apple.com | 11f3b27 | 2007-11-03 16:40:32 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
darin | 6c9bbfd | 2003-08-18 18:51:25 +0000 | [diff] [blame] | 1391 | } // namespace KJS |