@@ -2,14 +2,16 @@ package lib
22
33import (
44 "fmt"
5- "github.com/fatih/color"
6- "github.com/spf13/pflag"
75 "math"
86 "math/big"
97 "net"
108 "regexp"
119 "strconv"
1210 "strings"
11+
12+ "github.com/fatih/color"
13+ "github.com/ipinfo/cli/lib/iputil"
14+ "github.com/spf13/pflag"
1315)
1416
1517// CmdCalcFlags are flags expected by CmdCalc
@@ -105,21 +107,21 @@ func EvaluatePostfix(postfix []string) (*big.Float, error) {
105107 if el == "" {
106108 continue
107109 }
108- if isFloat (el ) || StrIsIPv4Str (el ) || StrIsIPv6Str (el ) {
110+ if isFloat (el ) || iputil . StrIsIPv4Str (el ) || iputil . StrIsIPv6Str (el ) {
109111 postfixStack .Push (el )
110112 continue
111113 }
112114
113115 // if operator pop two elements off of the stack.
114116 strNum1 , isEmpty := postfixStack .Pop ()
115117 if isEmpty {
116- return big .NewFloat (0 ), ErrInvalidInput
118+ return big .NewFloat (0 ), iputil . ErrInvalidInput
117119 }
118120 num1 , _ , _ := big .ParseFloat (strNum1 , 10 , precision , big .ToZero )
119121
120122 strNum2 , isEmpty := postfixStack .Pop ()
121123 if isEmpty {
122- return big .NewFloat (0 ), ErrInvalidInput
124+ return big .NewFloat (0 ), iputil . ErrInvalidInput
123125 }
124126 num2 , _ , _ := big .ParseFloat (strNum2 , 10 , precision , big .ToZero )
125127
@@ -136,7 +138,7 @@ func EvaluatePostfix(postfix []string) (*big.Float, error) {
136138 case operator == "/" :
137139 // Check for division by zero
138140 if num1 .Cmp (big .NewFloat (0 )) == 0 {
139- return big .NewFloat (0 ), ErrInvalidInput
141+ return big .NewFloat (0 ), iputil . ErrInvalidInput
140142 }
141143 result = new (big.Float ).Quo (num2 , num1 )
142144 case operator == "^" :
@@ -148,7 +150,7 @@ func EvaluatePostfix(postfix []string) (*big.Float, error) {
148150 res := math .Pow (num2F64 , num1F64 )
149151 result = new (big.Float ).SetPrec (precision ).SetFloat64 (res )
150152 default :
151- return big .NewFloat (0 ), ErrInvalidInput
153+ return big .NewFloat (0 ), iputil . ErrInvalidInput
152154 }
153155 strResult := result .Text ('f' , 50 )
154156 postfixStack .Push (strResult )
@@ -175,19 +177,19 @@ func translateToken(tempToken string, tokens []string) ([]string, error) {
175177
176178 if isFloat (tempToken ) {
177179 tokens = append (tokens , tempToken )
178- } else if StrIsIPv4Str (tempToken ) {
180+ } else if iputil . StrIsIPv4Str (tempToken ) {
179181 // Convert ipv4 to decimal then append to tokens
180182 ip := net .ParseIP (tempToken )
181- decimalIP := IP4toInt (ip )
183+ decimalIP := iputil . IP4toInt (ip )
182184 res := strconv .FormatInt (decimalIP , 10 )
183185 tokens = append (tokens , res )
184186
185- } else if StrIsIPv6Str (tempToken ) {
187+ } else if iputil . StrIsIPv6Str (tempToken ) {
186188 ip := net .ParseIP (tempToken )
187- decimalIP := IP6toInt (ip )
189+ decimalIP := iputil . IP6toInt (ip )
188190 tokens = append (tokens , decimalIP .String ())
189191 } else {
190- return []string {}, ErrInvalidInput
192+ return []string {}, iputil . ErrInvalidInput
191193 }
192194 return tokens , nil
193195}
@@ -324,7 +326,7 @@ func CmdCalc(f CmdCalcFlags, args []string, printHelp func()) error {
324326
325327 infix := args [0 ]
326328 if IsInvalidInfix (infix ) {
327- return ErrInvalidInput
329+ return iputil . ErrInvalidInput
328330 }
329331
330332 tokens , err := TokenizeInfix (infix )
0 commit comments