trw

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

README

trw: Text Re-Writer.

GoDoc Go Report Card

Package trw wraps around various text processing functions from the standard Go library to allow for functional composition of operations, also minimising memory consumption. Here is an example of usage.

About the package

The package is most useful in situations where a number of text rewriting operations is to be applied sequentially to a large input byte slice. For this scenario the package provides:

  • Functional composition of the existing or user-defined operations that can later be applied all at once;
  • Memory optimisation using various techniques to minimise (re)allocations.

Documentation

Overview

Package trw wraps around various text processing functions from the standard Go library to allow for functional composition of operations, also minimising memory allocations.

Example
package main

import "fmt"

func main() {
	src := []byte("*SomeSome*  example    _text_")
	res := rewriter.Do(src)

	fmt.Println(string(res))
}

var rewriter = Seq(
	Delete(LitN("Some", 1)),
	Replace(Patt(`[[:space:]]+`), " "),
	Expand(`_([^_]+)_`, `<i>${1}</i>`),
	Expand(`\*([^\*]+)\*`, `<b>${1}</b>`),
)
Output:

<b>Some</b> example <i>text</i>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

type Matcher = func([]byte) [][]int

Matcher is a type of a function that, given a byte slice, returns a slice holding the index pairs identifying all successive matches, or nil if there is no match.

func Lit

func Lit(patt string) Matcher

Lit creates a Matcher for the given string literal.

func LitN added in v0.6.0

func LitN(patt string, n int) Matcher

LitN creates a Matcher for the given string literal that matches up to n times.

func Patt

func Patt(patt string) Matcher

Patt creates a Matcher for the given regular expression pattern.

func PattN added in v0.6.0

func PattN(patt string, n int) Matcher

PattN creates a Matcher for the given regular expression pattern, matching up to n times.

func Re

func Re(re *regexp.Regexp) Matcher

Re creates a matcher for the given regular expression object.

func ReN added in v0.6.0

func ReN(re *regexp.Regexp, n int) Matcher

ReN creates a matcher for the given regular expression object, matching up to n times.

type Rewriter

type Rewriter func([]byte, []byte) ([]byte, []byte)

Rewriter is an opaque type representing a text rewriting operation.

func Delete

func Delete(match Matcher) Rewriter

Delete creates a Rewriter that removes all the matches produced by the given Matcher.

func Expand

func Expand(patt, subst string) Rewriter

Expand creates a Rewriter that applies Regexp.Expand() operation to every match of the given regular expression pattern.

func ExpandN added in v0.6.0

func ExpandN(patt, subst string, n int) Rewriter

ExpandN creates a Rewriter that applies Regexp.Expand() operation to the first n matches of the given regular expression pattern.

func ExpandRe

func ExpandRe(re *regexp.Regexp, subst string) Rewriter

ExpandRe creates a Rewriter that applies Regexp.Expand() operation to all matches of the given regular expression object.

func ExpandReN added in v0.6.0

func ExpandReN(re *regexp.Regexp, subst string, n int) Rewriter

ExpandReN creates a Rewriter that applies Regexp.Expand() operation to the first n matches of the given regular expression object.

func Replace

func Replace(match Matcher, subst string) Rewriter

Replace creates a rewriter that substitutes all the matches produced by the given Matcher with the specified string.

func Seq

func Seq(rewriters ...Rewriter) Rewriter

Seq is a sequential composition of Rewriters.

func (Rewriter) Do

func (rw Rewriter) Do(src []byte) (result []byte)

Do applies the Rewriter to the specified byte slice. The returned result may be either the source slice modified in-place, or a new slice.

Jump to

Keyboard shortcuts

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