From 58aa97913fa966b03d3b5bed77c6670e49821f1e Mon Sep 17 00:00:00 2001 From: couriersud Date: Fri, 31 Mar 2017 18:07:35 +0200 Subject: 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) --- src/lib/netlist/plib/pstring.cpp | 94 +++++++++++++++++++++++++++++----------- 1 file changed, 68 insertions(+), 26 deletions(-) (limited to 'src/lib/netlist/plib/pstring.cpp') diff --git a/src/lib/netlist/plib/pstring.cpp b/src/lib/netlist/plib/pstring.cpp index 0fa6a6f83d9..554baac9bc7 100644 --- a/src/lib/netlist/plib/pstring.cpp +++ b/src/lib/netlist/plib/pstring.cpp @@ -23,14 +23,14 @@ std::size_t strlen_mem(const T *s) } template -int pstring_t::pcmp(const pstring_t &right) const +int pstring_t::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::pcmp(const pstring_t &right) const int ret = (si == this->end() ? 0 : static_cast(*si) - static_cast(*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 pstring_t::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 pstring_t::substr(size_type start, size_type nlen) const template pstring_t pstring_t::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::size_type pstring_t::find(const pstring_t &search, siz template typename pstring_t::size_type pstring_t::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 pstring_t pstring_t::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 pstring_t::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 &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 &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 double pstring_t::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::as_double(bool *error) const template long pstring_t::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::as_long(bool *error) const template struct pstring_t; template struct pstring_t; - -const unsigned pu8_traits::MAXCODELEN; -const unsigned putf8_traits::MAXCODELEN; +template struct pstring_t; +template struct pstring_t; -- cgit v1.2.3-70-g09d2