⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basicstr.h

📁 winddk src目录下的WDM源码压缩!
💻 H
📖 第 1 页 / 共 3 页
字号:
template <class CharType>
CBasicStringBase<CharType>::~CBasicStringBase()
{
    Destroy();
}

template <class CharType>
void CBasicStringBase<CharType>::DeleteStorage()
{
    //
    // Only delete the string if it is non-NULL and not pointing to our non-dynamically allocated buffer
    //
    if (m_pstrData && m_pstrData != m_pstrAutoData)
    {
        delete[] m_pstrData;
    }
    m_pstrData = NULL;
}

template <class CharType>
CharType *CBasicStringBase<CharType>::CreateStorage( size_t nCount, size_t &nAllocated )
{
#if defined(SIMSTR_UNIT_TEST)
    if (m_bForceError)
    {
        m_bError = true;
        return NULL;
    }
#endif

    CharType *pResult = NULL;
    nAllocated = 0;

    //
    // If we are currently pointing to our fixed buffer, or the requested
    // size is greater than our fixed-length buffer, allocate using new.
    // Otherwise, return the address of our fixed-length buffer.
    //
    if (m_pstrData == m_pstrAutoData || nCount > ARRAYSIZE(m_pstrAutoData))
    {
        pResult = new CharType[nCount];
        if (pResult)
        {
            nAllocated = nCount;
        }
    }
    else
    {
        pResult = m_pstrAutoData;
        nAllocated = ARRAYSIZE(m_pstrAutoData);
    }
    if (!pResult)
    {
        m_bError = true;
    }

    return pResult;
}

template <class CharType>
void CBasicStringBase<CharType>::Destroy()
{
    DeleteStorage();
    m_nMaxSize = 0;
}

template <class CharType>
size_t CBasicStringBase<CharType>::Length() const
{
    return(m_pstrData ? GenericLength(m_pstrData) : 0);
}

template <class CharType>
CBasicStringBase<CharType> &CBasicStringBase<CharType>::operator=( const CBasicStringBase &other )
{
    if (&other != this)
    {
        Assign(other);
    }
    return *this;
}

template <class CharType>
CBasicStringBase<CharType> &CBasicStringBase<CharType>::operator=( const CharType *other )
{
    if (other != String())
    {
        Assign(other);
    }
    return *this;
}

template <class CharType>
CBasicStringBase<CharType> &CBasicStringBase<CharType>::operator+=( const CBasicStringBase &other )
{
    Concat(other.String());
    
    return *this;
}

template <class CharType>
bool CBasicStringBase<CharType>::Assign( const CharType *szStr )
{
    if (szStr && EnsureLength(GenericLength(szStr)+1))
    {
        GenericCopyLength(m_pstrData,szStr,GenericLength(szStr)+1);
    }
    else if (EnsureLength(1))
    {
        *m_pstrData = 0;
    }
    else Destroy();
    return OK();
}

template <class CharType>
bool CBasicStringBase<CharType>::Assign( const CBasicStringBase &other )
{
    Assign( other.String() );

    PropagateError( other );

    return OK();
}

template <class CharType>
void CBasicStringBase<CharType>::SetAt( size_t nIndex, CharType chValue )
{
    //
    // Make sure we don't go off the end of the string or overwrite the '\0'
    //
    if (Length() > nIndex)
    {
        m_pstrData[nIndex] = chValue;
    }
}


template <class CharType>
CharType CBasicStringBase<CharType>::GetAt( size_t nIndex ) const
{
    return m_pstrData[nIndex];
}


template <class CharType>
void CBasicStringBase<CharType>::Concat( const CBasicStringBase &other )
{
    if (EnsureLength( Length() + other.Length() + 1 ))
    {
        GenericConcatenate( m_pstrData, other.String() );

        PropagateError( other );
    }
}

