DUILIB拆解 CStdString

chunyou128 2021-09-07 原文


DUILIB拆解 CStdString

StdString.h

#ifndef __UIBASE_H__
#define __UIBASE_H__


#pragma once

class CStdString
{
public:
    enum { MAX_LOCAL_STRING_LEN = 63 };

    CStdString();
    CStdString(const TCHAR ch);
    CStdString(const CStdString& src);
    CStdString(LPCTSTR lpsz, int nLen = -1);
    ~CStdString();

    void Empty();
    int GetLength() const;
    bool IsEmpty() const;
    TCHAR GetAt(int nIndex) const;
    void Append(LPCTSTR pstr);
    void Assign(LPCTSTR pstr, int nLength = -1);
    LPCTSTR GetData();

    void SetAt(int nIndex, TCHAR ch);
    operator LPCTSTR() const;

    TCHAR operator[] (int nIndex) const;
    const CStdString& operator=(const CStdString& src);
    const CStdString& operator=(const TCHAR ch);
    const CStdString& operator=(LPCTSTR pstr);
#ifdef _UNICODE
    const CStdString& CStdString::operator=(LPCSTR lpStr);
    const CStdString& CStdString::operator+=(LPCSTR lpStr);
#else
    const CStdString& CStdString::operator=(LPCWSTR lpwStr);
    const CStdString& CStdString::operator+=(LPCWSTR lpwStr);
#endif
    CStdString operator+(const CStdString& src) const;
    CStdString operator+(LPCTSTR pstr) const;
    const CStdString& operator+=(const CStdString& src);
    const CStdString& operator+=(LPCTSTR pstr);
    const CStdString& operator+=(const TCHAR ch);

    bool operator == (LPCTSTR str) const;
    bool operator != (LPCTSTR str) const;
    bool operator <= (LPCTSTR str) const;
    bool operator <  (LPCTSTR str) const;
    bool operator >= (LPCTSTR str) const;
    bool operator >  (LPCTSTR str) const;

    int Compare(LPCTSTR pstr) const;
    int CompareNoCase(LPCTSTR pstr) const;

    void MakeUpper();
    void MakeLower();

    CStdString Left(int nLength) const;
    CStdString Mid(int iPos, int nLength = -1) const;
    CStdString Right(int nLength) const;

    int Find(TCHAR ch, int iPos = 0) const;
    int Find(LPCTSTR pstr, int iPos = 0) const;
    int ReverseFind(TCHAR ch) const;
    int Replace(LPCTSTR pstrFrom, LPCTSTR pstrTo);

    int __cdecl Format(LPCTSTR pstrFormat, ...);
    int __cdecl SmallFormat(LPCTSTR pstrFormat, ...);

protected:
    LPTSTR m_pstr;
    TCHAR m_szBuffer[MAX_LOCAL_STRING_LEN + 1];
};


#endif // __UIBASE_H__

 StdString.cpp

#include "StdAfx.h"
#include "StdString.h"

#ifdef _DEBUG
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#endif


CStdString::CStdString() : m_pstr(m_szBuffer)
{
    m_szBuffer[0] = \'\0\';
}

CStdString::CStdString(const TCHAR ch) : m_pstr(m_szBuffer)
{
    m_szBuffer[0] = ch;
    m_szBuffer[1] = \'\0\';
}

CStdString::CStdString(LPCTSTR lpsz, int nLen) : m_pstr(m_szBuffer)
{      
    ASSERT(!::IsBadStringPtr(lpsz,-1) || lpsz==NULL);
    m_szBuffer[0] = \'\0\';
    Assign(lpsz, nLen);
}

CStdString::CStdString(const CStdString& src) : m_pstr(m_szBuffer)
{
    m_szBuffer[0] = \'\0\';
    Assign(src.m_pstr);
}

CStdString::~CStdString()
{
    if( m_pstr != m_szBuffer ) free(m_pstr);
}

int CStdString::GetLength() const
{ 
    return (int) _tcslen(m_pstr); 
}

CStdString::operator LPCTSTR() const 
{ 
    return m_pstr; 
}

void CStdString::Append(LPCTSTR pstr)
{
    int nNewLength = GetLength() + (int) _tcslen(pstr);
    if( nNewLength >= MAX_LOCAL_STRING_LEN ) {
        if( m_pstr == m_szBuffer ) {
            m_pstr = static_cast<LPTSTR>(malloc((nNewLength + 1) * sizeof(TCHAR)));
            _tcscpy(m_pstr, m_szBuffer);
            _tcscat(m_pstr, pstr);
        }
        else {
            m_pstr = static_cast<LPTSTR>(realloc(m_pstr, (nNewLength + 1) * sizeof(TCHAR)));
            _tcscat(m_pstr, pstr);
        }
    }
    else {
        if( m_pstr != m_szBuffer ) {
            free(m_pstr);
            m_pstr = m_szBuffer;
        }
        _tcscat(m_szBuffer, pstr);
    }
}

void CStdString::Assign(LPCTSTR pstr, int cchMax)
{
    if( pstr == NULL ) pstr = _T("");
    cchMax = (cchMax < 0 ? (int) _tcslen(pstr) : cchMax);
    if( cchMax < MAX_LOCAL_STRING_LEN ) {
        if( m_pstr != m_szBuffer ) {
            free(m_pstr);
            m_pstr = m_szBuffer;
        }
    }
    else if( cchMax > GetLength() || m_pstr == m_szBuffer ) {
        if( m_pstr == m_szBuffer ) m_pstr = NULL;
        m_pstr = static_cast<LPTSTR>(realloc(m_pstr, (cchMax + 1) * sizeof(TCHAR)));
    }
    _tcsncpy(m_pstr, pstr, cchMax);
    m_pstr[cchMax] = \'\0\';
}

bool CStdString::IsEmpty() const 
{ 
    return m_pstr[0] == \'\0\'; 
}

void CStdString::Empty() 
{ 
    if( m_pstr != m_szBuffer ) free(m_pstr);
    m_pstr = m_szBuffer;
    m_szBuffer[0] = \'\0\'; 
}

LPCTSTR CStdString::GetData()
{
    return m_pstr;
}

TCHAR CStdString::GetAt(int nIndex) const
{
    return m_pstr[nIndex];
}

TCHAR CStdString::operator[] (int nIndex) const
{ 
    return m_pstr[nIndex];
}   

const CStdString& CStdString::operator=(const CStdString& src)
{      
    Assign(src);
    return *this;
}

const CStdString& CStdString::operator=(LPCTSTR lpStr)
{      
    ASSERT(!::IsBadStringPtr(lpStr,-1));
    Assign(lpStr);
    return *this;
}

#ifdef _UNICODE

const CStdString& CStdString::operator=(LPCSTR lpStr)
{
    ASSERT(!::IsBadStringPtrA(lpStr,-1));
    int cchStr = (int) strlen(lpStr) + 1;
    LPWSTR pwstr = (LPWSTR) _alloca(cchStr);
    if( pwstr != NULL ) ::MultiByteToWideChar(::GetACP(), 0, lpStr, -1, pwstr, cchStr) ;
    Assign(pwstr);
    return *this;
}

const CStdString& CStdString::operator+=(LPCSTR lpStr)
{
    ASSERT(!::IsBadStringPtrA(lpStr,-1));
    int cchStr = (int) strlen(lpStr) + 1;
    LPWSTR pwstr = (LPWSTR) _alloca(cchStr);
    if( pwstr != NULL ) ::MultiByteToWideChar(::GetACP(), 0, lpStr, -1, pwstr, cchStr) ;
    Append(pwstr);
    return *this;
}

#else

const CStdString& CStdString::operator=(LPCWSTR lpwStr)
{      
    ASSERT(!::IsBadStringPtrW(lpwStr,-1));
    int cchStr = ((int) wcslen(lpwStr) * 2) + 1;
    LPSTR pstr = (LPSTR) _alloca(cchStr);
    if( pstr != NULL ) ::WideCharToMultiByte(::GetACP(), 0, lpwStr, -1, pstr, cchStr, NULL, NULL);
    Assign(pstr);
    return *this;
}

