cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.75.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/output/ranges.go

45lines · modecode

1package output
2
3import (
4 "fmt"
5 "slices"
6 "strconv"
7 "strings"
8)
9
10func FormatLineRangeString(lines []int) string {
11 ls := make([]int, len(lines))
12 copy(ls, lines)
13 slices.Sort(ls)
14
15 var ranges []string
16 start := -1
17 end := -1
18 for _, l := range ls {
19 switch {
20 case start < 0:
21 start = l
22 end = l
23 case l == end+1:
24 end = l
25 default:
26 if start > 0 && end > 0 {
27 ranges = append(ranges, printRange(start, end))
28 }
29 start = l
30 end = l
31 }
32 }
33 if start > 0 && end > 0 {
34 ranges = append(ranges, printRange(start, end))
35 }
36
37 return strings.Join(ranges, " ")
38}
39
40func printRange(start, end int) string {
41 if start == end {
42 return strconv.Itoa(start)
43 }
44 return fmt.Sprintf("%d-%d", start, end)
45}
46