bytes.go 509 B

12345678910111213141516171819202122232425262728
  1. package jsonparser
  2. // About 3x faster then strconv.ParseInt because does not check for range error and support only base 10, which is enough for JSON
  3. func parseInt(bytes []byte) (v int64, ok bool) {
  4. if len(bytes) == 0 {
  5. return 0, false
  6. }
  7. var neg bool = false
  8. if bytes[0] == '-' {
  9. neg = true
  10. bytes = bytes[1:]
  11. }
  12. for _, c := range bytes {
  13. if c >= '0' && c <= '9' {
  14. v = (10 * v) + int64(c-'0')
  15. } else {
  16. return 0, false
  17. }
  18. }
  19. if neg {
  20. return -v, true
  21. } else {
  22. return v, true
  23. }
  24. }