github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/object/reflect/strconv.go (about)

     1  package reflect
     2  
     3  import (
     4  	"github.com/hirochachacha/plua/internal/strconv"
     5  
     6  	"github.com/hirochachacha/plua/object"
     7  )
     8  
     9  func integerToString(i object.Integer) object.String {
    10  	return object.String(strconv.FormatInt(int64(i), 10))
    11  }
    12  
    13  func numberToInteger(n object.Number) (object.Integer, bool) {
    14  	ival := object.Integer(n)
    15  	if n == object.Number(ival) {
    16  		return ival, true
    17  	}
    18  	return ival, false
    19  }
    20  
    21  func numberToString(n object.Number) object.String {
    22  	return object.String(strconv.FormatFloat(float64(n), 'f', 1, 64))
    23  }
    24  
    25  func numberToGoUint(n object.Number) (uint64, bool) {
    26  	u := uint64(n)
    27  	if n == object.Number(u) {
    28  		return u, true
    29  	}
    30  	return u, false
    31  }
    32  
    33  func stringToInteger(s object.String) (object.Integer, bool) {
    34  	i, err := strconv.ParseInt(string(s))
    35  	if err != nil {
    36  		return 0, false
    37  	}
    38  	return object.Integer(i), true
    39  }
    40  
    41  func stringToNumber(s object.String) (object.Number, bool) {
    42  	f, err := strconv.ParseFloat(string(s))
    43  	if err != nil {
    44  		if err == strconv.ErrRange {
    45  			return object.Number(f), true
    46  		}
    47  		return 0, false
    48  	}
    49  	return object.Number(f), true
    50  }
    51  
    52  func stringToGoUint(s object.String) (uint64, bool) {
    53  	u, err := strconv.ParseUint(string(s))
    54  	if err != nil {
    55  		return 0, false
    56  	}
    57  	return u, true
    58  }