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

Recursion in getStatementStart could be a loop #232

Open
panzi opened this issue Jun 9, 2024 · 0 comments
Open

Recursion in getStatementStart could be a loop #232

panzi opened this issue Jun 9, 2024 · 0 comments

Comments

@panzi
Copy link

panzi commented Jun 9, 2024

Hey, I just noticed that the recursion here could be a simple loop, I think? (I'm new to Go, not new to programming.)

godotenv/parser.go

Lines 50 to 68 in 3fc4292

func getStatementStart(src []byte) []byte {
pos := indexOfNonSpaceChar(src)
if pos == -1 {
return nil
}
src = src[pos:]
if src[0] != charComment {
return src
}
// skip comment section
pos = bytes.IndexFunc(src, isCharFunc('\n'))
if pos == -1 {
return nil
}
return getStatementStart(src[pos:])
}

Could be something like this:

func getStatementStart(src []byte) []byte {
	for {
		pos := indexOfNonSpaceChar(src)
		if pos == -1 {
			return nil
		}

		src = src[pos:]
		if src[0] != charComment {
			return src
		}

		// skip comment section
		pos = bytes.IndexFunc(src, isCharFunc('\n'))
		if pos == -1 {
			return nil
		}

		src = src[pos:]
	}
}

This is not a bug, just something I noticed while reading the source in order to understand exactly what syntax this tool is accepting. 😄
Sorry for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant