]> git.saurik.com Git - apple/dyld.git/blob - dyld3/shared-cache/StringUtils.h
dyld-832.7.1.tar.gz
[apple/dyld.git] / dyld3 / shared-cache / StringUtils.h
1 /*
2 * Copyright (c) 2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #ifndef StringUtils_h
26 #define StringUtils_h
27
28 #include <string>
29
30 inline bool startsWith(const std::string& str, const std::string& prefix)
31 {
32 return std::mismatch(prefix.begin(), prefix.end(), str.begin()).first == prefix.end();
33 }
34
35 inline bool startsWith(const std::string_view& str, const std::string_view& prefix)
36 {
37 return std::mismatch(prefix.begin(), prefix.end(), str.begin()).first == prefix.end();
38 }
39
40 inline bool endsWith(const std::string& str, const std::string& suffix)
41 {
42 std::size_t index = str.find(suffix, str.size() - suffix.size());
43 return (index != std::string::npos);
44 }
45
46 inline bool contains(const std::string& str, const std::string& search)
47 {
48 std::size_t index = str.find(search);
49 return (index != std::string::npos);
50 }
51
52 inline char hexDigit(uint8_t value)
53 {
54 if ( value < 10 )
55 return '0' + value;
56 else
57 return 'a' + value - 10;
58 }
59
60 inline void bytesToHex(const uint8_t* bytes, size_t byteCount, char buffer[])
61 {
62 char* p = buffer;
63 for (size_t i=0; i < byteCount; ++i) {
64 *p++ = hexDigit(bytes[i] >> 4);
65 *p++ = hexDigit(bytes[i] & 0x0F);
66 }
67 *p++ = '\0';
68 }
69
70 inline void putHexNibble(uint8_t value, char*& p)
71 {
72 if ( value < 10 )
73 *p++ = '0' + value;
74 else
75 *p++ = 'A' + value - 10;
76 }
77
78 inline void putHexByte(uint8_t value, char*& p)
79 {
80 value &= 0xFF;
81 putHexNibble(value >> 4, p);
82 putHexNibble(value & 0x0F, p);
83 }
84
85 inline bool hexCharToUInt(const char hexByte, uint8_t& value) {
86 if (hexByte >= '0' && hexByte <= '9') {
87 value = hexByte - '0';
88 return true;
89 } else if (hexByte >= 'A' && hexByte <= 'F') {
90 value = hexByte - 'A' + 10;
91 return true;
92 } else if (hexByte >= 'a' && hexByte <= 'f') {
93 value = hexByte - 'a' + 10;
94 return true;
95 }
96
97 return false;
98 }
99
100 inline uint64_t hexToUInt64(const char* startHexByte, const char** endHexByte) {
101 const char* scratch;
102 if (endHexByte == nullptr) {
103 endHexByte = &scratch;
104 }
105 if (startHexByte == nullptr)
106 return 0;
107 uint64_t retval = 0;
108 if (startHexByte[0] == '0' && startHexByte[1] == 'x') {
109 startHexByte +=2;
110 }
111 *endHexByte = startHexByte + 16;
112
113 //FIXME overrun?
114 for (uint32_t i = 0; i < 16; ++i) {
115 uint8_t value;
116 if (!hexCharToUInt(startHexByte[i], value)) {
117 *endHexByte = &startHexByte[i];
118 break;
119 }
120 retval = (retval << 4) + value;
121 }
122 return retval;
123 }
124
125 inline bool hexStringToBytes(const char* hexString, uint8_t buffer[], unsigned bufferMaxSize, unsigned& bufferLenUsed)
126 {
127 bufferLenUsed = 0;
128 bool high = true;
129 for (const char* s=hexString; *s != '\0'; ++s) {
130 if ( bufferLenUsed > bufferMaxSize )
131 return false;
132 uint8_t value;
133 if ( !hexCharToUInt(*s, value) )
134 return false;
135 if ( high )
136 buffer[bufferLenUsed] = value << 4;
137 else
138 buffer[bufferLenUsed++] |= value;
139 high = !high;
140 }
141 return true;
142 }
143
144 #endif // StringUtils_h
145