Skip to content

Commit

Permalink
Address PR review
Browse files Browse the repository at this point in the history
Deal with grapheme cluster bounderies correctly.
Updated docs.
  • Loading branch information
hubertp committed Mar 3, 2022
1 parent dbd75b4 commit f12c9e1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -615,18 +617,19 @@ 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
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).
Expand Down
14 changes: 0 additions & 14 deletions std-bits/base/src/main/java/org/enso/base/Text_Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
*
Expand Down
11 changes: 10 additions & 1 deletion test/Tests/src/Data/Text_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f12c9e1

Please sign in to comment.