aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbogem <[email protected]>2016-09-10 12:08:47 +0500
committerbogem <[email protected]>2016-09-10 12:08:47 +0500
commitd16d05ecad8d146ef127c975e2e3e167b6e38ad6 (patch)
treea13921d2f956128ed2d1c123d89749db36625645
parent6fd2ff4ff8dfcdf5556fbdc0ac0284408274b1a7 (diff)
Use strconv instead of fmt in values' String funcs
The existing implementation of flag values with fmt package uses more memory and works slower than the implementation with strconv package.
-rw-r--r--bool.go7
-rw-r--r--count.go7
-rw-r--r--float32.go7
-rw-r--r--float64.go7
-rw-r--r--int.go7
-rw-r--r--int32.go7
-rw-r--r--int64.go7
-rw-r--r--int8.go7
-rw-r--r--string.go4
-rw-r--r--uint.go7
-rw-r--r--uint16.go9
-rw-r--r--uint32.go11
-rw-r--r--uint64.go7
-rw-r--r--uint8.go7
14 files changed, 32 insertions, 69 deletions
diff --git a/bool.go b/bool.go
index d272e40..c4c5c0b 100644
--- a/bool.go
+++ b/bool.go
@@ -1,9 +1,6 @@
package pflag
-import (
- "fmt"
- "strconv"
-)
+import "strconv"
// optional interface to indicate boolean flags that can be
// supplied without "=value" text
@@ -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 }
diff --git a/count.go b/count.go
index 7b1f142..d22be41 100644
--- a/count.go
+++ b/count.go
@@ -1,9 +1,6 @@
package pflag
-import (
- "fmt"
- "strconv"
-)
+import "strconv"
// -- count Value
type countValue int
@@ -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)
diff --git a/float32.go b/float32.go
index 7683fae..a243f81 100644
--- a/float32.go
+++ b/float32.go
@@ -1,9 +1,6 @@
package pflag
-import (
- "fmt"
- "strconv"
-)
+import "strconv"
// -- float32 Value
type float32Value float32
@@ -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)
diff --git a/float64.go b/float64.go
index 50fbf8c..04b5492 100644
--- a/float64.go
+++ b/float64.go
@@ -1,9 +1,6 @@
package pflag
-import (
- "fmt"
- "strconv"
-)
+import "strconv"
// -- float64 Value
type float64Value float64
@@ -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)
diff --git a/int.go b/int.go
index b656036..1474b89 100644
--- a/int.go
+++ b/int.go
@@ -1,9 +1,6 @@
package pflag
-import (
- "fmt"
- "strconv"
-)
+import "strconv"
// -- int Value
type intValue int
@@ -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)
diff --git a/int32.go b/int32.go
index 41659a9..9b95944 100644
--- a/int32.go
+++ b/int32.go
@@ -1,9 +1,6 @@
package pflag
-import (
- "fmt"
- "strconv"
-)
+import "strconv"
// -- int32 Value
type int32Value int32
@@ -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)
diff --git a/int64.go b/int64.go
index 6e67e38..0026d78 100644
--- a/int64.go
+++ b/int64.go
@@ -1,9 +1,6 @@
package pflag
-import (
- "fmt"
- "strconv"
-)
+import "strconv"
// -- int64 Value
type int64Value int64
@@ -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)
diff --git a/int8.go b/int8.go
index 400db21..4da9222 100644
--- a/int8.go
+++ b/int8.go
@@ -1,9 +1,6 @@
package pflag
-import (
- "fmt"
- "strconv"
-)
+import "strconv"
// -- int8 Value
type int8Value int8
@@ -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)
diff --git a/string.go b/string.go
index e296136..04e0a26 100644
--- a/string.go
+++ b/string.go
@@ -1,7 +1,5 @@
package pflag
-import "fmt"
-
// -- string Value
type stringValue string
@@ -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
diff --git a/uint.go b/uint.go
index e142b49..dcbc2b7 100644
--- a/uint.go
+++ b/uint.go
@@ -1,9 +1,6 @@
package pflag
-import (
- "fmt"
- "strconv"
-)
+import "strconv"
// -- uint Value
type uintValue uint
@@ -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)
diff --git a/uint16.go b/uint16.go
index 5c96c19..7e9914e 100644
--- a/uint16.go
+++ b/uint16.go
@@ -1,9 +1,6 @@
package pflag
-import (
- "fmt"
- "strconv"
-)
+import "strconv"
// -- uint16 value
type uint16Value uint16
@@ -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)
@@ -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 {
diff --git a/uint32.go b/uint32.go
index 294fcaa..d802453 100644
--- a/uint32.go
+++ b/uint32.go
@@ -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)
@@ -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 {
diff --git a/uint64.go b/uint64.go
index c681885..f62240f 100644
--- a/uint64.go
+++ b/uint64.go
@@ -1,9 +1,6 @@
package pflag
-import (
- "fmt"
- "strconv"
-)
+import "strconv"
// -- uint64 Value
type uint64Value uint64
@@ -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)
diff --git a/uint8.go b/uint8.go
index 26db418..bb0e83c 100644
--- a/uint8.go
+++ b/uint8.go
@@ -1,9 +1,6 @@
package pflag
-import (
- "fmt"
- "strconv"
-)
+import "strconv"
// -- uint8 Value
type uint8Value uint8
@@ -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)