// Package confline contains small lexical helpers shared by mpv.conf, // input.conf, and script-options parsers. package confline // CommentIndex returns the byte index of the first unquoted, unescaped '#'. // When requireWhitespace is true, '#' starts a comment only at the beginning // of the line or after a space/tab; this preserves values such as #FF8232. func CommentIndex(line string, requireWhitespace bool) int { var quote byte escaped := false for index := 0; index < len(line); index++ { character := line[index] if escaped { escaped = false continue } if character == '\\' { escaped = true continue } if quote != 0 { if character == quote { quote = 0 } continue } if character == '\'' || character == '"' { quote = character continue } if character != '#' { continue } if !requireWhitespace || index == 0 || line[index-1] == ' ' || line[index-1] == '\t' { return index } } return -1 }