Performs URL-Encoding on the current string content. This is transformation is only carried out if URL characters exist in the string value
| Return Type | Function name | Arguments |
|---|---|---|
| hzString& | hzString::UrlEncode | (bool,) |
Declared in file: hzString.h
Defined in file : hzString.cpp
Function Logic:
Function body:
hzString& hzString::UrlEncode (bool bResv)
{
// Performs URL-Encoding on the current string content.
//
// This is transformation is only carried out if URL characters exist in the string value
//
// Arguments: 1) bResv With bResv false (default), only the standard URL characters are encoded. But with bResv true, an extended set of URL characters
// are converted. Note that these include the forward slash character.
//
// Returns: Reference to this string instance
_hzfunc("hzString::UrlEncode") ;
_strItem* thisCtl ; // This string's control area
_strItem* destCtl ; // New internal structure if required
char* i ; // For iteration
char* j ; // For iteration
uint32_t nLen ; // Lenght of original string
uint32_t destAddr ; // New string address if required
char buf [4]; // For Hex conversion
// If NULL return
if (!m_addr)
return *this ;
thisCtl = _strXlate(m_addr) ;
// Is change needed? Count chars that are to be converted as these will occupy 3 chars in the new string
nLen = thisCtl->_getSize() ;
// nLen = Length() ;
for (i = (char*) thisCtl->_data() ; *i ; i++)
{
// If the char is a % (string may already be encoded), pass this by
if (*i == CHAR_PERCENT)
continue ;
// If the char is a normal URL char, pass
if (IsUrlnorm(*i))
continue ;
// If the char is a reserved URL char, pass olny if bResv is false
if (IsUrlresv(*i))
{
if (bResv)
nLen += 2;
continue ;
}
// Only increas the expected length if there is any char that must be encoded
nLen += 2;
}
if (nLen == thisCtl->_getSize())
return *this ;
// Allocate new space
destAddr = _strAlloc(nLen) ;
destCtl = _strXlate(destAddr) ;
destCtl->_setSize(nLen) ;
destCtl->m_copy = 1;
j = (char*) destCtl->_data() ;
for (i = (char*) thisCtl->_data() ; *i ; i++)
{
if (*i == CHAR_PERCENT)
*j++ = *i ;
else if (IsUrlnorm(*i))
*j++ = *i ;
else if (IsUrlresv(*i) && !bResv)
*j++ = *i ;
else
{
*j++ = CHAR_PERCENT ;
sprintf(buf, "%02x", (uint32_t) *i) ;
*j++ = buf[0];
*j++ = buf[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 ;
}