📄 hxstring.cpp
字号:
return m_pRep->GetBuffer();}void CHXString::ReleaseBuffer(INT32 newSize){ // NOTE: newSize is string length, not including ending zero byte... if (m_pRep) { // Update the string size since the caller could've changed the // internal data (the whole point of this GetBuffer()/ReleaseBuffer() // stuff). char* pBuf = m_pRep->GetBuffer(); if (newSize >= m_pRep->GetBufferSize()) { HX_ASSERT(newSize < m_pRep->GetBufferSize()); // ...so if it's too big, clamp it to the max available. newSize = m_pRep->GetBufferSize() - 1; } if (newSize >= 0) pBuf[newSize] = '\0'; else newSize = strlen(pBuf); if (newSize > 0) { m_pRep->SetStringSize(newSize); m_pRep->ResizeAndCopy(newSize); } else { if (m_pRep) { m_pRep->Release(); m_pRep = NULL; } } } else { HX_ASSERT(!"Shouldn't call ReleaseBuffer() without GetBuffer()"); if (newSize > 0) m_pRep = new CHXStringRep(newSize); }}char* CHXString::GetBufferSetLength(INT32 newSize){ // NOTE : newSize is a string length, not including ending zero byte... HX_ASSERT(newSize >= 0); if (m_pRep) { EnsureUnique(); m_pRep->ResizeAndCopy(newSize, true); } else if (newSize > 0) m_pRep = new CHXStringRep(newSize, true); return m_pRep->GetBuffer();}void CHXString::FreeExtra(){ if (m_pRep) { INT32 newSize = GetLength(); if (newSize > 0) { EnsureUnique(); m_pRep->ResizeAndCopy(newSize); } else { if (m_pRep) { m_pRep->Release(); m_pRep = NULL; } } }}INT32 CHXString::GetAllocLength() const{ return GetLength();}INT32 CHXString::SetMinBufSize(INT32 minSize){ // NOTE: minSize is a string length, not including ending zero byte... HX_ASSERT(minSize >= 0); INT32 ret = 0; if (m_pRep) { if (minSize >= m_pRep->GetStringSize()) { if (minSize) { EnsureUnique(); m_pRep->ResizeAndCopy(minSize); } else { if (m_pRep) { m_pRep->Release(); m_pRep = NULL; } } } if (m_pRep) ret = m_pRep->GetBufferSize() - 1; } else if (minSize > 0) { m_pRep = new CHXStringRep(minSize); ret = minSize; } return ret;}#if defined(_MACINTOSH) || defined(_MAC_UNIX)#include "platform/mac/fullpathname.h"#include "platform/mac/cfwrappers.h"const CHXString& CHXString::SetFromStr255(const Str255 src){ char temp[256]; /* Flawfinder: ignore */ if (src==NULL) { Empty(); } else { memcpy (temp, &src[1], src[0]); /* Flawfinder: ignore */ temp[src[0]] = 0; *this = CHXString( temp, src[0]); } return *this;}const CHXString& CHXString::AppendFromStr255(const Str255 src){ char temp[256]; /* Flawfinder: ignore */ if (src!=NULL) { memcpy (temp, &src[1], src[0]); /* Flawfinder: ignore */ temp[src[0]] = 0; CHXString tempS; tempS = temp; *this = *this + tempS; } return *this;}const CHXString& CHXString::InsertFromStr255(const Str255 src){ char temp[256]; /* Flawfinder: ignore */ if (src!=NULL) { memcpy (temp, &src[1], src[0]); /* Flawfinder: ignore */ temp[src[0]] = 0; CHXString tempS; tempS = temp; *this = tempS + *this; } return *this;} const CHXString& CHXString::SetFromIndString(short strlist, short item){ Str255 theStr; GetIndString(theStr,strlist,item); SetFromStr255(theStr); return *this;}const CHXString& CHXString::operator =(FSSpec spec){ CHXString temp; PathNameFromFSSpec(&spec, temp); *this=temp; return *this;}CHXString::operator const FSSpec(void){ FSSpec spec; (void) FSSpecFromPathName((const char*)(*this), &spec); return spec;}HX_RESULT CHXString::MakeStr255(Str255& outPascalString) const{ UINT32 len = GetLength(); if (m_pRep) c2pstrcpy(outPascalString, m_pRep->GetBuffer()); return (len <= 255 ? HXR_OK : HXR_FAIL);}#if !defined(_CARBON) && !defined(_MAC_UNIX)CHXString::operator Str255* (void){#error "Not Implemented"}CHXString::operator const Str255* (void) const{#error "Not Implemented"}CHXString::operator ConstStr255Param (void) const{#error "Not Implemented"}#elseconst CHXString& CHXString::operator =(const FSRef& ref){ CHXString temp; (void) PathFromFSRef(&ref, temp); *this = temp; return *this;}CHXString::operator const FSRef(void){ FSRef ref; ZeroInit(&ref); (void) FSRefFromPath((const char*)(*this), &ref); return ref;}const CHXString& CHXString::operator =(CFStringRef ref){#ifdef _MAC_CFM CFStringEncoding encoding = CFStringGetSystemEncoding();#else CFStringEncoding encoding = kCFStringEncodingUTF8;#endif // we need the string to be canonically decomposed Unicode in case it'll be used as a path on // an HFS disk, so we'll make a mutable copy of the string, normalize it, and then encode that as UTF-8 const CFIndex kNoMaxLength = 0; CFMutableStringRef mutableRef = CFStringCreateMutableCopy(kCFAllocatorDefault, kNoMaxLength, ref);#ifndef __MWERKS__ // our version of CodeWarrior doesn't have CFStringNormalize in the headers since they are pre-10.2 headers, alas CFStringNormalize(mutableRef, kCFStringNormalizationFormD);#endif (void) SetFromCFString(mutableRef, encoding); CFRelease(mutableRef); return *this;}HX_RESULT CHXString::SetFromCFString(CFStringRef ref, CFStringEncoding encoding){ CHXString strTemp; char * pBuffer; BOOL bSuccess; CFIndex buffSize; bSuccess = FALSE; //const CFStringEncoding kEncoding = kCFStringEncodingUTF8; buffSize = 1 + CFStringGetMaximumSizeForEncoding(CFStringGetLength(ref), encoding); pBuffer = strTemp.GetBuffer(buffSize); if (pBuffer) { bSuccess = CFStringGetCString(ref, pBuffer, buffSize, encoding); strTemp.ReleaseBuffer(); } *this = strTemp; return bSuccess ? HXR_OK : HXR_FAIL; }HX_RESULT CHXString::SetFromHFSUniStr255(const HFSUniStr255& uniName, CFStringEncoding encoding){ CHXCFString cfs(uniName); if (cfs.IsSet()) { SetFromCFString(cfs, encoding); return HXR_OK; } return HXR_FAIL;}HX_RESULT CHXString::MakeHFSUniStr255(HFSUniStr255& outUniName, CFStringEncoding encoding) const{ if (GetLength() < 256) { CHXCFString cfs((const char*)(*this), encoding); if (cfs.IsSet()) { outUniName = (HFSUniStr255) cfs; return HXR_OK; } } return HXR_FAIL;}#endif /* _CARBON || _MAC_UNIX */#endif /* _MACINTOSH || _MAC_UNIX */void CHXString::Init(const char* pStr, UINT32 size){ if (size == UINT_MAX) size = (UINT32)SafeStrlen(pStr); if (m_pRep) { if ((UINT32)m_pRep->GetBufferSize() < (size + 1)) m_pRep->Resize(size); strncpy(m_pRep->GetBuffer(), pStr, size); /* Flawfinder: ignore */ m_pRep->GetBuffer()[size] = '\0'; m_pRep->SetStringSize(SafeStrlen(m_pRep->GetBuffer())); } else m_pRep = new CHXStringRep(pStr, size);}void CHXString::Nuke(){ if (m_pRep) { m_pRep->Release(); m_pRep = 0; }}void CHXString::ConcatInPlace(const char* pStr, const UINT32 size){ Append(pStr, (INT32)size);}void CHXString::EnsureUnique(){ if (m_pRep && m_pRep->IsShared()) { // m_pRep is being shared. Time to copy it so that we // have our own copy. CHXStringRep* pOld = m_pRep; m_pRep = new CHXStringRep(pOld->GetBuffer(), pOld->GetStringSize()); pOld->Release(); }}void CHXString::Release(){}INT32 CHXString::MinimalGrowth(INT32 currentSize, INT32 sizeNeeded){ return sizeNeeded;}INT32 CHXString::DoublingGrowth(INT32 currentSize, INT32 sizeNeeded){ INT32 ret = currentSize; while (ret < sizeNeeded) ret *= 2; return ret;}void CHXString::Append(const char* pStr, INT32 size){ HX_ASSERT(size >= 0); if (size) { if (m_pRep) { EnsureUnique(); int newSize = m_pRep->GetStringSize() + size; Grow(newSize + 1); strncpy(m_pRep->GetBuffer() + m_pRep->GetStringSize(), pStr, size); /* Flawfinder: ignore */ m_pRep->GetBuffer()[newSize] = '\0'; m_pRep->SetStringSize(newSize); } else m_pRep = new CHXStringRep(pStr, size); }}void CHXString::Grow(INT32 newSize){ HX_ASSERT(m_pRep); HX_ASSERT(newSize >= 0); if (newSize > m_pRep->GetBufferSize()) { INT32 growSize = m_pGrowthFunc(m_pRep->GetBufferSize(), newSize); // Protect ourselves from bad grow functions if (growSize < newSize) growSize = newSize; m_pRep->ResizeAndCopy(growSize - 1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -