parse.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. "io/ioutil"
  16. "strconv"
  17. "strings"
  18. )
  19. // ParseUint32s parses a slice of strings into a slice of uint32s.
  20. func ParseUint32s(ss []string) ([]uint32, error) {
  21. us := make([]uint32, 0, len(ss))
  22. for _, s := range ss {
  23. u, err := strconv.ParseUint(s, 10, 32)
  24. if err != nil {
  25. return nil, err
  26. }
  27. us = append(us, uint32(u))
  28. }
  29. return us, nil
  30. }
  31. // ParseUint64s parses a slice of strings into a slice of uint64s.
  32. func ParseUint64s(ss []string) ([]uint64, error) {
  33. us := make([]uint64, 0, len(ss))
  34. for _, s := range ss {
  35. u, err := strconv.ParseUint(s, 10, 64)
  36. if err != nil {
  37. return nil, err
  38. }
  39. us = append(us, u)
  40. }
  41. return us, nil
  42. }
  43. // ReadUintFromFile reads a file and attempts to parse a uint64 from it.
  44. func ReadUintFromFile(path string) (uint64, error) {
  45. data, err := ioutil.ReadFile(path)
  46. if err != nil {
  47. return 0, err
  48. }
  49. return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
  50. }