home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / STREX.CP_ / STREX.CP
Encoding:
Text File  |  1993-02-08  |  4.0 KB  |  183 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library. 
  2. // Copyright (C) 1992 Microsoft Corporation 
  3. // All rights reserved. 
  4. //  
  5. // This source code is only intended as a supplement to the 
  6. // Microsoft Foundation Classes Reference and Microsoft 
  7. // QuickHelp and/or WinHelp documentation provided with the library. 
  8. // See these sources for detailed information regarding the 
  9. // Microsoft Foundation Classes product. 
  10.  
  11.  
  12. #include "stdafx.h"
  13. #include <limits.h>
  14.  
  15. #ifdef AFX_AUX_SEG
  16. #pragma code_seg(AFX_AUX_SEG)
  17. #endif
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. #define new DEBUG_NEW
  25.  
  26. //////////////////////////////////////////////////////////////////////////////
  27. // More sophisticated construction
  28.  
  29. CString::CString(char ch, int nLength)
  30. {
  31.     if (nLength < 1)
  32.         // return empty string if invalid repeat count
  33.         Init();
  34.     else
  35.     {
  36.         AllocBuffer(nLength);
  37.         memset(m_pchData, ch, nLength);
  38.     }
  39. }
  40.  
  41.  
  42. CString::CString(const char* pch, int nLength)
  43. {
  44.     if (nLength == 0)
  45.         Init();
  46.     else
  47.     {
  48.         ASSERT(pch != NULL);
  49.         AllocBuffer(nLength);
  50.         memcpy(m_pchData, pch, nLength);
  51.     }
  52. }
  53.  
  54. //////////////////////////////////////////////////////////////////////////////
  55. // Additional constructors for far string data
  56.  
  57. #ifdef _NEARDATA
  58.  
  59. CString::CString(LPCSTR lpch, int nLen)
  60. {
  61.     if (nLen == 0)
  62.         Init();
  63.     else
  64.     {
  65.         AllocBuffer(nLen);
  66.         _fmemcpy(m_pchData, lpch, nLen);
  67.     }
  68. }
  69.  
  70. #endif //_NEARDATA
  71.  
  72. //////////////////////////////////////////////////////////////////////////////
  73. // Assignment operators
  74.  
  75. const CString& CString::operator =(char ch)
  76. {
  77.     AssignCopy(1, &ch);
  78.     return *this;
  79. }
  80.  
  81. //////////////////////////////////////////////////////////////////////////////
  82. // less common string expressions
  83.  
  84. CString AFXAPI operator +(const CString& string1, char ch)
  85. {
  86.     CString s;
  87.     s.ConcatCopy(string1.m_nDataLength, string1.m_pchData, 1, &ch);
  88.     return s;
  89. }
  90.  
  91.  
  92. CString AFXAPI operator +(char ch, const CString& string)
  93. {
  94.     CString s;
  95.     s.ConcatCopy(1, &ch, string.m_nDataLength, string.m_pchData);
  96.     return s;
  97. }
  98.  
  99.  
  100. //////////////////////////////////////////////////////////////////////////////
  101. // Very simple sub-string extraction
  102.  
  103. CString CString::Mid(int nFirst) const
  104. {
  105.     return Mid(nFirst, m_nDataLength - nFirst);
  106. }
  107.  
  108. CString CString::Mid(int nFirst, int nCount) const
  109. {
  110.     ASSERT(nFirst >= 0);
  111.     ASSERT(nCount >= 0);
  112.  
  113.     // out-of-bounds requests return sensible things
  114.     if (nFirst + nCount > m_nDataLength)
  115.         nCount = m_nDataLength - nFirst;
  116.     if (nFirst > m_nDataLength)
  117.         nCount = 0;
  118.  
  119.     CString dest;
  120.     AllocCopy(dest, nCount, nFirst, 0);
  121.     return dest;
  122. }
  123.  
  124. CString CString::Right(int nCount) const
  125. {
  126.     ASSERT(nCount >= 0);
  127.  
  128.     if (nCount > m_nDataLength)
  129.         nCount = m_nDataLength;
  130.  
  131.     CString dest;
  132.     AllocCopy(dest, nCount, m_nDataLength-nCount, 0);
  133.     return dest;
  134. }
  135.  
  136. CString CString::Left(int nCount) const
  137. {
  138.     ASSERT(nCount >= 0);
  139.  
  140.     if (nCount > m_nDataLength)
  141.         nCount = m_nDataLength;
  142.  
  143.     CString dest;
  144.     AllocCopy(dest, nCount, 0, 0);
  145.     return dest;
  146. }
  147.  
  148. // strspn equivalent
  149. CString CString::SpanIncluding(const char* pszCharSet) const
  150. {
  151.     ASSERT(pszCharSet != NULL);
  152.     return Left(strspn(m_pchData, pszCharSet));
  153. }
  154.  
  155.  
  156. // strcspn equivalent
  157. CString CString::SpanExcluding(const char* pszCharSet) const
  158. {
  159.     ASSERT(pszCharSet != NULL);
  160.     return Left(strcspn(m_pchData, pszCharSet));
  161. }
  162.  
  163. //////////////////////////////////////////////////////////////////////////////
  164. // Finding
  165.  
  166. int CString::ReverseFind(char ch) const
  167. {
  168.     // find a single character (start backwards, strrchr)
  169.     char* psz = (char*) strrchr(m_pchData, ch);
  170.     return (psz == NULL) ? -1 : (int)(psz - m_pchData);
  171. }
  172.  
  173.  
  174. // find a sub-string (like strstr)
  175. int CString::Find(const char* pszSub) const
  176. {
  177.     ASSERT(pszSub != NULL);
  178.     char* psz = (char*) strstr(m_pchData, pszSub);
  179.     return (psz == NULL) ? -1 : (int)(psz - m_pchData);
  180. }
  181.  
  182. ///////////////////////////////////////////////////////////////////////////////
  183.