Netstring
public struct Netstring
Netstring
reads and writes netstrings.
A netstring is a self-delimiting encoding of a string of bytes that declares
its length at its beginning.
The predeclared length makes it easy to limit the the size of accepted data and to find the end of message without imposing escaping on the sender.
-
Result of parsing.
See moreDeclaration
Swift
public enum ParseResult
-
A
Reader
should return as many bytes as specified in the parameter, or less if end of stream has been reached.Declaration
Swift
public typealias Reader = (Int) -> Bytes
-
The undecorated (no length, no delimiters) content of the netstring.
Declaration
Swift
public let payload: Bytes
-
Initialize Netstring with the given bytes. This initializer doesn’t parse the content.
Declaration
Swift
public init(payload: Bytes)
-
Parse a netstring from
reader
.This function will read as little data from
reader
as possible. The read position after this function can be determined based on the return value.If the return value is
success
, the reader will be left at the position after the final comma.If the return value is
rejected
, the position depends on the value ofskipTooLong
:true
: the reader will be left at the position after the final comma.false
: the reader will be left at the position after the colon.
If the return value is
failure
, the position is undefined.Warning
a nil value for
maxLength
means unbounded memory usage if you don’t control the data source forreader
.Declaration
Swift
public static func parse(reader: @escaping ((Int) -> Bytes), maxLength: Int? = 10240, skipTooLong: Bool = true) -> ParseResult
Parameters
reader
Source of data.
maxLength
Maximum length of netstring to accept. If nil, Netstring will attempt to read all the data.
skipTooLong
Read in chunks and discard the input netstring if it’s too long.
Return Value
ParseResult
-
Parse a netstring from
array
.This is a convenience function that creates an
ArrayReader
forarray
and callsparse(reader:maxLength:skipTooLong:)
.See also
ArrayReader
See also
parse(reader:maxLength:skipTooLong:)
Declaration
Swift
public static func parse(array: [UInt8]) -> ParseResult
-
Create a netstring based on
payload
.Declaration
Swift
public func export() -> Bytes
-
Declaration
Swift
public static func ==(lhs: Netstring, rhs: Netstring) -> Bool
Parameters
lhs
rhs