forked from glycerine/sshego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ignorespaces.go
72 lines (60 loc) · 1.43 KB
/
ignorespaces.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
64
65
66
67
68
69
70
71
72
package assertions
import (
"fmt"
"reflect"
)
// ShouldBeEqualIgnoringSpaces receives exactly two string parameters and ensures that the order
// of non-space characters in the two strings is identical. Only one character ' '
// is considered a space. This is not a general white-space ignoring routine! Differences in tabs or
// newlines *will* be noticed, and will cause the two strings to look different.
func ShouldBeEqualIgnoringSpaces(actual interface{}, expected ...interface{}) string {
if fail := need(1, expected); fail != success {
return fail
}
value, valueIsString := actual.(string)
expec, expecIsString := expected[0].(string)
if !valueIsString || !expecIsString {
return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0]))
}
if equalIgnoringSpaces(value, expec) {
return success
} else {
return fmt.Sprintf(shouldHaveBeenEqualIgnoringSpaces, value, expec)
}
}
func equalIgnoringSpaces(r, s string) bool {
nextr := 0
nexts := 0
for {
// skip past spaces in both r and s
for nextr < len(r) {
if r[nextr] == ' ' {
nextr++
} else {
break
}
}
for nexts < len(s) {
if s[nexts] == ' ' {
nexts++
} else {
break
}
}
if nextr >= len(r) && nexts >= len(s) {
return true
}
if nextr >= len(r) {
return false
}
if nexts >= len(s) {
return false
}
if r[nextr] != s[nexts] {
return false
}
nextr++
nexts++
}
return false
}