• 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 
32 std::string QuoteAndEscapeControlCodes(const std::string& raw);
33 
Lowercase(char c)34 inline char Lowercase(char c) {
35   return ('A' <= c && c <= 'Z') ? static_cast<char>(c - ('A' - 'a')) : c;
36 }
37 
Uppercase(char c)38 inline char Uppercase(char c) {
39   return ('a' <= c && c <= 'z') ? static_cast<char>(c + ('A' - 'a')) : c;
40 }
41 
42 inline Optional<uint32_t> CStringToUInt32(const char* s, int base = 10) {
43   char* endptr = nullptr;
44   auto value = static_cast<uint32_t>(strtoul(s, &endptr, base));
45   return (*s && !*endptr) ? base::make_optional(value) : base::nullopt;
46 }
47 
48 inline Optional<int32_t> CStringToInt32(const char* s, int base = 10) {
49   char* endptr = nullptr;
50   auto value = static_cast<int32_t>(strtol(s, &endptr, base));
51   return (*s && !*endptr) ? base::make_optional(value) : base::nullopt;
52 }
53 
54 // Note: it saturates to 7fffffffffffffff if parsing a hex number >= 0x8000...
55 inline Optional<int64_t> CStringToInt64(const char* s, int base = 10) {
56   char* endptr = nullptr;
57   auto value = static_cast<int64_t>(strtoll(s, &endptr, base));
58   return (*s && !*endptr) ? base::make_optional(value) : base::nullopt;
59 }
60 
61 inline Optional<uint64_t> CStringToUInt64(const char* s, int base = 10) {
62   char* endptr = nullptr;
63   auto value = static_cast<uint64_t>(strtoull(s, &endptr, base));
64   return (*s && !*endptr) ? base::make_optional(value) : base::nullopt;
65 }
66 
67 double StrToD(const char* nptr, char** endptr);
68 
CStringToDouble(const char * s)69 inline Optional<double> CStringToDouble(const char* s) {
70   char* endptr = nullptr;
71   double value = StrToD(s, &endptr);
72   Optional<double> result(base::nullopt);
73   if (*s != '\0' && *endptr == '\0')
74     result = value;
75   return result;
76 }
77 
78 inline Optional<uint32_t> StringToUInt32(const std::string& s, int base = 10) {
79   return CStringToUInt32(s.c_str(), base);
80 }
81 
82 inline Optional<int32_t> StringToInt32(const std::string& s, int base = 10) {
83   return CStringToInt32(s.c_str(), base);
84 }
85 
86 inline Optional<uint64_t> StringToUInt64(const std::string& s, int base = 10) {
87   return CStringToUInt64(s.c_str(), base);
88 }
89 
90 inline Optional<int64_t> StringToInt64(const std::string& s, int base = 10) {
91   return CStringToInt64(s.c_str(), base);
92 }
93 
StringToDouble(const std::string & s)94 inline Optional<double> StringToDouble(const std::string& s) {
95   return CStringToDouble(s.c_str());
96 }
97 
98 bool StartsWith(const std::string& str, const std::string& prefix);
99 bool EndsWith(const std::string& str, const std::string& suffix);
100 bool Contains(const std::string& haystack, const std::string& needle);
101 bool Contains(const std::string& haystack, char needle);
102 size_t Find(const StringView& needle, const StringView& haystack);
103 bool CaseInsensitiveEqual(const std::string& first, const std::string& second);
104 std::string Join(const std::vector<std::string>& parts,
105                  const std::string& delim);
106 std::vector<std::string> SplitString(const std::string& text,
107                                      const std::string& delimiter);
108 std::string StripPrefix(const std::string& str, const std::string& prefix);
109 std::string StripSuffix(const std::string& str, const std::string& suffix);
110 std::string ToLower(const std::string& str);
111 std::string ToUpper(const std::string& str);
112 std::string StripChars(const std::string& str,
113                        const std::string& chars,
114                        char replacement);
115 std::string ToHex(const char* data, size_t size);
ToHex(const std::string & s)116 inline std::string ToHex(const std::string& s) {
117   return ToHex(s.c_str(), s.size());
118 }
119 std::string IntToHexString(uint32_t number);
120 std::string Uint64ToHexString(uint64_t number);
121 std::string Uint64ToHexStringNoPrefix(uint64_t number);
122 std::string ReplaceAll(std::string str,
123                        const std::string& to_replace,
124                        const std::string& replacement);
125 std::string TrimLeading(const std::string& str);
126 std::string Base64Encode(const void* raw, size_t size);
127 
128 }  // namespace base
129 }  // namespace perfetto
130 
131 #endif  // INCLUDE_PERFETTO_EXT_BASE_STRING_UTILS_H_
132