Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit dcf44d0

Browse files
committed
[13266] Used MCStringSplitByDelimiter where possible
1 parent 606d57d commit dcf44d0

File tree

2 files changed

+21
-36
lines changed

2 files changed

+21
-36
lines changed

engine/src/button.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,25 +2728,20 @@ void MCButton::openmenu(Boolean grab)
27282728

27292729
uindex_t t_offset = 0;
27302730
uindex_t t_new_offset = 0;
2731-
for (uindex_t i = 0; i < t_chosen_option; i++)
2732-
{
2733-
if (i != 0)
2734-
t_offset = t_new_offset + 1;
2735-
/* UNCHECKED */ MCStringFirstIndexOfChar(t_menustring, '\n', t_offset, kMCStringOptionCompareExact, t_new_offset);
2736-
}
2731+
bool t_success = true;
27372732

2738-
// if the user selects the last option, t_new_offset will not be updated, as there is no \n after the last option
2739-
// so update t_new_offset here if this is the case
2740-
if (t_new_offset == t_offset - 1)
2741-
t_new_offset = MCStringGetLength(t_menustring) + 1;
2733+
MCAutoProperListRef t_options;
2734+
2735+
if (t_success)
2736+
t_success = MCStringSplitByDelimiter(t_menustring, kMCLineEndString, kMCStringOptionCompareExact, &t_options);
2737+
2738+
MCStringRef t_label = static_cast<MCStringRef>(MCProperListFetchElementAtIndex(*t_options, t_chosen_option - 1));
2739+
2740+
2741+
MCValueAssign(label, t_label);
27422742

2743-
MCAutoStringRef t_label;
2744-
/* UNCHECKED */ MCStringCopySubstring(t_menustring,
2745-
MCRangeMakeMinMax(t_offset, t_new_offset),
2746-
&t_label);
2747-
MCValueAssign(label, *t_label);
27482743
flags |= F_LABEL;
2749-
handlemenupick(*t_label, nil);
2744+
handlemenupick(t_label, nil);
27502745
}
27512746
return;
27522747
}

engine/src/mblandroidmisc.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -269,38 +269,33 @@ bool MCParseParameters(MCParameter*& p_parameters, const char *p_format, ...)
269269

270270
bool MCSystemPick(MCStringRef p_options, bool p_use_checkmark, uint32_t p_initial_index, uint32_t& r_chosen_index, MCRectangle p_button_rect)
271271
{
272-
// Split the string on new lines
273-
MCAutoArrayRef t_options_arrayref;
274-
uindex_t noptions = 0;
275272
r_chosen_index = 0;
276273

277274
MCStringRef *t_options_array = nil;
278275
MCPickList *t_pick_list = nil;
279276

277+
MCAutoProperListRef t_options_list;
278+
uindex_t t_count = 0;
279+
280+
// Split the string on new lines
280281
bool t_success = true;
281-
t_success = MCStringSplit(p_options, MCSTR("\n"), nil, kMCCompareExact, &t_options_arrayref);
282+
t_success = MCStringSplitByDelimiter(p_options, kMCLineEndString, kMCStringOptionCompareExact, &t_options_list);
282283

283284
if (t_success)
284285
{
285-
noptions = MCArrayGetCount(*t_options_arrayref);
286+
t_count = MCProperListGetLength(*t_options_list);
287+
t_success = MCMemoryNewArray(t_count, t_options_array);
286288

287-
t_success = MCMemoryNewArray(noptions, t_options_array);
288-
289-
for (uindex_t i = 0 ; t_success && i < noptions ; i++)
290-
{
291-
// Note: 't_options' is an array of strings
292-
MCValueRef t_optionval = nil;
293-
t_success = MCArrayFetchValueAtIndex(*t_options_arrayref, i + 1, t_optionval);
294-
t_options_array[i] = MCValueRetain((MCStringRef)t_optionval);
295-
}
289+
for (uindex_t i = 0; t_success && i < t_count; i++)
290+
t_options_array[i] = static_cast<MCStringRef>(MCProperListFetchElementAtIndex(*t_options_list, i));
296291
}
297292

298293
if (t_success)
299294
{
300295
t_success = MCMemoryNew(t_pick_list);
301296

302297
t_pick_list -> options = t_options_array;
303-
t_pick_list -> option_count = noptions;
298+
t_pick_list -> option_count = t_count;
304299
t_pick_list -> initial = p_initial_index;
305300
}
306301

@@ -319,11 +314,6 @@ bool MCSystemPick(MCStringRef p_options, bool p_use_checkmark, uint32_t p_initia
319314
if (t_success)
320315
{
321316
// cleanup
322-
for (uindex_t i = 0; i < noptions; i++)
323-
{
324-
MCValueRelease(t_options_array[i]);
325-
}
326-
327317
MCMemoryDeleteArray(t_options_array);
328318
MCMemoryDelete(t_pick_list);
329319
}

0 commit comments

Comments
 (0)