Skip to content

Commit 724e78a

Browse files
authored
Fix cannot detect font version (#8)
1 parent 45408ff commit 724e78a

File tree

7 files changed

+99
-20
lines changed

7 files changed

+99
-20
lines changed

.golangci.yaml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,25 @@ linters-settings:
1212
threshold: 100
1313
misspell:
1414
locale: US
15-
unused:
16-
check-exported: false
17-
unparam:
18-
check-exported: true
1915

2016
linters:
2117
enable-all: true
2218
disable:
2319
- depguard
24-
- execinquery
2520
- exhaustruct
2621
- forbidigo
2722
- forcetypeassert
2823
- gci
2924
- gochecknoglobals
30-
- gomnd
3125
- ireturn
3226
- lll
3327
- mnd
3428
- nonamedreturns
3529
- nolintlint # https://github.com/golangci/golangci-lint/issues/3063
3630
- paralleltest
31+
- recvcheck
3732
- tagliatelle
33+
- tenv
3834
- testpackage
3935
- varnamelen
4036
- wrapcheck

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ MODULE_NAME=fontforge
33
VENDOR_DIR = vendor
44

55
PYTHON_VERSION ?= 3.11
6-
GOLANGCI_LINT_VERSION ?= v1.60.1
6+
GOLANGCI_LINT_VERSION ?= v1.64.7
77

88
GO ?= go
99
GOLANGCI_LINT ?= $(shell go env GOPATH || true)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION)

font.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package fontforge
22

33
import (
44
"regexp"
5-
"strings"
65

7-
"github.com/Masterminds/semver/v3"
6+
semver "github.com/Masterminds/semver/v3"
87
python3 "go.nhat.io/python/v3"
98
)
109

@@ -257,14 +256,3 @@ func (n *SFNTName) UnmarshalPyObject(o *python3.Object) error { //nolint: unpara
257256

258257
// buildPattern is a regex pattern to detect build id in semver, such as `1.2 build 110`.
259258
var buildPattern = regexp.MustCompile(`\s+build\s+(\d+)$`)
260-
261-
func parseVersion(v string) *semver.Version {
262-
// Sanitize the version.
263-
v, _, _ = strings.Cut(v, ";")
264-
v = strings.TrimSpace(v)
265-
v = buildPattern.ReplaceAllString(v, "+$1")
266-
267-
r, _ := semver.NewVersion(v) //nolint: errcheck
268-
269-
return r
270-
}

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ toolchain go1.22.0
66

77
require (
88
github.com/Masterminds/semver/v3 v3.3.1
9+
github.com/stretchr/testify v1.10.0
910
go.nhat.io/python/v3 v3.11.2
1011
)
1112

1213
require (
14+
github.com/davecgh/go-spew v1.1.1 // indirect
15+
github.com/pmezard/go-difflib v1.0.0 // indirect
1316
go.nhat.io/cpy/v3 v3.11.2 // indirect
1417
go.nhat.io/once v0.3.0 // indirect
18+
gopkg.in/yaml.v3 v3.0.1 // indirect
1519
)
20+
21+
// v0.2.0 is incompatible with github.com/Masterminds/semver/v3 v3.3.1.
22+
retract v0.2.0

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ github.com/Masterminds/semver/v3 v3.3.1 h1:QtNSWtVZ3nBfk8mAOu/B6v7FMJ+NHTIgUPi7r
22
github.com/Masterminds/semver/v3 v3.3.1/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
6+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
7+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
8+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
59
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
610
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
711
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
@@ -12,5 +16,8 @@ go.nhat.io/once v0.3.0 h1:AwMxs8GWXhWS30Al5YbxDRvUwSo1XmgBNn1dr/x/KCQ=
1216
go.nhat.io/once v0.3.0/go.mod h1:1nB6JRBNV5S3GC/UIUtNDpfXxjlEOh55WyD2DxgQrsE=
1317
go.nhat.io/python/v3 v3.11.2 h1:BmO9KTjFSmi1TwURwWx8dqyhNOmFyPjyfinp/5ZyUL4=
1418
go.nhat.io/python/v3 v3.11.2/go.mod h1:G1y2bf0x2+TK/T9YbVC3MbxHeKCS2q7niqi2YjVPCHg=
19+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
20+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
21+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
1522
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1623
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

version.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package fontforge
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strconv"
7+
"strings"
8+
9+
semver "github.com/Masterminds/semver/v3"
10+
)
11+
12+
// semVerRegex is the regular expression used to parse a semantic version.
13+
const semVerRegex string = `v?([0-9]+)(\.[0-9]+)?(\.[0-9]+)?` +
14+
`(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?` +
15+
`(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?`
16+
17+
var versionRegex = regexp.MustCompile("^" + semVerRegex + "$")
18+
19+
func newVersion(v string) (*semver.Version, error) {
20+
m := versionRegex.FindStringSubmatch(v)
21+
if m == nil {
22+
return nil, semver.ErrInvalidSemVer
23+
}
24+
25+
metadata := m[8]
26+
pre := m[5]
27+
minor := uint64(0)
28+
patch := uint64(0)
29+
30+
var err error
31+
32+
major, err := strconv.ParseUint(m[1], 10, 64)
33+
if err != nil {
34+
return nil, fmt.Errorf("error parsing version segment: %w", err)
35+
}
36+
37+
if m[2] != "" {
38+
minor, err = strconv.ParseUint(strings.TrimPrefix(m[2], "."), 10, 64)
39+
if err != nil {
40+
return nil, fmt.Errorf("error parsing version segment: %w", err)
41+
}
42+
}
43+
44+
if m[3] != "" {
45+
patch, err = strconv.ParseUint(strings.TrimPrefix(m[3], "."), 10, 64)
46+
if err != nil {
47+
return nil, fmt.Errorf("error parsing version segment: %w", err)
48+
}
49+
}
50+
51+
return semver.NewVersion(semver.New(major, minor, patch, pre, metadata).String())
52+
}
53+
54+
func parseVersion(v string) *semver.Version {
55+
// Sanitize the version.
56+
v, _, _ = strings.Cut(v, ";")
57+
v = strings.TrimSpace(v)
58+
v = buildPattern.ReplaceAllString(v, "+$1")
59+
60+
r, _ := newVersion(v) //nolint: errcheck
61+
62+
return r
63+
}

version_internal_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package fontforge
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestNewVersion_Success(t *testing.T) {
11+
t.Parallel()
12+
13+
v, err := newVersion("003.001")
14+
require.NoError(t, err)
15+
require.NotNil(t, v)
16+
17+
assert.Equal(t, "3.1.0", v.String())
18+
}

0 commit comments

Comments
 (0)