Files
pip/pistring.cpp
2011-03-01 06:07:16 +03:00

192 lines
3.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "pistring.h"
PIString & PIString::operator +=(const char * str) {
uint i = 0;
while (str[i] != '\0') push_back(str[i++]);
return *this;
}
PIString & PIString::operator +=(const string & str) {
uint l = str.size();
for (uint i = 0; i < l; ++i) push_back(str[i]);
return *this;
}
PIString & PIString::operator +=(const PIString & str) {
uint l = str.size();
for (uint i = 0; i < l; ++i) push_back(str[i]);
return *this;
}
PIString & PIString::operator +=(const PIByteArray & ba) {
uint l = ba.size();
for (uint i = 0; i < l; ++i) push_back(ba[i]);
return *this;
}
bool PIString::operator ==(const PIString & str) const {
uint l = str.size();
if (size() != l) return false;
for (uint i = 0; i < l; ++i)
if (str[i] != at(i))
return false;
return true;
}
bool PIString::operator !=(const PIString & str) const {
uint l = str.size();
if (size() != l) return true;
for (uint i = 0; i < l; ++i)
if (str[i] != at(i))
return true;
return false;
}
PIString PIString::mid(const int start, const int len) const {
PIString str;
int s = start, l = len;
if (l == 0) return str;
if (s < 0) {
l += s;
s = 0;
}
if (l < 0) {
for (uint i = s; i < size(); ++i)
str += at(i);
} else {
if (l > length() - s)
l = length() - s;
for (int i = s; i < s + l; ++i)
str += at(i);
}
return str;
}
PIString & PIString::cutMid(const int start, const int len) {
int s = start, l = len;
if (l == 0) return *this;
if (s < 0) {
l += s;
s = 0;
}
if (l < 0)
remove(s, size() - s);
else {
if (l > length() - s)
l = length() - s;
remove(s, s + l);
}
return *this;
}
PIString PIString::trimmed() const {
int st = 0, fn = 0;
for (int i = 0; i < length(); ++i)
if (at(i) != ' ' && at(i) != '\t')
{st = i; break;}
for (int i = length() - 1; i >= 0; --i)
if (at(i) != ' ' && at(i) != '\t')
{fn = i; break;}
return mid(st, fn - st + 1);
}
PIString & PIString::trim() {
int st = 0, fn = 0;
for (int i = 0; i < length(); ++i)
if (at(i) != ' ' && at(i) != '\t')
{st = i; break;}
for (int i = length() - 1; i >= 0; --i)
if (at(i) != ' ' && at(i) != '\t')
{fn = i; break;}
*this = mid(st, fn - st + 1);
return *this;
}
PIStringList PIString::split(const PIString & delim) {
PIString ts(*this);
PIStringList sl;
int ci = ts.find(delim);
while (ci >= 0) {
sl << ts.left(ci);
ts.cutLeft(ci + delim.length());
ci = ts.find(delim);
}
if (ts.length() > 0) sl << ts;
return sl;
}
int PIString::find(const char str, const int start) {
for (int i = start; i < length(); ++i)
if (at(i) == str)
return i;
return -1;
}
int PIString::find(const PIString str, const int start) {
int l = str.length();
for (int i = start; i < length() - l + 1; ++i)
if (mid(i, l) == str)
return i;
return -1;
}
int PIString::findLast(const char str, const int start) {
for (int i = length() - 1; i >= start; --i)
if (at(i) == str)
return i;
return -1;
}
int PIString::findLast(const PIString str, const int start) {
int l = str.length();
for (int i = length() - l; i >= start; --i)
if (mid(i, l) == str)
return i;
return -1;
}
PIString PIString::toUpperCase() const {
PIString str(*this);
int l = str.size();
for (int i = 0; i < l; ++i) str[i] = chrUpr(str[i]);
return str;
}
PIString PIString::toLowerCase() const {
PIString str(*this);
int l = str.size();
for (int i = 0; i < l; ++i) str[i] = chrLwr(str[i]);
return str;
}
char chrUpr(char c) {
if (c >= 'a' && c <= 'z') return c + 'A' - 'a';
//if (c >= 'а' && c <= 'я') return c + 'А' - 'а';
return c;
}
char chrLwr(char c) {
if (c >= 'A' && c <= 'Z') return c + 'a' - 'A';
//if (c >= 'А' && c <= 'Я') return c + 'а' - 'А';
return c;
}