runematcher

package
v0.0.0-...-7272a43 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package runematcher provides functions that check whether a rune matches a certain condition. Also offers basic text processing methods based on this function.

Example (AnyOfCollapseFrom)
package main

import (
	"fmt"

	"github.com/abc-inc/goava/base/runematcher"
)

func main() {
	str := runematcher.AnyOf("eko").CollapseFrom("bookkeeper", '-')
	fmt.Println(str)
}
Output:

b-p-r
Example (AnyOfTrimFrom)
package main

import (
	"fmt"

	"github.com/abc-inc/goava/base/runematcher"
)

func main() {
	str := runematcher.AnyOf("ab").TrimFrom("abacatbab")
	fmt.Println(str)
}
Output:

cat
Example (AnyOfTrimLeadingFrom)
package main

import (
	"fmt"

	"github.com/abc-inc/goava/base/runematcher"
)

func main() {
	str := runematcher.AnyOf("ab").TrimLeadingFrom("abacatbab")
	fmt.Println(str)
}
Output:

catbab
Example (AnyOfTrimTrailingFrom)
package main

import (
	"fmt"

	"github.com/abc-inc/goava/base/runematcher"
)

func main() {
	str := runematcher.AnyOf("ab").TrimTrailingFrom("abacatbab")
	fmt.Println(str)
}
Output:

abacat
Example (AsciiMatchesAllOf)
package main

import (
	"fmt"

	"github.com/abc-inc/goava/base/runematcher"
)

func main() {
	valid := runematcher.ASCII().MatchesAllOf("abc")
	fmt.Println(valid)
}
Output:

true
Example (IsRemoveFrom)
package main

import (
	"fmt"

	"github.com/abc-inc/goava/base/runematcher"
)

func main() {
	str := runematcher.Is('a').RemoveFrom("bazaar")
	fmt.Println(str)
}
Output:

bzr
Example (IsReplaceFrom)
package main

import (
	"fmt"

	"github.com/abc-inc/goava/base/runematcher"
)

func main() {
	str := runematcher.Is('a').ReplaceFrom("yaha", "oo")
	fmt.Println(str)
}
Output:

yoohoo
Example (IsReplaceFromRune)
package main

import (
	"fmt"

	"github.com/abc-inc/goava/base/runematcher"
)

func main() {
	str := runematcher.Is('a').ReplaceFromRune("radar", 'o')
	fmt.Println(str)
}
Output:

rodor
Example (IsRetainFrom)
package main

import (
	"fmt"

	"github.com/abc-inc/goava/base/runematcher"
)

func main() {
	str := runematcher.Is('a').RetainFrom("bazaar")
	fmt.Println(str)
}
Output:

aaa
Example (WhitespaceTrimFrom)
package main

import (
	"fmt"

	"github.com/abc-inc/goava/base/runematcher"
)

func main() {
	trimmed := runematcher.Whitespace().TrimFrom("    charming    ")
	fmt.Println(trimmed)
}
Output:

charming

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

