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

Commit e885b82

Browse files
author
runrevali
committed
[[ StdMlc ]] Implement repeat for each byte, add repeat for each examples
1 parent 9e6be6e commit e885b82

5 files changed

Lines changed: 60 additions & 97 deletions

File tree

libscript/src/byte.mlc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public foreign handler StoreByteRangeOf(in Value as data, in Start as index, in
2525
public foreign handler StoreBeforeByteOf(in Value as data, in Index as index, inout Target as data) as undefined binds to "<builtin>"
2626
public foreign handler StoreAfterByteOf(in Value as data, in Index as index, inout Target as data) as undefined binds to "<builtin>"
2727

28+
public foreign handler RepeatForEachByte(inout Iterator as optional pointer, out Iterand as data, in Container as data) as bool binds to "<builtin>"
29+
2830
--
2931

3032
/*
@@ -219,6 +221,12 @@ begin
219221
StoreByteRangeOf(input, Start, Finish, Target)
220222
end syntax
221223

224+
syntax RepeatForEachByte is iterator
225+
"byte" <Iterand: Expression>
226+
begin
227+
MCByteRepeatForEachByte(iterator, Iterand, container)
228+
end syntax
229+
222230
--
223231

224232
end module

libscript/src/char.mlc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,27 @@ begin
301301
MCCharEvalNewlineCharacter(output)
302302
end syntax
303303

304+
/*
305+
Summary: Repeat over the elements of a list
306+
Iterand: An expression of the form 'tVar in tList'
307+
308+
Example:
309+
variable tString as string
310+
put "stressed" into tString
311+
312+
variable tReversed as string
313+
variable tChar as string
314+
put "" into tReversed
315+
repeat for each char tChar in tString
316+
put tChar before tReversed
317+
end repeat
318+
319+
// tReversed is "desserts"
320+
321+
Description:
322+
The repeat for each form is more efficient that the alternative formation using repeat **tCount** times.
323+
*/
324+
304325
syntax RepeatForEachChar is iterator
305326
"char" <Iterand: Expression>
306327
begin

libscript/src/list.mlc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,28 @@ begin
460460
MCListEvalEmpty(output)
461461
end syntax
462462

463+
/*
464+
Summary: Repeat over the elements of a list
465+
Iterand: An expression of the form 'tVar in tList'
466+
467+
Example:
468+
variable tList as list
469+
put [1, 1, 2, 3, 5, 8, 13] into tList
470+
471+
variable tRelist as list
472+
variable tElement as any
473+
put the empty list into tRelist
474+
475+
repeat for each element tElement in tList
476+
push tElement onto tRelist
477+
end repeat
478+
479+
// tRelist is the same as tList
480+
481+
Description:
482+
The repeat for each form is more efficient that the alternative formation using repeat **tCount** times.
483+
*/
484+
463485
syntax RepeatForEachElement is iterator
464486
"element" <Iterand: Expression>
465487
begin

libscript/src/module-byte.cpp

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -145,56 +145,20 @@ extern "C" void MCByteStoreByteOf(MCDataRef p_value, index_t p_index, MCDataRef&
145145
MCByteStoreByteRangeOf(p_value, p_index, p_index, x_target);
146146
}
147147

148-
extern "C" void MCByteStoreAfterByteOf(MCDataRef p_value, index_t p_index, MCDataRef& x_target)
148+
extern "C" bool MCByteRepeatForEachByte(void*& x_iterator, MCDataRef& r_iterand, MCDataRef p_data)
149149
{
150-
uindex_t t_start, t_count;
151-
MCChunkGetExtentsOfByteChunkByExpression(x_target, p_index, t_start, t_count);
152-
153-
if (t_count == 0)
154-
return;
155-
156-
if (t_start + t_count > MCDataGetLength(x_target))
157-
return;
158-
159-
t_start += t_count;
160-
161-
MCAutoDataRef t_data;
162-
if (!MCDataMutableCopy(x_target, &t_data))
163-
return;
164-
165-
if (!MCDataInsert(*t_data, t_start, p_value))
166-
return;
167-
168-
MCAutoDataRef t_new_data;
169-
if (!MCDataCopy(*t_data, &t_new_data))
170-
return;
171-
172-
MCValueAssign(x_target, *t_new_data);
173-
}
174-
175-
extern "C" void MCByteStoreBeforeByteOf(MCDataRef p_value, index_t p_index, MCDataRef& x_target)
176-
{
177-
uindex_t t_start, t_count;
178-
MCChunkGetExtentsOfByteChunkByRange(x_target, p_index, p_index, t_start, t_count);
179-
180-
if (t_count == 0)
181-
return;
182-
183-
if (t_start + t_count > MCDataGetLength(x_target))
184-
return;
150+
uindex_t t_offset;
151+
t_offset = (uindex_t)x_iterator;
185152

186-
MCAutoDataRef t_data;
187-
if (!MCDataMutableCopy(x_target, &t_data))
188-
return;
153+
if (t_offset == MCDataGetLength(p_data))
154+
return false;
189155

190-
if (!MCDataInsert(*t_data, t_start, p_value))
191-
return;
156+
if (!MCDataCopyRange(p_data, MCRangeMake(t_offset, 1), r_iterand))
157+
return false;
192158

193-
MCAutoDataRef t_new_data;
194-
if (!MCDataCopy(*t_data, &t_new_data))
195-
return;
159+
x_iterator = (void *)(t_offset + 1);
196160

197-
MCValueAssign(x_target, *t_new_data);
161+
return true;
198162
}
199163

200164
////////////////////////////////////////////////////////////////////////////////////////////////////

libscript/src/module-char.cpp

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -83,58 +83,6 @@ extern "C" void MCCharStoreCharOf(MCStringRef p_value, index_t p_index, MCString
8383
MCCharStoreCharRangeOf(p_value, p_index, p_index, x_target);
8484
}
8585

86-
extern "C" void MCCharStoreAfterCharOf(MCStringRef p_value, index_t p_index, MCStringRef& x_target)
87-
{
88-
uindex_t t_start, t_count;
89-
MCChunkGetExtentsOfCodeunitChunkByRange(x_target, p_index, p_index, t_start, t_count);
90-
91-
if (t_count == 0)
92-
return;
93-
94-
if (t_start + t_count > MCStringGetLength(x_target))
95-
return;
96-
97-
t_start += t_count;
98-
99-
MCAutoStringRef t_string;
100-
if (!MCStringMutableCopy(x_target, &t_string))
101-
return;
102-
103-
if (!MCStringInsert(*t_string, t_start, p_value))
104-
return;
105-
106-
MCAutoStringRef t_new_string;
107-
if (!MCStringCopy(*t_string, &t_new_string))
108-
return;
109-
110-
MCValueAssign(x_target, *t_new_string);
111-
}
112-
113-
extern "C" void MCCharStoreBeforeCharOf(MCStringRef p_value, index_t p_index, MCStringRef& x_target)
114-
{
115-
uindex_t t_start, t_count;
116-
MCChunkGetExtentsOfCodeunitChunkByRange(x_target, p_index, p_index, t_start, t_count);
117-
118-
if (t_count == 0)
119-
return;
120-
121-
if (t_start + t_count > MCStringGetLength(x_target))
122-
return;
123-
124-
MCAutoStringRef t_string;
125-
if (!MCStringMutableCopy(x_target, &t_string))
126-
return;
127-
128-
if (!MCStringInsert(*t_string, t_start, p_value))
129-
return;
130-
131-
MCAutoStringRef t_new_string;
132-
if (!MCStringCopy(*t_string, &t_new_string))
133-
return;
134-
135-
MCValueAssign(x_target, *t_new_string);
136-
}
137-
13886
extern "C" void MCCharEvalOffsetOfCharsInRange(bool p_is_last, MCStringRef p_needle, MCStringRef p_target, MCRange p_range, uindex_t& r_output)
13987
{
14088
uindex_t t_offset;

0 commit comments

Comments
 (0)