util

package
v0.0.0-...-06227b9 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: MIT Imports: 10 Imported by: 1

Documentation

Overview

Package util provides utility functions for the goldmark.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToReadOnlyString

func BytesToReadOnlyString(b []byte) string

BytesToReadOnlyString returns a string converted from given bytes.

func DedentPosition deprecated

func DedentPosition(bs []byte, currentPos, width int) (pos, padding int)

DedentPosition dedents lines by the given width.

Deprecated: This function has bugs. Use util.IndentPositionPadding and util.FirstNonSpacePosition.

func DedentPositionPadding deprecated

func DedentPositionPadding(bs []byte, currentPos, paddingv, width int) (pos, padding int)

DedentPositionPadding dedents lines by the given width. This function is mostly same as DedentPosition except this function takes account into additional paddings.

Deprecated: This function has bugs. Use util.IndentPositionPadding and util.FirstNonSpacePosition.

func DoFullUnicodeCaseFolding

func DoFullUnicodeCaseFolding(v []byte) []byte

DoFullUnicodeCaseFolding performs full unicode case folding to given bytes.

func EastAsianWidth

func EastAsianWidth(r rune) string

EastAsianWidth returns the east asian width of the given rune. See https://www.unicode.org/reports/tr11/tr11-36.html

func EscapeHTML

func EscapeHTML(v []byte) []byte

EscapeHTML escapes characters that should be escaped in HTML text.

func EscapeHTMLByte

func EscapeHTMLByte(b byte) []byte

EscapeHTMLByte returns HTML escaped bytes if the given byte should be escaped, otherwise nil.

func FindClosure deprecated

func FindClosure(bs []byte, opener, closure byte, codeSpan, allowNesting bool) int

FindClosure returns a position that closes the given opener. If codeSpan is set true, it ignores characters in code spans. If allowNesting is set true, closures correspond to nested opener will be ignored.

Deprecated: This function can not handle newlines. Many elements can be existed over multiple lines(e.g. link labels). Use text.Reader.FindClosure.

func FindEmailIndex

func FindEmailIndex(b []byte) int

FindEmailIndex returns a stop index value if the given bytes seem an email address.

func FindURLIndex

func FindURLIndex(b []byte) int

FindURLIndex returns a stop index value if the given bytes seem an URL. This function is equivalent to [A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\x00-\x20]* .

func FirstNonSpacePosition

func FirstNonSpacePosition(bs []byte) int

FirstNonSpacePosition returns a position line that is a first nonspace character.

func IndentPosition

func IndentPosition(bs []byte, currentPos, width int) (pos, padding int)

IndentPosition searches an indent position with the given width for the given line. If the line contains tab characters, paddings may be not zero. currentPos==0 and width==2:

position: 0    1
          [TAB]aaaa
width:    1234 5678

width=2 is in the tab character. In this case, IndentPosition returns (pos=1, padding=2).

func IndentPositionPadding

func IndentPositionPadding(bs []byte, currentPos, paddingv, width int) (pos, padding int)

IndentPositionPadding searches an indent position with the given width for the given line. This function is mostly same as IndentPosition except this function takes account into additional paddings.

func IndentWidth

func IndentWidth(bs []byte, currentPos int) (width, pos int)

IndentWidth calculate an indent width for the given line.

func IsAlphaNumeric

func IsAlphaNumeric(c byte) bool

IsAlphaNumeric returns true if the given character is a alphabet or a numeric, otherwise false.

func IsBlank

func IsBlank(bs []byte) bool

IsBlank returns true if the given string is all space characters.

func IsEastAsianWideRune

func IsEastAsianWideRune(r rune) bool

IsEastAsianWideRune returns trhe if the given rune is an east asian wide character, otherwise false.

func IsEscapedPunctuation

func IsEscapedPunctuation(source []byte, i int) bool

IsEscapedPunctuation returns true if character at a given index i is an escaped punctuation, otherwise false.

func IsHexDecimal

func IsHexDecimal(c byte) bool

IsHexDecimal returns true if the given character is a hexdecimal, otherwise false.

func IsNumeric

