Re: String.split emulation for EcmaScript 3rd ed compliance needed

From: Lasse Reichstein Nielsen (lrn_at_hotpop.com)
Date: 03/15/04


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.'


Relevant Pages

  • Re: RFC: Building the Perfect Tabbed Pane (an tutorial article)
    ... upon and beneficial to the jibbering site then I would support that. ... people give up when handed this task and only support recent browsers ... Then turn it into a link when you create the widget. ... feature testing and multiple handlers ...
    (comp.lang.javascript)
  • Re: How do I properly pass a parameter to a parameterized event handler in a loop?
    ... browsers that didn't know what a script element was and to stop them displaying the element content. ... Anyone still using one of those browsers on the web today has far more to worry about than the display of some script element content. ... var items =; ... I have a list (array) of JavaScript objects that are being provided through the DWR remoting library. ...
    (comp.lang.javascript)
  • RE: Subject: configure DNS suffix search on win2003 WMI.
    ... Microsoft MSDN Online Support Lead ... var SetSuffixes = Network.SetDNSSuffixSearchOrder; ... Replaced DNS domain suffix search order."); ...
    (microsoft.public.scripting.jscript)
  • Re: Script for Hiding/Un-Hiding Text On Click
    ... After you complained about context you didn't even restore the full context. ... And it won't be encountered by trying to write that style element as NN4.xx series browsers will never execute the document.write statement. ... Look again and you will note that Netscape4.x series browsers will *never* execute the document.write in question since it is wrapped in an if{test and since NN4 doesn't support that feature it will *never* execute it and thus never trigger the NRLB. ... support scripting style properties on-load the document, ...
    (comp.lang.javascript)
  • Re: Javascript Library
    ... JavaScript library of any kind, nor has used JavaScript code in any ... jQuery only supports the following browsers: ... Whatever you support, you shouldn't have to ...
    (comp.lang.javascript)