Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef vm_StringBuffer_h |
michael@0 | 8 | #define vm_StringBuffer_h |
michael@0 | 9 | |
michael@0 | 10 | #include "jscntxt.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "js/Vector.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace js { |
michael@0 | 15 | |
michael@0 | 16 | /* |
michael@0 | 17 | * String builder that eagerly checks for over-allocation past the maximum |
michael@0 | 18 | * string length. |
michael@0 | 19 | * |
michael@0 | 20 | * Any operation which would exceed the maximum string length causes an |
michael@0 | 21 | * exception report on the context and results in a failed return value. |
michael@0 | 22 | * |
michael@0 | 23 | * Well-sized extractions (which waste no more than 1/4 of their char |
michael@0 | 24 | * buffer space) are guaranteed for strings built by this interface. |
michael@0 | 25 | * See |extractWellSized|. |
michael@0 | 26 | */ |
michael@0 | 27 | class StringBuffer |
michael@0 | 28 | { |
michael@0 | 29 | /* cb's buffer is taken by the new string so use ContextAllocPolicy. */ |
michael@0 | 30 | typedef Vector<jschar, 32, ContextAllocPolicy> CharBuffer; |
michael@0 | 31 | |
michael@0 | 32 | CharBuffer cb; |
michael@0 | 33 | |
michael@0 | 34 | ExclusiveContext *context() const { |
michael@0 | 35 | return cb.allocPolicy().context()->asExclusiveContext(); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | StringBuffer(const StringBuffer &other) MOZ_DELETE; |
michael@0 | 39 | void operator=(const StringBuffer &other) MOZ_DELETE; |
michael@0 | 40 | |
michael@0 | 41 | public: |
michael@0 | 42 | explicit StringBuffer(ExclusiveContext *cx) : cb(cx) { } |
michael@0 | 43 | |
michael@0 | 44 | inline bool reserve(size_t len) { return cb.reserve(len); } |
michael@0 | 45 | inline bool resize(size_t len) { return cb.resize(len); } |
michael@0 | 46 | inline bool append(const jschar c) { return cb.append(c); } |
michael@0 | 47 | inline bool append(const jschar *chars, size_t len) { return cb.append(chars, len); } |
michael@0 | 48 | inline bool append(const JS::ConstCharPtr chars, size_t len) { return cb.append(chars.get(), len); } |
michael@0 | 49 | inline bool append(const jschar *begin, const jschar *end) { return cb.append(begin, end); } |
michael@0 | 50 | inline bool append(JSString *str); |
michael@0 | 51 | inline bool append(JSLinearString *str); |
michael@0 | 52 | inline bool appendN(const jschar c, size_t n) { return cb.appendN(c, n); } |
michael@0 | 53 | inline bool appendInflated(const char *cstr, size_t len); |
michael@0 | 54 | |
michael@0 | 55 | template <size_t ArrayLength> |
michael@0 | 56 | bool append(const char (&array)[ArrayLength]) { |
michael@0 | 57 | return cb.append(array, array + ArrayLength - 1); /* No trailing '\0'. */ |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | /* Infallible variants usable when the corresponding space is reserved. */ |
michael@0 | 61 | void infallibleAppend(const jschar c) { |
michael@0 | 62 | cb.infallibleAppend(c); |
michael@0 | 63 | } |
michael@0 | 64 | void infallibleAppend(const jschar *chars, size_t len) { |
michael@0 | 65 | cb.infallibleAppend(chars, len); |
michael@0 | 66 | } |
michael@0 | 67 | void infallibleAppend(const JS::ConstCharPtr chars, size_t len) { |
michael@0 | 68 | cb.infallibleAppend(chars.get(), len); |
michael@0 | 69 | } |
michael@0 | 70 | void infallibleAppend(const jschar *begin, const jschar *end) { |
michael@0 | 71 | cb.infallibleAppend(begin, end); |
michael@0 | 72 | } |
michael@0 | 73 | void infallibleAppendN(const jschar c, size_t n) { |
michael@0 | 74 | cb.infallibleAppendN(c, n); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | jschar *begin() { return cb.begin(); } |
michael@0 | 78 | jschar *end() { return cb.end(); } |
michael@0 | 79 | const jschar *begin() const { return cb.begin(); } |
michael@0 | 80 | const jschar *end() const { return cb.end(); } |
michael@0 | 81 | bool empty() const { return cb.empty(); } |
michael@0 | 82 | size_t length() const { return cb.length(); } |
michael@0 | 83 | |
michael@0 | 84 | /* |
michael@0 | 85 | * Creates a string from the characters in this buffer, then (regardless |
michael@0 | 86 | * whether string creation succeeded or failed) empties the buffer. |
michael@0 | 87 | */ |
michael@0 | 88 | JSFlatString *finishString(); |
michael@0 | 89 | |
michael@0 | 90 | /* Identical to finishString() except that an atom is created. */ |
michael@0 | 91 | JSAtom *finishAtom(); |
michael@0 | 92 | |
michael@0 | 93 | /* |
michael@0 | 94 | * Creates a raw string from the characters in this buffer. The string is |
michael@0 | 95 | * exactly the characters in this buffer: it is *not* null-terminated |
michael@0 | 96 | * unless the last appended character was |(jschar)0|. |
michael@0 | 97 | */ |
michael@0 | 98 | jschar *extractWellSized(); |
michael@0 | 99 | }; |
michael@0 | 100 | |
michael@0 | 101 | inline bool |
michael@0 | 102 | StringBuffer::append(JSLinearString *str) |
michael@0 | 103 | { |
michael@0 | 104 | JS::Anchor<JSString *> anch(str); |
michael@0 | 105 | return cb.append(str->chars(), str->length()); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | inline bool |
michael@0 | 109 | StringBuffer::append(JSString *str) |
michael@0 | 110 | { |
michael@0 | 111 | JSLinearString *linear = str->ensureLinear(context()); |
michael@0 | 112 | if (!linear) |
michael@0 | 113 | return false; |
michael@0 | 114 | return append(linear); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | inline bool |
michael@0 | 118 | StringBuffer::appendInflated(const char *cstr, size_t cstrlen) |
michael@0 | 119 | { |
michael@0 | 120 | size_t lengthBefore = length(); |
michael@0 | 121 | if (!cb.growByUninitialized(cstrlen)) |
michael@0 | 122 | return false; |
michael@0 | 123 | InflateStringToBuffer(cstr, cstrlen, begin() + lengthBefore); |
michael@0 | 124 | return true; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | /* ES5 9.8 ToString, appending the result to the string buffer. */ |
michael@0 | 128 | extern bool |
michael@0 | 129 | ValueToStringBufferSlow(JSContext *cx, const Value &v, StringBuffer &sb); |
michael@0 | 130 | |
michael@0 | 131 | inline bool |
michael@0 | 132 | ValueToStringBuffer(JSContext *cx, const Value &v, StringBuffer &sb) |
michael@0 | 133 | { |
michael@0 | 134 | if (v.isString()) |
michael@0 | 135 | return sb.append(v.toString()); |
michael@0 | 136 | |
michael@0 | 137 | return ValueToStringBufferSlow(cx, v, sb); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | /* ES5 9.8 ToString for booleans, appending the result to the string buffer. */ |
michael@0 | 141 | inline bool |
michael@0 | 142 | BooleanToStringBuffer(bool b, StringBuffer &sb) |
michael@0 | 143 | { |
michael@0 | 144 | return b ? sb.append("true") : sb.append("false"); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | } /* namespace js */ |
michael@0 | 148 | |
michael@0 | 149 | #endif /* vm_StringBuffer_h */ |