@@ -31,7 +31,7 @@ import Data.String as String
3131import Data.String (Pattern (..), Replacement (..), charAt , charCodeAt , contains , fromCharArray , joinWith , localeCompare , null , replace , replaceAll , split , stripPrefix , stripSuffix , toChar , toCharArray , toLower , toUpper , trim ) as StringReExports
3232import Data.Tuple (Tuple (Tuple))
3333import Data.Unfoldable (unfoldr )
34- import Prelude (class Eq , class Ord , (&&), (||), (*), (+), (-), (<$>), (<), (<=), (<<<), (/), (<>), mod )
34+ import Prelude (class Eq , class Ord , (&&), (||), (*), (+), (-), (<$>), (<), (<=), (<<<), (/), (<>), (==), mod )
3535
3636
3737newtype CodePoint = CodePoint Int
@@ -48,8 +48,8 @@ codePointToInt :: CodePoint -> Int
4848codePointToInt (CodePoint n) = n
4949
5050codePointFromSurrogatePair :: Int -> Int -> Maybe CodePoint
51- codePointFromSurrogatePair lead trail | isLead lead && isTrail trail
52- = Just (unsurrogate lead trail)
51+ codePointFromSurrogatePair lead trail | isLead lead && isTrail trail =
52+ Just (unsurrogate lead trail)
5353codePointFromSurrogatePair _ _ = Nothing
5454
5555unsurrogate :: Int -> Int -> CodePoint
@@ -64,14 +64,34 @@ isTrail cu = 0xDC00 <= cu && cu <= 0xDFFF
6464fromCharCode :: Int -> String
6565fromCharCode = String .singleton <<< Char .fromCharCode
6666
67+ unsafeCodePointAt0 :: String -> CodePoint
68+ unsafeCodePointAt0 = _unsafeCodePointAt0 unsafeCodePointAt0Fallback
69+
70+ foreign import _unsafeCodePointAt0
71+ :: (String -> CodePoint )
72+ -> String
73+ -> CodePoint
74+
75+ unsafeCodePointAt0Fallback :: String -> CodePoint
76+ unsafeCodePointAt0Fallback s | String .length s == 1 = CodePoint (_unsafeCharCodeAt 0 s)
77+ unsafeCodePointAt0Fallback s = CodePoint (((lead - 0xD800 ) * 0x400 ) + (trail - 0xDC00 ) + 0x10000 )
78+ where
79+ lead = _unsafeCharCodeAt 0 s
80+ trail = _unsafeCharCodeAt 1 s
81+
82+ foreign import _unsafeCharCodeAt :: Int -> String -> Int
83+
6784
6885codePointAt :: Int -> String -> Maybe CodePoint
69- codePointAt = _codePointAt codePointAtFallback Just Nothing
86+ codePointAt 0 " " = Nothing
87+ codePointAt 0 s = Just (unsafeCodePointAt0 s)
88+ codePointAt n s = _codePointAt codePointAtFallback Just Nothing unsafeCodePointAt0 n s
7089
7190foreign import _codePointAt
7291 :: (Int -> String -> Maybe CodePoint )
7392 -> (forall a . a -> Maybe a )
7493 -> (forall a . Maybe a )
94+ -> (String -> CodePoint )
7595 -> Int
7696 -> String
7797 -> Maybe CodePoint
@@ -174,10 +194,11 @@ takeWhile p s = take (count p s) s
174194
175195
176196toCodePointArray :: String -> Array CodePoint
177- toCodePointArray = _toCodePointArray toCodePointArrayFallback
197+ toCodePointArray = _toCodePointArray toCodePointArrayFallback unsafeCodePointAt0
178198
179199foreign import _toCodePointArray
180200 :: (String -> Array CodePoint )
201+ -> (String -> CodePoint )
181202 -> String
182203 -> Array CodePoint
183204
0 commit comments