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.
StaticString{N}
is a wrapper of aNTuple{N,UInt8}
of exactlyN
codeunits, padded with\0
, nul, bytes.SubStaticString{N, R <: AbstractUnitRange}
is a wrapper of aNTuple{N,UInt8}
of up toN
codeunits, with a unit range indicating a subset of codeunits.CStaticString{N}
is similar to aStaticString
but requires all the NUL bytes to be terminal codeunits. The struct also contains an extra terminal NUL.PaddedStaticString{N,PAD}
is siimlar toStaticString
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.StaticStrings
StaticStrings.AbstractStaticString
StaticStrings.CStaticString
StaticStrings.PaddedStaticString
StaticStrings.StaticString
StaticStrings.SubStaticString
StaticStrings.data
StaticStrings.pad
StaticStrings.@cstatic_str
StaticStrings.@padded_str
StaticStrings.@static_str
StaticStrings.@substatic_str
StaticStrings.StaticStrings
— ModuleStaticStrings.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
StaticStrings.AbstractStaticString
— TypeAbstractStaticString{N}
Represents a string of N
codeunits
StaticStrings.StaticString
— TypeStaticString(data::NTuple{N,UInt8})
static"string"N
AbstractStaticString
that stores codeunits in a NTuple{N,UInt8}.
StaticStrings.@static_str
— Macrostatic"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
StaticStrings.SubStaticString
— TypeSubStaticString(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.
StaticStrings.@substatic_str
— Macrosubstatic"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
StaticStrings.CStaticString
— TypeCStaticString(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.
StaticStrings.@cstatic_str
— Macrocstatic"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
StaticStrings.PaddedStaticString
— TypePaddedStaticString{N,PAD}(data::NTuple{N,UInt8})
padded"string[PAD]"N
AbstractStaticString
that is padded with PAD
.
StaticStrings.@padded_str
— Macropadded"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
StaticStrings.data
— FunctionStaticStrings.data(string::AbstractStaticString{N})::NTuple{N,UInt8} where N
Retrieve the internal Tuple
containing the N
stored UInt8
code units.
StaticStrings.pad
— FunctionStaticStrings.pad(string::PaddedStaticString)
Retrieve the UInt8 code unit used for padding.
StaticStrings.pad(s::AbstractString, N::Integer, PAD::UInt8=0x0)::PaddedStaticString
Pad an AbstractString
to N
code units with the code unit PAD
.