|
3 | 3 | package py
|
4 | 4 |
|
5 | 5 | import (
|
| 6 | + "bytes" |
6 | 7 | "strings"
|
7 | 8 | )
|
8 | 9 |
|
@@ -112,3 +113,58 @@ func BytesFromObject(x Object) Bytes {
|
112 | 113 | })
|
113 | 114 | return b
|
114 | 115 | }
|
| 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