Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Updating Text.repeat and adding Text.* #3310

Merged
merged 5 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
`Text.to_upper_case`][3302]
- [Implemented initial `Table.group_by` function on Standard.Table][3305]
- [Implemented `Text.pad` and `Text.trim`][3309]
- [Updated `Text.repeat` and added `*` operator shorthand][3310]

[debug-shortcuts]:
https://github.com/enso-org/enso/blob/develop/app/gui/docs/product/shortcuts.md#debug
Expand Down Expand Up @@ -92,6 +93,7 @@
[3302]: https://github.com/enso-org/enso/pull/3302
[3305]: https://github.com/enso-org/enso/pull/3305
[3309]: https://github.com/enso-org/enso/pull/3309
[3310]: https://github.com/enso-org/enso/pull/3310

#### Enso Compiler

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -878,8 +878,28 @@ Text.contains term="" matcher=Text_Matcher.new = case matcher of
Text.to_json : Json.String
Text.to_json = Json.String this

## Takes a non-negative integer and returns a new text, consisting of `count`
concatenated copies of `this`.

## Takes an integer and returns a new text, consisting of `count` concatenated
copies of `this`.

Arguments:
- count: The number of times that the text `this` should be repeated to make
the new text.

> Example
Repeat the string "A" five times.

"A" * 5 == "AAAAA"

> Example
Repeat the string "Hello " twice.

"Hello " * 2 == "Hello Hello "
Text.* : Integer -> Text
Text.* count = this.repeat count

## Takes an integer and returns a new text, consisting of `count` concatenated
copies of `this`.

Arguments:
- count: The number of times that the text `this` should be repeated to make
Expand All @@ -888,10 +908,17 @@ Text.to_json = Json.String this
> Example
Repeat the string "ABBA" five times.

"ABBA".repeat 5
"ABBA".repeat 5 == "ABBAABBAABBAABBAABBA"

> Example
Repeat the string "Hello " twice.

"Hello ".repeat 2 == "Hello Hello "
Text.repeat : Integer -> Text
Text.repeat count =
0.up_to count . fold "" acc-> _-> acc + this
Text.repeat count=1 =
## TODO max is a workaround until Range is sorted to make 0..-1 not cause an infinite loop
https://www.pivotaltracker.com/story/show/181435598
0.up_to (count.max 0) . fold "" acc-> _-> acc + this

## PRIVATE
Utility function taking a range pointing at grapheme clusters and converting to a range on the underlying code points
Expand Down
30 changes: 30 additions & 0 deletions test/Tests/src/Data/Text_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,36 @@ spec =
' \u{301} '.trim . should_equal ' \u{301}'
' \u{301}'.trim . should_equal ' \u{301}'

Test.specify "should allow repeating as in the examples" <|
"ABBA".repeat 5 . should_equal "ABBAABBAABBAABBAABBA"
"A".repeat 5 . should_equal "AAAAA"
"Hello ".repeat 2 . should_equal "Hello Hello "

Test.specify "should allow more general repeating" <|
'He\u{302}llo\u{308}'.repeat 1 . should_equal 'He\u{302}llo\u{308}'
'He\u{302}llo\u{308}'.repeat 3 . should_equal 'He\u{302}llo\u{308}He\u{302}llo\u{308}He\u{302}llo\u{308}'
'He\u{302}llo\u{308}'.repeat 0 . should_equal ''
'He\u{302}llo\u{308}'.repeat -5 . should_equal ''

''.repeat 100 . should_equal ''

'✨🚀🚧'.repeat 2 . should_equal '✨🚀🚧✨🚀🚧'

Test.specify "should allow repeating using * as in the examples" <|
"ABBA"*5 . should_equal "ABBAABBAABBAABBAABBA"
"A"*5 . should_equal "AAAAA"
"Hello "*2 . should_equal "Hello Hello "

Test.specify "should allow more general repeating using *" <|
'He\u{302}llo\u{308}'*1 . should_equal 'He\u{302}llo\u{308}'
'He\u{302}llo\u{308}'*3 . should_equal 'He\u{302}llo\u{308}He\u{302}llo\u{308}He\u{302}llo\u{308}'
'He\u{302}llo\u{308}'*0 . should_equal ''
'He\u{302}llo\u{308}'*(-5) . should_equal ''

''*100 . should_equal ''

'✨🚀🚧'*2 . should_equal '✨🚀🚧✨🚀🚧'

Test.group "Regex matching" <|
Test.specify "should be possible on text" <|
match = "My Text: Goes Here".match "^My Text: (.+)$" mode=Regex_Mode.First
Expand Down