diff options
| author | Shulhan <ms@kilabit.info> | 2026-01-13 01:44:23 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-01-13 01:45:18 +0700 |
| commit | 434139fe3918fdb56704558301ad9275f1da06ad (patch) | |
| tree | 76f4a942f9f47398ab153a8319b2c4d2e442d167 /file.go | |
| parent | e16e2a4ec74443aa8f4c21a73ee837cb72ed46fb (diff) | |
| download | spdxconv-434139fe3918fdb56704558301ad9275f1da06ad.tar.xz | |
all: split the delete_line_pattern into before and after
While at it, also add configuration for delete line before and after
for match-copyright section.
Diffstat (limited to 'file.go')
| -rw-r--r-- | file.go | 165 |
1 files changed, 124 insertions, 41 deletions
@@ -35,7 +35,8 @@ var reCopyrightText = regexp.MustCompile(`^(//+|#+|/\*+|<!--+)?\s?SPDX-FileCopyr // REUSE-IgnoreEnd type file struct { - matchLicense *matchLicense + matchCopyright *matchCopyright + matchLicense *matchLicense path string @@ -226,7 +227,7 @@ func (f *file) apply(conv *SPDXConv) (err error) { return err } - f.applyDelete() + f.applyDelete(&conv.cfg) // REUSE-IgnoreStart line := fmt.Sprintf("%sSPDX-License-Identifier: %s%s", @@ -360,19 +361,18 @@ func (f *file) applyCopyrightText(conv *SPDXConv) (err error) { } // Verify that the line actually match with one of // match-copyright pattern. - var cmc *matchCopyright - for _, cmc = range conv.cfg.MatchCopyright { - if cmc.match(string(line)) { - f.copyrightYear = cmc.year - f.copyrightText = cmc.String() + for _, f.matchCopyright = range conv.cfg.MatchCopyright { + if f.matchCopyright.match(string(line)) { + f.copyrightYear = f.matchCopyright.year + f.copyrightText = f.matchCopyright.String() break } } - if cmc == nil { - return fmt.Errorf(`%s: line %d does not match with any of match-license pattern`, - f.path, f.idxLicenseID) + if f.matchCopyright == nil { + return fmt.Errorf(`%s: line %d does not match with any of match-copyright pattern`, + f.path, f.idxCopyrightText) } - if f.idxLicenseID >= 0 { + if f.idxCopyrightText >= 0 { f.topLines[x] = nil } else { f.bottomLines[x] = nil @@ -381,48 +381,131 @@ func (f *file) applyCopyrightText(conv *SPDXConv) (err error) { return nil } -func (f *file) applyDelete() { - var reDeleteLine []*regexp.Regexp +func (f *file) applyDelete(cfg *config) { + var startat int + var lines [][]byte if f.matchLicense != nil { - reDeleteLine = f.matchLicense.reDeleteLine + if f.idxLicenseID >= 0 { + startat = f.idxLicenseID + lines = f.topLines + } else { + startat = f.idxLicenseID + cfg.MaxLineMatch + lines = f.bottomLines + } + f.deleteLineBefore(f.matchLicense.reDeleteLineBefore, lines, startat-1) + f.deleteLineAfter(f.matchLicense.reDeleteLineAfter, lines, startat+1) } - var lines [][]byte - for _, line := range f.topLines { - if line == nil { - continue + if f.matchCopyright != nil { + if f.idxCopyrightText >= 0 { + startat = f.idxCopyrightText + lines = f.topLines + } else { + startat = f.idxCopyrightText + cfg.MaxLineMatch + lines = f.bottomLines } - var found bool - for _, redel := range reDeleteLine { - if redel.Match(line) { - found = true - break - } + f.deleteLineBefore(f.matchCopyright.reDeleteLineBefore, lines, startat-1) + f.deleteLineAfter(f.matchCopyright.reDeleteLineAfter, lines, startat+1) + } + f.trimDeletedLines() +} + +func (f *file) deleteLineBefore(listre []*regexp.Regexp, lines [][]byte, startat int) { + if len(listre) == 0 { + return + } + var idxre = 0 + var re = listre[idxre] + var line []byte + for x := startat; x >= 0; x-- { + if lines[x] == nil { + return } - if found { - continue + line = lines[x] + if !re.Match(line) { + // Once the regex does not match with line, return + // immediately. + return + } + lines[x] = nil + idxre++ + if idxre == len(listre) { + break } - lines = append(lines, line) + re = listre[idxre] } - f.topLines = lines + // Continue deleting with the next lines. + for x := len(f.lines) - 1; x >= 0; x-- { + line = f.lines[x] + if !re.Match(line) { + return + } + f.lines[x] = nil + idxre++ + if idxre == len(listre) { + break + } + re = listre[idxre] + } +} - lines = nil - for _, line := range f.bottomLines { - if line == nil { - continue +func (f *file) deleteLineAfter(listre []*regexp.Regexp, lines [][]byte, startat int) { + if len(listre) == 0 { + return + } + var idxre = 0 + var re = listre[idxre] + var line []byte + for x := startat; x < len(lines); x++ { + if lines[x] == nil { + return } - var found bool - for _, redel := range reDeleteLine { - if redel.Match(line) { - found = true - break - } + line = lines[x] + if !re.Match(line) { + // Once the regex does not match with line, return + // immediately. + return + } + lines[x] = nil + idxre++ + if idxre == len(listre) { + break } - if found { + re = listre[idxre] + } + // Continue deleting with the next lines. + for x := 0; x < len(f.lines); x++ { + line = f.lines[x] + if !re.Match(line) { + return + } + f.lines[x] = nil + idxre++ + if idxre == len(listre) { + break + } + re = listre[idxre] + } +} + +func (f *file) trimDeletedLines() { + var newlines [][]byte + var lines [][]byte + if f.idxLicenseID >= 0 { + lines = f.topLines + } else { + lines = f.bottomLines + } + for x := 0; x < len(lines); x++ { + if lines[x] == nil { continue } - lines = append(lines, line) + newlines = append(newlines, lines[x]) + } + if f.idxLicenseID >= 0 { + f.topLines = newlines + } else { + f.bottomLines = newlines } - f.bottomLines = lines } // insertEmptyLine insert empty line after SPDX identifiers or any comments after it. |
