@@ -22,7 +22,7 @@ module Data.String.CodePoints
2222 ) where
2323
2424import Data.Array as Array
25- import Data.Char ( toCharCode )
25+ import Data.Char as Char
2626import Data.List (List (Cons, Nil), fromFoldable )
2727import Data.Maybe (Maybe (Just, Nothing))
2828import Data.Newtype (class Newtype )
@@ -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 , (&&), (||), (*), (+), (-), (<$>), (<), (<=), (<<<))
34+ import Prelude (class Eq , class Ord , (&&), (||), (*), (+), (-), (<$>), (<), (<=), (<<<), (/), (<>), mod )
3535
3636
3737newtype CodePoint = CodePoint Int
@@ -61,6 +61,9 @@ isLead cu = 0xD800 <= cu && cu <= 0xDBFF
6161isTrail :: Int -> Boolean
6262isTrail cu = 0xDC00 <= cu && cu <= 0xDFFF
6363
64+ fromCharCode :: Int -> String
65+ fromCharCode = String .singleton <<< Char .fromCharCode
66+
6467
6568codePointAt :: Int -> String -> Maybe CodePoint
6669codePointAt = _codePointAt codePointAtFallback Just Nothing
@@ -97,7 +100,13 @@ dropWhile :: (CodePoint -> Boolean) -> String -> String
97100dropWhile p s = drop (count p s) s
98101
99102
100- foreign import fromCodePointArray :: Array CodePoint -> String
103+ fromCodePointArray :: Array CodePoint -> String
104+ fromCodePointArray = _fromCodePointArray singletonFallback
105+
106+ foreign import _fromCodePointArray
107+ :: (CodePoint -> String )
108+ -> Array CodePoint
109+ -> String
101110
102111
103112indexOf :: String.Pattern -> String -> Maybe Int
@@ -124,7 +133,20 @@ length :: String -> Int
124133length = Array .length <<< toCodePointArray
125134
126135
127- foreign import singleton :: CodePoint -> String
136+ singleton :: CodePoint -> String
137+ singleton = _singleton singletonFallback
138+
139+ foreign import _singleton
140+ :: (CodePoint -> String )
141+ -> CodePoint
142+ -> String
143+
144+ singletonFallback :: CodePoint -> String
145+ singletonFallback (CodePoint cp) | cp <= 0xFFFF = fromCharCode cp
146+ singletonFallback (CodePoint cp) = fromCharCode lead <> fromCharCode trail
147+ where
148+ lead = ((cp - 0x10000 ) / 0x400 ) + 0xD800
149+ trail = (cp - 0x10000 ) `mod` 0x400 + 0xDC00
128150
129151
130152splitAt :: Int -> String -> Maybe { before :: String , after :: String }
@@ -160,7 +182,7 @@ foreign import _toCodePointArray
160182 -> Array CodePoint
161183
162184toCodePointArrayFallback :: String -> Array CodePoint
163- toCodePointArrayFallback s = unfoldr decode (fromFoldable (toCharCode <$> String .toCharArray s))
185+ toCodePointArrayFallback s = unfoldr decode (fromFoldable (Char . toCharCode <$> String .toCharArray s))
164186 where
165187 decode :: List Int -> Maybe (Tuple CodePoint (List Int ))
166188 decode (Cons h (Cons l rest)) | isLead h && isTrail l
0 commit comments