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/17 07:21] – 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 | + | {-# LANGUAGE |
| module Octetable | module Octetable | ||
| ( | ( | ||
| Octetable(..), | Octetable(..), | ||
| - | Octets | + | |
| + | | ||
| + | getOctets, | ||
| + | putOctets | ||
| ) where | ) where | ||
| Line 19: | Line 20: | ||
| import qualified Data.Bits as Bts | import qualified Data.Bits as Bts | ||
| 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 Octets = [W.Word8] | + | type Octet = W.Word8 |
| + | |||
| + | type Octets = [Octet] | ||
| + | |||
| + | getOctets :: SysIo.Handle -> IO [W.Word8] | ||
| + | getOctets = (fmap BS.unpack) . BS.hGetContents | ||
| + | |||
| + | putOctets :: SysIo.Handle -> [W.Word8] -> IO () | ||
| + | putOctets hOut octs = BS.hPut hOut (BS.pack octs) | ||
| class (Eq a, Show a) => Octetable a where | class (Eq a, Show a) => Octetable a where | ||
| toOctets :: a -> Octets | toOctets :: a -> Octets | ||
| fromOctets :: Octets -> a | fromOctets :: Octets -> a | ||
| - | + | ||
| instance Octetable W.Word32 where | instance Octetable W.Word32 where | ||
| toOctets w32 = | toOctets w32 = | ||
| Line 40: | Line 52: | ||
| fromOctets lw = Chr.chr (integralFromOctets lw) | fromOctets lw = Chr.chr (integralFromOctets lw) | ||
| - | instance Octetable [Char] where | + | {-| UTF-8 character |
| - | toOctets | + | |
| - | fromOctets [] = [] | + | * prefix: cu8 |
| - | fromOctets | + | * 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 | ||
| + | toOctets (OneByte w81) = [w81] | ||
| + | toOctets | ||
| + | toOctets | ||
| + | | ||
| + | fromOctets | ||
| + | | (w81 .&. 0b10000000) == 0b00000000 = OneByte w81 | ||
| + | | (w81 .&. 0b11100000) == 0b11000000 = TwoBytes (w81,w82) | ||
| + | | (w81 .&. 0b11110000) == 0b11100000 = ThreeBytes (w81, | ||
| + | | otherwise | ||
| + | fromOctets (w81: | ||
| + | | (w81 .&. 0b10000000) | ||
| + | | (w81 .&. 0b11100000) == 0b11000000 = TwoBytes (w81,w82) | ||
| + | | otherwise | ||
| + | fromOctets (w81:w82:[]) | ||
| + | | (w81 .&. 0b10000000) == 0b00000000 = OneByte w81 | ||
| + | | otherwise | ||
| + | fromOctets | ||
| + | fromOctets [] = (OneByte 0) | ||
| instance Octetable Integer where | instance Octetable Integer where | ||
| Line 69: | Line 109: | ||
| fIntegralToOctets' | fIntegralToOctets' | ||
| - | keepOctetsPositive :: Octets-> Octets | + | keepOctetsPositive :: Octets -> Octets |
| keepOctetsPositive [] = [] | keepOctetsPositive [] = [] | ||
| keepOctetsPositive lw8 | keepOctetsPositive lw8 | ||
| Line 75: | Line 115: | ||
| | otherwise | | otherwise | ||
| - | keepOctetsNegative :: Octets-> Octets | + | keepOctetsNegative :: Octets -> Octets |
| keepOctetsNegative [] = [] | keepOctetsNegative [] = [] | ||
| keepOctetsNegative lw8 | keepOctetsNegative lw8 | ||
| Line 86: | Line 126: | ||
| | otherwise | | otherwise | ||
| </ | </ | ||
| - | * code, of module '' | + | * code, of module '' |
| import qualified Octetable as Oct | import qualified Octetable as Oct | ||
| Line 291: | 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.1618636897.txt.gz · Last modified: (external edit)
