on 3cf8564 help sign in
42 lines 1.1 kB history raw
1{-# LANGUAGE MagicHash, BangPatterns #-}
3-- |
4-- Module : Data.Text.Encoding.Utf16
5-- Copyright : (c) 2008, 2009 Tom Harper,
6-- (c) 2009 Bryan O'Sullivan,
7-- (c) 2009 Duncan Coutts
8--
9-- License : BSD-style
10-- Maintainer : bos@serpentine.com, rtomharper@googlemail.com,
11-- duncan@haskell.org
12-- Stability : experimental
13-- Portability : GHC
14--
15-- Basic UTF-16 validation and character manipulation.
16module Data.Text.Encoding.Utf16
17 (
18 chr2
19 , validate1
20 , validate2
21 ) where
23import GHC.Exts
24import GHC.Word (Word16(..))
26chr2 :: Word16 -> Word16 -> Char
27chr2 (W16# a#) (W16# b#) = C# (chr# (upper# +# lower# +# 0x10000#))
28 where
29 !x# = word2Int# a#
30 !y# = word2Int# b#
31 !upper# = uncheckedIShiftL# (x# -# 0xD800#) 10#
32 !lower# = y# -# 0xDC00#
33{-# INLINE chr2 #-}
35validate1 :: Word16 -> Bool
36validate1 x1 = x1 < 0xD800 || x1 > 0xDFFF
37{-# INLINE validate1 #-}
39validate2 :: Word16 -> Word16 -> Bool
40validate2 x1 x2 = x1 >= 0xD800 && x1 <= 0xDBFF &&
41 x2 >= 0xDC00 && x2 <= 0xDFFF
42{-# INLINE validate2 #-}