codesnippets:octets
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| codesnippets:octets [2021/04/22 12:51] – f2b216 | codesnippets:octets [2025/10/08 00:48] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Octets ====== | ====== Octets ====== | ||
| - | ~~DISCUSSION~~ | ||
| * type '' | * type '' | ||
| - | * with type '' | + | * with type '' |
| * with instances for '' | * with instances for '' | ||
| - | * as an example | + | * example |
| - | * with test execution | + | |
| * code, of module '' | * code, of module '' | ||
| {-# LANGUAGE BinaryLiterals #-} | {-# LANGUAGE BinaryLiterals #-} | ||
| Line 129: | Line 127: | ||
| </ | </ | ||
| * code, of module '' | * code, of module '' | ||
| - | import qualified | + | import qualified |
| main :: IO () | main :: IO () | ||
| Line 170: | Line 168: | ||
| printExample (-hugeInteger) | printExample (-hugeInteger) | ||
| - | printExample :: Oct.OctetsVertable | + | printExample :: Oct.Octetable |
| printExample x = | printExample x = | ||
| let | let | ||
| Line 333: | Line 331: | ||
| | | ||
| </ | </ | ||
| - | | + | * example, used with module FileSystem |
| - | {-# LANGUAGE BinaryLiterals #-} | + | |
| - | + | import qualified FileSystem as FS | |
| - | module Octetable | + | import qualified |
| - | ( | + | import qualified |
| - | Octetable(..), | + | |
| - | Octet, | + | |
| - | Octets, | + | |
| - | getOctets, | + | |
| - | putOctets | + | |
| - | ) where | + | |
| - | + | ||
| - | import qualified | + | |
| - | import qualified | + | |
| import qualified Data.Char as Chr | import qualified Data.Char as Chr | ||
| - | import qualified System.IO as SysIo | ||
| - | import qualified Data.ByteString as BS | ||
| - | import | ||
| - | type Octet = W.Word8 | + | main :: IO () |
| + | main = | ||
| + | FS.processData | ||
| + | (FS.DataReader (FS.FileInp " | ||
| + | [ | ||
| + | FS.DataWriter (FS.FileOut " | ||
| + | FS.DataWriter FS.StdOut (hReplaceNPut octUnderscore octBlank), | ||
| + | FS.DataWriter FS.StdErr Oct.putOctets | ||
| + | ] | ||
| - | type Octets = [Octet] | + | octBlank :: Oct.Octet |
| + | octBlank = ((fromIntegral . Chr.ord) ' ') | ||
| - | getOctets | + | octUnderscore |
| - | getOctets | + | octUnderscore |
| - | putOctets | + | hGetNReplace |
| - | putOctets hOut octs = BS.hPut hOut (BS.pack octs) | + | hGetNReplace a b h = |
| + | do | ||
| + | s <- Oct.getOctets h | ||
| + | return | ||
| - | class (Eq a, Show a) => Octetable | + | hReplaceNPut :: Oct.Octet -> Oct.Octet -> SysIo.Handle -> Oct.Octets -> IO () |
| - | | + | hReplaceNPut a b h s = Oct.putOctets h (fmap (\x -> if x == a then b else x) s) |
| - | | + | </ |
| + | | ||
| + | * input, file " | ||
| + | The quick brown fox jumps over the lazy dog. | ||
| + | </code> | ||
| + | | ||
| + | The quick brown fox jumps over the lazy dog. | ||
| + | The_quick_brown_fox_jumps_over_the_lazy_dog. | ||
| + | </ | ||
| + | * executes, with output file " | ||
| + | The_quick_brown_fox_jumps_over_the_lazy_dog. | ||
| + | </code> | ||
| - | instance Octetable W.Word32 where | ||
| - | toOctets w32 = | ||
| - | [ fromIntegral (w32 `Bts.shiftR` 24) | ||
| - | , fromIntegral (w32 `Bts.shiftR` 16) | ||
| - | , fromIntegral (w32 `Bts.shiftR` 8) | ||
| - | , fromIntegral w32 | ||
| - | ] | ||
| - | fromOctets lw = foldl (\w32 w8 -> (w32 `Bts.shiftL` 8) + (fromIntegral w8)) 0 lw | ||
| - | |||
| - | instance Octetable Char where | ||
| - | toOctets ch = (fIntegralToOctets . Chr.ord) ch | ||
| - | fromOctets [] = ' | ||
| - | fromOctets lw = Chr.chr (integralFromOctets lw) | ||
| - | |||
| - | {-| UTF-8 character | ||
| - | |||
| - | * prefix: cu8 | ||
| - | * represent on UTF-8 character | ||
| - | * keeps one, two, three or four character | ||
| - | -} | ||
| - | data Utf8Char = | ||
| - | OneByte (W.Word8) | ||
| - | | TwoBytes (W.Word8, W.Word8) | ||
| - | | ThreeBytes (W.Word8, W.Word8, W.Word8) | ||
| - | | FourBytes (W.Word8, W.Word8, W.Word8, W.Word8) | ||
| - | deriving (Show, Eq) | ||
| - | |||
| - | instance Octetable Utf8Char where | ||
| - | toOctets (OneByte w81) = [w81] | ||
| - | toOctets (TwoBytes (w81,w82)) = [w81,w82] | ||
| - | toOctets (ThreeBytes (w81, | ||
| - | toOctets (FourBytes (w81, | ||
| - | fromOctets (w81: | ||
| - | | (w81 .&. 0b10000000) == 0b00000000 = OneByte w81 | ||
| - | | (w81 .&. 0b11100000) == 0b11000000 = TwoBytes (w81,w82) | ||
| - | | (w81 .&. 0b11110000) == 0b11100000 = ThreeBytes (w81, | ||
| - | | otherwise | ||
| - | fromOctets (w81: | ||
| - | | (w81 .&. 0b10000000) == 0b00000000 = OneByte w81 | ||
| - | | (w81 .&. 0b11100000) == 0b11000000 = TwoBytes (w81,w82) | ||
| - | | otherwise | ||
| - | fromOctets (w81: | ||
| - | | (w81 .&. 0b10000000) == 0b00000000 = OneByte w81 | ||
| - | | otherwise | ||
| - | fromOctets (w81:[]) = OneByte w81 | ||
| - | fromOctets [] = (OneByte 0) | ||
| - | |||
| - | instance Octetable Integer where | ||
| - | toOctets n = fIntegralToOctets n | ||
| - | fromOctets lw = integralFromOctets lw | ||
| - | |||
| - | instance Octetable Int where | ||
| - | toOctets n = fIntegralToOctets n | ||
| - | fromOctets lw = integralFromOctets lw | ||
| - | |||
| - | fIntegralToOctets :: (Integral a) => a -> Octets | ||
| - | fIntegralToOctets n | ||
| - | | n > 0 = | ||
| - | let (_, lw8) = fIntegralToOctets' | ||
| - | in keepOctetsPositive lw8 | ||
| - | | n < 0 = | ||
| - | let (_, lw8) = fIntegralToOctets' | ||
| - | in (keepOctetsNegative . (fmap Bts.complement)) lw8 | ||
| - | | otherwise = [] | ||
| - | |||
| - | fIntegralToOctets' | ||
| - | fIntegralToOctets' | ||
| - | tpl | ||
| - | fIntegralToOctets' | ||
| - | fIntegralToOctets' | ||
| - | |||
| - | keepOctetsPositive :: Octets -> Octets | ||
| - | keepOctetsPositive [] = [] | ||
| - | keepOctetsPositive lw8 | ||
| - | | Bts.testBit (head lw8) 7 = 0 : lw8 -- highest bit is set, hence would be interpreted as negative by integralFromOctets -> append 0x00 to keep positive | ||
| - | | otherwise | ||
| - | |||
| - | keepOctetsNegative :: Octets -> Octets | ||
| - | keepOctetsNegative [] = [] | ||
| - | keepOctetsNegative lw8 | ||
| - | | Bts.testBit (head lw8) 7 = lw8 -- highest bit is set, hence would be interpreted as negative by integralFromOctets -> append 0x00 to keep positive | ||
| - | | otherwise | ||
| - | |||
| - | integralFromOctets :: (Integral a) => Octets -> a | ||
| - | integralFromOctets lw8 | ||
| - | | Bts.testBit (head lw8) 7 = (negate . (+1) . integralFromOctets . (fmap Bts.complement)) lw8 | ||
| - | | otherwise | ||
| - | </ | ||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/octets.1619088711.txt.gz · Last modified: (external edit)
