Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/humanize.Comma/Commaf #50

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions http/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net"
"net/http"
"time"

"github.com/qiniu/x/humanize"
)

// -------------------------------------------------------------------------------
Expand Down Expand Up @@ -41,15 +43,15 @@ func Tee(a, b http.ResponseWriter) http.ResponseWriter {

type CodeRecorder struct {
Code int
Bytes int
Bytes int64
}

func (p *CodeRecorder) Header() http.Header {
return nil
}

func (p *CodeRecorder) Write(buf []byte) (n int, err error) {
p.Bytes += len(buf)
p.Bytes += int64(len(buf))
return
}

Expand All @@ -71,7 +73,8 @@ func New(h http.Handler) http.Handler {
start := time.Now()
h.ServeHTTP(tee, r)
dur := time.Since(start)
log.Printf("Returned %d of %s %v with %d bytes in %d ms\n", recorder.Code, r.Method, r.URL, recorder.Bytes, dur.Milliseconds())
bytes := humanize.Comma(recorder.Bytes)
log.Printf("Returned %d of %s %v with %s bytes in %d ms\n", recorder.Code, r.Method, r.URL, bytes, dur.Milliseconds())
})
}

Expand Down
94 changes: 94 additions & 0 deletions humanize/comma.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package humanize

import (
"bytes"
"math"
"strconv"
"strings"
)

// Comma produces a string form of the given number in base 10 with
// commas after every three orders of magnitude.
//
// e.g. Comma(834142) -> 834,142
func Comma(v int64) string {
sign := ""

// Min int64 can't be negated to a usable value, so it has to be special cased.
if v == math.MinInt64 {
return "-9,223,372,036,854,775,808"
}

if v < 0 {
sign = "-"
v = 0 - v
}

parts := []string{"", "", "", "", "", "", ""}
j := len(parts) - 1

for v > 999 {
parts[j] = strconv.FormatInt(v%1000, 10)
switch len(parts[j]) {
case 2:
parts[j] = "0" + parts[j]
case 1:
parts[j] = "00" + parts[j]
}
v = v / 1000
j--
}
parts[j] = strconv.Itoa(int(v))
return sign + strings.Join(parts[j:], ",")
}

// Commaf produces a string form of the given number in base 10 with
// commas after every three orders of magnitude.
//
// e.g. Commaf(834142.32) -> 834,142.32
func Commaf(v float64, decimals ...int) string {
buf := &bytes.Buffer{}
if v < 0 {
buf.Write([]byte{'-'})
v = 0 - v
}

comma := []byte{','}

parts := strings.Split(strconv.FormatFloat(v, 'f', -1, 64), ".")
pos := 0
if len(parts[0])%3 != 0 {
pos += len(parts[0]) % 3
buf.WriteString(parts[0][:pos])
buf.Write(comma)
}
for ; pos < len(parts[0]); pos += 3 {
buf.WriteString(parts[0][pos : pos+3])
buf.Write(comma)
}
buf.Truncate(buf.Len() - 1)

if len(parts) > 1 {
buf.Write([]byte{'.'})
buf.WriteString(parts[1])
}
ret := buf.String()
if decimals != nil {
ret = stripTrailingDigits(ret, decimals[0])
}
return ret
}

func stripTrailingDigits(s string, digits int) string {
if i := strings.Index(s, "."); i >= 0 {
if digits <= 0 {
return s[:i]
}
i++
if i+digits >= len(s) {
return s
}
return s[:i+digits]
}
return s
}