func IsNumeric(c byte) bool

IsNumeric returns true if the given character is a numeric, otherwise false.

func IsPunct

func IsPunct(c byte) bool

IsPunct returns true if the given character is a punctuation, otherwise false.

func IsPunctRune

func IsPunctRune(r rune) bool

IsPunctRune returns true if the given rune is a punctuation, otherwise false.

func IsSpace

func IsSpace(c byte) bool

IsSpace returns true if the given character is a space, otherwise false.

func IsSpaceDiscardingUnicodeRune

func IsSpaceDiscardingUnicodeRune(r rune) bool

IsSpaceDiscardingUnicodeRune returns true if the given rune is space-discarding unicode character, otherwise false. See https://www.w3.org/TR/2020/WD-css-text-3-20200429/#space-discard-set

func IsSpaceRune

func IsSpaceRune(r rune) bool

IsSpaceRune returns true if the given rune is a space, otherwise false.

func ReadWhile

func ReadWhile(source []byte, index [2]int, pred func(byte) bool) (int, bool)

ReadWhile read the given source while pred is true.

func ReplaceSpaces

func ReplaceSpaces(source []byte, repl byte) []byte

ReplaceSpaces replaces sequence of spaces with the given repl.

func ResolveEntityNames

func ResolveEntityNames(source []byte) []byte

ResolveEntityNames resolve entity references like '&ouml;" .

func ResolveNumericReferences

func ResolveNumericReferences(source []byte) []byte

ResolveNumericReferences resolve numeric references like '&#1234;" .

func StringToReadOnlyBytes

func StringToReadOnlyBytes(s string) (bs []byte)

StringToReadOnlyBytes returns bytes converted from given string.

func TabWidth

func TabWidth(currentPos int) int

TabWidth calculates actual width of a tab at the given position.

func ToLinkReference

func ToLinkReference(v []byte) string

ToLinkReference converts given bytes into a valid link reference string. ToLinkReference performs unicode case folding, trims leading and trailing spaces, converts into lower case and replace spaces with a single space character.

func ToRune

func ToRune(source []byte, pos int) rune

ToRune decode given bytes start at pos and returns a rune.

func ToValidRune

func ToValidRune(v rune) rune

ToValidRune returns 0xFFFD if the given rune is invalid, otherwise v.

func TrimLeft

func TrimLeft(source, b []byte) []byte

TrimLeft trims characters in the given s from head of the source. bytes.TrimLeft offers same functionalities, but bytes.TrimLeft allocates new buffer for the result.

func TrimLeftLength

func TrimLeftLength(source, s []byte) int

TrimLeftLength returns a length of leading specified characters.

func TrimLeftSpace

func TrimLeftSpace(source []byte) []byte

TrimLeftSpace returns a subslice of the given string by slicing off all leading space characters.

func TrimLeftSpaceLength

func TrimLeftSpaceLength(source []byte) int

TrimLeftSpaceLength returns a length of leading space characters.

func TrimRight

func TrimRight(source, b []byte) []byte

TrimRight trims characters in the given s from tail of the source.

func TrimRightLength

func TrimRightLength(source, s []byte) int

TrimRightLength returns a length of trailing specified characters.

func TrimRightSpace

func TrimRightSpace(source []byte) []byte

TrimRightSpace returns a subslice of the given string by slicing off all trailing space characters.

func TrimRightSpaceLength

func TrimRightSpaceLength(source []byte) int

TrimRightSpaceLength returns a length of trailing space characters.

func URLEscape

func URLEscape(v []byte, resolveReference bool) []byte

URLEscape escape the given URL. If resolveReference is set true:

  1. unescape punctuations
  2. resolve numeric references
  3. resolve entity references

URL encoded values (%xx) are kept as is.

func UTF8Len

func UTF8Len(b byte) int8

UTF8Len returns a byte length of the utf-8 character.

func UnescapePunctuations

func UnescapePunctuations(source []byte) []byte

UnescapePunctuations unescapes blackslash escaped punctuations.

func VisualizeSpaces

func VisualizeSpaces(bs []byte) []byte

