Performs URL-Encoding on the current string content. This is transformation is only carried out if URL characters exist in the string value

Return TypeFunction nameArguments
hzString&hzString::UrlEncode(bool,)

Declared in file: hzString.h
Defined in file : hzString.cpp

Function Logic:

0:START 1:unknown 2:Return *this 3:thisCtl nLen 4:unknown 5:unknown 6:unknown 7:unknown 8:unknown 9:nLen 10:nLen 11:unknown 12:Return *this 13:destAddr destCtl items destCtl j 14:unknown 15:unknown 16:* 17:IsUrlnorm(*i) 18:* 19:IsUrlresv(*i)&&!bResv 20:* 21:* items * * 22:unknown 23:unknown 24:items 25:unknown 26:items 27:unknown 28:items 29:m_addr 30:Return *this

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 ;
}