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

Commit afc8f42

Browse files
committed
com.livecode.char: Add in-place string reverse statement
Add a new function to reverse an LCB string in-place using the `reverse tString` statement.
1 parent 14ded7b commit afc8f42

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

libscript/src/char.lcb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,32 @@ begin
559559
MCCharRepeatForEachChar(iterator, Iterand, container)
560560
end syntax
561561

562+
--
563+
564+
public foreign handler MCStringExecReverseCharsOf(inout Target as String) \
565+
returns nothing binds to "<builtin>"
566+
567+
/**
568+
Summary: Reverse a string
569+
Target: A string
570+
571+
Example:
572+
variable tString
573+
put "abcdef" into tString
574+
reverse tString
575+
expect that tString is "fedcba"
576+
577+
Description:
578+
Reverses the order of characters in the <Target>.
579+
580+
Tags: Strings
581+
*/
582+
syntax ReverseCharsOf is statement
583+
"reverse" <Target: Expression>
584+
begin
585+
MCStringExecReverseCharsOf(Target)
586+
end syntax
587+
562588
----------------------------------------------------------------
563589

564590
/**

libscript/src/module-char.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ extern "C" MC_DLLEXPORT_DEF bool MCCharRepeatForEachChar(void*& x_iterator, MCSt
272272
return true;
273273
}
274274

275+
extern "C" MC_DLLEXPORT_DEF void
276+
MCStringExecReverseCharsOf(MCStringRef &x_string)
277+
{
278+
MCStringRef t_reversed = nullptr;
279+
if (!MCStringCopyReversed(x_string, t_reversed))
280+
return;
281+
MCValueAssign(x_string, t_reversed);
282+
}
283+
275284
////////////////////////////////////////////////////////////////
276285

277286
extern "C" MC_DLLEXPORT_DEF void MCStringEvalCodeOfChar(MCStringRef p_string, uinteger_t& r_code)

tests/lcb/stdlib/char.lcb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,37 @@ public handler TestRepeatChar()
406406
test "repeatchar (count)" when tCount is 3
407407
end handler
408408

409+
handler TestReverse_Inplace(in pDesc as String, \
410+
in pForward as String, in pReverse as String)
411+
412+
variable tReversed
413+
put pForward into tReversed
414+
415+
reverse tReversed
416+
test diagnostic tReversed
417+
test "reverse in-place (" & pDesc & ")" when tReversed is pReverse
418+
419+
reverse tReversed
420+
test diagnostic tReversed
421+
test "rereverse in-place (" & pDesc & ")" when tReversed is pForward
422+
end handler
423+
424+
public handler TestReverse()
425+
TestReverse_Inplace("empty", "", "")
426+
TestReverse_Inplace("native, odd", "abc", "cba")
427+
TestReverse_Inplace("native, even", "abcd", "dcba")
428+
TestReverse_Inplace("trivial, odd", \
429+
"\u{039a}\u{03b1}\u{03bb}\u{03b7}\u{03bc}\u{03ad}\u{03c1}\u{03b1}!", \
430+
"!\u{03b1}\u{03c1}\u{03ad}\u{03bc}\u{03b7}\u{03bb}\u{03b1}\u{039a}")
431+
TestReverse_Inplace("trivial, even", \
432+
"\u{039a}\u{03b1}\u{03bb}\u{03b7}\u{03bc}\u{03ad}\u{03c1}\u{03b1}", \
433+
"\u{03b1}\u{03c1}\u{03ad}\u{03bc}\u{03b7}\u{03bb}\u{03b1}\u{039a}")
434+
TestReverse_Inplace("BMP variation selector, 1", "a\u{fe0e}", "a\u{fe0e}")
435+
TestReverse_Inplace("BMP variation selector, even", \
436+
"\u{2b55}\u{fe0f}\u{25ef}", "\u{25ef}\u{2b55}\u{fe0f}")
437+
TestReverse_Inplace("SMP, odd", "a\u{1d11e}c", "c\u{1d11e}a")
438+
end handler
439+
409440
----------------------------------------------------------------
410441

411442
handler TestCharWithCode_Negative()

0 commit comments

Comments
 (0)