Conditional, content based truncation. If the supplied patern exists in the string, the new string will be everything beyond but not including the first instance of the patern. If the pattern is empty or does not exist in the string, the string content will be unchanged.
| Return Type | Function name | Arguments |
|---|---|---|
| hzString& | hzString::TruncateBeyond | (const char*,) |
Declared in file: hzString.h
Defined in file : hzString.cpp
Function Logic:
Function body:
hzString& hzString::TruncateBeyond (const char* patern)
{
// Category: Text Processing
//
// Conditional, content based truncation. If the supplied patern exists in the string, the new string will be everything beyond but not including the first
// instance of the patern. If the pattern is empty or does not exist in the string, the string content will be unchanged.
//
// Arguments: 1) pattern The pattern at which the string will be truncated.
// Returns: Reference to this string in all cases
_hzfunc("hzString::TruncateBeyond") ;
_strItem* thisCtl ; // This string's control area
_strItem* destCtl ; // New control area
char* i ; // Pointer into old string data
char* j ; // Pointer into new string data
uint32_t destAddr ; // New string address if required
uint32_t newLen ; // New string length
// If no pattern supplied, do nothing
if (!patern || !patern[0])
return *this ;
// If NULL return
if (!m_addr)
return *this ;
// Test if change required
thisCtl = _strXlate(m_addr) ;
i = strstr((char*) thisCtl->_data(), patern) ;
if (!i)
return *this ;
// Allocate new space
newLen = i - thisCtl->_data() ;
destAddr = _strAlloc(newLen) ;
destCtl = _strXlate(destAddr) ;
destCtl->_setSize(newLen) ;
destCtl->m_copy = 1;
i += strlen(patern) ;
for (j = destCtl->_data() ; *i ; *j++ = *i++) ;
*j = 0;
// Tidy up
if (thisCtl->m_copy && thisCtl->m_copy < 50)
{
if (!_hzGlobal_MT)
{
thisCtl->m_copy-- ;
if (!thisCtl->m_copy)
_strFree(m_addr, thisCtl->_getSize()) ;
}
else
{
if (__sync_add_and_fetch(&(thisCtl->m_copy), -1)== 0)
_strFree(m_addr, thisCtl->_getSize()) ;
}
}
m_addr = destAddr ;
return *this ;
}