From 57246a10a6449c0bf8be13c39f0ae3392d25147c Mon Sep 17 00:00:00 2001 From: Kapu1178 <75460809+Kapu1178@users.noreply.github.com> Date: Thu, 16 May 2024 01:39:32 -0400 Subject: [PATCH 1/3] tries to fix regex --- DMCompiler/DMStandard/Types/Regex.dm | 9 +++--- .../Procs/Native/DreamProcNativeRegex.cs | 28 +++++++++------- .../Procs/Native/DreamProcNativeRoot.cs | 32 +++++++------------ 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/DMCompiler/DMStandard/Types/Regex.dm b/DMCompiler/DMStandard/Types/Regex.dm index aa1afbe215..f7e662477f 100644 --- a/DMCompiler/DMStandard/Types/Regex.dm +++ b/DMCompiler/DMStandard/Types/Regex.dm @@ -20,14 +20,15 @@ src.flags = flags proc/Find(haystack, start = 1, end = 0) + return findtext(text, src, start, end) proc/Find_char(haystack, start = 1, end = 0) - set opendream_unimplemented = TRUE - + set opendream_unimplimented = TRUE + proc/Replace(haystack, replacement, start = 1, end = 0) + return replacetext(text, src, replacement, start, end) proc/Replace_char(haystack, replacement, start = 1, end = 0) - set opendream_unimplemented = TRUE - return haystack + set opendream_unimplimented = TRUE proc/regex(pattern, flags) diff --git a/OpenDreamRuntime/Procs/Native/DreamProcNativeRegex.cs b/OpenDreamRuntime/Procs/Native/DreamProcNativeRegex.cs index 9e3b56062d..5013a02d73 100644 --- a/OpenDreamRuntime/Procs/Native/DreamProcNativeRegex.cs +++ b/OpenDreamRuntime/Procs/Native/DreamProcNativeRegex.cs @@ -7,18 +7,24 @@ namespace OpenDreamRuntime.Procs.Native { internal static class DreamProcNativeRegex { [DreamProc("Find")] [DreamProcParameter("haystack", Type = DreamValue.DreamValueTypeFlag.String)] - [DreamProcParameter("start", Type = DreamValue.DreamValueTypeFlag.Float | DreamValue.DreamValueTypeFlag.DreamObject)] // BYOND docs say these are uppercase, they're not + [DreamProcParameter("start", DefaultValue = 1, Type = DreamValue.DreamValueTypeFlag.Float)] // BYOND docs say these are uppercase, they're not [DreamProcParameter("end", DefaultValue = 0, Type = DreamValue.DreamValueTypeFlag.Float)] - public static DreamValue NativeProc_Find(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) { - DreamObjectRegex dreamRegex = (DreamObjectRegex)src!; - DreamValue haystack = bundle.GetArgument(0, "haystack"); + public static async Task NativeProc_Find(AsyncNativeProc.State state) { + DreamValue haystack = state.GetArgument(0, "haystack"); + state.GetArgument(1, "start").TryGetValueAsInteger(out int start); + state.GetArgument(2, "end").TryGetValueAsInteger(out int end); + + return await RegexFind(state, state.Src, haystack, start, end); + } + + public static async Task RegexFind(AsyncNativeProc.State state, DreamObject regexInstance, DreamValue haystack, int start, int end) { + DreamObjectRegex dreamRegex = (DreamObjectRegex)regexInstance; if (!haystack.TryGetValueAsString(out var haystackString)) { haystackString = string.Empty; } - int next = GetNext(src!, bundle.GetArgument(1, "start"), dreamRegex.IsGlobal, haystackString); - int end = bundle.GetArgument(2, "end").GetValueAsInteger(); + int next = GetNext(dreamRegex, start, dreamRegex.IsGlobal, haystackString); dreamRegex.SetVariable("text", haystack); @@ -35,7 +41,7 @@ public static DreamValue NativeProc_Find(NativeProc.Bundle bundle, DreamObject? dreamRegex.SetVariable("index", new DreamValue(match.Index + 1)); dreamRegex.SetVariable("match", new DreamValue(match.Value)); if (match.Groups.Count > 0) { - DreamList groupList = bundle.ObjectTree.CreateList(match.Groups.Count); + DreamList groupList = state.ObjectTree.CreateList(match.Groups.Count); for (int i = 1; i < match.Groups.Count; i++) { groupList.AddValue(new DreamValue(match.Groups[i].Value)); @@ -140,17 +146,17 @@ public static async Task NativeProc_Replace(AsyncNativeProc.State st return await RegexReplace(state, state.Src, haystack, replacement, start, end); } - private static int GetNext(DreamObject regexInstance, DreamValue startParam, bool isGlobal, string haystackString) { - if (startParam.IsNull) { + private static int GetNext(DreamObject regexInstance, int startParam, bool isGlobal, string haystackString) { + if (isGlobal) { if (isGlobal && regexInstance.GetVariable("text").TryGetValueAsString(out string? lastHaystack) && lastHaystack == haystackString) { DreamValue nextVar = regexInstance.GetVariable("next"); - return (!nextVar.IsNull) ? nextVar.GetValueAsInteger() : 1; + return (!nextVar.IsNull) ? nextVar.MustGetValueAsInteger() : 1; } else { return 1; } } else { - return startParam.GetValueAsInteger(); + return startParam; } } } diff --git a/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs b/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs index d3c85a6cf4..cd1996900a 100644 --- a/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs +++ b/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs @@ -714,27 +714,25 @@ public static DreamValue NativeProc_filter(NativeProc.Bundle bundle, DreamObject [DreamProcParameter("Needle", Type = DreamValueTypeFlag.String)] [DreamProcParameter("Start", Type = DreamValueTypeFlag.Float, DefaultValue = 1)] [DreamProcParameter("End", Type = DreamValueTypeFlag.Float, DefaultValue = 0)] - public static DreamValue NativeProc_findtext(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) { + public static async Task NativeProc_findtext(AsyncNativeProc.State state) { // TODO This is for handling nulls, check if it works right for other bad types + DreamValue haystack = state.GetArgument(0, "Haystack"); + int start = state.GetArgument(2, "Start").MustGetValueAsInteger(); //1-indexed + int end = state.GetArgument(3, "End").MustGetValueAsInteger(); //1-indexed int failCount = 0; - if (!bundle.GetArgument(0, "Haystack").TryGetValueAsString(out var text)) { - failCount++; - } - DreamValue needleArg = bundle.GetArgument(1, "Needle"); - DreamObjectRegex? regex = null; - if (!needleArg.TryGetValueAsString(out var needle)) { - if(!needleArg.TryGetValueAsDreamObject(out regex)) { - failCount++; - } + if (!haystack.TryGetValueAsString(out var text)) { + failCount++; } - if (failCount > 0) { - return new DreamValue(failCount == 2 ? 1 : 0); + DreamValue needleArg = state.GetArgument(1, "Needle"); + if (needleArg.TryGetValueAsDreamObject(out var regexObject)) { + // According to the docs, this is the same as /regex.Replace() + return await DreamProcNativeRegex.RegexFind(state, regexObject, haystack, start, end); } - int start = bundle.GetArgument(2, "Start").MustGetValueAsInteger(); //1-indexed - int end = bundle.GetArgument(3, "End").MustGetValueAsInteger(); //1-indexed + if(!needleArg.TryGetValueAsString(out var needle)) + return DreamValue.True; if (start > text.Length || start == 0) return new DreamValue(0); @@ -750,12 +748,6 @@ public static DreamValue NativeProc_findtext(NativeProc.Bundle bundle, DreamObje end = text.Length + 1; } - if (regex is not null) { - Match match = regex.Regex.Match(text, start - 1, end - start); - - return match.Success ? new DreamValue(match.Index + 1) : new DreamValue(0); - } - int needleIndex = text.IndexOf(needle, start - 1, end - start, StringComparison.OrdinalIgnoreCase); return new DreamValue(needleIndex + 1); //1-indexed } From a828ef00c87cab53305ed641581de3afaf57df13 Mon Sep 17 00:00:00 2001 From: Kapu1178 <75460809+Kapu1178@users.noreply.github.com> Date: Thu, 16 May 2024 01:51:40 -0400 Subject: [PATCH 2/3] Update DMCompiler/DMStandard/Types/Regex.dm --- DMCompiler/DMStandard/Types/Regex.dm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DMCompiler/DMStandard/Types/Regex.dm b/DMCompiler/DMStandard/Types/Regex.dm index f7e662477f..2a4890cb29 100644 --- a/DMCompiler/DMStandard/Types/Regex.dm +++ b/DMCompiler/DMStandard/Types/Regex.dm @@ -29,6 +29,6 @@ return replacetext(text, src, replacement, start, end) proc/Replace_char(haystack, replacement, start = 1, end = 0) - set opendream_unimplimented = TRUE + set opendream_unimplemented = TRUE proc/regex(pattern, flags) From ea216fed8b659548841f81f207c3882e44bdd13c Mon Sep 17 00:00:00 2001 From: Kapu1178 <75460809+Kapu1178@users.noreply.github.com> Date: Thu, 16 May 2024 01:51:44 -0400 Subject: [PATCH 3/3] Update DMCompiler/DMStandard/Types/Regex.dm --- DMCompiler/DMStandard/Types/Regex.dm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DMCompiler/DMStandard/Types/Regex.dm b/DMCompiler/DMStandard/Types/Regex.dm index 2a4890cb29..8da1982039 100644 --- a/DMCompiler/DMStandard/Types/Regex.dm +++ b/DMCompiler/DMStandard/Types/Regex.dm @@ -23,7 +23,7 @@ return findtext(text, src, start, end) proc/Find_char(haystack, start = 1, end = 0) - set opendream_unimplimented = TRUE + set opendream_unimplemented = TRUE proc/Replace(haystack, replacement, start = 1, end = 0) return replacetext(text, src, replacement, start, end)