From 01e7b4f4503f6909dbefd12d691fa8ac2445298c Mon Sep 17 00:00:00 2001 From: kayomn Date: Sat, 24 Dec 2022 21:55:07 +0000 Subject: [PATCH] Replace the section component from entries in favor of parser state --- ini.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/ini.go b/ini.go index 5c0c02a..c191b7b 100644 --- a/ini.go +++ b/ini.go @@ -14,9 +14,8 @@ type Builder struct { // Singular key-value pair under the given section within an INI file. type Entry struct { - Section string - Key string - Value string + Key string + Value string } // Returns the last error that occured during parsing. @@ -87,9 +86,8 @@ func (parser *Parser) Parse() Entry { if assignmentIndex := strings.Index(line, "="); assignmentIndex > -1 { // Key with value. return Entry{ - Section: parser.section, - Key: unquote(strings.TrimSpace(line[0:assignmentIndex])), - Value: unquote(strings.TrimSpace(line[assignmentIndex+1:])), + Key: unquote(strings.TrimSpace(line[0:assignmentIndex])), + Value: unquote(strings.TrimSpace(line[assignmentIndex+1:])), } } @@ -97,9 +95,8 @@ func (parser *Parser) Parse() Entry { var keyValue = unquote(line[1:lineTail]) return Entry{ - Section: parser.section, - Key: keyValue, - Value: keyValue, + Key: keyValue, + Value: keyValue, } } @@ -117,6 +114,12 @@ type Parser struct { isEnd bool } +// Returns the name of the section last parsed, which will an empty string if currently in the +// default section. +func (parser *Parser) Section() string { + return parser.section +} + // Appends a new section titled `name` in the `builder`. func (builder *Builder) Section(name string) error { var _, printError = fmt.Fprintf(builder.writer, "[%s]\n", name)