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

Fully implement splittext and add test #1560

Merged
merged 10 commits into from
Jan 7, 2024
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
34 changes: 34 additions & 0 deletions Content.Tests/DMProject/Tests/Text/Splittext.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/proc/RunTest()
var/test_text = "The average of 1, 2, 3, 4, 5 is: 3"
var/list/test1 = splittext(test_text, " ")
var/list/test1_expected = list("The","average","of","1,","2,","3,","4,","5","is:","3")
ASSERT(test1 ~= test1_expected)

var/list/test2 = splittext(test_text, " ", 5)
var/test2_expected = list("average","of","1,","2,","3,","4,","5","is:","3")
ASSERT(test2 ~= test2_expected)

var/list/test3 = splittext(test_text, " ", 5, 10)
var/test3_expected = list("avera")
ASSERT(test3 ~= test3_expected)

var/list/test4 = splittext(test_text, " ", 10, 20)
var/test4_expected = list("ge","of","1,","2")
ASSERT(test4 ~= test4_expected)

var/list/test5 = splittext(test_text, " ", 10, 20, 1)
var/test5_expected = list("ge"," ","of"," ","1,"," ","2")
ASSERT(test5 ~= test5_expected)

//it's regex time
var/test6 = splittext(test_text, regex(@"\d"))
var/test6_expected = list("The average of ",", ",", ",", ",", "," is: ","")
ASSERT(test6 ~= test6_expected)

var/test7 = splittext(test_text, regex(@"\d"), 5, 30)
var/test7_expected = list("average of ",", ",", ",", ",", "," ")
ASSERT(test7 ~= test7_expected)

var/test8 = splittext(test_text, regex(@"\d"), 5, 30, 1)
var/test8_expected = list("average of ","1",", ","2",", ","3",", ","4",", ","5"," ")
ASSERT(test8 ~= test8_expected)
61 changes: 52 additions & 9 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2361,22 +2361,65 @@ public static DreamValue NativeProc_splicetext_char(NativeProc.Bundle bundle, Dr
[DreamProc("splittext")]
[DreamProcParameter("Text", Type = DreamValueTypeFlag.String)]
[DreamProcParameter("Delimiter", Type = DreamValueTypeFlag.String)]
[DreamProcParameter("Start", Type = DreamValueTypeFlag.Float, DefaultValue = 1)]
[DreamProcParameter("End", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
[DreamProcParameter("include_delimiters", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
public static DreamValue NativeProc_splittext(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
if (!bundle.GetArgument(0, "Text").TryGetValueAsString(out var text)) {
return new DreamValue(bundle.ObjectTree.CreateList());
}

var arg2 = bundle.GetArgument(1, "Delimiter");
if (!arg2.TryGetValueAsString(out var delimiter)) {
if (!arg2.Equals(DreamValue.Null)) {
return new DreamValue(bundle.ObjectTree.CreateList());
int start = 0;
int end = 0;
if(bundle.GetArgument(2, "Start").TryGetValueAsInteger(out start))
start -= 1; //1-indexed
if(bundle.GetArgument(3, "End").TryGetValueAsInteger(out end))
if(end == 0)
end = text.Length;
else
end -= 1; //1-indexed
bool includeDelimiters = false;
if(bundle.GetArgument(4, "include_delimiters").TryGetValueAsInteger(out var includeDelimitersInt))
includeDelimiters = includeDelimitersInt != 0; //idk why BYOND doesn't just use truthiness, but it doesn't, so...

if(start > 0 || end < text.Length)
text = text[Math.Max(start,0)..Math.Min(end, text.Length)];

var delim = bundle.GetArgument(1, "Delimiter"); //can either be a regex or string

if (delim.TryGetValueAsDreamObject<DreamObjectRegex>(out var regexObject)) {
if(includeDelimiters) {
var values = new List<string>();
int pos = 0;
foreach (Match m in regexObject.Regex.Matches(text)) {
values.Add(text.Substring(pos, m.Index - pos));
values.Add(m.Value);
pos = m.Index + m.Length;
}
values.Add(text.Substring(pos));
return new DreamValue(bundle.ObjectTree.CreateList(values.ToArray()));
} else {
return new DreamValue(bundle.ObjectTree.CreateList(regexObject.Regex.Split(text)));
}
} else if (delim.TryGetValueAsString(out var delimiter)) {
string[] splitText;
if(includeDelimiters) {
//basically split on delimeter, and then add the delimiter back in after each split (except the last one)
splitText= text.Split(delimiter);
string[] longerSplitText = new string[splitText.Length * 2 - 1];
for(int i = 0; i < splitText.Length; i++) {
longerSplitText[i * 2] = splitText[i];
if(i < splitText.Length - 1)
longerSplitText[i * 2 + 1] = delimiter;
}
splitText = longerSplitText;
} else {
splitText = text.Split(delimiter);
}
return new DreamValue(bundle.ObjectTree.CreateList(splitText));
} else {
return new DreamValue(bundle.ObjectTree.CreateList());
}

string[] splitText = text.Split(delimiter);
DreamList list = bundle.ObjectTree.CreateList(splitText);

return new DreamValue(list);
}

private static void OutputToStatPanel(DreamManager dreamManager, DreamConnection connection, DreamValue name, DreamValue value) {
Expand Down
Loading