diff --git a/kernel/av/sort.go b/kernel/av/sort.go index 381a29479..04d79587b 100644 --- a/kernel/av/sort.go +++ b/kernel/av/sort.go @@ -18,7 +18,6 @@ package av import ( "bytes" - "strconv" "strings" "github.com/siyuan-note/siyuan/kernel/util" @@ -196,11 +195,9 @@ func (value *Value) Compare(other *Value, attrView *AttributeView) int { } case KeyTypeTemplate: if nil != value.Template && nil != other.Template { - vContent := strings.TrimSpace(value.Template.Content) - oContent := strings.TrimSpace(other.Template.Content) - if util.IsNumeric(vContent) && util.IsNumeric(oContent) { - v1, _ := strconv.ParseFloat(vContent, 64) - v2, _ := strconv.ParseFloat(oContent, 64) + v1, ok1 := util.Convert2Float(value.Template.Content) + v2, ok2 := util.Convert2Float(other.Template.Content) + if ok1 && ok2 { if v1 > v2 { return 1 } @@ -236,9 +233,9 @@ func (value *Value) Compare(other *Value, attrView *AttributeView) int { } oContent := strings.TrimSpace(oContentBuf.String()) - if util.IsNumeric(vContent) && util.IsNumeric(oContent) { - v1, _ := strconv.ParseFloat(vContent, 64) - v2, _ := strconv.ParseFloat(oContent, 64) + v1, ok1 := util.Convert2Float(value.Template.Content) + v2, ok2 := util.Convert2Float(other.Template.Content) + if ok1 && ok2 { if v1 > v2 { return 1 } @@ -265,9 +262,9 @@ func (value *Value) Compare(other *Value, attrView *AttributeView) int { } oContent := strings.TrimSpace(oContentBuf.String()) - if util.IsNumeric(vContent) && util.IsNumeric(oContent) { - v1, _ := strconv.ParseFloat(vContent, 64) - v2, _ := strconv.ParseFloat(oContent, 64) + v1, ok1 := util.Convert2Float(value.Template.Content) + v2, ok2 := util.Convert2Float(other.Template.Content) + if ok1 && ok2 { if v1 > v2 { return 1 } diff --git a/kernel/av/table.go b/kernel/av/table.go index b4e98258f..ec7264fcb 100644 --- a/kernel/av/table.go +++ b/kernel/av/table.go @@ -19,7 +19,8 @@ package av import ( "math" "sort" - "strconv" + + "github.com/siyuan-note/siyuan/kernel/util" ) // LayoutTable 描述了表格布局的结构。 @@ -413,7 +414,7 @@ func (table *Table) calcColTemplate(col *TableColumn, colIndex int) { sum := 0.0 for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content { - val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64) + val, _ := util.Convert2Float(row.Cells[colIndex].Value.Template.Content) sum += val } } @@ -423,7 +424,7 @@ func (table *Table) calcColTemplate(col *TableColumn, colIndex int) { count := 0 for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content { - val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64) + val, _ := util.Convert2Float(row.Cells[colIndex].Value.Template.Content) sum += val count++ } @@ -435,7 +436,7 @@ func (table *Table) calcColTemplate(col *TableColumn, colIndex int) { values := []float64{} for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content { - val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64) + val, _ := util.Convert2Float(row.Cells[colIndex].Value.Template.Content) values = append(values, val) } } @@ -451,7 +452,7 @@ func (table *Table) calcColTemplate(col *TableColumn, colIndex int) { minVal := math.MaxFloat64 for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content { - val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64) + val, _ := util.Convert2Float(row.Cells[colIndex].Value.Template.Content) if val < minVal { minVal = val } @@ -464,7 +465,7 @@ func (table *Table) calcColTemplate(col *TableColumn, colIndex int) { maxVal := -math.MaxFloat64 for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content { - val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64) + val, _ := util.Convert2Float(row.Cells[colIndex].Value.Template.Content) if val > maxVal { maxVal = val } @@ -478,7 +479,7 @@ func (table *Table) calcColTemplate(col *TableColumn, colIndex int) { maxVal := -math.MaxFloat64 for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content { - val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64) + val, _ := util.Convert2Float(row.Cells[colIndex].Value.Template.Content) if val < minVal { minVal = val } @@ -1634,7 +1635,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) { for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { for _, content := range row.Cells[colIndex].Value.Rollup.Contents { - val, _ := strconv.ParseFloat(content.String(), 64) + val, _ := util.Convert2Float(content.String()) sum += val } } @@ -1646,7 +1647,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) { for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { for _, content := range row.Cells[colIndex].Value.Rollup.Contents { - val, _ := strconv.ParseFloat(content.String(), 64) + val, _ := util.Convert2Float(content.String()) sum += val count++ } @@ -1660,7 +1661,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) { for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { for _, content := range row.Cells[colIndex].Value.Rollup.Contents { - val, _ := strconv.ParseFloat(content.String(), 64) + val, _ := util.Convert2Float(content.String()) values = append(values, val) } } @@ -1678,7 +1679,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) { for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { for _, content := range row.Cells[colIndex].Value.Rollup.Contents { - val, _ := strconv.ParseFloat(content.String(), 64) + val, _ := util.Convert2Float(content.String()) if val < minVal { minVal = val } @@ -1693,7 +1694,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) { for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { for _, content := range row.Cells[colIndex].Value.Rollup.Contents { - val, _ := strconv.ParseFloat(content.String(), 64) + val, _ := util.Convert2Float(content.String()) if val > maxVal { maxVal = val } @@ -1709,7 +1710,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) { for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { for _, content := range row.Cells[colIndex].Value.Rollup.Contents { - val, _ := strconv.ParseFloat(content.String(), 64) + val, _ := util.Convert2Float(content.String()) if val < minVal { minVal = val } diff --git a/kernel/util/misc.go b/kernel/util/misc.go index c339917cf..3a55688c5 100644 --- a/kernel/util/misc.go +++ b/kernel/util/misc.go @@ -24,6 +24,7 @@ import ( "time" "unicode" + "github.com/88250/gulu" "github.com/88250/lute/html" ) @@ -117,9 +118,13 @@ func RemoveRedundantSpace(str string) string { return buf.String() } -func IsNumeric(s string) bool { - _, err := strconv.ParseFloat(s, 64) - return err == nil +func Convert2Float(s string) (float64, bool) { + s = gulu.Str.RemoveInvisible(s) + ret, err := strconv.ParseFloat(strings.TrimSpace(s), 64) + if nil != err { + return 0, false + } + return ret, true } func ContainsSubStr(s string, subStrs []string) bool {