Skip to content

Commit 8e6bf69

Browse files
committed
Merge pull request #1532 from blackwhale/fix-replace-check
Fix issue 10913 std.regex API regression
2 parents d39696d + 99f466f commit 8e6bf69

1 file changed

Lines changed: 30 additions & 14 deletions

File tree

std/regex.d

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,6 @@ private:
274274

275275
import std.uni : isAlpha, isWhite;
276276

277-
@safe:
278-
279277

280278
// IR bit pattern: 0b1_xxxxx_yy
281279
// where yy indicates class of instruction, xxxxx for actual operation code
@@ -2023,7 +2021,7 @@ public struct Regex(Char)
20232021
assert(!r.empty);
20242022
---
20252023
+/
2026-
@property bool empty() const nothrow { return ir is null; }
2024+
@safe @property bool empty() const nothrow { return ir is null; }
20272025

20282026
/++
20292027
A range of all the named captures in the regex.
@@ -2042,7 +2040,7 @@ public struct Regex(Char)
20422040
assert(nc[1..$].equal(["var"]));
20432041
----
20442042
+/
2045-
@property auto namedCaptures()
2043+
@safe @property auto namedCaptures()
20462044
{
20472045
static struct NamedGroupRange
20482046
{
@@ -2526,7 +2524,7 @@ public struct StaticRegex(Char)
25262524
{
25272525
private:
25282526
alias BacktrackingMatcher!(true) Matcher;
2529-
alias bool function(ref Matcher!Char) MatchFn;
2527+
alias bool function(ref Matcher!Char) @trusted MatchFn;
25302528
MatchFn nativeFn;
25312529
public:
25322530
Regex!Char _regex;
@@ -6045,7 +6043,7 @@ public auto bmatch(R, RegEx)(R input, RegEx re)
60456043

60466044

60476045
enum isReplaceFunctor(alias fun, R) =
6048-
__traits(compiles, (Captures!R c){ fun(c); });
6046+
__traits(compiles, (Captures!R c) { fun(c); });
60496047

60506048
// the lowest level - just stuff replacements into the sink
60516049
private @trusted void replaceCapturesInto(alias output, Sink, R, T)
@@ -6063,7 +6061,7 @@ private @trusted void replaceCapturesInto(alias output, Sink, R, T)
60636061
}
60646062

60656063
// ditto for a range of captures
6066-
private @trusted void replaceMatchesInto(alias output, Sink, R, T)
6064+
private void replaceMatchesInto(alias output, Sink, R, T)
60676065
(ref Sink sink, R input, T matches)
60686066
if(isOutputRange!(Sink, dchar) && isSomeString!R && is(T == RegexMatch!R))
60696067
{
@@ -6082,7 +6080,7 @@ private @trusted void replaceMatchesInto(alias output, Sink, R, T)
60826080
}
60836081

60846082
// a general skeleton of replaceFirst
6085-
private @trusted R replaceFirstWith(alias output, R, RegEx)(R input, RegEx re)
6083+
private R replaceFirstWith(alias output, R, RegEx)(R input, RegEx re)
60866084
if(isSomeString!R && isRegexFor!(RegEx, R))
60876085
{
60886086
auto data = matchFirst(input, re);
@@ -6095,7 +6093,7 @@ private @trusted R replaceFirstWith(alias output, R, RegEx)(R input, RegEx re)
60956093

60966094
// ditto for replaceAll
60976095
// the method parameter allows old API to ride on the back of the new one
6098-
private @trusted R replaceAllWith(alias output,
6096+
private R replaceAllWith(alias output,
60996097
alias method=matchAll, R, RegEx)(R input, RegEx re)
61006098
if(isSomeString!R && isRegexFor!(RegEx, R))
61016099
{
@@ -6128,7 +6126,7 @@ private @trusted R replaceAllWith(alias output,
61286126
assert(replaceFirst("noon", regex("n"), "[$&]") == "[n]oon");
61296127
---
61306128
+/
6131-
public @trusted R replaceFirst(R, C, RegEx)(R input, RegEx re, const(C)[] format)
6129+
public R replaceFirst(R, C, RegEx)(R input, RegEx re, const(C)[] format)
61326130
if(isSomeString!R && is(C : dchar) && isRegexFor!(RegEx, R))
61336131
{
61346132
return replaceFirstWith!((m, sink) => replaceFmt(format, m, sink))(input, re);
@@ -6157,7 +6155,7 @@ public @trusted R replaceFirst(R, C, RegEx)(R input, RegEx re, const(C)[] format
61576155
assert(newList == "#22 out of 46");
61586156
---
61596157
+/
6160-
public @trusted R replaceFirst(alias fun, R, RegEx)(R input, RegEx re)
6158+
public R replaceFirst(alias fun, R, RegEx)(R input, RegEx re)
61616159
if(isSomeString!R && isRegexFor!(RegEx, R))
61626160
{
61636161
return replaceFirstWith!((m, sink) => sink.put(fun(m)))(input, re);
@@ -6386,21 +6384,21 @@ public @trusted void replaceAllInto(alias fun, Sink, R, RegEx)
63866384
The use of this function is $(RED discouraged), please use $(LREF replaceAll)
63876385
or $(LREF replaceFirst) explicitly.
63886386
+/
6389-
public @trusted R replace(alias scheme = match, R, C, RegEx)(R input, RegEx re, const(C)[] format)
6387+
public R replace(alias scheme = match, R, C, RegEx)(R input, RegEx re, const(C)[] format)
63906388
if(isSomeString!R && isRegexFor!(RegEx, R))
63916389
{
63926390
return replaceAllWith!((m, sink) => replaceFmt(format, m, sink), match)(input, re);
63936391
}
63946392

63956393
///ditto
6396-
public @trusted R replace(alias fun, R, RegEx)(R input, RegEx re)
6394+
public R replace(alias fun, R, RegEx)(R input, RegEx re)
63976395
if(isSomeString!R && isRegexFor!(RegEx, R))
63986396
{
63996397
return replaceAllWith!(fun, match)(input, re);
64006398
}
64016399

64026400
//produce replacement string from format using captures for substitution
6403-
public @trusted void replaceFmt(R, Capt, OutR)
6401+
private void replaceFmt(R, Capt, OutR)
64046402
(R format, Capt captures, OutR sink, bool ignoreBadSubs = false)
64056403
if(isOutputRange!(OutR, ElementEncodingType!R[]) &&
64066404
isOutputRange!(OutR, ElementEncodingType!(Capt.String)[]))
@@ -7391,5 +7389,23 @@ unittest
73917389
assert(match("aaaa", re).hit == "aaaa");
73927390
}
73937391

7392+
// bugzilla 10913
7393+
unittest
7394+
{
7395+
@system static string foo(const(char)[] s)
7396+
{
7397+
return s.dup;
7398+
}
7399+
@safe static string bar(const(char)[] s)
7400+
{
7401+
return s.dup;
7402+
}
7403+
() @system {
7404+
replace!((a) => foo(a.hit))("blah", regex(`a`));
7405+
}();
7406+
() @safe {
7407+
replace!((a) => bar(a.hit))("blah", regex(`a`));
7408+
}();
7409+
}
73947410

73957411
}//version(unittest)

0 commit comments

Comments
 (0)