Skip to content

Commit

Permalink
Use strconv instead of fmt in values' String funcs
Browse files Browse the repository at this point in the history
The existing implementation of flag values with fmt package uses
more memory and works slower than the implementation with strconv
package.
  • Loading branch information
n10v committed Sep 10, 2016
1 parent 6fd2ff4 commit d16d05e
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 69 deletions.
7 changes: 2 additions & 5 deletions bool.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pflag

import (
"fmt"
"strconv"
)
import "strconv"

// optional interface to indicate boolean flags that can be
// supplied without "=value" text
Expand All @@ -30,7 +27,7 @@ func (b *boolValue) Type() string {
return "bool"
}

func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) }
func (b *boolValue) String() string { return strconv.FormatBool(bool(*b)) }

func (b *boolValue) IsBoolFlag() bool { return true }

Expand Down
7 changes: 2 additions & 5 deletions count.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pflag

import (
"fmt"
"strconv"
)
import "strconv"

// -- count Value
type countValue int
Expand All @@ -28,7 +25,7 @@ func (i *countValue) Type() string {
return "count"
}

func (i *countValue) String() string { return fmt.Sprintf("%v", *i) }
func (i *countValue) String() string { return strconv.Itoa(int(*i)) }

func countConv(sval string) (interface{}, error) {
i, err := strconv.Atoi(sval)
Expand Down
7 changes: 2 additions & 5 deletions float32.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pflag

import (
"fmt"
"strconv"
)
import "strconv"

// -- float32 Value
type float32Value float32
Expand All @@ -23,7 +20,7 @@ func (f *float32Value) Type() string {
return "float32"
}

func (f *float32Value) String() string { return fmt.Sprintf("%v", *f) }
func (f *float32Value) String() string { return strconv.FormatFloat(float64(*f), 'g', -1, 32) }

func float32Conv(sval string) (interface{}, error) {
v, err := strconv.ParseFloat(sval, 32)
Expand Down
7 changes: 2 additions & 5 deletions float64.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pflag

import (
"fmt"
"strconv"
)
import "strconv"

// -- float64 Value
type float64Value float64
Expand All @@ -23,7 +20,7 @@ func (f *float64Value) Type() string {
return "float64"
}

func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) }
func (f *float64Value) String() string { return strconv.FormatFloat(float64(*f), 'g', -1, 64) }