template <class CharType>
CBasicStringBase<CharType> &CBasicStringBase<CharType>::MakeUpper()
{
    //
    // Make sure the string is not NULL
    //
    if (m_pstrData)
    {
        IS_CHAR(*m_pstrData) ? CharUpperBuffA( (LPSTR)m_pstrData, (DWORD)Length() ) : CharUpperBuffW( (LPWSTR)m_pstrData, (DWORD)Length() );
    }
    return *this;
}

template <class CharType>
CBasicStringBase<CharType> &CBasicStringBase<CharType>::MakeLower()
{
    //
    // Make sure the string is not NULL
    //
    if (m_pstrData)
    {
        IS_CHAR(*m_pstrData) ? CharLowerBuffA( (LPSTR)m_pstrData, (DWORD)Length() ) : CharLowerBuffW( (LPWSTR)m_pstrData, (DWORD)Length() );
    }
    return *this;
}

template <class CharType>
CBasicStringBase<CharType> CBasicStringBase<CharType>::ToUpper() const
{
    CBasicStringBase str(*this);
    str.MakeUpper();
    return str;
}

template <class CharType>
CBasicStringBase<CharType> CBasicStringBase<CharType>::ToLower() const
{
    CBasicStringBase str(*this);
    str.MakeLower();
    return str;
}

template <class CharType>
CharType &CBasicStringBase<CharType>::operator[](int nIndex)
{
    return m_pstrData[nIndex];
}

template <class CharType>
const CharType &CBasicStringBase<CharType>::operator[](int index) const
{
    return m_pstrData[index];
}

template <class CharType>
int CBasicStringBase<CharType>::Find( CharType cChar ) const
{
    CharType strTemp[2] = { cChar, 0};
    return Find(strTemp);
}


template <class CharType>
int CBasicStringBase<CharType>::Find( const CBasicStringBase &other, size_t nStart ) const
{
    if (!m_pstrData)
        return -1;
    if (nStart > Length())
        return -1;
    CharType *pstrCurr = m_pstrData+nStart, *pstrSrc, *pstrSubStr;
    while (*pstrCurr)
    {
        pstrSrc = pstrCurr;
        pstrSubStr = (CharType *)other.String();
        while (*pstrSrc && *pstrSubStr && *pstrSrc == *pstrSubStr)
        {
            pstrSrc = GenericCharNext(pstrSrc);
            pstrSubStr = GenericCharNext(pstrSubStr);
        }
        if (!*pstrSubStr)
            return static_cast<int>(pstrCurr-m_pstrData);
        pstrCurr = GenericCharNext(pstrCurr);
    }
    return -1;
}

template <class CharType>
int CBasicStringBase<CharType>::ReverseFind( CharType cChar ) const
{
    CharType strTemp[2] = { cChar, 0};
    return ReverseFind(strTemp);
}

template <class CharType>
int CBasicStringBase<CharType>::ReverseFind( const CBasicStringBase &srcStr ) const
{
    int nLastFind = -1, nFind=0;
    while ((nFind = Find( srcStr, nFind )) >= 0)
    {
        nLastFind = nFind;
        ++nFind;
    }
    return nLastFind;
}

template <class CharType>
CBasicStringBase<CharType> CBasicStringBase<CharType>::SubStr( size_t nStart, size_t nCount ) const
{
    if (nStart >= Length())
    {
        return CBasicStringBase<CharType>();
    }
    
    nCount = min( Length(), nCount );
    
    CBasicStringBase<CharType> strResult;    
    CharType *pszBuffer = strResult.GetBuffer(nCount);
    if (pszBuffer)
    {
        GenericCopyLength( pszBuffer, m_pstrData+nStart, nCount+1 );
    }
    return strResult;
}

template <class CharType>
CBasicStringBase<CharType> CBasicStringBase<CharType>::SubStr( size_t nStart ) const
{
    return SubStr( nStart, Length() - nStart );
}


