Skip to content

Commit 2c2418a

Browse files
move fromCodePoint from JS to purs
1 parent 71cdcf2 commit 2c2418a

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

src/Data/String/CodePoints.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ var hasStringIterator =
1010
var hasFromCodePoint = typeof String.prototype.fromCodePoint === "function";
1111
var hasCodePointAt = typeof String.prototype.codePointAt === "function";
1212

13-
function fromCodePoint(cp) {
14-
if (cp <= 0xFFFF) return String.fromCharCode(cp);
15-
var cu1 = String.fromCharCode(Math.floor((cp - 0x10000) / 0x400) + 0xD800);
16-
var cu2 = String.fromCharCode((cp - 0x10000) % 0x400 + 0xDC00);
17-
return cu1 + cu2;
18-
}
19-
2013
var codePointAt0 = hasCodePointAt
2114
? function (str) { return str.codePointAt(0); }
2215
: function (str) {
@@ -78,12 +71,16 @@ exports._count = function (isLead) {
7871
};
7972
};
8073

81-
exports.fromCodePointArray = hasFromCodePoint
82-
// TODO: using F.p.apply here will fail for very large strings; use alternative implementation for very large strings
83-
? function (cps) { return String.fromCodePoint.apply(String, cps); }
84-
: function (cps) { return cps.map(fromCodePoint).join(""); };
74+
exports._fromCodePointArray = function (singleton) {
75+
return hasFromCodePoint
76+
// TODO: using F.p.apply here will fail for very large strings; use alternative implementation for very large strings
77+
? function (cps) { return String.fromCodePoint.apply(String, cps); }
78+
: function (cps) { return cps.map(singleton).join(""); };
79+
};
8580

86-
exports.singleton = hasFromCodePoint ? String.fromCodePoint : fromCodePoint;
81+
exports._singleton = function (fallback) {
82+
return hasFromCodePoint ? String.fromCodePoint : fallback;
83+
};
8784

8885
exports._take = function (fallback) {
8986
return function (n) {

src/Data/String/CodePoints.purs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module Data.String.CodePoints
2222
) where
2323

2424
import Data.Array as Array
25-
import Data.Char (toCharCode)
25+
import Data.Char as Char
2626
import Data.List (List(Cons, Nil), fromFoldable)
2727
import Data.Maybe (Maybe(Just, Nothing))
2828
import Data.Newtype (class Newtype)
@@ -31,7 +31,7 @@ import Data.String as String
3131
import Data.String (Pattern(..), Replacement(..), charAt, charCodeAt, contains, fromCharArray, joinWith, localeCompare, null, replace, replaceAll, split, stripPrefix, stripSuffix, toChar, toCharArray, toLower, toUpper, trim) as StringReExports
3232
import Data.Tuple (Tuple(Tuple))
3333
import Data.Unfoldable (unfoldr)
34-
import Prelude (class Eq, class Ord, (&&), (||), (*), (+), (-), (<$>), (<), (<=), (<<<))
34+
import Prelude (class Eq, class Ord, (&&), (||), (*), (+), (-), (<$>), (<), (<=), (<<<), (/), (<>), mod)
3535

3636

3737
newtype CodePoint = CodePoint Int
@@ -61,6 +61,9 @@ isLead cu = 0xD800 <= cu && cu <= 0xDBFF
6161
isTrail :: Int -> Boolean
6262
isTrail cu = 0xDC00 <= cu && cu <= 0xDFFF
6363

64+
fromCharCode :: Int -> String
65+
fromCharCode = String.singleton <<< Char.fromCharCode
66+
6467

6568
codePointAt :: Int -> String -> Maybe CodePoint
6669
codePointAt = _codePointAt codePointAtFallback Just Nothing
@@ -97,7 +100,13 @@ dropWhile :: (CodePoint -> Boolean) -> String -> String
97100
dropWhile 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

103112
indexOf :: String.Pattern -> String -> Maybe Int
@@ -124,7 +133,20 @@ length :: String -> Int
124133
length = 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

130152
splitAt :: Int -> String -> Maybe { before :: String, after :: String }
@@ -160,7 +182,7 @@ foreign import _toCodePointArray
160182
-> Array CodePoint
161183

162184
toCodePointArrayFallback :: 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

Comments
 (0)