📄 string.hpp
字号:
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - -
// - File: string.hpp. -
// - -
// - Contents: Implementation of string related fct's. -
// - -
// - Purpose: - -
// - -
// - Remarks: - -
// - -
// - Originator: Michael Mogensen, BassetSoft Denmark 2000. -
// - -
// - Compiler: MS Visual C++ ver6.0. -
// - -
// - Period: 00.00.00 - 00.00.00. -
// - -
// - Version: 1.00. -
// - -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - Miscellaneous. -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - Header(s). -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
#pragma once
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - Miscellaneous. -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
#define _UNICODE
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - Global(s), Predecleration(s). -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
inline const void cutchar_if_first(CString&, const TCHAR *pcRemoveEx = NULL);
inline const void cutchar_if_first(CString&, const TCHAR);
inline const void cutchar_if_last(CString&, const TCHAR *pcRemoveEx = NULL);
inline const void cutchar_if_last(CString&, const TCHAR);
inline const int chrinstr(const TCHAR*, const TCHAR);
inline const void first_upper_rest_lower(CString&);
inline const bool is_substr(const CString, const CString, const TCHAR);
inline const CString load_string(CONST UINT);
inline const void replacestr_n_first(CString&, const CString, const CString, const int);
inline const void replacestr_n_last(CString&, const CString, const CString, const int);
inline const size_t
substr(const CString, CString&, CONST UINT, const TCHAR);
template<class T> inline const size_t
substr(const CString, T&, CONST UINT, const TCHAR);
inline const CString& to_lower(CString&, const int, const int);
inline const CString& to_upper(CString&, const int, const int);
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - CStringEx. -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
class CStringEx : public CString
{
public:
// 1. Object. (alphabetical).
CStringEx() :
CString()
{}
CStringEx(const CString &strSource) :
CString(strSource)
{}
CStringEx(TCHAR chSource, int iRepeat = 1) :
CString(chSource, iRepeat)
{}
CStringEx(LPCSTR lpszSource) :
CString(lpszSource)
{}
CStringEx(LPCWSTR lpszSource) :
CString(lpszSource)
{}
CStringEx(LPCSTR lpszSource, int iLength) :
CString(lpszSource, iLength)
{}
CStringEx(LPCWSTR lpszSource, int iLength) :
CString(lpszSource, iLength)
{}
CStringEx(const unsigned char *lpszSource) :
CString(lpszSource)
{}
};
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - Global(s). -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
inline const void cutchar_if_first(CString &cstrText, const TCHAR *pcRemove)
// Removes first char. from the string iff it's cRemove. In case pcRemove is NULL it's always removed.
{
if(cstrText.GetLength() >= 1)
if(!pcRemove || cstrText[0] == *pcRemove)
cstrText.Format(TEXT("%s"), (LPCTSTR)cstrText.Right(cstrText.GetLength() - 1));
}
inline const void cutchar_if_first(CString &cstrText, const TCHAR cRemove)
// See desc. above.
{ cutchar_if_first(cstrText, &cRemove); }
inline const void cutchar_if_last(CString &cstrText, const TCHAR *pcRemove)
// Removes last char. from the string iff it's cRemove. In case pcRemove is NULL it's always removed.
{
if(cstrText.GetLength() >= 1)
if(!pcRemove || cstrText[cstrText.GetLength() - 1] == *pcRemove)
cstrText.Format(TEXT("%s"), (LPCTSTR)cstrText.Left(cstrText.GetLength() - 1));
}
inline const void cutchar_if_last(CString &cstrText, const TCHAR cRemove)
// See desc. above.
{ cutchar_if_last(cstrText, &cRemove); }
inline const void cutstr_if_last(CString &cstrText, const CString cstrText_Remove)
// Removes last part of cstrText iff it's cstrText_Remove.
{
if(cstrText.GetLength() >= cstrText_Remove.GetLength())
{
CString cstrText_Sub(_T(""));
cstrText_Sub = cstrText.Right(cstrText_Remove.GetLength());
if(cstrText_Sub == cstrText_Remove)
cstrText.Format(TEXT("%s"), (LPCTSTR)cstrText.Left(cstrText.GetLength() - cstrText_Remove.GetLength()));
}
}
inline const int chrinstr(const TCHAR *pszSource, const TCHAR cLookFor)
// Return number of occurences of cLookFor found in pszSource.
{
TCHAR *pszTarget_ = const_cast<TCHAR*>(pszSource);
int iCharCount = 0;
while((pszTarget_ = ::strchr(pszTarget_, cLookFor)) != NULL)
{ iCharCount++; pszTarget_++; }
return iCharCount;
}
inline const void first_upper_rest_lower(CString &cstrText)
// Format string so that first letter is made uppercase and the rest is made lowercase.
{
CString cstrTextEx(cstrText[0]);
cstrTextEx.MakeUpper();
cstrText.MakeLower();
cstrTextEx += cstrText.Right(cstrText.GetLength() - 1);
cstrText = cstrTextEx;
}
inline const bool is_substr(const CString cstrSource, const CString cstrCandidate, const TCHAR cDelim)
// Look for substring candidate in source string. Return T on found candidate and F if not.
{
CString cstrTarget(_T(""));
for(UINT uPart_2Lookup = 0; 1; uPart_2Lookup++)
if(!substr(cstrSource, cstrTarget, uPart_2Lookup, cDelim))
// Not found candidate (end of search).
return false;
else
if(cstrCandidate == cstrTarget)
// Found candidate.
return true;
}
inline const CString load_string(CONST UINT uId)
// Return entry from STRINGTABLE or empty string on not found.
{
CString cstrEntry(_T(""));
cstrEntry.LoadString(uId);
if(cstrEntry.IsEmpty())
{
cstrEntry.Format(TEXT("<no string>"));
}
return cstrEntry;
}
inline const void replacestr_n_first(
CString &cstrText,
const CString cstrText_Replace,
const CString cstrText_ReplaceWith,
const int iN)
// Replace up to iN instances of cstrText_Replace in cstrText with cstrText_ReplaceWith - starting search from the beginning.
{
int iLoc = 0;
CString cstrText_Right_Sub(_T("")),
cstrText_Left_Sub(_T(""));
for(int iId = 0; iId < iN && (iLoc = cstrText.Find(cstrText_Replace)) != -1; iId++)
{
cstrText_Left_Sub = cstrText.Left(iLoc);
cstrText_Right_Sub = cstrText.Right(cstrText.GetLength() - (cstrText_Left_Sub.GetLength() + cstrText_Replace.GetLength()));
cstrText.Format(
TEXT("%s%s%s"),
(LPCTSTR)cstrText_Left_Sub,
(LPCTSTR)cstrText_ReplaceWith,
(LPCTSTR)cstrText_Right_Sub);
}
}
inline const void replacestr_n_last(
CString &cstrText,
const CString cstrText_Replace,
const CString cstrText_ReplaceWith,
const int iN)
// Replace up to iN instances of cstrText_Replace in cstrText with cstrText_ReplaceWith - starting search from the end.
{
int iLoc = 0;
CString cstrText_Rev(cstrText),
cstrText_Rev_Replace(cstrText_Replace),
cstrText_Rev_ReplaceWith(cstrText_ReplaceWith),
cstrText_Rev_Right_Sub(_T("")),
cstrText_Rev_Left_Sub(_T(""));
cstrText.Empty();
cstrText_Rev.MakeReverse();
cstrText_Rev_Replace.MakeReverse();
cstrText_Rev_ReplaceWith.MakeReverse();
for(int iId = 0; iId < iN && (iLoc = cstrText_Rev.Find(cstrText_Rev_Replace)) != -1; iId++)
{
cstrText_Rev_Left_Sub = cstrText_Rev.Left(iLoc);
cstrText_Rev_Right_Sub = cstrText_Rev.Right(cstrText_Rev.GetLength() - (cstrText_Rev_Left_Sub.GetLength() + cstrText_Rev_Replace.GetLength()));
cstrText_Rev.Format(
TEXT("%s%s%s"),
(LPCTSTR)cstrText_Rev_Left_Sub,
(LPCTSTR)cstrText_Rev_ReplaceWith,
(LPCTSTR)cstrText_Rev_Right_Sub);
}
cstrText_Rev.MakeReverse();
cstrText = cstrText_Rev;
}
inline const void strrep(TCHAR *pszSource, const TCHAR cLookFor, const TCHAR cReplaceWith)
// Replace all occurences of cLookFor in pszSource with cReplaceWith
{
TCHAR *pCurrent = pszSource;
if(cLookFor != cReplaceWith)
while((pCurrent = ::strchr(pCurrent, cLookFor)) != NULL)
*pCurrent = cReplaceWith;
}
inline const size_t substr(const CString cstrSource, CString &cstrTarget, CONST UINT uPart_2Lookup, const TCHAR cDelim)
// Get substring no. uPart_2Lookup of cstrSource and place it in cstrTarget. Substrings are separated by cDelim. Return length of substring.
{
if(cstrSource.IsEmpty())
return 0;
cstrTarget.Empty();
int iLoc_Begin = -1,
iLoc_End = -1;
if(uPart_2Lookup == 0)
{
if(cstrSource[0] != cDelim)
{
iLoc_Begin = 0;
iLoc_End = cstrSource.Find(cDelim, 1);
}
}
else
{
for(UINT uPart = 0; uPart < uPart_2Lookup; uPart++)
{
iLoc_Begin = cstrSource.Find(cDelim, iLoc_Begin + 1);
iLoc_End = cstrSource.Find(cDelim, iLoc_Begin + 1);
if(iLoc_Begin == -1)
break;
}
if(iLoc_Begin != -1)
iLoc_Begin++;
}
if(iLoc_Begin != -1)
{
if(iLoc_End == -1)
iLoc_End = cstrSource.GetLength();
cstrTarget = cstrSource.Mid(iLoc_Begin, iLoc_End - iLoc_Begin);
}
return cstrTarget.GetLength();
}
template<class T>
inline const size_t substr(const CString cstrSource, T &Target, CONST UINT uPart_2Lookup, const TCHAR cDelim)
// Same as above, though generic.
{
CString cstrTarget(_T(""));
const size_t uLength = substr(cstrSource, cstrTarget, uPart_2Lookup, cDelim);
Target = (T)::atoi((LPCTSTR)cstrTarget);
return uLength;
}
inline const CString& to_lower(CString &cstrString,
const int iId_A, // Convert from this char.
const int iId_B) // Convert to this char.
// Bring substring into lower case.
{
if(iId_A <= iId_B)
{
CString cstrString_New(_T(""));
if(iId_A > 0)
{
cstrString_New += cstrString.Left(iId_A);
}
for(int iId = iId_A; iId <= iId_B; iId++)
{
cstrString_New += (TCHAR)::tolower((int)(cstrString[iId]));
}
const int iLength = cstrString.GetLength();
if(iId_B < iLength)
{
cstrString_New += cstrString.Right(iLength - iId_B - 1);
}
cstrString = cstrString_New;
}
return cstrString;
}
inline const CString& to_upper(CString &cstrString,
const int iId_A, // Convert from this char.
const int iId_B) // Convert to this char.
// Bring substring into upper case.
{
if(iId_A <= iId_B)
{
CString cstrString_New(_T(""));
if(iId_A > 0)
{
cstrString_New += cstrString.Left(iId_A);
}
for(int iId = iId_A; iId <= iId_B; iId++)
{
cstrString_New += (TCHAR)::toupper((int)(cstrString[iId]));
}
const int iLength = cstrString.GetLength();
if(iId_B < iLength)
{
cstrString_New += cstrString.Right(iLength - iId_B - 1);
}
cstrString = cstrString_New;
}
return cstrString;
}
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// 0. Data. (alphabetical).
// 1. Object. (alphabetical).
// 2. Event's. (alphabetical).
// 3.0. Menu choice. (menu order).
// 3.1. Menu choice, enablers. (menu order).
// 4. Slaves. (alphabetical).
// 5. Other. (alphabetical).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -