mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-05-06 22:18:28 +00:00
Canonical case-folding for Debian stanzas. #193
This commit is contained in:
+27
-1
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Stanza or paragraph of Debian control file
|
// Stanza or paragraph of Debian control file
|
||||||
@@ -157,6 +158,31 @@ func init() {
|
|||||||
multilineFields["MD5Sum"] = true
|
multilineFields["MD5Sum"] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func canonicalCase(field string) string {
|
||||||
|
upper := strings.ToUpper(field)
|
||||||
|
if upper == "SHA1" || upper == "SHA256" {
|
||||||
|
return upper
|
||||||
|
}
|
||||||
|
if upper == "MD5SUM" {
|
||||||
|
return "MD5Sum"
|
||||||
|
}
|
||||||
|
|
||||||
|
startOfWord := true
|
||||||
|
|
||||||
|
return strings.Map(func(r rune) rune {
|
||||||
|
if startOfWord {
|
||||||
|
startOfWord = false
|
||||||
|
return unicode.ToUpper(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
if r == '-' {
|
||||||
|
startOfWord = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return unicode.ToLower(r)
|
||||||
|
}, field)
|
||||||
|
}
|
||||||
|
|
||||||
// ControlFileReader implements reading of control files stanza by stanza
|
// ControlFileReader implements reading of control files stanza by stanza
|
||||||
type ControlFileReader struct {
|
type ControlFileReader struct {
|
||||||
scanner *bufio.Scanner
|
scanner *bufio.Scanner
|
||||||
@@ -195,7 +221,7 @@ func (c *ControlFileReader) ReadStanza() (Stanza, error) {
|
|||||||
if len(parts) != 2 {
|
if len(parts) != 2 {
|
||||||
return nil, ErrMalformedStanza
|
return nil, ErrMalformedStanza
|
||||||
}
|
}
|
||||||
lastField = parts[0]
|
lastField = canonicalCase(parts[0])
|
||||||
_, lastFieldMultiline = multilineFields[lastField]
|
_, lastFieldMultiline = multilineFields[lastField]
|
||||||
if lastFieldMultiline {
|
if lastFieldMultiline {
|
||||||
stanza[lastField] = parts[1]
|
stanza[lastField] = parts[1]
|
||||||
|
|||||||
@@ -123,6 +123,18 @@ func (s *ControlFileSuite) TestReadWriteStanza(c *C) {
|
|||||||
c.Assert(strings.HasPrefix(str, "Package: "), Equals, true)
|
c.Assert(strings.HasPrefix(str, "Package: "), Equals, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *ControlFileSuite) TestCanonicalCase(c *C) {
|
||||||
|
c.Check(canonicalCase("Package"), Equals, "Package")
|
||||||
|
c.Check(canonicalCase("package"), Equals, "Package")
|
||||||
|
c.Check(canonicalCase("pAckaGe"), Equals, "Package")
|
||||||
|
c.Check(canonicalCase("MD5Sum"), Equals, "MD5Sum")
|
||||||
|
c.Check(canonicalCase("SHA1"), Equals, "SHA1")
|
||||||
|
c.Check(canonicalCase("SHA256"), Equals, "SHA256")
|
||||||
|
c.Check(canonicalCase("Package-List"), Equals, "Package-List")
|
||||||
|
c.Check(canonicalCase("package-list"), Equals, "Package-List")
|
||||||
|
c.Check(canonicalCase("packaGe-lIst"), Equals, "Package-List")
|
||||||
|
}
|
||||||
|
|
||||||
func (s *ControlFileSuite) BenchmarkReadStanza(c *C) {
|
func (s *ControlFileSuite) BenchmarkReadStanza(c *C) {
|
||||||
for i := 0; i < c.N; i++ {
|
for i := 0; i < c.N; i++ {
|
||||||
reader := bytes.NewBufferString(controlFile)
|
reader := bytes.NewBufferString(controlFile)
|
||||||
|
|||||||
Reference in New Issue
Block a user