}
+static bool BackRefMatchesNoCase(int from,
+ int current,
+ int len,
+ Vector<const char> subject) {
+ for (int i = 0; i < len; i++) {
+ unsigned int old_char = subject[from++];
+ unsigned int new_char = subject[current++];
+ if (old_char == new_char) continue;
+ if (old_char - 'A' <= 'Z' - 'A') old_char |= 0x20;
+ if (new_char - 'A' <= 'Z' - 'A') new_char |= 0x20;
+ if (old_char != new_char) return false;
+ }
+ return true;
+}
+
+
#ifdef DEBUG
static void TraceInterpreter(const byte* code_base,
const byte* pc,
+template <typename Char>
static bool RawMatch(const byte* code_base,
- Vector<const uc16> subject,
+ Vector<const Char> subject,
int* registers,
int current,
int current_char) {
bool IrregexpInterpreter::Match(Handle<ByteArray> code_array,
- Handle<String> subject16,
+ Handle<String> subject,
int* registers,
int start_position) {
- ASSERT(StringShape(*subject16).IsTwoByteRepresentation());
- ASSERT(subject16->IsFlat(StringShape(*subject16)));
+ ASSERT(subject->IsFlat(StringShape(*subject)));
AssertNoAllocation a;
const byte* code_base = code_array->GetDataStartAddress();
+ StringShape subject_shape(*subject);
uc16 previous_char = '\n';
- Vector<const uc16> subject_vector =
- Vector<const uc16>(subject16->GetTwoByteData(), subject16->length());
- if (start_position != 0) previous_char = subject_vector[start_position - 1];
- return RawMatch(code_base,
- subject_vector,
- registers,
- start_position,
- previous_char);
+ if (subject_shape.IsAsciiRepresentation()) {
+ Vector<const char> subject_vector = subject->ToAsciiVector();
+ if (start_position != 0) previous_char = subject_vector[start_position - 1];
+ return RawMatch(code_base,
+ subject_vector,
+ registers,
+ start_position,
+ previous_char);
+ } else {
+ Vector<const uc16> subject_vector = subject->ToUC16Vector();
+ if (start_position != 0) previous_char = subject_vector[start_position - 1];
+ return RawMatch(code_base,
+ subject_vector,
+ registers,
+ start_position,
+ previous_char);
+ }
}
} } // namespace v8::internal
int tag = Smi::cast(irregexp->get(kIrregexpImplementationIndex))->value();
+ if (!subject->IsFlat(StringShape(*subject))) {
+ FlattenString(subject);
+ }
+
switch (tag) {
case RegExpMacroAssembler::kIA32Implementation: {
#ifndef ARM
- if (!subject->IsFlat(StringShape(*subject))) {
- FlattenString(subject);
- }
Handle<Code> code = IrregexpNativeCode(irregexp);
StringShape shape(*subject);
}
Handle<ByteArray> byte_codes = IrregexpByteCode(irregexp);
- Handle<String> two_byte_subject = CachedStringToTwoByte(subject);
-
rc = IrregexpInterpreter::Match(byte_codes,
- two_byte_subject,
+ subject,
offsets_vector,
previous_index);
break;
class RegExpCompiler {
public:
- RegExpCompiler(int capture_count, bool ignore_case);
+ RegExpCompiler(int capture_count, bool ignore_case, bool is_ascii);
int AllocateRegister() { return next_register_++; }
inline void DecrementRecursionDepth() { recursion_depth_--; }
inline bool ignore_case() { return ignore_case_; }
+ inline bool ascii() { return ascii_; }
private:
EndNode* accept_;
int recursion_depth_;
RegExpMacroAssembler* macro_assembler_;
bool ignore_case_;
+ bool ascii_;
};
// Attempts to compile the regexp using an Irregexp code generator. Returns
// a fixed array or a null handle depending on whether it succeeded.
-RegExpCompiler::RegExpCompiler(int capture_count, bool ignore_case)
+RegExpCompiler::RegExpCompiler(int capture_count, bool ignore_case, bool ascii)
: next_register_(2 * (capture_count + 1)),
work_list_(NULL),
recursion_depth_(0),
- ignore_case_(ignore_case) {
+ ignore_case_(ignore_case),
+ ascii_(ascii) {
accept_ = new EndNode(EndNode::ACCEPT);
}
chars[0],
chars[1],
on_failure)) {
- ok.Unuse();
} else {
macro_assembler->CheckCharacter(chars[0], &ok);
macro_assembler->CheckNotCharacter(chars[1], on_failure);
RegExpCharacterClass* cc,
int cp_offset,
Label* on_failure,
- bool check_offset) {
+ bool check_offset,
+ bool ascii) {
ZoneList<CharacterRange>* ranges = cc->ranges();
+ const int max_char = ascii ?
+ String::kMaxAsciiCharCode :
+ String::kMaxUC16CharCode;
Label success;
int range_count = ranges->length();
- if (range_count == 0) {
+ int last_valid_range = range_count - 1;
+ while (last_valid_range >= 0) {
+ CharacterRange& range = ranges->at(last_valid_range);
+ if (range.from() <= max_char) {
+ break;
+ }
+ last_valid_range--;
+ }
+
+ if (last_valid_range < 0) {
if (!cc->is_negated()) {
+ // TODO(plesner): We can remove this when the node level does our
+ // ASCII optimizations for us.
macro_assembler->GoTo(on_failure);
}
return;
}
- if (range_count == 1 &&
+ if (last_valid_range == 0 &&
!cc->is_negated() &&
- ranges->at(0).IsEverything(0xffff)) {
+ ranges->at(0).IsEverything(max_char)) {
// This is a common case hit by non-anchored expressions.
// TODO(erikcorry): We should have a macro assembler instruction that just
// checks for end of string without loading the character.
macro_assembler->LoadCurrentCharacterUnchecked(cp_offset);
}
- for (int i = 0; i < range_count - 1; i++) {
+ for (int i = 0; i <= last_valid_range; i++) {
CharacterRange& range = ranges->at(i);
Label next_range;
uc16 from = range.from();
uc16 to = range.to();
+ if (from > max_char) {
+ continue;
+ }
+ if (to > max_char) to = max_char;
if (to == from) {
macro_assembler->CheckCharacter(to, char_is_in_class);
} else {
if (from != 0) {
macro_assembler->CheckCharacterLT(from, &next_range);
}
- if (to != 0xffff) {
+ if (to != max_char) {
macro_assembler->CheckCharacterLT(to + 1, char_is_in_class);
} else {
macro_assembler->GoTo(char_is_in_class);
macro_assembler->Bind(&next_range);
}
- CharacterRange& range = ranges->at(range_count - 1);
+ CharacterRange& range = ranges->at(last_valid_range);
uc16 from = range.from();
uc16 to = range.to();
+ if (to > max_char) to = max_char;
+ ASSERT(to >= from);
+
if (to == from) {
if (cc->is_negated()) {
macro_assembler->CheckCharacter(to, on_failure);
macro_assembler->CheckCharacterLT(from, on_failure);
}
}
- if (to != 0xffff) {
+ if (to != String::kMaxUC16CharCode) {
if (cc->is_negated()) {
macro_assembler->CheckCharacterLT(to + 1, on_failure);
} else {
macro_assembler->GoTo(backtrack);
return true;
}
- // First, handle straight character matches.
+ // First check for non-ASCII text.
+ // TODO(plesner): We should do this at node level.
+ if (compiler->ascii()) {
+ for (int i = element_count - 1; i >= 0; i--) {
+ TextElement elm = elms_->at(i);
+ if (elm.type == TextElement::ATOM) {
+ Vector<const uc16> quarks = elm.data.u_atom->data();
+ for (int j = quarks.length() - 1; j >= 0; j--) {
+ if (quarks[j] > String::kMaxAsciiCharCode) {
+ macro_assembler->GoTo(backtrack);
+ return true;
+ }
+ }
+ } else {
+ ASSERT_EQ(elm.type, TextElement::CHAR_CLASS);
+ }
+ }
+ }
+ // Second, handle straight character matches.
int checked_up_to = -1;
for (int i = element_count - 1; i >= 0; i--) {
TextElement elm = elms_->at(i);
ASSERT_EQ(elm.type, TextElement::CHAR_CLASS);
}
}
- // Second, handle case independent letter matches if any.
+ // Third, handle case independent letter matches if any.
if (compiler->ignore_case()) {
for (int i = element_count - 1; i >= 0; i--) {
TextElement elm = elms_->at(i);
cc,
cp_offset,
backtrack,
- checked_up_to < cp_offset);
+ checked_up_to < cp_offset,
+ compiler->ascii());
if (cp_offset > checked_up_to) checked_up_to = cp_offset;
}
}
int elmc,
ZoneList<CharacterRange>* ranges) {
ASSERT(elmv[0] != 0x0000);
- ASSERT(elmv[elmc-1] != 0xFFFF);
+ ASSERT(elmv[elmc-1] != String::kMaxUC16CharCode);
uc16 last = 0x0000;
for (int i = 0; i < elmc; i += 2) {
ASSERT(last <= elmv[i] - 1);
ranges->Add(CharacterRange(last, elmv[i] - 1));
last = elmv[i + 1] + 1;
}
- ranges->Add(CharacterRange(last, 0xFFFF));
+ ranges->Add(CharacterRange(last, String::kMaxUC16CharCode));
}
entry->AddValue(value);
// Bail out if the last interval ended at 0xFFFF since otherwise
// adding 1 will wrap around to 0.
- if (entry->to() == 0xFFFF)
+ if (entry->to() == String::kMaxUC16CharCode)
break;
ASSERT(entry->to() + 1 > current.from());
current.set_from(entry->to() + 1);
if (last < range.from())
AddRange(CharacterRange(last, range.from() - 1));
if (range.to() >= last) {
- if (range.to() == 0xFFFF) {
+ if (range.to() == String::kMaxUC16CharCode) {
return;
} else {
last = range.to() + 1;
}
}
}
- AddRange(CharacterRange(last, 0xFFFF));
+ AddRange(CharacterRange(last, String::kMaxUC16CharCode));
}
bool is_multiline,
Handle<String> pattern,
bool is_ascii) {
- RegExpCompiler compiler(input->capture_count, ignore_case);
+ RegExpCompiler compiler(input->capture_count, ignore_case, is_ascii);
// Wrap the body of the regexp in capture #0.
RegExpNode* captured_body = RegExpCapture::ToNode(input->tree,
0,