StaticStrings

Documentation for StaticStrings.

Fixed-length strings wrapping a NTuple for Julia.

Introduction

StaticStrings.jl implements several AbstractString subtypes that wrap a NTuple{N,UInt8}. An AbstractStaticString is a AbstractString with N codeunits.

The concrete subtypes of AbstractStaticString are as follows.

  1. StaticString{N} is a wrapper of a NTuple{N,UInt8} of exactly N codeunits, padded with \0, nul, bytes.
  2. SubStaticString{N, R <: AbstractUnitRange} is a wrapper of a NTuple{N,UInt8} of up to N codeunits, with a unit range indicating a subset of codeunits.
  3. CStaticString{N} is similar to a StaticString but requires all the NUL bytes to be terminal codeunits. The struct also contains an extra terminal NUL.
  4. PaddedStaticString{N,PAD} is siimlar to StaticString but is padded with an arbitrary byte codeunit.

Usage

julia> using StaticStrings

julia> static"Hello world!"
static"Hello world!"12

julia> static"Hello world!" |> typeof
StaticString{12}

julia> cs = cstatic"Hello world!\n"
cstatic"Hello world!\n"13

julia> ccall(:printf, Cint, (Ptr{Cchar},), cs)
Hello world!
13

julia> ccall(:printf, Cint, (Ptr{CStaticString{13}},), Ref(cs))
Hello world!
13

julia> padded"Hello "20
padded"Hello "20

julia> ps = padded"Hello "20
padded"Hello "20

julia> StaticString(ps)
static"Hello               "20

julia> push!(StaticString["Hello"], "Bye")
2-element Vector{StaticString}:
 static"Hello"5
 static"Bye"5
StaticStrings.StaticStringsModule
StaticStrings.jl

The StaticStrings.jl package implements an AbstractString based on NTuple{N,UInt8}.

julia> using StaticStrings

julia> static"Thank you for using StaticStrings.jl"
static"Thank you for using StaticStrings.jl"36

julia> static"Thank you for using StaticStrings.jl"64
static"Thank you for using StaticStrings.jl\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"64

julia> StaticString((0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64))
static"Hello world"11

See StaticString, SubStaticString, CStaticString, PaddedStaticString

source
StaticStrings.@static_strMacro
static"string"[N]

Create a StaticString from "string". Optionally, specify the number of codeunits, N.

Examples

julia> static"großartig"
static"großartig"10

julia> static"verehrungswürdig"20
static"verehrungswürdig\0\0\0"20
source
StaticStrings.SubStaticStringType
SubStaticString(data::NTuple{N, UInt8}, ind::Integer)
SubStaticString(data::NTuple{N, UInt8}, ind::AbstractUnitRange)
substatic"string"N

AbstractStaticString that stores up to N codeunits in a NTuple{N,UInt8}. The actual codeunits used are a subset indicated by an AbstractUnitRange.

source
StaticStrings.@substatic_strMacro
substatic"string"[N]

Create a SubStaticString from "string". Optionally, specify the number of codeunits, N. The result will be a SubStaticString{N} but the codeunits used will be those of the original string.

Examples

julia> substatic"Як ти?"
substatic"Як ти?"10

julia> substatic"Як ти?"256
substatic"Як ти?"256
source
StaticStrings.CStaticStringType
CStaticString(data::NTuple{N,UInt8})
cstatic"string"N

AbstractStaticString that stores codeunits in a NTuple{N,UInt8} but requires NUL codeunits to be at the end.

Converting to a String will include null bytes. Use strip to get a SubString without the null bytes.

N.B. The size of a CStaticString{N} is N+1 bytes.

source
StaticStrings.@cstatic_strMacro
cstatic"string"[N]

Create a CStaticString from "string". Optionally, specify the number of codeunits, N.

Examples

julia> cstatic"Orada mısın"
cstatic"Orada mısın"13

julia> cstatic"Orada mısın"25
cstatic"Orada mısın"25

julia> ccall(:jl_printf, Cint, (Ptr{Nothing}, Ptr{Cchar},), stdout isa IOContext ? stdout.io : stdout, cstatic"Orada mısın\n"25)
Orada mısın
14
source
StaticStrings.@padded_strMacro
padded"string[PAD]"N

Create a PaddedStaticString of N codeunits from "string". The last codeunit in the provided string becomes the PAD.

Examples

julia> padded"私は元気です。 ありがとうございました。 "64
padded"私は元気です。 ありがとうございました。 "64

julia> padded"私は元気です。 ありがとうございました。 "64 |> StaticString
static"私は元気です。 ありがとうございました。      "64
source
StaticStrings.dataFunction
StaticStrings.data(string::AbstractStaticString{N})::NTuple{N,UInt8} where N

Retrieve the internal Tuple containing the N stored UInt8 code units.

source
StaticStrings.padFunction
StaticStrings.pad(string::PaddedStaticString)

Retrieve the UInt8 code unit used for padding.

source
StaticStrings.pad(s::AbstractString, N::Integer, PAD::UInt8=0x0)::PaddedStaticString

Pad an AbstractString to N code units with the code unit PAD.

source