Compare commits
2 Commits
7b7ad2e791
...
ca863af9c4
Author | SHA1 | Date |
---|---|---|
kayomn | ca863af9c4 | |
kayomn | 7cd6ac1c51 |
49
ini.go
49
ini.go
|
@ -18,10 +18,17 @@ func (parser *Parser) Err() error {
|
||||||
return parser.err
|
return parser.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns `true` if `parser` has reached the end of parsable tokens, either because the stream has
|
||||||
|
// ended or it has encountered an error.
|
||||||
|
func (parser *Parser) IsEnd() bool {
|
||||||
|
return parser.isEnd
|
||||||
|
}
|
||||||
|
|
||||||
// Creates and returns a new [Parser] by reference from `reader`
|
// Creates and returns a new [Parser] by reference from `reader`
|
||||||
func NewParser(reader io.Reader) *Parser {
|
func NewParser(reader io.Reader) *Parser {
|
||||||
return &Parser{
|
return &Parser{
|
||||||
scanner: bufio.NewScanner(reader),
|
scanner: bufio.NewScanner(reader),
|
||||||
|
isEnd: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +38,7 @@ func NewParser(reader io.Reader) *Parser {
|
||||||
//
|
//
|
||||||
// Note that the `parser` does not guarantee any parse order for key-value pairs extracted from the
|
// Note that the `parser` does not guarantee any parse order for key-value pairs extracted from the
|
||||||
// `parser` stream.
|
// `parser` stream.
|
||||||
func (parser *Parser) Parse() (Entry, bool) {
|
func (parser *Parser) Parse() Entry {
|
||||||
for parser.scanner.Scan() {
|
for parser.scanner.Scan() {
|
||||||
var line = strings.TrimSpace(parser.scanner.Text())
|
var line = strings.TrimSpace(parser.scanner.Text())
|
||||||
var lineLen = len(line)
|
var lineLen = len(line)
|
||||||
|
@ -57,37 +64,27 @@ func (parser *Parser) Parse() (Entry, bool) {
|
||||||
|
|
||||||
if assignmentIndex := strings.Index(line, "="); assignmentIndex > -1 {
|
if assignmentIndex := strings.Index(line, "="); assignmentIndex > -1 {
|
||||||
// Key with value.
|
// Key with value.
|
||||||
var value = strings.TrimSpace(line[assignmentIndex+1:])
|
|
||||||
var valueLen = len(value)
|
|
||||||
|
|
||||||
if valueLen != 0 {
|
|
||||||
var valueTail = len(value) - 1
|
|
||||||
|
|
||||||
if (value[0] == '"') && (value[valueTail] == '"') {
|
|
||||||
value = value[1:valueTail]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Entry{
|
return Entry{
|
||||||
Section: parser.section,
|
Section: parser.section,
|
||||||
Key: strings.TrimSpace(line[0:assignmentIndex]),
|
Key: unquote(strings.TrimSpace(line[0:assignmentIndex])),
|
||||||
Value: value,
|
Value: unquote(strings.TrimSpace(line[assignmentIndex+1:])),
|
||||||
}, true
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Key which is its own value.
|
// Key which is its own value.
|
||||||
var keyValue = line[1:lineTail]
|
var keyValue = unquote(line[1:lineTail])
|
||||||
|
|
||||||
return Entry{
|
return Entry{
|
||||||
Section: parser.section,
|
Section: parser.section,
|
||||||
Key: keyValue,
|
Key: keyValue,
|
||||||
Value: keyValue,
|
Value: keyValue,
|
||||||
}, true
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.err = parser.scanner.Err()
|
parser.err = parser.scanner.Err()
|
||||||
|
parser.isEnd = true
|
||||||
|
|
||||||
return Entry{}, false
|
return Entry{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// State machine for parsing streamable INI file sources.
|
// State machine for parsing streamable INI file sources.
|
||||||
|
@ -95,4 +92,20 @@ type Parser struct {
|
||||||
scanner *bufio.Scanner
|
scanner *bufio.Scanner
|
||||||
err error
|
err error
|
||||||
section string
|
section string
|
||||||
|
isEnd bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns a string with the the outer-most quotes surrounding `s` trimmed, should they exist.
|
||||||
|
func unquote(s string) string {
|
||||||
|
var sLen = len(s)
|
||||||
|
|
||||||
|
if sLen != 0 {
|
||||||
|
var sTail = sLen - 1
|
||||||
|
|
||||||
|
if (s[0] == '"') && (s[sTail] == '"') {
|
||||||
|
return s[1:sTail]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
Unsatisfied with the INI parsing tools provided by third-parties in the Golang ecosystem, INI Gusher aims to solve parsing and saving of INI files as simply as possible.
|
Unsatisfied with the INI parsing tools provided by third-parties in the Golang ecosystem, INI Gusher aims to solve parsing and saving of INI files as simply as possible.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var iniParser = ini.NewParser(modInfoFile)
|
var parser = ini.NewParser(modInfoFile)
|
||||||
|
|
||||||
for entry, parsed := iniParser.Parse(); parsed; entry, parsed = iniParser.Parse() {
|
for entry, parsed := parser.Parse(); parsed; entry, parsed = parser.Parse() {
|
||||||
var myValue = myValueMap[entry.Section]
|
var myValue = myValueMap[entry.Section]
|
||||||
|
|
||||||
switch entry.Key {
|
switch entry.Key {
|
||||||
|
@ -22,7 +22,7 @@ for entry, parsed := iniParser.Parse(); parsed; entry, parsed = iniParser.Parse(
|
||||||
myValueMap[entry.Section] = myValue
|
myValueMap[entry.Section] = myValue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := iniParser.Err(); err != nil {
|
if err := parser.Err(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -39,6 +39,7 @@ As with Golang itself, INI Gusher is fairly opinionated; The API uses incrementa
|
||||||
* Unquoted keys may only use alphanumerics of any case.
|
* Unquoted keys may only use alphanumerics of any case.
|
||||||
* Unquoted values may only use alphanumerics of any case and whitespace is only acknowledged if it is between them.
|
* Unquoted values may only use alphanumerics of any case and whitespace is only acknowledged if it is between them.
|
||||||
* Keys and values may be surrounded in double-quotes (`"`) to preseve trailing whitespace and allow all characters.
|
* Keys and values may be surrounded in double-quotes (`"`) to preseve trailing whitespace and allow all characters.
|
||||||
|
* Sections allow all characters between their opening and closing braces (`"[]"`).
|
||||||
* Whitespace is ignored everywhere else.
|
* Whitespace is ignored everywhere else.
|
||||||
* The default section is an empty string (`""`).
|
* The default section is an empty string (`""`).
|
||||||
|
|
||||||
|
@ -46,4 +47,4 @@ If the above design goals suit your use-case then INI Gusher is for you.
|
||||||
|
|
||||||
## What is Up with the Name?
|
## What is Up with the Name?
|
||||||
|
|
||||||
Most Golang libraries use some sort of word starting with "G" in their name and this was my attempt to follow the styleguide laid out before me.
|
Most Golang libraries use some sort of word starting with "G" in their name and this was my attempt to follow the styleguide laid out before me.
|
||||||
|
|
Loading…
Reference in New Issue