Skip to content

Implement range object #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Seperate range __repr__ for version constraint
strings.Builder is supported since v1.10,
so split files for older versions
  • Loading branch information
HyeockJinKim committed Sep 20, 2019
commit 33b1e4055448a546c8a1f139d16f5f7916340633
32 changes: 0 additions & 32 deletions py/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

package py

import (
"strings"
)

// A python Range object
// FIXME one day support BigInts too!
type Range struct {
Expand Down Expand Up @@ -112,34 +108,6 @@ func (r *Range) M__str__() (Object, error) {
return r.M__repr__()
}

func (r *Range) repr() (Object, error) {
var b strings.Builder
b.WriteString("range(")
start, err := ReprAsString(r.Start)
if err != nil {
return nil, err
}
stop, err := ReprAsString(r.Stop)
if err != nil {
return nil, err
}
b.WriteString(start)
b.WriteString(", ")
b.WriteString(stop)

if r.Step != 1 {
step, err := ReprAsString(r.Step)
if err != nil {
return nil, err
}
b.WriteString(", ")
b.WriteString(step)
}
b.WriteString(")")

return String(b.String()), nil
}

func (r *Range) M__repr__() (Object, error) {
return r.repr()
}
Expand Down
38 changes: 38 additions & 0 deletions py/range_repr110.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build go1.10
// Range object

package py

import "strings"

func (r *Range) repr() (Object, error) {
var b strings.Builder
b.WriteString("range(")
start, err := ReprAsString(r.Start)
if err != nil {
return nil, err
}
stop, err := ReprAsString(r.Stop)
if err != nil {
return nil, err
}
b.WriteString(start)
b.WriteString(", ")
b.WriteString(stop)

if r.Step != 1 {
step, err := ReprAsString(r.Step)
if err != nil {
return nil, err
}
b.WriteString(", ")
b.WriteString(step)
}
b.WriteString(")")

return String(b.String()), nil
}
38 changes: 38 additions & 0 deletions py/range_repr19.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !go1.10
// Range object

package py

import "bytes"

func (r *Range) repr() (Object, error) {
var b bytes.Buffer
b.WriteString("range(")
start, err := ReprAsString(r.Start)
if err != nil {
return nil, err
}
stop, err := ReprAsString(r.Stop)
if err != nil {
return nil, err
}
b.WriteString(start)
b.WriteString(", ")
b.WriteString(stop)

if r.Step != 1 {
step, err := ReprAsString(r.Step)
if err != nil {
return nil, err
}
b.WriteString(", ")
b.WriteString(step)
}
b.WriteString(")")

return String(b.String()), nil
}