Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions std/regex/internal/backtracking.d
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,10 @@ final:
if (!g)
goto L_backtrack;
auto referenced = s[g.begin .. g.end];
while (!atEnd && !referenced.empty && front == referenced.front)
immutable fold = (re.flags & RegexOption.casefold) != 0;
while (!atEnd && !referenced.empty
&& (fold ? equalCasefold(front, referenced.front)
: front == referenced.front))
{
next();
referenced.popFront();
Expand Down Expand Up @@ -819,6 +822,7 @@ struct CtContext
import std.conv : to, text;
//dirty flags
bool counter;
bool casefold;
//to mark the portion of matches to save
int match, total_matches;
int reserved;
Expand All @@ -837,6 +841,7 @@ struct CtContext
match = 1;
reserved = 1; //first match is skipped
total_matches = re.ngroup;
casefold = (re.flags & RegexOption.casefold) != 0;
foreach (ref set; re.charsets)
{
charsets ~= set.intervals;
Expand All @@ -848,6 +853,7 @@ struct CtContext
CtContext ct;
ct.total_matches = e - s;
ct.match = 1;
ct.casefold = casefold;
return ct;
}

Expand Down Expand Up @@ -1434,19 +1440,22 @@ struct CtContext
string gStr = ir[0].localRef
? ctSub("matches[$$]", ir[0].data)
: ctSub("backrefed[$$]", ir[0].data);
immutable cmp = casefold
? "equalCasefold(front, referenced.front)"
: "front == referenced.front";
code ~= ctSub( `
if (!$$)
$$
auto referenced = s[$$.begin .. $$.end];
while (!atEnd && !referenced.empty && front == referenced.front)
while (!atEnd && !referenced.empty && $$)
{
next();
referenced.popFront();
}
if (referenced.empty)
$$
else
$$`, gStr, bailOut, gStr, gStr, nextInstr, bailOut);
$$`, gStr, bailOut, gStr, gStr, cmp, nextInstr, bailOut);
break;
case IR.Nop:
case IR.End:
Expand Down
6 changes: 6 additions & 0 deletions std/regex/internal/ir.d
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ package(std) string regexOptionsToString()(uint flags) nothrow pure @safe
return buffer[0 .. pos].idup;
}

/// True if `a` and `b` match under simple Unicode case folding (for `/i` backrefs).
bool equalCasefold(dchar lhs, dchar rhs) @safe pure nothrow @nogc
{
return simpleCaseCmp(lhs, rhs) == 0;
}

// flags that allow guide execution of engine
enum RegexInfo : uint { oneShot = 0x80 }

Expand Down
7 changes: 7 additions & 0 deletions std/regex/internal/tests.d
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ debug(std_regex_test) import std.stdio;
TestVectors( `[adzУ-Я]{4}`, "DzюЯ", "y", "$&", "DzюЯ", "i"),
TestVectors( `\p{L}\p{Lu}{10}`, "абвгдеЖЗИКЛ", "y", "$&", "абвгдеЖЗИКЛ", "i"),
TestVectors( `(?:Dåb){3}`, "DåbDÅBdÅb", "y", "$&", "DåbDÅBdÅb", "i"),
// https://github.com/dlang/phobos/issues/11025 backrefs must casefold under /i
TestVectors( `(.)\1`, "Aa", "y", "$&", "Aa", "i"),
TestVectors( `(.)\1`, "aA", "y", "$&", "aA", "i"),
TestVectors( `(.)\1`, "BB", "y", "$&", "BB", "i"),
TestVectors( `(.)\1`, "xy", "n", "-", "-", "i"),
TestVectors( `(ab)\1`, "AbAb", "y", "$&", "AbAb", "i"),
TestVectors( `(?i)(.)\1`, "Aa BB", "y", "$&", "Aa"),
//escapes:
TestVectors( `\u0041\u005a\U00000065\u0001`, "AZe\u0001", "y", "$&", "AZe\u0001"),
TestVectors( `\u`, "", "c", "-", "-"),
Expand Down
6 changes: 5 additions & 1 deletion std/regex/internal/thompson.d
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,11 @@ template ThompsonOps(E, S, bool withInput:true)
{
size_t idx = source[n].begin + t.uopCounter;
size_t end = source[n].end;
if (s[idx .. end].front == front)
immutable fold = (re.flags & RegexOption.casefold) != 0;
immutable same = fold
? equalCasefold(s[idx .. end].front, front)
: s[idx .. end].front == front;
if (same)
{
import std.utf : stride;

Expand Down
65 changes: 32 additions & 33 deletions std/uni/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -8016,6 +8016,36 @@ static assert(Grapheme.sizeof == size_t.sizeof*4);
int[Grapheme] aa;
}

// Per-codepoint simple case compare (trie tables); used by $(LREF sicmp).
package(std) int simpleCaseCmp(dchar lhs, dchar rhs) @safe @nogc pure nothrow
{
import std.internal.unicode_tables : sTable = simpleCaseTable;
import std.ascii : toLower;

int diff = lhs - rhs;
if (!diff)
return 0;
if ((lhs | rhs) < 0x80)
return toLower(lhs) - toLower(rhs);
size_t idx = simpleCaseTrie[lhs];
size_t idx2 = simpleCaseTrie[rhs];
if (idx != EMPTY_CASE_TRIE)
{
if (idx2 != EMPTY_CASE_TRIE)
{
idx = idx - sTable(idx).n;
idx2 = idx2 - sTable(idx2).n;
if (idx == idx2)
return 0;
return sTable(idx).ch - sTable(idx2).ch;
}
return sTable(idx - sTable(idx).n).ch - rhs;
}
if (idx2 != EMPTY_CASE_TRIE)
return lhs - sTable(idx2 - sTable(idx2).n).ch;
return diff;
}

/++
$(P Does basic case-insensitive comparison of `r1` and `r2`.
This function uses simpler comparison rule thus achieving better performance
Expand Down Expand Up @@ -8043,12 +8073,10 @@ int sicmp(S1, S2)(scope S1 r1, scope S2 r2)
if (isInputRange!S1 && isSomeChar!(ElementEncodingType!S1)
&& isInputRange!S2 && isSomeChar!(ElementEncodingType!S2))
{
import std.internal.unicode_tables : sTable = simpleCaseTable; // generated file
import std.range.primitives : isInfinite;
import std.utf : decodeFront;
import std.traits : isDynamicArray;
import std.typecons : Yes;
static import std.ascii;

static if ((isDynamicArray!S1 || isRandomAccessRange!S1)
&& (isDynamicArray!S2 || isRandomAccessRange!S2)
Expand All @@ -8074,7 +8102,7 @@ if (isInputRange!S1 && isSomeChar!(ElementEncodingType!S1)
auto rhs = r2[i];
if ((lhs | rhs) >= 0x80) goto NonAsciiPath;
if (lhs == rhs) continue;
auto lowDiff = std.ascii.toLower(lhs) - std.ascii.toLower(rhs);
immutable lowDiff = simpleCaseCmp(lhs, rhs);
if (lowDiff) return lowDiff;
}
static if (isInfinite!S1)
Expand All @@ -8096,38 +8124,9 @@ if (isInputRange!S1 && isSomeChar!(ElementEncodingType!S1)
if (r2.empty)
return 1;
immutable rhs = decodeFront!(Yes.useReplacementDchar)(r2);
int diff = lhs - rhs;
immutable diff = simpleCaseCmp(lhs, rhs);
if (!diff)
continue;
if ((lhs | rhs) < 0x80)
{
immutable d = std.ascii.toLower(lhs) - std.ascii.toLower(rhs);
if (!d) continue;
return d;
}
size_t idx = simpleCaseTrie[lhs];
size_t idx2 = simpleCaseTrie[rhs];
// simpleCaseTrie is packed index table
if (idx != EMPTY_CASE_TRIE)
{
if (idx2 != EMPTY_CASE_TRIE)
{// both cased chars
// adjust idx --> start of bucket
idx = idx - sTable(idx).n;
idx2 = idx2 - sTable(idx2).n;
if (idx == idx2)// one bucket, equivalent chars
continue;
else// not the same bucket
diff = sTable(idx).ch - sTable(idx2).ch;
}
else
diff = sTable(idx - sTable(idx).n).ch - rhs;
}
else if (idx2 != EMPTY_CASE_TRIE)
{
diff = lhs - sTable(idx2 - sTable(idx2).n).ch;
}
// one of chars is not cased at all
return diff;
}
return int(r2.empty) - 1;
Expand Down