text

package
v2.0.0-beta.4 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package text provides functionalities to manipulate texts.

Index

Constants

View Source
const EOF = byte(0xff)

EOF indicates the end of file.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockReader

type BlockReader interface {
	Reader
	// Reset resets current state and sets new segments to the reader.
	Reset(segs []Segment)
}

A BlockReader interface is a reader that is optimized for Blocks.

func NewBlockReader

func NewBlockReader(source []byte, segs []Segment) BlockReader

NewBlockReader returns a new BlockReader.

type Index

type Index struct {
	Start int
	Stop  int
}

An Index represents a position range in a source byte slice. Unlike Segment, Index does not carry parsing metadata such as Padding or ForceNewline.

func NewIndex

func NewIndex(start, stop int) Index

NewIndex returns a new Index.

func NewIndexFromSegment

func NewIndexFromSegment(seg Segment) Index

NewIndexFromSegment returns a new Index derived from the given Segment, dropping any padding and ignoring ForceNewline.

func (Index) IsEmpty

func (i Index) IsEmpty() bool

IsEmpty returns true if this index is empty, otherwise false.

type Lines

type Lines struct {
	// contains filtered or unexported fields
}

A Lines value holds the raw content of a block node. It is either an owned string or a sequence of Segment values copied from the parser's source-segment list.

Lines is used for block nodes whose rendered content is taken directly from the source (CodeBlock, FencedCodeBlock, HTMLBlock) so that the content is self-contained and can be serialised without access to the original source.

func NewLines

func NewLines[T LinesInput](v T) Lines

NewLines returns a Lines from the given input, which may be a string, byte slice, or slice of Segment.

func NewSegmentsLines

func NewSegmentsLines(segs []Segment) Lines

NewSegmentsLines returns a Lines backed by source segments.

func NewStringLines

func NewStringLines(s string) Lines

NewStringLines returns a Lines backed by an owned string.

func (*Lines) AppendSegment

func (l *Lines) AppendSegment(seg Segment)

AppendSegment appends a Segment to the internal segment list.

func (Lines) Bytes

func (l Lines) Bytes(source []byte) []byte

Bytes returns the concatenated byte content of all segments, or the owned string. The returned byte slice is read-only and should not be modified.

func (Lines) IsOwned

func (l Lines) IsOwned() bool

IsOwned returns true if this value is an owned string not derived from the source byte slice.

func (Lines) Segments

func (l Lines) Segments() []Segment

Segments returns the internal segment slice, allowing callers to read and otherwise inspect the segment list.

func (Lines) Str

func (l Lines) Str(source []byte) string

Str returns the string representation of this value. The returned string is read-only and should not be modified. See Bytes() for details on how the string is constructed from source segments.

type LinesInput

type LinesInput interface {
	string | []byte | []Segment | Lines
}

LinesInput is a type constraint for types that can be converted to a Lines.

type MultilineValue

type MultilineValue struct {
	// contains filtered or unexported fields
}

A MultilineValue holds a potentially multiline inline value that is either an owned string or a set of source position ranges. Use MultilineValue for data that may span multiple lines per the CommonMark spec (e.g. link labels).

When constructing the byte value from source positions, the ranges are concatenated and any trailing newline of each range is replaced with a single space, matching CommonMark's line-folding rules.

func NewIndexMultilineValue

func NewIndexMultilineValue(i Index) MultilineValue

NewIndexMultilineValue returns a MultilineValue backed by a single source position.

func NewIndicesMultilineValue

func NewIndicesMultilineValue(indices []Index) MultilineValue

NewIndicesMultilineValue returns a MultilineValue backed by source positions.

func NewMultilineValue

func NewMultilineValue[T MultilineValueInput](v T) MultilineValue

NewMultilineValue returns a MultilineValue from the given input, which may be a string, byte slice, Index, or slice of Index.

func NewMultilineValueFromSegments

func NewMultilineValueFromSegments(segs []Segment) MultilineValue

NewMultilineValueFromSegments returns a MultilineValue backed by source positions derived from the given segments. Segment padding is dropped since MultilineValue is used for inline content where padding does not apply.

func NewStringMultilineValue

func NewStringMultilineValue(s string) MultilineValue

NewStringMultilineValue returns a MultilineValue backed by an owned string.

func (MultilineValue) Bytes

