1 : /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
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/
10 : *
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
14 : * License.
15 : *
16 : * The Original Code is Mozilla Communicator client code, released
17 : * March 31, 1998.
18 : *
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.
23 : *
24 : * Contributor(s):
25 : *
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.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef jsstrinlines_h___
41 : #define jsstrinlines_h___
42 :
43 : #include "mozilla/Attributes.h"
44 :
45 : #include "jsatom.h"
46 : #include "jsstr.h"
47 :
48 : #include "jscntxtinlines.h"
49 : #include "jsgcinlines.h"
50 : #include "vm/String-inl.h"
51 :
52 : namespace js {
53 :
54 : class RopeBuilder {
55 : JSContext *cx;
56 : JSString *res;
57 :
58 : RopeBuilder(const RopeBuilder &other) MOZ_DELETE;
59 : void operator=(const RopeBuilder &other) MOZ_DELETE;
60 :
61 : public:
62 13398 : RopeBuilder(JSContext *cx)
63 13398 : : cx(cx), res(cx->runtime->emptyString)
64 13398 : {}
65 :
66 40194 : inline bool append(JSString *str) {
67 40194 : res = js_ConcatStrings(cx, res, str);
68 40194 : return !!res;
69 : }
70 :
71 13398 : inline JSString *result() {
72 13398 : return res;
73 : }
74 : };
75 :
76 : class StringSegmentRange
77 456567 : {
78 : /*
79 : * If malloc() shows up in any profiles from this vector, we can add a new
80 : * StackAllocPolicy which stashes a reusable freed-at-gc buffer in the cx.
81 : */
82 : Vector<JSString *, 32> stack;
83 : JSLinearString *cur;
84 :
85 36315564 : bool settle(JSString *str) {
86 108523776 : while (str->isRope()) {
87 35892648 : JSRope &rope = str->asRope();
88 35892648 : if (!stack.append(rope.rightChild()))
89 0 : return false;
90 35892648 : str = rope.leftChild();
91 : }
92 36315564 : cur = &str->asLinear();
93 36315564 : return true;
94 : }
95 :
96 : public:
97 456567 : StringSegmentRange(JSContext *cx)
98 456567 : : stack(cx), cur(NULL)
99 456567 : {}
100 :
101 456567 : JS_WARN_UNUSED_RESULT bool init(JSString *str) {
102 456567 : JS_ASSERT(stack.empty());
103 456567 : return settle(str);
104 : }
105 :
106 73041297 : bool empty() const {
107 73041297 : return cur == NULL;
108 : }
109 :
110 36292365 : JSLinearString *front() const {
111 36292365 : JS_ASSERT(!cur->isRope());
112 36292365 : return cur;
113 : }
114 :
115 36292365 : JS_WARN_UNUSED_RESULT bool popFront() {
116 36292365 : JS_ASSERT(!empty());
117 36292365 : if (stack.empty()) {
118 433368 : cur = NULL;
119 433368 : return true;
120 : }
121 35858997 : return settle(stack.popCopy());
122 : }
123 : };
124 :
125 : /*
126 : * Return s advanced past any Unicode white space characters.
127 : */
128 : static inline const jschar *
129 2013370 : SkipSpace(const jschar *s, const jschar *end)
130 : {
131 2013370 : JS_ASSERT(s <= end);
132 :
133 4026884 : while (s < end && unicode::IsSpace(*s))
134 144 : s++;
135 :
136 2013370 : return s;
137 : }
138 :
139 : /*
140 : * Return less than, equal to, or greater than zero depending on whether
141 : * s1 is less than, equal to, or greater than s2.
142 : */
143 : inline bool
144 341792 : CompareChars(const jschar *s1, size_t l1, const jschar *s2, size_t l2, int32_t *result)
145 : {
146 341792 : size_t n = JS_MIN(l1, l2);
147 655404 : for (size_t i = 0; i < n; i++) {
148 644636 : if (int32_t cmp = s1[i] - s2[i]) {
149 331024 : *result = cmp;
150 331024 : return true;
151 : }
152 : }
153 :
154 10768 : *result = (int32_t)(l1 - l2);
155 10768 : return true;
156 : }
157 :
158 : } /* namespace js */
159 :
160 : #endif /* jsstrinlines_h___ */
|