improve codes (#3246)

This commit is contained in:
MkQtS
2026-02-04 15:03:04 +08:00
committed by GitHub
parent bfb35d7b68
commit 311b281000
2 changed files with 22 additions and 6 deletions

View File

@@ -33,9 +33,17 @@ type DomainList struct {
func (d *DomainRule) domain2String() string {
var dstr strings.Builder
dstr.Grow(len(d.Type) + len(d.Value) + 10)
fmt.Fprintf(&dstr, "%s:%s", d.Type, d.Value)
if len(d.Attrs) != 0 {
fmt.Fprintf(&dstr, ":@%s", strings.Join(d.Attrs, ",@"))
dstr.WriteString(d.Type)
dstr.WriteByte(':')
dstr.WriteString(d.Value)
for i, attr := range d.Attrs {
if i == 0 {
dstr.WriteByte(':')
} else {
dstr.WriteByte(',')
}
dstr.WriteByte('@')
dstr.WriteString(attr)
}
return dstr.String()
}

14
main.go
View File

@@ -156,9 +156,17 @@ func parseEntry(line string) (Entry, error) {
// Formated plain entry: type:domain.tld:@attr1,@attr2
var plain strings.Builder
plain.Grow(len(entry.Type) + len(entry.Value) + 10)
fmt.Fprintf(&plain, "%s:%s", entry.Type, entry.Value)
if len(entry.Attrs) != 0 {
fmt.Fprintf(&plain, ":@%s", strings.Join(entry.Attrs, ",@"))
plain.WriteString(entry.Type)
plain.WriteByte(':')
plain.WriteString(entry.Value)
for i, attr := range entry.Attrs {
if i == 0 {
plain.WriteByte(':')
} else {
plain.WriteByte(',')
}
plain.WriteByte('@')
plain.WriteString(attr)
}
entry.Plain = plain.String()