~ubuntu-branches/ubuntu/wily/mozjs17/wily

« back to all changes in this revision

Viewing changes to js/src/jsstrinlines.h

  • Committer: Package Import Robot
  • Author(s): Rico Tzschichholz
  • Date: 2013-05-25 12:24:23 UTC
  • Revision ID: package-import@ubuntu.com-20130525122423-zmxucrhtensw90xy
Tags: upstream-17.0.0
ImportĀ upstreamĀ versionĀ 17.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
 *
 
3
 * This Source Code Form is subject to the terms of the Mozilla Public
 
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
6
 
 
7
#ifndef jsstrinlines_h___
 
8
#define jsstrinlines_h___
 
9
 
 
10
#include "mozilla/Attributes.h"
 
11
 
 
12
#include "jsatom.h"
 
13
#include "jsstr.h"
 
14
 
 
15
#include "jscntxtinlines.h"
 
16
#include "jsgcinlines.h"
 
17
#include "vm/String-inl.h"
 
18
 
 
19
namespace js {
 
20
 
 
21
class RopeBuilder {
 
22
    JSContext *cx;
 
23
    RootedString res;
 
24
 
 
25
    RopeBuilder(const RopeBuilder &other) MOZ_DELETE;
 
26
    void operator=(const RopeBuilder &other) MOZ_DELETE;
 
27
 
 
28
  public:
 
29
    RopeBuilder(JSContext *cx)
 
30
      : cx(cx), res(cx, cx->runtime->emptyString)
 
31
    {}
 
32
 
 
33
    inline bool append(HandleString str) {
 
34
        res = js_ConcatStrings(cx, res, str);
 
35
        return !!res;
 
36
    }
 
37
 
 
38
    inline JSString *result() {
 
39
        return res;
 
40
    }
 
41
};
 
42
 
 
43
class StringSegmentRange
 
44
{
 
45
    /*
 
46
     * If malloc() shows up in any profiles from this vector, we can add a new
 
47
     * StackAllocPolicy which stashes a reusable freed-at-gc buffer in the cx.
 
48
     */
 
49
    AutoStringVector stack;
 
50
    Rooted<JSLinearString*> cur;
 
51
 
 
52
    bool settle(JSString *str) {
 
53
        while (str->isRope()) {
 
54
            JSRope &rope = str->asRope();
 
55
            if (!stack.append(rope.rightChild()))
 
56
                return false;
 
57
            str = rope.leftChild();
 
58
        }
 
59
        cur = &str->asLinear();
 
60
        return true;
 
61
    }
 
62
 
 
63
  public:
 
64
    StringSegmentRange(JSContext *cx)
 
65
      : stack(cx), cur(cx)
 
66
    {}
 
67
 
 
68
    JS_WARN_UNUSED_RESULT bool init(JSString *str) {
 
69
        JS_ASSERT(stack.empty());
 
70
        return settle(str);
 
71
    }
 
72
 
 
73
    bool empty() const {
 
74
        return cur == NULL;
 
75
    }
 
76
 
 
77
    JSLinearString *front() const {
 
78
        JS_ASSERT(!cur->isRope());
 
79
        return cur;
 
80
    }
 
81
 
 
82
    JS_WARN_UNUSED_RESULT bool popFront() {
 
83
        JS_ASSERT(!empty());
 
84
        if (stack.empty()) {
 
85
            cur = NULL;
 
86
            return true;
 
87
        }
 
88
        return settle(stack.popCopy());
 
89
    }
 
90
};
 
91
 
 
92
/*
 
93
 * Return s advanced past any Unicode white space characters.
 
94
 */
 
95
static inline const jschar *
 
96
SkipSpace(const jschar *s, const jschar *end)
 
97
{
 
98
    JS_ASSERT(s <= end);
 
99
 
 
100
    while (s < end && unicode::IsSpace(*s))
 
101
        s++;
 
102
 
 
103
    return s;
 
104
}
 
105
 
 
106
/*
 
107
 * Return less than, equal to, or greater than zero depending on whether
 
108
 * s1 is less than, equal to, or greater than s2.
 
109
 */
 
110
inline bool
 
111
CompareChars(const jschar *s1, size_t l1, const jschar *s2, size_t l2, int32_t *result)
 
112
{
 
113
    size_t n = Min(l1, l2);
 
114
    for (size_t i = 0; i < n; i++) {
 
115
        if (int32_t cmp = s1[i] - s2[i]) {
 
116
            *result = cmp;
 
117
            return true;
 
118
        }
 
119
    }
 
120
 
 
121
    *result = (int32_t)(l1 - l2);
 
122
    return true;
 
123
}
 
124
 
 
125
}  /* namespace js */
 
126
 
 
127
#endif /* jsstrinlines_h___ */