Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix next getting set on /regex.Replace() #1561

Merged
merged 2 commits into from
Dec 27, 2023
Merged
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
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
Loading