Skip to content

Commit add637f

Browse files
authored
Update splittext() to 515 behaviour (#2437)
1 parent 783dfc4 commit add637f

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

Content.Tests/DMProject/Tests/Text/Splittext.dm

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
ASSERT(test1 ~= test1_expected)
66

77
var/list/test2 = splittext(test_text, " ", 5)
8-
var/test2_expected = list("average","of","1,","2,","3,","4,","5","is:","3")
8+
var/test2_expected = list("The average","of","1,","2,","3,","4,","5","is:","3")
99
ASSERT(test2 ~= test2_expected)
1010

1111
var/list/test3 = splittext(test_text, " ", 5, 10)
12-
var/test3_expected = list("avera")
12+
var/test3_expected = list("The average of 1, 2, 3, 4, 5 is: 3")
1313
ASSERT(test3 ~= test3_expected)
1414

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

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

2323
//it's regex time
@@ -26,9 +26,9 @@
2626
ASSERT(test6 ~= test6_expected)
2727

2828
var/test7 = splittext(test_text, regex(@"\d"), 5, 30)
29-
var/test7_expected = list("average of ",", ",", ",", ",", "," ")
29+
var/test7_expected = list("The average of ",", ",", ",", ",", "," is: 3")
3030
ASSERT(test7 ~= test7_expected)
3131

3232
var/test8 = splittext(test_text, regex(@"\d"), 5, 30, 1)
33-
var/test8_expected = list("average of ","1",", ","2",", ","3",", ","4",", ","5"," ")
33+
var/test8_expected = list("The average of ","1",", ","2",", ","3",", ","4",", ","5"," is: 3")
3434
ASSERT(test8 ~= test8_expected)

OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,25 +2554,24 @@ public static DreamValue NativeProc_splicetext_char(NativeProc.Bundle bundle, Dr
25542554
[DreamProcParameter("End", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
25552555
[DreamProcParameter("include_delimiters", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
25562556
public static DreamValue NativeProc_splittext(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
2557-
if (!bundle.GetArgument(0, "Text").TryGetValueAsString(out var text)) {
2557+
if (!bundle.GetArgument(0, "Text").TryGetValueAsString(out var rawtext)) {
25582558
return new DreamValue(bundle.ObjectTree.CreateList());
25592559
}
25602560

2561-
int start = 0;
2562-
int end = 0;
2563-
if(bundle.GetArgument(2, "Start").TryGetValueAsInteger(out start))
2561+
if (bundle.GetArgument(2, "Start").TryGetValueAsInteger(out int start))
25642562
start -= 1; //1-indexed
2565-
if(bundle.GetArgument(3, "End").TryGetValueAsInteger(out end))
2563+
if (bundle.GetArgument(3, "End").TryGetValueAsInteger(out int end))
25662564
if(end == 0)
2567-
end = text.Length;
2565+
end = rawtext.Length;
25682566
else
25692567
end -= 1; //1-indexed
25702568
bool includeDelimiters = false;
25712569
if(bundle.GetArgument(4, "include_delimiters").TryGetValueAsInteger(out var includeDelimitersInt))
25722570
includeDelimiters = includeDelimitersInt != 0; //idk why BYOND doesn't just use truthiness, but it doesn't, so...
25732571

2574-
if(start > 0 || end < text.Length)
2575-
text = text[Math.Max(start,0)..Math.Min(end, text.Length)];
2572+
string text = rawtext;
2573+
if (start > 0 || end < rawtext.Length)
2574+
text = rawtext[Math.Max(start, 0)..Math.Min(end, text.Length)];
25762575

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

@@ -2587,9 +2586,18 @@ public static DreamValue NativeProc_splittext(NativeProc.Bundle bundle, DreamObj
25872586
}
25882587

25892588
values.Add(text.Substring(pos));
2589+
if (start > 0)
2590+
values[0] = rawtext.Substring(0, start) + values[0];
2591+
if (end < rawtext.Length)
2592+
values[^1] += rawtext.Substring(end, rawtext.Length - end);
25902593
return new DreamValue(bundle.ObjectTree.CreateList(values.ToArray()));
25912594
} else {
2592-
return new DreamValue(bundle.ObjectTree.CreateList(regexObject.Regex.Split(text)));
2595+
var values = regexObject.Regex.Split(text);
2596+
if (start > 0)
2597+
values[0] = rawtext.Substring(0, start) + values[0];
2598+
if (end < rawtext.Length)
2599+
values[^1] += rawtext.Substring(end, rawtext.Length - end);
2600+
return new DreamValue(bundle.ObjectTree.CreateList(values));
25932601
}
25942602
} else if (delim.TryGetValueAsString(out var delimiter)) {
25952603
string[] splitText;
@@ -2608,6 +2616,10 @@ public static DreamValue NativeProc_splittext(NativeProc.Bundle bundle, DreamObj
26082616
splitText = text.Split(delimiter);
26092617
}
26102618

2619+
if (start > 0)
2620+
splitText[0] = rawtext.Substring(0, start) + splitText[0];
2621+
if (end < rawtext.Length)
2622+
splitText[^1] += rawtext.Substring(end, rawtext.Length - end);
26112623
return new DreamValue(bundle.ObjectTree.CreateList(splitText));
26122624
} else {
26132625
return new DreamValue(bundle.ObjectTree.CreateList());

0 commit comments

Comments
 (0)