75 lines
1.3 KiB
Go
75 lines
1.3 KiB
Go
// Copyright 2023 Michael Amann and contributors
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
package utils
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
type StringBuilder struct {
|
|
sa []string
|
|
title string
|
|
}
|
|
|
|
func NewStringBuilder(title string) *StringBuilder {
|
|
sb := StringBuilder{
|
|
title: title,
|
|
sa: []string{},
|
|
}
|
|
return &sb
|
|
}
|
|
|
|
func NewQuickStringBuilder(title string, nvd [][3]string, v []string) *StringBuilder {
|
|
sb := StringBuilder{
|
|
title: title,
|
|
sa: []string{},
|
|
}
|
|
|
|
if len(nvd) > 0 {
|
|
sb.AddBulkIfNotDefaultOrEmpty(nvd)
|
|
}
|
|
|
|
if len(v) > 0 {
|
|
sb.AddBulk(v)
|
|
}
|
|
|
|
return &sb
|
|
}
|
|
|
|
func QuickStringBuilderDirect(title string, nvd [][3]string, v []string) string {
|
|
sb := NewQuickStringBuilder(title, nvd, v)
|
|
|
|
return sb.GetResult()
|
|
}
|
|
|
|
func (sb *StringBuilder) Add(s string) {
|
|
sb.sa = append(sb.sa, s)
|
|
}
|
|
|
|
func (sb *StringBuilder) AddIfNotDefaultOrEmpty(name, v, d string) {
|
|
if IsDefault(v, d) || strings.TrimSpace(v) == "" {
|
|
return
|
|
}
|
|
sb.Add(name + ": " + v)
|
|
}
|
|
|
|
func (sb *StringBuilder) GetResult() string {
|
|
if len(sb.sa) > 0 {
|
|
return sb.title + ": " + strings.Join(sb.sa, " ")
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (sb *StringBuilder) AddBulkIfNotDefaultOrEmpty(a [][3]string) {
|
|
for _, v := range a {
|
|
sb.AddIfNotDefaultOrEmpty(v[0], v[1], v[2])
|
|
}
|
|
}
|
|
|
|
func (sb *StringBuilder) AddBulk(a []string) {
|
|
for _, v := range a {
|
|
sb.Add(v)
|
|
}
|
|
}
|