-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.go
More file actions
236 lines (218 loc) · 5.32 KB
/
version.go
File metadata and controls
236 lines (218 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// The rbxver package handles parsing and formatting of Roblox version strings.
package rbxver
import (
"bytes"
"encoding/json"
"errors"
"io"
"strconv"
"strings"
)
// Format determines how a version is parsed and formatted.
type Format int
const (
// Parse by guessing separator. Format as `0.0.0.0`.
Any Format = iota
// Parse with dot as separator. Format as `0.0.0.0`.
Dot
// Parse with comma as separator. Format as `0, 0, 0, 0`.
Comma
)
// Version represents the version of a Roblox build. Versions can be compared
// for equality.
type Version struct {
Generation int // The first component.
Version int // The second component.
Patch int // The third component.
Commit int // The fourth component.
// How the version was formatted, or how to format the version.
Format Format
}
// Formats i, writing to b. Writes 0 if i is less than 0.
func formatInt(b *strings.Builder, i int) {
if i <= 0 {
b.WriteByte('0')
return
}
b.Write(strconv.AppendInt(nil, int64(i), 10))
}
// String returns v as a string according to v.Format.
func (v Version) String() string {
var sep string
switch v.Format {
default:
fallthrough
case Any, Dot:
sep = "."
case Comma:
sep = ", "
}
var b strings.Builder
formatInt(&b, v.Generation)
b.WriteString(sep)
formatInt(&b, v.Version)
b.WriteString(sep)
formatInt(&b, v.Patch)
b.WriteString(sep)
formatInt(&b, v.Commit)
return b.String()
}
// Compare returns -1 if v is semantically lower than u, 1 if v is semantically
// higher than u, and 0 if v is semantically equal to u.
func (v Version) Compare(u Version) int {
switch {
case v.Generation < u.Generation:
return -1
case v.Generation > u.Generation:
return 1
case v.Version < u.Version:
return -1
case v.Version > u.Version:
return 1
case v.Patch < u.Patch:
return -1
case v.Patch > u.Patch:
return 1
case v.Commit < u.Commit:
return -1
case v.Commit > u.Commit:
return 1
}
return 0
}
// Implements json.Marshaler.
func (v Version) MarshalJSON() (b []byte, err error) {
b = append(b, '"')
b = append(b, v.String()...)
b = append(b, '"')
return b, nil
}
// Implements json.Unmarshaler.
func (v *Version) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
u, n, err := ParseBytes([]byte(s), v.Format)
if err != nil {
return err
}
if n != len(s) {
return ErrSyntax
}
*v = u
return nil
}
// Parses an integer from b to comp. Returns false if an error occurred when
// parsing the integer, or the value is less than 0. b is set to the index after
// the parsed value.
func parseInt(comp *int, b *[]byte) bool {
i := 0
for ; len(*b) > i && '0' <= (*b)[i] && (*b)[i] <= '9'; i++ {
}
n, err := strconv.ParseInt(string((*b)[:i]), 10, strconv.IntSize)
if err != nil || n < 0 {
return false
}
*comp = int(n)
*b = (*b)[i:]
return true
}
// Expects sep at the start of b. If *sep is nil, then the separator will be
// guessed, and sep is set to the guessed separator. b is set to the index after
// the parsed separator and any whitespace.
func parseSep(sep *[]byte, b *[]byte) error {
if len(*b) < 2 {
return io.ErrUnexpectedEOF
}
if *sep == nil {
// Guess separator. This will be used for subsequent separators.
switch (*b)[0] {
case '.':
*sep = (*b)[:1]
case ',':
if (*b)[1] != ' ' {
return ErrSyntax
}
*sep = (*b)[:2]
default:
return ErrSyntax
}
} else {
if len(*b) < len(*sep) {
return io.ErrUnexpectedEOF
}
if !bytes.Equal((*b)[:len(*sep)], *sep) {
return ErrSyntax
}
}
*b = (*b)[len(*sep):]
return nil
}
// ErrSyntax indicates a syntax error while parsing a version string.
var ErrSyntax = errors.New("invalid syntax")
// ParseBytes parses a version from b according to f.
//
// n returns the number of bytes that were parsed from b. Trailing bytes that
// are not a part of the parsed version do not cause an error.
//
// err will be ErrSyntax if the syntax is invalid, or io.ErrUnexpectedEOF if b
// does not have enough bytes to correctly parse the version. In either case, n
// will indicate where the error occurred.
//
// Panics if f is not valid format.
func ParseBytes(b []byte, f Format) (v Version, n int, err error) {
var sep []byte
switch f {
case Any:
case Dot:
sep = []byte{'.'}
case Comma:
sep = []byte{',', ' '}
default:
panic("invalid format")
}
l := len(b)
if len(b) == 0 {
return v, l - len(b), io.ErrUnexpectedEOF
}
if !parseInt(&v.Generation, &b) {
return v, l - len(b), ErrSyntax
}
if err := parseSep(&sep, &b); err != nil {
return v, l - len(b), err
}
if !parseInt(&v.Version, &b) {
return v, l - len(b), ErrSyntax
}
if err := parseSep(&sep, &b); err != nil {
return v, l - len(b), err
}
if !parseInt(&v.Patch, &b) {
return v, l - len(b), ErrSyntax
}
if err := parseSep(&sep, &b); err != nil {
return v, l - len(b), err
}
if !parseInt(&v.Commit, &b) {
return v, l - len(b), ErrSyntax
}
switch sep[0] {
case '.':
v.Format = Dot
case ',':
v.Format = Comma
}
return v, l - len(b), nil
}
// Parse parses s as a version string according to f. Returns the zero value if
// a version could not be parsed. A correctly parsed version will always have a
// non-zero Format.
//
// Panics if f is not valid format.
func Parse(s string, f Format) Version {
if v, n, err := ParseBytes([]byte(s), f); err == nil && n == len(s) {
return v
}
return Version{}
}