aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p@janouch.name>2018-10-09 18:21:56 +0200
committerPřemysl Janouch <p@janouch.name>2018-10-09 18:25:41 +0200
commit1e03aeacdd96223d19260ee5a9d8794d2a14fe38 (patch)
tree18d4f0bed6f164285e81f35d986af65f26459c9f
parentf7bb33cc3d87fe6636b52bf99c810eb321b1dc42 (diff)
downloadell-1e03aeacdd96223d19260ee5a9d8794d2a14fe38.tar.gz
ell-1e03aeacdd96223d19260ee5a9d8794d2a14fe38.tar.xz
ell-1e03aeacdd96223d19260ee5a9d8794d2a14fe38.zip
Go: use string for strings instead of []byte
A few conversions more, a few conversions less.
-rw-r--r--cmd/interpreter/main.go2
-rw-r--r--cmd/repl/main.go2
-rw-r--r--ell/ell.go36
3 files changed, 19 insertions, 21 deletions
diff --git a/cmd/interpreter/main.go b/cmd/interpreter/main.go
index 822aec8..97c8c25 100644
--- a/cmd/interpreter/main.go
+++ b/cmd/interpreter/main.go
@@ -52,7 +52,7 @@ func main() {
var args *ell.V
tail := &args
for i := 2; i < len(os.Args); i++ {
- *tail = ell.NewString([]byte(os.Args[i]))
+ *tail = ell.NewString(os.Args[i])
tail = &(*tail).Next
}
diff --git a/cmd/repl/main.go b/cmd/repl/main.go
index e7f35fb..eb253d0 100644
--- a/cmd/repl/main.go
+++ b/cmd/repl/main.go
@@ -46,7 +46,7 @@ func complete(L *ell.Ell, line string) (res []string) {
line = strings.ToLower(line)
for v := L.Globals; v != nil; v = v.Next {
- name := string(v.Head.String)
+ name := v.Head.String
if strings.HasPrefix(strings.ToLower(name), line) {
res = append(res, name)
}
diff --git a/ell/ell.go b/ell/ell.go
index 8ed371e..9c04b2e 100644
--- a/ell/ell.go
+++ b/ell/ell.go
@@ -44,7 +44,7 @@ type V struct {
Type VType // the type of this value
Next *V // next value in sequence
Head *V // the head of a VTypeList
- String []byte // the immutable contents of a VTypeString
+ String string // the immutable contents of a VTypeString
}
// Clone clones a value without following the rest of its chain.
@@ -53,11 +53,9 @@ func (v *V) Clone() *V {
return nil
}
return &V{
- Type: v.Type,
- Next: nil,
- Head: v.Head.CloneSeq(),
- // TODO: Consider actually storing the string as a string,
- // so that the compiler/runtime assure its immutability.
+ Type: v.Type,
+ Next: nil,
+ Head: v.Head.CloneSeq(),
String: v.String,
}
}
@@ -73,7 +71,7 @@ func (v *V) CloneSeq() *V {
}
// NewString creates a new value containing a string.
-func NewString(string []byte) *V {
+func NewString(string string) *V {
return &V{
Type: VTypeString,
String: string,
@@ -316,7 +314,7 @@ func printString(w io.Writer, s *V) bool {
return false
}
if !printStringNeedsQuoting(s) {
- _, _ = w.Write(s.String)
+ _, _ = w.Write([]byte(s.String))
return true
}
@@ -457,7 +455,7 @@ func (p *Parser) skipNL() {
}
func parsePrefixList(seq *V, name string) *V {
- prefix := NewString([]byte(name))
+ prefix := NewString(name)
prefix.Next = seq
return NewList(prefix)
}
@@ -471,7 +469,7 @@ func (p *Parser) parseV() *V {
p.skipNL()
switch {
case p.accept(tString):
- return NewString(p.lexer.buf)
+ return NewString(string(p.lexer.buf))
case p.accept(tAt):
result = p.parseV()
return parsePrefixList(result, "set")
@@ -575,7 +573,7 @@ func scopeFind(scope **V, name string) **V {
}
func scopePrepend(scope **V, name string, v *V) {
- key := NewString([]byte(name))
+ key := NewString(name)
pair := NewList(key)
key.Next = v
@@ -791,7 +789,7 @@ func NewNumber(n float64) *V {
if s[i-1] == '.' {
i--
}
- return NewString([]byte(s[:i]))
+ return NewString(s[:i])
}
// Truthy decides whether any value is logically true.
@@ -802,9 +800,9 @@ func Truthy(v *V) bool {
// NewBoolean creates a new string value copying the boolean's truthiness.
func NewBoolean(b bool) *V {
if b {
- return NewString([]byte("1"))
+ return NewString("1")
}
- return NewString(nil)
+ return NewString("")
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -927,7 +925,7 @@ func fnPrint(ell *Ell, args *V, result **V) bool {
for ; args != nil; args = args.Next {
if args.Type != VTypeString {
PrintV(os.Stdout, args)
- } else if _, err := os.Stdout.Write(args.String); err != nil {
+ } else if _, err := os.Stdout.WriteString(args.String); err != nil {
return ell.Errorf("write failed: %s", err)
}
}
@@ -940,10 +938,10 @@ func fnCat(ell *Ell, args *V, result **V) bool {
if args.Type != VTypeString {
PrintV(buf, args)
} else {
- buf.Write(args.String)
+ buf.WriteString(args.String)
}
}
- *result = NewString(buf.Bytes())
+ *result = NewString(buf.String())
return true
}
@@ -978,7 +976,7 @@ func fnParse(ell *Ell, args *V, result **V) bool {
return ell.Errorf("first argument must be string")
}
- res, err := NewParser(body.String).Run()
+ res, err := NewParser([]byte(body.String)).Run()
if err != nil {
return ell.Errorf("%s", err)
}
@@ -998,7 +996,7 @@ func fnTry(ell *Ell, args *V, result **V) bool {
return true
}
- msg := NewString([]byte(ell.Error))
+ msg := NewString(ell.Error)
ell.Error = ""
*result = nil