Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong Token offset with CLRF and LF #303

Closed
mih-kopylov opened this issue Jun 27, 2022 · 1 comment · Fixed by #524
Closed

Wrong Token offset with CLRF and LF #303

mih-kopylov opened this issue Jun 27, 2022 · 1 comment · Fixed by #524
Labels
bug Something isn't working parser

Comments

@mih-kopylov
Copy link

Tests to reproduce

import (
	"github.com/goccy/go-yaml/lexer"
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestCrlfDemo(t *testing.T) {
	content := "project:\r\n  version: 1.2.3\r\n"
	tokens := lexer.Tokenize(content)
	assert.Len(t, tokens, 5)
	assert.Equal(t, "1.2.3", tokens[4].Value)
	assert.Equal(t, 21, tokens[4].Position.Offset)
}

func TestLfDemo(t *testing.T) {
	content := "project:\n  version: 1.2.3\n"
	tokens := lexer.Tokenize(content)
	assert.Len(t, tokens, 5)
	assert.Equal(t, "1.2.3", tokens[4].Value)
	assert.Equal(t, 20, tokens[4].Position.Offset)
}
=== RUN   TestCrlfDemo
    demo_test.go:14: 
        	Error Trace:	demo_test.go:14
        	Error:      	Not equal: 
        	            	expected: 21
        	            	actual  : 26
        	Test:       	TestCrlfDemo
--- FAIL: TestCrlfDemo (0.00s)

=== RUN   TestLfDemo
    demo_test.go:22: 
        	Error Trace:	demo_test.go:22
        	Error:      	Not equal: 
        	            	expected: 20
        	            	actual  : 26
        	Test:       	TestLfDemo
--- FAIL: TestLfDemo (0.00s)

Notes

  • TestCrlfDemo has the version token at the position 21, but the token stores 26, like the end of the token
  • TestLfDemo has the version token at the position 20, but the token stores 26 again, like the end of the token + 1
@goccy goccy added bug Something isn't working parser labels Nov 6, 2024
@goccy goccy mentioned this issue Nov 12, 2024
@goccy
Copy link
Owner

goccy commented Nov 12, 2024

@mih-kopylov Thank you for your reports. The offset starts at 1, with CRLF counting as 2 characters and LF as 1 character, so the correct values are 22 and 21.

I've fixed this problem with #524

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants