func rewriteContent()

in pkg/header/fix.go [75:101]


func rewriteContent(style *comments.CommentStyle, content []byte, licenseHeader string, licensePattern *regexp.Regexp) []byte {
	// Remove previous license header version to allow update it
	if licensePattern != nil {
		content = licensePattern.ReplaceAll(content, []byte(""))
	}

	if style.After == "" {
		return append([]byte(licenseHeader), content...)
	}

	content = []byte(strings.TrimLeft(string(content), " \n"))
	afterPattern := regexp.MustCompile(style.After)
	location := afterPattern.FindIndex(content)
	if location == nil || len(location) != 2 {
		if style.EnsureAfter != "" {
			return append([]byte(style.EnsureAfter+"\n"+licenseHeader+style.EnsureBefore), content...)
		}
		return append([]byte(licenseHeader), content...)
	}

	// if files do not have an empty line at the end, the content slice index given
	//  at index location[1]+1 could be out of range
	startIdx := math.Min(float64(location[1]+1), float64(len(content)))
	return append(content[0:location[1]],
		append(append([]byte("\n"), []byte(licenseHeader)...), content[int64(startIdx):]...)...,
	)
}