2015-12-03 05:40:34 +03:00
|
|
|
module Range where
|
|
|
|
|
2015-12-14 20:44:48 +03:00
|
|
|
import qualified Data.Char as Char
|
|
|
|
|
2015-12-03 05:40:34 +03:00
|
|
|
data Range = Range { start :: Int, end :: Int }
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
substring :: Range -> String -> String
|
|
|
|
substring range = take (end range - start range) . drop (start range)
|
|
|
|
|
2015-12-04 17:20:23 +03:00
|
|
|
totalRange :: [a] -> Range
|
|
|
|
totalRange list = Range 0 $ length list
|
|
|
|
|
2015-12-14 20:20:51 +03:00
|
|
|
offsetRange :: Int -> Range -> Range
|
|
|
|
offsetRange i (Range start end) = Range (i + start) (i + end)
|
|
|
|
|
2015-12-14 20:44:48 +03:00
|
|
|
rangesOfWordsFrom :: Int -> String -> [Range]
|
|
|
|
rangesOfWordsFrom startIndex string = case break Char.isSpace string of
|
|
|
|
([], []) -> []
|
|
|
|
([], rest) -> rangesOfWordsAfterWhitespace startIndex rest
|
|
|
|
(word, []) -> [ Range startIndex $ length word ]
|
|
|
|
(word, rest) -> (Range startIndex $ length word) : rangesOfWordsAfterWhitespace (startIndex + length word) rest
|
|
|
|
where
|
|
|
|
rangesOfWordsAfterWhitespace startIndex string | (whitespace, rest) <- break (not . Char.isSpace) string = rangesOfWordsFrom (startIndex + length whitespace) rest
|
|
|
|
|
2015-12-03 05:40:34 +03:00
|
|
|
instance Ord Range where
|
|
|
|
a <= b = start a <= start b
|