home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 11.ddi / CLASSSRC.ZIP / STRNG.CPP < prev   
Encoding:
C/C++ Source or Header  |  1991-02-13  |  5.7 KB  |  247 lines

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. // Contents ----------------------------------------------------------------
  4. //
  5. //      String::String                          copy constructor
  6. //      String::~String
  7. //      String::isEqual
  8. //      String::isLessThan
  9. //      String::isA
  10. //      String::nameOf
  11. //      String::hashValue
  12. //      String::printOn
  13. //         String::operator =
  14. //
  15. // Description
  16. //
  17. //      Contains the implementation of class String member functions.
  18. //
  19. // End ---------------------------------------------------------------------
  20.  
  21. // Interface Dependencies ---------------------------------------------------
  22.  
  23. #ifndef __CLSTYPES_H
  24. #include <clstypes.h>
  25. #endif
  26.  
  27. #ifndef __STRNG_H
  28. #include <strng.h>
  29. #endif
  30.  
  31. // End Interface Dependencies ------------------------------------------------
  32.  
  33. // Implementation Dependencies ----------------------------------------------
  34.  
  35. #ifndef __STDLIB_H
  36. #include <stdlib.h>
  37. #define __STDLIB_H
  38. #endif
  39.  
  40. #ifndef __STRING_H
  41. #include <string.h>
  42. #define __STRING_H
  43. #endif
  44.  
  45. // End Implementation Dependencies -------------------------------------------
  46.  
  47.  
  48. // Member Function //
  49.  
  50. String::String( const String& sourceString )
  51.  
  52. // Summary -----------------------------------------------------------------
  53. //
  54. //      Copy constructor
  55. //
  56. // Parameters
  57. //
  58. //     sourceString
  59. //
  60. //     The string we are to assign to this.
  61. //
  62. // End ---------------------------------------------------------------------
  63. {
  64.         len = sourceString.len;
  65.         theString = new char[ len ];
  66.         strcpy( theString, sourceString.theString );
  67. }
  68. // End Member Function String::operator = //
  69.  
  70. // Destructor //
  71.  
  72. String::~String()
  73.  
  74. // Summary -----------------------------------------------------------------
  75. //
  76. //      Destructor for a String object.  Deletes the storage for the
  77. //      character string.
  78. //
  79. // End ---------------------------------------------------------------------
  80. {
  81.     delete theString;
  82. }
  83. // End Destructor //
  84.  
  85.  
  86. // Member Function //
  87.  
  88. String::isEqual( const Object& testString ) const
  89.  
  90. // Summary -----------------------------------------------------------------
  91. //
  92. //     Determines whether two strings are equal.  Strings
  93. //      are equal if they are the same length and contain the same
  94. //      characters.
  95. //
  96. // Parameters
  97. //
  98. //      testString
  99. //
  100. //      The string we are testing against this.
  101. //
  102. // End ---------------------------------------------------------------------
  103. {
  104.     return ( len == ((String &)testString).len &&
  105.              !strcmp( theString, ((String &)testString).theString ) );
  106. }
  107. // End Member Function String::isEqual //
  108.  
  109.  
  110. // Member Function //
  111.  
  112. int String::isLessThan( const Object& testString ) const
  113.  
  114. // Summary -----------------------------------------------------------------
  115. //
  116. //      Determines whether the given string is less than this.  The strings
  117. //      need not be the same length.
  118. //
  119. // Parameters
  120. //
  121. //      testString
  122. //
  123. //      The string we are testing against this.
  124. //
  125. // End ---------------------------------------------------------------------
  126. {
  127.     return ( strcmp ( theString, ((String &)testString).theString ) < 0 );
  128. }
  129. // End Member Function String::isLessThan //
  130.  
  131.  
  132. // Member Function //
  133.  
  134. classType String::isA() const
  135.  
  136. // Summary -----------------------------------------------------------------
  137. //
  138. //      Returns a predefined value for the class String.
  139. //
  140. // Parameters
  141. //
  142. //      none
  143. //
  144. // End ---------------------------------------------------------------------
  145. {
  146.     return stringClass;
  147. }
  148. // End Member Function String::isA //
  149.  
  150.  
  151. // Member Function //
  152.  
  153. char *String::nameOf() const
  154.  
  155. // Summary -----------------------------------------------------------------
  156. //
  157. //      Returns the string "String".
  158. //
  159. // Parameters
  160. //
  161. //      none
  162. //
  163. // End ---------------------------------------------------------------------
  164. {
  165.     return "String";
  166. }
  167. // End Member Function String::nameOf //
  168.  
  169.  
  170. // Member Function //
  171.  
  172. hashValueType String::hashValue() const
  173.  
  174. // Summary -----------------------------------------------------------------
  175. //
  176. //      Returns the hash value of a string object.
  177. //
  178. // End ---------------------------------------------------------------------
  179. {
  180.     hashValueType    value = hashValueType(0);
  181.  
  182.     for( int i = 0; i < len; i++ )
  183.     {
  184.         value ^= theString[i];
  185.         value = _rotl( value, 1 );
  186.     }
  187.     return value;
  188. }
  189. // End Member Function String::hashValue //
  190.  
  191.  
  192. // Member Function //
  193.  
  194. void    String::printOn( ostream& outputStream ) const
  195.  
  196. // Summary -----------------------------------------------------------------
  197. //
  198. //      Displays this object on the given stream.
  199. //
  200. // Parameters
  201. //
  202. //      outputStream
  203. //
  204. //      The stream where we are to display the object.
  205. //
  206. // End ---------------------------------------------------------------------
  207. {
  208.     outputStream << theString;
  209. }
  210. // End Member Function String::printOn //
  211.  
  212.  
  213. // Member Function //
  214.  
  215. String& String::operator =( const String& sourceString )
  216.  
  217. // Summary -----------------------------------------------------------------
  218. //
  219. //      Assignment operator for strings.
  220. //
  221. // Parameters
  222. //
  223. //     sourceString
  224. //
  225. //     The string we are to assign to this.
  226. //
  227. // Functional Description
  228. //
  229. //     We check that we are doing s = s, then copy the contents of
  230. //     the source string into this string.
  231. //
  232. // End ---------------------------------------------------------------------
  233. {
  234.     if ( *this != sourceString )
  235.     {
  236.         if ( len != sourceString.len )
  237.         {
  238.             delete theString;
  239.             len = sourceString.len;
  240.             theString = new char[ len ];
  241.         }
  242.         (void)strcpy( theString, sourceString.theString );
  243.     }
  244.     return *this;
  245. }
  246. // End Member Function String::operator = //
  247.