File: string_ext.h

package info (click to toggle)
warzone2100 4.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,348 kB
  • sloc: cpp: 675,711; ansic: 387,204; javascript: 75,107; python: 16,628; php: 4,294; sh: 3,941; makefile: 2,330; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (262 lines) | stat: -rw-r--r-- 7,515 bytes parent folder | download | duplicates (2)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
	This file is part of Warzone 2100.
	Copyright (C) 1999-2004  Eidos Interactive
	Copyright (C) 2005-2020  Warzone 2100 Project

	Warzone 2100 is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Warzone 2100 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef STRING_EXT_H
#define STRING_EXT_H

#define FRAME_LIB_INCLUDE
#include "wzglobal.h"
#include "debug.h"
#include <string.h>
#include <stddef.h>
#include <assert.h>

#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif

#include <string>
#include <utility>
#include <stdarg.h>

/*!
 * On MSVC, in order to squelch tons of 'memory leaks' we set the allocator
 * to not count the memory received by strdup() calls.
 *
 */
#if defined(WZ_CC_MSVC)
#	ifdef strdup
#		undef strdup
#	endif
#	if defined(DEBUG)
#		define strdup(s) \
	strdup2(static_cast<const char*>(s),__LINE__)
static inline char *strdup2(const char *s, int line)
{
	char *result;

	(void)debug_MEMCHKOFF();
	result = (char *)malloc(strlen(s) + 1);
	(void)debug_MEMCHKON();

	if (result == (char *)0)
	{
		return (char *)0;
	}
	strcpy(result, s);
	return result;
}
#	else	// for release builds
#		define strdup _strdup
#	endif	//debug block
#endif

/*!
 * Safe variant of strlen.
 * Finds the length of the required buffer to store string.
 * \param string holds the string to scan the required buffer size for
 * \param maxlen the maximum amount of bytes to scan in string
 * \return the required size for a buffer to hold \c string or \c maxlen if
 *         no terminating '\\0' is found.
 *
 * \note This is the same as strnlen(string, maxlen - 1) + 1 when using the
 *       GNU C library.
 */
WZ_DECL_PURE static inline size_t strnlen1(const char *string, size_t maxlen)
{
	// Find the first NUL char
	const char *end = (const char *)memchr(string, '\0', maxlen); // Cast required for C++

	if (end != NULL)
	{
		return end - string + 1;
	}
	else
	{
		return maxlen;
	}
}


#ifndef HAVE_VALID_STRLCPY
# ifdef HAVE_SYSTEM_STRLCPY
// If the system provides a non-conformant strlcpy we use our own
#  ifdef strlcpy
#   undef strlcpy
#  endif
#  define strlcpy wz_strlcpy
# endif // HAVE_SYSTEM_STRLCPY

/*!
 * A safer variant of \c strncpy and its completely unsafe variant \c strcpy.
 * Copy src to string dest of size "size". At most size-1 characters will be copied.
 * Always nul-terminates, unless size = 0. Returned value is the entire length of string src.
 * \param dest a pointer to the destination buffer
 * \param src the source string to copy into the \c dest buffer
 * \param size the buffer size (in bytes) of buffer \c dest
 * \return Length to string src, if >= size truncation occurred
 */
static inline size_t strlcpy(char *WZ_DECL_RESTRICT dest, const char *WZ_DECL_RESTRICT src, size_t size)
{
#ifdef DEBUG
	ASSERT_OR_RETURN(0, src != NULL, "strlcpy was passed an invalid src parameter.");
	ASSERT_OR_RETURN(0, dest != NULL, "strlcpy was passed an invalid dest parameter.");
#endif

#if defined(WZ_CC_GNU) && !defined(WZ_CC_INTEL) && !defined(WZ_CC_CLANG) && (8 <= __GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wstringop-truncation"
#endif

	if (size > 0)
	{
		strncpy(dest, src, size - 1);
		// Guarantee to nul-terminate
		dest[size - 1] = '\0';
	}

#if defined(WZ_CC_GNU) && !defined(WZ_CC_INTEL) && !defined(WZ_CC_CLANG) && (8 <= __GNUC__)
# pragma GCC diagnostic pop
#endif

	return strlen(src);
}
#endif // HAVE_VALID_STRLCPY

#ifndef HAVE_VALID_STRLCAT
# ifdef HAVE_SYSTEM_STRLCAT
// If the system provides a non-conformant strlcat we use our own
#  ifdef strlcat
#   undef strlcat
#  endif
#  define strlcat wz_strlcat
# endif // HAVE_SYSTEM_STRLCAT
/**
 * A safer variant of \c strncat and its completely unsafe variant \c strcat.
 * Append src to string dest of size "size" (unlike strncat, size is the
 * full size of dest, not space left). At most size-1 characters will be copied.
 * Always nul-terminates, unless size < strlen(dest).
 * Returned value is the entire length of string src + min(size, strlen(dest)).
 * \param dest a pointer to the destination buffer
 * \param src the source string to copy into the \c dest buffer
 * \param size the buffer size (in bytes) of buffer \c dest
 * \return Length to string src + dest, if >= size truncation occurred.
 */
static inline size_t strlcat(char *WZ_DECL_RESTRICT dest, const char *WZ_DECL_RESTRICT src, size_t size)
{
	size_t len;

#ifdef DEBUG
	ASSERT_OR_RETURN(0, src != NULL, "strlcat was passed an invalid src parameter.");
	ASSERT_OR_RETURN(0, dest != NULL, "strlcat was passed an invalid dest parameter.");
#endif

	len = strlen(src);

	if (size > 0)
	{
		size_t dlen;

		dlen = strnlen1(dest, size);
		len += dlen;

		assert(dlen > 0);

		strlcpy(&dest[dlen - 1], src, size - dlen);
	}

	return len;
}
#endif // HAVE_VALID_STRLCAT

/*
 * Static array versions of common string functions. Safer because one less parameter to screw up.
 */
template <unsigned N>
static inline size_t sstrcpy(char (&dest)[N], char const *src) { return strlcpy(dest, src, N); }
template <unsigned N>
static inline size_t sstrcat(char (&dest)[N], char const *src) { return strlcat(dest, src, N); }
template <unsigned N1, unsigned N2>
static inline int sstrcmp(char const (&str1)[N1], char const (&str2)[N2]) { return strncmp(str1, str2, std::min(N1, N2)); }
template <unsigned N, typename... P>
static inline int ssprintf(char (&dest)[N], char const *format, P &&... params) { return snprintf(dest, N, format, std::forward<P>(params)...); }
template <unsigned N>
static inline int vssprintf(char (&dest)[N], char const *format, va_list params) { return vsnprintf(dest, N, format, params); }

template <typename... P>
static inline std::string astringf(char const *format, P &&... params)
{
	int len = snprintf(nullptr, 0, format, std::forward<P>(params)...);
	if (len <= 0)
	{
		return {};
	}
	std::string str;
	str.resize(len + 1);
	snprintf(&str[0], len + 1, format, std::forward<P>(params)...);
	str.resize(len);
	return str;
}


template <typename... P>
static inline void sstringf(std::string &str, char const *format, P &&... params)
{
	str.resize(str.capacity());
	int len = snprintf(&str[0], str.size(), format, std::forward<P>(params)...);
	if (len <= 0)
	{
		str.clear();
	}
	else if ((unsigned)len >= str.size())
	{
		str.resize(len + 1);
		snprintf(&str[0], len + 1, format, std::forward<P>(params)...);
	}
	str.resize(len);
}

static inline bool strEndsWith(const std::string &str, const std::string &suffix)
{
	return (str.size() >= suffix.size()) && (str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0);
}

static inline size_t nthOccurrenceOfChar(const std::string& str, const char c, size_t n)
{
	size_t pos = 0;
	size_t count = 0;

	while (count < n)
	{
		pos = str.find(c, (count > 0) ? pos + 1 : pos);
		if (pos == std::string::npos)
		{
			return std::string::npos;
		}
		++count;
		if (pos == str.length() - 1)
		{
			return std::string::npos;
		}
	}

	return pos;
}

#endif // STRING_EXT_H