• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
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 #ifndef INCLUDE_PERFETTO_EXT_BASE_STRING_UTILS_H_
18 #define INCLUDE_PERFETTO_EXT_BASE_STRING_UTILS_H_
19 
20 #include <string>
21 #include <vector>
22 
23 #include <inttypes.h>
24 #include <stdlib.h>
25 
26 #include "perfetto/ext/base/optional.h"
27 #include "perfetto/ext/base/string_view.h"
28 
29 namespace perfetto {
30 namespace base {
31 
Lowercase(char c)32 inline char Lowercase(char c) {
33   return ('A' <= c && c <= 'Z') ? static_cast<char>(c - ('A' - 'a')) : c;
34 }
35 
Uppercase(char c)36 inline char Uppercase(char c) {
37   return ('a' <= c && c <= 'z') ? static_cast<char>(c + ('A' - 'a')) : c;
38 }
39 
40 inline Optional<uint32_t> CStringToUInt32(const char* s, int base = 10) {
41   char* endptr = nullptr;
42   auto value = static_cast<uint32_t>(strtoul(s, &endptr, base));
43   return (*s && !*endptr) ? base::make_optional(value) : base::nullopt;
44 }
45 
46 inline Optional<int32_t> CStringToInt32(const char* s, int base = 10) {
47   char* endptr = nullptr;
48   auto value = static_cast<int32_t>(strtol(s, &endptr, base));
49   return (*s && !*endptr) ? base::make_optional(value) : base::nullopt;
50 }
51 
52 // Note: it saturates to 7fffffffffffffff if parsing a hex number >= 0x8000...
53 inline Optional<int64_t> CStringToInt64(const char* s, int base = 10) {
54   char* endptr = nullptr;
55   auto value = static_cast<int64_t>(strtoll(s, &endptr, base));
56   return (*s && !*endptr) ? base::make_optional(value) : base::nullopt;
57 }
58 
59 inline Optional<uint64_t> CStringToUInt64(const char* s, int base = 10) {
60   char* endptr = nullptr;
61   auto value = static_cast<uint64_t>(strtoull(s, &endptr, base));
62   return (*s && !*endptr) ? base::make_optional(value) : base::nullopt;
63 }
64 
CStringToDouble(const char * s)65 inline Optional<double> CStringToDouble(const char* s) {
66   char* endptr = nullptr;
67   double value = strtod(s, &endptr);
68   Optional<double> result(base::nullopt);
69   if (*s != '\0' && *endptr == '\0')
70     result = value;
71   return result;
72 }
73 
74 inline Optional<uint32_t> StringToUInt32(const std::string& s, int base = 10) {
75   return CStringToUInt32(s.c_str(), base);
76 }
77 
78 inline Optional<int32_t> StringToInt32(const std::string& s, int base = 10) {
79   return CStringToInt32(s.c_str(), base);
80 }
81 
82 inline Optional<uint64_t> StringToUInt64(const std::string& s, int base = 10) {
83   return CStringToUInt64(s.c_str(), base);
84 }
85 
86 inline Optional<int64_t> StringToInt64(const std::string& s, int base = 10) {
87   return CStringToInt64(s.c_str(), base);
88 }
89 
StringToDouble(const std::string & s)90 inline Optional<double> StringToDouble(const std::string& s) {
91   return CStringToDouble(s.c_str());
92 }
93 
94 bool StartsWith(const std::string& str, const std::string& prefix);
95 bool EndsWith(const std::string& str, const std::string& suffix);
96 bool Contains(const std::string& haystack, const std::string& needle);
97 size_t Find(const StringView& needle, const StringView& haystack);
98 bool CaseInsensitiveEqual(const std::string& first, const std::string& second);
99 std::string Join(const std::vector<std::string>& parts,
100                  const std::string& delim);
101 std::vector<std::string> SplitString(const std::string& text,
102                                      const std::string& delimiter);
103 std::string StripPrefix(const std::string& str, const std::string& prefix);
104 std::string StripSuffix(const std::string& str, const std::string& suffix);
105 std::string ToLower(const std::string& str);
106 std::string ToUpper(const std::string& str);
107 std::string StripChars(const std::string& str,
108                        const std::string& chars,
109                        char replacement);
110 std::string ToHex(const char* data, size_t size);
ToHex(const std::string & s)111 inline std::string ToHex(const std::string& s) {
112   return ToHex(s.c_str(), s.size());
113 }
114 std::string IntToHexString(uint32_t number);
115 std::string ReplaceAll(std::string str,
116                        const std::string& to_replace,
117                        const std::string& replacement);
118 std::string TrimLeading(const std::string& str);
119 
120 }  // namespace base
121 }  // namespace perfetto
122 
123 #endif  // INCLUDE_PERFETTO_EXT_BASE_STRING_UTILS_H_
124