OLD | NEW |
| (Empty) |
1 // Copyright 2012 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 (function(global, utils) { | |
6 | |
7 'use strict'; | |
8 | |
9 %CheckIsBootstrapping(); | |
10 | |
11 // ------------------------------------------------------------------- | |
12 | |
13 // ES#sec-getsubstitution | |
14 // GetSubstitution(matched, str, position, captures, replacement) | |
15 // Expand the $-expressions in the string and return a new string with | |
16 // the result. | |
17 function GetSubstitution(matched, string, position, captures, replacement) { | |
18 var matchLength = matched.length; | |
19 var stringLength = string.length; | |
20 var capturesLength = captures.length; | |
21 var tailPos = position + matchLength; | |
22 var result = ""; | |
23 var pos, expansion, peek, next, scaledIndex, advance, newScaledIndex; | |
24 | |
25 var next = %StringIndexOf(replacement, '$', 0); | |
26 if (next < 0) { | |
27 result += replacement; | |
28 return result; | |
29 } | |
30 | |
31 if (next > 0) result += %_SubString(replacement, 0, next); | |
32 | |
33 while (true) { | |
34 expansion = '$'; | |
35 pos = next + 1; | |
36 if (pos < replacement.length) { | |
37 peek = %_StringCharCodeAt(replacement, pos); | |
38 if (peek == 36) { // $$ | |
39 ++pos; | |
40 result += '$'; | |
41 } else if (peek == 38) { // $& - match | |
42 ++pos; | |
43 result += matched; | |
44 } else if (peek == 96) { // $` - prefix | |
45 ++pos; | |
46 result += %_SubString(string, 0, position); | |
47 } else if (peek == 39) { // $' - suffix | |
48 ++pos; | |
49 result += %_SubString(string, tailPos, stringLength); | |
50 } else if (peek >= 48 && peek <= 57) { | |
51 // Valid indices are $1 .. $9, $01 .. $09 and $10 .. $99 | |
52 scaledIndex = (peek - 48); | |
53 advance = 1; | |
54 if (pos + 1 < replacement.length) { | |
55 next = %_StringCharCodeAt(replacement, pos + 1); | |
56 if (next >= 48 && next <= 57) { | |
57 newScaledIndex = scaledIndex * 10 + ((next - 48)); | |
58 if (newScaledIndex < capturesLength) { | |
59 scaledIndex = newScaledIndex; | |
60 advance = 2; | |
61 } | |
62 } | |
63 } | |
64 if (scaledIndex != 0 && scaledIndex < capturesLength) { | |
65 var capture = captures.at(scaledIndex); | |
66 if (!IS_UNDEFINED(capture)) result += capture; | |
67 pos += advance; | |
68 } else { | |
69 result += '$'; | |
70 } | |
71 } else { | |
72 result += '$'; | |
73 } | |
74 } else { | |
75 result += '$'; | |
76 } | |
77 | |
78 // Go the the next $ in the replacement. | |
79 next = %StringIndexOf(replacement, '$', pos); | |
80 | |
81 // Return if there are no more $ characters in the replacement. If we | |
82 // haven't reached the end, we need to append the suffix. | |
83 if (next < 0) { | |
84 if (pos < replacement.length) { | |
85 result += %_SubString(replacement, pos, replacement.length); | |
86 } | |
87 return result; | |
88 } | |
89 | |
90 // Append substring between the previous and the next $ character. | |
91 if (next > pos) { | |
92 result += %_SubString(replacement, pos, next); | |
93 } | |
94 } | |
95 return result; | |
96 } | |
97 | |
98 // ------------------------------------------------------------------- | |
99 // Exports | |
100 | |
101 utils.Export(function(to) { | |
102 to.GetSubstitution = GetSubstitution; | |
103 }); | |
104 | |
105 }) | |
OLD | NEW |