summaryrefslogtreecommitdiff
path: root/lib/message/mu-labels.cc
blob: 9b90abae50e22db0de99a254c9af797041cf96b2 (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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
** Copyright (C) 2025 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program 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 3, or (at your option) any
** later version.
**
** This program 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 this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/


#include "mu-labels.hh"
#include <set>

using namespace Mu;
using namespace Mu::Labels;

Result<void>
Mu::Labels::validate_label(const std::string &label)
{
	if (label.empty())
		return Err(Error{Error::Code::InvalidArgument,
				 "labels cannot be empty"});
	else if (!g_utf8_validate(label.c_str(), label.size(), {}))
		// perhaps put hex in err str?
		return Err(Error{Error::Code::InvalidArgument,
				"labels must be valid UTF-8"});

	const auto cstr{label.c_str()};

	// labels must be at least two characters and not start with a
	// dash. these limitations are there to avoid confusion with
	// command-line parameters.
	if (cstr[0] == '-' || cstr[0] == '+')
		return Err(Error{Error::Code::InvalidArgument,
				 "labels cannot start with '+' or '-' ({})", label});

	for (auto cur = cstr; cur && *cur; cur = g_utf8_next_char(cur)) { // NOLINT

		const gunichar uc = g_utf8_get_char(cur);
		if (g_unichar_isalnum(uc))
			continue; // alphanum is okay

		if (::iscntrl(uc))
			return Err(Error{Error::Code::InvalidArgument,
					"control character {} not allowed in label",
					static_cast<int>(uc)});
		if (::isblank(uc))
			return Err(Error{Error::Code::InvalidArgument,
					"blank character {} not allowed in label",
					static_cast<int>(uc)});
		if (uc == '"' || uc == '\'' || uc == '`' || uc == ',' ||
		    uc == '\\' || uc == '/' || uc == '$')
			return Err(Error{Error::Code::InvalidArgument,
					"character '{}' not allowed in label", uc});
	}

	return Ok();
}

Result<DeltaLabel>
Mu::Labels::parse_delta_label(const std::string &expr)
{
	if (expr.size() < 1)
		return Err(Error{Error::Code::InvalidArgument,
				 "empty labels are invalid"});
	const auto cstr{expr.c_str()};

	// first char; either '+' or '-'
	if (cstr[0] != '+' && cstr[0] != '-')
		return Err(Error{Error::Code::InvalidArgument,
				 "invalid label expression '{}'; "
				 "must start with '+' or '-'",
				 expr});
	Delta delta{cstr[0] == '+' ? Delta::Add : Delta::Remove};
	std::string label{expr.substr(1)};

	if (const auto res = validate_label(label); !res)
		return Err(res.error());

	return Ok(DeltaLabel{std::move(delta), std::move(label)});
}

Result<DeltaLabelVec>
Mu::Labels::parse_delta_labels(const std::string& exprs,
			       const std::string sepa)
{

	DeltaLabelVec deltas{};
	for (const auto& expr: split(exprs, sepa)) {
		if (auto delta = parse_delta_label(expr); !delta)
			return Err(std::move(delta.error()));
		else
			deltas.emplace_back(*delta);
	}

	return Ok(std::move(deltas));
}

struct cmp_delta_label { // can not yet be a λ in C++17
	bool operator()(const DeltaLabel& dl1, const DeltaLabel& dl2) const {
		return dl1.second < dl2.second;
	}
};
std::pair<LabelVec, DeltaLabelVec>
Mu::Labels::updated_labels(const LabelVec& labels, const DeltaLabelVec& deltas)
{
	// quite complicated!

	// First, the delta; put in a set for uniqueness; and use a special
	// comparison operator so "add" and "remove" deltas are considered "the same"
	// for the set; then fill the set from the end of the deltas vec to the beginning,
	// so "the last one wins", as we want.

	// only one change per label, last one wins
	std::set<DeltaLabel, cmp_delta_label> working_deltas{
		deltas.rbegin(), deltas.rend()
	};

	// working set of lables; we start with _all_ (uniquified)
	std::set<std::string> working_labels{labels.begin(), labels.end()};

	// keep track of the deltas that actually changed something (ie.
	// removing a non-existing label or adding an already existing one is
	// not a change.)
	DeltaLabelVec effective_deltas;

	// now check each of our "workin deltas", apply on the working_labels, and
	// if they changed anything, add to 'effectivc_deltas
	for (auto&  delta: working_deltas) {
		switch (delta.first) {
		case Delta::Add:
			// add to the _effective_ deltas if the element wasn't
			// there before.
			if (working_labels.emplace(delta.second).second)
				effective_deltas.emplace_back(std::move(delta));
			break;
		case Delta::Remove:
			// add to the _effective_ deltas if the element was
			// actually removed.
			if (working_labels.erase(delta.second) > 0U)
				effective_deltas.emplace_back(std::move(delta));
			break;
		default:
			// can't have Neutral here.
			throw std::runtime_error("invalid delta");
		}
	}


	return {{ working_labels.begin(), working_labels.end()}, effective_deltas};
}



#ifdef BUILD_TESTS

#include "utils/mu-test-utils.hh"

static void
test_parse_delta_label()
{
	{
		const auto expr = parse_delta_label("+foo");
		assert_valid_result(expr);
		g_assert_true(expr->first == Delta::Add);
		assert_equal(expr->second, "foo");
	}

	{
		const auto expr = parse_delta_label("-bar@cuux");
		assert_valid_result(expr);
		g_assert_true(expr->first == Delta::Remove);
		assert_equal(expr->second, "bar@cuux");
	}

	g_assert_false(!!parse_delta_label("ravenking"));
	g_assert_false(!!parse_delta_label("+norrell strange"));
	g_assert_true(!!parse_delta_label("-😨"));
}


static void
test_parse_delta_labels()
{
	{
		const auto deltas = parse_delta_labels("+foo,-bar@cuux",",");
		assert_valid_result(deltas);

		g_assert_true(deltas->at(0).first == Delta::Add);
		assert_equal(deltas->at(0).second, "foo");

		g_assert_true(deltas->at(1).first == Delta::Remove);
		assert_equal(deltas->at(1).second, "bar@cuux");
	}

	{
		const auto expr = parse_delta_labels("+foo @boo🐂", " ");
		g_assert_false(!!expr);
	}
}



static void
test_validate_label()
{
	g_assert_true(!!validate_label("ravenking"));
	g_assert_true(!!validate_label("@raven+king"));
	g_assert_true(!!validate_label("operation:mindcrime"));
	g_assert_true(!!validate_label("😨"));
	g_assert_true(!!validate_label("foo%bar+1"));

	g_assert_false(!!validate_label("norrell strange"));
	g_assert_false(!!validate_label(""));
	g_assert_false(!!validate_label("+"));
	g_assert_false(!!validate_label("-"));
	g_assert_false(!!validate_label("foo$bar"));
	g_assert_false(!!validate_label("foo,bar"));
	g_assert_false(!!validate_label("foo`bar"));
	g_assert_false(!!validate_label("\"quoted\""));
}

static void
test_updated_labels()
{
	const auto assert_eq=[](const LabelVec& labels, const DeltaLabelVec& deltas,
				const LabelVec& exp_labels, const DeltaLabelVec& exp_deltas) {

		const auto& [res_labels, res_deltas] = updated_labels(labels, deltas);

		assert_equal_seq_str(res_labels, exp_labels);
		g_assert_cmpuint(res_deltas.size(), ==, exp_deltas.size());
		for (size_t i{}; i != res_deltas.size(); ++i) {
			g_assert_true(res_deltas[i].first == exp_deltas[i].first);
			assert_equal(res_deltas[i].second, exp_deltas[i].second);
		}
	};

	const auto delta_labels = [](std::initializer_list<std::string> strs)->DeltaLabelVec {
		DeltaLabelVec deltas;
		std::transform(strs.begin(), strs.end(), std::back_inserter(deltas),
			       [](auto str) {
				       const auto res = parse_delta_label(str);
				       assert_valid_result(res);
				       return *res;
			       });
		return deltas;
	};

	assert_eq({"foo", "bar", "cuux"}, delta_labels({"+fnorb", "+bar", "-bar", "+bar", "-cuux"}),
		  {"bar", "fnorb", "foo"}, delta_labels({"-cuux", "+fnorb"}));

	assert_eq({}, delta_labels({"-fnorb", "-fnorb", "+whiteward", "+altesia", "+fnorb"}),
		  {"altesia", "fnorb", "whiteward"}, delta_labels({"+altesia", "+fnorb", "+whiteward"}));

	assert_eq({"piranesi", "hyperion", "mordor", "piranesi"}, delta_labels({}),
		  {"hyperion", "mordor", "piranesi"}, delta_labels({}));
}

int
main(int argc, char* argv[])
{
	mu_test_init(&argc, &argv);

	g_test_add_func("/message/labels/parse-delta-label", test_parse_delta_label);
	g_test_add_func("/message/labels/parse-delta-labels", test_parse_delta_labels);
	g_test_add_func("/message/labels/validate-label", test_validate_label);
	g_test_add_func("/message/labels/updated-labels", test_updated_labels);

	return g_test_run();
}
#endif /*BUILD_TESTS*/