Closed Bug 283477 Opened 19 years ago Closed 19 years ago

String.prototype.lastIndexOf returns numbers smaller than -1 on failure

Categories

(Core :: JavaScript Engine, defect, P1)

x86
Windows XP
defect

Tracking

()

VERIFIED FIXED
mozilla1.8beta2

People

(Reporter: sjoerd, Assigned: brendan)

Details

(Keywords: js1.5)

Attachments

(1 file)

a.lastIndexOf(b, c) should return -1 when there is no match, but it returns
a.length - b.length when a is shorter than b and c is, or can be converted to, a
number.

Example: 
"".lastIndexOf("hello", 0) returns -5
"".lastIndexOf("hello") returns -1
Thanks, this bug is almost ten years old!  Fix forthwith.

/be
Assignee: general → brendan
Keywords: js1.5
Priority: -- → P1
Target Milestone: --- → mozilla1.8beta2
Attached patch fixSplinter Review
Conserving the original code structure, but if you follow ECMA-262 Edition 3,
the code would be

	    d = js_DoubleToInteger(d);
	    i = (jsint) JS_MIN(JS_MAX(d, 0), textlen);

Checking in now.

/be
Attachment #175454 - Flags: review+
Sjoerd, thanks very much!

Surprising that the testsuite didn't insist on -1 for patlen > textlen, but
there it is: a hole in test coverage.  Not the last one, either!

/be
Status: NEW → RESOLVED
Closed: 19 years ago
QA Contact: pschwartau → moz
Resolution: --- → FIXED
Well, if you write code for Mozilla, the whole world is your testsuite. 
My "Find previous" code always found matches on empty lines!

But thanks for the quick response. Maybe if you have time you could add a
comment (just a comment, not a fix) to bug 274888 too.
js/tests/js1_5/Regress/regress-283477.js checked in.
Flags: testcase+
verified fixed 1.8.x and trunk.
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: