-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
54 lines (42 loc) · 804 Bytes
/
util.go
File metadata and controls
54 lines (42 loc) · 804 Bytes
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
package gshell
import (
"io"
"os"
"golang.org/x/term"
)
func cost(x, y byte) int {
if x == y {
return 0
}
return 1
}
func editDistance(a, b string) int {
l1 := len(a)
l2 := len(b)
dp := make([][]int, l1+1)
for i := range dp {
dp[i] = make([]int, l2+1)
}
for i := 0; i <= l1; i++ {
dp[i][0] = i
}
for i := 0; i <= l2; i++ {
dp[0][i] = i
}
for i := 1; i <= l1; i++ {
for j := 1; j <= l2; j++ {
dp[i][j] = min(
dp[i-1][j]+1, // deletion
dp[i][j-1]+1, // insertion
dp[i-1][j-1]+cost(a[i-1], b[j-1]), // substitution
)
}
}
return dp[l1][l2]
}
func isTerminal(w io.Writer) bool {
if f, ok := w.(*os.File); ok {
return term.IsTerminal(int(f.Fd()))
}
return w != nil && w != os.Stdout && w != os.Stderr
}