2015-12-03 05:40:34 +03:00
module Range where
2015-12-15 00:23:09 +03:00
import Control.Applicative ( ( <|> ) )
2015-12-14 20:44:48 +03:00
import qualified Data.Char as Char
2015-12-24 01:48:43 +03:00
import Data.Maybe ( fromMaybe )
2015-12-14 20:44:48 +03:00
2015-12-21 20:23:21 +03:00
-- | A half-open interval of integers, defined by start & end indices.
2015-12-24 01:41:35 +03:00
data Range = Range { start :: ! Int , end :: ! Int }
2015-12-03 05:40:34 +03:00
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 23:37:30 +03:00
rangesAndWordsFrom :: Int -> String -> [ ( Range , String ) ]
2015-12-15 00:20:42 +03:00
rangesAndWordsFrom _ " " = []
2015-12-24 01:48:43 +03:00
rangesAndWordsFrom startIndex string = fromMaybe [] $ takeAndContinue <$> ( word <|> punctuation ) <|> skipAndContinue <$> space
2015-12-14 20:44:48 +03:00
where
2015-12-15 00:32:24 +03:00
word = parse isWord string
punctuation = parse ( not . isWordOrSpace ) string
space = parse Char . isSpace string
2015-12-15 01:18:24 +03:00
takeAndContinue ( parsed , rest ) = ( Range startIndex $ endFor parsed , parsed ) : rangesAndWordsFrom ( endFor parsed ) rest
skipAndContinue ( parsed , rest ) = rangesAndWordsFrom ( endFor parsed ) rest
endFor parsed = startIndex + length parsed
2015-12-15 00:07:49 +03:00
parse predicate string = case span predicate string of
( [] , _ ) -> Nothing
( parsed , rest ) -> Just ( parsed , rest )
2015-12-15 00:31:29 +03:00
isWordOrSpace c = Char . isSpace c || isWord c
2015-12-14 23:02:09 +03:00
-- | Is this a word character?
-- | Word characters are defined as in [Ruby’ s `\p{Word}` syntax](http://ruby-doc.org/core-2.1.1/Regexp.html#class-Regexp-label-Character+Properties), i.e.:
-- | > A member of one of the following Unicode general category _Letter_, _Mark_, _Number_, _Connector_Punctuation_
2015-12-14 23:01:38 +03:00
isWord c = Char . isLetter c || Char . isNumber c || Char . isMark c || Char . generalCategory c == Char . ConnectorPunctuation
2015-12-21 20:22:52 +03:00
-- | Return Just the last index from a non-empty range, or if the range is empty, Nothing.
2015-12-21 20:22:20 +03:00
maybeLastIndex :: Range -> Maybe Int
maybeLastIndex ( Range start end ) | start == end = Nothing
maybeLastIndex ( Range _ end ) = Just $ end - 1
2015-12-14 20:44:48 +03:00
2015-12-03 05:40:34 +03:00
instance Ord Range where
a <= b = start a <= start b