Convert an integer to a fixed-length string, where the width of the string should be greater than 0 Ensure that the buffer has sufficient capacity (将一个整形转换成一个固定长度的字符串,字符串宽度应该是大于0的 要确保buffer是有容量空间的)
(buf *bytes.Buffer, i int, wID int)
| 431 | // (将一个整形转换成一个固定长度的字符串,字符串宽度应该是大于0的 |
| 432 | // 要确保buffer是有容量空间的) |
| 433 | func itoa(buf *bytes.Buffer, i int, wID int) { |
| 434 | var u uint = uint(i) |
| 435 | if u == 0 && wID <= 1 { |
| 436 | buf.WriteByte('0') |
| 437 | return |
| 438 | } |
| 439 | |
| 440 | // Assemble decimal in reverse order. |
| 441 | var b [32]byte |
| 442 | bp := len(b) |
| 443 | for ; u > 0 || wID > 0; u /= 10 { |
| 444 | bp-- |
| 445 | wID-- |
| 446 | b[bp] = byte(u%10) + '0' |
| 447 | } |
| 448 | |
| 449 | // avoID slicing b to avoID an allocation. |
| 450 | for bp < len(b) { |
| 451 | buf.WriteByte(b[bp]) |
| 452 | bp++ |
| 453 | } |
| 454 | } |