Skip to content
Open
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
12 changes: 6 additions & 6 deletions Content.Tests/DMProject/Tests/Text/Splittext.dm
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
ASSERT(test1 ~= test1_expected)

var/list/test2 = splittext(test_text, " ", 5)
var/test2_expected = list("average","of","1,","2,","3,","4,","5","is:","3")
var/test2_expected = list("The 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")
var/test3_expected = list("The average of 1, 2, 3, 4, 5 is: 3")
ASSERT(test3 ~= test3_expected)

var/list/test4 = splittext(test_text, " ", 10, 20)
var/test4_expected = list("ge","of","1,","2")
var/test4_expected = list("The average","of","1,","2, 3, 4, 5 is: 3")
ASSERT(test4 ~= test4_expected)

var/list/test5 = splittext(test_text, " ", 10, 20, 1)
var/test5_expected = list("ge"," ","of"," ","1,"," ","2")
var/test5_expected = list("The average"," ","of"," ","1,"," ","2, 3, 4, 5 is: 3")
ASSERT(test5 ~= test5_expected)

//it's regex time
Expand All @@ -26,9 +26,9 @@
ASSERT(test6 ~= test6_expected)

var/test7 = splittext(test_text, regex(@"\d"), 5, 30)
var/test7_expected = list("average of ",", ",", ",", ",", "," ")
var/test7_expected = list("The average of ",", ",", ",", ",", "," is: 3")
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"," ")
var/test8_expected = list("The average of ","1",", ","2",", ","3",", ","4",", ","5"," is: 3")
ASSERT(test8 ~= test8_expected)
30 changes: 21 additions & 9 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2844,25 +2844,24 @@ public static DreamValue NativeProc_splicetext_char(NativeProc.Bundle bundle, Dr
[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)) {
if (!bundle.GetArgument(0, "Text").TryGetValueAsString(out var rawtext)) {
return new DreamValue(bundle.ObjectTree.CreateList());
}

int start = 0;
int end = 0;
if(bundle.GetArgument(2, "Start").TryGetValueAsInteger(out start))
if (bundle.GetArgument(2, "Start").TryGetValueAsInteger(out int start))
start -= 1; //1-indexed
if(bundle.GetArgument(3, "End").TryGetValueAsInteger(out end))
if (bundle.GetArgument(3, "End").TryGetValueAsInteger(out int end))
if(end == 0)
end = text.Length;
end = rawtext.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)];
string text = rawtext;
if (start > 0 || end < rawtext.Length)
text = rawtext[Math.Max(start, 0)..Math.Min(end, text.Length)];

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

Expand All @@ -2877,9 +2876,18 @@ public static DreamValue NativeProc_splittext(NativeProc.Bundle bundle, DreamObj
}

values.Add(text.Substring(pos));
if (start > 0)
values[0] = rawtext.Substring(0, start) + values[0];
if (end < rawtext.Length)
values[^1] += rawtext.Substring(end, rawtext.Length - end);
return new DreamValue(bundle.ObjectTree.CreateList(values.ToArray()));
} else {
return new DreamValue(bundle.ObjectTree.CreateList(regexObject.Regex.Split(text)));
var values = regexObject.Regex.Split(text);
if (start > 0)
values[0] = rawtext.Substring(0, start) + values[0];
if (end < rawtext.Length)
values[^1] += rawtext.Substring(end, rawtext.Length - end);
return new DreamValue(bundle.ObjectTree.CreateList(values));
}
} else if (delim.TryGetValueAsString(out var delimiter)) {
string[] splitText;
Expand All @@ -2898,6 +2906,10 @@ public static DreamValue NativeProc_splittext(NativeProc.Bundle bundle, DreamObj
splitText = text.Split(delimiter);
}

if (start > 0)
splitText[0] = rawtext.Substring(0, start) + splitText[0];
if (end < rawtext.Length)
splitText[^1] += rawtext.Substring(end, rawtext.Length - end);
return new DreamValue(bundle.ObjectTree.CreateList(splitText));
} else {
return new DreamValue(bundle.ObjectTree.CreateList());
Expand Down
Loading