Open Lighting Architecture  0.9.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StrUtil.h
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // from google3/strings/strutil.h
32 
33 #ifndef PROTOC_STRUTIL_H_
34 #define PROTOC_STRUTIL_H_
35 
36 #include <google/protobuf/stubs/common.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <string>
40 #include <vector>
41 
42 namespace ola {
43 
44 using std::vector;
45 using std::string;
46 #ifdef _MSC_VER
47 #define strtoll _strtoi64
48 #define strtoull _strtoui64
49 #elif defined(__DECCXX) && defined(__osf__)
50 // HP C++ on Tru64 does not have strtoll, but strtol is already 64-bit.
51 #define strtoll strtol
52 #define strtoull strtoul
53 #endif
54 
55 // ----------------------------------------------------------------------
56 // ascii_isalnum()
57 // Check if an ASCII character is alphanumeric. We can't use ctype's
58 // isalnum() because it is affected by locale. This function is applied
59 // to identifiers in the protocol buffer language, not to natural-language
60 // strings, so locale should not be taken into account.
61 // ascii_isdigit()
62 // Like above, but only accepts digits.
63 // ----------------------------------------------------------------------
64 
65 inline bool ascii_isalnum(char c) {
66  return ('a' <= c && c <= 'z') ||
67  ('A' <= c && c <= 'Z') ||
68  ('0' <= c && c <= '9');
69 }
70 
71 inline bool ascii_isdigit(char c) {
72  return ('0' <= c && c <= '9');
73 }
74 
75 // ----------------------------------------------------------------------
76 // HasSuffixString()
77 // Return true if str ends in suffix.
78 // StripSuffixString()
79 // Given a string and a putative suffix, returns the string minus the
80 // suffix string if the suffix matches, otherwise the original
81 // string.
82 // ----------------------------------------------------------------------
83 inline bool HasSuffixString(const string& str,
84  const string& suffix) {
85  return str.size() >= suffix.size() &&
86  str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
87 }
88 
89 inline string StripSuffixString(const string& str, const string& suffix) {
90  if (HasSuffixString(str, suffix)) {
91  return str.substr(0, str.size() - suffix.size());
92  } else {
93  return str;
94  }
95 }
96 
97 // ----------------------------------------------------------------------
98 // StringReplace()
99 // Give me a string and two patterns "old" and "new", and I replace
100 // the first instance of "old" in the string with "new", if it
101 // exists. RETURN a new string, regardless of whether the replacement
102 // happened or not.
103 // ----------------------------------------------------------------------
104 
105 LIBPROTOBUF_EXPORT string StringReplace(const string& s, const string& oldsub,
106  const string& newsub, bool replace_all);
107 
108 // ----------------------------------------------------------------------
109 // SplitStringUsing()
110 // Split a string using a character delimiter. Append the components
111 // to 'result'. If there are consecutive delimiters, this function skips
112 // over all of them.
113 // ----------------------------------------------------------------------
114 LIBPROTOBUF_EXPORT void SplitStringUsing(const string& full, const char* delim,
115  vector<string>* res);
116 
117 // ----------------------------------------------------------------------
118 // FastIntToBuffer()
119 // FastHexToBuffer()
120 // FastHex64ToBuffer()
121 // FastHex32ToBuffer()
122 // FastTimeToBuffer()
123 // These are intended for speed. FastIntToBuffer() assumes the
124 // integer is non-negative. FastHexToBuffer() puts output in
125 // hex rather than decimal. FastTimeToBuffer() puts the output
126 // into RFC822 format.
127 //
128 // FastHex64ToBuffer() puts a 64-bit unsigned value in hex-format,
129 // padded to exactly 16 bytes (plus one byte for '\0')
130 //
131 // FastHex32ToBuffer() puts a 32-bit unsigned value in hex-format,
132 // padded to exactly 8 bytes (plus one byte for '\0')
133 //
134 // All functions take the output buffer as an arg.
135 // They all return a pointer to the beginning of the output,
136 // which may not be the beginning of the input buffer.
137 // ----------------------------------------------------------------------
138 
139 // Suggested buffer size for FastToBuffer functions. Also works with
140 // DoubleToBuffer() and FloatToBuffer().
141 static const int kFastToBufferSize = 32;
142 
143 LIBPROTOBUF_EXPORT char* FastInt32ToBuffer(int32_t i, char* buffer);
144 LIBPROTOBUF_EXPORT char* FastInt64ToBuffer(int64_t i, char* buffer);
145 char* FastUInt32ToBuffer(uint32_t i, char* buffer); // inline below
146 char* FastUInt64ToBuffer(uint64_t i, char* buffer); // inline below
147 LIBPROTOBUF_EXPORT char* FastHexToBuffer(int i, char* buffer);
148 LIBPROTOBUF_EXPORT char* FastHex64ToBuffer(uint64_t i, char* buffer);
149 LIBPROTOBUF_EXPORT char* FastHex32ToBuffer(uint32_t i, char* buffer);
150 
151 // ----------------------------------------------------------------------
152 // FastInt32ToBufferLeft()
153 // FastUInt32ToBufferLeft()
154 // FastInt64ToBufferLeft()
155 // FastUInt64ToBufferLeft()
156 //
157 // Like the Fast*ToBuffer() functions above, these are intended for speed.
158 // Unlike the Fast*ToBuffer() functions, however, these functions write
159 // their output to the beginning of the buffer (hence the name, as the
160 // output is left-aligned). The caller is responsible for ensuring that
161 // the buffer has enough space to hold the output.
162 //
163 // Returns a pointer to the end of the string (i.e. the null character
164 // terminating the string).
165 // ----------------------------------------------------------------------
166 
167 LIBPROTOBUF_EXPORT char* FastInt32ToBufferLeft(int32_t i, char* buffer);
168 LIBPROTOBUF_EXPORT char* FastUInt32ToBufferLeft(uint32_t i, char* buffer);
169 LIBPROTOBUF_EXPORT char* FastInt64ToBufferLeft(int64_t i, char* buffer);
170 LIBPROTOBUF_EXPORT char* FastUInt64ToBufferLeft(uint64_t i, char* buffer);
171 
172 // Just define these in terms of the above.
173 inline char* FastUInt32ToBuffer(uint32_t i, char* buffer) {
174  FastUInt32ToBufferLeft(i, buffer);
175  return buffer;
176 }
177 inline char* FastUInt64ToBuffer(uint64_t i, char* buffer) {
178  FastUInt64ToBufferLeft(i, buffer);
179  return buffer;
180 }
181 
182 // ----------------------------------------------------------------------
183 // SimpleItoa()
184 // Description: converts an integer to a string.
185 //
186 // Return value: string
187 // ----------------------------------------------------------------------
188 LIBPROTOBUF_EXPORT string SimpleItoa(int i);
189 LIBPROTOBUF_EXPORT string SimpleItoa(unsigned int i);
190 LIBPROTOBUF_EXPORT string SimpleItoa(long i); // NOLINT(runtime/int)
191 LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long i); // NOLINT(runtime/int)
192 LIBPROTOBUF_EXPORT string SimpleItoa(long long i); // NOLINT(runtime/int)
193 LIBPROTOBUF_EXPORT string SimpleItoa(
194  unsigned long long i); // NOLINT(runtime/int)
195 
196 } // namespace ola
197 
198 #endif // PROTOC_STRUTIL_H_