Skip to content

Commit 8f5e31a

Browse files
committed
FormatAll: use a string-builder for formatting os-options
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 0165130 commit 8f5e31a

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

platforms.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,20 +373,50 @@ func FormatAll(platform specs.Platform) string {
373373
}
374374

375375
var b strings.Builder
376-
b.WriteString(encodeOSOption(platform.OSVersion))
377-
features := platform.OSFeatures
376+
b.WriteString(platform.OS)
377+
osv := encodeOSOption(platform.OSVersion)
378+
formatted := formatOSFeatures(platform.OSFeatures)
379+
if osv != "" || formatted != "" {
380+
b.Grow(len(osv) + len(formatted) + 3) // parens + maybe '+'
381+
b.WriteByte('(')
382+
if osv != "" {
383+
b.WriteString(osv)
384+
}
385+
if formatted != "" {
386+
b.WriteByte('+')
387+
b.WriteString(formatted)
388+
}
389+
b.WriteByte(')')
390+
}
391+
392+
return path.Join(b.String(), platform.Architecture, platform.Variant)
393+
}
394+
395+
func formatOSFeatures(features []string) string {
396+
if len(features) == 0 {
397+
return ""
398+
}
399+
378400
if !slices.IsSorted(features) {
379401
features = slices.Clone(features)
380402
slices.Sort(features)
381403
}
404+
var b strings.Builder
405+
var wrote bool
406+
var prev string
382407
for _, f := range features {
383-
b.WriteString("+" + encodeOSOption(f))
384-
}
385-
if b.Len() > 0 {
386-
osAndVersion := platform.OS + "(" + b.String() + ")"
387-
return path.Join(osAndVersion, platform.Architecture, platform.Variant)
408+
if f == "" || f == prev {
409+
// skip empty and duplicate values
410+
continue
411+
}
412+
prev = f
413+
if wrote {
414+
b.WriteByte('+')
415+
}
416+
b.WriteString(encodeOSOption(f))
417+
wrote = true
388418
}
389-
return path.Join(platform.OS, platform.Architecture, platform.Variant)
419+
return b.String()
390420
}
391421

392422
// osOptionReplacer encodes characters in OS option values (version and

0 commit comments

Comments
 (0)