Bug 1019512 part 1 - Make trim/trimLeft/trimRight work with Latin1 strings. r=luke

This commit is contained in:
Jan de Mooij 2014-06-05 12:01:54 +02:00
parent d221842029
commit 252475f532
2 changed files with 61 additions and 16 deletions

View File

@ -0,0 +1,27 @@
function test() {
// Latin1
var s = toLatin1(" \r\t\n\u00A0foo 123\t \r\n\u00A0");
var res = s.trim();
assertEq(isLatin1(res), true);
assertEq(res, "foo 123");
res = s.trimLeft();
assertEq(isLatin1(res), true);
assertEq(res, "foo 123\t \r\n\u00A0");
res = s.trimRight();
assertEq(isLatin1(res), true);
assertEq(res, " \r\t\n\u00A0foo 123");
res = toLatin1("foo 1234").trim();
assertEq(isLatin1(res), true);
assertEq(res, "foo 1234");
// TwoByte
s = " \r\t\n\u00A0\u2000foo\u1200123\t \r\n\u00A0\u2009";
assertEq(s.trim(), "foo\u1200123");
assertEq(s.trimLeft(), "foo\u1200123\t \r\n\u00A0\u2009");
assertEq(s.trimRight(), " \r\t\n\u00A0\u2000foo\u1200123");
}
test();

View File

@ -1719,20 +1719,12 @@ str_endsWith(JSContext *cx, unsigned argc, Value *vp)
return true;
}
static bool
js_TrimString(JSContext *cx, Value *vp, bool trimLeft, bool trimRight)
template <typename CharT>
static void
TrimString(const CharT *chars, bool trimLeft, bool trimRight, size_t length,
size_t *pBegin, size_t *pEnd)
{
CallReceiver call = CallReceiverFromVp(vp);
RootedString str(cx, ThisToStringForStringProto(cx, call));
if (!str)
return false;
size_t length = str->length();
const jschar *chars = str->getChars(cx);
if (!chars)
return false;
size_t begin = 0;
size_t end = length;
size_t begin = 0, end = length;
if (trimLeft) {
while (begin < length && unicode::IsSpace(chars[begin]))
@ -1744,6 +1736,32 @@ js_TrimString(JSContext *cx, Value *vp, bool trimLeft, bool trimRight)
--end;
}
*pBegin = begin;
*pEnd = end;
}
static bool
TrimString(JSContext *cx, Value *vp, bool trimLeft, bool trimRight)
{
CallReceiver call = CallReceiverFromVp(vp);
RootedString str(cx, ThisToStringForStringProto(cx, call));
if (!str)
return false;
JSLinearString *linear = str->ensureLinear(cx);
if (!linear)
return false;
size_t length = linear->length();
size_t begin, end;
if (linear->hasLatin1Chars()) {
AutoCheckCannotGC nogc;
TrimString(linear->latin1Chars(nogc), trimLeft, trimRight, length, &begin, &end);
} else {
AutoCheckCannotGC nogc;
TrimString(linear->twoByteChars(nogc), trimLeft, trimRight, length, &begin, &end);
}
str = js_NewDependentString(cx, str, begin, end - begin);
if (!str)
return false;
@ -1755,19 +1773,19 @@ js_TrimString(JSContext *cx, Value *vp, bool trimLeft, bool trimRight)
static bool
str_trim(JSContext *cx, unsigned argc, Value *vp)
{
return js_TrimString(cx, vp, true, true);
return TrimString(cx, vp, true, true);
}
static bool
str_trimLeft(JSContext *cx, unsigned argc, Value *vp)
{
return js_TrimString(cx, vp, true, false);
return TrimString(cx, vp, true, false);
}
static bool
str_trimRight(JSContext *cx, unsigned argc, Value *vp)
{
return js_TrimString(cx, vp, false, true);
return TrimString(cx, vp, false, true);
}
/*