template <class CharType>
int CBasicStringBase<CharType>::CompareNoCase( const CBasicStringBase &other, int cchLength ) const
{
    if (cchLength < 0)
    {
        //
        // Make sure both strings are non-NULL
        //
        if (!String() && !other.String())
        {
            return 0;
        }
        else if (!String())
        {
            return -1;
        }
        else if (!other.String())
        {
            return 1;
        }
        else return GenericCompareNoCase(m_pstrData,other.String());
    }
    CBasicStringBase<CharType> strSrc(*this);
    CBasicStringBase<CharType> strTgt(other);
    strSrc.MakeUpper();
    strTgt.MakeUpper();
    //
    // Make sure both strings are non-NULL
    //
    if (!strSrc.String() && !strTgt.String())
    {
        return 0;
    }
    else if (!strSrc.String())
    {
        return -1;
    }
    else if (!strTgt.String())
    {
        return 1;
    }
    else return GenericCompareLength(strSrc.String(),strTgt.String(),cchLength);
}


template <class CharType>
int CBasicStringBase<CharType>::Compare( const CBasicStringBase &other, int cchLength ) const
{
    //
    // Make sure both strings are non-NULL
    //
    if (!String() && !other.String())
    {
        return 0;
    }
    else if (!String())
    {
        return -1;
    }
    else if (!other.String())
    {
        return 1;
    }

    if (cchLength < 0)
    {
        return GenericCompare(String(),other.String());
    }
    return GenericCompareLength(String(),other.String(),cchLength);
}

//
// Two main typedefs
//
typedef CBasicStringBase<char>     CBasicStringAnsi;
typedef CBasicStringBase<wchar_t>  CBasicStringWide;

//
// LPCTSTR equivalents
//
#if defined(UNICODE) || defined(_UNICODE)
typedef CBasicStringWide CBasicString;
#else
typedef CBasicStringAnsi CBasicString;
#endif

//
// Operators
//
inline bool operator<( const CBasicStringAnsi &a, const CBasicStringAnsi &b )
{
    return a.Compare(b) < 0;
}

inline bool operator<( const CBasicStringWide &a, const CBasicStringWide &b )
{
    return a.Compare(b) < 0;
}

inline bool operator<=( const CBasicStringAnsi &a, const CBasicStringAnsi &b )
{
    return a.Compare(b) <= 0;
}

inline bool operator<=( const CBasicStringWide &a, const CBasicStringWide &b )
{
    return a.Compare(b) <= 0;
}

inline bool operator==( const CBasicStringAnsi &a, const CBasicStringAnsi &b )
{
    return a.Compare(b) == 0;
}

inline bool operator==( const CBasicStringWide &a, const CBasicStringWide &b )
{
    return a.Compare(b) == 0;
}

inline bool operator!=( const CBasicStringAnsi &a, const CBasicStringAnsi &b )
{
    return a.Compare(b) != 0;
}

inline bool operator!=( const CBasicStringWide &a, const CBasicStringWide &b )
{
    return a.Compare(b) != 0;
}

inline bool operator>=( const CBasicStringAnsi &a, const CBasicStringAnsi &b )
{
    return a.Compare(b) >= 0;
}

inline bool operator>=( const CBasicStringWide &a, const CBasicStringWide &b )
{
    return a.Compare(b) >= 0;
}

inline bool operator>( const CBasicStringAnsi &a, const CBasicStringAnsi &b )
{
    return a.Compare(b) > 0;
}

inline bool operator>( const CBasicStringWide &a, const CBasicStringWide &b )
{
    return a.Compare(b) > 0;
}

inline CBasicStringWide operator+( const CBasicStringWide &a, const CBasicStringWide &b )
{
    CBasicStringWide strResult(a);
    strResult.Concat(b);
    return strResult;
}

inline CBasicStringAnsi operator+( const CBasicStringAnsi &a, const CBasicStringAnsi &b )
{
    CBasicStringAnsi strResult(a);
    strResult.Concat(b);
    return strResult;
}

//
// Restore the warning state
//
#pragma warning( pop )

#endif  // ifndef _SIMSTR_H_INCLUDED


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -