Skip to content

Commit aa4df7e

Browse files
author
runrevali
committed
[[ Foundation ]] Refactor chunk related code into libfoundation
1 parent 40c6a6a commit aa4df7e

11 files changed

Lines changed: 1031 additions & 519 deletions

File tree

engine/src/chunk.cpp

Lines changed: 508 additions & 2 deletions
Large diffs are not rendered by default.

engine/src/chunk.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
1717
#ifndef CHUNK_H
1818
#define CHUNK_H
1919

20+
#ifndef __MC_FOUNDATION_CHUNK__
21+
#include "foundation-chunk.h"
22+
#endif
23+
2024
#include "express.h"
2125

2226
class MCCRef
@@ -232,4 +236,43 @@ class MCChunk : public MCExpression
232236
}
233237
};
234238

239+
MCChunkType MCChunkTypeFromChunkTerm(Chunk_term p_chunk_term);
240+
241+
class MCOldTextChunkIterator
242+
{
243+
MCStringRef text;
244+
MCScriptPoint *sp;
245+
Chunk_term type;
246+
MCRange range;
247+
bool exhausted;
248+
uindex_t length;
249+
bool first_chunk;
250+
MCAutoArray<MCRange> breaks;
251+
uindex_t break_position;
252+
253+
// store the number of codeunits matched in text when searching for
254+
// delimiter, so that we can increment the range appropriately.
255+
uindex_t delimiter_length;
256+
257+
public:
258+
MCOldTextChunkIterator(Chunk_term p_chunk_type, MCStringRef p_text);
259+
~MCOldTextChunkIterator();
260+
261+
MCRange getrange()
262+
{
263+
return range;
264+
}
265+
266+
bool isexhausted()
267+
{
268+
return exhausted;
269+
}
270+
271+
bool next(MCExecContext& ctxt);
272+
bool copystring(MCStringRef& r_string);
273+
uindex_t countchunks(MCExecContext& ctxt);
274+
bool isamong(MCExecContext& ctxt, MCStringRef p_needle);
275+
uindex_t chunkoffset(MCExecContext& ctxt, MCStringRef p_needle, uindex_t p_start_offset);
276+
};
277+
235278
#endif

engine/src/exec-keywords.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -436,35 +436,35 @@ void MCKeywordsExecRepeatFor(MCExecContext& ctxt, MCStatement *statements, MCExp
436436
switch (each)
437437
{
438438
case FU_LINE:
439-
tci = new MCTextChunkIterator(CT_LINE, *t_string);
439+
tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_LINE);
440440
break;
441441
case FU_PARAGRAPH:
442-
tci = new MCTextChunkIterator(CT_PARAGRAPH, *t_string);
442+
tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_PARAGRAPH);
443443
break;
444444
case FU_SENTENCE:
445-
tci = new MCTextChunkIterator(CT_SENTENCE, *t_string);
445+
tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_SENTENCE);
446446
break;
447447
case FU_ITEM:
448-
tci = new MCTextChunkIterator(CT_ITEM, *t_string);
448+
tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_ITEM);
449449
break;
450450
case FU_WORD:
451-
tci = new MCTextChunkIterator(CT_WORD, *t_string);
451+
tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_WORD);
452452
break;
453453
case FU_TRUEWORD:
454-
tci = new MCTextChunkIterator(CT_TRUEWORD, *t_string);
454+
tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_TRUEWORD);
455455
break;
456456
case FU_TOKEN:
457-
tci = new MCTextChunkIterator(CT_TOKEN, *t_string);
457+
tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_TOKEN);
458458
break;
459459
case FU_CODEPOINT:
460-
tci = new MCTextChunkIterator(CT_CODEPOINT, *t_string);
460+
tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_CODEPOINT);
461461
break;
462462
case FU_CODEUNIT:
463-
tci = new MCTextChunkIterator(CT_CODEUNIT, *t_string);
463+
tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_CODEUNIT);
464464
break;
465465
case FU_CHARACTER:
466466
default:
467-
tci = new MCTextChunkIterator(CT_CHARACTER, *t_string);
467+
tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_CHARACTER);
468468
break;
469469
}
470470
}
@@ -536,16 +536,16 @@ void MCKeywordsExecRepeatFor(MCExecContext& ctxt, MCStatement *statements, MCExp
536536

537537
default:
538538
{
539-
t_found = tci -> next(ctxt);
540-
endnext = tci -> isexhausted();
541-
539+
t_found = MCStringsTextChunkIteratorNext(ctxt, tci);
540+
endnext = tci -> IsExhausted();
541+
542542
if (!t_found)
543543
{
544544
t_unit = kMCEmptyString;
545545
done = true;
546546
}
547547
else
548-
tci -> copystring(&t_unit);
548+
tci -> CopyString(&t_unit);
549549
}
550550
}
551551
// MW-2010-12-15: [[ Bug 9218 ]] Added KEY to the type of repeat that already

engine/src/exec-strings-chunk.cpp

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,36 +71,7 @@ uinteger_t MCStringsCountChunkCallback(void *context)
7171

7272
void MCStringsSkipWord(MCExecContext& ctxt, MCStringRef p_string, bool p_skip_spaces, uindex_t& x_offset)
7373
{
74-
uindex_t t_space_offset;
75-
uindex_t t_length = MCStringGetLength(p_string);
76-
uindex_t t_end_quote_offset = t_length;
77-
uindex_t t_end_line_offset = t_length;
78-
79-
if (MCStringGetCharAtIndex(p_string, x_offset) == '"')
80-
{
81-
// then bump the offset up to the next quotation mark + 1, or the beginning of the next line
82-
// if neither of these are present then set offset to string length.
83-
MCStringFirstIndexOfChar(p_string, '"', x_offset + 1, kMCCompareExact, t_end_quote_offset);
84-
MCStringFirstIndexOf(p_string, ctxt . GetLineDelimiter(), x_offset + 1, kMCCompareExact, t_end_line_offset);
85-
86-
if (t_end_quote_offset < t_end_line_offset)
87-
x_offset = t_end_quote_offset + 1;
88-
else if (t_end_line_offset < t_end_quote_offset)
89-
x_offset = t_end_line_offset + MCStringGetLength(ctxt . GetLineDelimiter());
90-
else
91-
x_offset = t_length;
92-
}
93-
else
94-
{
95-
while (!MCUnicodeIsWhitespace(MCStringGetCharAtIndex(p_string, x_offset)) && x_offset < t_length)
96-
x_offset++;
97-
}
98-
99-
if (p_skip_spaces)
100-
{
101-
while (MCUnicodeIsWhitespace(MCStringGetCharAtIndex(p_string, x_offset)) && x_offset < t_length)
102-
x_offset++;
103-
}
74+
MCChunkSkipWord(p_string, ctxt . GetLineDelimiter(), ctxt . GetStringComparisonType(), p_skip_spaces, x_offset);
10475
}
10576

10677
void MCStringsCountChunks(MCExecContext& ctxt, Chunk_term p_chunk_type, MCStringRef p_string, uinteger_t& r_count)
@@ -122,9 +93,14 @@ void MCStringsCountChunks(MCExecContext& ctxt, Chunk_term p_chunk_type, MCString
12293
return;
12394
}
12495

96+
MCChunkType t_type;
97+
t_type = MCChunkTypeFromChunkTerm(p_chunk_type);
98+
12599
MCTextChunkIterator *tci;
126-
tci = new MCTextChunkIterator(p_chunk_type, p_string);
127-
r_count = tci -> countchunks(ctxt);
100+
tci = MCStringsTextChunkIteratorCreate(ctxt, p_string, p_chunk_type);
101+
102+
r_count = tci -> CountChunks();
103+
128104
delete tci;
129105
return;
130106
}
@@ -1121,3 +1097,59 @@ void MCStringsMarkBytesOfTextByOrdinal(MCExecContext& ctxt, Chunk_term p_ordinal
11211097
x_mark . start = x_mark . start + t_first;
11221098
x_mark . finish = x_mark . start + t_chunk_count;
11231099
}
1100+
1101+
////////////////////////////////////////////////////////////////////////////////
1102+
1103+
MCTextChunkIterator_Tokenized::MCTextChunkIterator_Tokenized(MCStringRef p_text, MCChunkType p_chunk_type) : MCTextChunkIterator(p_text, p_chunk_type)
1104+
{
1105+
m_sp = new MCScriptPoint(p_text);
1106+
}
1107+
1108+
MCTextChunkIterator_Tokenized::~MCTextChunkIterator_Tokenized()
1109+
{
1110+
delete m_sp;
1111+
}
1112+
1113+
bool MCTextChunkIterator_Tokenized::Next()
1114+
{
1115+
MCerrorlock++;
1116+
1117+
bool t_found = true;
1118+
uint2 t_pos;
1119+
Parse_stat ps = m_sp -> nexttoken();
1120+
if (ps == PS_ERROR || ps == PS_EOF)
1121+
t_found = false;
1122+
1123+
if (t_found)
1124+
{
1125+
m_range . offset = m_sp -> getindex();
1126+
m_range . length = MCStringGetLength(m_sp -> gettoken_stringref());
1127+
}
1128+
1129+
return t_found;
1130+
}
1131+
1132+
1133+
MCTextChunkIterator *MCStringsTextChunkIteratorCreate(MCExecContext& ctxt, MCStringRef p_text, Chunk_term p_chunk_type)
1134+
{
1135+
return MCChunkCreateTextChunkIterator(p_text, MCChunkTypeFromChunkTerm(p_chunk_type), p_chunk_type == CT_LINE ? ctxt . GetLineDelimiter() : ctxt . GetItemDelimiter(), ctxt . GetStringComparisonType());
1136+
}
1137+
1138+
bool MCStringsTextChunkIteratorNext(MCExecContext& ctxt, MCTextChunkIterator *tci)
1139+
{
1140+
tci -> SetOptions(ctxt . GetStringComparisonType());
1141+
1142+
MCChunkType t_type;
1143+
t_type = tci -> GetType();
1144+
1145+
if (t_type == kMCChunkTypeLine || t_type == kMCChunkTypeItem)
1146+
{
1147+
MCStringRef t_delimiter;
1148+
t_delimiter = t_type == kMCChunkTypeLine ? ctxt . GetLineDelimiter() : ctxt . GetItemDelimiter();
1149+
reinterpret_cast<MCTextChunkIterator_Delimited *>(tci) -> SetDelimiter(t_delimiter);
1150+
}
1151+
1152+
return tci -> Next();
1153+
}
1154+
1155+
////////////////////////////////////////////////////////////////////////////////

