cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.3.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/output/ranges.go

44lines · modecode

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