1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Communicator client code, released
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef jsstrinlines_h___
41 #define jsstrinlines_h___
44 #include "jscntxtinlines.h"
49 CheckStringLength(JSContext
*cx
, size_t length
)
51 if (JS_UNLIKELY(length
> JSString::MAX_LENGTH
)) {
52 js_ReportAllocationOverflow(cx
);
60 * String builder that eagerly checks for over-allocation past the maximum
63 * Note: over-allocation is not checked for when using the infallible
64 * |replaceRawBuffer|, so the implementation of |finishString| also must check
65 * for over-allocation.
69 typedef Vector
<jschar
, 32> CharBuffer
;
72 static inline bool checkLength(JSContext
*cx
, size_t length
);
73 inline bool checkLength(size_t length
);
74 JSContext
*context() const { return cb
.allocPolicy().context(); }
77 explicit inline StringBuffer(JSContext
*cx
);
78 bool reserve(size_t len
);
79 bool resize(size_t len
);
80 bool append(const jschar c
);
81 bool append(const jschar
*chars
, size_t len
);
82 bool append(const jschar
*begin
, const jschar
*end
);
83 bool append(JSString
*str
);
84 bool append(JSAtom
*atom
);
85 bool appendN(const jschar c
, size_t n
);
86 bool appendInflated(const char *cstr
, size_t len
);
87 JSAtom
*atomize(uintN flags
= 0);
88 static JSAtom
*atomize(JSContext
*cx
, const CharBuffer
&cb
, uintN flags
= 0);
89 static JSAtom
*atomize(JSContext
*cx
, const jschar
*begin
, size_t length
, uintN flags
= 0);
91 void replaceRawBuffer(jschar
*chars
, size_t len
) { cb
.replaceRawBuffer(chars
, len
); }
92 jschar
*begin() { return cb
.begin(); }
93 jschar
*end() { return cb
.end(); }
94 const jschar
*begin() const { return cb
.begin(); }
95 const jschar
*end() const { return cb
.end(); }
96 bool empty() const { return cb
.empty(); }
97 inline jsint
length() const;
100 * Produces a string, resetting the buffer to an empty state.
101 * This method takes responsibility for adding the terminating '\0'
102 * required by js_NewString.
104 JSFlatString
*finishString();
106 template <size_t ArrayLength
>
107 bool append(const char (&array
)[ArrayLength
]) {
108 return cb
.append(array
, array
+ ArrayLength
- 1); /* No trailing '\0'. */
113 StringBuffer::StringBuffer(JSContext
*cx
)
118 StringBuffer::reserve(size_t len
)
120 if (!checkLength(len
))
122 return cb
.reserve(len
);
126 StringBuffer::resize(size_t len
)
128 if (!checkLength(len
))
130 return cb
.resize(len
);
134 StringBuffer::append(const jschar c
)
136 if (!checkLength(cb
.length() + 1))
142 StringBuffer::append(const jschar
*chars
, size_t len
)
144 if (!checkLength(cb
.length() + len
))
146 return cb
.append(chars
, len
);
150 StringBuffer::append(const jschar
*begin
, const jschar
*end
)
152 if (!checkLength(cb
.length() + (end
- begin
)))
154 return cb
.append(begin
, end
);
158 StringBuffer::append(JSString
*str
)
160 JSLinearString
*linear
= str
->ensureLinear(context());
161 size_t strLen
= linear
->length();
162 if (!checkLength(cb
.length() + strLen
))
164 return cb
.append(linear
->chars(), strLen
);
168 StringBuffer::append(JSAtom
*atom
)
170 size_t strLen
= atom
->length();
171 if (!checkLength(cb
.length() + strLen
))
173 return cb
.append(atom
->chars(), strLen
);
177 StringBuffer::appendN(const jschar c
, size_t n
)
179 if (!checkLength(cb
.length() + n
))
181 return cb
.appendN(c
, n
);
185 StringBuffer::appendInflated(const char *cstr
, size_t cstrlen
)
187 size_t lengthBefore
= length();
188 if (!cb
.growByUninitialized(cstrlen
))
191 size_t oldcstrlen
= cstrlen
;
194 js_InflateStringToBuffer(context(), cstr
, cstrlen
, begin() + lengthBefore
, &cstrlen
);
195 JS_ASSERT(ok
&& oldcstrlen
== cstrlen
);
200 StringBuffer::length() const
202 JS_STATIC_ASSERT(jsint(JSString::MAX_LENGTH
) == JSString::MAX_LENGTH
);
203 JS_ASSERT(cb
.length() <= JSString::MAX_LENGTH
);
204 return jsint(cb
.length());
208 StringBuffer::checkLength(size_t length
)
210 return CheckStringLength(context(), length
);
215 inline JSFlatString
*
216 JSString::unitString(jschar c
)
218 JS_ASSERT(c
< UNIT_STRING_LIMIT
);
219 return const_cast<JSString
*>(&unitStringTable
[c
])->assertIsFlat();
222 inline JSLinearString
*
223 JSString::getUnitString(JSContext
*cx
, JSString
*str
, size_t index
)
225 JS_ASSERT(index
< str
->length());
226 const jschar
*chars
= str
->getChars(cx
);
229 jschar c
= chars
[index
];
230 if (c
< UNIT_STRING_LIMIT
)
231 return unitString(c
);
232 return js_NewDependentString(cx
, str
, index
, 1);
235 inline JSFlatString
*
236 JSString::length2String(jschar c1
, jschar c2
)
238 JS_ASSERT(fitsInSmallChar(c1
));
239 JS_ASSERT(fitsInSmallChar(c2
));
240 return const_cast<JSString
*> (
241 &length2StringTable
[(((size_t)toSmallChar
[c1
]) << 6) + toSmallChar
[c2
]]
245 inline JSFlatString
*
246 JSString::length2String(uint32 i
)
249 return length2String('0' + i
/ 10, '0' + i
% 10);
252 inline JSFlatString
*
253 JSString::intString(jsint i
)
255 jsuint u
= jsuint(i
);
256 JS_ASSERT(u
< INT_STRING_LIMIT
);
257 return const_cast<JSString
*>(JSString::intStringTable
[u
])->assertIsFlat();
260 /* Get a static atomized string for chars if possible. */
261 inline JSFlatString
*
262 JSString::lookupStaticString(const jschar
*chars
, size_t length
)
265 if (chars
[0] < UNIT_STRING_LIMIT
)
266 return unitString(chars
[0]);
270 if (fitsInSmallChar(chars
[0]) && fitsInSmallChar(chars
[1]))
271 return length2String(chars
[0], chars
[1]);
275 * Here we know that JSString::intStringTable covers only 256 (or at least
276 * not 1000 or more) chars. We rely on order here to resolve the unit vs.
277 * int string/length-2 string atom identity issue by giving priority to unit
278 * strings for "0" through "9" and length-2 strings for "10" through "99".
280 JS_STATIC_ASSERT(INT_STRING_LIMIT
<= 999);
282 if ('1' <= chars
[0] && chars
[0] <= '9' &&
283 '0' <= chars
[1] && chars
[1] <= '9' &&
284 '0' <= chars
[2] && chars
[2] <= '9') {
285 jsint i
= (chars
[0] - '0') * 100 +
286 (chars
[1] - '0') * 10 +
289 if (jsuint(i
) < INT_STRING_LIMIT
)
298 JSString::finalize(JSContext
*cx
) {
299 JS_ASSERT(!JSString::isStatic(this));
300 JS_RUNTIME_UNMETER(cx
->runtime
, liveStrings
);
302 JS_RUNTIME_UNMETER(cx
->runtime
, liveDependentStrings
);
303 } else if (isFlat()) {
305 * flatChars for stillborn string is null, but cx->free checks
306 * for a null pointer on its own.
308 cx
->runtime
->stringMemoryUsed
-= length() * 2;
309 cx
->free(const_cast<jschar
*>(flatChars()));
314 JSShortString::finalize(JSContext
*cx
)
316 JS_ASSERT(!JSString::isStatic(&mHeader
));
317 JS_ASSERT(mHeader
.isFlat());
318 JS_RUNTIME_UNMETER(cx
->runtime
, liveStrings
);
322 JSExternalString::finalize(JSContext
*cx
)
324 JS_ASSERT(unsigned(externalStringType
) < JS_ARRAY_LENGTH(str_finalizers
));
325 JS_ASSERT(!isStatic(this));
327 JS_RUNTIME_UNMETER(cx
->runtime
, liveStrings
);
329 /* A stillborn string has null chars. */
330 jschar
*chars
= const_cast<jschar
*>(flatChars());
333 JSStringFinalizeOp finalizer
= str_finalizers
[externalStringType
];
339 JSExternalString::finalize()
341 JS_ASSERT(unsigned(externalStringType
) < JS_ARRAY_LENGTH(str_finalizers
));
342 JSStringFinalizeOp finalizer
= str_finalizers
[externalStringType
];
345 * Assume that the finalizer for the permanently interned
346 * string knows how to deal with null context.
348 finalizer(NULL
, this);
359 RopeBuilder(JSContext
*cx
)
360 : cx(cx
), res(cx
->runtime
->emptyString
)
363 inline bool append(JSString
*str
) {
364 res
= js_ConcatStrings(cx
, res
, str
);
368 inline JSString
*result() {
373 class StringSegmentRange
376 * If malloc() shows up in any profiles from this vector, we can add a new
377 * StackAllocPolicy which stashes a reusable freed-at-gc buffer in the cx.
379 Vector
<JSString
*, 32> stack
;
382 bool settle(JSString
*str
) {
383 while (str
->isRope()) {
384 if (!stack
.append(str
->ropeRight()))
386 str
= str
->ropeLeft();
393 StringSegmentRange(JSContext
*cx
)
394 : stack(cx
), cur(NULL
)
397 JS_WARN_UNUSED_RESULT
bool init(JSString
*str
) {
398 JS_ASSERT(stack
.empty());
406 JSString
*front() const {
407 JS_ASSERT(!cur
->isRope());
411 JS_WARN_UNUSED_RESULT
bool popFront() {
417 return settle(stack
.popCopy());
423 #endif /* jsstrinlines_h___ */