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

Commit 46d92ea

Browse files
committed
[[ StdMlc ]] Added 'repeat for each char' custom iterator syntax to char module.
1 parent 7831c27 commit 46d92ea

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

libscript/src/char.mlc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public foreign handler MCCharEvalOffsetOfCharsAfter(in IsFirst as bool, in Needl
1818

1919
public foreign handler MCCharEvalNewlineCharacter(out Value as string) as undefined binds to "<builtin>"
2020

21+
public foreign handler MCCharRepeatForEachChar(inout Iterator as optional pointer, out Iterand as string, in Container as string) as bool binds to "<builtin>"
22+
2123
--
2224

2325
/*
@@ -220,4 +222,10 @@ begin
220222
MCCharEvalNewlineCharacter(output)
221223
end syntax
222224

225+
syntax RepeatForEachChar is iterator
226+
"char" <Iterand: Expression>
227+
begin
228+
MCCharRepeatForEachChar(iterator, Iterand, container)
229+
end syntax
230+
223231
end module

libscript/src/module-char.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,33 @@ extern "C" void MCCharEvalNewlineCharacter(MCStringRef& r_output)
156156
MCStringFormat(r_output, "\n");
157157
}
158158

159+
// Iterate syntax methods have special calling convention at the moment:
160+
//
161+
// Post assignment of out / inout variables only occurs if the method returns true.
162+
// If the method returns false, then it means iteration has finished *not* that an
163+
// error has been thrown.
164+
//
165+
// This means that the iterand out binding will not be updated on the final test
166+
// of the loop which means that:
167+
// repeat for each char tChar in tVar
168+
// end repeat
169+
// Will result in tChar containing the value it had at the point of end repeat.
170+
extern "C" bool MCCharRepeatForEachChar(void*& x_iterator, MCStringRef& r_iterand, MCStringRef p_string)
171+
{
172+
uindex_t t_offset;
173+
t_offset = (uindex_t)x_iterator;
174+
175+
if (t_offset == MCStringGetLength(p_string))
176+
return false;
177+
178+
if (!MCStringCopySubstring(p_string, MCRangeMake(t_offset, 1), r_iterand))
179+
return false;
180+
181+
x_iterator = (void *)(t_offset + 1);
182+
183+
return true;
184+
}
185+
159186
////////////////////////////////////////////////////////////////////////////////////////////////////
160187

161188
#ifdef _TEST

0 commit comments

Comments
 (0)