summaryrefslogtreecommitdiff
path: root/icespider/common/pathparts.cpp
blob: 654081a5cd33ef930dbd2c38e05eb44f70ebc8e3 (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
#include "pathparts.h"
#include <boost/algorithm/string/compare.hpp>
#include <boost/algorithm/string/find_iterator.hpp>
#include <boost/algorithm/string/finder.hpp>
#include <string>

namespace ba = boost::algorithm;

namespace IceSpider {
	const auto slash = ba::first_finder("/", ba::is_equal());
	Path::Path(const std::string_view & p) : path(p)
	{
		auto relp = p.substr(1);
		if (relp.empty()) {
			return;
		}
		for (auto pi = ba::make_split_iterator(relp, slash); pi != decltype(pi)(); ++pi) {
			std::string_view pp {pi->begin(), pi->end()};
			if (pp.front() == '{' && pp.back() == '}') {
				parts.push_back(std::make_unique<PathParameter>(pp));
			}
			else {
				parts.push_back(std::make_unique<PathLiteral>(pp));
			}
		}
	}

	std::size_t
	Path::pathElementCount() const
	{
		return parts.size();
	}

	PathLiteral::PathLiteral(const std::string_view & p) : value(p) { }

	bool
	PathLiteral::matches(const std::string_view & v) const
	{
		return value == v;
	}

	PathParameter::PathParameter(const std::string_view & s) : name(s.substr(1, s.length() - 2)) { }

	bool
	PathParameter::matches(const std::string_view &) const
	{
		return true;
	}
}