Documentation
¶
Overview ¶
Package text provides functionalities to manipulate texts.
Index ¶
- Constants
- type BlockReader
- type FindClosureOptions
- type Reader
- type Segment
- func (t *Segment) Between(other Segment) Segment
- func (t *Segment) ConcatPadding(v []byte) []byte
- func (t *Segment) IsEmpty() bool
- func (t *Segment) Len() int
- func (t *Segment) TrimLeftSpace(buffer []byte) Segment
- func (t *Segment) TrimLeftSpaceWidth(width int, buffer []byte) Segment
- func (t *Segment) TrimRightSpace(buffer []byte) Segment
- func (t *Segment) Value(buffer []byte) []byte
- func (t *Segment) WithStart(v int) Segment
- func (t *Segment) WithStop(v int) Segment
- type Segments
- func (s *Segments) Append(t Segment)
- func (s *Segments) AppendAll(t []Segment)
- func (s *Segments) At(i int) Segment
- func (s *Segments) Clear()
- func (s *Segments) Len() int
- func (s *Segments) Set(i int, v Segment)
- func (s *Segments) SetSliced(lo, hi int)
- func (s *Segments) Sliced(lo, hi int) []Segment
- func (s *Segments) Unshift(v Segment)
- func (s *Segments) Value(buffer []byte) []byte
Constants ¶
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.
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 NewSegmentPadding ¶
NewSegmentPadding returns a new Segment with the given padding.
func (*Segment) ConcatPadding ¶
ConcatPadding concats the padding to the given slice.
func (*Segment) TrimLeftSpace ¶
TrimLeftSpace returns a new segment by slicing off all leading space characters including padding.
func (*Segment) TrimLeftSpaceWidth ¶
TrimLeftSpaceWidth returns a new segment by slicing off leading space characters until the given width.
func (*Segment) TrimRightSpace ¶
TrimRightSpace returns a new segment by slicing off all trailing space characters.
type Segments ¶
type Segments struct {
// contains filtered or unexported fields
}
Segments is a collection of the Segment.
func (*Segments) AppendAll ¶
AppendAll appends all elements of given segments after the tail of the collection.