// Package header owns the shared rules for managed license headers. package header import ( "strings" "github.com/KofTwentyTwo/license-tool/internal/model" ) // Sentinel is the marker embedded in every header this tool writes. const Sentinel = "license-tool:managed" var bom = []byte{0xEF, 0xBB, 0xBF} // PreserveBoundary returns the byte offset after all leading preserve-first // prefixes the file type places before the managed header. func PreserveBoundary(content []byte, ft model.FileType) int { pos := 0 if HasRule(ft, model.PreserveBOM, false) && hasBOM(content[pos:]) { pos += len(bom) } allowShebang := true allowXML := HasRule(ft, model.PreserveXMLDecl, false) allowPHP := HasRule(ft, model.PreservePHPOpen, false) allowPragma := HasRule(ft, model.PreserveCodingPragma, false) allowGoBuild := HasRule(ft, model.PreserveGoBuildConstraint, false) allowCSSCharset := HasRule(ft, model.PreserveCSSCharset, false) allowDoctype := HasRule(ft, model.PreserveDoctype, false) for pos < len(content) { lineEnd := LineEndOffset(content, pos) lineText := string(content[pos:lineEnd]) trimmed := strings.TrimRight(lineText, "\r\n") consumed := false switch { case allowShebang && strings.HasPrefix(trimmed, "#!"): consumed = true case allowXML && strings.HasPrefix(strings.TrimSpace(trimmed), "= len(bom) && content[0] == bom[0] && content[1] == bom[1] && content[2] == bom[2] }