feat(清理代码): 清理非必要的代码

This commit is contained in:
2026-07-09 20:02:35 +08:00
parent 2c7697f6e5
commit f5ed0941bf
2 changed files with 13 additions and 16 deletions
+1 -4
View File
@@ -87,9 +87,6 @@ func (ini *GoINI) SaveFile(filename string) error {
// String 获取GoINI对象的字符串形式 // String 获取GoINI对象的字符串形式
// 返回: string 配置文件内容 // 返回: string 配置文件内容
func (ini *GoINI) String() string { func (ini *GoINI) String() string {
if len(ini.fileLines) == 0 {
return ""
}
renderedLines := make([]string, len(ini.fileLines)) renderedLines := make([]string, len(ini.fileLines))
for i, line := range ini.fileLines { for i, line := range ini.fileLines {
renderedLines[i] = renderLine(line) renderedLines[i] = renderLine(line)
@@ -113,7 +110,7 @@ func (ini *GoINI) SetString(name string, key string, value string) {
return return
} }
ini.ensureSectionLine(name) ini.ensureSectionLine(name)
ini.insertLine(ini.findSectionInsertIndex(name), iniLine{kind: lineKeyValue, sectionName: name, keyName: key, keyValue: value, separator: defaultSeparator, modified: true}) ini.insertLine(ini.findSectionInsertIndex(name), iniLine{kind: lineKeyValue, sectionName: name, keyName: key, keyValue: value, separator: defaultSeparator})
} }
// SetBool 设置单个配置项的布尔值 // SetBool 设置单个配置项的布尔值
+12 -12
View File
@@ -29,7 +29,7 @@ type iniLine struct {
modified bool modified bool
} }
type keyValueParts struct { type parsedKeyValue struct {
key string key string
value string value string
indent string indent string
@@ -68,12 +68,12 @@ func parseRawLine(rawLine string, sectionName string) (iniLine, string) {
line.sectionName = nextSection line.sectionName = nextSection
return line, nextSection return line, nextSection
} }
if parts, ok := parseKeyValue(rawLine); ok { if parsed, ok := parseKeyValue(rawLine); ok {
line.kind = lineKeyValue line.kind = lineKeyValue
line.keyName = parts.key line.keyName = parsed.key
line.keyValue = trimSurroundingQuotes(parts.value) line.keyValue = trimSurroundingQuotes(parsed.value)
line.indent = parts.indent line.indent = parsed.indent
line.separator = parts.separator line.separator = parsed.separator
return line, sectionName return line, sectionName
} }
return line, sectionName return line, sectionName
@@ -91,7 +91,7 @@ func parseSection(line string) (string, bool) {
return name, name != "" return name, name != ""
} }
func parseKeyValue(line string) (keyValueParts, bool) { func parseKeyValue(line string) (parsedKeyValue, bool) {
indentLen := leadingSpaceLen(line) indentLen := leadingSpaceLen(line)
indent := line[:indentLen] indent := line[:indentLen]
body := line[indentLen:] body := line[indentLen:]
@@ -101,19 +101,19 @@ func parseKeyValue(line string) (keyValueParts, bool) {
if split := strings.IndexFunc(body, unicode.IsSpace); split >= 0 { if split := strings.IndexFunc(body, unicode.IsSpace); split >= 0 {
return parseKeyValueParts(body, split, split, indent) return parseKeyValueParts(body, split, split, indent)
} }
return keyValueParts{}, false return parsedKeyValue{}, false
} }
func parseKeyValueParts(body string, keyEnd int, valueStart int, indent string) (keyValueParts, bool) { func parseKeyValueParts(body string, keyEnd int, valueStart int, indent string) (parsedKeyValue, bool) {
keyPart := body[:keyEnd] keyPart := body[:keyEnd]
valuePart := body[valueStart:] valuePart := body[valueStart:]
key := strings.TrimSpace(keyPart) key := strings.TrimSpace(keyPart)
if key == "" { if key == "" {
return keyValueParts{}, false return parsedKeyValue{}, false
} }
separatorStart := len(strings.TrimRightFunc(keyPart, unicode.IsSpace)) separatorStart := len(strings.TrimRightFunc(keyPart, unicode.IsSpace))
separatorEnd := valueStart + leadingSpaceLen(valuePart) separatorEnd := valueStart + leadingSpaceLen(valuePart)
return keyValueParts{ return parsedKeyValue{
key: key, key: key,
value: strings.TrimSpace(valuePart), value: strings.TrimSpace(valuePart),
indent: indent, indent: indent,
@@ -222,7 +222,7 @@ func (ini *GoINI) ensureSectionLine(sectionName string) {
if len(ini.fileLines) > 0 && renderLine(ini.fileLines[len(ini.fileLines)-1]) != "" { if len(ini.fileLines) > 0 && renderLine(ini.fileLines[len(ini.fileLines)-1]) != "" {
ini.fileLines = append(ini.fileLines, iniLine{kind: lineBlank}) ini.fileLines = append(ini.fileLines, iniLine{kind: lineBlank})
} }
ini.fileLines = append(ini.fileLines, iniLine{kind: lineSection, sectionName: sectionName, text: "[" + sectionName + "]"}) ini.fileLines = append(ini.fileLines, iniLine{kind: lineSection, sectionName: sectionName})
ini.rebuildIndexes() ini.rebuildIndexes()
} }