From 6227ccef1c939432eb674f7719f0e37c7ccb6e1e Mon Sep 17 00:00:00 2001 From: amylizzle Date: Tue, 17 Dec 2024 16:40:42 +0000 Subject: [PATCH 1/2] goddammit --- .../Tests/Regex/regex_find_replace.dm | 18 ++++++++++++++++++ .../Procs/Native/DreamProcNativeRegex.cs | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 Content.Tests/DMProject/Tests/Regex/regex_find_replace.dm diff --git a/Content.Tests/DMProject/Tests/Regex/regex_find_replace.dm b/Content.Tests/DMProject/Tests/Regex/regex_find_replace.dm new file mode 100644 index 0000000000..dafe74c512 --- /dev/null +++ b/Content.Tests/DMProject/Tests/Regex/regex_find_replace.dm @@ -0,0 +1,18 @@ +var/models = "/obj/cable/brown{\n\ticon_state = \"2-8\"\n\t},\n/obj/cable/brown{\n\ticon_state = \"4-8\"\n\t},\n/turf/simulated/floor/orangeblack,\n/area/station/devzone" +var/result_match = "/obj/cable/brown{\n\ticon_state = \"1\"\n\t},\n/obj/cable/brown{\n\ticon_state = \"2\"\n\t},\n/turf/simulated/floor/orangeblack,\n/area/station/devzone" + +/proc/RunTest() + var/list/originalStrings = list() + var/regex/noStrings = regex(@{"(["])(?:(?=(\\?))\2(.|\n))*?\1"}) + var/stringIndex = 1 + var/found + do + found = noStrings.Find(models, noStrings.next) + if(found) + var indexText = {""[stringIndex]""} + stringIndex++ + var match = copytext(noStrings.match, 2, -1) // Strip quotes + models = noStrings.Replace(models, indexText, found) + originalStrings[indexText] = (match) + while(found) + ASSERT(models == result_match) \ No newline at end of file diff --git a/OpenDreamRuntime/Procs/Native/DreamProcNativeRegex.cs b/OpenDreamRuntime/Procs/Native/DreamProcNativeRegex.cs index a06a823d57..09898b5d1d 100644 --- a/OpenDreamRuntime/Procs/Native/DreamProcNativeRegex.cs +++ b/OpenDreamRuntime/Procs/Native/DreamProcNativeRegex.cs @@ -117,7 +117,7 @@ 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))); + regexInstance.SetVariable("next", new DreamValue(match.Index + Math.Max(replacement.Length, 1) + 1)); } string replaced = regex.Regex.Replace(haystackSubstring, replacement, regex.IsGlobal ? -1 : 1, From c8bc202e3ff6b86ab4998c8803307b78271c3d5c Mon Sep 17 00:00:00 2001 From: Amy <3855802+amylizzle@users.noreply.github.com> Date: Wed, 18 Dec 2024 19:59:35 +0000 Subject: [PATCH 2/2] Update Content.Tests/DMProject/Tests/Regex/regex_find_replace.dm Co-authored-by: wixoa --- Content.Tests/DMProject/Tests/Regex/regex_find_replace.dm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Content.Tests/DMProject/Tests/Regex/regex_find_replace.dm b/Content.Tests/DMProject/Tests/Regex/regex_find_replace.dm index dafe74c512..d5463d7498 100644 --- a/Content.Tests/DMProject/Tests/Regex/regex_find_replace.dm +++ b/Content.Tests/DMProject/Tests/Regex/regex_find_replace.dm @@ -15,4 +15,6 @@ var/result_match = "/obj/cable/brown{\n\ticon_state = \"1\"\n\t},\n/obj/cable/br models = noStrings.Replace(models, indexText, found) originalStrings[indexText] = (match) while(found) - ASSERT(models == result_match) \ No newline at end of file + ASSERT(models == result_match) + ASSERT(originalStrings ~= list("\"1\"" = "2-8", "\"2\"" = "4-8")) + ASSERT(stringIndex == 3) \ No newline at end of file