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 ¶
Click to show internal directories.
Click to hide internal directories.