File: AsciiString.hs

package info (click to toggle)
haskell-basement 0.0.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: haskell: 11,336; ansic: 63; makefile: 5
file content (62 lines) | stat: -rw-r--r-- 2,216 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
-- |
-- Module      : Foundation.Primitives.Types.AsciiString
-- License     : BSD-style
-- Maintainer  : Haskell Foundation
-- Stability   : experimental
-- Portability : portable
--
-- A AsciiString type backed by a `ASCII` encoded byte array and all the necessary
-- functions to manipulate the string.
--
{-# LANGUAGE BangPatterns               #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MagicHash                  #-}
{-# LANGUAGE TypeFamilies               #-}
{-# LANGUAGE FlexibleContexts           #-}
module Basement.Types.AsciiString
    ( AsciiString(..)
    , MutableAsciiString(..)
    -- * Binary conversion
    , fromBytesUnsafe
    , fromBytes
    ) where

import           Basement.Compat.Base
import           Basement.Compat.Semigroup
import           Basement.Types.Char7
import           Basement.UArray.Base
import qualified Basement.Types.Char7 as Char7
import qualified Basement.UArray as A (all, unsafeRecast)

-- | Opaque packed array of characters in the ASCII encoding
newtype AsciiString = AsciiString { toBytes :: UArray Char7 }
    deriving (Typeable, Semigroup, Monoid, Eq, Ord)

newtype MutableAsciiString st = MutableAsciiString (MUArray Char7 st)
    deriving (Typeable)

instance Show AsciiString where
    show = fmap Char7.toChar . toList
instance IsString AsciiString where
    fromString = fromList . fmap Char7.fromCharMask
instance IsList AsciiString where
    type Item AsciiString = Char7
    fromList = AsciiString . fromList
    toList (AsciiString chars) = toList chars

-- | Convert a Byte Array representing ASCII data directly to an AsciiString without checking for ASCII validity
--
-- If the input contains invalid Char7 value (anything above 0x7f),
-- it will trigger runtime async errors when processing data.
--
-- In doubt, use 'fromBytes'
fromBytesUnsafe :: UArray Word8 -> AsciiString
fromBytesUnsafe = AsciiString . A.unsafeRecast

-- | Convert a Byte Array representing ASCII checking validity.
--
-- If the byte array is not valid, then Nothing is returned
fromBytes :: UArray Word8 -> Maybe AsciiString
fromBytes arr
    | A.all (\x -> x < 0x80) arr = Just $ AsciiString $ A.unsafeRecast arr
    | otherwise                  = Nothing