Skip to content

Commit fc906f0

Browse files
committed
Implement string __len__ and __bool__
1 parent 7d197e6 commit fc906f0

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

py/string.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,22 @@
99
package py
1010

1111
import (
12-
// "fmt"
12+
"unicode/utf8"
1313
)
1414

1515
type String string
1616

17-
var StringType = NewType("string", "str(object='') -> str\nstr(bytes_or_buffer[, encoding[, errors]]) -> str\n\nCreate a new string object from the given object. If encoding or\nerrors is specified, then the object must expose a data buffer\nthat will be decoded using the given encoding and error handler.\nOtherwise, returns the result of object.__str__() (if defined)\nor repr(object).\nencoding defaults to sys.getdefaultencoding().\nerrors defaults to 'strict'.")
17+
var StringType = NewType("string",
18+
`str(object='') -> str
19+
str(bytes_or_buffer[, encoding[, errors]]) -> str
20+
21+
Create a new string object from the given object. If encoding or
22+
errors is specified, then the object must expose a data buffer
23+
that will be decoded using the given encoding and error handler.
24+
Otherwise, returns the result of object.__str__() (if defined)
25+
or repr(object).
26+
encoding defaults to sys.getdefaultencoding().
27+
errors defaults to 'strict'.`)
1828

1929
// Type of this object
2030
func (s String) Type() *Type {
@@ -26,3 +36,15 @@ func (s String) Intern() String {
2636
// fmt.Printf("FIXME interning %q\n", s)
2737
return s
2838
}
39+
40+
func (s String) M__bool__() Object {
41+
return NewBool(len(s) > 0)
42+
}
43+
44+
func (s String) M__len__() Object {
45+
return Int(utf8.RuneCountInString(string(s)))
46+
}
47+
48+
// Check interface is satisfied
49+
var _ I__len__ = String("")
50+
var _ I__bool__ = String("")

0 commit comments

Comments
 (0)