blob: 5a7d2df06ae336fb231ab986d63beb3483fbf54c [file] [log] [blame]
khenaidoo26721882021-08-11 17:42:52 -04001// 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
14package util
15
16import (
Abhay Kumar40252eb2025-10-13 13:25:53 +000017 "errors"
18 "os"
khenaidoo26721882021-08-11 17:42:52 -040019 "strconv"
20 "strings"
21)
22
23// ParseUint32s parses a slice of strings into a slice of uint32s.
24func ParseUint32s(ss []string) ([]uint32, error) {
25 us := make([]uint32, 0, len(ss))
26 for _, s := range ss {
27 u, err := strconv.ParseUint(s, 10, 32)
28 if err != nil {
29 return nil, err
30 }
31
32 us = append(us, uint32(u))
33 }
34
35 return us, nil
36}
37
38// ParseUint64s parses a slice of strings into a slice of uint64s.
39func ParseUint64s(ss []string) ([]uint64, error) {
40 us := make([]uint64, 0, len(ss))
41 for _, s := range ss {
42 u, err := strconv.ParseUint(s, 10, 64)
43 if err != nil {
44 return nil, err
45 }
46
47 us = append(us, u)
48 }
49
50 return us, nil
51}
52
53// ParsePInt64s parses a slice of strings into a slice of int64 pointers.
54func ParsePInt64s(ss []string) ([]*int64, error) {
55 us := make([]*int64, 0, len(ss))
56 for _, s := range ss {
57 u, err := strconv.ParseInt(s, 10, 64)
58 if err != nil {
59 return nil, err
60 }
61
62 us = append(us, &u)
63 }
64
65 return us, nil
66}
67
Abhay Kumar40252eb2025-10-13 13:25:53 +000068// Parses a uint64 from given hex in string.
69func ParseHexUint64s(ss []string) ([]*uint64, error) {
70 us := make([]*uint64, 0, len(ss))
71 for _, s := range ss {
72 u, err := strconv.ParseUint(s, 16, 64)
73 if err != nil {
74 return nil, err
75 }
76
77 us = append(us, &u)
78 }
79
80 return us, nil
81}
82
khenaidoo26721882021-08-11 17:42:52 -040083// ReadUintFromFile reads a file and attempts to parse a uint64 from it.
84func ReadUintFromFile(path string) (uint64, error) {
Abhay Kumar40252eb2025-10-13 13:25:53 +000085 data, err := os.ReadFile(path)
khenaidoo26721882021-08-11 17:42:52 -040086 if err != nil {
87 return 0, err
88 }
89 return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
90}
91
92// ReadIntFromFile reads a file and attempts to parse a int64 from it.
93func ReadIntFromFile(path string) (int64, error) {
Abhay Kumar40252eb2025-10-13 13:25:53 +000094 data, err := os.ReadFile(path)
khenaidoo26721882021-08-11 17:42:52 -040095 if err != nil {
96 return 0, err
97 }
98 return strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
99}
100
101// ParseBool parses a string into a boolean pointer.
102func ParseBool(b string) *bool {
103 var truth bool
104 switch b {
105 case "enabled":
106 truth = true
107 case "disabled":
108 truth = false
109 default:
110 return nil
111 }
112 return &truth
113}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000114
115// ReadHexFromFile reads a file and attempts to parse a uint64 from a hexadecimal format 0xXX.
116func ReadHexFromFile(path string) (uint64, error) {
117 data, err := os.ReadFile(path)
118 if err != nil {
119 return 0, err
120 }
121 hexString := strings.TrimSpace(string(data))
122 if !strings.HasPrefix(hexString, "0x") {
123 return 0, errors.New("invalid format: hex string does not start with '0x'")
124 }
125 return strconv.ParseUint(hexString[2:], 16, 64)
126}