1{-# LANGUAGE BangPatterns #-}
4-- Module : Data.Text.Encoding.Fusion.Common
5-- Copyright : (c) Tom Harper 2008-2009,
6-- (c) Bryan O'Sullivan 2009,
7-- (c) Duncan Coutts 2009,
8-- (c) Jasper Van der Jeugt 2011
11-- Maintainer : bos@serpentine.com, rtomharper@googlemail.com,
13-- Stability : experimental
14-- Portability : portable
16-- Fusible 'Stream'-oriented functions for converting between 'Text'
17-- and several common encodings.
19module Data.Text.Encoding.Fusion.Common
22 -- Restreaming is the act of converting from one 'Stream'
23 -- representation to another.
31import Data.Bits ((.&.))
32import Data.Text.Fusion (Step(..), Stream(..))
33import Data.Text.Fusion.Internal (RS(..))
34import Data.Text.UnsafeChar (ord)
35import Data.Text.UnsafeShift (shiftR)
36import Data.Word (Word8)
37import qualified Data.Text.Encoding.Utf8 as U8
39-- | /O(n)/ Convert a Stream Char into a UTF-8 encoded Stream Word8.
40restreamUtf8 :: Stream Char -> Stream Word8
41restreamUtf8 (Stream next0 s0 len) = Stream next (RS0 s0) (len * 2)
43 next (RS0 s) = case next0 s of
45 Skip s' -> Skip (RS0 s')
47 | n <= 0x7F -> Yield c (RS0 s')
48 | n <= 0x07FF -> Yield a2 (RS1 s' b2)
49 | n <= 0xFFFF -> Yield a3 (RS2 s' b3 c3)
50 | otherwise -> Yield a4 (RS3 s' b4 c4 d4)
55 (a3,b3,c3) = U8.ord3 x
56 (a4,b4,c4,d4) = U8.ord4 x
57 next (RS1 s x2) = Yield x2 (RS0 s)
58 next (RS2 s x2 x3) = Yield x2 (RS1 s x3)
59 next (RS3 s x2 x3 x4) = Yield x2 (RS2 s x3 x4)
61{-# INLINE restreamUtf8 #-}
63restreamUtf16BE :: Stream Char -> Stream Word8
64restreamUtf16BE (Stream next0 s0 len) = Stream next (RS0 s0) (len * 2)
66 next (RS0 s) = case next0 s of
68 Skip s' -> Skip (RS0 s')
70 | n < 0x10000 -> Yield (fromIntegral $ n `shiftR` 8) $
71 RS1 s' (fromIntegral n)
72 | otherwise -> Yield c1 $ RS3 s' c2 c3 c4
76 c1 = fromIntegral (n1 `shiftR` 18 + 0xD8)
77 c2 = fromIntegral (n1 `shiftR` 10)
79 c3 = fromIntegral (n2 `shiftR` 8 + 0xDC)
81 next (RS1 s x2) = Yield x2 (RS0 s)
82 next (RS2 s x2 x3) = Yield x2 (RS1 s x3)
83 next (RS3 s x2 x3 x4) = Yield x2 (RS2 s x3 x4)
85{-# INLINE restreamUtf16BE #-}
87restreamUtf16LE :: Stream Char -> Stream Word8
88restreamUtf16LE (Stream next0 s0 len) = Stream next (RS0 s0) (len * 2)
90 next (RS0 s) = case next0 s of
92 Skip s' -> Skip (RS0 s')
94 | n < 0x10000 -> Yield (fromIntegral n) $
95 RS1 s' (fromIntegral $ shiftR n 8)
96 | otherwise -> Yield c1 $ RS3 s' c2 c3 c4
100 c2 = fromIntegral (shiftR n1 18 + 0xD8)
101 c1 = fromIntegral (shiftR n1 10)
103 c4 = fromIntegral (shiftR n2 8 + 0xDC)
105 next (RS1 s x2) = Yield x2 (RS0 s)
106 next (RS2 s x2 x3) = Yield x2 (RS1 s x3)
107 next (RS3 s x2 x3 x4) = Yield x2 (RS2 s x3 x4)
109{-# INLINE restreamUtf16LE #-}
111restreamUtf32BE :: Stream Char -> Stream Word8
112restreamUtf32BE (Stream next0 s0 len) = Stream next (RS0 s0) (len * 2)
114 next (RS0 s) = case next0 s of
116 Skip s' -> Skip (RS0 s')
117 Yield x s' -> Yield c1 (RS3 s' c2 c3 c4)
120 c1 = fromIntegral $ shiftR n 24
121 c2 = fromIntegral $ shiftR n 16
122 c3 = fromIntegral $ shiftR n 8
124 next (RS1 s x2) = Yield x2 (RS0 s)
125 next (RS2 s x2 x3) = Yield x2 (RS1 s x3)
126 next (RS3 s x2 x3 x4) = Yield x2 (RS2 s x3 x4)
128{-# INLINE restreamUtf32BE #-}
130restreamUtf32LE :: Stream Char -> Stream Word8
131restreamUtf32LE (Stream next0 s0 len) = Stream next (RS0 s0) (len * 2)
133 next (RS0 s) = case next0 s of
135 Skip s' -> Skip (RS0 s')
136 Yield x s' -> Yield c1 (RS3 s' c2 c3 c4)
139 c4 = fromIntegral $ shiftR n 24
140 c3 = fromIntegral $ shiftR n 16
141 c2 = fromIntegral $ shiftR n 8
143 next (RS1 s x2) = Yield x2 (RS0 s)
144 next (RS2 s x2 x3) = Yield x2 (RS1 s x3)
145 next (RS3 s x2 x3 x4) = Yield x2 (RS2 s x3 x4)
147{-# INLINE restreamUtf32LE #-}
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list