Optimize isRangeValid

Summary:
`isRangeValid` was doing lots of random indexing inside a Text.
Since we already have a convenient O(1), indexable `Vector Char`
we can just use it instead.

Reviewed By: patapizza

Differential Revision: D4744297

fbshipit-source-id: b23011b
This commit is contained in:
Bartosz Nitka 2017-03-21 08:38:17 -07:00 committed by Facebook Github Bot
parent 58bf36b9f4
commit 56a039eef1

View File

@ -25,6 +25,7 @@ import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Data.List as L
import qualified Data.Vector.Primitive as Vector
import Prelude
import qualified Text.Regex.Base as R
import qualified Text.Regex.PCRE as PCRE
@ -62,9 +63,10 @@ produce (Rule name _ production, _, etuor@(Node {nodeRange = Range _ e}:_)) = do
-- As regexes are matched without whitespace delimitator, we need to check
-- the reasonability of the match to actually be a word.
isRangeValid :: Document -> Range -> Bool
isRangeValid Document { rawInput = s } (Range start end) =
(start == 0 || isDifferent (Text.index s (start - 1)) (Text.index s start)) &&
(end == Text.length s || isDifferent (Text.index s (end - 1)) (Text.index s end))
isRangeValid Document { indexable = s } (Range start end) =
(start == 0 || isDifferent (s Vector.! (start - 1)) (s Vector.! start)) &&
(end == Vector.length s ||
isDifferent (s Vector.! (end - 1)) (s Vector.! end))
where
charClass :: Char -> Char
charClass c