Skip to content

Commit

Permalink
fix next getting set on replcae (OpenDreamProject#1561)
Browse files Browse the repository at this point in the history
  • Loading branch information
amylizzle authored Dec 27, 2023
1 parent 2f97f7c commit 79a6dfa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Content.Tests/DMProject/Tests/Regex/regex_defer.dm
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
/proc/RunTest()
var/regex/R = regex(@"\w+")
var/result = R.Replace("Hello, there", /proc/regex_callback)
ASSERT(result == "good, there")
ASSERT(result == "good, there")
ASSERT(R.next == 5)
10 changes: 9 additions & 1 deletion OpenDreamRuntime/Procs/Native/DreamProcNativeRegex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ async Task<DreamValue> DoProcReplace(AsyncNativeProc.State state, DreamProc proc
currentHaystack = regex.Regex.Replace(currentHaystack, replacement, 1, currentStart);
currentStart = match.Index + Math.Max(replacement.Length, 1);

if (!regex.IsGlobal)
if (!regex.IsGlobal){
regex.SetVariable("next", new DreamValue(currentStart + 1));
break;
}
}

var replaced = currentHaystack;
Expand All @@ -108,6 +110,12 @@ async Task<DreamValue> DoProcReplace(AsyncNativeProc.State state, DreamProc proc
}

DreamValue DoTextReplace(string replacement) {
if(!regex.IsGlobal) {
var match = regex.Regex.Match(haystackString, Math.Clamp(start - 1, 0, haystackSubstring.Length));
if (!match.Success) return new DreamValue(haystackString);
regexInstance.SetVariable("next", new DreamValue(match.Index + Math.Max(replacement.Length, 1)));
}

string replaced = regex.Regex.Replace(haystackSubstring, replacement, regex.IsGlobal ? -1 : 1,
Math.Clamp(start - 1, 0, haystackSubstring.Length));

Expand Down

0 comments on commit 79a6dfa

Please sign in to comment.