@@ -222,6 +222,7 @@ import Control.Monad.ST.Unsafe (unsafeIOToST)
222
222
import qualified Data.Text.Array as A
223
223
import qualified Data.List as L
224
224
import Data.Binary (Binary (get , put ))
225
+ import Data.Int (Int8 )
225
226
import Data.Monoid (Monoid (.. ))
226
227
import Data.Semigroup (Semigroup (.. ))
227
228
import Data.String (IsString (.. ))
@@ -1922,16 +1923,26 @@ stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1922
1923
--
1923
1924
-- >>> commonPrefixes "" "baz"
1924
1925
-- Nothing
1925
- commonPrefixes :: Text -> Text -> Maybe (Text ,Text ,Text )
1926
- commonPrefixes t0@ (Text arr0 off0 len0) t1@ (Text arr1 off1 len1) = go 0 0
1926
+ commonPrefixes :: Text -> Text -> Maybe (Text , Text , Text )
1927
+ commonPrefixes ! t0@ (Text arr0 off0 len0) ! t1@ (Text arr1 off1 len1)
1928
+ | len0 == 0 = Nothing
1929
+ | len1 == 0 = Nothing
1930
+ | otherwise = go 0 0
1927
1931
where
1928
- go ! i ! j | i < len0 && j < len1 && a == b = go (i+ d0) (j+ d1)
1929
- | i > 0 = Just (Text arr0 off0 i,
1930
- text arr0 (off0+ i) (len0- i),
1931
- text arr1 (off1+ j) (len1- j))
1932
- | otherwise = Nothing
1933
- where Iter a d0 = iter t0 i
1934
- Iter b d1 = iter t1 j
1932
+ go ! i ! j
1933
+ | i == len0 = Just (t0, empty, text arr1 (off1 + i) (len1 - i))
1934
+ | i == len1 = Just (t1, text arr0 (off0 + i) (len0 - i), empty)
1935
+ | a == b = go (i + 1 ) k
1936
+ | k > 0 = Just (Text arr0 off0 k,
1937
+ Text arr0 (off0 + k) (len0 - k),
1938
+ Text arr1 (off1 + k) (len1 - k))
1939
+ | otherwise = Nothing
1940
+ where
1941
+ a = A. unsafeIndex arr0 (off0 + i)
1942
+ b = A. unsafeIndex arr1 (off1 + i)
1943
+ isLeader = word8ToInt8 a >= - 64
1944
+ k = if isLeader then i else j
1945
+ {-# INLINE commonPrefixes #-}
1935
1946
1936
1947
-- | /O(n)/ Return the prefix of the second string if its suffix
1937
1948
-- matches the entire first string.
@@ -2002,6 +2013,9 @@ intToCSize = P.fromIntegral
2002
2013
cSsizeToInt :: CSsize -> Int
2003
2014
cSsizeToInt = P. fromIntegral
2004
2015
2016
+ word8ToInt8 :: Word8 -> Int8
2017
+ word8ToInt8 = P. fromIntegral
2018
+
2005
2019
-------------------------------------------------
2006
2020
-- NOTE: the named chunk below used by doctest;
2007
2021
-- verify the doctests via `doctest -fobject-code Data/Text.hs`
0 commit comments