Skip to content

Recursion in getStatementStart could be a loopΒ #232

@panzi

Description

@panzi

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions