- make master compilable.

This commit is contained in:
gazebo
2018-08-30 00:32:57 +08:00
parent f7f536078a
commit 3538371333
3 changed files with 149 additions and 31 deletions

View File

@@ -4,9 +4,11 @@ import (
"fmt"
"math"
"math/rand"
"regexp"
"strings"
"sync"
"time"
"unicode/utf8"
"git.rosy.net.cn/baseapi/platformapi/autonavi"
"git.rosy.net.cn/baseapi/utils"
@@ -17,6 +19,7 @@ import (
var (
routinePool *routinepool.Pool
skuNamePat *regexp.Regexp
)
type SyncMapWithTimeout struct {
@@ -27,6 +30,10 @@ type SyncMapWithTimeout struct {
func init() {
rand.Seed(time.Now().Unix())
routinePool = routinepool.New(1000, 1000)
// Go regex does not support lookarounds.
// https://stackoverflow.com/questions/38933898/error-parsing-regexp-invalid-or-unsupported-perl-syntax
skuNamePat = regexp.MustCompile(`([\(\[【][^\(\[【\)\]】]*[\)\]】])?(.*?)([(].*[)])?\s*约?([1-9][\d\.]*)(g|G|kg|kG|Kg|KG|l|L|ml|mL|Ml|ML|克)\s*([(].*[)])?\s*(?:\/||)\s*([^\s()]{0,2})\s*([(].*[)])?$`)
}
func (m *SyncMapWithTimeout) StoreWithTimeout(key, value interface{}, timeout time.Duration) {
@@ -191,3 +198,73 @@ func Errs2Str(sep string, errs ...error) (retVal string) {
}
return retVal
}
func IntWeight2Float(weight int) float32 {
return float32(weight) / 1000.0
}
func FloatWeight2Int(weight float32) int {
return int(math.Round(float64(weight * 1000)))
}
func ComposeSkuName(prefix, name, comment, unit string, spec_quality float32, spec_unit string, maxLen int) (skuName string) {
if prefix != "" {
skuName = "[" + prefix + "]"
}
skuName += name
if unit == "份" {
skuName += "约"
}
if math.Round(float64(spec_quality)) == float64(spec_quality) || (spec_unit != "L" && spec_unit != "kg") {
skuName += fmt.Sprintf("%d", int(spec_quality))
} else {
skuName += fmt.Sprintf("%.2f", spec_quality)
}
skuName += spec_unit
skuName += "/" + unit
if maxLen > 0 {
skuName = skuName[:maxLen]
}
return skuName
}
// 1商品特殊前缀
// 2商品名字
// 3商品说明1可缺失
// 4质量数字
// 5质量单位
// 6商品说明2可缺失
// 7商品单位
// 8商品说明3可缺失
func SplitSkuName(skuName string) (prefix, name, comment, specUnit, unit string, specQuality float32) {
searchResult := skuNamePat.FindStringSubmatch(skuName)
if searchResult != nil {
if searchResult[3] != "" {
comment = searchResult[3]
} else if searchResult[6] != "" {
comment = searchResult[6]
} else if searchResult[8] != "" {
comment = searchResult[8]
}
comment = strings.Trim(comment, " ()")
name = strings.Trim(searchResult[2], " ")
if comment != "" {
if utf8.RuneCountInString(comment) <= 5 {
name += "-" + comment
comment = ""
}
}
specUnit = strings.ToLower(strings.Replace(searchResult[5], "克", "g", -1))
if specUnit == "l" {
specUnit = "L"
}
if searchResult[7] == "" {
unit = "份"
} else {
unit = searchResult[7]
}
specQuality = float32(utils.Str2Float64(searchResult[4]))
prefix = strings.Trim(searchResult[1], " []()【】()")
}
return prefix, name, comment, specUnit, unit, specQuality
}