mirror of
https://github.com/xiaoqidun/goini.git
synced 2026-07-12 20:58:26 +08:00
feat(清理代码): 清理非必要的代码
This commit is contained in:
@@ -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
@@ -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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user