Skip to content

Commit 9641b76

Browse files
committed
[Bug 16407] Move split by empty check into Exec methods
1 parent 66f7bb6 commit 9641b76

3 files changed

Lines changed: 118 additions & 9 deletions

File tree

engine/src/cmdsm.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,14 +1327,6 @@ void MCArrayOp::exec_ctxt(MCExecContext &ctxt)
13271327
}
13281328
else
13291329
{
1330-
/* Delimiters cannot be empty for split */
1331-
if (MCStringIsEmpty(*t_element_del) ||
1332-
(nil != *t_key_del && MCStringIsEmpty(*t_key_del)))
1333-
{
1334-
ctxt.LegacyThrow(EE_ARRAYOP_BADEXP);
1335-
return;
1336-
}
1337-
13381330
MCAutoStringRef t_string;
13391331
if (!ctxt . ConvertToString(*t_container_value, &t_string))
13401332
return;

engine/src/exec-array.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,14 @@ void MCArraysExecCombineAsSet(MCExecContext& ctxt, MCArrayRef p_array, MCStringR
404404

405405
void MCArraysExecSplit(MCExecContext& ctxt, MCStringRef p_string, MCStringRef p_element_delimiter, MCStringRef p_key_delimiter, MCArrayRef& r_array)
406406
{
407+
/* Cannot split by empty delimiters */
408+
if (MCStringIsEmpty(p_element_delimiter) ||
409+
(nil != p_key_delimiter && MCStringIsEmpty(p_key_delimiter)))
410+
{
411+
ctxt.LegacyThrow(EE_ARRAYOP_BADEXP);
412+
return;
413+
}
414+
407415
if (MCStringSplit(p_string, p_element_delimiter, p_key_delimiter, ctxt . GetStringComparisonType(), r_array))
408416
return;
409417

@@ -415,7 +423,14 @@ void MCArraysExecSplitByColumn(MCExecContext& ctxt, MCStringRef p_string, MCArra
415423
MCStringRef t_row_delim, t_column_delim;
416424
t_row_delim = ctxt . GetRowDelimiter();
417425
t_column_delim = ctxt . GetColumnDelimiter();
418-
426+
427+
/* Cannot split by empty delimiters */
428+
if (MCStringIsEmpty(t_row_delim) || MCStringIsEmpty(t_column_delim))
429+
{
430+
ctxt.LegacyThrow(EE_ARRAYOP_BADEXP);
431+
return;
432+
}
433+
419434
// Output array
420435
MCAutoArrayRef t_array;
421436
if (!MCArrayCreateMutable(&t_array))
@@ -523,6 +538,13 @@ void MCArraysExecSplitByColumn(MCExecContext& ctxt, MCStringRef p_string, MCArra
523538

524539
void MCArraysExecSplitAsSet(MCExecContext& ctxt, MCStringRef p_string, MCStringRef p_element_delimiter, MCArrayRef& r_array)
525540
{
541+
/* Cannot split by empty delimiters */
542+
if (MCStringIsEmpty(p_element_delimiter))
543+
{
544+
ctxt.LegacyThrow(EE_ARRAYOP_BADEXP);
545+
return;
546+
}
547+
526548
// Split the incoming string into its components
527549
MCAutoArrayRef t_keys;
528550
if (!MCStringSplit(p_string, p_element_delimiter, nil, ctxt . GetStringComparisonType(), &t_keys))

tests/lcs/core/array/split.livecodescript

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,98 @@ on TestSplitByEmpty
134134
end try
135135

136136
end TestSplitByEmpty
137+
138+
139+
140+
on TestSplitByColumn
141+
local tResult, tExpected
142+
143+
put "a,b:c,d" into tResult
144+
put empty into tExpected
145+
put "a:c" into tExpected[1]
146+
put "b:d" into tExpected[2]
147+
set the columnDel to ","
148+
set the rowDel to ":"
149+
split tResult by column
150+
151+
TestAssert "split column (native, native, native)", tResult is tExpected
152+
153+
----------
154+
155+
put "a,b:c,d" into tResult
156+
set the columnDel to empty
157+
set the rowDel to ":"
158+
try
159+
split tResult by column
160+
TestAssert "split column (native, empty, native)", false
161+
catch tError
162+
TestAssert "split column (native, empty, native)", true
163+
end try
164+
165+
put "a,b:c,d" into tResult
166+
set the columnDel to ","
167+
set the rowDel to empty
168+
try
169+
split tResult by column
170+
TestAssert "split column (native, native, empty)", false
171+
catch tError
172+
TestAssert "split column (native, native, empty)", true
173+
end try
174+
end TestSplitByColumn
175+
176+
177+
178+
on TestSplitByRow
179+
local tResult, tExpected
180+
181+
put "a,b:c,d" into tResult
182+
put empty into tExpected
183+
put "a,b" into tExpected[1]
184+
put "c,d" into tExpected[2]
185+
set the columnDel to ","
186+
set the rowDel to ":"
187+
split tResult by row
188+
TestAssert "split row (native, native, native)", tResult is tExpected
189+
190+
-- The column delimiter isn't actually used when splitting by row.
191+
put "a,b:c,d" into tResult
192+
set the columnDel to empty
193+
set the rowDel to ":"
194+
split tResult by row
195+
TestAssert "split row (native, empty, native)", tResult is tExpected
196+
197+
----------
198+
199+
put "a,b:c,d" into tResult
200+
set the columnDel to ","
201+
set the rowDel to empty
202+
try
203+
split tResult by row
204+
TestAssert "split row (native, native, empty)", false
205+
catch tError
206+
TestAssert "split row (native, native, empty)", true
207+
end try
208+
end TestSplitByRow
209+
210+
211+
212+
on TestSplitAsSet
213+
local tResult, tExpected, tError
214+
215+
put "a:b" into tResult
216+
put empty into tExpected
217+
put true into tExpected["a"]
218+
put true into tExpected["b"]
219+
split tResult with ":" as set
220+
TestAssert "split set (native, native)", tResult is tExpected
221+
222+
----------
223+
224+
put "a:b" into tResult
225+
try
226+
split tResult with empty as set
227+
TestAssert "split set (native, empty)", false
228+
catch tError
229+
TestAssert "split set (native, empty)", true
230+
end try
231+
end TestSplitAsSet

0 commit comments

Comments
 (0)