...

Source file src/github.com/json-iterator/go/iter_int.go

Documentation: github.com/json-iterator/go

     1  package jsoniter
     2  
     3  import (
     4  	"math"
     5  	"strconv"
     6  )
     7  
     8  var intDigits []int8
     9  
    10  const uint32SafeToMultiply10 = uint32(0xffffffff)/10 - 1
    11  const uint64SafeToMultiple10 = uint64(0xffffffffffffffff)/10 - 1
    12  const maxFloat64 = 1<<53 - 1
    13  
    14  func init() {
    15  	intDigits = make([]int8, 256)
    16  	for i := 0; i < len(intDigits); i++ {
    17  		intDigits[i] = invalidCharForNumber
    18  	}
    19  	for i := int8('0'); i <= int8('9'); i++ {
    20  		intDigits[i] = i - int8('0')
    21  	}
    22  }
    23  
    24  // ReadUint read uint
    25  func (iter *Iterator) ReadUint() uint {
    26  	if strconv.IntSize == 32 {
    27  		return uint(iter.ReadUint32())
    28  	}
    29  	return uint(iter.ReadUint64())
    30  }
    31  
    32  // ReadInt read int
    33  func (iter *Iterator) ReadInt() int {
    34  	if strconv.IntSize == 32 {
    35  		return int(iter.ReadInt32())
    36  	}
    37  	return int(iter.ReadInt64())
    38  }
    39  
    40  // ReadInt8 read int8
    41  func (iter *Iterator) ReadInt8() (ret int8) {
    42  	c := iter.nextToken()
    43  	if c == '-' {
    44  		val := iter.readUint32(iter.readByte())
    45  		if val > math.MaxInt8+1 {
    46  			iter.ReportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
    47  			return
    48  		}
    49  		return -int8(val)
    50  	}
    51  	val := iter.readUint32(c)
    52  	if val > math.MaxInt8 {
    53  		iter.ReportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
    54  		return
    55  	}
    56  	return int8(val)
    57  }
    58  
    59  // ReadUint8 read uint8
    60  func (iter *Iterator) ReadUint8() (ret uint8) {
    61  	val := iter.readUint32(iter.nextToken())
    62  	if val > math.MaxUint8 {
    63  		iter.ReportError("ReadUint8", "overflow: "+strconv.FormatInt(int64(val), 10))
    64  		return
    65  	}
    66  	return uint8(val)
    67  }
    68  
    69  // ReadInt16 read int16
    70  func (iter *Iterator) ReadInt16() (ret int16) {
    71  	c := iter.nextToken()
    72  	if c == '-' {
    73  		val := iter.readUint32(iter.readByte())
    74  		if val > math.MaxInt16+1 {
    75  			iter.ReportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
    76  			return
    77  		}
    78  		return -int16(val)
    79  	}
    80  	val := iter.readUint32(c)
    81  	if val > math.MaxInt16 {
    82  		iter.ReportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
    83  		return
    84  	}
    85  	return int16(val)
    86  }
    87  
    88  // ReadUint16 read uint16
    89  func (iter *Iterator) ReadUint16() (ret uint16) {
    90  	val := iter.readUint32(iter.nextToken())
    91  	if val > math.MaxUint16 {
    92  		iter.ReportError("ReadUint16", "overflow: "+strconv.FormatInt(int64(val), 10))
    93  		return
    94  	}
    95  	return uint16(val)
    96  }
    97  
    98  // ReadInt32 read int32
    99  func (iter *Iterator) ReadInt32() (ret int32) {
   100  	c := iter.nextToken()
   101  	if c == '-' {
   102  		val := iter.readUint32(iter.readByte())
   103  		if val > math.MaxInt32+1 {
   104  			iter.ReportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
   105  			return
   106  		}
   107  		return -int32(val)
   108  	}
   109  	val := iter.readUint32(c)
   110  	if val > math.MaxInt32 {
   111  		iter.ReportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
   112  		return
   113  	}
   114  	return int32(val)
   115  }
   116  
   117  // ReadUint32 read uint32
   118  func (iter *Iterator) ReadUint32() (ret uint32) {
   119  	return iter.readUint32(iter.nextToken())
   120  }
   121  
   122  func (iter *Iterator) readUint32(c byte) (ret uint32) {
   123  	ind := intDigits[c]
   124  	if ind == 0 {
   125  		iter.assertInteger()
   126  		return 0 // single zero
   127  	}
   128  	if ind == invalidCharForNumber {
   129  		iter.ReportError("readUint32", "unexpected character: "+string([]byte{byte(ind)}))
   130  		return
   131  	}
   132  	value := uint32(ind)
   133  	if iter.tail-iter.head > 10 {
   134  		i := iter.head
   135  		ind2 := intDigits[iter.buf[i]]
   136  		if ind2 == invalidCharForNumber {
   137  			iter.head = i
   138  			iter.assertInteger()
   139  			return value
   140  		}
   141  		i++
   142  		ind3 := intDigits[iter.buf[i]]
   143  		if ind3 == invalidCharForNumber {
   144  			iter.head = i
   145  			iter.assertInteger()
   146  			return value*10 + uint32(ind2)
   147  		}
   148  		//iter.head = i + 1
   149  		//value = value * 100 + uint32(ind2) * 10 + uint32(ind3)
   150  		i++
   151  		ind4 := intDigits[iter.buf[i]]
   152  		if ind4 == invalidCharForNumber {
   153  			iter.head = i
   154  			iter.assertInteger()
   155  			return value*100 + uint32(ind2)*10 + uint32(ind3)
   156  		}
   157  		i++
   158  		ind5 := intDigits[iter.buf[i]]
   159  		if ind5 == invalidCharForNumber {
   160  			iter.head = i
   161  			iter.assertInteger()
   162  			return value*1000 + uint32(ind2)*100 + uint32(ind3)*10 + uint32(ind4)
   163  		}
   164  		i++
   165  		ind6 := intDigits[iter.buf[i]]
   166  		if ind6 == invalidCharForNumber {
   167  			iter.head = i
   168  			iter.assertInteger()
   169  			return value*10000 + uint32(ind2)*1000 + uint32(ind3)*100 + uint32(ind4)*10 + uint32(ind5)
   170  		}
   171  		i++
   172  		ind7 := intDigits[iter.buf[i]]
   173  		if ind7 == invalidCharForNumber {
   174  			iter.head = i
   175  			iter.assertInteger()
   176  			return value*100000 + uint32(ind2)*10000 + uint32(ind3)*1000 + uint32(ind4)*100 + uint32(ind5)*10 + uint32(ind6)
   177  		}
   178  		i++
   179  		ind8 := intDigits[iter.buf[i]]
   180  		if ind8 == invalidCharForNumber {
   181  			iter.head = i
   182  			iter.assertInteger()
   183  			return value*1000000 + uint32(ind2)*100000 + uint32(ind3)*10000 + uint32(ind4)*1000 + uint32(ind5)*100 + uint32(ind6)*10 + uint32(ind7)
   184  		}
   185  		i++
   186  		ind9 := intDigits[iter.buf[i]]
   187  		value = value*10000000 + uint32(ind2)*1000000 + uint32(ind3)*100000 + uint32(ind4)*10000 + uint32(ind5)*1000 + uint32(ind6)*100 + uint32(ind7)*10 + uint32(ind8)
   188  		iter.head = i
   189  		if ind9 == invalidCharForNumber {
   190  			iter.assertInteger()
   191  			return value
   192  		}
   193  	}
   194  	for {
   195  		for i := iter.head; i < iter.tail; i++ {
   196  			ind = intDigits[iter.buf[i]]
   197  			if ind == invalidCharForNumber {
   198  				iter.head = i
   199  				iter.assertInteger()
   200  				return value
   201  			}
   202  			if value > uint32SafeToMultiply10 {
   203  				value2 := (value << 3) + (value << 1) + uint32(ind)
   204  				if value2 < value {
   205  					iter.ReportError("readUint32", "overflow")
   206  					return
   207  				}
   208  				value = value2
   209  				continue
   210  			}
   211  			value = (value << 3) + (value << 1) + uint32(ind)
   212  		}
   213  		if !iter.loadMore() {
   214  			iter.assertInteger()
   215  			return value
   216  		}
   217  	}
   218  }
   219  
   220  // ReadInt64 read int64
   221  func (iter *Iterator) ReadInt64() (ret int64) {
   222  	c := iter.nextToken()
   223  	if c == '-' {
   224  		val := iter.readUint64(iter.readByte())
   225  		if val > math.MaxInt64+1 {
   226  			iter.ReportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
   227  			return
   228  		}
   229  		return -int64(val)
   230  	}
   231  	val := iter.readUint64(c)
   232  	if val > math.MaxInt64 {
   233  		iter.ReportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
   234  		return
   235  	}
   236  	return int64(val)
   237  }
   238  
   239  // ReadUint64 read uint64
   240  func (iter *Iterator) ReadUint64() uint64 {
   241  	return iter.readUint64(iter.nextToken())
   242  }
   243  
   244  func (iter *Iterator) readUint64(c byte) (ret uint64) {
   245  	ind := intDigits[c]
   246  	if ind == 0 {
   247  		iter.assertInteger()
   248  		return 0 // single zero
   249  	}
   250  	if ind == invalidCharForNumber {
   251  		iter.ReportError("readUint64", "unexpected character: "+string([]byte{byte(ind)}))
   252  		return
   253  	}
   254  	value := uint64(ind)
   255  	if iter.tail-iter.head > 10 {
   256  		i := iter.head
   257  		ind2 := intDigits[iter.buf[i]]
   258  		if ind2 == invalidCharForNumber {
   259  			iter.head = i
   260  			iter.assertInteger()
   261  			return value
   262  		}
   263  		i++
   264  		ind3 := intDigits[iter.buf[i]]
   265  		if ind3 == invalidCharForNumber {
   266  			iter.head = i
   267  			iter.assertInteger()
   268  			return value*10 + uint64(ind2)
   269  		}
   270  		//iter.head = i + 1
   271  		//value = value * 100 + uint32(ind2) * 10 + uint32(ind3)
   272  		i++
   273  		ind4 := intDigits[iter.buf[i]]
   274  		if ind4 == invalidCharForNumber {
   275  			iter.head = i
   276  			iter.assertInteger()
   277  			return value*100 + uint64(ind2)*10 + uint64(ind3)
   278  		}
   279  		i++
   280  		ind5 := intDigits[iter.buf[i]]
   281  		if ind5 == invalidCharForNumber {
   282  			iter.head = i
   283  			iter.assertInteger()
   284  			return value*1000 + uint64(ind2)*100 + uint64(ind3)*10 + uint64(ind4)
   285  		}
   286  		i++
   287  		ind6 := intDigits[iter.buf[i]]
   288  		if ind6 == invalidCharForNumber {
   289  			iter.head = i
   290  			iter.assertInteger()
   291  			return value*10000 + uint64(ind2)*1000 + uint64(ind3)*100 + uint64(ind4)*10 + uint64(ind5)
   292  		}
   293  		i++
   294  		ind7 := intDigits[iter.buf[i]]
   295  		if ind7 == invalidCharForNumber {
   296  			iter.head = i
   297  			iter.assertInteger()
   298  			return value*100000 + uint64(ind2)*10000 + uint64(ind3)*1000 + uint64(ind4)*100 + uint64(ind5)*10 + uint64(ind6)
   299  		}
   300  		i++
   301  		ind8 := intDigits[iter.buf[i]]
   302  		if ind8 == invalidCharForNumber {
   303  			iter.head = i
   304  			iter.assertInteger()
   305  			return value*1000000 + uint64(ind2)*100000 + uint64(ind3)*10000 + uint64(ind4)*1000 + uint64(ind5)*100 + uint64(ind6)*10 + uint64(ind7)
   306  		}
   307  		i++
   308  		ind9 := intDigits[iter.buf[i]]
   309  		value = value*10000000 + uint64(ind2)*1000000 + uint64(ind3)*100000 + uint64(ind4)*10000 + uint64(ind5)*1000 + uint64(ind6)*100 + uint64(ind7)*10 + uint64(ind8)
   310  		iter.head = i
   311  		if ind9 == invalidCharForNumber {
   312  			iter.assertInteger()
   313  			return value
   314  		}
   315  	}
   316  	for {
   317  		for i := iter.head; i < iter.tail; i++ {
   318  			ind = intDigits[iter.buf[i]]
   319  			if ind == invalidCharForNumber {
   320  				iter.head = i
   321  				iter.assertInteger()
   322  				return value
   323  			}
   324  			if value > uint64SafeToMultiple10 {
   325  				value2 := (value << 3) + (value << 1) + uint64(ind)
   326  				if value2 < value {
   327  					iter.ReportError("readUint64", "overflow")
   328  					return
   329  				}
   330  				value = value2
   331  				continue
   332  			}
   333  			value = (value << 3) + (value << 1) + uint64(ind)
   334  		}
   335  		if !iter.loadMore() {
   336  			iter.assertInteger()
   337  			return value
   338  		}
   339  	}
   340  }
   341  
   342  func (iter *Iterator) assertInteger() {
   343  	if iter.head < iter.tail && iter.buf[iter.head] == '.' {
   344  		iter.ReportError("assertInteger", "can not decode float as int")
   345  	}
   346  }
   347  

View as plain text