Files
pip/pichar.h
2013-03-18 12:07:44 +04:00

127 lines
6.1 KiB
C++

/*
PIP - Platform Independent Primitives
Unicode char
Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PICHAR_H
#define PICHAR_H
#include "pibytearray.h"
class PIChar
{
friend class PIString;
friend PIByteArray & operator <<(PIByteArray & s, const PIChar & v);
friend PIByteArray & operator >>(PIByteArray & s, PIChar & v);
public:
PIChar(const char c) {ch = c; ch &= 0xFF;}
PIChar(const short c) {ch = c; ch &= 0xFFFF;}
PIChar(const int c = 0) {ch = c;}
PIChar(const uchar c) {ch = c; ch &= 0xFF;}
PIChar(const ushort c) {ch = c; ch &= 0xFFFF;}
PIChar(const uint c) {ch = c;}
PIChar(const char * c) {ch = *reinterpret_cast<const int * >(c);}
//inline operator const int() {return static_cast<const int>(ch);}
//inline operator const char() {return toAscii();}
PIChar & operator =(const char v) {ch = v; return *this;}
/*inline PIChar & operator =(const short v) {ch = v; return *this;}
inline PIChar & operator =(const int v) {ch = v; return *this;}
inline PIChar & operator =(const uchar v) {ch = v; return *this;}
inline PIChar & operator =(const ushort v) {ch = v; return *this;}
inline PIChar & operator =(const uint v) {ch = v; return *this;}*/
bool operator ==(const PIChar & o) const {return strcmp(o.toCharPtr(), toCharPtr()) == 0;}
/*inline bool operator ==(const PIChar & o) const {if (o.isAscii() ^ isAscii()) return false;
if (isAscii()) return (o.toAscii() == toAscii());
return (o.toInt() == toInt());}
inline bool operator ==(const char o) const {return (PIChar(o) == *this);}
inline bool operator ==(const short o) const {return (PIChar(o) == *this);}
inline bool operator ==(const int o) const {return (PIChar(o) == *this);}
inline bool operator ==(const uchar o) const {return (PIChar(o) == *this);}
inline bool operator ==(const ushort o) const {return (PIChar(o) == *this);}
inline bool operator ==(const uint o) const {return (PIChar(o) == *this);}*/
bool operator !=(const PIChar & o) const {return !(o == *this);}
/*inline bool operator !=(const char o) const {return (PIChar(o) != *this);}
inline bool operator !=(const short o) const {return (PIChar(o) != *this);}
inline bool operator !=(const int o) const {return (PIChar(o) != *this);}
inline bool operator !=(const uchar o) const {return (PIChar(o) != *this);}
inline bool operator !=(const ushort o) const {return (PIChar(o) != *this);}
inline bool operator !=(const uint o) const {return (PIChar(o) != *this);}*/
bool operator >(const PIChar & o) const {return strcmp(o.toCharPtr(), toCharPtr()) < 0;}
bool operator <(const PIChar & o) const {return strcmp(o.toCharPtr(), toCharPtr()) > 0;}
bool operator >=(const PIChar & o) const {return strcmp(o.toCharPtr(), toCharPtr()) <= 0;}
bool operator <=(const PIChar & o) const {return strcmp(o.toCharPtr(), toCharPtr()) >= 0;}
bool isDigit() const {return isdigit(ch) != 0;}
bool isHex() const {return isxdigit(ch) != 0;}
bool isGraphical() const {return isgraph(ch) != 0;}
bool isControl() const {return iscntrl(ch) != 0;}
bool isLower() const {return islower(ch) != 0;}
bool isUpper() const {return isupper(ch) != 0;}
bool isPrint() const {return isprint(ch) != 0;}
bool isSpace() const {return isspace(ch) != 0;}
bool isAlpha() const {return isalpha(ch) != 0;}
bool isAscii() const {return isascii(ch) != 0;}
int toInt() const {return static_cast<const int>(ch);}
const wchar_t * toWCharPtr() const {return &ch;}
const char * toCharPtr() const {return reinterpret_cast<const char * >(&ch);}
wchar_t toWChar() const {return ch;}
char toAscii() const {return ch % 256;}
int unicode16Code() const {wchar_t wc; if (mbtowc(&wc, toCharPtr(), 4) > 0) return wc; return 0;}
//#ifdef WINDOWS
// inline PIChar toUpper() const __attribute__ ((optimize(0))) {return PIChar(toupper(ch));}
// inline PIChar toLower() const __attribute__ ((optimize(0))) {return PIChar(tolower(ch));}
//#else
PIChar toUpper() const {return PIChar(toupper(ch));}
PIChar toLower() const {return PIChar(tolower(ch));}
//#endif
private:
wchar_t ch;
};
inline std::ostream & operator <<(std::ostream & s, const PIChar & v) {s << v.toCharPtr(); return s;}
inline PIByteArray & operator <<(PIByteArray & s, const PIChar & v) {s << uint(v.ch); return s;}
inline PIByteArray & operator >>(PIByteArray & s, PIChar & v) {uint i; s >> i; v.ch = wchar_t(i); return s;}
inline bool operator ==(const char v, const PIChar & c) {return (PIChar(v) == c);}
inline bool operator >(const char v, const PIChar & c) {return (PIChar(v) > c);}
inline bool operator <(const char v, const PIChar & c) {return (PIChar(v) < c);}
inline bool operator >=(const char v, const PIChar & c) {return (PIChar(v) >= c);}
inline bool operator <=(const char v, const PIChar & c) {return (PIChar(v) <= c);}
inline bool operator ==(const char * v, const PIChar & c) {return (PIChar(v) == c);}
inline bool operator >(const char * v, const PIChar & c) {return (PIChar(v) > c);}
inline bool operator <(const char * v, const PIChar & c) {return (PIChar(v) < c);}
inline bool operator >=(const char * v, const PIChar & c) {return (PIChar(v) >= c);}
inline bool operator <=(const char * v, const PIChar & c) {return (PIChar(v) <= c);}
inline bool operator ==(const int v, const PIChar & c) {return (PIChar(v) == c);}
inline bool operator >(const int v, const PIChar & c) {return (PIChar(v) > c);}
inline bool operator <(const int v, const PIChar & c) {return (PIChar(v) < c);}
inline bool operator >=(const int v, const PIChar & c) {return (PIChar(v) >= c);}
inline bool operator <=(const int v, const PIChar & c) {return (PIChar(v) <= c);}
#endif // PICHAR_H