Help us shape the future of Go! Take the 2026 Go Developer Survey

text

package
v1.8.7 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 5 Imported by: 1,272

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(segment *Segments)
}

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

func NewBlockReader

func NewBlockReader(source []byte, segments *Segments) BlockReader

NewBlockReader returns a new BlockReader.

type FindClosureOptions added in v1.4.1

type FindClosureOptions struct {
	// CodeSpan is a flag for the FindClosure. If this is set to true,
	// FindClosure ignores closers in codespans.
	CodeSpan bool

	// Nesting is a flag for the FindClosure. If this is set to true,
	// FindClosure allows nesting.
	Nesting bool

	// Newline is a flag for the FindClosure. If this is set to true,
	// FindClosure searches for a closer over multiple lines.
	Newline bool

	// Advance is a flag for the FindClosure. If this is set to true,
	// FindClosure advances pointers when closer is found.
	Advance bool
}

FindClosureOptions is options for Reader.FindClosure.

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)

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

	// Value returns a value of the given segment.
	Value(Segment) []byte

	// 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

	// FindClosure finds corresponding closure.
	FindClosure(opener, closer byte, options FindClosureOptions) (*Segments, bool)
}

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) ConcatPadding

func (t *Segment) ConcatPadding(v []byte) []byte

ConcatPadding concats the padding to the given slice.

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) 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) Value

func (t *Segment) Value(buffer []byte) []byte

Value returns a value of the segment.

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 Segments

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

Segments is a collection of the Segment.

func NewSegments

func NewSegments() *Segments

NewSegments return a new Segments.

func (*Segments) Append

func (s *Segments) Append(t Segment)

Append appends the given segment after the tail of the collection.

func (*Segments) AppendAll

func (s *Segments) AppendAll(t []Segment)

AppendAll appends all elements of given segments after the tail of the collection.

func (*Segments) At

func (s *Segments) At(i int) Segment

At returns a segment at the given index.

func (*Segments) Clear

func (s *Segments) Clear()

Clear delete all element of the collection.

func (*Segments) Len

func (s *Segments) Len() int

Len returns the length of the collection.

func (*Segments) Set

func (s *Segments) Set(i int, v Segment)

Set sets the given Segment.

func (*Segments) SetSliced

func (s *Segments) SetSliced(lo, hi int)

SetSliced replace the collection with a subsliced value.

func (*Segments) Sliced

func (s *Segments) Sliced(lo, hi int) []Segment

Sliced returns a subslice of the collection.

func (*Segments) Unshift

func (s *Segments) Unshift(v Segment)

Unshift insert the given Segment to head of the collection.

func (*Segments) Value added in v1.7.7

func (s *Segments) Value(buffer []byte) []byte

Value returns a string value of the collection.

Jump to

Keyboard shortcuts

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