summaryrefslogtreecommitdiffstats
path: root/src/lib/netlist/plib/pstring.h
diff options
context:
space:
mode:
author couriersud <couriersud@gmx.org>2017-01-18 00:32:12 +0100
committer couriersud <couriersud@gmx.org>2017-01-20 22:20:36 +0100
commit1ae3e29ea3fd90e5df31d52c5d3860fde7f3cbac (patch)
treee32ddf77ae8408a1aea8d7abb15f5048bba3ed81 /src/lib/netlist/plib/pstring.h
parentd0b6742563ac7f9ed626a89ebd022d09c64dfa4d (diff)
Assume string literals are UTF8 in netlist code.
At the same time, any char pointer has to be explicitly converted to pstring by specifying an encoding. Not yet optimal, but certainly better than what was there before. Removed unneeded methods from pstring. (nw)
Diffstat (limited to 'src/lib/netlist/plib/pstring.h')
-rw-r--r--src/lib/netlist/plib/pstring.h83
1 files changed, 46 insertions, 37 deletions
diff --git a/src/lib/netlist/plib/pstring.h b/src/lib/netlist/plib/pstring.h
index 16bf61b08ee..ae255d4a6cd 100644
--- a/src/lib/netlist/plib/pstring.h
+++ b/src/lib/netlist/plib/pstring.h
@@ -53,20 +53,36 @@ public:
typedef typename F::code_t code_t;
typedef std::size_t size_type;
+ enum enc_t
+ {
+ UTF8
+ };
+
// simple construction/destruction
- pstring_t()
+ pstring_t() : m_ptr(&m_zero)
{
- init();
+ init(nullptr);
}
~pstring_t();
- // construction with copy
- pstring_t(const mem_t *string) { init(); if (string != nullptr && *string != 0) pcopy(string); }
- pstring_t(const pstring_t &string) { init(); pcopy(string); }
+ // FIXME: Do something with encoding
+ pstring_t(const mem_t *string, const enc_t enc) : m_ptr(&m_zero)
+ {
+ init(string);
+ }
+
+ template<typename C, std::size_t N>
+ pstring_t(C (&string)[N]) : m_ptr(&m_zero) {
+ 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");
+ //static_assert(string[N-1] == 0, "pstring constructor parameter not a string literal");
+ init(string);
+ }
+
+ pstring_t(const pstring_t &string) : m_ptr(&m_zero) { init(string); }
pstring_t(pstring_t &&string) : m_ptr(string.m_ptr) { string.m_ptr = nullptr; }
// assignment operators
- pstring_t &operator=(const mem_t *string) { pcopy(string); return *this; }
pstring_t &operator=(const pstring_t &string) { pcopy(string); return *this; }
struct iterator final : public std::iterator<std::forward_iterator_tag, mem_t>
@@ -94,41 +110,24 @@ public:
// concatenation operators
pstring_t& operator+=(const pstring_t &string) { pcat(string); return *this; }
- pstring_t& operator+=(const mem_t *string) { pcat(string); 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 mem_t *rhs) { return pstring_t(lhs) += rhs; }
- friend pstring_t operator+(const mem_t *lhs, const pstring_t &rhs) { return pstring_t(lhs) += rhs; }
// comparison operators
- bool operator==(const mem_t *string) const { return (pcmp(string) == 0); }
bool operator==(const pstring_t &string) const { return (pcmp(string) == 0); }
- bool operator!=(const mem_t *string) const { return (pcmp(string) != 0); }
bool operator!=(const pstring_t &string) const { return (pcmp(string) != 0); }
- bool operator<(const mem_t *string) const { return (pcmp(string) < 0); }
bool operator<(const pstring_t &string) const { return (pcmp(string) < 0); }
- bool operator<=(const mem_t *string) const { return (pcmp(string) <= 0); }
bool operator<=(const pstring_t &string) const { return (pcmp(string) <= 0); }
- bool operator>(const mem_t *string) const { return (pcmp(string) > 0); }
bool operator>(const pstring_t &string) const { return (pcmp(string) > 0); }
- bool operator>=(const mem_t *string) const { return (pcmp(string) >= 0); }
bool operator>=(const pstring_t &string) const { return (pcmp(string) >= 0); }
bool equals(const pstring_t &string) const { return (pcmp(string) == 0); }
-
- //int cmp(const pstring_t &string) const { return pcmp(string); }
- //int cmp(const mem_t *string) const { return pcmp(string); }
-
bool startsWith(const pstring_t &arg) const;
- bool startsWith(const mem_t *arg) const;
-
bool endsWith(const pstring_t &arg) const;
- bool endsWith(const mem_t *arg) const { return endsWith(pstring_t(arg)); }
pstring_t replace(const pstring_t &search, const pstring_t &replace) const;
-
const pstring_t cat(const pstring_t &s) const { return *this + s; }
- const pstring_t cat(const mem_t *s) const { return *this + s; }
+ const pstring_t cat(const code_t c) const { return *this + c; }
size_type blen() const { return m_ptr->len(); }
@@ -145,11 +144,9 @@ public:
pstring_t& operator+=(const code_t c) { mem_t buf[traits::MAXCODELEN+1] = { 0 }; traits::encode(c, buf); pcat(buf); return *this; }
friend pstring_t operator+(const pstring_t &lhs, const code_t rhs) { return pstring_t(lhs) += rhs; }
- iterator find(const pstring_t &search, iterator start) const;
- iterator find(const pstring_t &search) const { return find(search, begin()); }
- iterator find(const mem_t *search, iterator start) const;
- iterator find(const mem_t *search) const { return find(search, begin()); }
- iterator find(const code_t search, iterator start) const { mem_t buf[traits::MAXCODELEN+1] = { 0 }; traits::encode(search, buf); return find(buf, start); }
+ iterator find(const pstring_t search, iterator start) const;
+ iterator find(const pstring_t search) const { return find(search, begin()); }
+ iterator find(const code_t search, iterator start) const;
iterator find(const code_t search) const { return find(search, begin()); }
const pstring_t substr(const iterator start, const iterator end) const ;
@@ -162,9 +159,9 @@ public:
iterator find_first_not_of(const pstring_t &no) const;
iterator find_last_not_of(const pstring_t &no) const;
- const pstring_t ltrim(const pstring_t &ws = " \t\n\r") const;
- const pstring_t rtrim(const pstring_t &ws = " \t\n\r") const;
- const pstring_t trim(const pstring_t &ws = " \t\n\r") const { return this->ltrim(ws).rtrim(ws); }
+ const pstring_t ltrim(const pstring_t ws = pstring_t(" \t\n\r")) const;
+ const pstring_t rtrim(const pstring_t ws = pstring_t(" \t\n\r")) const;
+ const pstring_t trim(const pstring_t ws = pstring_t(" \t\n\r")) const { return this->ltrim(ws).rtrim(ws); }
const pstring_t rpad(const pstring_t &ws, const size_type cnt) const;
@@ -172,16 +169,29 @@ public:
const pstring_t ucase() const;
+ // FIXME: do something with encoding
+ // FIXME: belongs into derived class
+ // This is only used in state saving to support "owners" whose name() function
+ // returns char*.
+ static pstring_t from_utf8(const mem_t *c) { return pstring_t(c, UTF8); }
+ static pstring_t from_utf8(const pstring_t &c) { return c; }
+
static void resetmem();
protected:
pstr_t *m_ptr;
private:
- void init()
+ void init(const mem_t *string)
{
- m_ptr = &m_zero;
m_ptr->m_ref_count++;
+ if (string != nullptr && *string != 0)
+ pcopy(string);
+ }
+ void init(const pstring_t &string)
+ {
+ m_ptr->m_ref_count++;
+ pcopy(string);
}
int pcmp(const pstring_t &right) const;
@@ -191,14 +201,12 @@ private:
void pcopy(const mem_t *from, std::size_t size);
void pcopy(const mem_t *from);
-
void pcopy(const pstring_t &from)
{
sfree(m_ptr);
m_ptr = from.m_ptr;
m_ptr->m_ref_count++;
}
-
void pcat(const mem_t *s);
void pcat(const pstring_t &s);
@@ -358,7 +366,8 @@ public:
// C string conversion helpers
const char *c_str() const { return m_ptr; }
- operator pstring() const { return pstring(m_ptr); }
+ // FIXME: encoding should be parameter
+ operator pstring() const { return pstring(m_ptr, pstring::UTF8); }
// concatenation operators
pstringbuffer& operator+=(const char c) { char buf[2] = { c, 0 }; pcat(buf); return *this; }