home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Frameworks / MacZoop 1.6.5 / Basic Classes / Z Sources / ZString.cpp < prev    next >
Encoding:
Text File  |  1997-08-18  |  3.7 KB  |  167 lines  |  [TEXT/CWIE]

  1. //    ZString.cpp
  2. //    11/12/96
  3.  
  4. #include "ZString.h"
  5.  
  6. #define MIN(a,b) ((a)<(b)? a:b)
  7.  
  8. // string comparison function
  9. // return 1 if s1>s2, -1 if s1<s2, 0 if equal
  10. short JStrcmp( const ZString& s1, const ZString& s2 )
  11. {
  12.     for (int i=0; i<s1.buf[0] && i<s2.buf[0]; i++) {
  13.         if (s1.buf[i] != s2.buf[i]) {
  14.             return (s1.buf[i] < s2.buf[i] ? -1 : 1);
  15.         }
  16.     }
  17.     if (s1.buf[0]==s2.buf[0]) return 0;
  18.     return (s1.buf[0] < s2.buf[0] ? -1 : 1);
  19. }
  20.  
  21. // constructors
  22.  
  23. ZString::ZString( const long pNum )
  24. {
  25.     NumToString( pNum, buf );
  26. }
  27.  
  28. long IntValue( const ZString &s )
  29. {
  30.     long    temp=0;
  31.     StringToNum( s, &temp );
  32.     return temp;
  33. }
  34.  
  35. ZString Plural( const ZString &s )
  36. {
  37.     if (s.Right()=="s" || s.Right(2)=="ch" || s.Right(2)=="sh" || s.Right()=="x") return s+"es";
  38.     if (s.Right()=="y" && s.Right(2)!="ey") return s.Left(s.Length()-1)+"ies";
  39.     return s+"s";
  40. }
  41.  
  42. ZString& ZString::operator= ( const ZString& pStr )
  43. {
  44.     BlockMove(pStr.buf, buf, pStr.buf[0] + 1L);
  45.     return *this;
  46. }
  47.  
  48. void ZString::StuffPstr( unsigned char* pPstr, const int maxlen )
  49. {
  50.     long charsToMove = MIN(maxlen-1, buf[0]);
  51.     BlockMove(buf + 1, pPstr + 1, charsToMove);
  52.     pPstr[0] = charsToMove;
  53. }
  54.  
  55. ZString& ZString::operator= ( const char* pChars )
  56. {
  57.     short i=0;
  58.     for (; pChars[i]; i++) {
  59.         buf[i+1] = pChars[i];
  60.     }
  61.     buf[0] = i;
  62.     return *this;
  63. }
  64.  
  65. void ZString::StuffChars( char* pChars, const int maxlen )
  66. {
  67.     long charsToMove = MIN(maxlen-1, buf[0]);
  68.     BlockMove(buf + 1, pChars, charsToMove);
  69.     pChars[charsToMove] = 0;
  70. }
  71.  
  72. ZString& ZString::operator= ( const Str255 pPstr )
  73. {
  74.     BlockMove(pPstr, buf, pPstr[0] + 1L);
  75.     return *this;
  76. }
  77.  
  78. ZString& ZString::operator+= ( const ZString& pStr )
  79. {
  80.     long charsToCopy;
  81.  
  82.     // Truncate if concatenated string would be longer than 255 chars.
  83.     charsToCopy = MIN(pStr.buf[0], 255 - buf[0]);
  84.     BlockMove(pStr.buf + 1, buf + buf[0] + 1, charsToCopy);
  85.     buf[0] += charsToCopy;
  86.     return *this;
  87. }
  88.  
  89. ZString ZString::Substr ( const int pFrom, const int pTo ) const
  90. {
  91.     int    from = (pFrom < 1 ? 1 : pFrom);
  92.     if (from > buf[0]) return "\p";
  93.     int    to = (pTo > buf[0]+1 ? buf[0]+1 : pTo);
  94.     if (from > to) return "\p";
  95.  
  96.     ZString temp;
  97.     temp.buf[0] = to-from;
  98.     BlockMove(buf+from, temp.buf+1, temp.buf[0]);
  99.     return temp;
  100. }
  101.  
  102. ZString ZString::Element( const int pElem, const char pDelim ) const
  103. {
  104.     short i=0, p=0;
  105.     
  106.     // find occurances of pDelim in the string until i=pElem    
  107.     while (p<buf[0] && i<pElem) {
  108.         if (buf[++p] == pDelim) i++;
  109.     }
  110.     
  111.     // if i<pElem, then there aren't that many elements in the string
  112.     if (i<pElem) return "\p";
  113.     
  114.     // otherwise, return a string from here to the next occurance of the element
  115.     short p2=p+1;
  116.     while (p2<=buf[0] && buf[p2]!=pDelim) p2++;
  117.     return (*this)(p+(p!=0), p2);
  118. }
  119.  
  120. ZString ZString::Trim( void ) const
  121. {
  122.     // trim leading and trailing whitespace from the string
  123.     short p1=1;
  124.     while (p1<buf[0] && buf[p1]!=' ' && buf[p1]!='\t' && buf[p1]!='n') p1++;
  125.     
  126.     short p2=buf[0];
  127.     while (p2>0 && buf[p2]!=' ' && buf[p2]!='\t' && buf[p2]!='n') p2++;
  128.  
  129.     return (*this)(p1, p2-1);
  130. }
  131.  
  132. ZString ZString::Compress( void ) const
  133. {
  134.     // change all tabs and multiple spaces into single spaces
  135.     ZString temp;
  136.     Boolean onSpace = 0;
  137.     const unsigned char *src = buf+1;
  138.     unsigned char *dest = temp.buf+1;
  139.     while (*src) {
  140.         if (*src == ' ' || *src == '\t') {
  141.             if (!onSpace) {
  142.                 // start of a new space run...
  143.                 onSpace = 1;
  144.                 *dest++ = ' ';
  145.                 src++;
  146.             } else {
  147.                 // continuation of a space run -- don't copy; just advance the src pointer
  148.                 src++;
  149.             }            
  150.         } else {
  151.             // non-whitespace; copy char and terminate space run flag
  152.             *dest++ = *src++;
  153.             onSpace = 0;
  154.         }
  155.     }
  156.     temp[0] = dest-temp;
  157.     return temp;
  158. }
  159.  
  160. ZString Uppercase( const ZString& pStr )
  161. {
  162.     ZString out(pStr);
  163.     for (short i=1; i<pStr.buf[0]; i++)
  164.         if (pStr.buf[i] >= 'a' && pStr.buf[i] <= 'z') out.buf[i] -= ('a'-'A');
  165.     return out;
  166. }
  167.