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 file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef StringBuffer_inl_h___
8 : #define StringBuffer_inl_h___
9 :
10 : #include "vm/StringBuffer.h"
11 :
12 : #include "vm/String-inl.h"
13 :
14 : namespace js {
15 :
16 : inline bool
17 5154096 : StringBuffer::checkLength(size_t length)
18 : {
19 5154096 : return JSString::validateLength(context(), length);
20 : }
21 :
22 : inline bool
23 1874721 : StringBuffer::reserve(size_t len)
24 : {
25 1874721 : if (!checkLength(len))
26 0 : return false;
27 1874721 : return cb.reserve(len);
28 : }
29 :
30 : inline bool
31 : StringBuffer::resize(size_t len)
32 : {
33 : if (!checkLength(len))
34 : return false;
35 : return cb.resize(len);
36 : }
37 :
38 : inline bool
39 1180748 : StringBuffer::append(const jschar c)
40 : {
41 1180748 : if (!checkLength(cb.length() + 1))
42 0 : return false;
43 1180748 : return cb.append(c);
44 : }
45 :
46 : inline bool
47 1621906 : StringBuffer::append(const jschar *chars, size_t len)
48 : {
49 1621906 : if (!checkLength(cb.length() + len))
50 0 : return false;
51 1621906 : return cb.append(chars, len);
52 : }
53 :
54 : inline bool
55 89939 : StringBuffer::append(const jschar *begin, const jschar *end)
56 : {
57 89939 : if (!checkLength(cb.length() + (end - begin)))
58 0 : return false;
59 89939 : return cb.append(begin, end);
60 : }
61 :
62 : inline bool
63 982 : StringBuffer::appendN(const jschar c, size_t n)
64 : {
65 982 : if (!checkLength(cb.length() + n))
66 0 : return false;
67 982 : return cb.appendN(c, n);
68 : }
69 :
70 : /* ES5 9.8 ToString, appending the result to the string buffer. */
71 : extern bool
72 : ValueToStringBufferSlow(JSContext *cx, const Value &v, StringBuffer &sb);
73 :
74 : inline bool
75 1953944 : ValueToStringBuffer(JSContext *cx, const Value &v, StringBuffer &sb)
76 : {
77 1953944 : if (v.isString())
78 1261336 : return sb.append(v.toString());
79 :
80 692608 : return ValueToStringBufferSlow(cx, v, sb);
81 : }
82 :
83 : /* ES5 9.8 ToString for booleans, appending the result to the string buffer. */
84 : inline bool
85 47922 : BooleanToStringBuffer(JSContext *cx, bool b, StringBuffer &sb)
86 : {
87 47922 : return b ? sb.append("true") : sb.append("false");
88 : }
89 :
90 : } /* namespace js */
91 :
92 : #endif /* StringBuffer_inl_h__ */
|