首页 > 代码库 > [PRUI]第一个工程中第一个类·PRString
[PRUI]第一个工程中第一个类·PRString
第一个类PRString的Beta版本算是做好了,ANSI 和Unicode转换那一块还有BUG,还有几个小功能没补。
PRString 就是一字符串类,类似std::string
PRMisc.h
#pragma once#include "stdafx.h"#include "PRUI_CodeArea\PRUI_Import.h"template<class T>BOOLEAN PRPURE_API PRMemory_PutValue(PVOID pvMem,T dwObject){ if (IsBadWritePtr(pvMem, sizeof(T))) return FALSE; *(T*)pvMem = dwObject; return TRUE;} class PRUI_CLASSAPI PRString{private: LPWSTR szStringBase; DWORD dwStringLength;public: //Constructor PRString(); ~PRString(); PRString(LPWSTR szReference); PRString(LPSTR szReference); //String equal //For UnicodeString PRString& operator=(LPWSTR szString); //For AnsiString PRString& operator=(LPSTR szString); //Get/Set Character WCHAR operator[](size_t dwIndex); void ModifyCharacter(size_t dwIndex, WCHAR chNewChar); WCHAR IndexOf(size_t dwIndex); //String API DWORD LengthOf(); PRString& Duplicate(); //It‘s safe. LPWSTR GetString(); //Unsafe Method LPWSTR GetBuffer(); //Make it safe. void SyncBuffer(); LPSTR GetStringA(); //String Upper/Lower PRString& UpperString(BOOLEAN bUpdateThis); PRString& LowerString(BOOLEAN bUpdateThis); //String compare BOOLEAN Compare(PRString& szTarget, BOOLEAN bCaseInSensitive); bool operator == (PRString &szTarget); //Memory PRString& AllocateString(size_t dwSize, BOOLEAN bReAllocate, BOOLEAN bUpdateThis); PRString& WriteString (size_t dwLocation, LPWSTR szString, size_t dwCount, BOOLEAN bUpdateThis, LPDWORD lpdwCopied); //Reload PRString& WriteString (size_t dwLocation, PRString& szString, size_t dwCount, BOOLEAN bUpdateThis, LPDWORD lpdwCopied); //String append PRString& AppendString(PRString& szTarget, BOOLEAN bUpdateThis); PRString& operator + (PRString &szTarget); //Find //Replace //Trim from...to...};
PRMisc.cpp
#pragma once#include "stdafx.h"#include "PRUI_CodeArea\PRUI_Import.h"#include "PRMisc.h"#pragma region PRString Area //Constructor PRString::PRString() { PRString(""); } PRString::~PRString() { LocalFree((HLOCAL)szStringBase); szStringBase = 0; dwStringLength = 0; } PRString::PRString(LPWSTR szReference) { *this = szReference; } PRString::PRString(LPSTR szReference) { *this = szReference; } //String equal //For UnicodeString PRString& PRString::operator=(LPWSTR szString) { AllocateString(lstrlenW(szString), FALSE, TRUE); WriteString(0, szString, lstrlenW(szString), TRUE, 0); return *this; } //For AnsiString PRString& PRString::operator=(LPSTR szString) { dwStringLength = MultiByteToWideChar(CP_ACP, 0, szString, -1, 0, 0); AllocateString(dwStringLength, FALSE, TRUE); MultiByteToWideChar(CP_ACP, 0, szString, -1, GetBuffer(), dwStringLength); dwStringLength--; //ANSI String->Unicode String,本类以Unicode String为主 return *this; } //Get/Set Character WCHAR PRString::operator[](size_t dwIndex) { return szStringBase[dwIndex]; } void PRString::ModifyCharacter(size_t dwIndex, WCHAR chNewChar) { szStringBase[dwIndex] = chNewChar; if (chNewChar == ‘\0‘) SyncBuffer(); } WCHAR PRString::IndexOf(size_t dwIndex) { return this->operator[](dwIndex); } //String API DWORD PRString::LengthOf() { return dwStringLength; } PRString& PRString::Duplicate() { PRString* szObjRef = new PRString(GetBuffer()); PRString& szReturn = *szObjRef; return szReturn; } LPWSTR PRString::GetString() { return Duplicate().GetBuffer(); } //It‘s safe. LPWSTR PRString::GetBuffer() { return szStringBase; } void PRString::SyncBuffer() { dwStringLength = lstrlenW(szStringBase); } //Make it safe. LPSTR PRString::GetStringA() { DWORD dwAnsiStringLength; CHAR* szAnsiString; dwAnsiStringLength = WideCharToMultiByte(CP_ACP, 0, szStringBase, -1, 0, 0, 0, 0); szAnsiString = new CHAR[dwAnsiStringLength]; WideCharToMultiByte(CP_ACP, 0, szStringBase, -1, szAnsiString, dwAnsiStringLength, 0, 0); return szAnsiString; } PRString& PRString::UpperString(BOOLEAN bUpdateThis) { PRString& szTemp = bUpdateThis ? *this : Duplicate(); _wcsupr_s(szTemp.GetBuffer(), szTemp.LengthOf()); szTemp.SyncBuffer(); return szTemp; } PRString& PRString::LowerString(BOOLEAN bUpdateThis) { PRString& szTemp = bUpdateThis ? *this : Duplicate(); _wcslwr_s(szTemp.GetBuffer(), szTemp.LengthOf()); szTemp.SyncBuffer(); return szTemp; } //String compare BOOLEAN PRString::Compare(PRString& szTarget, BOOLEAN bCaseInSensitive) { PRString szTarget_ = szTarget, szDestination = *this; if (!bCaseInSensitive) { szTarget_ = szTarget_.UpperString(FALSE); szDestination = szDestination.UpperString(FALSE); } if (szTarget_.LengthOf() != szDestination.LengthOf()) return FALSE; for (size_t i = 0; i < szTarget_.LengthOf(); i++) { if (szTarget_[i] != szDestination[i]) return FALSE; } return TRUE; } bool PRString::operator == (PRString &szTarget) { return Compare(szTarget, 1) == 1 ? true : false; } //Memory PRString& PRString::AllocateString(size_t dwSize, BOOLEAN bReAllocate, BOOLEAN bUpdateThis) { PRString& szResult = bUpdateThis ? *this : Duplicate(); HANDLE hHeap = GetProcessHeap(); LPWSTR szBuffer = (LPWSTR)HeapAlloc(hHeap, 8, dwSize * sizeof(WCHAR) + 1); if (bReAllocate) lstrcpyW(szBuffer, this->GetBuffer()); szResult.szStringBase = szBuffer; szResult.dwStringLength = dwSize; return szResult; } PRString& PRString::WriteString (size_t dwLocation, LPWSTR szString, size_t dwCount, BOOLEAN bUpdateThis, LPDWORD lpdwCopied) { PRString& szTarget = bUpdateThis ? *this : Duplicate(); PRMemory_PutValue<int>((PVOID)lpdwCopied, 0); for (size_t i = dwLocation; i < (((dwLocation + dwCount - 1) > szTarget.LengthOf()) ? szTarget.LengthOf() : dwLocation + dwCount); i++) { szTarget.ModifyCharacter(i, szString[i - dwLocation]); PRMemory_PutValue<int>((PVOID)lpdwCopied, i - dwLocation + 1); } return szTarget; } //Reload PRString& PRString::WriteString (size_t dwLocation, PRString& szString, size_t dwCount, BOOLEAN bUpdateThis, LPDWORD lpdwCopied) { PRString& szTarget = bUpdateThis ? *this : Duplicate(); PRMemory_PutValue<int>((PVOID)lpdwCopied, 0); for (size_t i = dwLocation; i < (((dwLocation + dwCount - 1) > szTarget.LengthOf()) ? szTarget.LengthOf() : dwLocation + dwCount); i++) { szTarget.ModifyCharacter(i, szString[i - dwLocation]); PRMemory_PutValue<int>((PVOID)lpdwCopied, i - dwLocation + 1); } return szTarget; } //String append PRString& PRString::AppendString(PRString& szTarget, BOOLEAN bUpdateThis) { DWORD dwLengthTotal = szTarget.LengthOf() + this->LengthOf(); PRString& szResult = bUpdateThis ? *this : Duplicate(); szResult = szResult.AllocateString(dwLengthTotal, TRUE, TRUE); szResult.WriteString(this->LengthOf(), szTarget, szTarget.LengthOf(), TRUE, 0); return szResult; } PRString& PRString::operator + (PRString &szTarget) { return AppendString(szTarget, TRUE); }#pragma endregion
[PRUI]第一个工程中第一个类·PRString
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。