forked from maddyblue/sqlfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_int_set_str.go
executable file
·56 lines (53 loc) · 1.32 KB
/
fast_int_set_str.go
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
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package util
import (
"bytes"
"fmt"
)
// String returns a list representation of elements. Sequential runs of positive
// numbers are shown as ranges. For example, for the set {0, 1, 2, 5, 6, 10},
// the output is "(0-2,5,6,10)".
func (s FastIntSet) String() string {
var buf bytes.Buffer
buf.WriteByte('(')
appendRange := func(start, end int) {
if buf.Len() > 1 {
buf.WriteByte(',')
}
if start == end {
fmt.Fprintf(&buf, "%d", start)
} else if start+1 == end {
fmt.Fprintf(&buf, "%d,%d", start, end)
} else {
fmt.Fprintf(&buf, "%d-%d", start, end)
}
}
rangeStart, rangeEnd := -1, -1
s.ForEach(func(i int) {
if i < 0 {
appendRange(i, i)
return
}
if rangeStart != -1 && rangeEnd == i-1 {
rangeEnd = i
} else {
if rangeStart != -1 {
appendRange(rangeStart, rangeEnd)
}
rangeStart, rangeEnd = i, i
}
})
if rangeStart != -1 {
appendRange(rangeStart, rangeEnd)
}
buf.WriteByte(')')
return buf.String()
}