parse.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2018 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package util
  14. import (
  15. "errors"
  16. "os"
  17. "strconv"
  18. "strings"
  19. )
  20. // ParseUint32s parses a slice of strings into a slice of uint32s.
  21. func ParseUint32s(ss []string) ([]uint32, error) {
  22. us := make([]uint32, 0, len(ss))
  23. for _, s := range ss {
  24. u, err := strconv.ParseUint(s, 10, 32)
  25. if err != nil {
  26. return nil, err
  27. }
  28. us = append(us, uint32(u))
  29. }
  30. return us, nil
  31. }
  32. // ParseUint64s parses a slice of strings into a slice of uint64s.
  33. func ParseUint64s(ss []string) ([]uint64, error) {
  34. us := make([]uint64, 0, len(ss))
  35. for _, s := range ss {
  36. u, err := strconv.ParseUint(s, 10, 64)
  37. if err != nil {
  38. return nil, err
  39. }
  40. us = append(us, u)
  41. }
  42. return us, nil
  43. }
  44. // ParsePInt64s parses a slice of strings into a slice of int64 pointers.
  45. func ParsePInt64s(ss []string) ([]*int64, error) {
  46. us := make([]*int64, 0, len(ss))
  47. for _, s := range ss {
  48. u, err := strconv.ParseInt(s, 10, 64)
  49. if err != nil {
  50. return nil, err
  51. }
  52. us = append(us, &u)
  53. }
  54. return us, nil
  55. }
  56. // Parses a uint64 from given hex in string.
  57. func ParseHexUint64s(ss []string) ([]*uint64, error) {
  58. us := make([]*uint64, 0, len(ss))
  59. for _, s := range ss {
  60. u, err := strconv.ParseUint(s, 16, 64)
  61. if err != nil {
  62. return nil, err
  63. }
  64. us = append(us, &u)
  65. }
  66. return us, nil
  67. }
  68. // ReadUintFromFile reads a file and attempts to parse a uint64 from it.
  69. func ReadUintFromFile(path string) (uint64, error) {
  70. data, err := os.ReadFile(path)
  71. if err != nil {
  72. return 0, err
  73. }
  74. return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
  75. }
  76. // ReadIntFromFile reads a file and attempts to parse a int64 from it.
  77. func ReadIntFromFile(path string) (int64, error) {
  78. data, err := os.ReadFile(path)
  79. if err != nil {
  80. return 0, err
  81. }
  82. return strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
  83. }
  84. // ParseBool parses a string into a boolean pointer.
  85. func ParseBool(b string) *bool {
  86. var truth bool
  87. switch b {
  88. case "enabled":
  89. truth = true
  90. case "disabled":
  91. truth = false
  92. default:
  93. return nil
  94. }
  95. return &truth
  96. }
  97. // ReadHexFromFile reads a file and attempts to parse a uint64 from a hexadecimal format 0xXX.
  98. func ReadHexFromFile(path string) (uint64, error) {
  99. data, err := os.ReadFile(path)
  100. if err != nil {
  101. return 0, err
  102. }
  103. hexString := strings.TrimSpace(string(data))
  104. if !strings.HasPrefix(hexString, "0x") {
  105. return 0, errors.New("invalid format: hex string does not start with '0x'")
  106. }
  107. return strconv.ParseUint(hexString[2:], 16, 64)
  108. }