func (v MultilineValue) Bytes(source []byte) []byte

Bytes returns the byte representation of this value. When backed by source positions:

  • a single index: returns the source sub-slice as-is
  • two or more indices: the first range is returned as-is, and each subsequent range has leading whitespace trimmed before concatenation

If the value is owned, source is ignored. The returned byte slice is read-only and should not be modified.

func (MultilineValue) IsOwned

func (v MultilineValue) IsOwned() bool

IsOwned returns true if this value is an owned string not derived from the source byte slice.

func (MultilineValue) Str

func (v MultilineValue) Str(source []byte) string

Str returns the string representation of this value. The returned string is read-only and should not be modified. See Bytes() for details on how the string is constructed from source positions.

type MultilineValueBuilder

type MultilineValueBuilder struct {
	// contains filtered or unexported fields
}

MultilineValueBuilder is a helper for building a MultilineValue. It optimises for the common case of a single Index by storing it directly in the struct and only allocating a slice if multiple indices are added.

func (*MultilineValueBuilder) AddIndex

func (b *MultilineValueBuilder) AddIndex(i Index)

AddIndex adds an Index to the builder.

func (*MultilineValueBuilder) AddSegment

func (b *MultilineValueBuilder) AddSegment(seg Segment)

AddSegment adds an Index derived from the given Segment to the builder.

func (*MultilineValueBuilder) Build

Build returns a MultilineValue from the accumulated state. If SetString was called, the result is backed by that string. Otherwise, the result is backed by the accumulated indices.

func (*MultilineValueBuilder) Collection

func (b *MultilineValueBuilder) Collection() []Index

Collection returns the slice of Index values in the builder. The result is meaningful only when IsCollection() returns true.

func (*MultilineValueBuilder) IsCollection

func (b *MultilineValueBuilder) IsCollection() bool

IsCollection returns true if the builder contains multiple Indices.

func (*MultilineValueBuilder) IsOwned

func (b *MultilineValueBuilder) IsOwned() bool

IsOwned returns true if the builder contains an owned string value.

func (*MultilineValueBuilder) IsSingle

func (b *MultilineValueBuilder) IsSingle() bool

IsSingle returns true if the builder contains at most one Index.

func (*MultilineValueBuilder) SetString

func (b *MultilineValueBuilder) SetString(s string)

SetString sets an owned string value. When Build is called, the resulting MultilineValue will be backed by this string rather than any accumulated indices.

func (*MultilineValueBuilder) Single

func (b *MultilineValueBuilder) Single() Index

Single returns the single Index in the builder. The result is meaningful only when IsSingle() returns true.

type MultilineValueInput

type MultilineValueInput interface {
	string | []byte | Index | []Index | MultilineValue
}

MultilineValueInput is a type constraint for types that can be converted to a MultilineValue.

type Reader

type Reader interface {
	io.RuneReader

	// Source returns a source of the reader.
	Source() []byte

	// ResetPosition resets positions.
	ResetPosition()

	// Peek returns a byte at current position without advancing the internal pointer.
	Peek() byte

	// PeekLine returns the current line without advancing the internal pointer.
	PeekLine() ([]byte, Segment)

	// PrecedingCharacter returns a character just before current internal pointer.
	PrecedingCharacter() rune

	// ValueBetween returns a MultilineValue covering the given [start, stop)
	// byte range within the source.
	ValueBetween(start, stop int) MultilineValue

	// LineOffset returns a distance from the line head to current position.
	LineOffset() int

	// Position returns current line number and position.
	Position() (int, Segment)

	// SetPosition sets current line number and position.
	SetPosition(int, Segment)

	// SetPadding sets padding to the reader.
	SetPadding(int)

	// Advance advances the internal pointer.
	Advance(int)

	// AdvanceAndSetPadding advances the internal pointer and add padding to the
	// reader.
	AdvanceAndSetPadding(int, int)

	// AdvanceToEOL advances the internal pointer to the end of line.
	// If the line ends with a newline, it will be included in the segment.
	// If the line ends with EOF, it will not be included in the segment.
	AdvanceToEOL()

	// AdvanceLine advances the internal pointer to the next line head.
	AdvanceLine()

	// SkipSpaces skips space characters and returns a non-blank line.
	// If it reaches EOF, returns false.
	SkipSpaces() (Segment, int, bool)

	// SkipSpaces skips blank lines and returns a non-blank line.
	// If it reaches EOF, returns false.
	SkipBlankLines() (Segment, int, bool)

	// Match performs regular expression matching to current line.
	Match(reg *regexp.Regexp) bool

	// Match performs regular expression searching to current line.
	FindSubMatch(reg *regexp.Regexp) [][]byte
}

