literal

package
v0.10.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2024 License: Apache-2.0 Imports: 8 Imported by: 15

Documentation

Overview

Package literal implements conversions to and from string representations of basic data types.

Index

Constants

View Source
const (
	K = mulDec | mul1
	M = mulDec | mul2
	G = mulDec | mul3
	T = mulDec | mul4
	P = mulDec | mul5
	E = mulDec | mul6
	Z = mulDec | mul7
	Y = mulDec | mul8

	Ki = mulBin | mul1
	Mi = mulBin | mul2
	Gi = mulBin | mul3
	Ti = mulBin | mul4
	Pi = mulBin | mul5
	Ei = mulBin | mul6
	Zi = mulBin | mul7
	Yi = mulBin | mul8
)

Variables

This section is empty.

Functions

func IndentTabs added in v0.3.0

func IndentTabs(s string, n int) string

IndentTabs takes a quoted string and reindents it for the given indentation. If a string is not a multiline string it will return the string as is.

func ParseNum added in v0.1.0

func ParseNum(s string, n *NumInfo) error

ParseNum parses s and populates NumInfo with the result.

func Unquote

func Unquote(s string) (string, error)

Unquote interprets s as a single- or double-quoted, single- or multi-line string, possibly with custom escape delimiters, returning the string value that s quotes.

Types

type Form added in v0.3.0

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

Form defines how to quote a string or bytes literal.

var (
	// String defines the format of a CUE string. Conversions may be lossy.
	String Form = stringForm

	// Label is like String, but optimized for labels.
	Label Form = stringForm

	// Bytes defines the format of bytes literal.
	Bytes Form = bytesForm
)

func (Form) Append added in v0.3.0

func (f Form) Append(buf []byte, s string) []byte

Append appends a CUE string literal representing s, as generated by Quote, to buf and returns the extended buffer.

func (Form) AppendEscaped added in v0.3.0

func (f Form) AppendEscaped(buf []byte, s string) []byte

AppendEscaped appends a CUE string literal representing s, as generated by Quote but without the quotes, to buf and returns the extended buffer.

It does not include the last indentation.

func (Form) Quote added in v0.3.0

func (f Form) Quote(s string) string

Quote returns CUE string literal representing s. The returned string uses CUE escape sequences (\t, \n, \u00FF, \u0100) for control characters and non-printable characters as defined by strconv.IsPrint.

It reports an error if the string cannot be converted to the desired form.

func (Form) WithASCIIOnly added in v0.3.0

func (f Form) WithASCIIOnly() Form

WithASCIIOnly ensures the quoted strings consists solely of valid ASCII characters.

func (Form) WithGraphicOnly added in v0.3.0

func (f Form) WithGraphicOnly() Form

WithGraphicOnly ensures the quoted strings consists solely of printable characters.

func (Form) WithOptionalTabIndent added in v0.3.0

func (f Form) WithOptionalTabIndent(tabs int) Form

WithOptionalIndent is like WithTabIndent, but only returns a multiline strings if it doesn't contain any newline characters.

func (Form) WithTabIndent added in v0.3.0

func (f Form) WithTabIndent(n int) Form

WithTabIndent returns a new Form with indentation set to the given number of tabs. The result will be a multiline string.

type Multiplier added in v0.1.0

type Multiplier byte

A Multiplier indicates a multiplier indicator used in the literal.

type NumInfo added in v0.1.0

type NumInfo struct {
	UseSep bool
	// contains filtered or unexported fields
}

NumInfo contains information about a parsed numbers.

Reusing a NumInfo across parses may avoid memory allocations.

func (*NumInfo) Decimal added in v0.1.0

func (p *NumInfo) Decimal(v *decimal) error

Decimal is for internal use.

func (*NumInfo) IsInt added in v0.1.0

func (p *NumInfo) IsInt() bool

IsInt reports whether the number is an integral number.

func (*NumInfo) Multiplier added in v0.1.0

func (p *NumInfo) Multiplier() Multiplier

Multiplier reports which multiplier was used in an integral number.

func (*NumInfo) String added in v0.1.0

func (p *NumInfo) String() string

String returns a canonical string representation of the number so that it can be parsed with math.Float.Parse.

type QuoteInfo

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

QuoteInfo describes the type of quotes used for a string.

func ParseQuotes

func ParseQuotes(start, end string) (q QuoteInfo, nStart, nEnd int, err error)

ParseQuotes checks if the opening quotes in start matches the ending quotes in end and reports its type as q or an error if they do not matching or are invalid. nStart indicates the number of bytes used for the opening quote.

func (QuoteInfo) IsDouble

func (q QuoteInfo) IsDouble() bool

IsDouble reports whether the literal uses double quotes.

func (QuoteInfo) IsMulti added in v0.3.0

func (q QuoteInfo) IsMulti() bool

IsMulti reports whether a multi-line string was parsed.

func (QuoteInfo) Unquote

func (q QuoteInfo) Unquote(s string) (string, error)

Unquote unquotes the given string, which should not contain the initial quote character(s). It must be terminated with a quote or an interpolation start. Escape sequences are expanded and surrogates are replaced with the corresponding non-surrogate code points.

func (QuoteInfo) Whitespace added in v0.3.0

func (q QuoteInfo) Whitespace() string

Whitespace returns prefix whitespace for multiline strings.

Jump to

Keyboard shortcuts

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