1 /*
2 * Copyright (c) 2018, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file
31 * This file implements OpenThread String class and functions.
32 */
33
34 #include "string.hpp"
35 #include "debug.hpp"
36
37 #include <string.h>
38
39 namespace ot {
40
41 namespace {
42
43 // The definitions below are included in an unnamed namespace
44 // to limit their scope to this translation unit (this file).
45
46 enum MatchType : uint8_t
47 {
48 kNoMatch,
49 kPrefixMatch,
50 kFullMatch,
51 };
52
Match(const char * aString,const char * aPrefixString,StringMatchMode aMode)53 MatchType Match(const char *aString, const char *aPrefixString, StringMatchMode aMode)
54 {
55 // This is file private function that is used by other functions.
56 // It matches @p aString with @p aPrefixString using match @ aMode.
57 //
58 // If @p aString and @p aPrefixString match and have the
59 // same length `kFullMatch` is returned. If @p aString starts
60 // with @p aPrefixString but contains more characters, then
61 // `kPrefixMatch` is returned. Otherwise `kNoMatch` is returned.
62
63 MatchType match = kNoMatch;
64
65 switch (aMode)
66 {
67 case kStringExactMatch:
68 while (*aPrefixString != kNullChar)
69 {
70 VerifyOrExit(*aString++ == *aPrefixString++);
71 }
72 break;
73
74 case kStringCaseInsensitiveMatch:
75 while (*aPrefixString != kNullChar)
76 {
77 VerifyOrExit(ToLowercase(*aString++) == ToLowercase(*aPrefixString++));
78 }
79 break;
80 }
81
82 match = (*aString == kNullChar) ? kFullMatch : kPrefixMatch;
83
84 exit:
85 return match;
86 }
87
88 } // namespace
89
StringLength(const char * aString,uint16_t aMaxLength)90 uint16_t StringLength(const char *aString, uint16_t aMaxLength)
91 {
92 uint16_t ret;
93
94 for (ret = 0; (ret < aMaxLength) && (aString[ret] != kNullChar); ret++)
95 {
96 // Empty loop.
97 }
98
99 return ret;
100 }
101
StringFind(const char * aString,char aChar)102 const char *StringFind(const char *aString, char aChar)
103 {
104 const char *ret = nullptr;
105
106 for (; *aString != kNullChar; aString++)
107 {
108 if (*aString == aChar)
109 {
110 ret = aString;
111 break;
112 }
113 }
114
115 return ret;
116 }
117
StringFind(const char * aString,const char * aSubString,StringMatchMode aMode)118 const char *StringFind(const char *aString, const char *aSubString, StringMatchMode aMode)
119 {
120 const char *ret = nullptr;
121 size_t len = strlen(aString);
122 size_t subLen = strlen(aSubString);
123
124 VerifyOrExit(subLen <= len);
125
126 for (size_t index = 0; index <= static_cast<size_t>(len - subLen); index++)
127 {
128 if (Match(&aString[index], aSubString, aMode) != kNoMatch)
129 {
130 ExitNow(ret = &aString[index]);
131 }
132 }
133
134 exit:
135 return ret;
136 }
137
StringStartsWith(const char * aString,const char * aPrefixString,StringMatchMode aMode)138 bool StringStartsWith(const char *aString, const char *aPrefixString, StringMatchMode aMode)
139 {
140 return Match(aString, aPrefixString, aMode) != kNoMatch;
141 }
142
StringEndsWith(const char * aString,char aChar)143 bool StringEndsWith(const char *aString, char aChar)
144 {
145 size_t len = strlen(aString);
146
147 return (len > 0) && (aString[len - 1] == aChar);
148 }
149
StringEndsWith(const char * aString,const char * aSubString,StringMatchMode aMode)150 bool StringEndsWith(const char *aString, const char *aSubString, StringMatchMode aMode)
151 {
152 size_t len = strlen(aString);
153 size_t subLen = strlen(aSubString);
154
155 return (subLen > 0) && (len >= subLen) && (Match(&aString[len - subLen], aSubString, aMode) != kNoMatch);
156 }
157
StringMatch(const char * aFirstString,const char * aSecondString,StringMatchMode aMode)158 bool StringMatch(const char *aFirstString, const char *aSecondString, StringMatchMode aMode)
159 {
160 return Match(aFirstString, aSecondString, aMode) == kFullMatch;
161 }
162
StringConvertToLowercase(char * aString)163 void StringConvertToLowercase(char *aString)
164 {
165 for (; *aString != kNullChar; aString++)
166 {
167 *aString = ToLowercase(*aString);
168 }
169 }
170
StringConvertToUppercase(char * aString)171 void StringConvertToUppercase(char *aString)
172 {
173 for (; *aString != kNullChar; aString++)
174 {
175 *aString = ToUppercase(*aString);
176 }
177 }
178
ToLowercase(char aChar)179 char ToLowercase(char aChar)
180 {
181 if ((aChar >= 'A') && (aChar <= 'Z'))
182 {
183 aChar += 'a' - 'A';
184 }
185
186 return aChar;
187 }
188
ToUppercase(char aChar)189 char ToUppercase(char aChar)
190 {
191 if ((aChar >= 'a') && (aChar <= 'z'))
192 {
193 aChar -= 'a' - 'A';
194 }
195
196 return aChar;
197 }
198
ToYesNo(bool aBool)199 const char *ToYesNo(bool aBool)
200 {
201 static const char *const kYesNoStrings[] = {"no", "yes"};
202
203 return kYesNoStrings[aBool];
204 }
205
StringWriter(char * aBuffer,uint16_t aSize)206 StringWriter::StringWriter(char *aBuffer, uint16_t aSize)
207 : mBuffer(aBuffer)
208 , mLength(0)
209 , mSize(aSize)
210 {
211 mBuffer[0] = kNullChar;
212 }
213
Clear(void)214 StringWriter &StringWriter::Clear(void)
215 {
216 mBuffer[0] = kNullChar;
217 mLength = 0;
218 return *this;
219 }
220
Append(const char * aFormat,...)221 StringWriter &StringWriter::Append(const char *aFormat, ...)
222 {
223 va_list args;
224 va_start(args, aFormat);
225 AppendVarArgs(aFormat, args);
226 va_end(args);
227
228 return *this;
229 }
230
AppendVarArgs(const char * aFormat,va_list aArgs)231 StringWriter &StringWriter::AppendVarArgs(const char *aFormat, va_list aArgs)
232 {
233 int len;
234
235 len = vsnprintf(mBuffer + mLength, (mSize > mLength ? (mSize - mLength) : 0), aFormat, aArgs);
236 OT_ASSERT(len >= 0);
237
238 mLength += static_cast<uint16_t>(len);
239
240 if (IsTruncated())
241 {
242 mBuffer[mSize - 1] = kNullChar;
243 }
244
245 return *this;
246 }
247
AppendHexBytes(const uint8_t * aBytes,uint16_t aLength)248 StringWriter &StringWriter::AppendHexBytes(const uint8_t *aBytes, uint16_t aLength)
249 {
250 while (aLength--)
251 {
252 Append("%02x", *aBytes++);
253 }
254
255 return *this;
256 }
257
IsValidUtf8String(const char * aString)258 bool IsValidUtf8String(const char *aString)
259 {
260 return IsValidUtf8String(aString, strlen(aString));
261 }
262
IsValidUtf8String(const char * aString,size_t aLength)263 bool IsValidUtf8String(const char *aString, size_t aLength)
264 {
265 bool ret = true;
266 uint8_t byte;
267 uint8_t continuationBytes = 0;
268 size_t position = 0;
269
270 while (position < aLength)
271 {
272 byte = *reinterpret_cast<const uint8_t *>(aString + position);
273 ++position;
274
275 if ((byte & 0x80) == 0)
276 {
277 // We don't allow control characters.
278 VerifyOrExit(!iscntrl(byte), ret = false);
279 continue;
280 }
281
282 // This is a leading byte 1xxx-xxxx.
283
284 if ((byte & 0x40) == 0) // 10xx-xxxx
285 {
286 // We got a continuation byte pattern without seeing a leading byte earlier.
287 ExitNow(ret = false);
288 }
289 else if ((byte & 0x20) == 0) // 110x-xxxx
290 {
291 continuationBytes = 1;
292 }
293 else if ((byte & 0x10) == 0) // 1110-xxxx
294 {
295 continuationBytes = 2;
296 }
297 else if ((byte & 0x08) == 0) // 1111-0xxx
298 {
299 continuationBytes = 3;
300 }
301 else // 1111-1xxx (invalid pattern).
302 {
303 ExitNow(ret = false);
304 }
305
306 while (continuationBytes-- != 0)
307 {
308 VerifyOrExit(position < aLength, ret = false);
309
310 byte = *reinterpret_cast<const uint8_t *>(aString + position);
311 ++position;
312
313 // Verify the continuation byte pattern 10xx-xxxx
314 VerifyOrExit((byte & 0xc0) == 0x80, ret = false);
315 }
316 }
317
318 exit:
319 return ret;
320 }
321
322 } // namespace ot
323