Skip to content

Commit

Permalink
Merge pull request #7123 from n8sh/issue-20098
Browse files Browse the repository at this point in the history
Fix Issue 20098 - Improve result of printing std.regex compiled pattern
merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
  • Loading branch information
dlang-bot committed Aug 4, 2019
2 parents 5094f68 + e226022 commit cc9a56f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
28 changes: 28 additions & 0 deletions std/regex/internal/ir.d
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ enum RegexOption: uint {
//do not reorder this list
alias RegexOptionNames = AliasSeq!('g', 'i', 'x', 'U', 'm', 's');
static assert( RegexOption.max < 0x80);

package(std) string regexOptionsToString()(uint flags) nothrow pure @safe
{
flags &= (RegexOption.max << 1) - 1;
if (!flags)
return "";
char[RegexOptionNames.length] buffer = void;
size_t pos = 0;
foreach (i, flag; __traits(allMembers, RegexOption))
if (flags & __traits(getMember, RegexOption, flag))
buffer[pos++] = RegexOptionNames[i];
return buffer[0 .. pos].idup;
}

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

Expand Down Expand Up @@ -709,6 +723,20 @@ package(std.regex):
writeln("Max counter nesting depth: ", maxCounterDepth);
}

public string toString()() const
{
import std.format : format;
static if (is(typeof(pattern) : string))
alias patternString = pattern;
else
{
import std.conv : to;
auto patternString = conv.to!string(pattern);
}
auto quotedEscapedPattern = format("%(%s %)", [patternString]);
auto flagString = regexOptionsToString(flags);
return "Regex!" ~ Char.stringof ~ "(" ~ quotedEscapedPattern ~ ", \"" ~ flagString ~ "\")";
}
}

// The stuff below this point is temporarrily part of IR module
Expand Down
12 changes: 12 additions & 0 deletions std/regex/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,18 @@ if (isSomeString!(S))
assert(m.front[1] == "12");
}

@system unittest
{
import std.conv : to;
import std.string : indexOf;

immutable pattern = "s+";
auto regexString = to!string(regex(pattern, "U"));
assert(regexString.length <= pattern.length + 100, "String representation shouldn't be unreasonably bloated.");
assert(indexOf(regexString, "s+") >= 0, "String representation should include pattern.");
assert(indexOf(regexString, 'U') >= 0, "String representation should include flags.");
}

public auto regexImpl(S)(S pattern, const(char)[] flags="")
if (isSomeString!(S))
{
Expand Down

0 comments on commit cc9a56f

Please sign in to comment.