Re: String.split emulation for EcmaScript 3rd ed compliance needed
From: Lasse Reichstein Nielsen (lrn_at_hotpop.com)
Date: 03/15/04
- Next message: Mike Bittel: "Re: iFrame Difficulty"
- Previous message: Dr John Stockton: "Re: Is there any posibility of auto install font with jscript, html, or others?"
- In reply to: Marek Mand: "Re: String.split emulation for EcmaScript 3rd ed compliance needed"
- Next in thread: Marek Mand: "Re: String.split emulation for EcmaScript 3rd ed compliance needed"
- Reply: Marek Mand: "Re: String.split emulation for EcmaScript 3rd ed compliance needed"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 15 Mar 2004 19:41:06 +0100
Marek Mand <.@.> writes:
> for quickjump: There are serious issues with newly posted code.
So much for a quick hack.
Ok, if you want ECMA-compliant behavior, we'll just have to check what
the specification says. Ofcourse, implementing the ECMA262v3 definition
of split directly would be stupid ... so here you go :) The number in the
comment refers to the corresponding line number in the definition.
---
function mySplit5(string,separator,limit) {
var S = String(string); // 1
var A = new Array(); // 2
var lim;
if (typeof limit == "undefined") { // 3
lim = 0xffff;
} else {
lim = limit >> 0; // toUInt32
}
var s = S.length; // 4
var p = 0; // 5
var R;
if (separator instanceof RegExp) { // 6
R = separator;
} else {
R = String(separator);
}
if (lim == 0) {return A;} // 7
if (typeof separator == "undefined") { // 8
A[0]=S; // 33
return A; // 34
}
if (s == 0) { // 9
var z = SplitMatch(R,S,0); // 31
if (!z.isFailure()) { // 32
return A;
}
A[0]=S; // 33
return A; // 34
}
var q = p; // 10
while(q != s) { // 11
z = SplitMatch(R,S,q); // 12
if (!z.isFailure()) { // 13
var e = z.endIndex; // 14
var cap = z.captureArray; // 14
if (e != p) { // 15
var T = S.substring(p,q); // 16
A[A.length]=T; // 17
if (A.length == lim) {return A;} // 18
p = e; // 19
var i=0; // 20
while(i != cap.length) { // 21
i++; // 22
A[A.length]=cap[i-1]; // 23 // 0-based array
if (A.length == lim) {return A;} // 24
} // 25
q = p // 10
continue; // goto 11
}
}
q++; // 26
} // 27
T = S.substring(p,s); // 28
A[A.length] = T; // 29
return A; // 30
}
// MatchResults: Failure or State
var Failure = new Object();
Failure.isFailure = function(){return true;};
function State(endIndex,captureArray) {
this.endIndex = endIndex;
this.captureArray = captureArray;
}
State.prototype.isFailure = function(){return false;};
function SplitMatch(R,S,q) {
if (!R instanceof RegExp) { // 1
var r = R.length; // R is a string // 2
var s = S.length; // 3
if (q+r>s) {return Failure;} // 4
if (R.indexOf(S)!=0) {return Failure;} // 5
var cap = []; // 6
return new State(q+r,cap); // 7
}
var match = R.exec(S.substring(q)); // 8
if (match && (match[0].length==0 || S.substring(q).indexOf(match[0])==0)) {
return new State(q+match[0].length,match.slice(1));
} else {
return Failure;
}
}
---
There is probably the inevitable bug somewhere in there, but
it works on the HTML example and the zero-width matchers.
(Interstingly, Opera 7.5preview3 thinks string.indexof("")==-1 , where
the other browsers thinks it is 0). I think I'll bugreport it later :)
> Oh no, apply and call and functional argument. If there is possible
> way to avoid them, it must be done at all cost in web enivornment for
> some years to come to achieve broadreach .
Which browsers is it that doesn't support them? IE 4 and 5, right?
Sigh. We need forced upgrade ... if needed at gun point :)
> I think when some new browser appears on the market with JS support,
> those are the things that are most 'complex' part of the script
> spec, thus support for them is implemented in later builds of a
> script interpreter.
It was in Netscape 4!
> Thanks for Your participation and hope You have found this thread
> interesting among in the gray mass of 'usual problem posts how to open
> a window' ! =D
Definitly. Can't resist a good challenge. Some time I might sit down and
understand the specification, so I can make a more efficient version :)
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
- Next message: Mike Bittel: "Re: iFrame Difficulty"
- Previous message: Dr John Stockton: "Re: Is there any posibility of auto install font with jscript, html, or others?"
- In reply to: Marek Mand: "Re: String.split emulation for EcmaScript 3rd ed compliance needed"
- Next in thread: Marek Mand: "Re: String.split emulation for EcmaScript 3rd ed compliance needed"
- Reply: Marek Mand: "Re: String.split emulation for EcmaScript 3rd ed compliance needed"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|