Skip to content

Commit 7a8f1f9

Browse files
committed
Factor out surrogate pair predicate
1 parent ff2e987 commit 7a8f1f9

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

System/Console/Haskeline/Backend/Win32.hsc

+9-3
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,17 @@ eventReader h = do
7272
es <- readEvents h
7373
return $ combineSurrogatePairs $ mapMaybe processEvent es
7474

75+
isSurrogatePair :: Char -> Char -> Maybe Char
76+
isSurrogatePair c1 c2
77+
| 0xD800 <= ord c1 && ord c1 < 0xDC00 && 0xDC00 <= ord c2 && ord c2 < 0xE000
78+
= Just $ (((ord c1 .&. 0x3FF) `shiftL` 10) .|. (ord c2 .&. 0x3FF)) + 0x10000
79+
| otherwise
80+
= Nothing
81+
7582
combineSurrogatePairs :: [Event] -> [Event]
7683
combineSurrogatePairs (KeyInput [Key m1 (KeyChar c1)] : KeyInput [Key _ (KeyChar c2)] : es)
77-
| 0xD800 <= ord c1 && ord c1 < 0xDC00 && 0xDC00 <= ord c2 && ord c2 < 0xE000
78-
= let c = (((ord c1 .&. 0x3FF) `shiftL` 10) .|. (ord c2 .&. 0x3FF)) + 0x10000
79-
in KeyInput [Key m1 (KeyChar (chr c))] : combineSurrogatePairs es
84+
| Just c <- isSurrogatePair c1 c2
85+
= KeyInput [Key m1 (KeyChar (chr c))] : combineSurrogatePairs es
8086
combineSurrogatePairs (e:es) = e : combineSurrogatePairs es
8187
combineSurrogatePairs [] = []
8288

0 commit comments

Comments
 (0)