prefix

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

type Matcher struct {
	Pattern string // pattern to match against input (default: leading whitespace)
	// contains filtered or unexported fields
}

Matcher can be used to remove or add a prefix from / to each row of a table.

This is useful for re-formatting tables which are embedded in e.g. code comments.

Example
package main

import (
	"fmt"
	"strings"

	"codeberg.org/japh/psv/encoding/prefix"
)

func main() {
	testCases := []struct {
		pattern string
		line    string
		prefix  string
		content string
		name    string
	}{
		{"", "", "", "", "empty line"},
		{"", "  abc", "  ", "abc", "indented line"},
		{"", "// abc", "", "// abc", "unexpected comment"},
		{"//", "// abc", "// ", "abc", "expected comment"},
		{"//", "  //  //    abc", "  //  //    ", "abc", "multiple comments"},
		{"# //", " #    //  abc", " #    //  ", "abc", "multiple comments with variable whitespace"},
		{"# //", " //    #  abc", " ", "//    #  abc", "unexpected comments with variable whitespace"},
	}

	for _, tc := range testCases {
		// set up a prefix pattern (equivalent to `psv -i {pattern}`)
		splitter := &prefix.Matcher{}
		splitter.SetPrefixPattern(tc.pattern)

		// split a line into its prefix and content parts
		p, c := splitter.SplitLine(tc.line)

		fmt.Println("---")
		fmt.Printf("Test:    %s\n", tc.name)
		fmt.Printf("pattern: %q\n", tc.pattern)
		fmt.Printf("input:   %q\n", tc.line)
		fmt.Printf("prefix:  [%s%s]\n", p, strings.Repeat("_", len(c)))
		fmt.Printf("content: [%s%s]\n", strings.Repeat("_", len(p)), c)
	}

}
Output:

---
Test:    empty line
pattern: ""
input:   ""
prefix:  []
content: []
---
Test:    indented line
pattern: ""
input:   "  abc"
prefix:  [  ___]
content: [__abc]
---
Test:    unexpected comment
pattern: ""
input:   "// abc"
prefix:  [______]
content: [// abc]
---
Test:    expected comment
pattern: "//"
input:   "// abc"
prefix:  [// ___]
content: [___abc]
---
Test:    multiple comments
pattern: "//"
input:   "  //  //    abc"
prefix:  [  //  //    ___]
content: [____________abc]
---
Test:    multiple comments with variable whitespace
pattern: "# //"
input:   " #    //  abc"
prefix:  [ #    //  ___]
content: [__________abc]
---
Test:    unexpected comments with variable whitespace
pattern: "# //"
input:   " //    #  abc"
prefix:  [ ____________]
content: [_//    #  abc]

func (*Matcher) ClearPrefixPattern

func (pm *Matcher) ClearPrefixPattern()

func (*Matcher) SetPrefixPattern

func (pm *Matcher) SetPrefixPattern(pattern string)

func (*Matcher) SplitLine

func (pm *Matcher) SplitLine(line string) (prefix, content string)

SplitLine attempts to match a line with a specific indent & prefix.

If the indent / prefix does not match, then the prefix returned will be "" and the line will be returned unchanged.

func (*Matcher) String

func (pm *Matcher) String() string

Jump to

Keyboard shortcuts

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