File: string_utils.cpp

package info (click to toggle)
freespace2 25.0.0~rc11%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 47,232 kB
  • sloc: cpp: 657,500; ansic: 22,305; sh: 293; python: 200; makefile: 198; xml: 181
file content (45 lines) | stat: -rw-r--r-- 964 bytes parent folder | download
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
//
//

#include "string_utils.h"


namespace util {

std::vector<std::string> split_string(const std::string& s, char delim)
{
	std::vector<std::string> elems;
	split_string(s, delim, std::back_inserter(elems));
	return elems;
}

bool isStringOneOf(const std::string& value, const std::vector<std::string>& candidates)
{
	return std::any_of(candidates.begin(), candidates.end(), [&value](const std::string& candidate) {
		return lcase_equal(value, candidate);
	});
}

std::unique_ptr<char[]> unique_copy(const char *str, bool null_if_empty)
{
	size_t len;

	if (str == nullptr || (len = strlen(str), null_if_empty && len == 0))
		return {};

	auto copy = std::make_unique<char[]>(len + 1);
	strcpy(copy.get(), str);
	return copy;
}

SCP_vm_unique_ptr<char> vm_unique_copy(const char *str, bool null_if_empty)
{
	size_t len;

	if (str == nullptr || (len = strlen(str), null_if_empty && len == 0))
		return {};

	return SCP_vm_unique_ptr<char>(vm_strdup(str));
}

}