-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
CHIPMemString.h
198 lines (178 loc) · 6.08 KB
/
CHIPMemString.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
*
* Copyright (c) 2020 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* This file defines string operations that allocate heap memory.
*/
#pragma once
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <lib/support/ScopedBuffer.h>
#include <lib/support/Span.h>
namespace chip {
namespace Platform {
/**
* Copies a C-style string.
*
* This differs from `strncpy()` in some important ways:
* - `dest` can be nullptr, in which case no copy is attempted, and the function returns nullptr.
* - A non-nullptr result is always null-terminated.
*
* @param[in] dest Destination string buffer or nullptr.
*
* @param[in] destLength Maximum length to be copied. Will need space for null terminator as
* well (string will be truncated if it does not fit). If 0 this method
* is a noop.
*
* @param[in] source String to be copied.
*
* @retval Same as `dest`.
*/
inline void CopyString(char * dest, size_t destLength, const char * source)
{
if (dest && destLength)
{
strncpy(dest, source, destLength);
dest[destLength - 1] = 0;
}
}
/**
* Convenience method for CopyString to auto-detect destination size.
*/
template <size_t N>
inline void CopyString(char (&dest)[N], const char * source)
{
CopyString(dest, N, source);
}
/**
* Creates a null-terminated string from a ByteSpan.
* If dest is nullptr, no copy happens. Non-nullptr result is always null-terminated.
*
* @param[in] dest Destination string buffer or nullptr.
*
* @param[in] destLength Maximum length to be copied. Will need space for null terminator as
* well (string will be truncated if it does not fit). If 0 this method
* is a noop.
*
* @param[in] source Data to be copied.
*/
inline void CopyString(char * dest, size_t destLength, ByteSpan source)
{
if ((dest == nullptr) || (destLength == 0))
{
return; // no space to copy anything, not even a null terminator
}
if (source.empty())
{
*dest = '\0'; // just a null terminator, we are copying empty data
return;
}
size_t maxChars = std::min(destLength - 1, source.size());
memcpy(dest, source.data(), maxChars);
dest[maxChars] = '\0';
}
/**
* Convenience method for CopyString to auto-detect destination size.
*/
template <size_t N>
inline void CopyString(char (&dest)[N], ByteSpan source)
{
CopyString(dest, N, source);
}
/**
* Creates a null-terminated string from a CharSpan.
* If dest is nullptr, no copy happens. Non-nullptr result is always null-terminated.
*
* @param[in] dest Destination string buffer or nullptr.
*
* @param[in] destLength Maximum length to be copied. Will need space for null terminator as
* well (string will be truncated if it does not fit). If 0 this method
* is a noop.
*
* @param[in] source Data to be copied.
*/
inline void CopyString(char * dest, size_t destLength, CharSpan source)
{
if ((dest == nullptr) || (destLength == 0))
{
return; // no space to copy anything, not even a null terminator
}
if (source.empty())
{
*dest = '\0'; // just a null terminator, we are copying empty data
return;
}
size_t maxChars = std::min(destLength - 1, source.size());
memcpy(dest, source.data(), maxChars);
dest[maxChars] = '\0';
}
/**
* Convenience method for CopyString to auto-detect destination size.
*/
template <size_t N>
inline void CopyString(char (&dest)[N], CharSpan source)
{
CopyString(dest, N, source);
}
/**
* This function copies a C-style string to memory newly allocated by Platform::MemoryAlloc().
*
* @param[in] string String to be copied.
*
* @param[in] length Length to be copied. Like `strncpy()`, if the `string` is shorter
* than `length`, then the remaining space will be filled with null
* bytes. Like `strndup()` but unlike `strncpy()`, the result is always
* null-terminated.
*
* @retval Pointer to a null-terminated string in case of success.
* @retval `nullptr` if memory allocation fails.
*
*/
inline char * MemoryAllocString(const char * string, size_t length)
{
size_t lengthWithNull = length + 1;
char * result = static_cast<char *>(MemoryAlloc(lengthWithNull));
CopyString(result, lengthWithNull, string);
return result;
}
/**
* Represents a C string in a ScopedMemoryBuffer.
*/
class ScopedMemoryString : public ScopedMemoryBuffer<char>
{
public:
/**
* Create a ScopedMemoryString.
*
* @param[in] string String to be copied.
*
* @param[in] length Length to be copied. Like `strncpy()`, if the `string` is shorter than
* `length`, then the remaining space will be filled with null bytes. Like
* `strndup()` but unlike `strncpy()`, the result is always null-terminated.
*/
ScopedMemoryString(const char * string, size_t length)
{
size_t lengthWithNull = length + 1;
// We must convert the source string to a CharSpan, so we call the
// version of CopyString that handles unterminated strings.
CopyString(Alloc(lengthWithNull).Get(), lengthWithNull, CharSpan(string, length));
}
};
} // namespace Platform
} // namespace chip