From 17bb29e95c84045f2e68be54bdd29bb7b7a71663 Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Thu, 26 Nov 2026 12:01:26 +0530 Subject: [PATCH] Skip the whole key-value delimiter when it is not ASCII MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit readKeyName moved past the delimiter by one byte, so a multi-byte delimiter such as ":" left its other bytes at the start of the value. --- docs/migrate-v1-to-v2.md | 1 + ini_test.go | 8 ++++++++ lexer.go | 13 ++++++++++--- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/docs/migrate-v1-to-v2.md b/docs/migrate-v1-to-v2.md index f3a58a8..759c853 100644 --- a/docs/migrate-v1-to-v2.md +++ b/docs/migrate-v1-to-v2.md @@ -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. diff --git a/ini_test.go b/ini_test.go index 84d19df..57633ba 100644 --- a/ini_test.go +++ b/ini_test.go @@ -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) { diff --git a/lexer.go b/lexer.go index 2c86e28..efc11be 100644 --- a/lexer.go +++ b/lexer.go @@ -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) @@ -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