func float64Conv(sval string) (interface{}, error) {
return strconv.ParseFloat(sval, 64)
Expand Down
7 changes: 2 additions & 5 deletions int.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pflag

import (
"fmt"
"strconv"
)
import "strconv"

// -- int Value
type intValue int
Expand All @@ -23,7 +20,7 @@ func (i *intValue) Type() string {
return "int"
}

func (i *intValue) String() string { return fmt.Sprintf("%v", *i) }
func (i *intValue) String() string { return strconv.Itoa(int(*i)) }

func intConv(sval string) (interface{}, error) {
return strconv.Atoi(sval)
Expand Down
7 changes: 2 additions & 5 deletions int32.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pflag

import (
"fmt"
"strconv"
)
import "strconv"

// -- int32 Value
type int32Value int32
Expand All @@ -23,7 +20,7 @@ func (i *int32Value) Type() string {
return "int32"
}

func (i *int32Value) String() string { return fmt.Sprintf("%v", *i) }
func (i *int32Value) String() string { return strconv.FormatInt(int64(*i), 10) }

func int32Conv(sval string) (interface{}, error) {
v, err := strconv.ParseInt(sval, 0, 32)
Expand Down
7 changes: 2 additions & 5 deletions int64.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pflag

import (
"fmt"
"strconv"
)
import "strconv"

// -- int64 Value
type int64Value int64
Expand All @@ -23,7 +20,7 @@ func (i *int64Value) Type() string {
return "int64"
}

func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) }
func (i *int64Value) String() string { return strconv.FormatInt(int64(*i), 10) }

func int64Conv(sval string) (interface{}, error) {
return strconv.ParseInt(sval, 0, 64)
Expand Down
7 changes: 2 additions & 5 deletions int8.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pflag

import (
"fmt"
"strconv"
)
import "strconv"

// -- int8 Value
type int8Value int8
Expand All @@ -23,7 +20,7 @@ func (i *int8Value) Type() string {
return "int8"
}

func (i *int8Value) String() string { return fmt.Sprintf("%v", *i) }
func (i *int8Value) String() string { return strconv.FormatInt(int64(*i), 10) }

func int8Conv(sval string) (interface{}, error) {
v, err := strconv.ParseInt(sval, 0, 8)
Expand Down
4 changes: 1 addition & 3 deletions string.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package pflag

import "fmt"

// -- string Value
type stringValue string

Expand All @@ -18,7 +16,7 @@ func (s *stringValue) Type() string {
return "string"
}

func (s *stringValue) String() string { return fmt.Sprintf("%s", *s) }
func (s *stringValue) String() string { return string(*s) }

func stringConv(sval string) (interface{}, error) {
return sval, nil
Expand Down
7 changes: 2 additions & 5 deletions uint.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pflag

import (
"fmt"
"strconv"
)
import "strconv"

// -- uint Value
type uintValue uint
Expand All @@ -23,7 +20,7 @@ func (i *uintValue) Type() string {
return "uint"
}

func (i *uintValue) String() string { return fmt.Sprintf("%v", *i) }
func (i *uintValue) String() string { return strconv.FormatUint(uint64(*i), 10) }

func uintConv(sval string) (interface{}, error) {
v, err := strconv.ParseUint(sval, 0, 0)
Expand Down
9 changes: 4 additions & 5 deletions uint16.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pflag

import (
"fmt"
"strconv"
)
import "strconv"

// -- uint16 value
type uint16Value uint16
Expand All @@ -12,7 +9,7 @@ func newUint16Value(val uint16, p *uint16) *uint16Value {
*p = val
return (*uint16Value)(p)
}
func (i *uint16Value) String() string { return fmt.Sprintf("%d", *i) }

func (i *uint16Value) Set(s string) error {
v, err := strconv.ParseUint(s, 0, 16)
*i = uint16Value(v)
Expand All @@ -23,6 +20,8 @@ func (i *uint16Value) Type() string {
return "uint16"
}

func (i *uint16Value) String() string { return strconv.FormatUint(uint64(*i), 10) }

func uint16Conv(sval string) (interface{}, error) {
v, err := strconv.ParseUint(sval, 0, 16)
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions uint32.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package pflag

import (
"fmt"
"strconv"
)
import "strconv"

// -- uint16 value
// -- uint32 value
type uint32Value uint32

func newUint32Value(val uint32, p *uint32) *uint32Value {
*p = val
return (*uint32Value)(p)
}
func (i *uint32Value) String() string { return fmt.Sprintf("%d", *i) }

func (i *uint32Value) Set(s string) error {
v, err := strconv.ParseUint(s, 0, 32)
*i = uint32Value(v)
Expand All @@ -23,6 +20,8 @@ func (i *uint32Value) Type() string {
return "uint32"
}

func (i *uint32Value) String() string { return strconv.FormatUint(uint64(*i), 10) }

func uint32Conv(sval string) (interface{}, error) {
v, err := strconv.ParseUint(sval, 0, 32)
if err != nil {
Expand Down
7 changes: 2 additions & 5 deletions uint64.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pflag

import (
"fmt"
"strconv"
)
import "strconv"

// -- uint64 Value
type uint64Value uint64
Expand All @@ -23,7 +20,7 @@ func (i *uint64Value) Type() string {
return "uint64"
}

func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i) }
func (i *uint64Value) String() string { return strconv.FormatUint(uint64(*i), 10) }

func uint64Conv(sval string) (interface{}, error) {
v, err := strconv.ParseUint(sval, 0, 64)
Expand Down
7 changes: 2 additions & 5 deletions uint8.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pflag

import (
"fmt"
"strconv"
)
import "strconv"

// -- uint8 Value
type uint8Value uint8
Expand All @@ -23,7 +20,7 @@ func (i *uint8Value) Type() string {
return "uint8"
}

func (i *uint8Value) String() string { return fmt.Sprintf("%v", *i) }
func (i *uint8Value) String() string { return strconv.FormatUint(uint64(*i), 10) }

func uint8Conv(sval string) (interface{}, error) {
v, err := strconv.ParseUint(sval, 0, 8)
Expand Down

0 comments on commit d16d05e

Please sign in to comment.