1 {-# LANGUAGE MagicHash #-}
    2 
    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.
   16 module Data.Text.Encoding.Utf16
   17     (
   18       chr2
   19     , validate1
   20     , validate2
   21     ) where
   22 
   23 import GHC.Exts
   24 import GHC.Word (Word16(..))
   25 
   26 chr2 :: Word16 -> Word16 -> Char
   27 -- entered 53,899 timeschr2 (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 #-}
   34 
   35 validate1    :: Word16 -> Bool
   36 -- entered 10,492 timesvalidate1 x1 = x1 < 0xD800 || x1 > 0xDFFF
   37 {-# INLINE validate1 #-}
   38 
   39 validate2       ::  Word16 -> Word16 -> Bool
   40 -- entered 6300 timesvalidate2 x1 x2 = x1 >= 0xD800 && x1 <= 0xDBFF &&
   41                   x2 >= 0xDC00 && x2 <= 0xDFFF
   42 {-# INLINE validate2 #-}