mirror of
https://github.com/github/semantic.git
synced 2024-12-01 09:15:01 +03:00
Rewrite with elemIndex
Should be a bit faster b/c elemIndex uses memchr(3) under the covers.
This commit is contained in:
parent
891a32f7d1
commit
ddb8c4f3b1
@ -110,20 +110,20 @@ sourceLineRangesWithin range = uncurry (zipWith Range)
|
|||||||
. sourceBytes
|
. sourceBytes
|
||||||
. slice range
|
. slice range
|
||||||
|
|
||||||
-- | Return all indices of newlines ('\n', '\r', '\r\n') in the 'ByteString'.
|
-- | Return all indices of newlines ('\n', '\r', and '\r\n') in the 'ByteString'.
|
||||||
newlineIndices :: B.ByteString -> [Int]
|
newlineIndices :: B.ByteString -> [Int]
|
||||||
newlineIndices = loop 0
|
newlineIndices = go 0
|
||||||
where
|
where go n bs | B.null bs = []
|
||||||
loop i bytes = case B.uncons bytes of
|
| otherwise = case (searchCR bs, searchLF bs) of
|
||||||
Nothing -> []
|
(Nothing, Nothing) -> []
|
||||||
Just (b, bs) | isLF b -> i : loop (succ i) bs
|
(Just i, Nothing) -> recur n i bs
|
||||||
| isCR b -> case B.uncons bs of
|
(Nothing, Just i) -> recur n i bs
|
||||||
Nothing -> loop (succ i) bs
|
(Just crI, Just lfI)
|
||||||
Just (b', bs') | isLF b' -> succ i : loop (succ (succ i)) bs'
|
| succ crI == lfI -> recur n lfI bs
|
||||||
| otherwise -> i : loop (succ i) bs
|
| otherwise -> recur n (min crI lfI) bs
|
||||||
| otherwise -> loop (succ i) bs
|
recur n i bs = let j = n + i in j : go (succ j) (B.drop (succ i) bs)
|
||||||
isLF = (== toEnum (ord '\n'))
|
searchLF = B.elemIndex (toEnum (ord '\n'))
|
||||||
isCR = (== toEnum (ord '\r'))
|
searchCR = B.elemIndex (toEnum (ord '\r'))
|
||||||
|
|
||||||
{-# INLINE newlineIndices #-}
|
{-# INLINE newlineIndices #-}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user