fix comments inside quotes in colors/icons files
This commit is contained in:
parent
17453f10f5
commit
3e3a4a6386
31
misc.go
31
misc.go
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -136,23 +135,36 @@ func splitWord(s string) (word, rest string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var reComment = regexp.MustCompile(`#.*$`)
|
// This function reads whitespace separated string pairs at each line. Single
|
||||||
var reTrailingSpace = regexp.MustCompile(`\s+$`)
|
// or double quotes can be used to escape whitespaces. Hash characters can be
|
||||||
|
// used to add a comment until the end of line. Indentation and trailing space
|
||||||
|
// is trimmed. Empty lines are skipped.
|
||||||
func readPairs(r io.Reader) ([][]string, error) {
|
func readPairs(r io.Reader) ([][]string, error) {
|
||||||
var pairs [][]string
|
var pairs [][]string
|
||||||
s := bufio.NewScanner(r)
|
s := bufio.NewScanner(r)
|
||||||
for s.Scan() {
|
for s.Scan() {
|
||||||
line := s.Text()
|
line := s.Text()
|
||||||
|
|
||||||
line = reComment.ReplaceAllString(line, "")
|
squote, dquote := false, false
|
||||||
line = reTrailingSpace.ReplaceAllString(line, "")
|
for i := 0; i < len(line); i++ {
|
||||||
|
if line[i] == '\'' && !dquote {
|
||||||
|
squote = !squote
|
||||||
|
} else if line[i] == '"' && !squote {
|
||||||
|
dquote = !dquote
|
||||||
|
}
|
||||||
|
if !squote && !dquote && line[i] == '#' {
|
||||||
|
line = line[:i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
|
||||||
if line == "" {
|
if line == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
squote, dquote := false, false
|
squote, dquote = false, false
|
||||||
pair := strings.FieldsFunc(line, func(r rune) bool {
|
pair := strings.FieldsFunc(line, func(r rune) bool {
|
||||||
if r == '\'' && !dquote {
|
if r == '\'' && !dquote {
|
||||||
squote = !squote
|
squote = !squote
|
||||||
@ -163,12 +175,11 @@ func readPairs(r io.Reader) ([][]string, error) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if len(pair) != 2 {
|
if len(pair) != 2 {
|
||||||
return nil, errors.New(fmt.Sprintf("expected pair but found: %s", s.Text()))
|
return nil, fmt.Errorf("expected pair but found: %s", s.Text())
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(pair); i++ {
|
for i := 0; i < len(pair); i++ {
|
||||||
squote, dquote := false, false
|
squote, dquote = false, false
|
||||||
buf := make([]rune, 0, len(pair[i]))
|
buf := make([]rune, 0, len(pair[i]))
|
||||||
for _, r := range pair[i] {
|
for _, r := range pair[i] {
|
||||||
if r == '\'' && !dquote {
|
if r == '\'' && !dquote {
|
||||||
|
23
misc_test.go
23
misc_test.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -165,6 +166,28 @@ func TestSplitWord(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReadPairs(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
s string
|
||||||
|
exp [][]string
|
||||||
|
}{
|
||||||
|
{"foo bar", [][]string{[]string{"foo", "bar"}}},
|
||||||
|
{"foo bar ", [][]string{[]string{"foo", "bar"}}},
|
||||||
|
{" foo bar", [][]string{[]string{"foo", "bar"}}},
|
||||||
|
{" foo bar ", [][]string{[]string{"foo", "bar"}}},
|
||||||
|
{"foo bar#baz", [][]string{[]string{"foo", "bar"}}},
|
||||||
|
{"foo bar #baz", [][]string{[]string{"foo", "bar"}}},
|
||||||
|
{`'foo#baz' bar`, [][]string{[]string{"foo#baz", "bar"}}},
|
||||||
|
{`"foo#baz" bar`, [][]string{[]string{"foo#baz", "bar"}}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
if got, _ := readPairs(strings.NewReader(test.s)); !reflect.DeepEqual(got, test.exp) {
|
||||||
|
t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestHumanize(t *testing.T) {
|
func TestHumanize(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
i int64
|
i int64
|
||||||
|
Loading…
Reference in New Issue
Block a user