diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-04-04 00:58:09 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-03 12:58:09 -0400 |
commit | 19de52e0f4cbd2d62f9d41589fe8815c2c3ceef2 (patch) | |
tree | f5e2f0a5431f315946aed6d72e40e5e9893dabc0 /modules/util/util.go | |
parent | 01d9466bfdbb2e043a03368ca7872944db211f49 (diff) | |
download | gitea-19de52e0f4cbd2d62f9d41589fe8815c2c3ceef2.tar.gz gitea-19de52e0f4cbd2d62f9d41589fe8815c2c3ceef2.zip |
Introduce GiteaLocaleNumber custom element to handle number localization on pages. (#23861)
Follow #21429 & #22861
Use `<gitea-locale-number>` instead of backend `PrettyNumber`. All old
`PrettyNumber` related functions are removed. A lot of code could be
simplified.
And some functions haven't been used for long time (dead code), so they
are also removed by the way (eg: `SplitStringAtRuneN`, `Dedent`)
This PR only tries to improve the `PrettyNumber` rendering problem, it
doesn't touch the "plural" problem.
Screenshot:
![image](https://user-images.githubusercontent.com/2114189/229290804-1f63db65-1e34-4a54-84ba-e00b44331b17.png)
![image](https://user-images.githubusercontent.com/2114189/229290911-c88dea00-b11d-48dd-accb-9f52edd73ce4.png)
Diffstat (limited to 'modules/util/util.go')
-rw-r--r-- | modules/util/util.go | 58 |
1 files changed, 25 insertions, 33 deletions
diff --git a/modules/util/util.go b/modules/util/util.go index 9d3a8dcfac..9c7097ad34 100644 --- a/modules/util/util.go +++ b/modules/util/util.go @@ -7,8 +7,9 @@ import ( "bytes" "crypto/rand" "errors" + "fmt" "math/big" - "regexp" + "os" "strconv" "strings" @@ -200,40 +201,14 @@ func ToTitleCaseNoLower(s string) string { return titleCaserNoLower.String(s) } -var ( - whitespaceOnly = regexp.MustCompile("(?m)^[ \t]+$") - leadingWhitespace = regexp.MustCompile("(?m)(^[ \t]*)(?:[^ \t\n])") -) - -// Dedent removes common indentation of a multi-line string along with whitespace around it -// Based on https://github.com/lithammer/dedent -func Dedent(s string) string { - var margin string - - s = whitespaceOnly.ReplaceAllString(s, "") - indents := leadingWhitespace.FindAllStringSubmatch(s, -1) - - for i, indent := range indents { - if i == 0 { - margin = indent[1] - } else if strings.HasPrefix(indent[1], margin) { - continue - } else if strings.HasPrefix(margin, indent[1]) { - margin = indent[1] - } else { - margin = "" - break - } - } - - if margin != "" { - s = regexp.MustCompile("(?m)^"+margin).ReplaceAllString(s, "") - } - return strings.TrimSpace(s) +func logError(msg string, args ...any) { + // TODO: the "util" package can not import the "modules/log" package, so we use the "fmt" package here temporarily. + // In the future, we should decouple the dependency between them. + _, _ = fmt.Fprintf(os.Stderr, msg, args...) } -// NumberIntoInt64 transform a given int into int64. -func NumberIntoInt64(number interface{}) int64 { +// ToInt64 transform a given int into int64. +func ToInt64(number interface{}) int64 { var value int64 switch v := number.(type) { case int: @@ -246,6 +221,23 @@ func NumberIntoInt64(number interface{}) int64 { value = int64(v) case int64: value = v + case uint: + value = int64(v) + case uint8: + value = int64(v) + case uint16: + value = int64(v) + case uint32: + value = int64(v) + case uint64: + value = int64(v) + case string: + var err error + if value, err = strconv.ParseInt(v, 10, 64); err != nil { + logError("strconv.ParseInt failed for %q: %v", v, err) + } + default: + logError("unable to convert %q to int64", v) } return value } |