summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/pstonum.h
blob: c4bf692d177689f6af8175d55d539b50bc492385 (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
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
// license:GPL-2.0+
// copyright-holders:Couriersud

#ifndef PSTONUM_H_
#define PSTONUM_H_

///
/// \file pstonum.h
///

#include "pconfig.h"
#include "pexception.h"
#include "pgsl.h"
#include "pmath.h" // for pstonum
#include "pstring.h"

#include <algorithm>
#include <initializer_list>
#include <iostream>
#include <locale>
#include <sstream>

namespace plib
{
	// ----------------------------------------------------------------------------------------
	// number conversions
	// ----------------------------------------------------------------------------------------

	template <typename T, typename S>
	T pstonum_locale(const std::locale &loc, const S &arg, std::size_t *idx)
	{
		std::stringstream ss;
		ss.imbue(loc);
		ss << arg;
		auto len(ss.tellp());
		T x(constants<T>::zero());
		if (ss >> x)
		{
			auto pos(ss.tellg());
			if (pos == decltype(pos)(-1))
				pos = len;
			*idx = narrow_cast<std::size_t>(pos);
		}
		else
			*idx = constants<std::size_t>::zero();
		return x;
	}

	template <typename T, typename E = void>
	struct pstonum_helper;

	template<typename T>
	struct pstonum_helper<T, std::enable_if_t<plib::is_integral<T>::value && plib::is_signed<T>::value>>
	{
		template <typename S>
		long long operator()(std::locale loc, const S &arg, std::size_t *idx)
		{
			//return std::stoll(arg, idx);
			return pstonum_locale<long long>(loc, arg, idx);
		}
	};

	template<typename T>
	struct pstonum_helper<T, std::enable_if_t<plib::is_integral<T>::value && !plib::is_signed<T>::value>>
	{
		template <typename S>
		unsigned long long operator()(std::locale loc, const S &arg, std::size_t *idx)
		{
			//return std::stoll(arg, idx);
			return pstonum_locale<unsigned long long>(loc, arg, idx);
		}
	};

	template<typename T>
	struct pstonum_helper<T, std::enable_if_t<std::is_floating_point<T>::value>>
	{
		template <typename S>
		long double operator()(std::locale loc, const S &arg, std::size_t *idx)
		{
			return pstonum_locale<long double>(loc, arg, idx);
		}
	};

#if PUSE_FLOAT128
	template<>
	struct pstonum_helper<FLOAT128>
	{
		// FIXME: use strtoflt128 from quadmath.h
		template <typename S>
		FLOAT128 operator()(std::locale loc, const S &arg, std::size_t *idx)
		{
			return narrow_cast<FLOAT128>(pstonum_locale<long double>(loc, arg, idx));
		}
	};
#endif

	template<typename T, typename S>
	T pstonum(const S &arg, const std::locale &loc = std::locale::classic()) noexcept(false)
	{
		decltype(arg.c_str()) cstr = arg.c_str();
		std::size_t idx(0);
		auto ret = pstonum_helper<T>()(loc, cstr, &idx);
		using ret_type = decltype(ret);
		if (ret >= narrow_cast<ret_type>(plib::numeric_limits<T>::lowest())
			&& ret <= narrow_cast<ret_type>(plib::numeric_limits<T>::max()))
		{
			if (cstr[idx] != 0)
				throw pexception(pstring("Continuation after numeric value ends: ") + pstring(cstr));
		}
		else
		{
			throw pexception(pstring("Out of range: ") + pstring(cstr));
		}
		return narrow_cast<T>(ret);
	}

	template<typename R, typename T>
	R pstonum_ne(const T &str, bool &err, std::locale loc = std::locale::classic()) noexcept
	{
		try
		{
			err = false;
			return pstonum<R>(str, loc);
		}
		catch (...)
		{
			err = true;
			return R(0);
		}
	}

	template<typename R, typename T>
	R pstonum_ne_def(const T &str, R def, std::locale loc = std::locale::classic()) noexcept
	{
		try
		{
			return pstonum<R>(str, loc);
		}
		catch (...)
		{
			return def;
		}
	}

} // namespace plib

#endif // PUTIL_H_