A Reader interface provides abstracted method for reading text.

func NewReader

func NewReader(source []byte) Reader

NewReader return a new Reader that can read UTF-8 bytes .

type Segment

type Segment struct {
	// Start is a start position of the segment.
	Start int

	// Stop is a stop position of the segment.
	// This value should be excluded.
	Stop int

	// Padding is a padding length of the segment.
	Padding int

	// ForceNewline is true if the segment should be ended with a newline.
	// Some elements(i.e. CodeBlock, FencedCodeBlock) does not trim trailing
	// newlines. Spec defines that EOF is treated as a newline, so we need to
	// add a newline to the end of the segment if it is not empty.
	//
	// i.e.:
	//
	//     “`go
	//     const test = "test"
	//
	// This code does not close the code block and ends with EOF. In this case,
	// we need to add a newline to the end of the last line like `const test = "test"\n`.
	ForceNewline bool
}

A Segment struct holds information about source positions.

func NewSegment

func NewSegment(start, stop int) Segment

NewSegment return a new Segment.

func NewSegmentPadding

func NewSegmentPadding(start, stop, n int) Segment

NewSegmentPadding returns a new Segment with the given padding.

func (Segment) Between

func (t Segment) Between(other Segment) Segment

Between returns a segment between this segment and the given segment.

func (Segment) Bytes

func (t Segment) Bytes(buffer []byte) []byte

Bytes returns bytes of the segment.

func (Segment) IsEmpty

func (t Segment) IsEmpty() bool

IsEmpty returns true if this segment is empty, otherwise false.

func (Segment) Len

func (t Segment) Len() int

Len returns a length of the segment.

func (Segment) Str

func (t Segment) Str(buffer []byte) string

Str returns a string of the segment.

func (Segment) TrimLeftSpace

func (t Segment) TrimLeftSpace(buffer []byte) Segment

TrimLeftSpace returns a new segment by slicing off all leading space characters including padding.

func (Segment) TrimLeftSpaceWidth

func (t Segment) TrimLeftSpaceWidth(width int, buffer []byte) Segment

TrimLeftSpaceWidth returns a new segment by slicing off leading space characters until the given width.

func (Segment) TrimRightSpace

func (t Segment) TrimRightSpace(buffer []byte) Segment

TrimRightSpace returns a new segment by slicing off all trailing space characters.

func (Segment) WithStart

func (t Segment) WithStart(v int) Segment

WithStart returns a new Segment with same value except Start.

func (Segment) WithStop

func (t Segment) WithStop(v int) Segment

WithStop returns a new Segment with same value except Stop.

type Value

type Value struct {
	// contains filtered or unexported fields
}

A Value holds a single-line inline value that is either an owned string or a position range within a source byte slice. Use Value for data that must not contain newlines per the CommonMark spec (e.g. link destinations, fenced code block info strings).

func NewIndexValue

func NewIndexValue(i Index) Value

NewIndexValue returns a Value backed by a source position.

func NewStringValue

func NewStringValue(s string) Value

NewStringValue returns a Value backed by an owned string.

func NewValue

func NewValue[T ValueInput](v T) Value

NewValue returns a Value from the given input, which may be a string, byte slice, or Index.

func (Value) Bytes

func (v Value) Bytes(source []byte) []byte

Bytes returns the byte representation of this value. If the value is owned, source is ignored. The returned byte slice is read-only and should not be modified.

func (Value) Index

func (v Value) Index() Index

Index returns the source position of this value. The result is meaningful only when IsOwned() returns false.

func (Value) IsOwned

func (v Value) IsOwned() bool

IsOwned returns true if this value is an owned string not derived from the source byte slice.

func (Value) Str

func (v Value) Str(source []byte) string

Str returns the string representation of this value. The returned string is read-only and should not be modified.

type ValueInput

type ValueInput interface {
	string | []byte | Index | Value
}

ValueInput is a type constraint for types that can be converted to a Value.

Jump to

Keyboard shortcuts

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