diff --git a/Content.Tests/DMProject/Tests/Text/Splittext.dm b/Content.Tests/DMProject/Tests/Text/Splittext.dm index da2d898418..be415a4346 100644 --- a/Content.Tests/DMProject/Tests/Text/Splittext.dm +++ b/Content.Tests/DMProject/Tests/Text/Splittext.dm @@ -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 @@ -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) \ No newline at end of file diff --git a/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs b/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs index 95b2a3e81b..377d58b5b3 100644 --- a/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs +++ b/OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs @@ -2554,25 +2554,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 @@ -2587,9 +2586,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; @@ -2608,6 +2616,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());