mirror of
https://github.com/holub/mame
synced 2025-04-23 00:39:36 +03:00
pstring, pdynlib, pfmtlog refactoring :
pstring: - added support for UTF16LE to pstring. - renamed size() to mem_t_size() - renmaed len() to length() - added size() == length() - added empty() - added simple compare() pfmtlog: - Simplified pfmtlog, added more c++ pdynlib: - add a dynproc type to dynlib to wrap dynamic library calls. various: - fix two coverty scan issue. - various clang warnings fixed. (nw)
This commit is contained in:
parent
9e78adc115
commit
58aa97913f
@ -27,7 +27,7 @@ namespace netlist
|
||||
NETLIB_UPDATEI()
|
||||
{
|
||||
/* use pstring::sprintf, it is a LOT faster */
|
||||
m_writer.writeline(plib::pfmt("{1} {2}").e(netlist().time().as_double(),".9").e(static_cast<double>(m_I())));
|
||||
m_writer.writeline(plib::pfmt("{1:.9} {2}").e(netlist().time().as_double()).e(static_cast<double>(m_I())));
|
||||
}
|
||||
|
||||
NETLIB_RESETI() { }
|
||||
@ -46,7 +46,7 @@ namespace netlist
|
||||
|
||||
NETLIB_UPDATEI()
|
||||
{
|
||||
m_writer.writeline(plib::pfmt("{1} {2}").e(netlist().time().as_double(),".9").e(static_cast<double>(m_I() - m_I2())));
|
||||
m_writer.writeline(plib::pfmt("{1:.9} {2}").e(netlist().time().as_double()).e(static_cast<double>(m_I() - m_I2())));
|
||||
}
|
||||
|
||||
NETLIB_RESETI() { }
|
||||
|
@ -347,8 +347,8 @@ void truthtable_parser::parseline(unsigned cur, std::vector<pstring> list,
|
||||
{
|
||||
// cutoff previous inputs and outputs for ignore
|
||||
if (m_outs[nstate] != m_outs.mask() && m_outs[nstate] != val)
|
||||
nl_exception(plib::pfmt("Error in truthtable: State {1} already set, {2} != {3}\n")
|
||||
.x(nstate.as_uint(),"04")(m_outs[nstate])(val) );
|
||||
nl_exception(plib::pfmt("Error in truthtable: State {1:04} already set, {2} != {3}\n")
|
||||
.x(nstate.as_uint())(m_outs[nstate])(val) );
|
||||
m_outs.set(nstate, val);
|
||||
for (std::size_t j=0; j<m_NO; j++)
|
||||
m_timing[nstate * m_NO + j] = timing_index[j];
|
||||
|
@ -273,7 +273,7 @@ void setup_t::register_frontier(const pstring &attach, const double r_IN, const
|
||||
|
||||
void setup_t::register_param(const pstring ¶m, const double value)
|
||||
{
|
||||
register_param(param, plib::pfmt("{1}").e(value,".9"));
|
||||
register_param(param, plib::pfmt("{1:.9}").e(value));
|
||||
}
|
||||
|
||||
void setup_t::register_param(const pstring ¶m, const pstring &value)
|
||||
@ -820,7 +820,7 @@ void setup_t::model_parse(const pstring &model_in, detail::model_map_t &map)
|
||||
if (!remainder.endsWith(")"))
|
||||
log().fatal(MF_1_MODEL_ERROR_1, model);
|
||||
// FIMXE: Not optimal
|
||||
remainder = remainder.left(remainder.len() - 1);
|
||||
remainder = remainder.left(remainder.length() - 1);
|
||||
|
||||
std::vector<pstring> pairs(plib::psplit(remainder," ", true));
|
||||
for (pstring &pe : pairs)
|
||||
@ -852,7 +852,7 @@ nl_double setup_t::model_value(detail::model_map_t &map, const pstring &entity)
|
||||
pstring tmp = model_value_str(map, entity);
|
||||
|
||||
nl_double factor = NL_FCONST(1.0);
|
||||
auto p = std::next(tmp.begin(), static_cast<pstring::difference_type>(tmp.len() - 1));
|
||||
auto p = std::next(tmp.begin(), static_cast<pstring::difference_type>(tmp.length() - 1));
|
||||
switch (*p)
|
||||
{
|
||||
case 'M': factor = 1e6; break;
|
||||
@ -868,7 +868,7 @@ nl_double setup_t::model_value(detail::model_map_t &map, const pstring &entity)
|
||||
log().fatal(MF_1_UNKNOWN_NUMBER_FACTOR_IN_1, entity);
|
||||
}
|
||||
if (factor != NL_FCONST(1.0))
|
||||
tmp = tmp.left(tmp.len() - 1);
|
||||
tmp = tmp.left(tmp.length() - 1);
|
||||
return tmp.as_double() * factor;
|
||||
}
|
||||
|
||||
@ -997,12 +997,12 @@ bool source_t::parse(const pstring &name)
|
||||
|
||||
std::unique_ptr<plib::pistream> source_string_t::stream(const pstring &name)
|
||||
{
|
||||
return plib::make_unique_base<plib::pistream, plib::pimemstream>(m_str.c_str(), m_str.len());
|
||||
return plib::make_unique_base<plib::pistream, plib::pimemstream>(m_str.c_str(), m_str.mem_t_size());
|
||||
}
|
||||
|
||||
std::unique_ptr<plib::pistream> source_mem_t::stream(const pstring &name)
|
||||
{
|
||||
return plib::make_unique_base<plib::pistream, plib::pimemstream>(m_str.c_str(), m_str.len());
|
||||
return plib::make_unique_base<plib::pistream, plib::pimemstream>(m_str.c_str(), m_str.mem_t_size());
|
||||
}
|
||||
|
||||
std::unique_ptr<plib::pistream> source_file_t::stream(const pstring &name)
|
||||
|
@ -35,6 +35,35 @@ private:
|
||||
void *m_lib;
|
||||
};
|
||||
|
||||
template <typename R, typename... Args>
|
||||
class dynproc
|
||||
{
|
||||
public:
|
||||
using calltype = R(*) (Args... args);
|
||||
|
||||
dynproc() : m_sym(nullptr) { }
|
||||
|
||||
dynproc(dynlib &dl, const pstring &name)
|
||||
{
|
||||
m_sym = dl.getsym<calltype>(name);
|
||||
}
|
||||
|
||||
void load(dynlib &dl, const pstring &name)
|
||||
{
|
||||
m_sym = dl.getsym<calltype>(name);
|
||||
}
|
||||
|
||||
R operator ()(Args&&... args) const
|
||||
{
|
||||
return m_sym(std::forward<Args>(args)...);
|
||||
//return m_sym(args...);
|
||||
}
|
||||
|
||||
bool resolved() { return m_sym != nullptr; }
|
||||
private:
|
||||
calltype m_sym;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* PSTRING_H_ */
|
||||
|
130
src/lib/netlist/plib/pfmtlog.cpp
Normal file → Executable file
130
src/lib/netlist/plib/pfmtlog.cpp
Normal file → Executable file
@ -12,8 +12,8 @@
|
||||
#include <cstdlib>
|
||||
#include <cstdarg>
|
||||
#include <algorithm>
|
||||
|
||||
//* FIXME: remove cstring, replace with pstring */
|
||||
#include <locale>
|
||||
#include <iostream>
|
||||
|
||||
namespace plib {
|
||||
|
||||
@ -21,115 +21,75 @@ plog_dispatch_intf::~plog_dispatch_intf()
|
||||
{
|
||||
}
|
||||
|
||||
pfmt::pfmt(const pstring &fmt)
|
||||
: m_str(m_str_buf), m_allocated(0), m_arg(0)
|
||||
{
|
||||
std::size_t l = fmt.size() + 1;
|
||||
if (l>sizeof(m_str_buf))
|
||||
{
|
||||
m_allocated = 2 * l;
|
||||
m_str = palloc_array<char>(2 * l);
|
||||
}
|
||||
std::copy(fmt.c_str(), fmt.c_str() + l, m_str);
|
||||
}
|
||||
|
||||
pfmt::~pfmt()
|
||||
{
|
||||
if (m_allocated > 0)
|
||||
pfree_array(m_str);
|
||||
}
|
||||
|
||||
void pfmt::format_element(const char *f, const char *l, const char *fmt_spec, ...)
|
||||
pfmt &pfmt::format_element(const char *l, const unsigned cfmt_spec, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt_spec);
|
||||
char fmt[30] = "%";
|
||||
char search[10] = "";
|
||||
char buf[2048];
|
||||
va_start(ap, cfmt_spec);
|
||||
pstring fmt("%");
|
||||
char buf[2048]; // FIXME
|
||||
std::size_t sl;
|
||||
|
||||
m_arg++;
|
||||
std::size_t sl = static_cast<std::size_t>(sprintf(search, "{%d:", m_arg));
|
||||
char *p = strstr(m_str, search);
|
||||
if (p == nullptr)
|
||||
|
||||
pstring search("{");
|
||||
search += plib::to_string(m_arg);
|
||||
sl = search.length();
|
||||
|
||||
auto p = m_str.find(search + ":");
|
||||
sl++; // ":"
|
||||
if (p == pstring::npos) // no further specifiers
|
||||
{
|
||||
sl = static_cast<std::size_t>(sprintf(search, "{%d}", m_arg));
|
||||
p = strstr(m_str, search);
|
||||
if (p == nullptr)
|
||||
p = m_str.find(search + "}");
|
||||
if (p == pstring::npos) // not found try default
|
||||
{
|
||||
sl = 2;
|
||||
p = strstr(m_str, "{}");
|
||||
p = m_str.find("{}");
|
||||
}
|
||||
if (p==nullptr)
|
||||
if (p == pstring::npos)
|
||||
{
|
||||
sl=1;
|
||||
p = strstr(m_str, "{");
|
||||
if (p != nullptr)
|
||||
p = m_str.find("{");
|
||||
if (p != pstring:: npos)
|
||||
{
|
||||
char *p1 = strstr(p, "}");
|
||||
if (p1 != nullptr)
|
||||
auto p1 = m_str.find("}", p);
|
||||
if (p1 != pstring::npos)
|
||||
{
|
||||
sl = static_cast<std::size_t>(p1 - p + 1);
|
||||
strncat(fmt, p+1, static_cast<std::size_t>(p1 - p - 2));
|
||||
sl = p1 - p + 1;
|
||||
fmt += m_str.substr(p+1, p1 - p - 2);
|
||||
}
|
||||
else
|
||||
strcat(fmt, f);
|
||||
}
|
||||
else
|
||||
strcat(fmt, f);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
char *p1 = strstr(p, "}");
|
||||
if (p1 != nullptr)
|
||||
auto p1 = m_str.find("}", p);
|
||||
if (p1 != pstring::npos)
|
||||
{
|
||||
sl = static_cast<std::size_t>(p1 - p + 1);
|
||||
if (m_arg>=10)
|
||||
strncat(fmt, p+4, static_cast<std::size_t>(p1 - p - 4));
|
||||
else
|
||||
strncat(fmt, p+3, static_cast<std::size_t>(p1 - p - 3));
|
||||
sl = p1 - p + 1;
|
||||
fmt += ((m_arg>=10) ? m_str.substr(p+4, p1 - p - 4) : m_str.substr(p+3, p1 - p - 3));
|
||||
}
|
||||
}
|
||||
pstring::code_t pend = fmt.at(fmt.length() - 1);
|
||||
if (pstring("duxo").find(cfmt_spec) != pstring::npos)
|
||||
{
|
||||
if (pstring("duxo").find(pend) == pstring::npos)
|
||||
fmt += (pstring(l, pstring::UTF8) + cfmt_spec);
|
||||
else
|
||||
strcat(fmt, f);
|
||||
fmt = fmt.left(fmt.length() - 1) + pstring(l, pstring::UTF8) + fmt.right(1);
|
||||
}
|
||||
strcat(fmt, l);
|
||||
char *pend = fmt + strlen(fmt) - 1;
|
||||
if (strchr("fge", *fmt_spec) != nullptr)
|
||||
else if (pstring("fge").find(cfmt_spec) != pstring::npos)
|
||||
{
|
||||
if (strchr("fge", *pend) == nullptr)
|
||||
strcat(fmt, fmt_spec);
|
||||
}
|
||||
else if (strchr("duxo", *fmt_spec) != nullptr)
|
||||
{
|
||||
if (strchr("duxo", *pend) == nullptr)
|
||||
strcat(fmt, fmt_spec);
|
||||
if (pstring("fge").find(pend) == pstring::npos)
|
||||
fmt += cfmt_spec;
|
||||
}
|
||||
else
|
||||
strcat(fmt, fmt_spec);
|
||||
std::size_t nl = static_cast<std::size_t>(vsprintf(buf, fmt, ap));
|
||||
if (p != nullptr)
|
||||
{
|
||||
// check room
|
||||
std::size_t new_size = static_cast<std::size_t>(p - m_str) + nl + strlen(p) + 1 - sl;
|
||||
if (new_size > m_allocated)
|
||||
{
|
||||
std::size_t old_alloc = std::max(m_allocated, sizeof(m_str_buf));
|
||||
if (m_allocated < old_alloc)
|
||||
m_allocated = old_alloc;
|
||||
while (new_size > m_allocated)
|
||||
m_allocated *= 2;
|
||||
char *np = palloc_array<char>(m_allocated);
|
||||
std::copy(m_str, m_str + old_alloc, np);
|
||||
p = np + (p - m_str);
|
||||
if (m_str != m_str_buf)
|
||||
pfree_array(m_str);
|
||||
m_str = np;
|
||||
}
|
||||
// Make room
|
||||
//memmove(p+nl, p+sl, strlen(p) + 1 - sl);
|
||||
std::copy_backward(p + sl, p + strlen(p) + 1, p + nl + strlen(p) + 1 - sl);
|
||||
std::copy(buf, buf + nl, p);
|
||||
}
|
||||
fmt += cfmt_spec;
|
||||
vsprintf(buf, fmt.c_str(), ap);
|
||||
if (p != pstring::npos)
|
||||
m_str = m_str.substr(0, p) + pstring(buf, pstring::UTF8) + m_str.substr(p + sl);
|
||||
va_end(ap);
|
||||
return *this;
|
||||
}
|
||||
|
||||
}
|
||||
|
205
src/lib/netlist/plib/pfmtlog.h
Normal file → Executable file
205
src/lib/netlist/plib/pfmtlog.h
Normal file → Executable file
@ -25,15 +25,20 @@ P_ENUM(plog_level,
|
||||
template <typename T>
|
||||
struct ptype_traits_base
|
||||
{
|
||||
static T cast(T x) { return x; }
|
||||
static const T cast(const T &x) { return x; }
|
||||
static const bool is_signed = std::numeric_limits<T>::is_signed;
|
||||
static const char *size_spec() { return ""; }
|
||||
static char32_t fmt_spec() { return 'u'; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct ptype_traits_base<bool>
|
||||
{
|
||||
static unsigned int cast(bool x) { return static_cast<unsigned int>(x); }
|
||||
static unsigned int cast(bool &x) { return static_cast<unsigned int>(x); }
|
||||
static unsigned int cast(const bool &x) { return static_cast<unsigned int>(x); }
|
||||
static const bool is_signed = std::numeric_limits<bool>::is_signed;
|
||||
static const char *size_spec() { return ""; }
|
||||
static char32_t fmt_spec() { return 'u'; }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
@ -42,143 +47,155 @@ struct ptype_traits;
|
||||
template<>
|
||||
struct ptype_traits<bool> : ptype_traits_base<bool>
|
||||
{
|
||||
static const char *size_specifier() { return ""; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<char> : ptype_traits_base<char>
|
||||
{
|
||||
static const char *size_specifier() { return "h"; }
|
||||
static const char *size_spec() { return "h"; }
|
||||
static char32_t fmt_spec() { return is_signed ? 'd' : 'u'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<short> : ptype_traits_base<short>
|
||||
{
|
||||
static const char *size_specifier() { return "h"; }
|
||||
static const char *size_spec() { return "h"; }
|
||||
static char32_t fmt_spec() { return 'd'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<int> : ptype_traits_base<int>
|
||||
{
|
||||
static const char *size_specifier() { return ""; }
|
||||
static char32_t fmt_spec() { return 'd'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<long> : ptype_traits_base<long>
|
||||
{
|
||||
static const char *size_specifier() { return "l"; }
|
||||
static const char *size_spec() { return "l"; }
|
||||
static char32_t fmt_spec() { return 'd'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<long long> : ptype_traits_base<long long>
|
||||
{
|
||||
static const char *size_specifier() { return "ll"; }
|
||||
static const char *size_spec() { return "ll"; }
|
||||
static char32_t fmt_spec() { return 'd'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<signed char> : ptype_traits_base<signed char>
|
||||
{
|
||||
static const char *size_specifier() { return "h"; }
|
||||
static const char *size_spec() { return "h"; }
|
||||
static char32_t fmt_spec() { return 'd'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<unsigned char> : ptype_traits_base<unsigned char>
|
||||
{
|
||||
static const char *size_specifier() { return "h"; }
|
||||
static const char *size_spec() { return "h"; }
|
||||
static char32_t fmt_spec() { return 'u'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<unsigned short> : ptype_traits_base<unsigned short>
|
||||
{
|
||||
static const char *size_specifier() { return "h"; }
|
||||
static const char *size_spec() { return "h"; }
|
||||
static char32_t fmt_spec() { return 'u'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<unsigned int> : ptype_traits_base<unsigned int>
|
||||
{
|
||||
static const char *size_specifier() { return ""; }
|
||||
static char32_t fmt_spec() { return 'u'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<unsigned long> : ptype_traits_base<unsigned long>
|
||||
{
|
||||
static const char *size_specifier() { return "l"; }
|
||||
static const char *size_spec() { return "l"; }
|
||||
static char32_t fmt_spec() { return 'u'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<unsigned long long> : ptype_traits_base<unsigned long long>
|
||||
{
|
||||
static const char *size_specifier() { return "ll"; }
|
||||
static const char *size_spec() { return "ll"; }
|
||||
static char32_t fmt_spec() { return 'u'; }
|
||||
};
|
||||
|
||||
template <typename P>
|
||||
class pformat_base
|
||||
template<>
|
||||
struct ptype_traits<float> : ptype_traits_base<float>
|
||||
{
|
||||
public:
|
||||
|
||||
P &operator ()(const double x, const char *f = "") { format_element(f, "", "f", x); return static_cast<P &>(*this); }
|
||||
P & e(const double x, const char *f = "") { format_element(f, "", "e", x); return static_cast<P &>(*this); }
|
||||
P & g(const double x, const char *f = "") { format_element(f, "", "g", x); return static_cast<P &>(*this); }
|
||||
|
||||
P &operator ()(const float x, const char *f = "") { format_element(f, "", "f", x); return static_cast<P &>(*this); }
|
||||
P & e(const float x, const char *f = "") { format_element(f, "", "e", x); return static_cast<P &>(*this); }
|
||||
P & g(const float x, const char *f = "") { format_element(f, "", "g", x); return static_cast<P &>(*this); }
|
||||
|
||||
P &operator ()(const char *x, const char *f = "") { format_element(f, "", "s", x); return static_cast<P &>(*this); }
|
||||
P &operator ()(char *x, const char *f = "") { format_element(f, "", "s", x); return static_cast<P &>(*this); }
|
||||
P &operator ()(const void *x, const char *f = "") { format_element(f, "", "p", x); return static_cast<P &>(*this); }
|
||||
P &operator ()(const pstring &x, const char *f = "") { format_element(f, "", "s", x.c_str() ); return static_cast<P &>(*this); }
|
||||
|
||||
template<typename T>
|
||||
P &operator ()(const T x, const char *f = "")
|
||||
{
|
||||
if (ptype_traits<T>::is_signed)
|
||||
format_element(f, ptype_traits<T>::size_specifier(), "d", ptype_traits<T>::cast(x));
|
||||
else
|
||||
format_element(f, ptype_traits<T>::size_specifier(), "u", ptype_traits<T>::cast(x));
|
||||
return static_cast<P &>(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
P &x(const T x, const char *f = "")
|
||||
{
|
||||
format_element(f, ptype_traits<T>::size_specifier(), "x", x);
|
||||
return static_cast<P &>(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
P &o(const T x, const char *f = "")
|
||||
{
|
||||
format_element(f, ptype_traits<T>::size_specifier(), "o", x);
|
||||
return static_cast<P &>(*this);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
~pformat_base() { }
|
||||
virtual void format_element(const char *f, const char *l, const char *fmt_spec, ...) = 0;
|
||||
|
||||
static char32_t fmt_spec() { return 'f'; }
|
||||
};
|
||||
|
||||
class pfmt : public pformat_base<pfmt>
|
||||
template<>
|
||||
struct ptype_traits<double> : ptype_traits_base<double>
|
||||
{
|
||||
static char32_t fmt_spec() { return 'f'; }
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
struct ptype_traits<char *> : ptype_traits_base<char *>
|
||||
{
|
||||
static const char *cast(const char *x) { return x; }
|
||||
static char32_t fmt_spec() { return 's'; }
|
||||
};
|
||||
|
||||
class pfmt
|
||||
{
|
||||
public:
|
||||
explicit pfmt(const pstring &fmt);
|
||||
virtual ~pfmt();
|
||||
explicit pfmt(const pstring &fmt)
|
||||
: m_str(fmt), m_arg(0)
|
||||
{
|
||||
}
|
||||
|
||||
operator pstring() const { return pstring(m_str, pstring::UTF8); }
|
||||
~pfmt()
|
||||
{
|
||||
}
|
||||
|
||||
const char *c_str() { return m_str; }
|
||||
operator pstring() const { return m_str; }
|
||||
pfmt & e(const double &x) {return format_element("", 'e', x); }
|
||||
pfmt & g(const double &x) {return format_element("", 'g', x); }
|
||||
|
||||
pfmt & e(const float &x) {return format_element("", 'e', static_cast<double>(x)); }
|
||||
pfmt & g(const float &x) {return format_element("", 'g', static_cast<double>(x)); }
|
||||
|
||||
pfmt &operator ()(const void *x) {return format_element("", 'p', x); }
|
||||
pfmt &operator ()(const pstring &x) {return format_element("", 's', x.c_str() ); }
|
||||
|
||||
template<typename T>
|
||||
pfmt &operator ()(const T &x)
|
||||
{
|
||||
return format_element(ptype_traits<T>::size_spec(), ptype_traits<T>::fmt_spec(), ptype_traits<T>::cast(x));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
pfmt &operator ()(const T *x)
|
||||
{
|
||||
return format_element(ptype_traits<T *>::size_spec(), ptype_traits<T *>::fmt_spec(), ptype_traits<T *>::cast(x));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
pfmt &x(const T &x)
|
||||
{
|
||||
return format_element(ptype_traits<T>::size_spec(), 'x', x);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
pfmt &o(const T &x)
|
||||
{
|
||||
return format_element(ptype_traits<T>::size_spec(), 'o', x);
|
||||
}
|
||||
protected:
|
||||
void format_element(const char *f, const char *l, const char *fmt_spec, ...) override;
|
||||
|
||||
pfmt &format_element(const char *l, const unsigned fmt_spec, ...);
|
||||
|
||||
private:
|
||||
|
||||
char *m_str;
|
||||
char m_str_buf[256];
|
||||
std::size_t m_allocated;
|
||||
pstring m_str;
|
||||
unsigned m_arg;
|
||||
};
|
||||
|
||||
@ -194,42 +211,18 @@ public:
|
||||
template<bool enabled, typename... Args>
|
||||
void log(const pstring & fmt, Args&&... args) const
|
||||
{
|
||||
if (build_enabled && enabled && m_enabled) (*this)(fmt, std::forward<Args>(args)...);
|
||||
if (build_enabled && enabled && m_enabled)
|
||||
vdowrite(xlog(pfmt(fmt), std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
void operator ()(const pstring &fmt) const
|
||||
template<typename... Args>
|
||||
void operator ()(const pstring &fmt, Args&&... args) const
|
||||
{
|
||||
if (build_enabled && m_enabled) vdowrite(fmt);
|
||||
}
|
||||
|
||||
template<typename T1>
|
||||
void operator ()(const pstring &fmt, const T1 &v1) const
|
||||
{
|
||||
if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1));
|
||||
}
|
||||
|
||||
template<typename T1, typename T2>
|
||||
void operator ()(const pstring &fmt, const T1 &v1, const T2 &v2) const
|
||||
{
|
||||
if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2));
|
||||
}
|
||||
|
||||
template<typename T1, typename T2, typename T3>
|
||||
void operator ()(const pstring &fmt, const T1 &v1, const T2 &v2, const T3 &v3) const
|
||||
{
|
||||
if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3));
|
||||
}
|
||||
|
||||
template<typename T1, typename T2, typename T3, typename T4>
|
||||
void operator ()(const pstring &fmt, const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4) const
|
||||
{
|
||||
if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)(v4));
|
||||
}
|
||||
|
||||
template<typename T1, typename T2, typename T3, typename T4, typename T5>
|
||||
void operator ()(const pstring &fmt, const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5) const
|
||||
{
|
||||
if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)(v4)(v5));
|
||||
if (build_enabled && m_enabled)
|
||||
{
|
||||
pfmt pf(fmt);
|
||||
vdowrite(xlog(pf, std::forward<Args>(args)...));
|
||||
}
|
||||
}
|
||||
|
||||
void set_enabled(const bool v)
|
||||
@ -244,6 +237,14 @@ protected:
|
||||
virtual void vdowrite(const pstring &ls) const = 0;
|
||||
|
||||
private:
|
||||
pfmt &xlog(pfmt &fmt) const { return fmt; }
|
||||
|
||||
template<typename X, typename... Args>
|
||||
pfmt &xlog(pfmt &fmt, X&& x, Args&&... args) const
|
||||
{
|
||||
return xlog(fmt(std::forward<X>(x)), std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
bool m_enabled;
|
||||
|
||||
};
|
||||
|
@ -136,7 +136,7 @@ namespace plib {
|
||||
std::size_t p = 1;
|
||||
opt = getopt_short(arg.substr(p, 1));
|
||||
++p;
|
||||
if (p < arg.len())
|
||||
if (p < arg.length())
|
||||
{
|
||||
has_equal_arg = true;
|
||||
opt_arg = arg.substr(p);
|
||||
@ -184,7 +184,7 @@ namespace plib {
|
||||
pstring line = pstring("").rpad(" ", firstline_indent);
|
||||
for (auto &s : psplit(p, " "))
|
||||
{
|
||||
if (line.len() + s.len() > width)
|
||||
if (line.length() + s.length() > width)
|
||||
{
|
||||
ret += line + "\n";
|
||||
line = pstring("").rpad(" ", indent);
|
||||
@ -228,14 +228,14 @@ namespace plib {
|
||||
{
|
||||
line += v + "|";
|
||||
}
|
||||
line = line.left(line.len() - 1);
|
||||
line = line.left(line.length() - 1);
|
||||
}
|
||||
else
|
||||
line += "Value";
|
||||
}
|
||||
}
|
||||
line = line.rpad(" ", indent - 2) + " ";
|
||||
if (line.len() > indent)
|
||||
if (line.length() > indent)
|
||||
{
|
||||
//ret += "TestGroup abc\n def gef\nxyz\n\n" ;
|
||||
ret += line + "\n";
|
||||
@ -259,7 +259,7 @@ namespace plib {
|
||||
ex += split_paragraphs(example->help(), width, 4, 4) + "\n";
|
||||
}
|
||||
}
|
||||
if (ex.len() > 0)
|
||||
if (ex.length() > 0)
|
||||
{
|
||||
ret += "\n\nExamples:\n\n" + ex;
|
||||
}
|
||||
|
@ -245,7 +245,7 @@ ptokenizer::token_t ptokenizer::get_token_internal()
|
||||
{
|
||||
tokstr += c;
|
||||
/* expensive, check for single char tokens */
|
||||
if (tokstr.len() == 1)
|
||||
if (tokstr.length() == 1)
|
||||
{
|
||||
auto id = m_tokens.find(tokstr);
|
||||
if (id != m_tokens.end())
|
||||
|
@ -39,7 +39,7 @@ void state_manager_t::remove_save_items(const void *owner)
|
||||
for (auto i = m_custom.begin(); i != m_custom.end(); )
|
||||
{
|
||||
if (i->get()->m_owner == owner)
|
||||
i = m_save.erase(i);
|
||||
i = m_custom.erase(i);
|
||||
else
|
||||
i++;
|
||||
}
|
||||
|
@ -157,7 +157,7 @@ protected:
|
||||
m_buf += pstring(static_cast<const pstring::mem_t *>(buf), n, pstring::UTF8);
|
||||
}
|
||||
virtual void vseek(const pos_type n) override { }
|
||||
virtual pos_type vtell() override { return m_buf.len(); }
|
||||
virtual pos_type vtell() override { return m_buf.mem_t_size(); }
|
||||
|
||||
private:
|
||||
pstring m_buf;
|
||||
@ -284,7 +284,7 @@ private:
|
||||
class pistringstream : public pimemstream
|
||||
{
|
||||
public:
|
||||
explicit pistringstream(const pstring &str) : pimemstream(str.c_str(), str.len()), m_str(str) { }
|
||||
explicit pistringstream(const pstring &str) : pimemstream(str.c_str(), str.mem_t_size()), m_str(str) { }
|
||||
virtual ~pistringstream() override;
|
||||
|
||||
private:
|
||||
@ -348,7 +348,7 @@ public:
|
||||
|
||||
void write(const pstring &text) const
|
||||
{
|
||||
m_strm.write(text.c_str(), text.size());
|
||||
m_strm.write(text.c_str(), text.mem_t_size());
|
||||
}
|
||||
|
||||
void write(const pstring::code_t c) const
|
||||
@ -391,8 +391,8 @@ public:
|
||||
|
||||
void write(const pstring &s)
|
||||
{
|
||||
write(s.size());
|
||||
m_strm.write(s.c_str(), s.size());
|
||||
write(s.mem_t_size());
|
||||
m_strm.write(s.c_str(), s.mem_t_size());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
@ -23,14 +23,14 @@ std::size_t strlen_mem(const T *s)
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
int pstring_t<F>::pcmp(const pstring_t &right) const
|
||||
int pstring_t<F>::compare(const pstring_t &right) const
|
||||
{
|
||||
std::size_t l = std::min(size(), right.size());
|
||||
std::size_t l = std::min(mem_t_size(), right.mem_t_size());
|
||||
if (l == 0)
|
||||
{
|
||||
if (size() == 0 && right.size() == 0)
|
||||
if (mem_t_size() == 0 && right.mem_t_size() == 0)
|
||||
return 0;
|
||||
else if (right.size() == 0)
|
||||
else if (right.mem_t_size() == 0)
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
@ -45,9 +45,9 @@ int pstring_t<F>::pcmp(const pstring_t &right) const
|
||||
int ret = (si == this->end() ? 0 : static_cast<int>(*si) - static_cast<int>(*ri));
|
||||
if (ret == 0)
|
||||
{
|
||||
if (this->size() > right.size())
|
||||
if (this->mem_t_size() > right.mem_t_size())
|
||||
ret = 1;
|
||||
else if (this->size() < right.size())
|
||||
else if (this->mem_t_size() < right.mem_t_size())
|
||||
ret = -1;
|
||||
}
|
||||
return ret;
|
||||
@ -58,7 +58,7 @@ pstring_t<F> pstring_t<F>::substr(size_type start, size_type nlen) const
|
||||
{
|
||||
pstring_t ret;
|
||||
//FIXME: throw ?
|
||||
const size_type l = len();
|
||||
const size_type l = length();
|
||||
if (start < l)
|
||||
{
|
||||
if (nlen == npos || start + nlen > l)
|
||||
@ -73,7 +73,7 @@ pstring_t<F> pstring_t<F>::substr(size_type start, size_type nlen) const
|
||||
template<typename F>
|
||||
pstring_t<F> pstring_t<F>::ucase() const
|
||||
{
|
||||
pstring_t ret = "";
|
||||
pstring_t ret;
|
||||
for (const auto &c : *this)
|
||||
if (c >= 'a' && c <= 'z')
|
||||
ret += (c - 'a' + 'A');
|
||||
@ -149,17 +149,17 @@ typename pstring_t<F>::size_type pstring_t<F>::find(const pstring_t &search, siz
|
||||
template<typename F>
|
||||
typename pstring_t<F>::size_type pstring_t<F>::find(code_t search, size_type start) const
|
||||
{
|
||||
mem_t buf[traits_type::MAXCODELEN+1] = { 0 };
|
||||
traits_type::encode(search, buf);
|
||||
return find(pstring_t(&buf[0], UTF8), start);
|
||||
pstring_t ss;
|
||||
traits_type::encode(search, ss.m_str);
|
||||
return find(ss, start);
|
||||
}
|
||||
|
||||
|
||||
template<typename F>
|
||||
pstring_t<F> pstring_t<F>::replace_all(const pstring_t &search, const pstring_t &replace) const
|
||||
{
|
||||
pstring_t ret("");
|
||||
const size_type slen = search.len();
|
||||
pstring_t ret;
|
||||
const size_type slen = search.length();
|
||||
|
||||
size_type last_s = 0;
|
||||
size_type s = find(search, last_s);
|
||||
@ -180,22 +180,64 @@ pstring_t<F> pstring_t<F>::rpad(const pstring_t &ws, const size_type cnt) const
|
||||
// FIXME: pstringbuffer ret(*this);
|
||||
|
||||
pstring_t ret(*this);
|
||||
size_type wsl = ws.len();
|
||||
for (auto i = ret.len(); i < cnt; i+=wsl)
|
||||
size_type wsl = ws.length();
|
||||
for (auto i = ret.length(); i < cnt; i+=wsl)
|
||||
ret += ws;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static double pstod(const pstring_t<pu8_traits> &str, std::size_t *e)
|
||||
{
|
||||
return std::stod(str.cpp_string(), e);
|
||||
}
|
||||
|
||||
static double pstod(const pstring &str, std::size_t *e)
|
||||
{
|
||||
return std::stod(str.cpp_string(), e);
|
||||
}
|
||||
|
||||
static double pstod(const pwstring &str, std::size_t *e)
|
||||
{
|
||||
return std::stod(str.cpp_string(), e);
|
||||
}
|
||||
|
||||
static double pstod(const pu16string &str, std::size_t *e)
|
||||
{
|
||||
pstring c;
|
||||
c = str;
|
||||
return std::stod(c.cpp_string(), e);
|
||||
}
|
||||
|
||||
static long pstol(const pstring_t<pu8_traits> &str, std::size_t *e, int base = 10)
|
||||
{
|
||||
return std::stol(str.cpp_string(), e, base);
|
||||
}
|
||||
|
||||
static long pstol(const pstring &str, std::size_t *e, int base = 10)
|
||||
{
|
||||
return std::stol(str.cpp_string(), e, base);
|
||||
}
|
||||
|
||||
static long pstol(const pwstring &str, std::size_t *e, int base = 10)
|
||||
{
|
||||
return std::stol(str.cpp_string(), e, base);
|
||||
}
|
||||
|
||||
static long pstol(const pu16string &str, std::size_t *e, int base = 10)
|
||||
{
|
||||
pstring c;
|
||||
c = str;
|
||||
return std::stol(c.cpp_string(), e, base);
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
double pstring_t<F>::as_double(bool *error) const
|
||||
{
|
||||
double ret;
|
||||
std::size_t e = 0;
|
||||
|
||||
if (error != nullptr)
|
||||
*error = false;
|
||||
ret = std::stod(m_str, &e);
|
||||
if (e != size())
|
||||
double ret = pstod(*this, &e);
|
||||
if (e != mem_t_size())
|
||||
if (error != nullptr)
|
||||
*error = true;
|
||||
return ret;
|
||||
@ -204,16 +246,17 @@ double pstring_t<F>::as_double(bool *error) const
|
||||
template<typename F>
|
||||
long pstring_t<F>::as_long(bool *error) const
|
||||
{
|
||||
static pstring_t prefix(pstring("0x"));
|
||||
long ret;
|
||||
std::size_t e = 0;
|
||||
|
||||
if (error != nullptr)
|
||||
*error = false;
|
||||
if (startsWith("0x"))
|
||||
ret = std::stol(substr(2).m_str, &e, 16);
|
||||
if (startsWith(prefix))
|
||||
ret = pstol(substr(2), &e, 16);
|
||||
else
|
||||
ret = std::stol(m_str, &e, 10);
|
||||
if (e != size())
|
||||
ret = pstol(*this, &e, 10);
|
||||
if (e != mem_t_size())
|
||||
if (error != nullptr)
|
||||
*error = true;
|
||||
return ret;
|
||||
@ -225,6 +268,5 @@ long pstring_t<F>::as_long(bool *error) const
|
||||
|
||||
template struct pstring_t<pu8_traits>;
|
||||
template struct pstring_t<putf8_traits>;
|
||||
|
||||
const unsigned pu8_traits::MAXCODELEN;
|
||||
const unsigned putf8_traits::MAXCODELEN;
|
||||
template struct pstring_t<putf16_traits>;
|
||||
template struct pstring_t<pwchar_traits>;
|
||||
|
334
src/lib/netlist/plib/pstring.h
Normal file → Executable file
334
src/lib/netlist/plib/pstring.h
Normal file → Executable file
@ -28,15 +28,31 @@ public:
|
||||
typedef typename traits_type::code_t code_t;
|
||||
typedef std::size_t size_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef typename traits_type::string_type string_type;
|
||||
|
||||
class ref_value_type final
|
||||
{
|
||||
public:
|
||||
ref_value_type() = delete;
|
||||
ref_value_type(const ref_value_type &) = delete;
|
||||
ref_value_type(ref_value_type &&) = delete;
|
||||
ref_value_type &operator=(const ref_value_type &) = delete;
|
||||
ref_value_type &operator=(ref_value_type &&) = delete;
|
||||
operator code_t() const noexcept { return traits_type::code(&m); }
|
||||
private:
|
||||
const mem_t m;
|
||||
};
|
||||
typedef const ref_value_type& const_reference ;
|
||||
typedef const_reference reference;
|
||||
|
||||
enum enc_t
|
||||
{
|
||||
UTF8
|
||||
UTF8,
|
||||
UTF16
|
||||
};
|
||||
|
||||
// simple construction/destruction
|
||||
pstring_t()
|
||||
: m_str("")
|
||||
{
|
||||
}
|
||||
~pstring_t()
|
||||
@ -54,10 +70,10 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
template<typename C, std::size_t N>
|
||||
template<typename C, std::size_t N,
|
||||
class = typename std::enable_if<std::is_same<C, const mem_t>::value>::type>
|
||||
pstring_t(C (&string)[N])
|
||||
{
|
||||
static_assert(std::is_same<C, const mem_t>::value, "pstring constructor only accepts const mem_t");
|
||||
static_assert(N > 0,"pstring from array of length 0");
|
||||
if (string[N-1] != 0)
|
||||
throw std::exception();
|
||||
@ -68,35 +84,45 @@ public:
|
||||
: m_str(string.m_str)
|
||||
{ }
|
||||
|
||||
explicit pstring_t(const string_type &string, const enc_t enc)
|
||||
: m_str(string)
|
||||
{ }
|
||||
|
||||
pstring_t(pstring_t &&string)
|
||||
: m_str(string.m_str)
|
||||
{ }
|
||||
|
||||
explicit pstring_t(code_t code)
|
||||
{
|
||||
pstring_t t;
|
||||
t+= code;
|
||||
m_str.assign(t.m_str);
|
||||
*this += code;
|
||||
}
|
||||
|
||||
template <typename T,
|
||||
class = typename std::enable_if<!std::is_same<T, pstring_t::traits_type>::value>::type>
|
||||
explicit pstring_t(const pstring_t<T> &string)
|
||||
{
|
||||
m_str.clear();
|
||||
for (auto &c : string)
|
||||
*this += static_cast<code_t>(c); // FIXME: codepage conversion for u8
|
||||
}
|
||||
|
||||
// assignment operators
|
||||
pstring_t &operator=(const pstring_t &string) { m_str = string.m_str; return *this; }
|
||||
|
||||
template <typename T,
|
||||
class = typename std::enable_if<!std::is_same<T, pstring_t::traits_type>::value>::type>
|
||||
pstring_t &operator=(const pstring_t<T> &string)
|
||||
{
|
||||
m_str.clear();
|
||||
for (auto &c : string)
|
||||
*this += c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
class const_iterator final
|
||||
{
|
||||
public:
|
||||
class value_type final
|
||||
{
|
||||
public:
|
||||
value_type() = delete;
|
||||
value_type(const value_type &) = delete;
|
||||
value_type(value_type &&) = delete;
|
||||
value_type &operator=(const value_type &) = delete;
|
||||
value_type &operator=(value_type &&) = delete;
|
||||
operator code_t() const noexcept { return traits_type::code(&m); }
|
||||
private:
|
||||
const mem_t m;
|
||||
};
|
||||
|
||||
typedef ref_value_type value_type;
|
||||
|
||||
typedef value_type const *pointer;
|
||||
typedef value_type const &reference;
|
||||
@ -104,7 +130,7 @@ public:
|
||||
typedef std::forward_iterator_tag iterator_category;
|
||||
|
||||
const_iterator() noexcept : p() { }
|
||||
explicit constexpr const_iterator(const std::string::const_iterator &x) noexcept : p(x) { }
|
||||
explicit constexpr const_iterator(const typename string_type::const_iterator &x) noexcept : p(x) { }
|
||||
const_iterator(const const_iterator &rhs) noexcept = default;
|
||||
const_iterator(const_iterator &&rhs) noexcept = default;
|
||||
const_iterator &operator=(const const_iterator &rhs) noexcept = default;
|
||||
@ -121,7 +147,7 @@ public:
|
||||
|
||||
private:
|
||||
template <typename G> friend struct pstring_t;
|
||||
std::string::const_iterator p;
|
||||
typename string_type::const_iterator p;
|
||||
};
|
||||
|
||||
// no non-const const_iterator for now
|
||||
@ -136,10 +162,14 @@ public:
|
||||
|
||||
// C string conversion helpers
|
||||
const mem_t *c_str() const { return static_cast<const mem_t *>(m_str.c_str()); }
|
||||
const mem_t *data() const { return c_str(); }
|
||||
|
||||
size_type size() const { return m_str.size(); }
|
||||
size_type length() const { return traits_type::len(m_str); }
|
||||
size_type size() const { return traits_type::len(m_str); }
|
||||
bool empty() const { return m_str.size() == 0; }
|
||||
|
||||
pstring_t substr(size_type start, size_type nlen = npos) const;
|
||||
int compare(const pstring_t &right) const;
|
||||
|
||||
size_type find(const pstring_t &search, size_type start = 0) const;
|
||||
size_type find(code_t search, size_type start = 0) const;
|
||||
@ -149,25 +179,27 @@ public:
|
||||
|
||||
// concatenation operators
|
||||
pstring_t& operator+=(const pstring_t &string) { m_str.append(string.m_str); return *this; }
|
||||
pstring_t& operator+=(const code_t c) { mem_t buf[traits_type::MAXCODELEN+1] = { 0 }; traits_type::encode(c, buf); m_str.append(buf); return *this; }
|
||||
pstring_t& operator+=(const code_t c) { traits_type::encode(c, m_str); return *this; }
|
||||
friend pstring_t operator+(const pstring_t &lhs, const pstring_t &rhs) { return pstring_t(lhs) += rhs; }
|
||||
friend pstring_t operator+(const pstring_t &lhs, const code_t rhs) { return pstring_t(lhs) += rhs; }
|
||||
|
||||
// comparison operators
|
||||
bool operator==(const pstring_t &string) const { return (pcmp(string) == 0); }
|
||||
bool operator!=(const pstring_t &string) const { return (pcmp(string) != 0); }
|
||||
bool operator==(const pstring_t &string) const { return (compare(string) == 0); }
|
||||
bool operator!=(const pstring_t &string) const { return (compare(string) != 0); }
|
||||
|
||||
bool operator<(const pstring_t &string) const { return (pcmp(string) < 0); }
|
||||
bool operator<=(const pstring_t &string) const { return (pcmp(string) <= 0); }
|
||||
bool operator>(const pstring_t &string) const { return (pcmp(string) > 0); }
|
||||
bool operator>=(const pstring_t &string) const { return (pcmp(string) >= 0); }
|
||||
bool operator<(const pstring_t &string) const { return (compare(string) < 0); }
|
||||
bool operator<=(const pstring_t &string) const { return (compare(string) <= 0); }
|
||||
bool operator>(const pstring_t &string) const { return (compare(string) > 0); }
|
||||
bool operator>=(const pstring_t &string) const { return (compare(string) >= 0); }
|
||||
|
||||
const_reference at(const size_type pos) const { return *reinterpret_cast<const ref_value_type *>(F::nthcode(m_str.c_str(),pos)); }
|
||||
|
||||
/* The following is not compatible to std::string */
|
||||
|
||||
bool equals(const pstring_t &string) const { return (pcmp(string) == 0); }
|
||||
bool equals(const pstring_t &string) const { return (compare(string) == 0); }
|
||||
|
||||
bool startsWith(const pstring_t &arg) const { return arg.size() > size() ? false : m_str.compare(0, arg.size(), arg.m_str) == 0; }
|
||||
bool endsWith(const pstring_t &arg) const { return arg.size() > size() ? false : m_str.compare(size()-arg.size(), arg.size(), arg.m_str) == 0; }
|
||||
bool startsWith(const pstring_t &arg) const { return arg.mem_t_size() > mem_t_size() ? false : m_str.compare(0, arg.mem_t_size(), arg.m_str) == 0; }
|
||||
bool endsWith(const pstring_t &arg) const { return arg.mem_t_size() > mem_t_size() ? false : m_str.compare(mem_t_size()-arg.mem_t_size(), arg.mem_t_size(), arg.m_str) == 0; }
|
||||
|
||||
pstring_t replace_all(const pstring_t &search, const pstring_t &replace) const;
|
||||
pstring_t cat(const pstring_t &s) const { return *this + s; }
|
||||
@ -178,14 +210,14 @@ public:
|
||||
double as_double(bool *error = nullptr) const;
|
||||
long as_long(bool *error = nullptr) const;
|
||||
|
||||
size_type len() const { return traits_type::len(m_str); }
|
||||
|
||||
/* the following are extensions to <string> */
|
||||
|
||||
size_type mem_t_size() const { return m_str.size(); }
|
||||
|
||||
pstring_t left(size_type len) const { return substr(0, len); }
|
||||
pstring_t right(size_type nlen) const
|
||||
{
|
||||
return nlen >= len() ? pstring_t(*this) : substr(len() - nlen, nlen);
|
||||
return nlen >= length() ? *this : substr(length() - nlen, nlen);
|
||||
}
|
||||
|
||||
pstring_t ltrim(const pstring_t &ws = pstring_t(" \t\n\r")) const
|
||||
@ -196,50 +228,45 @@ public:
|
||||
pstring_t rtrim(const pstring_t &ws = pstring_t(" \t\n\r")) const
|
||||
{
|
||||
auto f = find_last_not_of(ws);
|
||||
if (f==npos)
|
||||
return pstring_t("");
|
||||
else
|
||||
return substr(0, f + 1);
|
||||
return f == npos ? pstring_t() : substr(0, f + 1);
|
||||
}
|
||||
|
||||
pstring_t trim(const pstring_t &ws = pstring_t(" \t\n\r")) const { return this->ltrim(ws).rtrim(ws); }
|
||||
|
||||
pstring_t rpad(const pstring_t &ws, const size_type cnt) const;
|
||||
|
||||
code_t code_at(const size_type pos) const {return F::code(F::nthcode(m_str.c_str(),pos)); }
|
||||
|
||||
pstring_t ucase() const;
|
||||
|
||||
const string_type &cpp_string() const { return m_str; }
|
||||
|
||||
static const size_type npos = static_cast<size_type>(-1);
|
||||
|
||||
protected:
|
||||
std::string m_str;
|
||||
string_type m_str;
|
||||
|
||||
private:
|
||||
|
||||
int pcmp(const pstring_t &right) const;
|
||||
};
|
||||
|
||||
struct pu8_traits
|
||||
{
|
||||
static const unsigned MAXCODELEN = 1; /* in memory units */
|
||||
typedef char mem_t;
|
||||
typedef char code_t;
|
||||
static std::size_t len(const std::string &p) { return p.size(); }
|
||||
static unsigned codelen(const mem_t *p) { return 1; }
|
||||
static unsigned codelen(const code_t c) { return 1; }
|
||||
typedef std::string string_type;
|
||||
static std::size_t len(const string_type &p) { return p.size(); }
|
||||
static std::size_t codelen(const mem_t *p) { return 1; }
|
||||
static std::size_t codelen(const code_t c) { return 1; }
|
||||
static code_t code(const mem_t *p) { return *p; }
|
||||
static void encode(const code_t c, mem_t *p) { *p = c; }
|
||||
static void encode(const code_t c, string_type &s) { s += static_cast<mem_t>(c); }
|
||||
static const mem_t *nthcode(const mem_t *p, const std::size_t n) { return &(p[n]); }
|
||||
};
|
||||
|
||||
/* No checking, this may deliver invalid codes */
|
||||
struct putf8_traits
|
||||
{
|
||||
static const unsigned MAXCODELEN = 4; /* in memory units, RFC 3629 */
|
||||
typedef char mem_t;
|
||||
typedef char32_t code_t;
|
||||
static std::size_t len(const std::string &p)
|
||||
typedef std::string string_type;
|
||||
static std::size_t len(const string_type &p)
|
||||
{
|
||||
std::size_t ret = 0;
|
||||
for (const auto &c : p)
|
||||
@ -265,7 +292,7 @@ struct putf8_traits
|
||||
return 1; // not correct
|
||||
}
|
||||
}
|
||||
static char32_t codelen(const code_t c)
|
||||
static std::size_t codelen(const code_t c)
|
||||
{
|
||||
if (c < 0x0080)
|
||||
return 1;
|
||||
@ -290,30 +317,29 @@ struct putf8_traits
|
||||
else
|
||||
return *p1; // not correct
|
||||
}
|
||||
static void encode(const code_t c, mem_t *p)
|
||||
static void encode(const code_t c, string_type &s)
|
||||
{
|
||||
unsigned char *m = reinterpret_cast<unsigned char *>(p);
|
||||
if (c < 0x0080)
|
||||
{
|
||||
m[0] = static_cast<unsigned char>(c);
|
||||
s += static_cast<mem_t>(c);
|
||||
}
|
||||
else if (c < 0x800)
|
||||
{
|
||||
m[0] = static_cast<unsigned char>(0xC0 | (c >> 6));
|
||||
m[1] = static_cast<unsigned char>(0x80 | (c & 0x3f));
|
||||
s += static_cast<mem_t>(0xC0 | (c >> 6));
|
||||
s += static_cast<mem_t>(0x80 | (c & 0x3f));
|
||||
}
|
||||
else if (c < 0x10000)
|
||||
{
|
||||
m[0] = static_cast<unsigned char>(0xE0 | (c >> 12));
|
||||
m[1] = static_cast<unsigned char>(0x80 | ((c>>6) & 0x3f));
|
||||
m[2] = static_cast<unsigned char>(0x80 | (c & 0x3f));
|
||||
s += static_cast<mem_t>(0xE0 | (c >> 12));
|
||||
s += static_cast<mem_t>(0x80 | ((c>>6) & 0x3f));
|
||||
s += static_cast<mem_t>(0x80 | (c & 0x3f));
|
||||
}
|
||||
else /* U+10000 U+1FFFFF */
|
||||
{
|
||||
m[0] = static_cast<unsigned char>(0xF0 | (c >> 18));
|
||||
m[1] = static_cast<unsigned char>(0x80 | ((c>>12) & 0x3f));
|
||||
m[2] = static_cast<unsigned char>(0x80 | ((c>>6) & 0x3f));
|
||||
m[3] = static_cast<unsigned char>(0x80 | (c & 0x3f));
|
||||
s += static_cast<mem_t>(0xF0 | (c >> 18));
|
||||
s += static_cast<mem_t>(0x80 | ((c>>12) & 0x3f));
|
||||
s += static_cast<mem_t>(0x80 | ((c>>6) & 0x3f));
|
||||
s += static_cast<mem_t>(0x80 | (c & 0x3f));
|
||||
}
|
||||
}
|
||||
static const mem_t *nthcode(const mem_t *p, const std::size_t n)
|
||||
@ -326,17 +352,191 @@ struct putf8_traits
|
||||
}
|
||||
};
|
||||
|
||||
struct putf16_traits
|
||||
{
|
||||
typedef char16_t mem_t;
|
||||
typedef char32_t code_t;
|
||||
typedef std::u16string string_type;
|
||||
static std::size_t len(const string_type &p)
|
||||
{
|
||||
std::size_t ret = 0;
|
||||
auto i = p.begin();
|
||||
while (i != p.end())
|
||||
{
|
||||
// FIXME: check that size is equal
|
||||
uint16_t c = static_cast<uint16_t>(*i++);
|
||||
if (!((c & 0xd800) == 0xd800))
|
||||
ret++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
static std::size_t codelen(const mem_t *p)
|
||||
{
|
||||
uint16_t c = static_cast<uint16_t>(*p);
|
||||
return ((c & 0xd800) == 0xd800) ? 2 : 1;
|
||||
}
|
||||
static std::size_t codelen(const code_t c)
|
||||
{
|
||||
if (c < 0x10000)
|
||||
return 1;
|
||||
else /* U+10000 U+1FFFFF */
|
||||
return 2;
|
||||
}
|
||||
static code_t code(const mem_t *p)
|
||||
{
|
||||
uint32_t c = static_cast<uint32_t>(*p++);
|
||||
if ((c & 0xd800) == 0xd800)
|
||||
{
|
||||
c = (c - 0xd800) << 10;
|
||||
c += static_cast<uint32_t>(*p) - 0xdc00 + 0x10000;
|
||||
}
|
||||
return static_cast<code_t>(c);
|
||||
}
|
||||
static void encode(code_t c, string_type &s)
|
||||
{
|
||||
uint32_t cu = static_cast<uint32_t>(c);
|
||||
if (c > 0xffff)
|
||||
{ //make a surrogate pair
|
||||
uint32_t t = ((cu - 0x10000) >> 10) + 0xd800;
|
||||
cu = (cu & 0x3ff) + 0xdc00;
|
||||
s += static_cast<mem_t>(t);
|
||||
s += static_cast<mem_t>(cu);
|
||||
}
|
||||
else
|
||||
{
|
||||
s += static_cast<mem_t>(cu);
|
||||
}
|
||||
}
|
||||
static const mem_t *nthcode(const mem_t *p, const std::size_t n)
|
||||
{
|
||||
std::size_t i = n;
|
||||
while (i-- > 0)
|
||||
p += codelen(p);
|
||||
return p;
|
||||
}
|
||||
};
|
||||
|
||||
struct pwchar_traits
|
||||
{
|
||||
typedef wchar_t mem_t;
|
||||
typedef char32_t code_t;
|
||||
typedef std::wstring string_type;
|
||||
static std::size_t len(const string_type &p)
|
||||
{
|
||||
if (sizeof(wchar_t) == 2)
|
||||
{
|
||||
std::size_t ret = 0;
|
||||
auto i = p.begin();
|
||||
while (i != p.end())
|
||||
{
|
||||
// FIXME: check that size is equal
|
||||
uint32_t c = static_cast<uint32_t>(*i++);
|
||||
if (!((c & 0xd800) == 0xd800))
|
||||
ret++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
return p.size();
|
||||
}
|
||||
|
||||
static std::size_t codelen(const mem_t *p)
|
||||
{
|
||||
if (sizeof(wchar_t) == 2)
|
||||
{
|
||||
uint16_t c = static_cast<uint16_t>(*p);
|
||||
return ((c & 0xd800) == 0xd800) ? 2 : 1;
|
||||
}
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
|
||||
static std::size_t codelen(const code_t c)
|
||||
{
|
||||
if (sizeof(wchar_t) == 2)
|
||||
return ((c & 0xd800) == 0xd800) ? 2 : 1;
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
|
||||
static code_t code(const mem_t *p)
|
||||
{
|
||||
if (sizeof(wchar_t) == 2)
|
||||
{
|
||||
uint32_t c = static_cast<uint32_t>(*p++);
|
||||
if ((c & 0xd800) == 0xd800)
|
||||
{
|
||||
c = (c - 0xd800) << 10;
|
||||
c += static_cast<uint32_t>(*p) - 0xdc00 + 0x10000;
|
||||
}
|
||||
return static_cast<code_t>(c);
|
||||
}
|
||||
else
|
||||
return static_cast<code_t>(*p);
|
||||
}
|
||||
|
||||
static void encode(code_t c, string_type &s)
|
||||
{
|
||||
if (sizeof(wchar_t) == 2)
|
||||
{
|
||||
uint32_t cu = static_cast<uint32_t>(c);
|
||||
if (c > 0xffff)
|
||||
{ //make a surrogate pair
|
||||
uint32_t t = ((cu - 0x10000) >> 10) + 0xd800;
|
||||
cu = (cu & 0x3ff) + 0xdc00;
|
||||
s += static_cast<mem_t>(t);
|
||||
s += static_cast<mem_t>(cu);
|
||||
}
|
||||
else
|
||||
s += static_cast<mem_t>(cu);
|
||||
}
|
||||
else
|
||||
s += static_cast<wchar_t>(c);
|
||||
}
|
||||
static const mem_t *nthcode(const mem_t *p, const std::size_t n)
|
||||
{
|
||||
if (sizeof(wchar_t) == 2)
|
||||
{
|
||||
std::size_t i = n;
|
||||
while (i-- > 0)
|
||||
p += codelen(p);
|
||||
return p;
|
||||
}
|
||||
else
|
||||
return p + n;
|
||||
}
|
||||
};
|
||||
|
||||
extern template struct pstring_t<pu8_traits>;
|
||||
extern template struct pstring_t<putf8_traits>;
|
||||
extern template struct pstring_t<putf16_traits>;
|
||||
extern template struct pstring_t<pwchar_traits>;
|
||||
|
||||
typedef pstring_t<putf8_traits> pstring;
|
||||
typedef pstring_t<putf16_traits> pu16string;
|
||||
typedef pstring_t<pwchar_traits> pwstring;
|
||||
|
||||
namespace plib
|
||||
{
|
||||
template<typename T>
|
||||
pstring to_string(const T &v)
|
||||
{
|
||||
return pstring(std::to_string(v), pstring::UTF8);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
pwstring to_wstring(const T &v)
|
||||
{
|
||||
return pwstring(std::to_wstring(v), pwstring::UTF16);
|
||||
}
|
||||
}
|
||||
|
||||
// custom specialization of std::hash can be injected in namespace std
|
||||
namespace std
|
||||
{
|
||||
template<> struct hash<pstring>
|
||||
template<typename T> struct hash<pstring_t<T>>
|
||||
{
|
||||
typedef pstring argument_type;
|
||||
typedef pstring_t<T> argument_type;
|
||||
typedef std::size_t result_type;
|
||||
result_type operator()(argument_type const& s) const
|
||||
{
|
||||
|
@ -50,15 +50,15 @@ namespace plib
|
||||
while (pn != pstring::npos)
|
||||
{
|
||||
pstring t = str.substr(p, pn - p);
|
||||
if (!ignore_empty || t.len() != 0)
|
||||
if (!ignore_empty || t.length() != 0)
|
||||
ret.push_back(t);
|
||||
p = pn + onstr.len();
|
||||
p = pn + onstr.length();
|
||||
pn = str.find(onstr, p);
|
||||
}
|
||||
if (p < str.len())
|
||||
if (p < str.length())
|
||||
{
|
||||
pstring t = str.substr(p);
|
||||
if (!ignore_empty || t.len() != 0)
|
||||
if (!ignore_empty || t.length() != 0)
|
||||
ret.push_back(t);
|
||||
}
|
||||
return ret;
|
||||
@ -75,7 +75,7 @@ namespace plib
|
||||
std::size_t p = static_cast<std::size_t>(-1);
|
||||
for (std::size_t j=0; j < onstrl.size(); j++)
|
||||
{
|
||||
if (std::equal(onstrl[j].c_str(), onstrl[j].c_str() + onstrl[j].size(), i))
|
||||
if (std::equal(onstrl[j].begin(), onstrl[j].end(), i))
|
||||
{
|
||||
p = j;
|
||||
break;
|
||||
@ -88,7 +88,7 @@ namespace plib
|
||||
|
||||
col = "";
|
||||
ret.push_back(onstrl[p]);
|
||||
i = std::next(i, static_cast<pstring::difference_type>(onstrl[p].len()));
|
||||
i = std::next(i, static_cast<pstring::difference_type>(onstrl[p].length()));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
8
src/lib/netlist/prg/nltool.cpp
Normal file → Executable file
8
src/lib/netlist/prg/nltool.cpp
Normal file → Executable file
@ -591,7 +591,7 @@ void tool_app_t::listdevices()
|
||||
|
||||
for (auto & f : list)
|
||||
{
|
||||
pstring out = plib::pfmt("{1} {2}(<id>")(f->classname(),"-20")(f->name());
|
||||
pstring out = plib::pfmt("{1:-20} {2}(<id>")(f->classname())(f->name());
|
||||
std::vector<pstring> terms;
|
||||
|
||||
f->macro_actions(nt.setup().netlist(), f->name() + "_lc");
|
||||
@ -602,7 +602,7 @@ void tool_app_t::listdevices()
|
||||
{
|
||||
if (t.second->name().startsWith(d->name()))
|
||||
{
|
||||
pstring tn(t.second->name().substr(d->name().len()+1));
|
||||
pstring tn(t.second->name().substr(d->name().length()+1));
|
||||
if (tn.find(".") == pstring::npos)
|
||||
terms.push_back(tn);
|
||||
}
|
||||
@ -612,7 +612,7 @@ void tool_app_t::listdevices()
|
||||
{
|
||||
if (t.first.startsWith(d->name()))
|
||||
{
|
||||
pstring tn(t.first.substr(d->name().len()+1));
|
||||
pstring tn(t.first.substr(d->name().length()+1));
|
||||
//printf("\t%s %s %s\n", t.first.c_str(), t.second.c_str(), tn.c_str());
|
||||
if (tn.find(".") == pstring::npos)
|
||||
{
|
||||
@ -621,7 +621,7 @@ void tool_app_t::listdevices()
|
||||
//printf("\t%s %s %s\n", t.first.c_str(), t.second.c_str(), resolved.c_str());
|
||||
if (resolved != t.first)
|
||||
{
|
||||
auto found = std::find(terms.begin(), terms.end(), resolved.substr(d->name().len()+1));
|
||||
auto found = std::find(terms.begin(), terms.end(), resolved.substr(d->name().length()+1));
|
||||
if (found!=terms.end())
|
||||
terms.erase(found);
|
||||
}
|
||||
|
@ -328,9 +328,9 @@ void matrix_solver_t::setup_matrix()
|
||||
if ((0))
|
||||
for (unsigned k = 0; k < iN; k++)
|
||||
{
|
||||
pstring line = plib::pfmt("{1}")(k, "3");
|
||||
pstring line = plib::pfmt("{1:3}")(k);
|
||||
for (unsigned j = 0; j < m_terms[k]->m_nzrd.size(); j++)
|
||||
line += plib::pfmt(" {1}")(m_terms[k]->m_nzrd[j], "3");
|
||||
line += plib::pfmt(" {1:3}")(m_terms[k]->m_nzrd[j]);
|
||||
log().verbose("{1}", line);
|
||||
}
|
||||
|
||||
|
@ -136,7 +136,7 @@ public:
|
||||
|
||||
virtual std::pair<pstring, pstring> create_solver_code()
|
||||
{
|
||||
return std::pair<pstring, pstring>("", plib::pfmt("/* {1} doesn't support static compile */"));
|
||||
return std::pair<pstring, pstring>("", plib::pfmt("/* solver doesn't support static compile */\n\n"));
|
||||
}
|
||||
|
||||
protected:
|
||||
|
@ -33,7 +33,7 @@ public:
|
||||
: matrix_solver_t(anetlist, name, matrix_solver_t::ASCENDING, params)
|
||||
, m_dim(size)
|
||||
, mat(size)
|
||||
, m_proc(nullptr)
|
||||
, m_proc()
|
||||
{
|
||||
}
|
||||
|
||||
@ -63,7 +63,8 @@ private:
|
||||
std::vector<unsigned> m_term_cr[storage_N];
|
||||
mat_cr_t<storage_N> mat;
|
||||
|
||||
extsolver m_proc;
|
||||
//extsolver m_proc;
|
||||
plib::dynproc<void, double * RESTRICT, double * RESTRICT, double * RESTRICT> m_proc;
|
||||
|
||||
};
|
||||
|
||||
@ -153,11 +154,19 @@ void matrix_solver_GCR_t<m_N, storage_N>::vsetup(analog_net_t::list_t &nets)
|
||||
if (netlist().lib().isLoaded())
|
||||
{
|
||||
pstring symname = static_compile_name();
|
||||
#if 0
|
||||
m_proc = this->netlist().lib().template getsym<extsolver>(symname);
|
||||
if (m_proc != nullptr)
|
||||
this->log().verbose("External static solver {1} found ...", symname);
|
||||
else
|
||||
this->log().warning("External static solver {1} not found ...", symname);
|
||||
#else
|
||||
m_proc.load(this->netlist().lib(), symname);
|
||||
if (m_proc.resolved())
|
||||
this->log().warning("External static solver {1} found ...", symname);
|
||||
else
|
||||
this->log().warning("External static solver {1} not found ...", symname);
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
@ -375,7 +384,8 @@ unsigned matrix_solver_GCR_t<m_N, storage_N>::vsolve_non_dynamic(const bool newt
|
||||
|
||||
/* now solve it */
|
||||
|
||||
if (m_proc != nullptr)
|
||||
//if (m_proc != nullptr)
|
||||
if (m_proc.resolved())
|
||||
{
|
||||
//static_solver(m_A, RHS);
|
||||
m_proc(mat.A, RHS, new_V);
|
||||
|
@ -210,7 +210,7 @@ double nl_convert_base_t::get_sp_unit(const pstring &unit)
|
||||
double nl_convert_base_t::get_sp_val(const pstring &sin)
|
||||
{
|
||||
std::size_t p = 0;
|
||||
while (p < sin.len() && (m_numberchars.find(sin.substr(p, 1)) != pstring::npos))
|
||||
while (p < sin.length() && (m_numberchars.find(sin.substr(p, 1)) != pstring::npos))
|
||||
++p;
|
||||
pstring val = sin.left(p);
|
||||
pstring unit = sin.substr(p);
|
||||
@ -276,7 +276,7 @@ void nl_convert_spice_t::process_line(const pstring &line)
|
||||
{
|
||||
std::vector<pstring> tt(plib::psplit(line, " ", true));
|
||||
double val = 0.0;
|
||||
switch (tt[0].code_at(0))
|
||||
switch (tt[0].at(0))
|
||||
{
|
||||
case ';':
|
||||
out("// {}\n", line.substr(1));
|
||||
@ -316,14 +316,14 @@ void nl_convert_spice_t::process_line(const pstring &line)
|
||||
std::vector<pstring> m(plib::psplit(model,"{"));
|
||||
if (m.size() == 2)
|
||||
{
|
||||
if (m[1].len() != 4)
|
||||
if (m[1].length() != 4)
|
||||
fprintf(stderr, "error with model desc %s\n", model.c_str());
|
||||
pins = m[1].left(3);
|
||||
}
|
||||
add_device("QBJT_EB", tt[0], m[0]);
|
||||
add_term(tt[1], tt[0] + "." + pins.code_at(0));
|
||||
add_term(tt[2], tt[0] + "." + pins.code_at(1));
|
||||
add_term(tt[3], tt[0] + "." + pins.code_at(2));
|
||||
add_term(tt[1], tt[0] + "." + pins.at(0));
|
||||
add_term(tt[2], tt[0] + "." + pins.at(1));
|
||||
add_term(tt[3], tt[0] + "." + pins.at(2));
|
||||
}
|
||||
break;
|
||||
case 'R':
|
||||
@ -467,7 +467,7 @@ void nl_convert_eagle_t::convert(const pstring &contents)
|
||||
tok.require_token(tok.m_tok_SEMICOLON);
|
||||
token = tok.get_token();
|
||||
}
|
||||
switch (name.code_at(0))
|
||||
switch (name.at(0))
|
||||
{
|
||||
case 'Q':
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user