8000 Rich comparison for string · go-python/gpython@0dedea3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0dedea3

Browse files
committed
Rich comparison for string
1 parent 732ebed commit 0dedea3

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

py/string.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,63 @@ func (a String) M__imul__(other Object) Object {
8282
return a.M__mul__(other)
8383
}
8484

85-
// Check interface is satisfied
85+
// Convert an Object to an String
86+
//
87+
// Retrurns ok as to whether the conversion worked or not
88+
func convertToString(other Object) (String, bool) {
89+
switch b := other.(type) {
90+
case String:
91+
return b, true
92+
}
93+
return "", false
94+
}
95+
96+
// Rich comparison
97+
98+
func (a String) M__lt__(other Object) Object {
99+
if b, ok := convertToString(other); ok {
100+
return NewBool(a < b)
101+
}
102+
return NotImplemented
103+
}
104+
105+
func (a String) M__le__(other Object) Object {
106+
if b, ok := convertToString(other); ok {
107+
return NewBool(a <= b)
108+
}
109+
return NotImplemented
110+
}
111+
112+
func (a String) M__eq__(other Object) Object {
113+
if b, ok := convertToString(other); ok {
114+
return NewBool(a == b)
115+
}
116+
return NotImplemented
117+
}
118+
119+
func (a String) M__ne__(other Object) Object {
120+
if b, ok := convertToString(other); ok {
121+
return NewBool(a != b)
122+
}
123+
return NotImplemented
124+
}
125+
126+
func (a String) M__gt__(other Object) Object {
127+
if b, ok := convertToString(other); ok {
128+
return NewBool(a > b)
129+
}
130+
return NotImplemented
131+
}
132+
133+
func (a String) M__ge__(other Object) Object {
134+
if b, ok := convertToString(other); ok {
135+
return NewBool(a >= b)
136+
}
137+
return NotImplemented
138+
}
139+
140+
// Check stringerface is satisfied
141+
var _ richComparison = String("")
86142
var _ sequenceArithmetic = String("")
87143
var _ I__len__ = String("")
88144
var _ I__bool__ = String("")

0 commit comments

Comments
 (0)
0