Skip to content

Commit

Permalink
added substring and simplified get function
Browse files Browse the repository at this point in the history
For #7876 adds a Text.substring function which supports negative indexes and returns a part of a string from 0-based index 'start' and continuing for 'length'.

Also simplified get function as it looped unnecessarily.
  • Loading branch information
Cassandra-Clark committed Sep 27, 2023
1 parent 9b335c5 commit 1fd5b20
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,14 @@ Text.at self index =
"건반(Korean)".get 1 == "반"
Text.get : Integer -> Any -> Any
Text.get self index ~if_missing=Nothing =
case index < 0 of
True ->
length = self.length
new_index = index + length
if new_index < 0 then if_missing else
self.at new_index
False ->
iterator = BreakIterator.getCharacterInstance
iterator.setText self
first = iterator.next index
next = if first == -1 then -1 else iterator.next
if (next == -1) then if_missing else
Text_Utils.substring self first next
new_index = if index < 0 then index + self.length else index
if new_index < 0 then if_missing else
iterator = BreakIterator.getCharacterInstance
iterator.setText self
first = iterator.next new_index
next = if first == -1 then -1 else iterator.next
if (next == -1) then if_missing else
Text_Utils.substring self first next

## GROUP Selections
Returns the first character from the text.
Expand Down Expand Up @@ -1791,19 +1786,6 @@ Text.parse_time_of_day self format:Date_Time_Formatter=Date_Time_Formatter.iso_t
Text.parse_time_zone : Time_Zone ! Time_Error
Text.parse_time_zone self = Time_Zone.parse self

## PRIVATE
Returns the appropriate index for a text object given a negative index.
If positive returns index.
Text.get_position : Integer -> Integer
Text.get_position self index ~if_missing=Nothing =
case index < 0 of
True ->
length = self.length
new_index = index + length
if new_index < 0 then if_missing else
self.get_position new_index
False ->
index
## ALIAS mid, slice, substring
GROUP Selections
Creates a new Text by selecting the specified range of the input.
Expand All @@ -1825,11 +1807,13 @@ Text.get_position self index ~if_missing=Nothing =

Text.substring : Integer -> Integer -> Range ! Index_Out_Of_Bounds
Text.substring self start:Integer length:Integer=self.length =
if length <= 0 then Error.throw (Illegal_Argument.Error "length must be greater than 0") else
fixed_start = self.get_position start
if fixed_start == Nothing then Error.throw (Illegal_Argument.Error "negative start was greater than the total number of characters") else
range = fixed_start.up_to fixed_start+length
self.take range
if length < 0 then Error.throw (Illegal_Argument.Error "length must be greater than or equal to 0") else
if length == 0 then "" else
self_length = self.length
fixed_start = if start < 0 then start + self_length else start
if fixed_start < 0 then (Error.throw (Index_Out_Of_Bounds.Error start self.length)) else
range = fixed_start.up_to fixed_start+length
self.take range

## PRIVATE
Returns a new Text constructed by slicing the input according to the provided
Expand Down
16 changes: 2 additions & 14 deletions test/Tests/src/Data/Text_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ spec =
str.get 1 . should_equal facepalm
str.get 2 . should_equal accent_1
str.get 3 . should_equal accent_2
str.get_position 0 . should_equal 0
str.get_position 1 . should_equal 1
str.get_position 2 . should_equal 2
str.get_position 3 . should_equal 3
str.first . should_equal kshi
str.second . should_equal facepalm
str.last . should_equal accent_2
Expand All @@ -204,10 +200,6 @@ spec =
str.get -3 . should_equal facepalm
str.get -2 . should_equal accent_1
str.get -1 . should_equal accent_2
str.get_position -4 . should_equal 0
str.get_position -3 . should_equal 1
str.get_position -2 . should_equal 2
str.get_position -1 . should_equal 3

Test.specify "should return a dataflow error when accessing characters out of bounds" <|
str = kshi + facepalm + accent_1 + accent_2
Expand All @@ -218,9 +210,6 @@ spec =
str.get -5 . should_equal Nothing
str.get 4 . should_equal Nothing
str.get -5 "?" . should_equal "?"
str.get_position -5 . should_equal Nothing
str.get_position 4 . should_equal Nothing
str.get_position -5 "?" . should_equal "?"
"".first.should_fail_with Index_Out_Of_Bounds
"".second.should_fail_with Index_Out_Of_Bounds
"".last.should_fail_with Index_Out_Of_Bounds
Expand Down Expand Up @@ -1665,11 +1654,10 @@ spec =
"Hello World!".substring 5 7 . should_equal " World!"
"Hello World!".substring -7 3 . should_equal " Wo"

Test.specify "should error on length 0 or less" <|
"Hello World!".substring 5 0 . should_fail_with Illegal_Argument
Test.specify "should error on length less than 0" <|
"Hello World!".substring 5 -2 . should_fail_with Illegal_Argument

Test.specify "should error if negative start index is too large"
"Hello World!".substring -20 5 . should_fail_with Illegal_Argument
"Hello World!".substring -20 5 . should_fail_with Index_Out_Of_Bounds

main = Test_Suite.run_main spec

0 comments on commit 1fd5b20

Please sign in to comment.