text

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2025 License: Unlicense Imports: 6 Imported by: 0

Documentation

Overview

Package text is a collection of helpers for working with text inside nostr events such as implementing the string escaping scheme defined in NIP-01.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendBackticks

func AppendBackticks(dst, src []byte, ac AppendBytesClosure) []byte

AppendBackticks appends a provided AppendBytesClosure's output from a given source of bytes, wrapped in backticks “.

func AppendBinaryFromHex

func AppendBinaryFromHex(dst, src []byte, unquote bool) (
	b []byte,
	err error,
)

AppendBinaryFromHex encodes binary input as hex and appends it to the output.

func AppendBrace

func AppendBrace(dst, src []byte, ac AppendBytesClosure) []byte

AppendBrace appends a provided AppendBytesClosure's output from a given source of bytes, wrapped in braces ().

func AppendBracket

func AppendBracket(dst, src []byte, ac AppendBytesClosure) []byte

AppendBracket appends a provided AppendBytesClosure's output from a given source of bytes, wrapped in brackets [].

func AppendHexFromBinary

func AppendHexFromBinary(dst, src []byte, quote bool) (b []byte)

AppendHexFromBinary appends to a hex output from binary input.

func AppendList

func AppendList(
	dst []byte, src [][]byte, separator byte,
	ac AppendBytesClosure,
) []byte

AppendList appends an input source bytes processed by an AppendBytesClosure and separates elements with the given separator byte.

func AppendParenthesis

func AppendParenthesis(dst, src []byte, ac AppendBytesClosure) []byte

AppendParenthesis appends a provided AppendBytesClosure's output from a given source of bytes, wrapped in parentheses {}.

func AppendQuote

func AppendQuote(dst, src []byte, ac AppendBytesClosure) []byte

AppendQuote appends a source of bytes, that have been processed by an AppendBytesClosure and returns the appended-to slice.

func AppendSingleQuote

func AppendSingleQuote(dst, src []byte, ac AppendBytesClosure) []byte

AppendSingleQuote appends a provided AppendBytesClosure's output from a given source of bytes, wrapped in single quotes ”.

func Comma

func Comma(b []byte) (rem []byte, err error)

func False

func False() []byte

func JSONKey

func JSONKey(dst, k []byte) (b []byte)

JSONKey generates the JSON format for an object key and terminates with the semicolon.

func MarshalBool

func MarshalBool(src []byte, truth bool) []byte

func MarshalHexArray

func MarshalHexArray(dst []byte, ha [][]byte) (b []byte)

func Noop

func Noop(dst, src []byte) []byte

Noop simply appends the source to the destination slice and returns it.

func NostrEscape

func NostrEscape(dst, src []byte) []byte

NostrEscape for JSON encoding according to RFC8259.

This is the efficient implementation based on the NIP-01 specification:

To prevent implementation differences from creating a different event Id for the same event, the following rules MUST be followed while serializing:

No whitespace, line breaks or other unnecessary formatting should be included
in the output JSON. No characters except the following should be escaped, and
instead should be included verbatim:

- A line break, 0x0A, as \n
- A double quote, 0x22, as \"
- A backslash, 0x5C, as \\
- A carriage return, 0x0D, as \r
- A tab character, 0x09, as \t
- A backspace, 0x08, as \b
- A form feed, 0x0C, as \f

UTF-8 should be used for encoding.

func NostrUnescape

func NostrUnescape(dst []byte) (b []byte)

NostrUnescape reverses the operation of NostrEscape except instead of appending it to the provided slice, it rewrites it, eliminating a memory copy. Keep in mind that the original JSON will be mangled by this operation, but the resultant slices will cost zero allocations.

func Quote

func Quote(dst, src []byte) []byte

Quote simply quotes a provided source and attaches it to the provided destination slice.

func True

func True() []byte

func UnmarshalBool

func UnmarshalBool(src []byte) (rem []byte, truth bool, err error)

func UnmarshalHex

func UnmarshalHex(b []byte) (h []byte, rem []byte, err error)

UnmarshalHex takes a byte string that should contain a quoted hexadecimal encoded value, decodes it using a SIMD hex codec and returns the decoded bytes in a newly allocated buffer.

func UnmarshalHexArray

func UnmarshalHexArray(b []byte, size int) (t [][]byte, rem []byte, err error)

UnmarshalHexArray unpacks a JSON array containing strings with hexadecimal, and checks all values have the specified byte size.

func UnmarshalQuoted

func UnmarshalQuoted(b []byte) (content, rem []byte, err error)

UnmarshalQuoted performs an in-place unquoting of NIP-01 quoted byte string.

func UnmarshalStringArray

func UnmarshalStringArray(b []byte) (t [][]byte, rem []byte, err error)

UnmarshalStringArray unpacks a JSON array containing strings.

func Unquote

func Unquote(b []byte) []byte

Unquote removes the quotes around a slice of bytes.

Types

type AppendBytesClosure

type AppendBytesClosure func(dst, src []byte) []byte

AppendBytesClosure is a function type for appending data from a source to a destination and returning the appended-to slice.

type AppendClosure

type AppendClosure func(dst []byte) []byte

AppendClosure is a simple append where the caller appends to the destination and returns the appended-to slice.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL