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/20 13:06] – 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 #-} | ||
| - | module | + | module |
| ( | ( | ||
| - | | + | |
| + | Octet, | ||
| Octets, | Octets, | ||
| getOctets, | getOctets, | ||
| putOctets | putOctets | ||
| ) where | ) where | ||
| + | |||
| import qualified Data.Word as W | import qualified Data.Word as W | ||
| import qualified Data.Bits as Bts | import qualified Data.Bits as Bts | ||
| Line 25: | Line 24: | ||
| import | import | ||
| - | type Octets | + | type Octet = W.Word8 |
| + | |||
| + | type Octets = [Octet] | ||
| getOctets :: SysIo.Handle -> IO [W.Word8] | getOctets :: SysIo.Handle -> IO [W.Word8] | ||
| Line 33: | Line 34: | ||
| putOctets hOut octs = BS.hPut hOut (BS.pack octs) | putOctets hOut octs = BS.hPut hOut (BS.pack octs) | ||
| - | class (Eq a, Show a) => OctetsVertable | + | class (Eq a, Show a) => Octetable |
| toOctets :: a -> Octets | toOctets :: a -> Octets | ||
| fromOctets :: Octets -> a | fromOctets :: Octets -> a | ||
| - | instance | + | instance |
| toOctets w32 = | toOctets w32 = | ||
| [ fromIntegral (w32 `Bts.shiftR` 24) | [ fromIntegral (w32 `Bts.shiftR` 24) | ||
| Line 46: | Line 47: | ||
| fromOctets lw = foldl (\w32 w8 -> (w32 `Bts.shiftL` 8) + (fromIntegral w8)) 0 lw | fromOctets lw = foldl (\w32 w8 -> (w32 `Bts.shiftL` 8) + (fromIntegral w8)) 0 lw | ||
| - | instance | + | instance |
| toOctets ch = (fIntegralToOctets . Chr.ord) ch | toOctets ch = (fIntegralToOctets . Chr.ord) ch | ||
| fromOctets [] = ' | fromOctets [] = ' | ||
| Line 64: | Line 65: | ||
| deriving (Show, Eq) | deriving (Show, Eq) | ||
| - | instance | + | instance |
| toOctets (OneByte w81) = [w81] | toOctets (OneByte w81) = [w81] | ||
| toOctets (TwoBytes (w81,w82)) = [w81,w82] | toOctets (TwoBytes (w81,w82)) = [w81,w82] | ||
| Line 84: | Line 85: | ||
| fromOctets [] = (OneByte 0) | fromOctets [] = (OneByte 0) | ||
| - | instance | + | instance |
| toOctets n = fIntegralToOctets n | toOctets n = fIntegralToOctets n | ||
| fromOctets lw = integralFromOctets lw | fromOctets lw = integralFromOctets lw | ||
| - | instance | + | instance |
| toOctets n = fIntegralToOctets n | toOctets n = fIntegralToOctets n | ||
| fromOctets lw = integralFromOctets lw | fromOctets lw = integralFromOctets lw | ||
| Line 126: | Line 127: | ||
| </ | </ | ||
| * code, of module '' | * code, of module '' | ||
| - | import qualified | + | import qualified |
| main :: IO () | main :: IO () | ||
| Line 167: | Line 168: | ||
| printExample (-hugeInteger) | printExample (-hugeInteger) | ||
| - | printExample :: Oct.OctetsVertable | + | printExample :: Oct.Octetable |
| printExample x = | printExample x = | ||
| let | let | ||
| Line 330: | Line 331: | ||
| | | ||
| </ | </ | ||
| + | * example, used with module FileSystem | ||
| + | * code, of module '' | ||
| + | import qualified FileSystem as FS | ||
| + | import qualified System.IO as SysIo | ||
| + | import qualified Octetable as Oct | ||
| + | import qualified Data.Char as Chr | ||
| + | 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 | ||
| + | ] | ||
| + | |||
| + | octBlank :: Oct.Octet | ||
| + | octBlank = ((fromIntegral . Chr.ord) ' ') | ||
| + | |||
| + | octUnderscore :: Oct.Octet | ||
| + | octUnderscore = ((fromIntegral . Chr.ord) ' | ||
| + | |||
| + | hGetNReplace :: Oct.Octet -> Oct.Octet -> SysIo.Handle -> IO Oct.Octets | ||
| + | hGetNReplace a b h = | ||
| + | do | ||
| + | s <- Oct.getOctets h | ||
| + | return (fmap (\x -> if x == a then b else x) s) | ||
| + | |||
| + | 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) | ||
| + | </ | ||
| + | * compiles, error and warning free, with compiler: GHC 8.10.4, using compiler option -Wall | ||
| + | * input, file " | ||
| + | The quick brown fox jumps over the lazy dog. | ||
| + | </ | ||
| + | * executes, with output, first line stdout, second line stderr:< | ||
| + | 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. | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/octets.1618916777.txt.gz · Last modified: (external edit)
