github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/os/str.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Simple conversions to avoid depending on strconv.
     6  
     7  package os
     8  
     9  // Convert integer to decimal string
    10  func itoa(val int) string {
    11  	if val < 0 {
    12  		return "-" + uitoa(uint(-val))
    13  	}
    14  	return uitoa(uint(val))
    15  }
    16  
    17  // Convert unsigned integer to decimal string
    18  func uitoa(val uint) string {
    19  	if val == 0 { // avoid string allocation
    20  		return "0"
    21  	}
    22  	var buf [20]byte // big enough for 64bit value base 10
    23  	i := len(buf) - 1
    24  	for val >= 10 {
    25  		q := val / 10
    26  		buf[i] = byte('0' + val - q*10)
    27  		i--
    28  		val = q
    29  	}
    30  	// val < 10
    31  	buf[i] = byte('0' + val)
    32  	return string(buf[i:])
    33  }