home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / STRPP11 / STR.DOC < prev    next >
Text File  |  1992-03-24  |  11KB  |  269 lines

  1.  
  2.  
  3.                             String++ Version 1.1
  4.  
  5.                       Copyright 1992 by Carl Moreland
  6.                                  03/23/92
  7.  
  8.     String++ is a public domain string class written in C++.  The distribu-
  9. ted files are:
  10.  
  11.         str.h           header file for String++
  12.         str.cpp         source code for String++
  13.         str.doc         this file
  14.         str.his         revision history
  15.         str_test.exe    demonstration of strings
  16.         str_test.cpp    demo source
  17.  
  18. To  use  the string class, simply #include "str.h" in your source  and  add
  19. str.cpp to the project or make file. You may use and distribute  this  code
  20. freely, but all distributed copies must be unmodified,  and there may be no
  21. distribution  charges.  Comments, suggestions for improvement, and bug  re-
  22. ports may be sent to me via CompuServe (72137,2657), or mail them to:
  23.  
  24.         Carl Moreland
  25.         4314 Filmore Rd
  26.         Greensboro, NC 27409
  27.  
  28. ---------------------------------------------------------------------------
  29. ---------------------------------------------------------------------------
  30.  
  31. Introduction:
  32.  
  33. ---------------------------------------------------------------------------
  34.     Strings may be declared or initialized in the following ways:
  35.  
  36.         string str;                     // NULL string
  37.                str('H');                // single character
  38.                str = 'H';               // single character
  39.                str(ch);                 // single character ch
  40.                str = ch;                // single character ch
  41.                str(ch, 5);              // make 5 copies of ch
  42.                str("Hello World");      // char*
  43.                str = "Hello World";     // char*
  44.                str(ptr);                // char *ptr
  45.                str = ptr;               // char *ptr
  46.                str(ptr, 5);             // make 5 copies of ptr
  47.                str(str1);               // another string
  48.                str = str1;              // another string
  49.                str(str1, 5);            // make 5 copies of str
  50.  
  51. Unlike  many  other string classes, there is no provision  for  creating  a
  52. string  using only a length parameter, such as "string str(80)".  I see  no
  53. use  in this since the length would be dynamically adjusted as soon as  str
  54. is set to something useful.
  55.  
  56. The following operators are available:
  57.  
  58.         =       see above
  59.         ()      str() returns the char* of the string
  60.         []      str[i] returns the ith character of str
  61.         +  +=   concatenates
  62.         *  *=   multiple duplicates
  63.         == !=   comparisons
  64.         <  >    comparisons
  65.         <= >=   comparisons
  66.  
  67. The "+" operator and all comparison operators are declared as friend  func-
  68. tions.  This enables them to accept char, char*, and string as valid  argu-
  69. ments  on either side of the operator.  Therefore, each of these statements
  70. is valid:
  71.  
  72.         str1 = "Hello" + str2;
  73.         str1 = str2 + "World";
  74.         str1 = "Hello" + str2 + "World";
  75.         if(str1 == "Hello World") ...
  76.         if("Hello World" == str1) ...
  77.  
  78. Technically,  most  of the operators do not need  overloading  because  the
  79. string  constructors  will  automatically  convert the  arguments  to  type
  80. string.  The advantage is that fewer functions must be coded,  which  saves
  81. memory. The disadvantage is that there is a speed penalty because construc-
  82. tors (might) have to be called to convert arguments.  The class was  tested
  83. both ways and it was found that the added code was small, but the speed was
  84. more than doubled when using overloaded operators. Therefore, the operators
  85. are overloaded to accept string and char*.  There are no overloads for type
  86. char parameters because it would most probably be a rare case, and the con-
  87. structor can pick it up. Besides, the two following statements are entirely
  88. equivalent:
  89.  
  90.         if(str1 == 'a') ...
  91.         if(str1 == "a") ...
  92.  
  93.     Finally, the demo program str_test.cpp contains a suite of string mani-
  94. pulations which demonstrate most of the string functions.
  95.  
  96. ---------------------------------------------------------------------------
  97. ---------------------------------------------------------------------------
  98.  
  99. Class Functions:        
  100.  
  101. ---------------------------------------------------------------------------
  102. int length(void) const
  103. int len(void)    const
  104.  
  105. Both functions return the length of the string:
  106.  
  107.         str = "Hello World";
  108.         i = str.length();               // i = 11
  109.  
  110. ---------------------------------------------------------------------------
  111. string right(int len);
  112. string left(int len);
  113. string mid(int pos, int len);
  114.  
  115. These functions return substrings of a string:
  116.  
  117.         str = "This is a test";
  118.         str1 = str.left(4);             // str1 = "This"
  119.         str2 = str.mid(5, 4);           // str2 = "is a"
  120.         str3 = str.right(4);            // str3 = "test"
  121.  
  122. ---------------------------------------------------------------------------
  123. string justify(int mode, int len, int clip=0);
  124.  
  125. This function justifies a string by padding it with spaces (0x20) until  it
  126. is 'len' long.  If the original string had any leading  or  trailing  white
  127. spaces then they are first removed.  If the original string is longer  than
  128. 'len' (after removal of white spaces) then it is clipped to len if  clip=1,
  129. otherwise the trimmed string is returned.
  130.  
  131.         str = "Hello World";
  132.         str1 = str.justify(LEFT_JUSTIFY, 20);
  133.         str2 = str.justify(CENTER_JUSTIFY, 20);
  134.         str3 = str.justify(RIGHT_JUSTIFY, 20);
  135.     // str1 = "Hello World         "
  136.     // str1 = "    Hello World     "
  137.     // str1 = "         Hello World"
  138.  
  139. ---------------------------------------------------------------------------
  140. void toupper(void);
  141. void tolower(void);
  142.  
  143. These functions convert the string to upper or lower case,  and also return
  144. the converted string.
  145.  
  146.         str1 = "uppercase";
  147.         str2 = "LOWERCASE";
  148.         str1.toupper();                 // str1 is now "UPPERCASE"
  149.         str2.tolower();                 // str2 is now "lowercase"
  150.  
  151. ---------------------------------------------------------------------------
  152. ---------------------------------------------------------------------------
  153.  
  154. Awk-style Functions:
  155.  
  156. Note: AWK  begins array indexing at 1, whereas C/C++ begins array  indexing
  157.       at 0.  Therefore, most of the implemented AWK functions return values
  158.       that correspond to C-style indexing.  An AWK function that might nor-
  159.       mally return a 0 as a failure indicator will return a -1 here.
  160.  
  161. ---------------------------------------------------------------------------
  162. int length(string& s);
  163.  
  164. Returns the length of a string:
  165.  
  166.         str = "Hello World";
  167.         i = length(str);                // i = 11
  168.  
  169. ---------------------------------------------------------------------------
  170. int index(string& s, string& t);
  171.  
  172. Returns the position of 't' in 's' if it exists, or -1 if it doesn't:
  173.  
  174.         str = "d:\\prog\\str"
  175.         i = index(str, ":");            // i = 1
  176.  
  177. ---------------------------------------------------------------------------
  178. string substr(string& s, unsigned int p, unsigned int n=10000);
  179.  
  180. Returns the substring of 's' beginning at position 'p' with length 'n':
  181.  
  182.         str1 = "Don't just stand there...";
  183.         str2 = substr(str, 11, 5);      // str2 = "stand"
  184.  
  185. ---------------------------------------------------------------------------
  186. int split(string& s, string **a, string& fs);
  187.  
  188. Splits  string 's' into an array 'a' on field separator 'fs'.  Array 'a' is
  189. normally  declared  as  an uninitialized pointer in  the  calling  function
  190. (string *a),  and  the address of the pointer (&a) is  passed  to  split().
  191. split() will then allocate memory for the correct array size and return the
  192. number of fields:
  193.  
  194.         string *array;
  195.         str = "d:\\prog\\str";
  196.         i = split(str, &array, "\\";    // i = 3
  197.         // array[0] = "d:"
  198.     // array[1] = "prog"
  199.     // array[2] = "str"
  200.  
  201. ---------------------------------------------------------------------------
  202. int sub(string& from, string& to, string& str);
  203.  
  204. Substitute  'to'  for the leftmost substring of 'str' that  is  matched  by
  205. 'from'. Return the number of substitutions (0 or 1). This function actually
  206. calls gsub() with num=1;
  207.  
  208.         str = "d:\\prog\\str";
  209.         i = sub("\\", "/", str);        // i = 1
  210.         // str = "d:/prog\str"
  211.  
  212. ---------------------------------------------------------------------------
  213. int gsub(string& from, string& to, string& str, int num=10000);
  214.  
  215. Substitute 'to' for 'from' globally in string 'str' up to 'num' times.  Re-
  216. turn the number of substitutions.
  217.  
  218.         str = "d:\\prog\\str";
  219.         i = gsub("\\", "/", str);       // i = 2
  220.         // str = "d:/prog/str"
  221.  
  222. ---------------------------------------------------------------------------
  223. ---------------------------------------------------------------------------
  224.  
  225. C-Style Functions:
  226.  
  227. ---------------------------------------------------------------------------
  228. string toupper(string& s);
  229. string tolower(string& s);
  230.  
  231. These  functions return the upper- or lowercase versions of a  string.  The
  232. string itself is not modified.
  233.  
  234.         str1 = "uppercase";
  235.         str2 = "LOWERCASE";
  236.         str3 = toupper(str1);           // str3 = "UPPERCASE"
  237.         str4 = tolower(str2);           // str4 = "lowercase"
  238.  
  239. ---------------------------------------------------------------------------
  240. string right(string& s, int n);
  241. string left(string& s, int n);
  242. string mid(const string& s, int p, int n);
  243.  
  244. These functions return substrings of a string:
  245.  
  246.         str = "This is a test";
  247.         str1 = left(str, 4);            // str1 = "This"
  248.         str2 = mid(str, 5, 4);          // str2 = "is a"
  249.         str3 = right(str, 4);           // str3 = "test"
  250.  
  251. ---------------------------------------------------------------------------
  252. string justify(string& s, int mode, int len, int clip=0);
  253.  
  254. This function justifies a string by padding it with spaces (0x20) until  it
  255. is 'len' long.  If the original string had any leading  or  trailing  white
  256. spaces then they are first removed.  If the original string is longer  than
  257. 'len' (after removal of white spaces)  then it is clipped to len if clip=1,
  258. otherwise the trimmed string is returned.
  259.  
  260.         str = "Hello World";
  261.         str1 = justify(str, LEFT_JUSTIFY, 20);
  262.         str2 = justify(str, CENTER_JUSTIFY, 20);
  263.         str3 = justify(str, RIGHT_JUSTIFY, 20);
  264.     // str1 = "Hello World         "
  265.     // str1 = "    Hello World     "
  266.     // str1 = "         Hello World"
  267.  
  268. ---------------------------------------------------------------------------
  269.