Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Improve performance for rightOfBack and leftOfBack #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
47 changes: 35 additions & 12 deletions src/String/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -741,12 +741,17 @@ consisting of the characters in the string that are to the right of the pattern.
-}
rightOfBack : String -> String -> String
rightOfBack pattern string =
string
|> String.indexes pattern
|> List.reverse
|> List.head
|> Maybe.map ((+) (String.length pattern) >> (\a -> String.dropLeft a string))
|> Maybe.withDefault ""
case String.indexes pattern string of
[] ->
""

firstIndex :: rest ->
case last rest of
Just lastIndex ->
String.slice (String.length pattern + lastIndex) (String.length string) string

Nothing ->
String.slice (String.length pattern + firstIndex) (String.length string) string


{-| Search a string from right to left for a pattern and return a substring
Expand All @@ -757,12 +762,30 @@ consisting of the characters in the string that are to the left of the pattern.
-}
leftOfBack : String -> String -> String
leftOfBack pattern string =
string
|> String.indexes pattern
|> List.reverse
|> List.head
|> Maybe.map (\a -> String.left a string)
|> Maybe.withDefault ""
case String.indexes pattern string of
[] ->
""

firstIndex :: rest ->
case last rest of
Just lastIndex ->
String.slice 0 lastIndex string

Nothing ->
String.slice 0 firstIndex string


last : List a -> Maybe a
last items =
case items of
[] ->
Nothing

[ x ] ->
Just x

_ :: rest ->
last rest


{-| Convert a string into a list of UTF-32 code points.
Expand Down
13 changes: 13 additions & 0 deletions tests/LeftOfBackTest.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module LeftOfBackTest exposing (leftOfBackTest)

import Expect
import String.Extra exposing (leftOfBack)
import Test exposing (Test, test)


leftOfBackTest : Test
leftOfBackTest =
test "leftOfBack" <|
\() ->
leftOfBack "_" "This_is_a_test_string"
|> Expect.equal "This_is_a_test"
13 changes: 13 additions & 0 deletions tests/RightOfBackTest.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module RightOfBackTest exposing (rightOfBackTest)

import Expect
import String.Extra exposing (rightOfBack)
import Test exposing (Test, test)


rightOfBackTest : Test
rightOfBackTest =
test "rightOfBack" <|
\() ->
rightOfBack "_" "This_is_a_test_string"
|> Expect.equal "string"