diff options
author | 2015-05-27 13:47:22 +0200 | |
---|---|---|
committer | 2015-05-27 23:18:44 +0200 | |
commit | cef370aa13953c3bcfa185c818c76812c34f13fc (patch) | |
tree | 6ecc90526c03f1e2411328ac2e3d635f9b952922 /src/emu/netlist/plib/pstring.h | |
parent | e9fe1e74c46f40379bcdc472b97c4aef4d3a91e6 (diff) |
Moved all files in src/emu/netlist starting with p into plib folder.
This is a first step to ease synchronisation with a stand alone, e.g.
outside mame, netlist implementation. More signed/unsigned cleanups and
started work on generic truthtable devices. (nw)
Diffstat (limited to 'src/emu/netlist/plib/pstring.h')
-rw-r--r-- | src/emu/netlist/plib/pstring.h | 256 |
1 files changed, 256 insertions, 0 deletions
diff --git a/src/emu/netlist/plib/pstring.h b/src/emu/netlist/plib/pstring.h new file mode 100644 index 00000000000..7cfb936472c --- /dev/null +++ b/src/emu/netlist/plib/pstring.h @@ -0,0 +1,256 @@ +// license:GPL-2.0+ +// copyright-holders:Couriersud +/* + * pstring.h + */ + +#ifndef _PSTRING_H_ +#define _PSTRING_H_ + +#include "pconfig.h" + +// ---------------------------------------------------------------------------------------- +// pstring: immutable strings ... +// +// pstrings are just a pointer to a "pascal-style" string representation. +// It uses reference counts and only uses new memory when a string changes. +// ---------------------------------------------------------------------------------------- + +struct pstring +{ +public: + // simple construction/destruction + pstring() + { + init(); + } + ~pstring(); + + // construction with copy + pstring(const char *string) {init(); if (string != NULL && *string != 0) pcopy(string); } + pstring(const pstring &string) {init(); pcopy(string); } + + // assignment operators + pstring &operator=(const char *string) { pcopy(string); return *this; } + pstring &operator=(const pstring &string) { pcopy(string); return *this; } + + // C string conversion operators and helpers + operator const char *() const { return m_ptr->str(); } + inline const char *cstr() const { return m_ptr->str(); } + + // concatenation operators + pstring& operator+=(const char c) { char buf[2] = { c, 0 }; pcat(buf); return *this; } + pstring& operator+=(const pstring &string) { pcat(string.cstr()); return *this; } + pstring& operator+=(const char *string) { pcat(string); return *this; } + friend pstring operator+(const pstring &lhs, const pstring &rhs) { return pstring(lhs) += rhs; } + friend pstring operator+(const pstring &lhs, const char *rhs) { return pstring(lhs) += rhs; } + friend pstring operator+(const pstring &lhs, const char rhs) { return pstring(lhs) += rhs; } + friend pstring operator+(const char *lhs, const pstring &rhs) { return pstring(lhs) += rhs; } + + // comparison operators + bool operator==(const char *string) const { return (pcmp(string) == 0); } + bool operator==(const pstring &string) const { return (pcmp(string.cstr()) == 0); } + bool operator!=(const char *string) const { return (pcmp(string) != 0); } + bool operator!=(const pstring &string) const { return (pcmp(string.cstr()) != 0); } + bool operator<(const char *string) const { return (pcmp(string) < 0); } + bool operator<(const pstring &string) const { return (pcmp(string.cstr()) < 0); } + bool operator<=(const char *string) const { return (pcmp(string) <= 0); } + bool operator<=(const pstring &string) const { return (pcmp(string.cstr()) <= 0); } + bool operator>(const char *string) const { return (pcmp(string) > 0); } + bool operator>(const pstring &string) const { return (pcmp(string.cstr()) > 0); } + bool operator>=(const char *string) const { return (pcmp(string) >= 0); } + bool operator>=(const pstring &string) const { return (pcmp(string.cstr()) >= 0); } + + inline int len() const { return m_ptr->len(); } + + inline bool equals(const pstring &string) const { return (pcmp(string.cstr(), m_ptr->str()) == 0); } + inline bool iequals(const pstring &string) const { return (pcmpi(string.cstr(), m_ptr->str()) == 0); } + + inline int cmp(const pstring &string) const { return pcmp(string.cstr()); } + inline int cmpi(const pstring &string) const { return pcmpi(cstr(), string.cstr()); } + + int find(const char *search, int start = 0) const; + + int find(const char search, int start = 0) const; + + // various + + inline bool startsWith(const pstring &arg) const { return (pcmp(cstr(), arg.cstr(), arg.len()) == 0); } + bool startsWith(const char *arg) const; + + pstring replace(const pstring &search, const pstring &replace) const; + + // these return nstring ... + inline pstring cat(const pstring &s) const { return *this + s; } + inline pstring cat(const char *s) const { return *this + s; } + + pstring substr(unsigned int start, int count = -1) const ; + + inline pstring left(unsigned int count) const { return substr(0, count); } + inline pstring right(unsigned int count) const { return substr(len() - count, count); } + + int find_first_not_of(const pstring no) const; + int find_last_not_of(const pstring no) const; + + pstring ltrim(const pstring ws = " \t\n\r") const; + pstring rtrim(const pstring ws = " \t\n\r") const; + inline pstring trim(const pstring ws = " \t\n\r") const { return this->ltrim(ws).rtrim(ws); } + + pstring rpad(const pstring ws, const int cnt) const + { + // FIXME: slow! + pstring ret = *this; + while (ret.len() < cnt) + ret += ws; + return ret.substr(0, cnt); + } + + pstring ucase() const; + + // conversions + + double as_double(bool *error = NULL) const; + + long as_long(bool *error = NULL) const; + + // printf using string as format ... + + pstring vprintf(va_list args) const; + + // static + static pstring sprintf(const char *format, ...) ATTR_PRINTF(1,2); + static void resetmem(); + +protected: + + struct str_t + { + //str_t() : m_ref_count(1), m_len(0) { m_str[0] = 0; } + str_t(const int alen) + { + init(alen); + } + void init(const int alen) + { + m_ref_count = 1; + m_len = alen; + m_str[0] = 0; + } + char *str() { return &m_str[0]; } + int len() { return m_len; } + int m_ref_count; + private: + int m_len; + char m_str[1]; + }; + + str_t *m_ptr; + +private: + inline void init() + { + m_ptr = &m_zero; + m_ptr->m_ref_count++; + } + + inline int pcmp(const char *right) const + { + return pcmp(m_ptr->str(), right); + } + + int pcmp(const char *left, const char *right, int count = -1) const; + + int pcmpi(const char *lhs, const char *rhs, int count = -1) const; + + void pcopy(const char *from, int size); + + void pcopy(const char *from); + + inline void pcopy(const pstring &from) + { + sfree(m_ptr); + m_ptr = from.m_ptr; + m_ptr->m_ref_count++; + } + + void pcat(const char *s); + + static str_t *salloc(int n); + static void sfree(str_t *s); + + static str_t m_zero; +}; + +// ---------------------------------------------------------------------------------------- +// pstringbuffer: a string buffer implementation +// +// string buffer are optimized to handle concatenations. This implementation is designed +// to specifically interact with pstrings nicely. +// ---------------------------------------------------------------------------------------- + +struct pstringbuffer +{ +public: + static const int DEFAULT_SIZE = 2048; + // simple construction/destruction + pstringbuffer() + { + init(); + resize(DEFAULT_SIZE); + } + + ~pstringbuffer(); + + // construction with copy + pstringbuffer(const char *string) {init(); if (string != NULL) pcopy(string); } + pstringbuffer(const pstring &string) {init(); pcopy(string); } + + // assignment operators + pstringbuffer &operator=(const char *string) { pcopy(string); return *this; } + pstringbuffer &operator=(const pstring &string) { pcopy(string); return *this; } + pstringbuffer &operator=(const pstringbuffer &string) { pcopy(string.cstr()); return *this; } + + // C string conversion operators and helpers + operator const char *() const { return m_ptr; } + inline const char *cstr() const { return m_ptr; } + + operator pstring() const { return pstring(m_ptr); } + + // concatenation operators + pstringbuffer& operator+=(const char c) { char buf[2] = { c, 0 }; pcat(buf); return *this; } + pstringbuffer& operator+=(const pstring &string) { pcat(string.cstr()); return *this; } + + inline std::size_t len() const { return m_len; } + + inline void cat(const pstring &s) { pcat(s); } + inline void cat(const char *s) { pcat(s); } + + pstring substr(unsigned int start, int count = -1) + { + return pstring(m_ptr).substr(start, count); + } + +private: + + inline void init() + { + m_ptr = NULL; + m_size = 0; + m_len = 0; + } + + void resize(const std::size_t size); + + void pcopy(const char *from); + void pcopy(const pstring &from); + void pcat(const char *s); + void pcat(const pstring &s); + + char *m_ptr; + std::size_t m_size; + std::size_t m_len; + +}; + + +#endif /* _PSTRING_H_ */ |