From efbc8754d78027946f625dd122a393851e73f86a Mon Sep 17 00:00:00 2001 From: James Dunkerley Date: Tue, 1 Mar 2022 17:46:40 +0000 Subject: [PATCH 1/5] Updating repeat function - fix issue with negative count - add * operator Add tests --- .../0.0.0-dev/src/Data/Text/Extensions.enso | 45 ++++++++++++++++--- test/Tests/src/Data/Text_Spec.enso | 30 +++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso index df3c70919a92..e0b62a1701e1 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso @@ -878,8 +878,33 @@ 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 "ABBA" five times. + + "ABBA" * 5 == "ABBAABBAABBAABBAABBA" + + > 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 @@ -888,10 +913,20 @@ Text.to_json = Json.String this > Example Repeat the string "ABBA" five times. - "ABBA".repeat 5 + "ABBA".repeat 5 == "ABBAABBAABBAABBAABBA" + + > Example + Repeat the string "A" five times. + + "A".repeat 5 == "AAAAA" + + > 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 = + 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 diff --git a/test/Tests/src/Data/Text_Spec.enso b/test/Tests/src/Data/Text_Spec.enso index 9be150d1703d..5dd8e6eed9a5 100644 --- a/test/Tests/src/Data/Text_Spec.enso +++ b/test/Tests/src/Data/Text_Spec.enso @@ -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 From 4e7fe838b159204200b38bc333b2a51ed0c61203 Mon Sep 17 00:00:00 2001 From: James Dunkerley Date: Tue, 1 Mar 2022 17:49:52 +0000 Subject: [PATCH 2/5] Change log --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 035efcff8b84..971cc9043179 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 From 26a9c9a71830364a57d5ac720b7bcf9f5de2609b Mon Sep 17 00:00:00 2001 From: James Dunkerley Date: Wed, 2 Mar 2022 17:06:01 +0000 Subject: [PATCH 3/5] Add ToDo --- .../lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso | 2 ++ 1 file changed, 2 insertions(+) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso index e0b62a1701e1..e3d3e26a9132 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso @@ -926,6 +926,8 @@ Text.* count = this.repeat count "Hello ".repeat 2 == "Hello Hello " Text.repeat : Integer -> Text 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/n/projects/2539304 0.up_to (count.max 0) . fold "" acc-> _-> acc + this ## PRIVATE From d279a4fe33df28873e6fbded760597b24e2b8894 Mon Sep 17 00:00:00 2001 From: James Dunkerley Date: Wed, 2 Mar 2022 17:06:55 +0000 Subject: [PATCH 4/5] Correct link --- .../lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso index e3d3e26a9132..c043a76f0ba2 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso @@ -927,7 +927,7 @@ Text.* count = this.repeat count Text.repeat : Integer -> Text 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/n/projects/2539304 + https://www.pivotaltracker.com/story/show/181435598 0.up_to (count.max 0) . fold "" acc-> _-> acc + this ## PRIVATE From e46f7a5bb71ee8433828d375af43f0af5a6ee612 Mon Sep 17 00:00:00 2001 From: James Dunkerley Date: Wed, 2 Mar 2022 17:40:07 +0000 Subject: [PATCH 5/5] Examples --- .../Base/0.0.0-dev/src/Data/Text/Extensions.enso | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso index c043a76f0ba2..8aa292910d75 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text/Extensions.enso @@ -886,11 +886,6 @@ Text.to_json = Json.String this - count: The number of times that the text `this` should be repeated to make the new text. - > Example - Repeat the string "ABBA" five times. - - "ABBA" * 5 == "ABBAABBAABBAABBAABBA" - > Example Repeat the string "A" five times. @@ -915,11 +910,6 @@ Text.* count = this.repeat count "ABBA".repeat 5 == "ABBAABBAABBAABBAABBA" - > Example - Repeat the string "A" five times. - - "A".repeat 5 == "AAAAA" - > Example Repeat the string "Hello " twice.