VisualizeSpaces visualize invisible space characters.

Types

type BufWriter

type BufWriter interface {
	io.Writer
	Available() int
	Buffered() int
	Flush() error
	WriteByte(c byte) error
	WriteRune(r rune) (size int, err error)
	WriteString(s string) (int, error)
}

A BufWriter is a subset of the bufio.Writer .

type BytesFilter

type BytesFilter interface {
	// Add adds given bytes to this set.
	Add([]byte)

	// Contains return true if this set contains given bytes, otherwise false.
	Contains([]byte) bool

	// Extend copies this filter and adds given bytes to new filter.
	Extend(...[]byte) BytesFilter
}

BytesFilter is a efficient data structure for checking whether bytes exist or not. BytesFilter is thread-safe.

func NewBytesFilter

func NewBytesFilter(elements ...[]byte) BytesFilter

NewBytesFilter returns a new BytesFilter.

type CopyOnWriteBuffer

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

A CopyOnWriteBuffer is a byte buffer that copies buffer when it need to be changed.

func NewCopyOnWriteBuffer

func NewCopyOnWriteBuffer(buffer []byte) CopyOnWriteBuffer

NewCopyOnWriteBuffer returns a new CopyOnWriteBuffer.

func (*CopyOnWriteBuffer) Append

func (b *CopyOnWriteBuffer) Append(value []byte)

Append appends given bytes to the buffer. Append copy buffer at the first time.

func (*CopyOnWriteBuffer) AppendByte

func (b *CopyOnWriteBuffer) AppendByte(c byte)

AppendByte appends given bytes to the buffer. AppendByte copy buffer at the first time.

func (*CopyOnWriteBuffer) AppendString

func (b *CopyOnWriteBuffer) AppendString(value string)

AppendString appends given string to the buffer. AppendString copy buffer at the first time.

func (*CopyOnWriteBuffer) Bytes

func (b *CopyOnWriteBuffer) Bytes() []byte

Bytes returns bytes of this buffer.

func (*CopyOnWriteBuffer) IsCopied

func (b *CopyOnWriteBuffer) IsCopied() bool

IsCopied returns true if buffer has been copied, otherwise false.

func (*CopyOnWriteBuffer) Write

func (b *CopyOnWriteBuffer) Write(value []byte)

Write writes given bytes to the buffer. Write allocate new buffer and clears it at the first time.

func (*CopyOnWriteBuffer) WriteByte

func (b *CopyOnWriteBuffer) WriteByte(c byte) error

WriteByte writes the given byte to the buffer. WriteByte allocate new buffer and clears it at the first time.

func (*CopyOnWriteBuffer) WriteString

func (b *CopyOnWriteBuffer) WriteString(value string)

WriteString writes given string to the buffer. WriteString allocate new buffer and clears it at the first time.

type HTML5Entity

type HTML5Entity struct {
	Name       string
	CodePoints []int
	Characters []byte
}

An HTML5Entity struct represents HTML5 entitites.

func LookUpHTML5EntityByName

func LookUpHTML5EntityByName(name string) (*HTML5Entity, bool)

LookUpHTML5EntityByName returns (an HTML5Entity, true) if an entity named given name is found, otherwise (nil, false).

type PrioritizedSlice

type PrioritizedSlice []PrioritizedValue

PrioritizedSlice is a slice of the PrioritizedValues.

func (PrioritizedSlice) Remove

func (s PrioritizedSlice) Remove(v interface{}) PrioritizedSlice

Remove removes the given value from this slice.

func (PrioritizedSlice) Sort

func (s PrioritizedSlice) Sort()

Sort sorts the PrioritizedSlice in ascending order.

type PrioritizedValue

type PrioritizedValue struct {
	// Value is an arbitrary value that you want to prioritize.
	Value interface{}
	// Priority is a priority of the value.
	Priority int
}

A PrioritizedValue struct holds pair of an arbitrary value and a priority.

func Prioritized

func Prioritized(v interface{}, priority int) PrioritizedValue

Prioritized returns a new PrioritizedValue.

Jump to

Keyboard shortcuts

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