home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C!T ROM 2
/
ctrom_ii_b.zip
/
ctrom_ii_b
/
PROGRAM
/
C
/
STRPP11
/
STR.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1992-03-23
|
10KB
|
501 lines
/* -------------------------------------------------------------------- */
/* str.cpp */
/* */
/* String++ Version 1.1 Last revised 03/23/92 */
/* */
/* Enhanced string class for Turbo C++/Borland C++. */
/* Copyright 1991, 1992 by Carl W. Moreland */
/* This source code may be freely distributed as long as the copyright */
/* notice remains intact. */
/* -------------------------------------------------------------------- */
#include <ctype.h>
#include "str.h"
/* ----- Constructors/Destructors ------------------------------------- */
string::string()
{
_ptr = new char[1];
_ptr[0] = '\0';
}
string::string(const char c, unsigned int n)
{
_ptr = new char[n+1];
for(int i=0; i<n; i++)
_ptr[i] = c;
_ptr[i] = '\0';
}
string::string(const char *s, unsigned int n)
{
_ptr = new char[n*strlen(s)+1];
strcpy(_ptr, s);
for(int i=1; i<n; i++)
strcat(_ptr, s);
}
string::string(const string& s, unsigned int n)
{
_ptr = new char[n*s.len()+1];
strcpy(_ptr, s.ptr());
for(int i=1; i<n; i++)
strcat(_ptr, s.ptr());
}
string::~string(void)
{
if(_ptr)
delete _ptr;
}
/* -------------------------------------------------------------------- */
string string::right(int len) const
{
string tmp = this->mid(this->len()-len, len);
return tmp;
}
string string::left(int len) const
{
string tmp = this->mid(0, len);
return tmp;
}
string string::mid(int pos, int len) const
{
int this_len = this->len();
if(pos > this_len) // check for boundary errors
pos = this_len;
if(len > this_len - pos)
len = this_len - pos;
char *tmp = new char[len+1]; // allocate new memory
strncpy(tmp, _ptr+pos, len); // copy the substring
tmp[len] = '\0'; // need to NULL terminate
string str(tmp); // need to return a string, not char*
delete tmp; // done with tmp
return str;
}
string string::justify(int mode, int len, int clip) const
{
int begin, end, s_len;
begin = 0;
end = this->len()-1;
while(isspace(_ptr[begin])) // delete leading whitespace
begin++;
while(isspace(_ptr[end])) // delete trailing whitespace
end--;
s_len = end-begin+1;
string tmp = this->mid(begin, s_len); // get the rest of the string
if(s_len >= len && clip == 0) // check for out-of-bounds
return tmp;
if(s_len > len && clip == 1) // check for clipping
{
if(mode == LEFT_JUSTIFY)
tmp = tmp.left(len);
else if(mode == CENTER_JUSTIFY)
tmp = tmp.mid((tmp.len()-len)/2, len);
else if(mode == RIGHT_JUSTIFY)
tmp = tmp.right(len);
return tmp; // return clipped string
}
if(mode == LEFT_JUSTIFY)
tmp += string(' ', len-s_len);
else if(mode == CENTER_JUSTIFY)
tmp = string(' ', (len-s_len)/2) + tmp + string(' ', len - (len+s_len)/2);
else if(mode == RIGHT_JUSTIFY)
tmp = string(' ', len-s_len) + tmp;
return tmp; // return normal string
}
string& string::toupper(void)
{
for(int i=0; i<strlen(_ptr); i++)
_ptr[i] = ::toupper(_ptr[i]);
return *this;
}
string& string::tolower(void)
{
for(int i=0; i<strlen(_ptr); i++)
_ptr[i] = ::tolower(_ptr[i]);
return *this;
}
/* ----- Operators ---------------------------------------------------- */
// =
string& string::operator=(const char ch)
{
if(_ptr)
delete _ptr;
_ptr = new char[2];
_ptr[0] = ch;
_ptr[1] = '\0';
return *this;
}
string& string::operator=(const char *s)
{
char *tmp = _ptr;
_ptr = new char[strlen(s)+1];
strcpy(_ptr, s);
if(tmp)
delete tmp;
return *this;
}
string& string::operator=(const string& s)
{
char *tmp = _ptr;
_ptr = new char[s.len()+1];
strcpy(_ptr, s.ptr());
if(tmp)
delete tmp;
return *this;
}
// +
string operator+(const string& s1, const char *s2)
{
string tmp(s1);
tmp += s2;
return tmp;
}
string operator+(const char *s1, const string& s2)
{
string tmp(s1);
tmp += s2;
return tmp;
}
string operator+(const string& s1, const string& s2)
{
string tmp(s1);
tmp += s2;
return tmp;
}
// +=
string& string::operator+=(const char ch)
{
char *tmp = _ptr;
int this_len = this->len();
_ptr = new char[this_len + 2];
strcpy(_ptr, tmp);
_ptr[this_len] = ch;
_ptr[this_len+1] = '\0';
if(tmp)
delete tmp;
return *this;
}
string& string::operator+=(const char *s)
{
char *tmp = _ptr;
_ptr = new char[strlen(tmp) + strlen(s) + 1];
strcpy(_ptr, tmp);
strcat(_ptr, s);
if(tmp)
delete tmp;
return *this;
}
string& string::operator+=(const string& s)
{
char *tmp = _ptr;
_ptr = new char[strlen(tmp) + s.len() + 1];
strcpy(_ptr, tmp);
strcat(_ptr, s.ptr());
if(tmp)
delete tmp;
return *this;
}
// *
string operator*(const string& s1, int n)
{
string tmp(s1);
for(int i=1; i<n; i++)
tmp += s1;
return tmp;
}
// *=
string& string::operator*=(int n)
{
char *tmp = _ptr;
_ptr = new char[n*strlen(tmp) + 1];
strcpy(_ptr, tmp);
for(int i=1; i<n; i++)
strcat(_ptr, tmp);
if(tmp)
delete tmp;
return *this;
}
// []
const char string::operator[](int n) const
{
return *(_ptr + n);
}
// ==
int operator==(const string& s1, const char* s2)
{
return strcmp(s1._ptr,s2)==0;
}
int operator==(const char* s1, const string& s2)
{
return strcmp(s1,s2._ptr)==0;
}
int operator==(const string& s1, const string& s2)
{
return strcmp(s1._ptr,s2._ptr)==0;
}
// !=
int operator!=(const string& s1, const char *s2)
{
return strcmp(s1._ptr,s2)!=0;
}
int operator!=(const char *s1, const string& s2)
{
return strcmp(s1,s2._ptr)!=0;
}
int operator!=(const string& s1, const string& s2)
{
return strcmp(s1._ptr,s2._ptr)!=0;
}
// <
int operator<(const string& s1, const char *s2)
{
return strcmp(s1._ptr,s2)< 0;
}
int operator<(const char *s1, const string& s2)
{
return strcmp(s1,s2._ptr)< 0;
}
int operator<(const string& s1, const string& s2)
{
return strcmp(s1._ptr,s2._ptr)< 0;
}
// >
int operator>(const string& s1, const char *s2)
{
return strcmp(s1._ptr,s2)> 0;
}
int operator>(const char *s1, const string& s2)
{
return strcmp(s1,s2._ptr)> 0;
}
int operator>(const string& s1, const string& s2)
{
return strcmp(s1._ptr,s2._ptr)> 0;
}
// <=
int operator<=(const string& s1, const char *s2)
{
return strcmp(s1._ptr,s2)<=0;
}
int operator<=(const char *s1, const string& s2)
{
return strcmp(s1,s2._ptr)<=0;
}
int operator<=(const string& s1, const string& s2)
{
return strcmp(s1._ptr,s2._ptr)<=0;
}
// >=
int operator>=(const string& s1, const char *s2)
{
return strcmp(s1._ptr,s2)>=0;
}
int operator>=(const char *s1, const string& s2)
{
return strcmp(s1,s2._ptr)>=0;
}
int operator>=(const string& s1, const string& s2)
{
return strcmp(s1._ptr,s2._ptr)>=0;
}
/* -------------------------------------------------------------------- */
/* AWK-style functions */
/* -------------------------------------------------------------------- */
int index(const string& s, const string& t)
{
int pos;
char *tmp;
if((tmp = strstr(s.ptr(), t.ptr())) != NULL)
pos = (int)(tmp-(const)s.ptr());
else
pos = -1;
return pos;
}
string substr(const string& s, unsigned int p, unsigned int n)
{
string tmp = s.mid(p, n);
return tmp;
}
int split(const string& s, string **array, const string& fs)
{
int i=0, j=1, start=0;
int fs_len = fs.len();
int s_len = s.len();
string *tmp;
while(i < s_len)
{
if(strncmp(s(i), fs(), fs_len) == 0)
j++;
i++;
}
tmp = new string[j];
i = 0;
j = 0;
while(i < s_len)
{
if(strncmp(s(i), fs(), fs_len) == 0)
{
tmp[j++] = string(s.mid(start, i-start));
i += fs_len;
start = i;
}
else
i++;
}
tmp[j++] = s.mid(start, i-start);
*array = tmp;
return j;
}
int sub(const string& from, const string& to, string& str)
{
return gsub(from, to, str, 1);
}
int gsub(const string& from, const string& to, string& str, int count)
{
int i=0, j=0;
int from_len, to_len;
from_len = from.len();
to_len = to.len();
while(i <= str.len()-from_len)
{
if(strncmp(str(i), from(), from_len) == 0)
{
str = str.left(i) + to + str.right(str.len()-i-from_len);
i += to_len;
if(++j == count)
break;
}
else
i++;
}
return j;
}
/* -------------------------------------------------------------------- */
/* C-style functions */
/* -------------------------------------------------------------------- */
string toupper(const string& s)
{
string tmp(s);
tmp.toupper();
return tmp;
}
string tolower(const string& s)
{
string tmp(s);
tmp.tolower();
return tmp;
}
string right(const string& s, int len)
{
string tmp = s.mid(s.len()-len, len);
return tmp;
}
string left(const string& s, int len)
{
string tmp = s.mid(0, len);
return tmp;
}
string mid(const string& s, int pos, int len)
{
string tmp = s.mid(pos, len);
return tmp;
}
string justify(const string& s, int mode, int len, int clip)
{
string tmp = s.justify(mode, len, clip);
return tmp;
}