/*! \file pistring.h
 * \brief String
 * 
 * This file declare string and string list classes
*/
/*
	PIP - Platform Independent Primitives
	String
	Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser 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 Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PISTRING_H
#define PISTRING_H

#include "pibytearray.h"

#define PIStringAscii PIString::fromAscii


class PIStringList;

class PIP_EXPORT PIString: public PIDeque<PIChar>
{
	friend PIByteArray & operator >>(PIByteArray & s, PIString & v);
public:
	//! Contructs an empty string
	PIString(): PIDeque<PIChar>() {}

	static const float ElideLeft  ;
	static const float ElideCenter;
	static const float ElideRight ;

	PIString & operator +=(const PIChar c) {push_back(c); return *this;}
	PIString & operator +=(const char c) {push_back(PIChar(c)); return *this;}
	PIString & operator +=(const char * str);
	PIString & operator +=(const wchar_t * str);
	PIString & operator +=(const PIByteArray & ba) {appendFromChars((const char * )ba.data(), ba.size_s(), __utf8name__); return *this;}
	PIString & operator +=(const PIString & str);

	PIString(const PIString & o): PIDeque<PIChar>(o) {}

	PIString(PIString && o): PIDeque<PIChar>(std::move(o)) {}

	
	//! Contructs string with single symbol "c"
	PIString(const PIChar c): PIDeque<PIChar>() {*this += c;}
	PIString(const char c): PIDeque<PIChar>() {*this += PIChar(c);}
	
	/*! \brief Contructs string from c-string "str"
	 * \details "str" should be null-terminated\n
	 * Example: \snippet pistring.cpp PIString(char * ) */
	PIString(const char * str): PIDeque<PIChar>() {*this += str;}
	
	/*! \brief Contructs string from \c wchar_t c-string "str"
	 * \details "str" should be null-terminated\n
	 * Example: \snippet pistring.cpp PIString(wchar_t * ) */
	PIString(const wchar_t * str): PIDeque<PIChar>() {*this += str;}

	//! Contructs string from byte array "ba"
	PIString(const PIByteArray & ba): PIDeque<PIChar>() {*this += ba;}

	//! \brief Contructs string from "len" characters of buffer "str"
	PIString(const PIChar * str, const int len): PIDeque<PIChar>(str, size_t(len)) {}

	/*! \brief Contructs string from "len" characters of buffer "str"
	 * \details Example: \snippet pistring.cpp PIString(char * , int) */
	PIString(const char * str, const int len): PIDeque<PIChar>() {appendFromChars(str, len);}

	/*! \brief Contructs string as sequence of characters "c" of buffer with length "len"
	 * \details Example: \snippet pistring.cpp PIString(int, char) */
	PIString(const int len, const char c): PIDeque<PIChar>() {for (int i = 0; i < len; ++i) push_back(c);}

	/*! \brief Contructs string as sequence of symbols "c" of buffer with length "len"
	 * \details Example: \snippet pistring.cpp PIString(int, PIChar) */
	PIString(const int len, const PIChar c): PIDeque<PIChar>() {for (int i = 0; i < len; ++i) push_back(c);}
	
	~PIString() {}


	PIString & operator =(const PIString & o) {if (this == &o) return *this; clear(); *this += o; return *this;}

	PIString & operator =(PIString && o) {swap(o); return *this;}

	//! Compare operator
	bool operator ==(const PIString & str) const;

	//! Compare operator
	bool operator ==(const PIChar c) const {if (size_s() != 1) return false; return at(0) == c;}

	//! Compare operator
	bool operator ==(const char * str) const {return *this == PIString(str);}

	//! Compare operator
	bool operator !=(const PIString & str) const;

	//! Compare operator
	bool operator !=(const PIChar c) const {if (size_s() != 1) return true; return at(0) != c;}

	//! Compare operator
	bool operator !=(const char * str) const {return *this != PIString(str);}

	//! Compare operator
	bool operator <(const PIString & str) const;

	//! Compare operator
	bool operator <(const PIChar c) const {if (size_s() != 1) return size_s() < 1; return at(0) < c;}

	//! Compare operator
	bool operator <(const char * str) const {return *this < PIString(str);}

	//! Compare operator
	bool operator >(const PIString & str) const;

	//! Compare operator
	bool operator >(const PIChar c) const {if (size_s() != 1) return size_s() > 1; return at(0) > c;}

	//! Compare operator
	bool operator >(const char * str) const {return *this > PIString(str);}

	//! Compare operator
	bool operator <=(const PIString & str) const {return !(*this > str);}

	//! Compare operator
	bool operator <=(const PIChar c) const {return !(*this > c);}

	//! Compare operator
	bool operator <=(const char * str) const {return *this <= PIString(str);}

	//! Compare operator
	bool operator >=(const PIString & str) const {return !(*this < str);}

	//! Compare operator
	bool operator >=(const PIChar c) const {return !(*this < c);}

	//! Compare operator
	bool operator >=(const char * str) const {return *this >= PIString(str);}

	/*! \brief Append string "str" at the end of string
	 * \details Example: \snippet pistring.cpp PIString::<<(PIString) */
	PIString & operator <<(const PIString & str) {*this += str; return *this;}

	/*! \brief Append symbol "c" at the end of string
	 * \details Example: \snippet pistring.cpp PIString::<<(PIChar) */
	PIString & operator <<(const PIChar c) {*this += c; return *this;}

	/*! \brief Append symbol "c" at the end of string
	 * \details Example: \snippet pistring.cpp PIString::<<(PIChar) */
	PIString & operator <<(const char c) {*this += PIChar(c); return *this;}

	/*! \brief Append c-string "str" at the end of string
	 * \details Example: \snippet pistring.cpp PIString::<<(char * ) */
	PIString & operator <<(const char * str) {*this += str; return *this;}

	/*! \brief Append \c wchar_t c-string "str" at the end of string
	 * \details Example: \snippet pistring.cpp PIString::<<(wchar_t * ) */
	PIString & operator <<(const wchar_t * str) {*this += str; return *this;}

	/*! \brief Append string representation of "num" at the end of string
	 * \details Example: \snippet pistring.cpp PIString::<<(int) */
	PIString & operator <<(const int & num) {*this += PIString::fromNumber(num); return *this;}
	PIString & operator <<(const uint & num) {*this += PIString::fromNumber(num); return *this;}
	
	/*! \brief Append string representation of "num" at the end of string
	 * \details Example: \snippet pistring.cpp PIString::<<(int) */
	PIString & operator <<(const long & num) {*this += PIString::fromNumber(num); return *this;}
	PIString & operator <<(const ulong & num) {*this += PIString::fromNumber(num); return *this;}
	
	PIString & operator <<(const llong & num) {*this += PIString::fromNumber(num); return *this;}
	PIString & operator <<(const ullong & num) {*this += PIString::fromNumber(num); return *this;}
	
	/*! \brief Append string representation of "num" at the end of string
	 * \details Example: \snippet pistring.cpp PIString::<<(int) */
	PIString & operator <<(const float & num) {*this += PIString::fromNumber(num); return *this;}
	
	/*! \brief Append string representation of "num" at the end of string
	 * \details Example: \snippet pistring.cpp PIString::<<(int) */
	PIString & operator <<(const double & num) {*this += PIString::fromNumber(num); return *this;}
	
	
	//! \brief Insert string "str" at the begin of string
	PIString & prepend(const PIString & str) {insert(0, str); return *this;}
	
	//! \brief Insert string "str" at the end of string
	PIString & append(const PIString & str) {*this += str; return *this;}

	
	/*! \brief Return part of string from symbol at index "start" and maximum length "len"
	 * \details All variants demonstrated in example: \snippet pistring.cpp PIString::mid
	 * \sa \a left(), \a right() */
	PIString mid(const int start, const int len = -1) const;

	/*! \brief Return sub-string of string from symbol at index "start" and maximum length "len" */
	PIString subString(const int start, const int len = -1) const {return mid(start, len);}
	
	/*! \brief Return part of string from left and maximum length "len"
	 * \details Example: \snippet pistring.cpp PIString::left
	 * \sa \a mid(), \a right() */
	PIString left(const int len) const {return len <= 0 ? PIString() : mid(0, len);}
	
	/*! \brief Return part of string from right and maximum length "len"
	 * \details Example: \snippet pistring.cpp PIString::right
	 * \sa \a mid(), \a left() */
	PIString right(const int len) const {return len <= 0 ? PIString() : mid(size() - len, len);}
	
	/*! \brief Remove part of string from symbol as index "start" and maximum length "len"
	 * and return this string
	 * \details All variants demonstrated in example: \snippet pistring.cpp PIString::cutMid
	 * \sa \a cutLeft(), \a cutRight() */
	PIString & cutMid(const int start, const int len);
	
	/*! \brief Remove part of string from left and maximum length "len" and return this string
	 * \details Example: \snippet pistring.cpp PIString::cutLeft
	 * \sa \a cutMid(), \a cutRight() */
	PIString & cutLeft(const int len) {return len <= 0 ? *this : cutMid(0, len);}
	
	/*! \brief Remove part of string from right and maximum length "len" and return this string
	 * \details Example: \snippet pistring.cpp PIString::cutRight
	 * \sa \a cutMid(), \a cutLeft() */
	PIString & cutRight(const int len) {return len <= 0 ? *this : cutMid(size() - len, len);}
	
	/*! \brief Remove spaces at the start and at the end of string and return this string
	 * \details Example: \snippet pistring.cpp PIString::trim
	 * \sa \a trimmed() */
	PIString & trim();
	
	/*! \brief Return copy of this string without spaces at the start and at the end
	 * \details Example: \snippet pistring.cpp PIString::trimmed
	 * \sa \a trim() */
	PIString   trimmed() const;
	
	/*! \brief Replace part of string from index "from" and maximum length "len"
	 * with string "with" and return this string
	 * \details Example: \snippet pistring.cpp PIString::replace_0
	 * \sa \a replaced(), \a replaceAll() */
	PIString & replace(const int from, const int count, const PIString & with);
	
	/*! \brief Replace part copy of this string from index "from" and maximum length "len"
	 * with string "with" and return copied string
	 * \details Example: \snippet pistring.cpp PIString::replaced_0
	 * \sa \a replace(), \a replaceAll() */
	PIString   replaced(const int from, const int count, const PIString & with) const {PIString str(*this); str.replace(from, count, with); return str;}
	
	/*! \brief Replace first founded substring "what" with string "with" and return this string
	 * \details If "ok" is not null, it set to "true" if something was replaced\n
	 * Example: \snippet pistring.cpp PIString::replace_1
	 * \sa \a replaced(), \a replaceAll() */
	PIString & replace(const PIString & what, const PIString & with, bool * ok = 0);
	
	/*! \brief Replace first founded substring "what" with string "with" and return copied string
	 * \details If "ok" is not null, it set to "true" if something was replaced\n
	 * Example: \snippet pistring.cpp PIString::replaced_1
	 * \sa \a replaced(), \a replaceAll() */
	PIString   replaced(const PIString & what, const PIString & with, bool * ok = 0) const {PIString str(*this); str.replace(what, with, ok); return str;}
	
	/*! \brief Replace all founded substrings "what" with strings "with" and return this string
	 * \details Example: \snippet pistring.cpp PIString::replaceAll
	 * \sa \a replace(), \a replaced() */
	PIString & replaceAll(const PIString & what, const PIString & with);

	/*! \brief Replace all founded substrings "what" with symbol "with" and return this string
	 * \details Example: \snippet pistring.cpp PIString::replaceAll
	 * \sa \a replace(), \a replaced() */
	PIString & replaceAll(const PIString & what, const char with);

	/*! \brief Replace all founded symbols "what" with symbol "with" and return this string
	 * \details Example: \snippet pistring.cpp PIString::replaceAll
	 * \sa \a replace(), \a replaced() */
	PIString & replaceAll(const char what, const char with);

	PIString   replacedAll(const PIString & what, const PIString & with) const {PIString str(*this); str.replaceAll(what, with); return str;}

	PIString   replacedAll(const char what, const char with) const {PIString str(*this); str.replaceAll(what, with); return str;}

	PIString & removeAll(const PIString & str);

	PIString & removeAll(char c) {PIDeque<PIChar>::removeAll(PIChar(c)); return *this;}

	/*! \brief Repeat content of string "times" times and return this string
	 * \details Example: \snippet pistring.cpp PIString::repeat */
	PIString & repeat(int times) {PIString ss(*this); times--; piForTimes (times) *this += ss; return *this;}
	
	/*! \brief Returns repeated "times" times string
	 * \details Example: \snippet pistring.cpp PIString::repeated */
	PIString repeated(int times) const {PIString ss(*this); return ss.repeat(times);}
	
	/*! \brief Insert symbol "c" after index "index" and return this string
	 * \details Example: \snippet pistring.cpp PIString::insert_0 */
	PIString & insert(const int index, const PIChar c) {PIDeque<PIChar>::insert(index, c); return *this;}
	
	/*! \brief Insert symbol "c" after index "index" and return this string
	 * \details Example: \snippet pistring.cpp PIString::insert_1 */
	PIString & insert(const int index, const char c) {return insert(index, PIChar(c));}
	
	/*! \brief Insert string "str" after index "index" and return this string
	 * \details Example: \snippet pistring.cpp PIString::insert_2 */
	PIString & insert(const int index, const PIString & str);
	
	/*! \brief Insert string "str" after index "index" and return this string
	 * \details Example: \snippet pistring.cpp PIString::insert_2 */
	PIString & insert(const int index, const char * c) {return insert(index, PIString(c));}
	
	/*! \brief Enlarge string to length "len" by addition sequence of symbols
	 * "c" at the end of string, and return this string
	 * \details Example: \snippet pistring.cpp PIString::expandRightTo
	 * \sa \a expandLeftTo() */
	PIString & expandRightTo(const int len, const PIChar c) {if (len > length()) resize(len, c); return *this;}
	
	/*! \brief Enlarge string to length "len" by addition sequence of symbols
	 * "c" at the beginning of string, and return this string
	 * \details Example: \snippet pistring.cpp PIString::expandLeftTo
	 * \sa \a expandRightTo() */
	PIString & expandLeftTo(const int len, const PIChar c) {if (len > length()) insert(0, PIString(len - length(), c)); return *this;}

	/*! \brief Enlarge and returns copy of this string to length "len"
	 * by addition sequence of symbols "c" at the end of string
	 * \sa \a expandRightTo() */
	PIString   expandedRightTo(const int len, const PIChar c) const {return PIString(*this).expandRightTo(len, c);}

	/*! \brief Enlarge and returns copy of this string to length "len"
	 * by addition sequence of symbols "c" at the beginning of string
	 * \sa \a expandLeftTo() */
	PIString   expandedLeftTo(const int len, const PIChar c) const {return PIString(*this).expandLeftTo(len, c);}

	/*! \brief Add "c" symbols at the beginning and end of the string, and return this string
	 * \sa \a quoted() */
	PIString & quote(PIChar c = PIChar('"')) {insert(0, c); *this += c; return *this;}

	/*! \brief Return quoted copy of this string
	 * \sa \a quote() */
	PIString   quoted(PIChar c = PIChar('"')) {return PIString(*this).quote(c);}
	
	/*! \brief Reverse string and return this string
	 * \details Example: \snippet pistring.cpp PIString::reverse
	 * \sa \a reversed() */
	PIString & reverse() {PIString str(*this); clear(); piForeachCR (PIChar c, str) push_back(c); return *this;}
	
	/*! \brief Reverse copy of this string and return it
	 * \details Example: \snippet pistring.cpp PIString::reversed
	 * \sa \a reverse() */
	PIString   reversed() const {PIString str(*this); str.reverse(); return str;}

	/*! \brief Elide string to maximum size \"size\" and return this string
	 * \sa \a elided() */
	PIString & elide(int size, float pos = ElideCenter);

	/*! \brief Elide copy of this string to maximum size \"size\" and return it
	 * \details Example: \snippet pistring.cpp PIString::elided
	 * \sa \a elide() */
	PIString   elided(int size, float pos = ElideCenter) const {PIString str(*this); str.elide(size, pos); return str;}
	
	
	/*! \brief Take a part of string from symbol at index "start" and maximum length "len" and return it
	 * \details Example: \snippet pistring.cpp PIString::takeMid
	 * \sa \a takeLeft, \a takeRight() */
	PIString takeMid(const int start, const int len = -1) {PIString ret(mid(start, len)); cutMid(start, len); return ret;}
	
	/*! \brief Take a part from the begin of string with maximum length "len" and return it
	 * \details Example: \snippet pistring.cpp PIString::takeLeft
	 * \sa \a takeMid(), \a takeRight() */
	PIString takeLeft(const int len) {PIString ret(left(len)); cutLeft(len); return ret;}
	
	/*! \brief Take a part from the end of string with maximum length "len" and return it
	 * \details Example: \snippet pistring.cpp PIString::takeRight
	 * \sa \a takeMid(), \a takeLeft() */
	PIString takeRight(const int len) {PIString ret(right(len)); cutRight(len); return ret;}
	
	/*! \brief Take a symbol from the begin of this string and return it
	 * \details Example: \snippet pistring.cpp PIString::takeSymbol
	 * \sa \a takeWord(), \a takeCWord(), \a takeLine(), \a takeNumber(), \a takeRange() */
	PIString takeSymbol();
	
	/*! \brief Take a word from the begin of this string and return it
	 * \details Example: \snippet pistring.cpp PIString::takeWord
	 * \sa \a takeSymbol(), \a takeCWord(), \a takeLine(), \a takeNumber(), \a takeRange() */
	PIString takeWord();
	
	/*! \brief Take a word with letters, numbers and '_' symbols from the
	 * begin of this string and return it
	 * \details Example: \snippet pistring.cpp PIString::takeCWord
	 * \sa \a takeSymbol(), \a takeWord(), \a takeLine(), \a takeNumber(), \a takeRange() */
	PIString takeCWord();
	
	/*! \brief Take a line from the begin of this string and return it
	 * \details Example: \snippet pistring.cpp PIString::takeLine
	 * \sa \a takeSymbol(), \a takeWord(), \a takeCWord(), \a takeNumber(), \a takeRange() */
	PIString takeLine();
	
	/*! \brief Take a number with C-format from the begin of this string and return it
	 * \details Example: \snippet pistring.cpp PIString::takeNumber
	 * \sa \a takeSymbol(), \a takeWord(), \a takeCWord(), \a takeLine(), \a takeRange() */
	PIString takeNumber();
	
	/*! \brief Take a range between "start" and "end" symbols from the begin of this
	 * string and return it.
	 * \details "Shield" symbol prevent analysis of the next symbol.
	 * Example: \snippet pistring.cpp PIString::takeRange
	 * \sa \a takeSymbol(), \a takeWord(), \a takeLine(), \a takeNumber() */
	PIString takeRange(const PIChar start, const PIChar end, const PIChar shield = '\\');


	/*! \brief Return a string in brackets "start" and "end" symbols from the begin of this
	 * string and return it.
	 * \details Example: string = "a(b(c)d)e"; inBrackets('(', ')') = "b(c)d"; */
	PIString inBrackets(const PIChar start, const PIChar end) const;
	
	/*! \brief Return real bytes count of this string
	 * \details It`s equivalent length of char sequence
	 * returned by function \a data() - 1, without terminating null-char \n
	 * Example: \snippet pistring.cpp PIString::lengthAscii
	 * \sa \a data() */
	int lengthAscii() const {buildData(__syslocname__); return data_.size_s() - 1;}
	
	/*! \brief Return \c char * representation of this string in system codepage
	 * \details This function fill buffer by sequence
	 * of chars. Minimum length of this buffer is count
	 * of symbols. Returned \c char * is valid until next
	 * execution of this function.\n
	 * Example: \snippet pistring.cpp PIString::data
	 * \sa \a dataConsole(), \a dataUTF8() */
	const char * data() const {buildData(__syslocname__); return (const char *)(data_.data());}
	
	/*! \brief Return \c char * representation of this string in terminal codepage
	 * \details This function fill buffer by sequence
	 * of chars. Minimum length of this buffer is count
	 * of symbols. Returned \c char * is valid until next
	 * execution of this function.\n
	 * \sa \a data(), \a dataUTF8() */
	const char * dataConsole() const;
	
	/*! \brief Return \c char * representation of this string in UTF-8
	 * \details This function fill buffer by sequence
	 * of chars. Minimum length of this buffer is count
	 * of symbols. Returned \c char * is valid until next
	 * execution of this function.\n
	 * \sa \a data(), \a dataConsole() */
	const char * dataUTF8() const;
	
	/*! \brief Return \c char * representation of this string in ASCII
	 * \details This function fill buffer by sequence
	 * of chars. Minimum length of this buffer is count
	 * of symbols. Returned \c char * is valid until next
	 * execution of this function.\n */
	const char * dataAscii() const;

	//! Returns hash
	uint hash() const;

	//! \brief Return \a PIByteArray contains \a data() of this string without terminating null-char
	PIByteArray toByteArray() const {buildData(__utf8name__); return data_.resized(data_.size_s() - 1);}

	//! \brief Return \a PIByteArray contains UTF-8 \a data() of this string without terminating null-char
	PIByteArray toUTF8() const;

	//! \brief Return \a PIByteArray contains custom charset representation of this string without terminating null-char
	PIByteArray toCharset(const char * c) const;

	/*! \brief Split string with delimiter "delim" to \a PIStringList and return it
	 * \details Example: \snippet pistring.cpp PIString::split */
	PIStringList split(const PIString & delim) const;


	//! \brief Convert each symbol in copyed string to upper case and return it
	PIString toUpperCase() const;
	
	//! \brief Convert each symbol in copyed string to lower case and return it
	PIString toLowerCase() const;
	
	PIString toNativeDecimalPoints() const;


	//! \brief Returns if string contains "c"
	bool contains(const char c) const {return PIDeque<PIChar>::contains(PIChar(c));}

	//! \brief Returns if string contains "str"
	bool contains(const char * str) const {return contains(PIString(str));}

	//! \brief Returns if string contains "str"
	bool contains(const PIString & str) const {return find(str) >= 0;}

	
	//! \brief Search symbol "c" from symbol at index "start" and return first occur position
	//! \details Example: \snippet pistring.cpp PIString::find
	int find(const char c, const int start = 0) const;
	
	//! \brief Search substring "str" from symbol at index "start" and return first occur position
	//! \details Example: \snippet pistring.cpp PIString::find
	int find(const PIString & str, const int start = 0) const;
	
	//! \brief Search substring "str" from symbol at index "start" and return first occur position
	//! \details Example: \snippet pistring.cpp PIString::find
	int find(const char * str, const int start = 0) const {return find(PIString(str), start);}
	
	//! \brief Search symbol "c" from symbol at index "start" and return last occur position
	//! \details Example: \snippet pistring.cpp PIString::findLast
	int findLast(const char c, const int start = 0) const;
	
	//! \brief Search substring "str" from symbol at index "start" and return last occur position
	//! \details Example: \snippet pistring.cpp PIString::findLast
	int findLast(const PIString & str, const int start = 0) const;
	
	//! \brief Search substring "str" from symbol at index "start" and return last occur position
	//! \details Example: \snippet pistring.cpp PIString::findLast
	int findLast(const char * str, const int start = 0) const {return findLast(PIString(str), start);}
	
	//! \brief Search word "word" from symbol at index "start" and return first occur position.
	//! \details Example: \snippet pistring.cpp PIString::findWord
	int findWord(const PIString & word, const int start = 0) const;
	
	//! \brief Search C-style word "word" from symbol at index "start" and return first occur position.
	//! \details Example: \snippet pistring.cpp PIString::findCWord
	int findCWord(const PIString & word, const int start = 0) const;

	//! \brief Search range between "start" and "end" symbols at index "start_index" and return first occur position.
	//! \details Example: \snippet pistring.cpp PIString::findRange
	int findRange(const PIChar start, const PIChar end, const PIChar shield = '\\', const int start_index = 0, int * len = 0) const;

	//! \brief Search any symbol of "str" from symbol at index "start" and return first occur position
	//! \details Example: \snippet pistring.cpp PIString::findAny
	int findAny(const PIString & str, const int start = 0) const;

	//! \brief Search any symbol of "str" from symbol at index "start" and return first occur position
	//! \details Example: \snippet pistring.cpp PIString::findAny
	int findAny(const char * str, const int start = 0) const {return findAny(PIString(str), start);}

	//! \brief Search any symbol of "str" from symbol at index "start" and return last occur position
	//! \details Example: \snippet pistring.cpp PIString::findAnyLast
	int findAnyLast(const PIString & str, const int start = 0) const;

	//! \brief Search any symbol of "str" from symbol at index "start" and return last occur position
	//! \details Example: \snippet pistring.cpp PIString::findAnyLast
	int findAnyLast(const char * str, const int start = 0) const {return findAnyLast(PIString(str), start);}

	//! \brief Returns number of occurrences of symbol "c"
	int entries(const PIChar c) const;

	//! \brief Returns number of occurrences of symbol "c"
	int entries(char c) const {return entries(PIChar(c));}

	//! \brief Return if string starts with "str"
	bool startsWith(const PIString & str) const;
	
	//! \brief Return if string ends with "str"
	bool endsWith(const PIString & str) const;
	
	//! \brief Return symbols length of string
	int length() const {return size();}
	
	//! \brief Return \c true if string is empty, i.e. length = 0
	bool isEmpty() const {return (size() == 0 || *this == "");}

	
	//! \brief Return \c true if string equal "true", "yes", "on" or positive not null numeric value
	bool toBool() const;
	
	//! \brief Return \c char numeric value of string
	char toChar() const;
	
	//! \brief Return \c short numeric value of string in base "base"
	//! \details Example: \snippet pistring.cpp PIString::toNumber
	short toShort(int base = -1, bool * ok = 0) const {return short(toNumberBase(*this, base, ok));}
	
	//! \brief Return \c ushort numeric value of string in base "base"
	//! \details Example: \snippet pistring.cpp PIString::toNumber
	ushort toUShort(int base = -1, bool * ok = 0) const {return ushort(toNumberBase(*this, base, ok));}
	
	//! \brief Return \c int numeric value of string in base "base"
	//! \details Example: \snippet pistring.cpp PIString::toNumber
	int toInt(int base = -1, bool * ok = 0) const {return int(toNumberBase(*this, base, ok));}
	
	//! \brief Return \c uint numeric value of string in base "base"
	//! \details Example: \snippet pistring.cpp PIString::toNumber
	uint toUInt(int base = -1, bool * ok = 0) const {return uint(toNumberBase(*this, base, ok));}
	
	//! \brief Return \c long numeric value of string in base "base"
	//! \details Example: \snippet pistring.cpp PIString::toNumber
	long toLong(int base = -1, bool * ok = 0) const {return long(toNumberBase(*this, base, ok));}
	
	//! \brief Return \c ulong numeric value of string in base "base"
	//! \details Example: \snippet pistring.cpp PIString::toNumber
	ulong toULong(int base = -1, bool * ok = 0) const {return ulong(toNumberBase(*this, base, ok));}
	
	//! \brief Return \c llong numeric value of string in base "base"
	//! \details Example: \snippet pistring.cpp PIString::toNumber
	llong toLLong(int base = -1, bool * ok = 0) const {return toNumberBase(*this, base, ok);}
	
	//! \brief Return \c ullong numeric value of string in base "base"
	//! \details Example: \snippet pistring.cpp PIString::toNumber
	ullong toULLong(int base = -1, bool * ok = 0) const {return ullong(toNumberBase(*this, base, ok));}
	
	//! \brief Return \c float numeric value of string
	//! \details Example: \snippet pistring.cpp PIString::toFloat
	float toFloat() const;
	
	//! \brief Return \c double numeric value of string
	//! \details Example: \snippet pistring.cpp PIString::toFloat
	double toDouble() const;
	
	//! \brief Return \c ldouble numeric value of string
	//! \details Example: \snippet pistring.cpp PIString::toFloat
	ldouble toLDouble() const;

	//! \brief Set string content to numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::setNumber
	PIString & setNumber(const short value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
	
	//! \brief Set string content to numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::setNumber
	PIString & setNumber(const ushort value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
	
	//! \brief Set string content to numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::setNumber
	PIString & setNumber(const int value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
	
	//! \brief Set string content to numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::setNumber
	PIString & setNumber(const uint value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
	
	//! \brief Set string content to numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::setNumber
	PIString & setNumber(const long value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
	
	//! \brief Set string content to numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::setNumber
	PIString & setNumber(const ulong value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
	
	//! \brief Set string content to numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::setNumber
	PIString & setNumber(const llong & value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
	
	//! \brief Set string content to numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::setNumber
	PIString & setNumber(const ullong & value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
	
	//! \brief Set string content to numeric representation of "value"
	//! \details Example: \snippet pistring.cpp PIString::setFloat
	PIString & setNumber(const float value, char format = 'f', int precision = 8) {clear(); *this += PIString::fromNumber(value, format, precision); return *this;}
	
	//! \brief Set string content to numeric representation of "value"
	//! \details Example: \snippet pistring.cpp PIString::setFloat
	PIString & setNumber(const double & value, char format = 'f', int precision = 8) {clear(); *this += PIString::fromNumber(value, format, precision); return *this;}
	
	//! \brief Set string content to numeric representation of "value"
	//! \details Example: \snippet pistring.cpp PIString::setFloat
	PIString & setNumber(const ldouble & value, char format = 'f', int precision = 8) {clear(); *this += PIString::fromNumber(value, format, precision); return *this;}
	
	//! \brief Set string content to human readable size in B/kB/MB/GB/TB
	//! \details Example: \snippet pistring.cpp PIString::setReadableSize
	PIString & setReadableSize(llong bytes);

	//! \brief Return string contains numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::fromNumber
	static PIString fromNumber(const short value, int base = 10, bool * ok = 0) {return fromNumberBaseS(llong(value), base, ok);}
	
	//! \brief Return string contains numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::fromNumber
	static PIString fromNumber(const ushort value, int base = 10, bool * ok = 0) {return fromNumberBaseU(ullong(value), base, ok);}
	
	//! \brief Return string contains numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::fromNumber
	static PIString fromNumber(const int value, int base = 10, bool * ok = 0) {return fromNumberBaseS(llong(value), base, ok);}
	
	//! \brief Return string contains numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::fromNumber
	static PIString fromNumber(const uint value, int base = 10, bool * ok = 0) {return fromNumberBaseU(ullong(value), base, ok);}
	
	//! \brief Return string contains numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::fromNumber
	static PIString fromNumber(const long value, int base = 10, bool * ok = 0) {return fromNumberBaseS(llong(value), base, ok);}
	
	//! \brief Return string contains numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::fromNumber
	static PIString fromNumber(const ulong value, int base = 10, bool * ok = 0) {return fromNumberBaseU(ullong(value), base, ok);}
	
	//! \brief Return string contains numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::fromNumber
	static PIString fromNumber(const llong & value, int base = 10, bool * ok = 0) {return fromNumberBaseS(value, base, ok);}
	
	//! \brief Return string contains numeric representation of "value" in base "base"
	//! \details Example: \snippet pistring.cpp PIString::fromNumber
	static PIString fromNumber(const ullong & value, int base = 10, bool * ok = 0) {return fromNumberBaseU(value, base, ok);}
	
	//! \brief Return string contains numeric representation of "value"
	//! \details Example: \snippet pistring.cpp PIString::fromFloat
	static PIString fromNumber(const float value, char format = 'f', int precision = 8) {return ftos(value, format, precision);}
	
	//! \brief Return string contains numeric representation of "value"
	//! \details Example: \snippet pistring.cpp PIString::fromFloat
	static PIString fromNumber(const double & value, char format = 'f', int precision = 8) {return dtos(value, format, precision);}
	
	//! \brief Return string contains numeric representation of "value"
	//! \details Example: \snippet pistring.cpp PIString::fromFloat
	static PIString fromNumber(const ldouble & value, char format = 'f', int precision = 8) {return dtos(value, format, precision);}
	
	//! \brief Return "true" or "false"
	static PIString fromBool(const bool value) {return PIString(value ? "true" : "false");}
	
	//! \brief Return string constructed from terminal codepage
	static PIString fromConsole(const char * s);
	
	//! \brief Return string constructed from system codepage
	static PIString fromSystem(const char * s);
	
	//! \brief Return string constructed from UTF-8
	static PIString fromUTF8(const char * s);
	
	//! \brief Return string constructed from UTF-8
	static PIString fromUTF8(const PIByteArray &ba);

	//! \brief Return string constructed from ASCII
	static PIString fromAscii(const char * s);

	//! \brief Return string constructed from "len" chars ASCII
	static PIString fromAscii(const char * s, int len);

	//! \brief Return string constructed from "c" codepage
	static PIString fromCodepage(const char * s, const char * c);

	//! \brief Return string contains human readable size in B/kB/MB/GB/TB
	//! \details Example: \snippet pistring.cpp PIString::readableSize
	static PIString readableSize(llong bytes);

private:
	static const char toBaseN[];
	static const int fromBaseN[];
	
	static PIString itos(const int num);
	static PIString ltos(const long num);
	static PIString lltos(const llong num);
	static PIString uitos(const uint num);
	static PIString ultos(const ulong num);
	static PIString ulltos(const ullong num);
	static PIString ftos(const float num, char format = 'f', int precision = 8);
	static PIString dtos(const double num, char format = 'f', int precision = 8);
	static PIString fromNumberBaseS(const llong value, int base = 10, bool * ok = 0);
	static PIString fromNumberBaseU(const ullong value, int base = 10, bool * ok = 0);
	static llong toNumberBase(const PIString & value, int base = -1, bool * ok = 0);
	void appendFromChars(const char * c, int s, const char * cp = __syslocname__);
	void buildData(const char * cp = __syslocname__) const;
	void trimsubstr(int &st, int &fn) const;
	mutable PIByteArray data_;
};


//! \relatesalso PIString \relatesalso PICout \brief Output operator to PICout
PIP_EXPORT PICout operator <<(PICout s, const PIString & v);


//! \relatesalso PIString \relatesalso PIByteArray \brief Output operator to PIByteArray
inline PIByteArray & operator <<(PIByteArray & s, const PIString & v) {s << *(PIDeque<PIChar>*)&v; return s;}

//! \relatesalso PIString \relatesalso PIByteArray \brief Input operator from PIByteArray
inline PIByteArray & operator >>(PIByteArray & s, PIString & v) {v.clear(); s >> *(PIDeque<PIChar>*)&v; return s;}


//! \relatesalso PIString \brief Return concatenated string
inline PIString operator +(const PIString & str, const PIString & f) {PIString s(str); s += f; return s;}

//! \relatesalso PIString \brief Return concatenated string
inline PIString operator +(const PIString & f, const char * str) {PIString s(f); s += str; return s;}

//! \relatesalso PIString \brief Return concatenated string
inline PIString operator +(const char * str, const PIString & f) {return PIString(str) + f;}

//! \relatesalso PIString \brief Return concatenated string
inline PIString operator +(const char c, const PIString & f) {return PIChar(c) + f;}

//! \relatesalso PIString \brief Return concatenated string
inline PIString operator +(const PIString & f, const char c) {return f + PIChar(c);}


int PIP_EXPORT versionCompare(const PIString & v0, const PIString & v1, int components = 6);

PIString PIP_EXPORT versionNormalize(const PIString & v);



template<> inline uint piHash(const PIString & s) {return s.hash();}
template<> inline void piSwap(PIString & f, PIString & s) {f.swap(s);}

#endif // PISTRING_H