type Matcher interface {
	fmt.Stringer

	// Matches determines a true or false value for the given character.
	Matches(r rune) bool

	// Negate returns a matcher that matches any character not matched by this matcher.
	Negate() Matcher

	// And returns a matcher that matches any character matched by both this matcher and other.
	And(other Matcher) Matcher

	// Or returns a matcher that matches any character matched by either this matcher or other.
	Or(other Matcher) Matcher

	// MatchesAnyOf returns true if a character sequence contains at least one matching character.
	//
	// Equivalent to !MatchesNoneOf(sequence)
	MatchesAnyOf(str string) bool

	// MatchesAllOf returns true if a character sequence contains only matching characters.
	MatchesAllOf(str string) bool

	// MatchesNoneOf returns true if a character sequence contains no matching characters.
	//
	// Equivalent to !MatchesAnyOf(sequence).
	MatchesNoneOf(str string) bool

	// IndexIn returns the index of the first matching character in a character sequence,
	// starting from a given position, or -1 if no character matches after that position.
	IndexIn(str string, start int) int

	// IndexInRunes returns the index of the first matching character in a character sequence,
	// starting from a given position, or -1 if no character matches after that position.
	IndexInRunes(runes []rune, start int) int

	// LastIndexIn returns the index of the last matching character in a character sequence,
	// or -1 if no matching character is present.
	LastIndexIn(str string) int

	// CountIn returns the number of matching characters found in a character sequence.
	CountIn(str string) int

	// RemoveFrom returns a string containing all non-matching characters of a character sequence, in order.
	RemoveFrom(str string) string

	// RetainFrom returns a string containing all matching characters of a character sequence, in order.
	RetainFrom(str string) string

	// ReplaceFromRune returns a string copy of the input character sequence, with each matching character
	// replaced by a given replacement character.
	ReplaceFromRune(str string, replacement rune) string

	// ReplaceFrom returns a string copy of the input character sequence, with each matching character
	// replaced by a given replacement sequence.
	ReplaceFrom(str string, replacement string) string

	// TrimFrom returns a substring of the input character sequence that omits all matching characters
	// from the beginning and from the end of the string.
	TrimFrom(str string) string

	// TrimLeadingFrom returns a substring of the input character sequence that omits all matching characters
	// from the beginning of the string.
	TrimLeadingFrom(str string) string

	// TrimTrailingFrom returns a substring of the input character sequence that omits all matching characters
	// from the end of the string.
	TrimTrailingFrom(str string) string

	// CollapseFrom returns a string copy of the input character sequence, with each group of consecutive matching
	// characters replaced by a single replacement character.
	CollapseFrom(str string, replacement rune) string

	// TrimAndCollapseFrom collapses groups of matching characters exactly as CollapseFrom(str, replacement) does,
	// except that groups of matching characters at the start or end of the sequence are removed without replacement.
	TrimAndCollapseFrom(str string, replacement rune) string
}

Matcher determines a true or false value for any rune. Also offers basic text processing methods based on this function. Implementations are strongly encouraged to be side-effect-free and immutable.

Throughout the documentation of this type, the phrase "matching character" is used to mean "any rune value r for which Matches(r) returns true".

func ASCII

func ASCII() Matcher

ASCII determines whether a character is ASCII, meaning that its code point is less than 128.

func Any

func Any() Matcher

Any matches any character.

func AnyOf

func AnyOf(match string) Matcher

AnyOf returns a char matcher that matches any character present in the given character sequence.

func BreakingWhitespace

func BreakingWhitespace() Matcher

BreakingWhitespace determines whether a character is a breaking whitespace (that is, a whitespace which can be interpreted as a break between words for formatting purposes).

func Digit

func Digit() Matcher

Digit determines whether a character is a digit according to Unicode. If you only care to match ASCII digits, you can use InRange('0', '9').

func ForPredicate

func ForPredicate(p func(rune) bool) Matcher

ForPredicate returns a matcher with identical behavior to the given character-based predicate.

func InRange

func InRange(startIncl, endIncl rune) Matcher

InRange returns a char matcher that matches any character in a given range (both endpoints are inclusive).

For example, to match any lowercase letter of the English alphabet, use InRange('a', 'z').

func Invisible

func Invisible() Matcher

Invisible determines whether a character is invisible; that is, if its Unicode category is any of SPACE_SEPARATOR, LINE_SEPARATOR, PARAGRAPH_SEPARATOR, CONTROL, FORMAT, SURROGATE, and PRIVATE_USE.

func Is

func Is(r rune) Matcher

Is returns a char matcher that matches only one specified character.

func IsNot

func IsNot(r rune) Matcher

IsNot returns a char matcher that matches any character except the character specified.

To negate another Matcher, use Negate().

func None

func None() Matcher

None matches no characters.

func NoneOf

func NoneOf(str string) Matcher

NoneOf returns a char matcher that matches any character not present in the given character sequence.

func SingleWidth

func SingleWidth() Matcher

SingleWidth determines whether a character is single-width (not double-width).

When in doubt, this matcher errs on the side of returning false (that is, it tends to assume a character is double-width).

func Whitespace

func Whitespace() Matcher

Whitespace determines whether a character is whitespace according to the latest Unicode standard.

Jump to

Keyboard shortcuts

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