8000 py.Bytes: implement comparisons · go-python/gpython@b881849 · GitHub
[go: up one dir, main page]

Skip to content

Commit b881849

Browse files
committed
py.Bytes: implement comparisons
1 parent b8b5f4e commit b881849

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

py/bytes.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package py
44

55
import (
6+
"bytes"
67
"strings"
78
)
89

@@ -112,3 +113,58 @@ func BytesFromObject(x Object) Bytes {
112113
})
113114
return b
114115
}
116+
117+
// Convert an Object to an Bytes
118+
//
119+
// Retrurns ok as to whether the conversion worked or not
120+
func convertToBytes(other Object) (Bytes, bool) {
121+
switch b := other.(type) {
122+
case Bytes:
123+
return b, true
124+
}
125+
return []byte(nil), false
126+
}
127+
128+
// Rich comparison
129+
130+
func (a Bytes) M__lt__(other Object) Object {
131+
if b, ok := convertToBytes(other); ok {
132+
return NewBool(bytes.Compare(a, b) < 0)
133+
}
134+
return NotImplemented
135+
}
136+
137+
func (a Bytes) M__le__(other Object) Object {
138+
if b, ok := convertToBytes(other); ok {
139+
return NewBool(bytes.Compare(a, b) <= 0)
140+
}
141+
return NotImplemented
142+
}
143+
144+
func (a Bytes) M__eq__(other Object) Object {
145+
if b, ok := convertToBytes(other); ok {
146+
return NewBool(bytes.Compare(a, b) == 0)
147+
}
148+
return NotImplemented
149+
}
150+
151+
func (a Bytes) M__ne__(other Object) Object {
152+
if b, ok := convertToBytes(other); ok {
153+
return NewBool(bytes.Compare(a, b) != 0)
154+
}
155+
return NotImplemented
156+
}
157+
158+
func (a Bytes) M__gt__(other Object) Object {
159+
if b, ok := convertToBytes(other); ok {
160+
return NewBool(bytes.Compare(a, b) > 0)
161+
}
162+
return NotImplemented
163+
}
164+
165+
func (a Bytes) M__ge__(other Object) Object {
166+
if b, ok := convertToBytes(other); ok {
167+
return NewBool(bytes.Compare(a, b) >= 0)
168+
}
169+
return NotImplemented
170+
}

0 commit comments

Comments
 (0)
0