-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
159 lines (139 loc) · 3.24 KB
/
main.go
File metadata and controls
159 lines (139 loc) · 3.24 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// sort - sort text files by lines.
package main
import (
"solod.dev/so/bufio"
"solod.dev/so/c"
"solod.dev/so/flag"
"solod.dev/so/fmt"
"solod.dev/so/io"
"solod.dev/so/mem"
"solod.dev/so/os"
"solod.dev/so/slices"
"solod.dev/so/strings"
)
var reverse bool
var keyField int
var separator string
func main() {
parseFlags()
args := flag.Args()
lines := slices.MakeCap[string](mem.System, 0, 64)
defer freeLines(lines)
exitCode := 0
if len(args) == 0 {
lines = readLines(os.Stdin, lines)
} else {
for _, fname := range args {
if fname == "-" {
lines = readLines(os.Stdin, lines)
continue
}
f, err := os.Open(fname)
if err != nil {
fmt.Fprintf(os.Stderr, "sort: %s: No such file or directory\n", fname)
exitCode = 2
continue
}
lines = readLines(&f, lines)
f.Close()
}
}
slices.SortFunc(lines, compareLines)
for _, line := range lines {
fmt.Println(line)
}
os.Exit(exitCode)
}
// parseFlags parses command-line flags.
func parseFlags() {
flag.BoolVar(&reverse, "r", false, "sort in reverse order")
flag.IntVar(&keyField, "k", 0, "1-based field index to sort by")
flag.StringVar(&separator, "t", "", "field separator character")
flag.Parse()
}
// readLines reads lines from r and appends them to the lines slice.
func readLines(r io.Reader, lines []string) []string {
scanner := bufio.NewScanner(mem.System, r)
defer scanner.Free()
for scanner.Scan() {
line := strings.Clone(mem.System, scanner.Text())
lines = slices.Append(mem.System, lines, line)
}
return lines
}
// compareLines compares two lines for sorting, using
// the specified key field and separator if applicable.
func compareLines(a, b any) int {
sa := *c.PtrAs[string](a)
sb := *c.PtrAs[string](b)
if keyField > 0 {
sa = extractField(sa)
sb = extractField(sb)
}
result := strings.Compare(sa, sb)
if reverse {
return -result
}
return result
}
// freeLines frees the memory used by the lines slice and its contents.
func freeLines(lines []string) {
for _, line := range lines {
mem.FreeString(mem.System, line)
}
slices.Free(mem.System, lines)
}
// extractField extracts the specified key field from the line.
func extractField(line string) string {
if len(separator) > 0 {
return fieldBySep(line, keyField, separator[0])
}
return fieldBySpace(line, keyField)
}
// fieldBySpace extracts the specified field from the line,
// using whitespace as the separator.
func fieldBySpace(line string, field int) string {
n := len(line)
current := 0
i := 0
for i < n {
// Skip whitespace.
for i < n && isSpace(line[i]) {
i++
}
if i >= n {
break
}
current++
start := i
// Skip non-whitespace.
for i < n && !isSpace(line[i]) {
i++
}
if current == field {
return line[start:i]
}
}
return ""
}
// fieldBySep extracts the specified field from the line,
// using the specified separator character.
func fieldBySep(line string, field int, sep byte) string {
n := len(line)
current := 1
start := 0
for i := 0; i <= n; i++ {
if i == n || line[i] == sep {
if current == field {
return line[start:i]
}
current++
start = i + 1
}
}
return ""
}
// isSpace reports whether ch is a whitespace character.
func isSpace(ch byte) bool {
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
}