Commit b8d6807c authored by Camil Staps's avatar Camil Staps 🐧

Merge branch 'moreEfficientReplaceSubString' into 'master'

more efficient replaceSubString for Strings

See merge request !257
parents 18ca39e0 7ac69f03
Pipeline #24539 passed with stage
in 3 minutes and 38 seconds
......@@ -2,7 +2,7 @@ implementation module Text
import qualified StdArray
import StdOverloaded, StdString, StdArray, StdChar, StdInt, StdBool, StdClass, StdList
import Data.List
import Data.List, Data.Func
instance Text String
where
......@@ -104,13 +104,15 @@ instance Text String
subString start len haystack = haystack % (start, start + len - 1)
replaceSubString :: !String !String !String -> String
replaceSubString needle replacement haystack
#! index = indexOf needle haystack
| index == -1 = haystack
| otherwise
#! start = subString 0 index haystack
#! end = subString (index + size needle) (size haystack) haystack
= start +++ replacement +++ (replaceSubString needle replacement end)
replaceSubString needle replacement haystack = concat $ replaceSubString` 0 []
where
replaceSubString` :: !Int ![String] -> [String]
replaceSubString` haystackIdx acc
# index = indexOfAfter haystackIdx needle haystack
| index == -1 = reverse [subString haystackIdx (size haystack - index) haystack: acc]
| otherwise
#! start = subString haystackIdx (index - haystackIdx) haystack
= replaceSubString` (index + size needle) [replacement, start: acc]
trim :: !String -> String
trim s = ltrim (rtrim s)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment