Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e57a0df

Browse files
committed
Implement chr()
1 parent 338ca42 commit e57a0df

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

builtin/builtin.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func init() {
2424
// py.NewMethod("ascii", builtin_ascii, 0, ascii_doc),
2525
// py.NewMethod("bin", builtin_bin, 0, bin_doc),
2626
// py.NewMethod("callable", builtin_callable, 0, callable_doc),
27-
// py.NewMethod("chr", builtin_chr, 0, chr_doc),
27+
py.NewMethod("chr", builtin_chr, 0, chr_doc),
2828
py.NewMethod("compile", builtin_compile, 0, compile_doc),
2929
// py.NewMethod("delattr", builtin_delattr, 0, delattr_doc),
3030
// py.NewMethod("dir", builtin_dir, 0, dir_doc),
@@ -557,3 +557,21 @@ Return the number of items of a sequence or mapping.`
557557
func builtin_len(self, v py.Object) py.Object {
558558
return py.Len(v)
559559
}
560+
561+
const chr_doc = `chr(i) -> Unicode character
562+
563+
Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.`
564+
565+
func builtin_chr(self py.Object, args py.Tuple) py.Object {
566+
var xObj py.Object
567+
568+
py.ParseTuple(args, "i:chr", &xObj)
569+
570+
x := xObj.(py.Int)
571+
if x < 0 || x >= 0x110000 {
572+
panic(py.ExceptionNewf(py.ValueError, "chr() arg not in range(0x110000)"))
573+
}
574+
buf := make([]byte, 8)
575+
n := utf8.EncodeRune(buf, rune(x))
576+
return py.String(buf[:n])
577+
}

0 commit comments

Comments
 (0)