home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / NString 1.0 beta / Sources / NString_Transformation.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-20  |  2.1 KB  |  102 lines  |  [TEXT/KAHL]

  1.  
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. #include "NString.h"
  6. #include "NString_Misc.h"
  7.  
  8. //_________________________________________________________________________________
  9.  
  10. NString NString::upcase (void) const
  11. {
  12.     unsigned long int i;
  13.     NString result(sb->len);
  14.     
  15.     if (sb->len == 0)
  16.         return (*this);
  17.     
  18.     for (i=0; i < sb->len; i++)
  19.         result.sb->str[i] = toupper(sb->str[i]);
  20.  
  21.     return (result);
  22. }
  23.  
  24. //_________________________________________________________________________________
  25.  
  26. NString NString::lowcase (void) const
  27. {
  28.     unsigned long int i;
  29.     NString result(sb->len);
  30.     
  31.     if (sb->len == 0)    
  32.         return (*this);
  33.     
  34.     for (i=0; i < sb->len; i++)
  35.         result.sb->str[i] = tolower(sb->str[i]);
  36.  
  37.     return (result);
  38. }
  39.  
  40. //_________________________________________________________________________________
  41.  
  42. NString& NString::toupcase (void)
  43. {
  44.     unsigned long int i;
  45.  
  46.     if (sb->len == 0)
  47.         return *this;
  48.  
  49.     if (sb->refs > 1)                            // the string body is used by another NString: must duplicate it to prevent
  50.         if (! DuplicateSB())                    //        changes in common body
  51.             OUT_OF_MEM("toupcase (void)");
  52.  
  53.     for (i=0; i < sb->len; i++)
  54.         sb->str[i] = toupper(sb->str[i]);
  55.  
  56.     return *this;
  57. }
  58.  
  59. //_________________________________________________________________________________
  60.             
  61. NString& NString::tolowcase (void)
  62. {
  63.     unsigned long int i;
  64.  
  65.     if (sb->len == 0)
  66.         return *this;
  67.  
  68.     if (sb->refs > 1)                            // the string body is used by another NString: must duplicate it to prevent
  69.         if (! DuplicateSB())                    //        changes in common body
  70.             OUT_OF_MEM("tolowcase (void)");
  71.  
  72.     for (i=0; i < sb->len; i++)
  73.         sb->str[i] = tolower(sb->str[i]);
  74.  
  75.     return *this;
  76. }
  77.  
  78. //_________________________________________________________________________________
  79.  
  80. NString& NString::clear (void)
  81. {
  82.     const char *fname = "clear (void)";
  83.  
  84.     if (sb->len == 0)
  85.         return *this;
  86.  
  87.     if (sb->refs > 1)
  88.     {
  89.         if (! GetNewSB(0))                                // Get a new string body & inscribe terminating NUL and new length
  90.             OUT_OF_MEM(fname);
  91.             
  92.         return *this;
  93.     }
  94.     
  95.     if (! ReallocStrBuf(0))                            // Resize the current string body's buffer
  96.         OUT_OF_MEM(fname);
  97.         
  98.     sb->str[0] = '\0';                                    // Terminate the new (empty) string
  99.     sb->len = 0;                                            // Inscribe new length
  100.     return *this;
  101. }
  102.