Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/migrate-v1-to-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ With options:
- `AllowPythonMultilineValues`: empty lines followed by an indented line stay in the value, as in Python's configparser. v1 ended the value at the empty line and read the indented line as a key, which usually failed.
- `SpaceBeforeInlineComment`: the first ` #` or ` ;` starts the comment. v1 looked for ` #` first.
- `RawSections`: a raw section with an empty body is raw, so `SetBody` works on it.
- `KeyValueDelimiters`: a non-ASCII delimiter, such as `:`, is skipped whole. v1 left the rest of its bytes at the start of the value.

Most API changes are renames that the compiler finds, and the tables under [API changes](#api-changes) list the replacements. The changes in the next section still compile, so read it first.

Expand Down
8 changes: 8 additions & 0 deletions ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,14 @@ key2!value2
assert.Equal(t, "value1", f.Section("section").Key("key1").String())
assert.Equal(t, "value2", f.Section("section").Key("key2").String())
})

t.Run("non-ASCII key-value delimiters", func(t *testing.T) {
f, err := Load([]byte("key1:value1\n\"key 2\"→value2\n"), KeyValueDelimiters(":→"))
require.NoError(t, err)

assert.Equal(t, "value1", f.Section("").Key("key1").String())
assert.Equal(t, "value2", f.Section("").Key("key 2").String())
})
}

func Test_PreserveSurroundingQuotes(t *testing.T) {
Expand Down
13 changes: 10 additions & 3 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,8 @@ func readKeyName(delimiters *charSet, line string) (string, int, error) {
if i < 0 {
return "", -1, fmt.Errorf("%w: %s", ErrDelimiterNotFound, line)
}
endIdx = pos + i
return strings.TrimSpace(line[startIdx:pos]), endIdx + startIdx + 1, nil
endIdx = pos + startIdx + i
return strings.TrimSpace(line[startIdx:pos]), endIdx + delimiterLen(line[endIdx:]), nil
}

endIdx = delimiters.index(line)
Expand All @@ -571,7 +571,14 @@ func readKeyName(delimiters *charSet, line string) (string, int, error) {
return "", -1, fmt.Errorf("%w: %s", ErrEmptyKeyName, line)
}

return strings.TrimSpace(line[0:endIdx]), endIdx + 1, nil
return strings.TrimSpace(line[0:endIdx]), endIdx + delimiterLen(line[endIdx:]), nil
}

// delimiterLen returns the length in bytes of the delimiter that starts s,
// which may be a non-ASCII character.
func delimiterLen(s string) int {
_, n := utf8.DecodeRuneInString(s)
return n
}

// hasSurroundedQuote reports whether in starts and ends with quote and has no
Expand Down
Loading