Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
up: math - update the value compare op chars
- Loading branch information
Showing
7 changed files
with
198 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package mathutil | ||
|
||
// Compare intX,floatX value by given op. returns `srcVal op(=,!=,<,<=,>,>=) dstVal` | ||
// | ||
// Usage: | ||
// | ||
// mathutil.Compare(2, 3, ">") // false | ||
// mathutil.Compare(2, 1.3, ">") // true | ||
// mathutil.Compare(2.2, 1.3, ">") // true | ||
// mathutil.Compare(2.1, 2, ">") // true | ||
func Compare(srcVal, dstVal interface{}, op string) (ok bool) { | ||
if srcVal == nil || dstVal == nil { | ||
return false | ||
} | ||
|
||
// float | ||
if srcFlt, ok := srcVal.(float64); ok { | ||
if dstFlt, err := ToFloat(dstVal); err == nil { | ||
return CompFloat(srcFlt, dstFlt, op) | ||
} | ||
return false | ||
} | ||
|
||
if srcFlt, ok := srcVal.(float32); ok { | ||
if dstFlt, err := ToFloat(dstVal); err == nil { | ||
return CompFloat(float64(srcFlt), dstFlt, op) | ||
} | ||
return false | ||
} | ||
|
||
// as int64 | ||
srcInt, err := ToInt64(srcVal) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
dstInt, err := ToInt64(dstVal) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return CompInt64(srcInt, dstInt, op) | ||
} | ||
|
||
// CompInt64 compare int64, returns the srcI64 op dstI64 | ||
func CompInt64(srcI64, dstI64 int64, op string) (ok bool) { | ||
switch op { | ||
case "<", "lt": | ||
ok = srcI64 < dstI64 | ||
case "<=", "lte": | ||
ok = srcI64 <= dstI64 | ||
case ">", "gt": | ||
ok = srcI64 > dstI64 | ||
case ">=", "gte": | ||
ok = srcI64 >= dstI64 | ||
case "=", "eq": | ||
ok = srcI64 == dstI64 | ||
case "!=", "ne", "neq": | ||
ok = srcI64 != dstI64 | ||
} | ||
return | ||
} | ||
|
||
// CompFloat compare float64 | ||
func CompFloat(srcF64, dstF64 float64, op string) (ok bool) { | ||
switch op { | ||
case "<", "lt": | ||
ok = srcF64 < dstF64 | ||
case "<=", "lte": | ||
ok = srcF64 <= dstF64 | ||
case ">", "gt": | ||
ok = srcF64 > dstF64 | ||
case ">=", "gte": | ||
ok = srcF64 >= dstF64 | ||
case "=", "eq": | ||
ok = srcF64 == dstF64 | ||
case "!=", "ne", "neq": | ||
ok = srcF64 != dstF64 | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package mathutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gookit/goutil/comdef" | ||
"github.com/gookit/goutil/mathutil" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestCompare(t *testing.T) { | ||
tests := []struct { | ||
x, y interface{} | ||
op string | ||
}{ | ||
{2, 2, comdef.OpEq}, | ||
{2, 3, comdef.OpNeq}, | ||
{2, 3, comdef.OpLt}, | ||
{2, 3, comdef.OpLte}, | ||
{2, 2, comdef.OpLte}, | ||
{2, 1, comdef.OpGt}, | ||
{2, 2, comdef.OpGte}, | ||
{2, 1, comdef.OpGte}, | ||
{2.2, 2.2, comdef.OpEq}, | ||
{2.2, 3.1, comdef.OpNeq}, | ||
{2.3, 3.2, comdef.OpLt}, | ||
{2.3, 3.3, comdef.OpLte}, | ||
{2.3, 2.3, comdef.OpLte}, | ||
{2.3, 1.3, comdef.OpGt}, | ||
{2.3, 2.3, comdef.OpGte}, | ||
{2.3, 1.3, comdef.OpGte}, | ||
} | ||
|
||
for _, test := range tests { | ||
assert.True(t, mathutil.Compare(test.x, test.y, test.op)) | ||
} | ||
|
||
assert.False(t, mathutil.Compare(2, 3, comdef.OpGt)) | ||
assert.False(t, mathutil.Compare(nil, 3, comdef.OpGt)) | ||
assert.False(t, mathutil.Compare(2, nil, comdef.OpGt)) | ||
assert.False(t, mathutil.Compare("abc", 3, comdef.OpGt)) | ||
assert.False(t, mathutil.Compare(2, "def", comdef.OpGt)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Package tscanner text-scanner | ||
package tscanner | ||
|
||
import ( | ||
"io" | ||
"text/scanner" | ||
) | ||
|
||
const ( | ||
TokInvalid = iota | ||
TokComments | ||
TokILComments | ||
TokMLComments | ||
TokValue | ||
TokMLValue | ||
) | ||
|
||
const ( | ||
Value = iota | ||
MValue | ||
) | ||
|
||
type tokenItem struct { | ||
// see TokValueLine | ||
kind rune | ||
// key string. eg: top.sub.some-key | ||
key string | ||
|
||
// token value | ||
value string | ||
// for multi line value. | ||
values []string | ||
// for multi line comments. | ||
comments []string | ||
} | ||
|
||
func newTokenItem(key, value string, kind rune) *tokenItem { | ||
tk := &tokenItem{ | ||
key: key, | ||
kind: kind, | ||
value: value, | ||
} | ||
|
||
return tk | ||
} | ||
|
||
// Valid of the token data. | ||
func (ti *tokenItem) addValue(val string) { | ||
ti.values = append(ti.values, val) | ||
} | ||
|
||
const bufLen = 1024 // at least utf8.UTFMax | ||
|
||
// TextScanner struct. | ||
// refer text/scanner.Scanner | ||
type TextScanner struct { | ||
scanner.Position | ||
// Input | ||
src io.Reader | ||
|
||
// k-v split char, default is "=" | ||
kvSep string | ||
} | ||
|
||
func (s TextScanner) Next() *tokenItem { | ||
|
||
} |