Skip to content

Commit 8202a91

Browse files
authored
Merge pull request #6852 from kinke/regex
std.regex: Fix reassignment of ctRegex wrapper structs
2 parents 6a759b8 + 5cc7fc5 commit 8202a91

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

std/regex/package.d

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,16 @@ if (isSomeString!(S))
423423
}
424424

425425

426+
private struct CTRegexWrapper(Char)
427+
{
428+
private immutable(Regex!Char)* re;
429+
430+
// allow code that expects mutable Regex to still work
431+
// we stay "logically const"
432+
@property @trusted ref getRe() const { return *cast(Regex!Char*) re; }
433+
alias getRe this;
434+
}
435+
426436
template ctRegexImpl(alias pattern, string flags=[])
427437
{
428438
import std.regex.internal.backtracking, std.regex.internal.parser;
@@ -437,14 +447,7 @@ template ctRegexImpl(alias pattern, string flags=[])
437447
}
438448
static immutable staticRe =
439449
cast(immutable) r.withFactory(new CtfeFactory!(BacktrackingMatcher, Char, func));
440-
struct Wrapper
441-
{
442-
// allow code that expects mutable Regex to still work
443-
// we stay "logically const"
444-
@property @trusted ref getRe() const { return *cast(Regex!Char*)&staticRe; }
445-
alias getRe this;
446-
}
447-
enum wrapper = Wrapper();
450+
enum wrapper = CTRegexWrapper!Char(&staticRe);
448451
}
449452

450453
@safe unittest
@@ -457,6 +460,17 @@ template ctRegexImpl(alias pattern, string flags=[])
457460
test(re);
458461
}
459462

463+
@safe unittest
464+
{
465+
auto re = ctRegex!`foo`;
466+
assert(matchFirst("foo", re));
467+
468+
// test reassignment
469+
re = ctRegex!`bar`;
470+
assert(matchFirst("bar", re));
471+
assert(!matchFirst("bar", ctRegex!`foo`));
472+
}
473+
460474
/++
461475
Compile regular expression using CTFE
462476
and generate optimized native machine code for matching it.

0 commit comments

Comments
 (0)