/*
    PIP - Platform Independent Primitives
    C-String class
    Ivan Pelipenko peri4ko@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/>.
*/
#include "piconstchars.h"

#include "pistring.h"


//! \~\class PIConstChars piconstchars.h
//! \~\details
//! \~english \section PIConstChars_sec0 Synopsis
//! \~russian \section PIConstChars_sec0 Краткий обзор
//! \~english
//! This is wrapper around \c const char * string. %PIConstChars doesn`t
//! copy string, just save pointer and size.
//!
//! Provides API similar to string, with information and compare methods.
//!
//! Used to more handly works with ordinary C-strings.
//!
//! \~russian
//! Это обертка вокруг \c const char * строки. %PIConstChars не скопирует
//! строку, а хранит только указатель и размер.
//!
//! Предоставляет API схожий с обычной строкой, с методами сравнения и информационными.
//!
//! Используется для более удобной работы с обычными C-строками.
//!


bool PIConstChars::contains(char c) const {
	for (int i = 0; i < (int)len; ++i)
		if (str[i] == c) return true;
	return false;
}


bool PIConstChars::startsWith(const PIConstChars & str) const {
	if (size() < str.size()) return false;
	return str == left(str.size());
}


bool PIConstChars::startsWith(const char c) const {
	if (size() < 1) return false;
	return str[0] == c;
}


bool PIConstChars::endsWith(const PIConstChars & str) const {
	if (size() < str.size()) return false;
	return str == right(str.size());
}


bool PIConstChars::endsWith(const char c) const {
	if (size() < 1) return false;
	return str[len - 1] == c;
}


PIConstChars PIConstChars::mid(const int start, const int len) const {
	int s = start, l = len;
	if (l == 0 || s >= (int)size() || isEmpty()) return PIConstChars("");
	if (s < 0) {
		l += s;
		s = 0;
	}
	if (l < 0) {
		return PIConstChars(str + s, (int)size() - s);
	} else {
		if (l > (int)size() - s) l = (int)size() - s;
		return PIConstChars(str + s, l);
	}
	return PIConstChars("");
}


PIConstChars PIConstChars::left(const int l) const {
	if (l <= 0) return PIConstChars("");
	return mid(0, l);
}


PIConstChars PIConstChars::right(const int l) const {
	if (l <= 0) return PIConstChars("");
	return mid((int)size() - l, l);
}


PIConstChars & PIConstChars::cutLeft(const int l) {
	if (l <= 0) return *this;
	if (l >= (int)size())
		*this = PIConstChars("");
	else {
		str += l;
		len -= l;
	}
	return *this;
}


PIConstChars & PIConstChars::cutRight(const int l) {
	if (l <= 0) return *this;
	if (l >= (int)size())
		*this = PIConstChars("");
	else {
		len -= l;
	}
	return *this;
}


PIConstChars PIConstChars::takeLeft(const int len) {
	PIConstChars ret(left(len));
	cutLeft(len);
	return ret;
}


PIConstChars PIConstChars::takeRight(const int len) {
	PIConstChars ret(right(len));
	cutRight(len);
	return ret;
}


PIConstChars & PIConstChars::trim() {
	if (isEmpty()) return *this;
	int st = -1, fn = 0;
	for (int i = 0; i < (int)len; ++i) {
		if (at(i) != ' ' && at(i) != '\t' && at(i) != '\n' && at(i) != '\r' && at(i) != char(12) && at(i) != uchar(0)) {
			st = i;
			break;
		}
	}
	if (st < 0) {
		*this = PIConstChars("");
		return *this;
	}
	for (int i = (int)len - 1; i >= 0; --i) {
		if (at(i) != ' ' && at(i) != '\t' && at(i) != '\n' && at(i) != '\r' && at(i) != char(12) && at(i) != uchar(0)) {
			fn = i;
			break;
		}
	}
	if (fn < (int)len - 1) cutRight((int)len - fn - 1);
	if (st > 0) cutLeft(st);
	return *this;
}


PIString PIConstChars::toString() const {
	if (isEmpty()) return PIString();
	return PIString::fromAscii(str, len);
}
