strcore.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 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 related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #include <SysUtils.hpp>
  12. //////////////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. CString::CString(const CString& stringSrc)
  15. {
  16. m_Data = stringSrc.m_Data;
  17. }
  18. void CString::Empty()
  19. {
  20. m_Data = EmptyStr;
  21. }
  22. //////////////////////////////////////////////////////////////////////////////
  23. // More sophisticated construction
  24. CString::CString(const wchar_t * lpsz)
  25. {
  26. m_Data = lpsz;
  27. }
  28. /////////////////////////////////////////////////////////////////////////////
  29. // Special conversion constructors
  30. CString::CString(const char * lpsz)
  31. {
  32. m_Data = UnicodeString(lpsz);
  33. }
  34. CString::CString(const UnicodeString& str)
  35. {
  36. m_Data = str;
  37. }
  38. //////////////////////////////////////////////////////////////////////////////
  39. // Assignment operators
  40. // All assign a new value to the string
  41. //
  42. // All routines return the new string (but as a 'const CString&' so that
  43. // assigning it again will cause a copy, eg: s1 = s2 = "hi there".
  44. //
  45. const CString& CString::operator=(const CString& stringSrc)
  46. {
  47. m_Data = stringSrc.m_Data;
  48. return *this;
  49. }
  50. const CString& CString::operator=(const wchar_t * lpsz)
  51. {
  52. m_Data = lpsz;
  53. return *this;
  54. }
  55. /////////////////////////////////////////////////////////////////////////////
  56. // Special conversion assignment
  57. const CString& CString::operator=(const char * lpsz)
  58. {
  59. m_Data = UnicodeString(lpsz);
  60. return *this;
  61. }
  62. //////////////////////////////////////////////////////////////////////////////
  63. // concatenation
  64. // NOTE: "operator+" is done as friend functions for simplicity
  65. // There are three variants:
  66. // CString + CString
  67. // and for ? = wchar_t, const wchar_t *
  68. // CString + ?
  69. // ? + CString
  70. CString operator+(const CString& string1, const CString& string2)
  71. {
  72. return CString(string1.m_Data + string2.m_Data);
  73. }
  74. CString operator+(const CString& string, const wchar_t * lpsz)
  75. {
  76. return CString(string.m_Data + lpsz);
  77. }
  78. CString operator+(const wchar_t * lpsz, const CString& string)
  79. {
  80. return CString(lpsz + string.m_Data);
  81. }
  82. //////////////////////////////////////////////////////////////////////////////
  83. // concatenate in place
  84. const CString& CString::operator+=(const wchar_t * lpsz)
  85. {
  86. m_Data += lpsz;
  87. return *this;
  88. }
  89. const CString& CString::operator+=(wchar_t ch)
  90. {
  91. m_Data += ch;
  92. return *this;
  93. }
  94. const CString& CString::operator+=(const CString& string)
  95. {
  96. m_Data += string.m_Data;
  97. return *this;
  98. }
  99. ///////////////////////////////////////////////////////////////////////////////
  100. // Commonly used routines (rarely used routines in STREX.CPP)
  101. int CString::Find(wchar_t ch) const
  102. {
  103. return Find(ch, 0);
  104. }
  105. int CString::Find(wchar_t ch, int nStart) const
  106. {
  107. int nLength = m_Data.Length();
  108. if (nStart >= nLength)
  109. return -1;
  110. // find first single character
  111. wchar_t * lpsz = wcschr(m_Data.c_str() + nStart, ch);
  112. // return -1 if not found and index otherwise
  113. return (lpsz == NULL) ? -1 : (int)(lpsz - m_Data.c_str());
  114. }
  115. int CString::FindOneOf(const wchar_t * lpszCharSet) const
  116. {
  117. wchar_t * lpsz = wcspbrk(m_Data.c_str(), lpszCharSet);
  118. return (lpsz == NULL) ? -1 : (int)(lpsz - m_Data.c_str());
  119. }
  120. void CString::MakeLower()
  121. {
  122. m_Data = m_Data.LowerCase();
  123. }
  124. void CString::SetAt(int nIndex, wchar_t ch)
  125. {
  126. ASSERT(nIndex >= 0);
  127. ASSERT(nIndex < m_Data.Length());
  128. // Implies Unique()
  129. m_Data[nIndex + 1] = ch;
  130. }
  131. ///////////////////////////////////////////////////////////////////////////////