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

Commit 6c98156

Browse files
committed
[Bug 14846] com.livecode.char: Fix "(offset|index) of _ (before|after) _"
1 parent f718e4b commit 6c98156

3 files changed

Lines changed: 38 additions & 38 deletions

File tree

libscript/src/char.mlc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,6 @@ The first (respectively last) offset of <Needle> in <Target> is number of chars
386386

387387
Tags: Strings
388388
*/
389-
390-
/* bug 14846
391389
syntax CharOffsetAfter is prefix operator with precedence 1
392390
"the" ( "first" <IsLast=false> | "last" <IsLast=true> | <IsLast=false> ) "offset" "of" <Needle: Expression> "after" <After: Expression> "in" <Target: Expression>
393391
begin
@@ -398,7 +396,6 @@ syntax CharIndexAfter is prefix operator with precedence 1
398396
begin
399397
MCCharEvalOffsetOfCharsAfter(IsLast, Needle, After, Target, output)
400398
end syntax
401-
*/
402399

403400
/*
404401

@@ -426,7 +423,6 @@ The first (respectively last) offset of <Needle> in <Target> is number of chars
426423
Tags: Strings
427424
*/
428425

429-
/* bug 14846
430426
syntax CharOffsetBefore is prefix operator with precedence 1
431427
"the" ( "first" <IsFirst=true> | "last" <IsFirst=false> | <IsFirst=false> ) "offset" "of" <Needle: Expression> "before" <Before: Expression> "in" <Target: Expression>
432428
begin
@@ -437,7 +433,6 @@ syntax CharIndexBefore is prefix operator with precedence 1
437433
begin
438434
MCCharEvalOffsetOfCharsBefore(IsFirst, Needle, Before, Target, output)
439435
end syntax
440-
*/
441436

442437
/*
443438
Summary: Determines whether <Source> contains <Needle>.

libscript/src/module-char.cpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -108,34 +108,41 @@ extern "C" MC_DLLEXPORT void MCCharEvalOffsetOfCharsInRange(bool p_is_last, MCSt
108108
{
109109
uindex_t t_offset;
110110
t_offset = 0;
111-
if (!MCStringIsEmpty(p_needle))
112-
{
113-
MCRange t_range;
114-
if (p_range . length == UINDEX_MAX)
115-
{
116-
MCStringMapGraphemeIndices(p_target, kMCLocaleBasic, MCRangeMake(p_range . offset, 1), t_range);
117-
t_range . length = UINDEX_MAX;
118-
}
119-
else
120-
MCStringMapGraphemeIndices(p_target, kMCLocaleBasic, p_range, t_range);
111+
if (MCStringIsEmpty(p_needle))
112+
{
113+
r_output = 0;
114+
return;
115+
}
116+
117+
MCRange t_range;
118+
if (p_range . length == UINDEX_MAX)
119+
{
120+
MCStringMapGraphemeIndices(p_target, kMCLocaleBasic, MCRangeMake(p_range . offset, 1), t_range);
121+
t_range . length = UINDEX_MAX;
122+
}
123+
else
124+
MCStringMapGraphemeIndices(p_target, kMCLocaleBasic, p_range, t_range);
121125

122-
bool t_found;
123-
if (p_is_last)
124-
t_found = MCStringLastIndexOfStringInRange(p_target, p_needle, t_range, kMCStringOptionCompareExact, t_offset);
125-
else
126-
t_found = MCStringFirstIndexOfStringInRange(p_target, p_needle, t_range, kMCStringOptionCompareExact, t_offset);
126+
bool t_found;
127+
if (p_is_last)
128+
t_found = MCStringLastIndexOfStringInRange(p_target, p_needle, t_range, kMCStringOptionCompareExact, t_offset);
129+
else
130+
t_found = MCStringFirstIndexOfStringInRange(p_target, p_needle, t_range, kMCStringOptionCompareExact, t_offset);
127131

128-
// correct output index
129-
if (t_found)
130-
{
131-
t_offset -= p_range . offset;
132-
t_offset++;
133-
}
134-
}
135-
132+
if (!t_found)
133+
{
134+
r_output = 0;
135+
return;
136+
}
137+
138+
// correct output index
139+
t_offset -= t_range . offset;
140+
t_offset++;
141+
136142
MCRange t_output_range;
137143
MCStringUnmapGraphemeIndices(p_target, kMCLocaleBasic, MCRangeMake(t_offset, 1), t_output_range);
138-
r_output = t_output_range . offset;
144+
145+
r_output = t_output_range . offset + p_range . offset;
139146
}
140147

141148
extern "C" MC_DLLEXPORT void MCCharEvalOffsetOfChars(bool p_is_last, MCStringRef p_needle, MCStringRef p_target, uindex_t& r_output)

tests/lcb/stdlib/char.lcb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,14 @@ public handler TestOffset()
304304
test "index" when the index of "x" in ".xx." is 2
305305
end handler
306306

307-
/* bug 14846
308307
public handler TestOffsetAfter()
309308
-- Only test single-character needles for now
310-
test "offset after (single, +ve)" when the offset of "x" after 1 in "x.xx." is 2
311-
test "first offset after (single, +ve)" when the first offset of "x" after 1 in "x.xx." is 2
312-
test "last offset after (single, +ve)" when the last offset of "x" after 1 in "x.xx." is 3
313-
test "offset after (single, -ve)" when the offset of "x" after -5 in "x.xx." is 2
314-
test "first offset after (single, -ve)" when the first offset of "x" after -5 in "x.xx." is 2
315-
test "last offset after (single, -ve)" when the last offset of "x" after -5 in "x.xx." is 3
309+
test "offset after (single, +ve)" when the offset of "x" after 1 in "x.xx." is 3
310+
test "first offset after (single, +ve)" when the first offset of "x" after 1 in "x.xx." is 3
311+
test "last offset after (single, +ve)" when the last offset of "x" after 1 in "x.xx." is 4
312+
test "offset after (single, -ve)" when the offset of "x" after -5 in "x.xx." is 3
313+
test "first offset after (single, -ve)" when the first offset of "x" after -5 in "x.xx." is 3
314+
test "last offset after (single, -ve)" when the last offset of "x" after -5 in "x.xx." is 4
316315

317316
test "offset after (single, missing, +ve)" when the offset of "x" after 1 in "x.." is 0
318317
test "first offset after (single, missing, +ve)" when the first offset of "x" after 1 in "x.." is 0
@@ -329,7 +328,7 @@ public handler TestOffsetAfter()
329328
MCUnitTestHandlerThrows(TestLastOffsetAfter_SingleInvalidNegative, "last offset after (single, invalid -ve)")
330329

331330
-- For chars, "index" is synonymous with "offset"
332-
test "index after" when the index of "x" after 2 in ".xx." is 1
331+
test "index after" when the index of "x" after 2 in ".xx." is 3
333332
end handler
334333

335334
handler TestOffsetAfter_SingleInvalidPositive()
@@ -373,7 +372,6 @@ public handler TestOffsetAfterZero()
373372
put the last offset of "x" in "x" into tNoAfter
374373
test "the last offset after (single, 0, same)" when the last offset of "x" after 0 in "x" is tNoAfter
375374
end handler
376-
*/
377375

378376
public handler TestBeginsWith()
379377
test "begins with" when "xx." begins with "xx"

0 commit comments

Comments
 (0)