text

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2024 License: CC0-1.0, CC0-1.0, MIT Imports: 3 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EscapeByteString added in v0.0.6

func EscapeByteString(dst []byte, s []byte) []byte

EscapeByteString 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 UnescapeByteString

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

Types

type Buffer

type Buffer struct {
	Pos int
	Buf []byte
}

func NewBuffer

func NewBuffer(b []byte) (buf *Buffer)

NewBuffer returns a new buffer containing the provided slice. This slice can/will be mutated.

func (*Buffer) Bytes

func (b *Buffer) Bytes() (bb []byte)

func (*Buffer) Copy

func (b *Buffer) Copy(length, src, dest int) (err error)

Copy a given length of bytes starting at src position to dest position, and move the cursor to the end of the written segment.

func (*Buffer) Head

func (b *Buffer) Head() []byte

Head returns the buffer from the start until the current Pos position.

func (*Buffer) Read

func (b *Buffer) Read() (bb byte, err error)

Read the next byte out of the buffer or return io.EOF if there is no more.

func (*Buffer) ReadBytes

func (b *Buffer) ReadBytes(count int) (bb []byte, err error)

ReadBytes returns the specified number of byte, and advances the cursor, or io.EOF if there isn't this much remaining after the cursor.

func (*Buffer) ReadEnclosed

func (b *Buffer) ReadEnclosed() (bb []byte, err error)

ReadEnclosed scans quickly while keeping count of open and close brackets [] or braces {} and returns the byte sub-slice starting with a bracket and ending with the same depth bracket. Selects the counted characters based on the first.

Ignores anything within quotes.

Useful for quickly finding a potentially valid array or object in JSON.

func (*Buffer) ReadThrough

func (b *Buffer) ReadThrough(c byte) (bb []byte, err error)

ReadThrough is the same as ReadUntil except it returns a slice *including* the character being sought.

func (*Buffer) ReadUntil

func (b *Buffer) ReadUntil(c byte) (bb []byte, err error)

ReadUntil returns all of the buffer from the Pos at invocation, until the index immediately before the match of the requested character.

The next Read or Write after this will return the found character or mutate it. If the first character at the index of the Pos is the one being sought, it returns a zero length slice.

Note that the implementation does not increment the Pos position until either the end of the buffer or when the requested character is found, because there is no need to write the value twice for no reason.

When this function returns an error, the state of the buffer is unchanged from prior to the invocation.

If the character is not `"` then any match within a pair of unescaped `"` is ignored. The closing `"` is not counted if it is escaped with a \.

If the character is `"` then any `"` with a `\` before it is ignored (and included in the returned slice).

func (*Buffer) Scan

func (b *Buffer) Scan(c byte, through, slice bool) (subSlice []byte,
	err error)

Scan is the utility back end that does all the scan/read functionality

func (*Buffer) ScanForOneOf

func (b *Buffer) ScanForOneOf(through bool, c ...byte) (which byte, err error)

ScanForOneOf provides the ability to scan for two or more different bytes.

For simplicity it does not skip quotes, it was actually written to find quotes or braces but just to make it clear this is very bare.

if through is set to true, the cursor is advanced to the next after the match

func (*Buffer) ScanThrough

func (b *Buffer) ScanThrough(c byte) (err error)

ScanThrough does the same as ScanUntil except it returns the next index *after* the found item.

func (*Buffer) ScanUntil

func (b *Buffer) ScanUntil(c byte) (err error)

ScanUntil does the same as ReadUntil except it doesn't slice what it passed over.

func (*Buffer) String

func (b *Buffer) String() (s string)

String returns the whole buffer as a string.

func (*Buffer) Tail

func (b *Buffer) Tail() []byte

Tail returns the buffer starting from the current Pos position.

func (*Buffer) Write

func (b *Buffer) Write(bb byte) (err error)

Write a byte into the next index of the buffer or return io.EOF if there is no space left.

func (*Buffer) WriteBytes

func (b *Buffer) WriteBytes(bb []byte) (err error)

WriteBytes copies over top of the current buffer with the bytes given.

Returns io.EOF if the write would exceed the end of the buffer, and does not perform the operation, nor move the cursor.

Jump to

Keyboard shortcuts

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