Added rtrim, ltrim and trim to pstring. (nw)

This commit is contained in:
couriersud 2015-04-19 00:02:31 +02:00
parent f56db1ff3f
commit 5905f4d907
2 changed files with 56 additions and 0 deletions

View File

@ -88,6 +88,53 @@ pstring pstring::ucase() const
return ret;
}
int pstring::find_first_not_of(const pstring no) const
{
for (int i=0; i < len(); i++)
{
bool f = true;
for (int j=0; j < no.len(); j++)
if (m_ptr->m_str[i] == no.m_ptr->m_str[j])
f = false;
if (f)
return i;
}
return -1;
}
int pstring::find_last_not_of(const pstring no) const
{
for (int i=len() - 1; i >= 0; i--)
{
bool f = true;
for (int j=0; j < no.len(); j++)
if (m_ptr->m_str[i] == no.m_ptr->m_str[j])
f = false;
if (f)
return i;
}
return -1;
}
pstring pstring::ltrim(const pstring ws) const
{
int f = find_first_not_of(ws);
if (f>=0)
return substr(f);
else
return "";
}
pstring pstring::rtrim(const pstring ws) const
{
int f = find_last_not_of(ws);
if (f>=0)
return left(f+1);
else
return "";
}
//-------------------------------------------------
// pcmpi - compare a character array to an nstring
//-------------------------------------------------

View File

@ -155,6 +155,15 @@ public:
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 ucase() const;
// conversions