1{-# LANGUAGE MagicHash, BangPatterns #-}
2
3-- |
4-- Module      : Data.Text.Internal.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
11-- Stability   : experimental
12-- Portability : GHC
13--
14-- /Warning/: this is an internal module, and does not have a stable
15-- API or name. Functions in this module may not check or enforce
16-- preconditions expected by public modules. Use at your own risk!
17--
18-- Basic UTF-16 validation and character manipulation.
19module Data.Text.Internal.Encoding.Utf16
20    (
21      chr2
22    , validate1
23    , validate2
24    ) where
25
26import GHC.Exts
27import GHC.Word (Word16(..))
28
29chr2 :: Word16 -> Word16 -> Char
30chr2 (W16# a#) (W16# b#) = C# (chr# (upper# +# lower# +# 0x10000#))
31    where
32      !x# = word2Int# a#
33      !y# = word2Int# b#
34      !upper# = uncheckedIShiftL# (x# -# 0xD800#) 10#
35      !lower# = y# -# 0xDC00#
36{-# INLINE chr2 #-}
37
38validate1    :: Word16 -> Bool
39validate1 x1 = x1 < 0xD800 || x1 > 0xDFFF
40{-# INLINE validate1 #-}
41
42validate2       ::  Word16 -> Word16 -> Bool
43validate2 x1 x2 = x1 >= 0xD800 && x1 <= 0xDBFF &&
44                  x2 >= 0xDC00 && x2 <= 0xDFFF
45{-# INLINE validate2 #-}
46