Bug 733602 - Various StringBuffer cleanups, mostly removing unimplemented methods. r=luke

--HG--
extra : rebase_source : 388d94bf9c332896d1b5d95aac53c4ca75e9fc97
This commit is contained in:
Jeff Walden 2012-03-06 15:28:48 -08:00
parent 1aae0dd36a
commit 08d94514e3
6 changed files with 43 additions and 59 deletions

View File

@ -186,13 +186,6 @@ js_BooleanToString(JSContext *cx, JSBool b)
return cx->runtime->atomState.booleanAtoms[b ? 1 : 0];
}
/* This function implements E-262-3 section 9.8, toString. */
bool
js::BooleanToStringBuffer(JSContext *cx, JSBool b, StringBuffer &sb)
{
return b ? sb.append("true") : sb.append("false");
}
namespace js {
bool

View File

@ -54,9 +54,6 @@ js_BooleanToString(JSContext *cx, JSBool b);
namespace js {
extern bool
BooleanToStringBuffer(JSContext *cx, JSBool b, StringBuffer &sb);
inline bool
BooleanGetPrimitiveValue(JSContext *cx, JSObject &obj, Value *vp);

View File

@ -3265,26 +3265,6 @@ js::ToStringSlow(JSContext *cx, const Value &arg)
return str;
}
/* This function implements E-262-3 section 9.8, toString. */
bool
js::ValueToStringBufferSlow(JSContext *cx, const Value &arg, StringBuffer &sb)
{
Value v = arg;
if (!ToPrimitive(cx, JSTYPE_STRING, &v))
return false;
if (v.isString())
return sb.append(v.toString());
if (v.isNumber())
return NumberValueToStringBuffer(cx, v, sb);
if (v.isBoolean())
return BooleanToStringBuffer(cx, v.toBoolean(), sb);
if (v.isNull())
return sb.append(cx->runtime->atomState.nullAtom);
JS_ASSERT(v.isUndefined());
return sb.append(cx->runtime->atomState.typeAtoms[JSTYPE_VOID]);
}
JS_FRIEND_API(JSString *)
js_ValueToSource(JSContext *cx, const Value &v)
{

View File

@ -67,6 +67,7 @@ StringBuffer::appendN(const jschar c, size_t n)
return cb.appendN(c, n);
}
/* ES5 9.8 ToString, appending the result to the string buffer. */
extern bool
ValueToStringBufferSlow(JSContext *cx, const Value &v, StringBuffer &sb);
@ -79,6 +80,13 @@ ValueToStringBuffer(JSContext *cx, const Value &v, StringBuffer &sb)
return ValueToStringBufferSlow(cx, v, sb);
}
/* ES5 9.8 ToString for booleans, appending the result to the string buffer. */
inline bool
BooleanToStringBuffer(JSContext *cx, bool b, StringBuffer &sb)
{
return b ? sb.append("true") : sb.append("false");
}
} /* namespace js */
#endif /* StringBuffer_inl_h__ */

View File

@ -6,6 +6,8 @@
#include "vm/StringBuffer.h"
#include "jsobjinlines.h"
#include "vm/String-inl.h"
#include "vm/StringBuffer-inl.h"
@ -45,8 +47,7 @@ StringBuffer::finishString()
return cx->runtime->atomState.emptyAtom;
size_t length = cb.length();
if (!checkLength(length))
return NULL;
JS_ASSERT(checkLength(length));
JS_STATIC_ASSERT(JSShortString::MAX_SHORT_LENGTH < CharBuffer::InlineLength);
if (JSShortString::lengthFits(length))
@ -78,3 +79,22 @@ StringBuffer::finishAtom()
cb.clear();
return atom;
}
bool
js::ValueToStringBufferSlow(JSContext *cx, const Value &arg, StringBuffer &sb)
{
Value v = arg;
if (!ToPrimitive(cx, JSTYPE_STRING, &v))
return false;
if (v.isString())
return sb.append(v.toString());
if (v.isNumber())
return NumberValueToStringBuffer(cx, v, sb);
if (v.isBoolean())
return BooleanToStringBuffer(cx, v.toBoolean(), sb);
if (v.isNull())
return sb.append(cx->runtime->atomState.nullAtom);
JS_ASSERT(v.isUndefined());
return sb.append(cx->runtime->atomState.typeAtoms[JSTYPE_VOID]);
}

View File

@ -26,10 +26,6 @@ namespace js {
* Well-sized extractions (which waste no more than 1/4 of their char
* buffer space) are guaranteed for strings built by this interface.
* See |extractWellSized|.
*
* Note: over-allocation is not checked for when using the infallible
* |replaceRawBuffer|, so the implementation of |finishString| also must check
* for over-allocation.
*/
class StringBuffer
{
@ -38,7 +34,6 @@ class StringBuffer
CharBuffer cb;
static inline bool checkLength(JSContext *cx, size_t length);
inline bool checkLength(size_t length);
JSContext *context() const { return cb.allocPolicy().context(); }
jschar *extractWellSized();
@ -47,7 +42,8 @@ class StringBuffer
void operator=(const StringBuffer &other) MOZ_DELETE;
public:
explicit inline StringBuffer(JSContext *cx);
explicit StringBuffer(JSContext *cx) : cb(cx) { }
inline bool reserve(size_t len);
inline bool resize(size_t len);
inline bool append(const jschar c);
@ -58,6 +54,11 @@ class StringBuffer
inline bool appendN(const jschar c, size_t n);
inline bool appendInflated(const char *cstr, size_t len);
template <size_t ArrayLength>
bool append(const char (&array)[ArrayLength]) {
return cb.append(array, array + ArrayLength - 1); /* No trailing '\0'. */
}
/* Infallible variants usable when the corresponding space is reserved. */
void infallibleAppend(const jschar c) {
cb.infallibleAppend(c);
@ -72,11 +73,6 @@ class StringBuffer
cb.infallibleAppendN(c, n);
}
JSAtom *atomize(unsigned flags = 0);
static JSAtom *atomize(JSContext *cx, const CharBuffer &cb, unsigned flags = 0);
static JSAtom *atomize(JSContext *cx, const jschar *begin, size_t length, unsigned flags = 0);
void replaceRawBuffer(jschar *chars, size_t len) { cb.replaceRawBuffer(chars, len); }
jschar *begin() { return cb.begin(); }
jschar *end() { return cb.end(); }
const jschar *begin() const { return cb.begin(); }
@ -92,17 +88,14 @@ class StringBuffer
/* Identical to finishString() except that an atom is created. */
JSAtom *finishAtom();
template <size_t ArrayLength>
bool append(const char (&array)[ArrayLength]) {
return cb.append(array, array + ArrayLength - 1); /* No trailing '\0'. */
}
};
inline
StringBuffer::StringBuffer(JSContext *cx)
: cb(cx)
{}
inline bool
StringBuffer::append(JSLinearString *str)
{
JS::Anchor<JSString *> anch(str);
return cb.append(str->chars(), str->length());
}
inline bool
StringBuffer::append(JSString *str)
@ -113,13 +106,6 @@ StringBuffer::append(JSString *str)
return append(linear);
}
inline bool
StringBuffer::append(JSLinearString *str)
{
JS::Anchor<JSString *> anch(str);
return cb.append(str->chars(), str->length());
}
inline size_t
StringBuffer::length() const
{