-
Notifications
You must be signed in to change notification settings - Fork 0
/
nstr.go
126 lines (104 loc) · 2.75 KB
/
nstr.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
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
package dirsyn
/*
NumericString implements [§ 3.3.23 of RFC 4517]:
NumericString = 1*(DIGIT / SPACE)
[§ 3.3.23 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.3.23
*/
type NumericString string
/*
String returns the string representation of the receiver instance.
*/
func (r NumericString) String() string {
return string(r)
}
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r NumericString) IsZero() bool { return len(r) == 0 }
func numericString(x any) (result Boolean) {
_, err := marshalNumericString(x)
result.Set(err == nil)
return
}
/*
NumericString returns an instance of [NumericString] alongside an error
following an analysis of x in the context of a Numeric String.
*/
func (r RFC4517) NumericString(x any) (NumericString, error) {
return marshalNumericString(x)
}
func marshalNumericString(x any) (ns NumericString, err error) {
var raw string
if raw, err = assertNumericString(x); err == nil {
for _, char := range raw {
if !(isDigit(rune(char)) || char == ' ') {
err = errorTxt("Incompatible character for Numeric String: " + string(char))
break
}
}
}
if err == nil {
ns = NumericString(raw)
}
return
}
func assertNumericString(x any) (raw string, err error) {
switch tv := x.(type) {
case int, int8, int16, int32, int64:
if isNegativeInteger(tv) {
err = errorTxt("Incompatible sign (-) for Numeric String")
break
}
var cint int64
if cint, err = castInt64(tv); err == nil {
raw = fmtInt(cint, 10)
}
case uint, uint8, uint16, uint32, uint64:
var cuint uint64
if cuint, err = castUint64(tv); err == nil {
raw = fmtUint(cuint, 10)
}
case string:
if len(tv) == 0 {
err = errorBadLength("Numeric String", 0)
break
}
raw = tv
default:
err = errorBadType("Numeric String")
}
return
}
// RFC 4518 § 2.6.2
func prepareNumericStringAssertion(a, b any) (str1, str2 string, err error) {
if str1, err = assertString(a, 0, "numericString"); err != nil {
return
}
if str2, err = assertString(b, 0, "numericString"); err != nil {
return
}
str1 = repAll(str1, ` `, ``)
str2 = repAll(str2, ` `, ``)
return
}
func numericStringMatch(a, b any) (result Boolean, err error) {
var str1, str2 string
if str1, str2, err = prepareNumericStringAssertion(a, b); err == nil {
result.Set(streq(str1, str2))
}
return
}
func numericStringOrderingMatch(a, b any) (result Boolean, err error) {
var str1, str2 string
if str1, str2, err = prepareNumericStringAssertion(a, b); err == nil {
result.Set(str1 < str2)
}
return
}
func numericStringSubstringsMatch(a, b any) (result Boolean, err error) {
var str1, str2 string
if str1, str2, err = prepareNumericStringAssertion(a, b); err == nil {
result, err = caseExactSubstringsMatch(str1, str2)
}
return
}