fix comments inside quotes in colors/icons files

This commit is contained in:
Gokcehan committed 2022-02-13 23:25:03 +03:00
1 parent 17453f10f5
commit 3e3a4a6386
2 files changed
+44 -10

No files matched your search

+21 -10
View File
@@ -2,7 +2,6 @@ package main
import (
"bufio"
"errors"
"fmt"
"io"
"path/filepath"
@@ -136,23 +135,36 @@ func splitWord(s string) (word, rest string) {
return
}
var reComment = regexp.MustCompile(`#.*$`)
var reTrailingSpace = regexp.MustCompile(`\s+$`)
// This function reads whitespace separated string pairs at each line. Single
// 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) {
var pairs [][]string
s := bufio.NewScanner(r)
for s.Scan() {
line := s.Text()
line = reComment.ReplaceAllString(line, "")
line = reTrailingSpace.ReplaceAllString(line, "")
squote, dquote := false, false
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 == "" {
continue
}
squote, dquote := false, false
squote, dquote = false, false
pair := strings.FieldsFunc(line, func(r rune) bool {
if r == '\'' && !dquote {
squote = !squote
@@ -163,12 +175,11 @@ func readPairs(r io.Reader) ([][]string, error) {
})
if len(pair) != 2 {
return nil, errors.New(fmt.Sprintf("expected pair but found: %s", s.Text()))
continue
return nil, fmt.Errorf("expected pair but found: %s", s.Text())
}
for i := 0; i < len(pair); i++ {
squote, dquote := false, false
squote, dquote = false, false
buf := make([]rune, 0, len(pair[i]))
for _, r := range pair[i] {
if r == '\'' && !dquote {