aboutsummaryrefslogtreecommitdiff
path: root/frontend/strings.cpp
blob: 05c1548523a2aa6d38cd3ddb597a9253f86e4f6e (plain)
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
#include <algorithm>

#include "strings.h"

#include "backend/print.h"

using namespace std;

void str_print(const char * str) {
	lprtf("%s\n", str_wrap(str).c_str());
}

string str_wrap(const char * str) {
	string out;

	for (; *str != '\0'; str++) {
		if (str[0] == '\n') {
			if (str[1] == '\n') {
				out += "\n\n";
				str++;
			} else {
				out += " ";
			}
			continue;
		}

		out += *str;
	}

	return out;
}

vector<string> str_split(const string & src, const string & delim) {
	if (src.size() == 0) return {};
	vector<string> out;
	size_t start = 0;
	size_t end = src.find(delim);

	while (end != string::npos) {
		out.push_back(src.substr(start, end - start));
		start = end + delim.length();
		end = src.find(delim, start);
	}
	out.push_back(src.substr(start));

	return out;
}

string str_lower(const string & input) {
	string out = input;
	transform(out.begin(), out.end(), out.begin(), [](unsigned char c){ return tolower(c); });
	return out;
}

string str_title(const string & input) {
	if (input.size() == 0) return "";
	string out = str_lower(input);
	out[0] = toupper(out[0]);
	return out;
}

string str_consume_arg(string & argv) {
	const char * delim = " \t";
	size_t start = argv.find_first_not_of(delim);
	if (start == string::npos) start = 0;
	size_t end = argv.find_first_of(delim, start);
	if (end == string::npos) end = argv.size();
	string out = argv.substr(start, end - start);
	size_t nextarg = argv.find_first_not_of(delim, end);
	if (nextarg == string::npos) nextarg = end;
	argv = argv.substr(nextarg);
	return out;
}