//	11/01/95//	String.cxx#include "String.hxx"#include <stdio.h>	// for conversion routines (sprintf etc.)#define itsChars itsData+1#define itsLength itsData[0]// constructorsString::String( const int pInt ){	StringInit;	sprintf( itsChars, "%d", pInt );	itsLength = strlen(itsChars);}int IntValue( const String &s ){	if (!s.Length()) return 0;	int	temp=0;	sscanf( s.itsChars, "%d", &temp );	return temp;}String Plural( const String &s ){	if (s.Right()=="s" || s.Right(2)=="ch" || s.Right(2)=="sh" || s.Right()=="x") return s+"es";	return s+"s";}// assignment operatorsString& String::operator= ( const String& pStr ){    if (&pStr == this) return *this;    itsLength = pStr.Length();    strcpy( itsChars, pStr.itsChars );    return *this;}String& String::operator= ( const char* pChars ){	int l=strlen( pChars );	if (l < BUFSIZE-2) {	    itsLength = l;	    strcpy( itsChars, pChars );	} else {		itsLength = BUFSIZE-2;		strncpy( itsChars, pChars, BUFSIZE-2 );		itsData[BUFSIZE-1] = 0;	}    return *this;}#ifdef macintoshString& String::operator= ( const Str255 pPstr ){	memcpy( itsData, pPstr, pPstr[0]+1 );	itsData[ pPstr[0]+1 ] = 0;	return *this;}#endifString& String::operator+= ( const String& pStr ){    int newlen = Length() + pStr.Length();    if (newlen >= BUFSIZE-2) newlen = BUFSIZE-2;    if (newlen > Length()) {		strncat( itsChars, pStr.itsChars, newlen - Length() );		itsLength = newlen;    }    return *this;}String& String::operator+= ( const char* pChars ){    int newlen = Length() + strlen( pChars );    if (newlen >= BUFSIZE-2) newlen = BUFSIZE-2;     if (newlen > Length()) {		strncat( itsChars, pChars, newlen - Length() );		itsLength = newlen;    }    return *this;}String String::Substr ( const int pFrom, const int pTo ) const{	char	temp[BUFSIZE];	int	from = (pFrom < 0 ? 0 : pFrom);	if (from > Length()) return "";	int	to = (pTo > Length()-1 ? Length()-1 : pTo);	if (from > to) return "";	strncpy(temp, itsChars+from, to-from+1);	temp[to-from+1] = 0;	return temp;}String String::Element( const int pElem, const char pDelim ) const{	char* c;	int i;		// find occurances of pDelim in the string until i=pElem		for (i=0, c=itsChars-1; i<pElem && c; i++)		c = strchr( c+1, pDelim );		// if c is null, then there aren't that many elements in the string	if (!c) return "";		// otherwise, return a string from here to the next occurance of the element	c++;	char* c2 = strchr( c, pDelim );		if (!c2) return c;	return (*this)(c-(itsChars),c2-(itsChars)-1);}String String::Trim( void ) const{	// trim leading and trailing whitespace from the string	char	buf[BUFSIZE];	strcpy( buf, itsChars );	char	*c = buf;	while (*c==' ' || *c=='\t' || *c=='\n')		c++;	int ln = strlen(c)-1;	while (ln>-1 && (c[ln]==' ' || c[ln]=='\t' || c[ln]=='\n') )		ln--;	c[ln+1] = '\0';	return c;}// debugging routinesvoid String::Dump( void ) const{    cout << "[" << (int)itsData[0] << ":" << 	(const char *)(&itsData[1]) << "]\n";}