V8 Project
v8::internal::CompiledReplacement Class Reference
+ Collaboration diagram for v8::internal::CompiledReplacement:

Classes

struct  ReplacementPart
 

Public Member Functions

 CompiledReplacement (Zone *zone)
 
bool Compile (Handle< String > replacement, int capture_count, int subject_length)
 
void Apply (ReplacementStringBuilder *builder, int match_from, int match_to, int32_t *match)
 
int parts ()
 
Zonezone () const
 

Private Types

enum  PartType {
  SUBJECT_PREFIX = 1 , SUBJECT_SUFFIX , SUBJECT_CAPTURE , REPLACEMENT_SUBSTRING ,
  REPLACEMENT_STRING , NUMBER_OF_PART_TYPES
}
 

Private Member Functions

template<typename Char >
bool ParseReplacementPattern (ZoneList< ReplacementPart > *parts, Vector< Char > characters, int capture_count, int subject_length, Zone *zone)
 

Private Attributes

ZoneList< ReplacementPartparts_
 
ZoneList< Handle< String > > replacement_substrings_
 
Zonezone_
 

Detailed Description

Definition at line 18 of file runtime-regexp.cc.

Member Enumeration Documentation

◆ PartType

Enumerator
SUBJECT_PREFIX 
SUBJECT_SUFFIX 
SUBJECT_CAPTURE 
REPLACEMENT_SUBSTRING 
REPLACEMENT_STRING 
NUMBER_OF_PART_TYPES 

Definition at line 37 of file runtime-regexp.cc.

Constructor & Destructor Documentation

◆ CompiledReplacement()

v8::internal::CompiledReplacement::CompiledReplacement ( Zone zone)
inlineexplicit

Definition at line 20 of file runtime-regexp.cc.

Member Function Documentation

◆ Apply()

void v8::internal::CompiledReplacement::Apply ( ReplacementStringBuilder builder,
int  match_from,
int  match_to,
int32_t *  match 
)

Definition at line 246 of file runtime-regexp.cc.

247  {
248  DCHECK_LT(0, parts_.length());
249  for (int i = 0, n = parts_.length(); i < n; i++) {
250  ReplacementPart part = parts_[i];
251  switch (part.tag) {
252  case SUBJECT_PREFIX:
253  if (match_from > 0) builder->AddSubjectSlice(0, match_from);
254  break;
255  case SUBJECT_SUFFIX: {
256  int subject_length = part.data;
257  if (match_to < subject_length) {
258  builder->AddSubjectSlice(match_to, subject_length);
259  }
260  break;
261  }
262  case SUBJECT_CAPTURE: {
263  int capture = part.data;
264  int from = match[capture * 2];
265  int to = match[capture * 2 + 1];
266  if (from >= 0 && to > from) {
267  builder->AddSubjectSlice(from, to);
268  }
269  break;
270  }
272  case REPLACEMENT_STRING:
273  builder->AddString(replacement_substrings_[part.data]);
274  break;
275  default:
276  UNREACHABLE();
277  }
278  }
279 }
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in only print modified registers Trace simulator debug messages Implied by trace sim abort randomize hashes to avoid predictable hash Fixed seed to use to hash property Print the time it takes to deserialize the snapshot A filename with extra code to be included in the A file to write the raw snapshot bytes to(mksnapshot only)") DEFINE_STRING(raw_context_file
#define UNREACHABLE()
Definition: logging.h:30
#define DCHECK_LT(v1, v2)
Definition: logging.h:209

References v8::internal::ReplacementStringBuilder::AddString(), v8::internal::ReplacementStringBuilder::AddSubjectSlice(), v8::internal::CompiledReplacement::ReplacementPart::data, DCHECK_LT, parts_, REPLACEMENT_STRING, REPLACEMENT_SUBSTRING, replacement_substrings_, SUBJECT_CAPTURE, SUBJECT_PREFIX, SUBJECT_SUFFIX, v8::internal::CompiledReplacement::ReplacementPart::tag, to(), and UNREACHABLE.

Referenced by v8::internal::StringReplaceGlobalRegExpWithString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Compile()

bool v8::internal::CompiledReplacement::Compile ( Handle< String replacement,
int  capture_count,
int  subject_length 
)

Definition at line 205 of file runtime-regexp.cc.

206  {
207  {
209  String::FlatContent content = replacement->GetFlatContent();
210  DCHECK(content.IsFlat());
211  bool simple = false;
212  if (content.IsOneByte()) {
213  simple = ParseReplacementPattern(&parts_, content.ToOneByteVector(),
214  capture_count, subject_length, zone());
215  } else {
216  DCHECK(content.IsTwoByte());
217  simple = ParseReplacementPattern(&parts_, content.ToUC16Vector(),
218  capture_count, subject_length, zone());
219  }
220  if (simple) return true;
221  }
222 
223  Isolate* isolate = replacement->GetIsolate();
224  // Find substrings of replacement string and create them as String objects.
225  int substring_index = 0;
226  for (int i = 0, n = parts_.length(); i < n; i++) {
227  int tag = parts_[i].tag;
228  if (tag <= 0) { // A replacement string slice.
229  int from = -tag;
230  int to = parts_[i].data;
232  isolate->factory()->NewSubString(replacement, from, to), zone());
234  parts_[i].data = substring_index;
235  substring_index++;
236  } else if (tag == REPLACEMENT_STRING) {
237  replacement_substrings_.Add(replacement, zone());
238  parts_[i].data = substring_index;
239  substring_index++;
240  }
241  }
242  return false;
243 }
bool ParseReplacementPattern(ZoneList< ReplacementPart > *parts, Vector< Char > characters, int capture_count, int subject_length, Zone *zone)
#define DCHECK(condition)
Definition: logging.h:205
PerThreadAssertScopeDebugOnly< HEAP_ALLOCATION_ASSERT, false > DisallowHeapAllocation
Definition: assert-scope.h:110

