static-text

Lists, Texts, ByteStrings and Vectors of statically known length

https://github.com/dzhus/static-text#readme

Version on this page:0.2.0.4
LTS Haskell 22.41:0.2.0.7
Stackage Nightly 2024-11-12:0.2.0.7
Latest on Hackage:0.2.0.7

See all snapshots static-text appears in

BSD-3-Clause licensed by Dmitry Dzhus
Maintained by [email protected]
This version can be pinned in stack with:static-text-0.2.0.4@sha256:3da14d6456cf58eacb2dd4882f4a7a7e30200fd009a336893e1d12199f7f792b,2749

Module documentation for 0.2.0.4

static-text: lists, Texts, ByteStrings and Vectors of statically known length

Travis CI build status Hackage Hackage deps

static-text provides type-level safety for basic operations on string-like types (finite lists of elements), such as Data.Text, String (and all lists), Data.ByteString and Data.Vector. Use it when you need static guarantee on lengths of strings produced in your code.

An example application would be a network exchange protocol built of packets with fixed-width fields:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
import Data.ByteString.Char8 (ByteString, pack)
import Data.StaticText

mkPacket :: ByteString -> Static ByteString 32
mkPacket inp =
  -- 5-character version signature
  $(st "PKT10") `append`
  -- 25-character payload
  payload `append`
  -- 2-character payload checksum
  checksum
  where
    payload = createLeft 0x20 inp
    checksum :: Static ByteString 2
    checksum = createLeft 0x20 $
               pack $ show $ Data.StaticText.length payload `mod` 100

message :: Static ByteString 64
message = mkPacket "Hello" `append` mkPacket "world"

main :: IO ()
main = print message

Please consult the Hackage page for static-text for documentation and examples.

Alternatives

The emphasis of static-text is on type-safe padding/truncation and type-safe string literals. Other similar libraries may suit different use cases:

Changes

Changelog

0.2.0.4 - 2019-01-23

Changed

  • Test suite dependencies bump

0.2.0.3 - 2018-10-08

Changed

  • GHC 8.6.x support

0.2.0.2 - 2018-07-30

Changed

  • Use doctest-driver-gen to run doctests

  • Dependencies bump

  • Use markdown-unlit to test code example in README

0.2.0.1 - 2018-03-15

Changed

  • GHC 8.4.x support

0.2.0 - 2018-02-17

Changed

  • Package renamed to static-text as per https://github.com/dzhus/static-text/issues/2. Old names were changed as follows:

    before after
    Data.Sext Data.StaticText
    Sext n a Static a n
    Sextable a IsStaticText a
    $(sext ..) $(st ..)

0.1.3.1 - 2017-10-29

Added

  • GHC 8.2.x support

0.1.3 - 2017-03-26

Added

  • ShortByteString support

Fixed

0.1.2 - 2017-01-18

Added

0.1.1 - 2016-19-12

Added

  • GHC 8.0.x support

0.1.0.2 - 2015-12-06

Added

  • GHC 7.10.x support

0.1.0.0 - 2014-08-10