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

[HELP WANTED] Maybe(?) fixes(?) regex #1793

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
5 changes: 3 additions & 2 deletions DMCompiler/DMStandard/Types/Regex.dm
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
src.flags = flags

proc/Find(haystack, start = 1, end = 0)
return findtext(text, src, start, end)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runtime replaces this proc with the native proc found in DreamProcNativeRegex.cs so this doesn't do anything.


proc/Find_char(haystack, start = 1, end = 0)
set opendream_unimplemented = TRUE

proc/Replace(haystack, replacement, start = 1, end = 0)
return replacetext(text, src, replacement, start, end)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with this proc


proc/Replace_char(haystack, replacement, start = 1, end = 0)
set opendream_unimplemented = TRUE
return haystack
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed?


proc/regex(pattern, flags)
28 changes: 17 additions & 11 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeRegex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DreamValue> 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);
Comment on lines +15 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be verified that these are both treated as 0 for non-integer values.


return await RegexFind(state, state.Src, haystack, start, end);
}

public static async Task<DreamValue> RegexFind(AsyncNativeProc.State state, DreamObject regexInstance, DreamValue haystack, int start, int end) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why async? I don't see anything in here that is asynchronous.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copied this from RegexReplace, not gonna lie!

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);

Expand All @@ -35,7 +41,7 @@ internal static class DreamProcNativeRegex {
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));
Expand Down Expand Up @@ -140,17 +146,17 @@ internal static class DreamProcNativeRegex {
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;
}
}
}
Expand Down
32 changes: 12 additions & 20 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -714,27 +714,25 @@ internal static class DreamProcNativeRoot {
[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<DreamValue> 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<DreamObjectRegex>(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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BYOND returns different values for different combinations of invalid arguments. Are those still replicated?


if (start > text.Length || start == 0) return new DreamValue(0);

Expand All @@ -750,12 +748,6 @@ internal static class DreamProcNativeRoot {
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
}
Expand Down
Loading