|
| 1 | +function shouldBe(actual, expected, message) { |
| 2 | + if (actual !== expected) |
| 3 | + throw new Error(message + ": expected " + JSON.stringify(expected) + " but got " + JSON.stringify(actual)); |
| 4 | +} |
| 5 | + |
| 6 | +function splitAndCaptureStatics(regexp, input, limit) { |
| 7 | + /ZZZ/.exec("xxxZZZyyy"); |
| 8 | + const result = input.split(regexp, limit); |
| 9 | + result.push(RegExp.input, RegExp.lastMatch, RegExp.leftContext, RegExp.rightContext, regexp.lastIndex); |
| 10 | + return result.join("|"); |
| 11 | +} |
| 12 | + |
| 13 | +function compare(source, flags, inputs, limits) { |
| 14 | + const regexp = new RegExp(source, flags); |
| 15 | + const reference = new RegExp("(?:" + source + ")", flags); |
| 16 | + regexp.lastIndex = reference.lastIndex = 3; |
| 17 | + for (const input of inputs) { |
| 18 | + for (const limit of limits) { |
| 19 | + const actual = splitAndCaptureStatics(regexp, input, limit); |
| 20 | + const expected = splitAndCaptureStatics(reference, input, limit); |
| 21 | + if (actual !== expected) |
| 22 | + throw new Error("/" + source + "/" + flags + " " + JSON.stringify(input) + " " + limit + ": expected " + JSON.stringify(expected) + " but got " + JSON.stringify(actual)); |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +const inputs = [""]; |
| 28 | +for (let i = 0; inputs[i].length < 5; ++i) { |
| 29 | + for (const character of ["a", "\r", "\n"]) |
| 30 | + inputs.push(inputs[i] + character); |
| 31 | +} |
| 32 | +const shortInputs = inputs.slice(0, 121); |
| 33 | +for (const input of inputs.slice(0, 40)) |
| 34 | + inputs.push("\u3042" + input, input + "\u3042"); |
| 35 | +for (const length of [7, 8, 15, 16, 17, 31, 32, 33, 63, 64, 65]) { |
| 36 | + for (const separator of ["\n", "\r\n", "\r", "\r\r\n", "\n\r"]) { |
| 37 | + for (const fill of ["x", "\u3042"]) |
| 38 | + inputs.push(fill.repeat(length) + separator + fill.repeat(length) + separator); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +compare("\\r?\\n", "", inputs, [undefined, 0, 1, 2, 5, -1]); |
| 43 | + |
| 44 | +for (const flags of ["g", "ds"]) |
| 45 | + compare("\\r?\\n", flags, shortInputs, [undefined, 2]); |
| 46 | + |
| 47 | +for (const source of ["\\r\\n", "\\r??\\n", "\\r*\\n", "\\r+\\n", "\\r{0,2}\\n", "\\n?\\n", "a?\\n", "\\r?\\r", "\\r?a", "\\r?\\n?", "\\r?\\n*", "\\r?\\n+", "\\r?\\n{2}", "\\r?", "\\r?\\n\\n", "\\r?\\na", "\\r?\\n|a", "(\\r?\\n)", "^\\r?\\n", "\\r?\\n$"]) |
| 48 | + compare(source, "", shortInputs, [undefined, 2]); |
| 49 | + |
| 50 | +function split(input, limit) { |
| 51 | + return input.split(/\r?\n/, limit); |
| 52 | +} |
| 53 | +noInline(split); |
| 54 | + |
| 55 | +function symbolSplit(input, limit) { |
| 56 | + return /\r?\n/[Symbol.split](input, limit); |
| 57 | +} |
| 58 | +noInline(symbolSplit); |
| 59 | + |
| 60 | +function test(splitFunction, mixed, wide) { |
| 61 | + shouldBe(splitFunction(mixed).join("|"), "a|b|c\rd\r|e|\rf|", "mixed"); |
| 62 | + shouldBe(RegExp.lastMatch, "\n", "lastMatch"); |
| 63 | + shouldBe(RegExp.leftContext, "a\nb\r\nc\rd\r\r\ne\n\rf", "leftContext"); |
| 64 | + shouldBe(RegExp.rightContext, "", "rightContext"); |
| 65 | + |
| 66 | + shouldBe(splitFunction(wide, 2).join("|"), "\u3042|\u3044", "wide"); |
| 67 | + shouldBe(RegExp.lastMatch, "\n", "lastMatch with limit"); |
| 68 | + shouldBe(RegExp.leftContext, "\u3042\r\n\u3044", "leftContext with limit"); |
| 69 | + shouldBe(RegExp.rightContext, "\u3046\r", "rightContext with limit"); |
| 70 | +} |
| 71 | + |
| 72 | +const mixed = "a\nb\r\nc\rd\r\r\ne\n\rf\n"; |
| 73 | +const wide = "\u3042\r\n\u3044\n\u3046\r"; |
| 74 | +const nonAtomMixed = mixed.split("").join(""); |
| 75 | +const nonAtomWide = wide.split("").join(""); |
| 76 | + |
| 77 | +for (let i = 0; i < testLoopCount; ++i) { |
| 78 | + const splitFunction = i & 1 ? split : symbolSplit; |
| 79 | + if (i & 2) |
| 80 | + test(splitFunction, mixed, wide); |
| 81 | + else |
| 82 | + test(splitFunction, nonAtomMixed, nonAtomWide); |
| 83 | +} |
0 commit comments