Skip to content

Commit

Permalink
Merge pull request #50 from xushiwei/q
Browse files Browse the repository at this point in the history
x/humanize.Comma/Commaf
  • Loading branch information
xushiwei authored Sep 1, 2023
2 parents 5ae40ee + e86531b commit 371356f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
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
}

0 comments on commit 371356f

Please sign in to comment.