2 Star 4 Fork 10

王布衣/engine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
range_number.go 2.90 KB
一键复制 编辑 原始数据 按行查看 历史
package config
import (
"fmt"
"gitee.com/quant1x/gox/api"
"gitee.com/quant1x/pandas/stat"
"gitee.com/quant1x/pkg/yaml"
"regexp"
"strings"
)
// 值范围正则表达式
var (
numberRangePattern = "[~]\\s*"
numberRangeRegexp = regexp.MustCompile(numberRangePattern)
)
// NumberRange 数值范围
//
// 支持:
// 1) "1~2", 最小值1, 最大值2
// 2) "", 最小值默认, 最大值默认
// 3) "3.82", 最小值, 最大值默认
// 4) "3.82~", 最小值, 最大值默认
// 5) "~3.82", 最小值默认, 最大值
type NumberRange struct {
min float64
max float64
}
func (this NumberRange) String() string {
return fmt.Sprintf("{min: %f, max: %f}", this.min, this.max)
}
func (this *NumberRange) setMinDefault() {
this.min = stat.MinFloat64
}
func (this *NumberRange) setMaxDefault() {
this.max = stat.MaxFloat64
}
func (this *NumberRange) init() {
this.setMinDefault()
this.setMaxDefault()
}
func (this *NumberRange) Max() float64 {
return this.max
}
func (this *NumberRange) Min() float64 {
return this.min
}
func (this *NumberRange) Parse(text string) error {
text = strings.TrimSpace(text)
if len(text) == 0 {
// 如果字符串为空, 设置默认值
this.init()
return ErrRangeFormat
}
arr := numberRangeRegexp.Split(text, -1)
if len(arr) > 2 {
// 如果字符串拆分超过2个元素,格式错误,设置默认值
this.init()
return ErrRangeFormat
}
var begin, end string
switch len(arr) {
case 1: // 如果只有1个值,只是最小
begin = strings.TrimSpace(arr[0])
case 2: // 如果有2个值, 分最大值和最小值
begin = strings.TrimSpace(arr[0])
end = strings.TrimSpace(arr[1])
default:
// 如果字符串拆分超过2个元素,格式错误,设置默认值
// 按说流程是不会走到这里的
this.init()
return ErrRangeFormat
}
// 修订最大最小值
if len(begin) == 0 {
// 如果begin为空, 设置最小默认值
this.setMinDefault()
} else {
this.min = stat.AnyToFloat64(begin)
}
if len(end) == 0 {
// 如果end为空, 设置最大默认值
this.setMaxDefault()
} else {
this.max = stat.AnyToFloat64(end)
}
if this.min > this.max {
this.min, this.max = this.max, this.min
}
return nil
}
func (this NumberRange) MarshalText() (text []byte, err error) {
str := this.String()
return api.String2Bytes(str), nil
}
// UnmarshalYAML YAML自定义解析
func (this *NumberRange) UnmarshalYAML(node *yaml.Node) error {
var value string
if len(node.Content) == 0 {
value = node.Value
} else if len(node.Content) == 2 {
value = node.Content[1].Value
}
return this.Parse(value)
}
// UnmarshalText 设置默认值调用
func (this *NumberRange) UnmarshalText(bytes []byte) error {
text := api.Bytes2String(bytes)
return this.Parse(text)
}
// Validate 验证
func (this *NumberRange) Validate(v float64) bool {
if this.min == 0 && this.max == 0 {
return true
} else if v >= this.min && v < this.max {
return true
}
return false
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/quant1x/engine.git
git@gitee.com:quant1x/engine.git
quant1x
engine
engine
v1.4.3

搜索帮助

Cb406eda 1850385 E526c682 1850385