From f12c9e14e8f820b855c60dcae235471e64f81ac3 Mon Sep 17 00:00:00 2001 From: Hubert Plociniczak Date: Thu, 3 Mar 2022 10:09:01 +0100 Subject: [PATCH] Address PR review Deal with grapheme cluster bounderies correctly. Updated docs. --- .../0.0.0-dev/src/Data/Text/Extensions.enso | 21 +++++++++++-------- .../main/java/org/enso/base/Text_Utils.java | 14 ------------- test/Tests/src/Data/Text_Spec.enso | 11 +++++++++- 3 files changed, 22 insertions(+), 24 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 70988b1c7578c..003a652fb2915 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 @@ -602,10 +602,12 @@ Text.not_empty = this.is_empty.not ## Inserts text value at the specified index. Arguments: - - index: The location in the text to inset text at (0-based). The - index is also allowed to be negative, then the characters are - counted from the end of the text, i.e. -1 will insert the text after - the last x. + - index: The position (0-based) in the text to inset text at. + When the index is non-negative the text is inserted before the + specified position. + When the index is negative, then the characters are counted from + the end of the text and the text is inserted after the specified + position, i.e. -1 will insert the text after the last character. ! What is a Character? A character is defined as an Extended Grapheme Cluster, see Unicode @@ -615,6 +617,7 @@ Text.not_empty = this.is_empty.not > Example Insert text at a specified index + "Hello World!".insert 0 " Cruel" == " CruelHello World!" "Hello World!".insert 5 " Cruel" == "Hello Cruel World!" "Hello World!".insert -1 " Cruel" == "Hello World! Cruel" Text.insert : Integer -> Text -> Text ! Index_Out_Of_Bounds_Error @@ -622,11 +625,11 @@ Text.insert index that = len = this.length idx = if index < 0 then len + index + 1 else index if (idx < 0) || (idx > len) then Error.throw (Index_Out_Of_Bounds_Error index len) else - if idx == 0 then Text_Utils.concat that this else - if idx == len then Text_Utils.concat this that else - pre = Text_Utils.substring this 0 idx - post = Text_Utils.substring this idx len - Text_Utils.concat pre that post + if idx == 0 then that + this else + if idx == len then this + that else + pre = this.take (Range 0 idx) + post = this.take (Range idx len) + pre + that + post ## Returns if a character from the text at the specified index (0-based) is a digit (0-9). diff --git a/std-bits/base/src/main/java/org/enso/base/Text_Utils.java b/std-bits/base/src/main/java/org/enso/base/Text_Utils.java index da51a99d798da..0d1ef707e1642 100644 --- a/std-bits/base/src/main/java/org/enso/base/Text_Utils.java +++ b/std-bits/base/src/main/java/org/enso/base/Text_Utils.java @@ -186,20 +186,6 @@ public static boolean contains(String string, String substring) { return searcher.first() != StringSearch.DONE; } - /** - * Combine strings {@code strings} - * - * @param strings the string values to be concatenated - * @return the resulting string - */ - public static String concat(String... strings) { - StringBuilder sb = new StringBuilder(); - for (String arg: strings) { - sb.append(arg); - } - return sb.toString(); - } - /** * Replaces all occurrences of {@code oldSequence} within {@code str} with {@code newSequence}. * diff --git a/test/Tests/src/Data/Text_Spec.enso b/test/Tests/src/Data/Text_Spec.enso index 342d37dec2f5c..11665aa3a2f1d 100644 --- a/test/Tests/src/Data/Text_Spec.enso +++ b/test/Tests/src/Data/Text_Spec.enso @@ -344,18 +344,27 @@ spec = hello_world.insert 5 cruel . should_equal "Hello Cruel World!" hello_world.insert (hello_world.length - 1) cruel . should_equal "Hello World Cruel!" hello_world.insert hello_world.length cruel . should_equal "Hello World! Cruel" + txt = kshi + facepalm + accent_1 + txt.insert 1 cruel . should_equal (kshi + cruel + facepalm + accent_1) + txt.insert 3 cruel . should_equal (kshi + facepalm + accent_1 + cruel) Test.specify "should report Index_Out_Of_Bounds_Error when inserting text at an invalid positive index position" <| hello_world.insert (hello_world.length + 1) cruel . should_fail_with Index_Out_Of_Bounds_Error + (kshi + facepalm + accent_1).insert 4 cruel . should_fail_with Index_Out_Of_Bounds_Error Test.specify "should insert text at a negative index position" <| hello_world.insert -1 cruel . should_equal "Hello World! Cruel" hello_world.insert -5 cruel . should_equal "Hello Wo Cruelrld!" hello_world.insert -(hello_world.length) cruel . should_equal "H Cruelello World!" - hello_world.insert -(hello_world.length + 1) cruel . should_equal "" CruelHello World!" + hello_world.insert -(hello_world.length + 1) cruel . should_equal " CruelHello World!" + txt = kshi + facepalm + accent_1 + txt.insert -1 cruel . should_equal (txt + cruel) + txt.insert -(txt.length) cruel . should_equal (kshi + cruel + facepalm + accent_1) Test.specify "should report Index_Out_Of_Bounds_Error when inserting text at an invalid negative index position" <| hello_world.insert -(hello_world.length + 2) cruel . should_fail_with Index_Out_Of_Bounds_Error + txt = kshi + facepalm + accent_1 + txt.insert -(txt.length + 2) cruel . should_fail_with Index_Out_Of_Bounds_Error Test.specify "should be able to check by index if is a digit" <| str = kshi + "A12" + accent_2