25 lines
517 B
Go
25 lines
517 B
Go
|
package octal
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"math"
|
||
|
)
|
||
|
|
||
|
// ParseOctal takes a string representation of an octal number
|
||
|
// and returns the decimal value of that number, or an error if it
|
||
|
// was unparseable as an octal number
|
||
|
func ParseOctal(v string) (int64, error) {
|
||
|
var ret float64
|
||
|
octLen := float64(len(v))
|
||
|
for i := range v {
|
||
|
octLen--
|
||
|
switch {
|
||
|
case v[i] > 47 && v[i] < 56:
|
||
|
ret = ret + float64(v[i]-48)*math.Pow(8, octLen)
|
||
|
default:
|
||
|
return 0, errors.New("Unparseable as octal")
|
||
|
}
|
||
|
}
|
||
|
return int64(ret), nil
|
||
|
}
|