summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/pstring.cpp
blob: f25b669c8addda07b3e52e2a82486f026ed0fea1 (plain) (blame)
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
// license:GPL-2.0+
// copyright-holders:Couriersud

#include "pstring.h"
#include "palloc.h"

#include <algorithm>
#include <atomic>
#include <stack>

template<typename F>
int pstring_t<F>::compare(const pstring_t &right) const noexcept
{
#if 0
	return m_str.compare(right.m_str);
#else
	auto si = this->begin();
	auto ri = right.begin();
	const auto se = this->end();
	const auto re = right.end();

	while (si != se && ri != re && *si == *ri)
	{
		++ri;
		++si;
	}

	if (si != se && ri != re)
		return plib::narrow_cast<int>(*si) - plib::narrow_cast<int>(*ri);
	if (si != se)
		return 1;
	if (ri != re)
		return -1;
	return 0;
#endif
}

template<typename F>
pstring_t<F> pstring_t<F>::substr(size_type start, size_type nlen) const
{
	pstring_t ret;
	auto ps = begin();
	while (ps != end() && start > 0)
	{
		++ps;
		--start;
	}
	//FIXME: throw ?
	if (ps != end())
	{
		auto pe = ps;
		while (pe != end() && nlen > 0)
		{
			++pe;
			--nlen;
		}
		ret.m_str.assign(ps.p, pe.p);
	}
	return ret;
}

template<typename F>
pstring_t<F> pstring_t<F>::substr(size_type start) const
{
	pstring_t ret;
	auto ps = begin();
	while (ps != end() && start > 0)
	{
		++ps;
		--start;
	}
	//FIXME: throw ?
	if (ps != end())
	{
		ret.m_str.assign(ps.p, end().p);
	}
	return ret;
}

template<typename F>
typename pstring_t<F>::size_type pstring_t<F>::find(const pstring_t &search, size_type start) const noexcept
{
	auto istart = std::next(begin(), static_cast<difference_type>(start));
	for (; istart != end(); ++istart)
	{
		auto itc(istart);
		auto cmp = search.begin();
		while (itc != end() && cmp != search.end() && *itc == *cmp)
		{
			++itc;
			++cmp;
		}
		if (cmp == search.end())
			return start;
		++start;
	}
	return npos;
}

template<typename F>
typename pstring_t<F>::size_type pstring_t<F>::find(code_t search, size_type start) const noexcept
{
	auto i = std::next(begin(), static_cast<difference_type>(start));
	for (; i != end(); ++i)
	{
		if (*i == search)
			return start;
		++start;
	}
	return npos;
}

// ----------------------------------------------------------------------------------------
// template stuff ...
// ----------------------------------------------------------------------------------------

template struct pstring_t<pu8_traits>;
template struct pstring_t<putf8_traits>;
template struct pstring_t<putf16_traits>;
template struct pstring_t<putf32_traits>;
template struct pstring_t<pwchar_traits>;