References DCHECK, v8::internal::Isolate::factory(), v8::internal::String::FlatContent::IsFlat(), v8::internal::String::FlatContent::IsOneByte(), v8::internal::String::FlatContent::IsTwoByte(), ParseReplacementPattern(), parts_, REPLACEMENT_STRING, REPLACEMENT_SUBSTRING, replacement_substrings_, to(), v8::internal::String::FlatContent::ToOneByteVector(), v8::internal::String::FlatContent::ToUC16Vector(), and zone().

Referenced by v8::internal::StringReplaceGlobalRegExpWithString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ParseReplacementPattern()

template<typename Char >
bool v8::internal::CompiledReplacement::ParseReplacementPattern ( ZoneList< ReplacementPart > *  parts,
Vector< Char >  characters,
int  capture_count,
int  subject_length,
Zone zone 
)
inlineprivate

Definition at line 92 of file runtime-regexp.cc.

94  {
95  int length = characters.length();
96  int last = 0;
97  for (int i = 0; i < length; i++) {
98  Char c = characters[i];
99  if (c == '$') {
100  int next_index = i + 1;
101  if (next_index == length) { // No next character!
102  break;
103  }
104  Char c2 = characters[next_index];
105  switch (c2) {
106  case '$':
107  if (i > last) {
108  // There is a substring before. Include the first "$".
109  parts->Add(
110  ReplacementPart::ReplacementSubString(last, next_index),
111  zone);
112  last = next_index + 1; // Continue after the second "$".
113  } else {
114  // Let the next substring start with the second "$".
115  last = next_index;
116  }
117  i = next_index;
118  break;
119  case '`':
120  if (i > last) {
122  }
124  i = next_index;
125  last = i + 1;
126  break;
127  case '\'':
128  if (i > last) {
130  }
131  parts->Add(ReplacementPart::SubjectSuffix(subject_length), zone);
132  i = next_index;
133  last = i + 1;
134  break;
135  case '&':
136  if (i > last) {
138  }
140  i = next_index;
141  last = i + 1;
142  break;
143  case '0':
144  case '1':
145  case '2':
146  case '3':
147  case '4':
148  case '5':
149  case '6':
150  case '7':
151  case '8':
152  case '9': {
153  int capture_ref = c2 - '0';
154  if (capture_ref > capture_count) {
155  i = next_index;
156  continue;
157  }
158  int second_digit_index = next_index + 1;
159  if (second_digit_index < length) {
160  // Peek ahead to see if we have two digits.
161  Char c3 = characters[second_digit_index];
162  if ('0' <= c3 && c3 <= '9') { // Double digits.
163  int double_digit_ref = capture_ref * 10 + c3 - '0';
164  if (double_digit_ref <= capture_count) {
165  next_index = second_digit_index;
166  capture_ref = double_digit_ref;
167  }
168  }
169  }
170  if (capture_ref > 0) {
171  if (i > last) {
173  zone);
174  }
175  DCHECK(capture_ref <= capture_count);
176  parts->Add(ReplacementPart::SubjectCapture(capture_ref), zone);
177  last = next_index + 1;
178  }
179  i = next_index;
180  break;
181  }
182  default:
183  i = next_index;
184  break;
185  }
186  }
187  }
188  if (length > last) {
189  if (last == 0) {
190  // Replacement is simple. Do not use Apply to do the replacement.
191  return true;
192  } else {
193  parts->Add(ReplacementPart::ReplacementSubString(last, length), zone);
194  }
195  }
196  return false;
197  }
int length() const
Definition: vector.h:41
static ReplacementPart ReplacementSubString(int from, int to)
static ReplacementPart SubjectSuffix(int subject_length)
static ReplacementPart SubjectCapture(int capture_index)

References DCHECK, v8::internal::Vector< T >::length(), parts(), v8::internal::CompiledReplacement::ReplacementPart::ReplacementSubString(), v8::internal::CompiledReplacement::ReplacementPart::SubjectCapture(), v8::internal::CompiledReplacement::ReplacementPart::SubjectMatch(), v8::internal::CompiledReplacement::ReplacementPart::SubjectPrefix(), v8::internal::CompiledReplacement::ReplacementPart::SubjectSuffix(), and zone().

Referenced by Compile().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parts()

int v8::internal::CompiledReplacement::parts ( )
inline

Definition at line 32 of file runtime-regexp.cc.

32 { return parts_.length(); }

References parts_.

Referenced by ParseReplacementPattern(), and v8::internal::StringReplaceGlobalRegExpWithString().

+ Here is the caller graph for this function:

◆ zone()

Zone* v8::internal::CompiledReplacement::zone ( ) const
inline

Definition at line 34 of file runtime-regexp.cc.

34 { return zone_; }

References zone_.

Referenced by Compile(), and ParseReplacementPattern().

+ Here is the caller graph for this function:

Member Data Documentation

◆ parts_

ZoneList<ReplacementPart> v8::internal::CompiledReplacement::parts_
private

Definition at line 199 of file runtime-regexp.cc.

Referenced by Apply(), Compile(), and parts().

◆ replacement_substrings_

ZoneList<Handle<String> > v8::internal::CompiledReplacement::replacement_substrings_
private

Definition at line 200 of file runtime-regexp.cc.

Referenced by Apply(), and Compile().

◆ zone_

Zone* v8::internal::CompiledReplacement::zone_
private

Definition at line 201 of file runtime-regexp.cc.

Referenced by zone().


The documentation for this class was generated from the following file: