File: vmallocator.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 (143 lines) | stat: -rw-r--r-- 2,644 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
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
// Stuff that can't be included in vmallocator.h

#include "globalincs/vmallocator.h"

std::locale SCP_default_locale("");

void SCP_tolower(SCP_string &str)
{
	std::for_each(str.begin(), str.end(), [](char &ch) { ch = SCP_tolower(ch); });
}

void SCP_toupper(SCP_string &str)
{
	std::for_each(str.begin(), str.end(), [](char &ch) { ch = SCP_toupper(ch); });
}

void SCP_tolower(char *str)
{
	for (; *str != '\0'; ++str)
		*str = SCP_tolower(*str);
}

void SCP_toupper(char *str)
{
	for (; *str != '\0'; ++str)
		*str = SCP_toupper(*str);
}

// in-place modification of string to title case; this is a bit naive but it is good enough for the time being
void SCP_totitle(char *str)
{
	bool prev_alpha = false;

	for (; *str != '\0'; ++str)
	{
		bool this_alpha = (*str >= 'a' && *str <= 'z') || (*str >= 'A' && *str <= 'Z');

		if (this_alpha)
		{
			if (prev_alpha)
				*str = SCP_tolower(*str);
			else
				*str = SCP_toupper(*str);
		}

		prev_alpha = this_alpha;
	}
}

// in-place modification of string to title case; same naive algorithm as above
void SCP_totitle(SCP_string &str)
{
	SCP_string title_str;
	bool prev_alpha = false;

	std::for_each(str.begin(), str.end(), [&prev_alpha](char &ch)
	{
		bool this_alpha = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');

		if (this_alpha)
		{
			if (prev_alpha)
				ch = SCP_tolower(ch);
			else
				ch = SCP_toupper(ch);
		}

		prev_alpha = this_alpha;
	});
}

bool SCP_truncate(SCP_string &str, size_t len)
{
	if (str.length() > len)
	{
		str.resize(len);
		return true;
	}
	else
		return false;
}

bool SCP_trim(SCP_string& str)
{
	auto start = str.find_first_not_of(" \t\r\n");
	auto end = str.find_last_not_of(" \t\r\n");
	if (start == SCP_string::npos) {
		str.clear();
		return true;
	}
	if (start > 0 || end < str.length() - 1) {
		str = str.substr(start, end - start + 1);
		return true;
	}
	return false;
}

bool lcase_equal(const SCP_string& _Left, const SCP_string& _Right)
{
	if (_Left.size() != _Right.size())
		return false;

	auto l_it = _Left.cbegin();
	auto r_it = _Right.cbegin();

	while (l_it != _Left.cend())
	{
		if (SCP_tolower(*l_it) != SCP_tolower(*r_it))
			return false;

		++l_it;
		++r_it;
	}

	return true;
}

bool lcase_lessthan(const SCP_string& _Left, const SCP_string& _Right)
{
	auto l_it = _Left.cbegin();
	auto r_it = _Right.cbegin();

	while (true)
	{
		if (l_it == _Left.cend())
			return (r_it != _Right.cend());
		else if (r_it == _Right.cend())
			return false;

		auto lch = SCP_tolower(*l_it);
		auto rch = SCP_tolower(*r_it);

		if (lch < rch)
			return true;
		else if (lch > rch)
			return false;

		++l_it;
		++r_it;
	}

	return true;
}