Truncate the string
| Return Type | Function name | Arguments |
|---|---|---|
| hzString& | hzString::Truncate | (uint32_t,) |
Declared in file: hzString.h
Defined in file : hzString.cpp
Function Logic:
Function body:
hzString& hzString::Truncate (uint32_t limit)
{
// Truncate the string
//
// Arguments: 1) limit Sets max length for the string and truncates beyond this.
// Returns: Reference to this string in all cases
_hzfunc("hzString::Truncate") ;
_strItem* thisCtl ; // This string's control area
_strItem* destCtl ; // New control area
uint32_t destAddr = 0; // New string address if required
// If NULL return
if (!m_addr)
return *this ;
thisCtl = _strXlate(m_addr) ;
// Do nothing if the size limit exceeds length of string value
if (limit >&eq; thisCtl->_getSize())
return *this ;
// Full truncation, delete
if (limit)
{
// Perform the (partial) truncation
destAddr = _strAlloc(limit) ;
destCtl = _strXlate(destAddr) ;
destCtl->_setSize(limit) ;
memcpy(destCtl->_data(), thisCtl->_data(), limit) ;
destCtl->m_copy = 1;
}
// 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 ;
}