github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/convert/string-to-integer.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package convert
     4  
     5  import "../protocol"
     6  
     7  const (
     8  	maxUint8Value  uint8  = 255
     9  	maxUint16Value uint16 = 65535
    10  	maxUint32Value uint32 = 4294967295
    11  )
    12  
    13  // StringToUint8Base10 Parse given string and returns uint8
    14  func StringToUint8Base10(str string) (num uint8, err protocol.Error) {
    15  	if str == "" {
    16  		err = ErrEmptyValue
    17  		return
    18  	}
    19  	var stringLen = len(str)
    20  	if stringLen > 3 {
    21  		err = ErrValueOutOfRange
    22  		return
    23  	}
    24  
    25  	var n uint16
    26  	for i := 0; i < stringLen; i++ {
    27  		var char = str[i]
    28  		var numSegment byte
    29  		switch {
    30  		case '0' <= char && char <= '9':
    31  			numSegment = char - '0'
    32  		default:
    33  			return 0, ErrBadValue
    34  		}
    35  		n *= 10
    36  		n += uint16(numSegment)
    37  		if n > uint16(maxUint8Value) {
    38  			return 0, ErrValueOutOfRange
    39  		}
    40  	}
    41  	return uint8(n), nil
    42  }
    43  
    44  // StringToUint16Base10 Parse given string and returns uint16
    45  func StringToUint16Base10(str string) (num uint16, err protocol.Error) {
    46  	if str == "" {
    47  		return 0, ErrEmptyValue
    48  	}
    49  	var stringLen = len(str)
    50  	if stringLen > 5 {
    51  		err = ErrValueOutOfRange
    52  		return
    53  	}
    54  
    55  	var n uint32
    56  	for i := 0; i < stringLen; i++ {
    57  		var char = str[i]
    58  		var numSegment byte
    59  		switch {
    60  		case '0' <= char && char <= '9':
    61  			numSegment = char - '0'
    62  		default:
    63  			return 0, ErrBadValue
    64  		}
    65  		n *= 10
    66  		n += uint32(numSegment)
    67  		if n > uint32(maxUint16Value) {
    68  			return 0, ErrValueOutOfRange
    69  		}
    70  	}
    71  	return uint16(n), nil
    72  }
    73  
    74  // StringToUint32Base10 Parse given string and returns uint32
    75  func StringToUint32Base10(str string) (num uint32, err protocol.Error) {
    76  	if str == "" {
    77  		return 0, ErrEmptyValue
    78  	}
    79  	var stringLen = len(str)
    80  	if stringLen > 10 {
    81  		err = ErrValueOutOfRange
    82  		return
    83  	}
    84  
    85  	var n uint64
    86  	for i := 0; i < stringLen; i++ {
    87  		var char = str[i]
    88  		var numSegment byte
    89  		switch {
    90  		case '0' <= char && char <= '9':
    91  			numSegment = char - '0'
    92  		default:
    93  			return 0, ErrBadValue
    94  		}
    95  		n *= 10
    96  		n += uint64(numSegment)
    97  		if n > uint64(maxUint32Value) {
    98  			return 0, ErrValueOutOfRange
    99  		}
   100  	}
   101  	return uint32(n), nil
   102  }
   103  
   104  // StringToUint64Base10 Parse given string and returns uint64
   105  func StringToUint64Base10(str string) (num uint64, err protocol.Error) {
   106  	if str == "" {
   107  		return 0, ErrEmptyValue
   108  	}
   109  	var stringLen = len(str)
   110  	if stringLen > 20 {
   111  		err = ErrValueOutOfRange
   112  		return
   113  	}
   114  
   115  	for i := 0; i < stringLen; i++ {
   116  		var char = str[i]
   117  		var numSegment byte
   118  		switch {
   119  		case '0' <= char && char <= '9':
   120  			numSegment = char - '0'
   121  		default:
   122  			return 0, ErrBadValue
   123  		}
   124  		var n1 = num * 10
   125  		if n1 < num {
   126  			return 0, ErrValueOutOfRange
   127  		}
   128  		num = n1 + uint64(numSegment)
   129  	}
   130  	return
   131  }