bytenum.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Copyright 2014 Paul Querna
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. /* Portions of this file are on Go stdlib's strconv/iota.go */
  18. // Copyright 2009 The Go Authors. All rights reserved.
  19. // Use of this source code is governed by a BSD-style
  20. // license that can be found in the LICENSE file.
  21. package v1
  22. import (
  23. "github.com/pquerna/ffjson/fflib/v1/internal"
  24. )
  25. func ParseFloat(s []byte, bitSize int) (f float64, err error) {
  26. return internal.ParseFloat(s, bitSize)
  27. }
  28. // ParseUint is like ParseInt but for unsigned numbers, and oeprating on []byte
  29. func ParseUint(s []byte, base int, bitSize int) (n uint64, err error) {
  30. if len(s) == 1 {
  31. switch s[0] {
  32. case '0':
  33. return 0, nil
  34. case '1':
  35. return 1, nil
  36. case '2':
  37. return 2, nil
  38. case '3':
  39. return 3, nil
  40. case '4':
  41. return 4, nil
  42. case '5':
  43. return 5, nil
  44. case '6':
  45. return 6, nil
  46. case '7':
  47. return 7, nil
  48. case '8':
  49. return 8, nil
  50. case '9':
  51. return 9, nil
  52. }
  53. }
  54. return internal.ParseUint(s, base, bitSize)
  55. }
  56. func ParseInt(s []byte, base int, bitSize int) (i int64, err error) {
  57. if len(s) == 1 {
  58. switch s[0] {
  59. case '0':
  60. return 0, nil
  61. case '1':
  62. return 1, nil
  63. case '2':
  64. return 2, nil
  65. case '3':
  66. return 3, nil
  67. case '4':
  68. return 4, nil
  69. case '5':
  70. return 5, nil
  71. case '6':
  72. return 6, nil
  73. case '7':
  74. return 7, nil
  75. case '8':
  76. return 8, nil
  77. case '9':
  78. return 9, nil
  79. }
  80. }
  81. return internal.ParseInt(s, base, bitSize)
  82. }