Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions license/classifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func TestStandardizeLicensePyPIClassifiers(t *testing.T) {
{"dedup", []string{"License :: OSI Approved :: BSD License", "License :: OSI Approved :: BSD License"}, []string{"BSD-3-Clause"}},
{"none", []string{"Programming Language :: Python :: 3"}, []string{"Unknown"}},
{"not-included", []string{"License :: Public Domain"}, []string{"Unknown"}},
// LGPLv3 (without "+") denotes exactly v3, so it must map to
// LGPL-3.0-only, not the more permissive -or-later form.
{"lgplv3-is-v3-only", []string{"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)"}, []string{"LGPL-3.0-only"}},
// Versionless GPL/LGPL classifiers state no version, so we must not
// fabricate one; they derive to Unknown.
{"versionless-gpl-is-unknown", []string{"License :: OSI Approved :: GNU General Public License (GPL)"}, []string{"Unknown"}},
{"versionless-lgpl-is-unknown", []string{"License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)"}, []string{"Unknown"}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions license/data/pypi_licenses.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
{
"Classifier": "License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
"Name": "GNU Lesser General Public License v3 (LGPLv3)",
"SPDXIdentifier": "LGPL-3.0-or-later",
"SPDXIdentifier": "LGPL-3.0-only",
"SPDXNotIncluded": false
},
{
Expand Down Expand Up @@ -356,8 +356,8 @@
{
"Classifier": "License :: OSI Approved :: GNU General Public License (GPL)",
"Name": "GNU General Public License (GPL)",
"SPDXIdentifier": "GPL-3.0-only",
"SPDXNotIncluded": false
"SPDXIdentifier": "",
"SPDXNotIncluded": true
},
{
"Classifier": "License :: OSI Approved :: Nokia Open Source License",
Expand Down Expand Up @@ -416,8 +416,8 @@
{
"Classifier": "License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)",
"Name": "GNU Library or Lesser General Public License (LGPL)",
"SPDXIdentifier": "LGPL-3.0-only",
"SPDXNotIncluded": false
"SPDXIdentifier": "",
"SPDXNotIncluded": true
},
{
"Classifier": "License :: OSI Approved :: MIT No Attribution License (MIT-0)",
Expand Down
12 changes: 6 additions & 6 deletions license/spdx_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package license
import (
"strings"
"unicode"
"unicode/utf8"
)

// ParseSPDXExpression parses an SPDX license expression and returns the individual
Expand Down Expand Up @@ -49,34 +50,33 @@ func tokenizeSPDX(expr string) []string {
}
}

// Iterate rune-by-rune (not byte-by-byte) so that multi-byte UTF-8
// whitespace separators (e.g. NBSP U+00A0) are classified correctly by
// unicode.IsSpace and split the expression like an ASCII space would.
i := 0
for i < len(expr) {
ch := rune(expr[i])
ch, size := utf8.DecodeRuneInString(expr[i:])
i += size

switch {
case ch == '(' || ch == ')':
flushCurrent()
tokens = append(tokens, string(ch))
i++

case ch == '+':
// + suffix attaches to the previous identifier (no whitespace per spec)
// but we handle it as part of the identifier
current.WriteRune(ch)
i++

case ch == ':':
// Part of DocumentRef-X:LicenseRef-Y format
current.WriteRune(ch)
i++

case unicode.IsSpace(ch):
flushCurrent()
i++

default:
current.WriteRune(ch)
i++
}
}

Expand Down
7 changes: 7 additions & 0 deletions license/spdx_expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ func TestParseSPDXExpression(t *testing.T) {
// Edge cases
{"extra whitespace", " MIT OR Apache-2.0 ", []string{"MIT", "Apache-2.0"}},
{"mixed case operators", "MIT Or Apache-2.0", []string{"MIT", "Apache-2.0"}},

// Non-ASCII (Unicode) whitespace separators must split like ASCII spaces.
// NBSP (U+00A0) is multi-byte in UTF-8, so a byte-wise tokenizer would
// misread it and fail to split the expression.
{"NBSP-separated OR expression", "MIT OR Apache-2.0", []string{"MIT", "Apache-2.0"}},
{"NBSP-separated AND expression", "MIT AND Apache-2.0", []string{"MIT", "Apache-2.0"}},
{"ideographic-space-separated OR expression", "MIT OR Apache-2.0", []string{"MIT", "Apache-2.0"}},
}

for _, tc := range cases {
Expand Down