-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.go
63 lines (56 loc) · 1015 Bytes
/
utils.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
57
58
59
60
61
62
63
package diff
import (
"bytes"
)
// min for int's
func min(i1, i2 int) int {
if i1 < i2 {
return i1
}
return i2
}
// max for int's
func max(i1, i2 int) int {
if i1 > i2 {
return i1
}
return i2
}
func countNewLines(text string) int {
count := 0
for _, char := range text {
if char == '\n' {
count++
}
}
return count
}
//
// string.Split is not usable for this purpose:
// * splits a text and removes the seperator
// * if the last element has a \n, this is added
// as a extra element
//
func splitText(text string) []string {
var lines []string
start := 0
for i := 0; i < len(text); i++ {
if text[i] == '\n' {
lines = append(lines, text[start:i+1])
start = i + 1
}
}
// add last element if one is pending
// (last element was without a \n)
if start < len(text) {
lines = append(lines, text[start:])
}
return lines
}
func joinLines(lines []string) string {
var buf bytes.Buffer
for _, line := range lines {
buf.WriteString(line)
}
return buf.String()
}