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

[Bug 14861] LCB: Add "reverse _" syntax for sequence types (String, List, Data) #5328

Merged
merged 12 commits into from Apr 5, 2017

Conversation

peter-b
Copy link
Contributor

@peter-b peter-b commented Mar 31, 2017

This patch adds in-place reverse _ syntax to LCB for sequence types (List, String and Data).

variable tList
put [1, 2, 3] into tList
reverse tList
expect that tList is [3, 2, 1]

There are several components:

  • An MCInplaceReverse() template function for libfoundation internal use that supports any type that has a swap() operation
  • In-place MCProperListReverse() and MCDataReverse() functions which operate on mutable values
  • MCStringCopyReverse(), which creates a copy of a string with the order of its graphemes reversed
  • LCB syntax definitions, documentation, examples and unit tests

This is useful when `MCAutoValueRefBase`-derived managed pointers are
used in the implementation of libfoundation.  `t_string->chars` is a
lot clearer than `(*t_string)->chars`.
Add a private template that provides in-place reverse for an array.
The algorithm works from the middle of the array, working outwards and
swapping elements between the front and the back.  It can only be used
for types that are suitable for use with `std::swap()`; either
trivially copyable, or with an implementation of `T::swap(T& other)`.
Add a function that reverses a mutable list instance in place.
Add a function that reverses the order of bytes in a mutable `MCData`
instance in-place.
Strings cannot be reversed in-place in the general case.  There is no
upper limit on the number of codeunits in a grapheme, which means that
a temporary buffer of at the same length of the string is required for
general correctness.

This patch adds a "reversed copy" function which performs a deep copy
of the source string, and then constructs the reversed form in the
copy by re-reading from the source string via a grapheme break
iterator.

In practice, the "trivial" flag is not getting set for trivial
strings, so all strings that use UTF-16 internal representatino go
through the slow grapheme break iterator path.
Add the ability to reverse a binary string in an LCB program with a
new `reverse tData` statement.
Add the ability to reverse the order of bytes in a list with the
`reverse tList` statement.
Add a new function to reverse an LCB string in-place using the
`reverse tString` statement.
Now that there is a `reverse` statement, provide an alternative
example of looping over the contents of a string.
@peter-b peter-b added this to the 9.0.0-dp-7 milestone Mar 31, 2017
@mention-bot
Copy link

@peter-b, thanks for your PR! By analyzing the history of the files in this pull request, we identified @livecodeian, @runrevmark and @livecodesebastien to be potential reviewers.

The newly-introduced `MCInplaceReverse()` function requires
`<utility>` to get `std::swap()`.
* unichar_t codeunits items per grapheme. In this case, we
* reverse by iterating over the contents of the original
* string, copying the graphemes into the new string. */
MCStringRef t_original = __MCStringIsIndirect(self) ? self->string : self;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid using the ternary conditional operator

@livecodeali
Copy link
Member

@livecode-vulcan review ok de86d30

@livecode-vulcan
Copy link
Contributor

💙 review by @livecodeali ok de86d30

livecode-vulcan added a commit that referenced this pull request Apr 5, 2017
[Bug 14861] LCB: Add "reverse _" syntax for sequence types (String, List, Data)

This patch adds in-place `reverse _` syntax to LCB for sequence types (`List`, `String` and `Data`).

```
variable tList
put [1, 2, 3] into tList
reverse tList
expect that tList is [3, 2, 1]
```

There are several components:

- An `MCInplaceReverse()` template function for libfoundation internal use that supports any type that has a `swap()` operation
- In-place `MCProperListReverse()` and `MCDataReverse()` functions which operate on mutable values
- `MCStringCopyReverse()`, which creates a copy of a string with the order of its _graphemes_ reversed
- LCB syntax definitions, documentation, examples and unit tests
@livecode-vulcan
Copy link
Contributor

😎 test success de86d30

  • try-community-armv6-android-api8: success
  • try-community-armv6-android-api9: success
  • try-community-js-emscripten-sdk1.35: success
  • try-community-universal-ios-iphoneos10.2: success
  • try-community-universal-ios-iphonesimulator10.2: success
  • try-community-universal-mac-macosx10.6: success
  • try-community-universal-mac-macosx10.9: success
  • try-community-x86-linux-debian7: success
  • try-community-x86-linux-debian8: success
  • try-community-x86_64-linux-debian7: success
  • try-community-x86_64-linux-debian8: success
  • try-community-x86-win32: success
  • try-community-x86_64-win32: success

@peter-b peter-b merged commit 1edabd0 into livecode:develop Apr 5, 2017
@peter-b peter-b deleted the bugfix-14861 branch April 5, 2017 10:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants