on master help sign in
Merge pull request #80 from glguy/utf8-input-patch

Avoid discarding input bytes after multi-byte encoded codepoint

commita90eb921d93492bec09dc8c957b8ffb379a0e587 authorCorey O'Connor <coreyoconnor@gmail.com> dateSat 8 Aug 2015, 13:59 (11 years ago) parent0a9f5df78c1940
1 file changed+14−7
src/Graphics/Vty/Input/Classify.hs+14−7
src/Graphics/Vty/Input/Classify.hs+14−7
@@ -60,14 +60,21 @@ classify :: ClassifyMap -> [Char] -> KClass
6060classify table =
6161 let standardClassifier = compile table
6262 in \s -> case s of
63− (c:_) | ord c >= 0xC2 && utf8Length (ord c) > length s -> Prefix -- beginning of an utf8 sequence
64− (c:_) | ord c >= 0xC2 -> classifyUtf8 s -- As soon as
65− _ -> standardClassifier s
63+ c:cs | ord c >= 0xC2 -> classifyUtf8 c cs
64+ _ -> standardClassifier s
6665
67−classifyUtf8 :: [Char] -> KClass
68−classifyUtf8 s = case decode ((map (fromIntegral . ord) s) :: [Word8]) of
69− Just (unicodeChar, _) -> Valid (EvKey (KChar unicodeChar) []) []
70− _ -> Invalid -- something bad happened; just ignore and continue.
66+classifyUtf8 :: Char -> [Char] -> KClass
67+classifyUtf8 c cs =
68+ let n = utf8Length (ord c)
69+ (codepoint,rest) = splitAt n (c:cs)
70+
71+ codepoint8 :: [Word8]
72+ codepoint8 = map (fromIntegral . ord) codepoint
73+
74+ in case decode codepoint8 of
75+ _ | n < length codepoint -> Prefix
76+ Just (unicodeChar, _) -> Valid (EvKey (KChar unicodeChar) []) rest
77+ Nothing -> Invalid -- something bad happened; just ignore and continue.
7178
7279utf8Length :: (Num t, Ord a, Num a) => a -> t
7380utf8Length c