From d378321b6e5e33ed0d5cedf1e80f46c76f7c9185 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Fri, 17 Oct 2008 17:28:09 -0700 Subject: [PATCH] add atol and ltoa. probably want unsigned at some point too. R=rsc DELTA=14 (10 added, 0 deleted, 4 changed) OCL=17387 CL=17390 --- src/lib/strings.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/lib/strings.go b/src/lib/strings.go index 4a2184341f7..b83f2736cf1 100644 --- a/src/lib/strings.go +++ b/src/lib/strings.go @@ -118,7 +118,7 @@ export func join(a *[]string, sep string) string { // Convert decimal string to integer. // TODO: Doesn't check for overflow. -export func atoi(s string) (i int, ok bool) { +export func atol(s string) (i int64, ok bool) { // empty string bad if len(s) == 0 { return 0, false @@ -149,12 +149,12 @@ export func atoi(s string) (i int, ok bool) { } // parse number - n := 0; + n := int64(0); for i := 0; i < len(s); i++ { if s[i] < '0' || s[i] > '9' { return 0, false } - n = n*10 + int(s[i] - '0') + n = n*10 + int64(s[i] - '0') } if neg { n = -n @@ -162,7 +162,13 @@ export func atoi(s string) (i int, ok bool) { return n, true } -export func itoa(i int) string { +export func atoi(s string) (i int, ok bool) { + ii, okok := atoi(s); + i = int32(ii); + return i, okok +} + +export func itol(i int64) string { if i == 0 { return "0" } @@ -189,3 +195,7 @@ export func itoa(i int) string { // BUG return string(b[bp:len(b)]) return string((&b)[bp:len(b)]) } + +export func itoa(i int) string { + return itol(int64(i)); +}