String.prototype.split() continuation.

In this spec 'ECMA-262, 1st edition, June 1997' there is section '15.5.4.8 String.prototype.split(separator)'

1. Call ToString, giving it the this value as its argument.

2. Create a new Array object of length 0 and call it A.

3. If separator is not supplied, call the [[Put]] method of A with 0 and Result(1) as arguments, and then

return A.

4. Call ToString(separator).

5. Compute the number of characters in Result(1).

6. Compute the number of characters in the string that is Result(4).

7. Let p be 0.

8. If Result(6) is zero (the separator string is empty), go to step 17.

9. Compute the smallest possible integer k not smaller than p such that k+Result(6) is not greater than

Result(5), and for all nonnegative integers j less than Result(6), the character at position k+j of Result(1) is

the same as the character at position j of Result(2); but if there is no such integer k, then go to step 14.

10. Compute a string value equal to the substring of Result(1), consisting of the characters at positions p

through k−1, inclusive.

11. Call the [[Put]] method of A with A.length and Result(10) as arguments.

12. Let p be k+Result(6).

13. Go to step 9.

14. Compute a string value equal to the substring of Result(1), consisting of the characters from position p to

the end of Result(1).

15. Call the [[Put]] method of A with A.length and Result(14) as arguments.

16. Return A.

17. If p equals Result(5), return A.

18. Compute a string value equal to the substring of Result(1), consisting of the single character at position p.

19. Call the [[Put]] method of A with A.length and Result(18) as arguments.

20. Increase p by 1.

21. Go to step 17.

In this algorithm, provided that the separator, for example, consists of one character and the string on which split method is called "Hello, World!", step number 9 is not clear to me. At this step I cannot find 'k' and 'j' because the array from step number 2 is empty (there are no characters in this array) and, accordingly, move on to step number 14. At step number 14, with 'p' equal to 0, the result of the step will be the entire original string. And the algorithm will end with steps 15 and 16 with the result equal to the original string, although the separator may be present in this string. Please point out places where I don't understand what's going on. Thank you!

Offhand, I think step 9's reference to Result(2) is a typo for Result(4).

1 Like

Typo!? In the specification? Which is also in the second version? And only in the third version (with the advent of regular expressions) has this algorithm already been rewritten (although I haven’t gotten to it yet :smiley:). Anyone else reading the specifications? Brothers in misfortune, respond!

It’s a living standard so I’m not sure it’s worth reading anything but the latest version on github, except for historical interest.

1 Like

Right, reading historical versions of the spec can be interesting, but they aren't particularly relevant for understanding what browsers are currently doing.

The current definition of this method is at https://tc39.es/ecma262/#sec-string.prototype.split

1 Like

Yes, gentlemen, I completely agree with you and do not argue. With the exception of one small detail, back in 1997, this specification was precisely the latest version. :face_with_hand_over_mouth:

1 Like