engine/src/exec-strings.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
3636
#include "chunk.h"
3737
#include "date.h"
3838

39+
#include "foundation-chunk.h"
40+
3941
////////////////////////////////////////////////////////////////////////////////
4042

4143
MC_EXEC_DEFINE_EVAL_METHOD(Strings, ToLower, 2)
@@ -1584,12 +1586,18 @@ void MCStringsEvalEndsWith(MCExecContext& ctxt, MCStringRef p_whole, MCStringRef
15841586

15851587
bool MCStringsEvalIsAmongTheChunksOf(MCExecContext& ctxt, MCStringRef p_chunk, MCStringRef p_text, Chunk_term p_chunk_type)
15861588
{
1589+
MCChunkType t_type;
1590+
t_type = MCChunkTypeFromChunkTerm(p_chunk_type);
1591+
15871592
MCTextChunkIterator *tci;
1588-
tci = new MCTextChunkIterator(p_chunk_type, p_text);
1593+
tci = MCStringsTextChunkIteratorCreate(ctxt, p_text, p_chunk_type);
1594+
15891595
bool t_result;
1590-
t_result = tci -> isamong(ctxt, p_chunk);
1596+
t_result = tci -> IsAmong(p_chunk);
1597+
15911598
delete tci;
15921599
return t_result;
1600+
15931601
}
15941602

15951603
void MCStringsEvalIsAmongTheLinesOf(MCExecContext& ctxt, MCStringRef p_chunk, MCStringRef p_string, bool& r_result)
@@ -1722,9 +1730,14 @@ void MCStringsEvalIsNotAmongTheBytesOf(MCExecContext& ctxt, MCDataRef p_chunk, M
17221730

17231731
uindex_t MCStringsChunkOffset(MCExecContext& ctxt, MCStringRef p_chunk, MCStringRef p_string, uindex_t p_start_offset, Chunk_term p_chunk_type)
17241732
{
1733+
MCChunkType t_type;
1734+
t_type = MCChunkTypeFromChunkTerm(p_chunk_type);
1735+
17251736
MCTextChunkIterator *tci;
1726-
tci = new MCTextChunkIterator(p_chunk_type, p_string);
1727-
uindex_t t_offset = tci -> chunkoffset(ctxt, p_chunk, p_start_offset);
1737+
tci = MCStringsTextChunkIteratorCreate(ctxt, p_string, p_chunk_type);
1738+
1739+
uindex_t t_offset = tci -> ChunkOffset(p_chunk, p_start_offset, ctxt . GetWholeMatches());
1740+
17281741
delete tci;
17291742
return t_offset;
17301743
}

engine/src/exec.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,6 +2329,11 @@ void MCStringsMarkBytesOfTextByOrdinal(MCExecContext& ctxt, Chunk_term p_ordinal
23292329

23302330
void MCStringsSkipWord(MCExecContext& ctxt, MCStringRef p_string, bool p_skip_spaces, uindex_t& x_offset);
23312331

2332+
class MCTextChunkIterator;
2333+
2334+
MCTextChunkIterator *MCStringsTextChunkIteratorCreate(MCExecContext& ctxt, MCStringRef p_text, Chunk_term p_chunk_type);
2335+
bool MCStringsTextChunkIteratorNext(MCExecContext& ctxt, MCTextChunkIterator *tci);
2336+
23322337
///////////
23332338

23342339
struct MCInterfaceBackdrop;

0 commit comments

Comments
 (0)