]>
git.saurik.com Git - apple/dyld.git/blob - dyld3/shared-cache/StringUtils.h
2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
30 inline bool startsWith(const std::string
& str
, const std::string
& prefix
)
32 return std::mismatch(prefix
.begin(), prefix
.end(), str
.begin()).first
== prefix
.end();
35 inline bool startsWith(const std::string_view
& str
, const std::string_view
& prefix
)
37 return std::mismatch(prefix
.begin(), prefix
.end(), str
.begin()).first
== prefix
.end();
40 inline bool endsWith(const std::string
& str
, const std::string
& suffix
)
42 std::size_t index
= str
.find(suffix
, str
.size() - suffix
.size());
43 return (index
!= std::string::npos
);
46 inline bool contains(const std::string
& str
, const std::string
& search
)
48 std::size_t index
= str
.find(search
);
49 return (index
!= std::string::npos
);
52 inline char hexDigit(uint8_t value
)
57 return 'a' + value
- 10;
60 inline void bytesToHex(const uint8_t* bytes
, size_t byteCount
, char buffer
[])
63 for (size_t i
=0; i
< byteCount
; ++i
) {
64 *p
++ = hexDigit(bytes
[i
] >> 4);
65 *p
++ = hexDigit(bytes
[i
] & 0x0F);
70 inline uint8_t hexCharToUInt(const char hexByte
, uint8_t& value
) {
71 if (hexByte
>= '0' && hexByte
<= '9') {
72 value
= hexByte
- '0';
74 } else if (hexByte
>= 'A' && hexByte
<= 'F') {
75 value
= hexByte
- 'A' + 10;
77 } else if (hexByte
>= 'a' && hexByte
<= 'f') {
78 value
= hexByte
- 'a' + 10;
85 inline uint64_t hexToUInt64(const char* startHexByte
, const char** endHexByte
) {
86 if (startHexByte
== nullptr)
89 if (startHexByte
[0] == '0' && startHexByte
[1] == 'x') {
92 *endHexByte
= startHexByte
+ 16;
95 for (uint32_t i
= 0; i
< 16; ++i
) {
97 if (!hexCharToUInt(startHexByte
[i
], value
)) {
98 *endHexByte
= &startHexByte
[i
];
101 retval
= (retval
<< 4) + value
;
106 inline bool hexToBytes(const char* startHexByte
, uint32_t length
, uint8_t buffer
[]) {
107 if (startHexByte
== nullptr)
109 const char *currentHexByte
= startHexByte
;
110 for (uint32_t i
= 0; i
< length
; ++i
) {
112 if (!hexCharToUInt(currentHexByte
[i
], value
)) {
116 buffer
[i
/2] = value
<< 4;
118 buffer
[(i
-1)/2] |= value
;
124 #endif // StringUtils_h