1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 06:41:45 +03:00
semantic/src/Range.hs

28 lines
1023 B
Haskell
Raw Normal View History

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)
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 $ startIndex + length word ]
(word, rest) -> (Range startIndex $ startIndex + length word) : rangesOfWordsAfterWhitespace (startIndex + length word) rest
2015-12-14 20:44:48 +03:00
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