home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1997 November
/
PCWorld_1997-11_cd.bin
/
software
/
programy
/
komix
/
DATA.Z
/
String.hxx
< prev
next >
Wrap
Text File
|
1996-05-31
|
15KB
|
534 lines
/*---------------------------------------------------------------------------
*
* (c) Westmount Technology 1989, 1993
*
* File : @(#)String.hxx @(#)String.hxx 1.1 (1.4) (1.7)
* Description : String class
*
*---------------------------------------------------------------------------
SccsId = @(#)String.hxx 1.1 (1.4) (1.7) 09 Nov 1993 */
#ifndef STRING_HXX
#define STRING_HXX
#ifndef __STRING_H
# include <string.h>
#endif
#ifndef RANGE_HXX
# include <Range.hxx>
#endif
class String;
class SubString;
class istream;
class ostream;
class StringRep // reference counted String buffer
{
friend String;
public: // ctor, dtor, assignment
StringRep(void);
StringRep(const StringRep&);
StringRep(const char*);
StringRep(char* buf, unsigned buflen); // buf not copied!!
~StringRep(void);
const StringRep& operator= (const StringRep&);
public:
operator const char* () const { return p; }
inline int compare(const StringRep&) const;
inline int compare(const char*) const;
inline int collate(const StringRep&) const;
inline int collate(const char*) const;
unsigned hash(void) const;
void toAscii(void);
void toLower(void);
void toUpper(void);
public: // reference counting
void addRef(void) { refcount++; }
void removeRef(void) { if ( --refcount == 0 ) delete this; }
int shared() const { return refcount > 1; }
public: // buffer info
unsigned references() const { return refcount; }
unsigned buf_len() const { return len; }
const char* buf_ptr() const { return p; }
public: // buffer manipulation
void replace(const Range& r, const char* src, unsigned srclen);
void append(const char* src, unsigned srclen);
private: // data
unsigned refcount;
unsigned len;
char* p;
};
class SubString // Range of characters within a string
{
friend String;
public: // comparison functions
int compare(const char*) const;
int compare(const String&) const;
int compare(const SubString&) const;
int collate(const char*) const;
int collate(const String&) const;
int collate(const SubString&) const;
public: // operators
inline int operator< (const SubString& ss) const;
inline int operator> (const SubString& ss) const;
inline int operator<= (const SubString& ss) const;
inline int operator>= (const SubString& ss) const;
inline int operator== (const SubString& ss) const;
inline int operator!= (const SubString& ss) const;
inline int operator< (const String&) const;
inline int operator> (const String&) const;
inline int operator<= (const String&) const;
inline int operator>= (const String&) const;
inline int operator== (const String&) const;
inline int operator!= (const String&) const;
inline int operator< (const char* cs) const;
inline int operator> (const char* cs) const;
inline int operator<= (const char* cs) const;
inline int operator>= (const char* cs) const;
inline int operator== (const char* cs) const;
inline int operator!= (const char* cs) const;
friend int operator< (const char* cs, const SubString& ss);
friend int operator> (const char* cs, const SubString& ss);
friend int operator<= (const char* cs, const SubString& ss);
friend int operator>= (const char* cs, const SubString& ss);
friend int operator== (const char* cs, const SubString& ss);
friend int operator!= (const char* cs, const SubString& ss);
String operator+ (const String&) const;
String operator+ (const SubString&) const;
String operator+ (const char*) const;
String operator+ (char) const;
friend String operator+ (const char*, const SubString&);
friend String operator+ (char, const SubString&);
public: // assignment: modifies associated string
void operator= (const String&);
void operator= (const SubString&);
void operator= (const char*);
public: // access
unsigned length() const { return sr.length(); }
unsigned position() const { return sr.firstIndex(); }
inline const char* ptr() const;
public: // I/O
void dumpOn(ostream& strm) const;
void printOn(ostream& strm) const;
protected: // constructors
SubString(const SubString& ss): st(ss.st), sr(ss.sr) {}
SubString(const String&, const Range&);
protected:
void checkSubStr(void) const;
private: // data
String& st; // String this is a SubString of
Range sr; // Range of characters
};
class String
{
friend SubString;
protected:
void indexRangeErr(void) const;
public: // constructors
String(void);
String(const char*);
String(const char c);
String(const char c, unsigned repeat_count);
String(const String&);
String(const SubString&);
~String(void);
public: // conversion
operator const char* () const { return rep->buf_ptr(); }
const char* as_ptr() const { return rep->buf_ptr(); }
protected: // access to individual characters (index not checked)
char at(unsigned i) const { return rep->buf_ptr()[i]; }
char& at(unsigned i) { unique(); return rep->p[i]; }
public: // access operators ( check for overflow, see at() )
char& operator[] (unsigned i);
char operator[] (unsigned i) const;
public: // SubString operators
SubString operator() (unsigned pos, unsigned lgt);
const SubString operator() (unsigned pos, unsigned lgt) const;
SubString operator() (const Range& r);
const SubString operator() (const Range& r) const;
public: // operators
void operator= (const String&);
void operator= (const SubString&);
void operator= (const char*);
inline int operator< (const String& s) const;
inline int operator> (const String& s) const;
inline int operator<= (const String& s) const;
inline int operator>= (const String& s) const;
inline int operator== (const String& s) const;
inline int operator!= (const String& s) const;
inline int operator< (const SubString& ss) const;
inline int operator> (const SubString& ss) const;
inline int operator<= (const SubString& ss) const;
inline int operator>= (const SubString& ss) const;
inline int operator== (const SubString& ss) const;
inline int operator!= (const SubString& ss) const;
inline int operator< (const char* cs) const;
inline int operator> (const char* cs) const;
inline int operator<= (const char* cs) const;
inline int operator>= (const char* cs) const;
inline int operator== (const char* cs) const;
inline int operator!= (const char* cs) const;
friend int operator< (const char* cs, const String& s);
friend int operator> (const char* cs, const String& s);
friend int operator<= (const char* cs, const String& s);
friend int operator>= (const char* cs, const String& s);
friend int operator== (const char* cs, const String& s);
friend int operator!= (const char* cs, const String& s);
String operator+ (const String& s) const;
String operator+ (const SubString& ss) const;
String operator+ (const char* cs) const;
String operator+ (char c) const;
friend String operator+ (const char*, const String& s);
friend String operator+ (const char*, const SubString&);
friend String operator+ (char c, const String& s);
friend String operator+ (char c, const SubString& ss);
void operator+= (const String&);
void operator+= (const SubString&);
void operator+= (const char* cs);
void operator+= (char c);
public: // functions
unsigned length() const { return rep->buf_len(); }
unsigned capacity() const { return rep->buf_len(); }
unsigned hash(void) const { return rep->hash(); }
void toAscii(void) { unique(); rep->toAscii(); }
void toLower(void) { unique(); rep->toLower(); }
void toUpper(void) { unique(); rep->toUpper(); }
int index(char c, unsigned start_pos =0) const;
int rindex(char c, unsigned start_pos) const;
int rindex(char c) const { return rindex(c, rep->buf_len()); }
void printOn(ostream& strm) const;
void scanFrom(istream& strm);
protected: // function
void unique(void); // copy rep if shared
private: // data
StringRep* rep;
static StringRep* const theEmptyRep;
};
/*
** Substring inline functions
*/
inline const char*
SubString::ptr() const
{ return &st.rep->buf_ptr()[sr.firstIndex()]; }
inline void
SubString::operator=(const String& s)
{ st.unique(); st.rep->replace(sr, s, s.length()); }
inline void
SubString::operator=(const SubString& ss)
{ st.unique(); st.rep->replace(sr, ss.ptr(), ss.length()); }
inline void
SubString::operator=(const char* cs)
{ st.unique(); st.rep->replace(sr, cs, strlen(cs)); }
inline SubString
String::operator() (unsigned pos, unsigned lgt)
{ return SubString(*this, Range(pos, lgt)); }
inline const SubString
String::operator() (unsigned pos, unsigned lgt) const
{ return SubString(*this, Range(pos, lgt)); }
/*
** Comparison
*/
inline int
StringRep::compare(const StringRep& sr) const
{ return (&sr == this)? 0: strcmp(p, sr.p); }
inline int
StringRep::compare(const char* cp) const
{ return strcmp(p, cp); }
inline int
StringRep::collate(const StringRep& sr) const
{ return (&sr == this)? 0: strcoll(p, sr.p); }
inline int
StringRep::collate(const char* cp) const
{ return strcoll(p, cp); }
inline int
String::operator< (const String& s) const
{ return rep->collate(s.rep->buf_ptr()) < 0; }
inline int
SubString::operator< (const String& s) const
{ return collate(s) < 0; }
inline int
String::operator< (const SubString& ss) const
{ return ss.collate(*this) > 0; }
inline int
SubString::operator< (const SubString& ss) const
{ return collate(ss) < 0; }
inline int
String::operator< (const char* cp) const
{ return rep->collate(cp) < 0; }
inline int
SubString::operator< (const char* cp) const
{ return collate(cp) < 0; }
inline int
operator< (const char* cp, const String& s)
{ return s.rep->collate(cp) > 0; }
inline int
operator< (const char* cp, const SubString& ss)
{ return ss.collate(cp) > 0; }
inline int
String::operator> (const String& s) const
{ return rep->collate(s.rep->buf_ptr()) > 0; }
inline int
SubString::operator> (const String& s) const
{ return collate(s) > 0; }
inline int
String::operator> (const SubString& ss) const
{ return ss.collate(*this) < 0; }
inline int
SubString::operator> (const SubString& ss) const
{ return collate(ss) > 0; }
inline int
String::operator> (const char* cp) const
{ return rep->collate(cp) > 0; }
inline int
SubString::operator> (const char* cp) const
{ return collate(cp) > 0; }
inline int
operator> (const char* cp, const String& s)
{ return s.rep->collate(cp) < 0; }
inline int
operator> (const char* cp, const SubString& ss)
{ return ss.collate(cp) < 0; }
inline int
String::operator<= (const String& s) const
{ return rep->collate(s.rep->buf_ptr()) <= 0; }
inline int
SubString::operator<= (const String& s) const
{ return collate(s) <= 0; }
inline int
String::operator<= (const SubString& ss) const
{ return ss.collate(*this) >= 0; }
inline int
SubString::operator<= (const SubString& ss) const
{ return collate(ss) <= 0; }
inline int
String::operator<= (const char* cp) const
{ return rep->collate(cp) <= 0; }
inline int
SubString::operator<= (const char* cp) const
{ return collate(cp) <= 0; }
inline int
operator<= (const char* cp, const String& s)
{ return s.rep->collate(cp) >= 0; }
inline int
operator<= (const char* cp, const SubString& ss)
{ return ss.collate(cp) >= 0; }
inline int
String::operator>= (const String& s) const
{ return rep->collate(s.rep->buf_ptr()) >= 0; }
inline int
String::operator>= (const SubString& ss) const
{ return ss.collate(*this) <= 0; }
inline int
String::operator>= (const char* cp) const
{ return rep->collate(cp) >= 0; }
inline int
SubString::operator>= (const String& s) const
{ return collate(s) >= 0; }
inline int
SubString::operator>= (const SubString& ss) const
{ return collate(ss) >= 0; }
inline int
SubString::operator>= (const char* cp) const
{ return collate(cp) <= 0; }
inline int
operator>= (const char* cp, const String& s)
{ return s.rep->collate(cp) >= 0; }
inline int
operator>= (const char* cp, const SubString& ss)
{ return ss.collate(cp) >= 0; }
inline int
String::operator== (const String& s) const
{ return rep->compare(s.rep->buf_ptr()) == 0; }
inline int
String::operator== (const SubString& ss) const
{ return ss.compare(*this) == 0; }
inline int
String::operator== (const char* cp) const
{ return rep->compare(cp) == 0; }
inline int
SubString::operator== (const String& s) const
{ return compare(s) == 0; }
inline int
SubString::operator== (const SubString& ss) const
{ return compare(ss) == 0; }
inline int
SubString::operator== (const char* cp) const
{ return compare(cp) == 0; }
inline int
operator== (const char* cp, const String& s)
{ return s.rep->compare(cp) == 0; }
inline int
operator== (const char* cp, const SubString& ss)
{ return ss.compare(cp) == 0; }
inline int
String::operator!= (const String& s) const
{ return rep->compare(s.rep->buf_ptr()) != 0; }
inline int
String::operator!= (const SubString& ss) const
{ return ss.compare(*this) != 0; }
inline int
String::operator!= (const char* cp) const
{ return rep->compare(cp) != 0; }
inline int
SubString::operator!= (const String& s) const
{ return compare(s) != 0; }
inline int
SubString::operator!= (const SubString& ss) const
{ return compare(ss) != 0; }
inline int
SubString::operator!= (const char* cp) const
{ return compare(cp) != 0; }
inline int
operator!= (const char* cp, const String& s)
{ return s.rep->compare(cp) != 0; }
inline int
operator!= (const char* cp, const SubString& ss)
{ return ss.compare(cp) != 0; }
/*
** Stream I/O
*/
inline istream&
operator>> (istream& strm, String& s)
{
s.scanFrom(strm);
return strm;
}
inline ostream&
operator<< (ostream& strm, const String& s)
{
s.printOn(strm);
return(strm);
}
inline ostream&
operator<< (ostream& strm, const SubString& ss)
{
ss.printOn(strm);
return(strm);
}
#endif