const CStdString& CStdString::operator+=(LPCWSTR lpwStr)
{
    ASSERT(!::IsBadStringPtrW(lpwStr,-1));
    int cchStr = ((int) wcslen(lpwStr) * 2) + 1;
    LPSTR pstr = (LPSTR) _alloca(cchStr);
    if( pstr != NULL ) ::WideCharToMultiByte(::GetACP(), 0, lpwStr, -1, pstr, cchStr, NULL, NULL);
    Append(pstr);
    return *this;
}

#endif // _UNICODE

const CStdString& CStdString::operator=(const TCHAR ch)
{
    Empty();
    m_szBuffer[0] = ch;
    m_szBuffer[1] = \'\0\';
    return *this;
}

CStdString CStdString::operator+(const CStdString& src) const
{
    CStdString sTemp = *this;
    sTemp.Append(src);
    return sTemp;
}

CStdString CStdString::operator+(LPCTSTR lpStr) const
{
    ASSERT(!::IsBadStringPtr(lpStr,-1));
    CStdString sTemp = *this;
    sTemp.Append(lpStr);
    return sTemp;
}

const CStdString& CStdString::operator+=(const CStdString& src)
{      
    Append(src);
    return *this;
}

const CStdString& CStdString::operator+=(LPCTSTR lpStr)
{      
    ASSERT(!::IsBadStringPtr(lpStr,-1));
    Append(lpStr);
    return *this;
}

const CStdString& CStdString::operator+=(const TCHAR ch)
{      
    TCHAR str[] = { ch, \'\0\' };
    Append(str);
    return *this;
}

bool CStdString::operator == (LPCTSTR str) const { return (Compare(str) == 0); };
bool CStdString::operator != (LPCTSTR str) const { return (Compare(str) != 0); };
bool CStdString::operator <= (LPCTSTR str) const { return (Compare(str) <= 0); };
bool CStdString::operator <  (LPCTSTR str) const { return (Compare(str) <  0); };
bool CStdString::operator >= (LPCTSTR str) const { return (Compare(str) >= 0); };
bool CStdString::operator >  (LPCTSTR str) const { return (Compare(str) >  0); };

void CStdString::SetAt(int nIndex, TCHAR ch)
{
    ASSERT(nIndex>=0 && nIndex<GetLength());
    m_pstr[nIndex] = ch;
}

int CStdString::Compare(LPCTSTR lpsz) const 
{ 
    return _tcscmp(m_pstr, lpsz); 
}

int CStdString::CompareNoCase(LPCTSTR lpsz) const 
{ 
    return _tcsicmp(m_pstr, lpsz); 
}

void CStdString::MakeUpper() 
{ 
    _tcsupr(m_pstr); 
}

void CStdString::MakeLower() 
{ 
    _tcslwr(m_pstr); 
}

CStdString CStdString::Left(int iLength) const
{
    if( iLength < 0 ) iLength = 0;
    if( iLength > GetLength() ) iLength = GetLength();
    return CStdString(m_pstr, iLength);
}

CStdString CStdString::Mid(int iPos, int iLength) const
{
    if( iLength < 0 ) iLength = GetLength() - iPos;
    if( iPos + iLength > GetLength() ) iLength = GetLength() - iPos;
    if( iLength <= 0 ) return CStdString();
    return CStdString(m_pstr + iPos, iLength);
}

CStdString CStdString::Right(int iLength) const
{
    int iPos = GetLength() - iLength;
    if( iPos < 0 ) {
        iPos = 0;
        iLength = GetLength();
    }
    return CStdString(m_pstr + iPos, iLength);
}

int CStdString::Find(TCHAR ch, int iPos /*= 0*/) const
{
    ASSERT(iPos>=0 && iPos<=GetLength());
    if( iPos != 0 && (iPos < 0 || iPos >= GetLength()) ) return -1;
    LPCTSTR p = _tcschr(m_pstr + iPos, ch);
    if( p == NULL ) return -1;
    return (int)(p - m_pstr);
}

int CStdString::Find(LPCTSTR pstrSub, int iPos /*= 0*/) const
{
    ASSERT(!::IsBadStringPtr(pstrSub,-1));
    ASSERT(iPos>=0 && iPos<=GetLength());
    if( iPos != 0 && (iPos < 0 || iPos > GetLength()) ) return -1;
    LPCTSTR p = _tcsstr(m_pstr + iPos, pstrSub);
    if( p == NULL ) return -1;
    return (int)(p - m_pstr);
}

int CStdString::ReverseFind(TCHAR ch) const
{
    LPCTSTR p = _tcsrchr(m_pstr, ch);
    if( p == NULL ) return -1;
    return (int)(p - m_pstr);
}

int CStdString::Replace(LPCTSTR pstrFrom, LPCTSTR pstrTo)
{
    CStdString sTemp;
    int nCount = 0;
    int iPos = Find(pstrFrom);
    if( iPos < 0 ) return 0;
    int cchFrom = (int) _tcslen(pstrFrom);
    int cchTo = (int) _tcslen(pstrTo);
    while( iPos >= 0 ) {
        sTemp = Left(iPos);
        sTemp += pstrTo;
        sTemp += Mid(iPos + cchFrom);
        Assign(sTemp);
        iPos = Find(pstrFrom, iPos + cchTo);
        nCount++;
    }
    return nCount;
}

int CStdString::Format(LPCTSTR pstrFormat, ...)
{
    CStdString sFormat = pstrFormat;
    // Do ordinary printf replacements
    // NOTE: Documented max-length of wvsprintf() is 1024
    TCHAR szBuffer[1025] = { 0 };
    va_list argList;
    va_start(argList, pstrFormat);
    int iRet = ::wvsprintf(szBuffer, sFormat, argList);
    va_end(argList);
    Assign(szBuffer);
    return iRet;
}

int CStdString::SmallFormat(LPCTSTR pstrFormat, ...)
{
    CStdString sFormat = pstrFormat;
    TCHAR szBuffer[64] = { 0 };
    va_list argList;
    va_start(argList, pstrFormat);
    int iRet = ::wvsprintf(szBuffer, sFormat, argList);
    va_end(argList);
    Assign(szBuffer);
    return iRet;
}



 

void CTestStringDlg::OnBnClickedOk()
{
	CStdString str;
	str = L"amtf";
	::MessageBox(0,str,0,0);
}

 

发表于
2014-10-29 22:08 
无名使者 
阅读(111
评论(0
编辑 
收藏 
举报

 

版权声明:本文为chunyou128原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/chunyou128/articles/4060716.html

DUILIB拆解 CStdString的更多相关文章

随机推荐

  1. Hadoop入门之hdfs

                                                        大数据 […]...

  2. 何为用户体验?附《用户体验的要素》PDF版下载

    一、什么是用户体验? 用户体验(User Experience,简称UE/UX)是用户在使用产品过程中建立起来 […]...

  3. SpringBoot整合Springfox-Swagger2

    前言 不管Spring Boot整合还是SpringMVC整合Swagger都基本类似,重点就在于配置Swag […]...

  4. 迁移学习之零次学习最新研究综述 | 前沿

    特约作者 : Innse 最近看了一段时间零次学习系列的文章,这里介绍一些代表性的论文,算是对这段时间学习的一 […]...

  5. 写技术博客那点事

    前言 写文章是一个短期收益少,长期收益很大的一件事情,人们总是高估短期收益,低估长期收益。往往是很多人坚持不下 […]...

  6. mfc架构的股票行情软件源代码

    C++写的股票行情软件,mfc架构。分为客户端、服务端、数据引擎三部分。代码加资源文件有1个多G,另外还有软件 […]...

  7. Eclipse/MyEclipse转IntelliJ IDEA完全攻略

      前言 安装 配置 基本技巧 常见问题 前言     Eclipse/MyEclipse曾经是Java开发的 […]...

  8. (原+转)linux安装rtl 8812au驱动 – darkknightzh

    (原+转)linux安装rtl 8812au驱动 转载请注明出处: http://www.cnblogs.co […]...

展开目录

目录导航