Branch: Tag:

2016-08-28

2016-08-28 13:35:32 by Martin Nilsson <nilsson@fastmail.com>

Allow sprintf %x on a string.

884:   }       - /*! @decl string string2hex(string data) + /*! @decl string string2hex(string data, void|int uppercase)    *! @appears String.string2hex    *!    *! Convert a string of binary data to a hexadecimal string.    *! -  +  *! @param uppercase +  *! If set, the string will use upper cased characters. +  *!    *! @seealso    *! @[hex2string()]    */
898:    'a','b','c','d','e','f'   };    + static const char HEXCHAR[] = { +  '0','1','2','3','4','5','6','7','8','9', +  'A','B','C','D','E','F' + }; +    static const unsigned char hexdecode[256] =   {    16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
930:   };      PMOD_EXPORT -  PIKEFUN string(0..255) string2hex(string s) +  PIKEFUN string(48..102) string2hex(string s, void|int uppercase) +  efun;    optflags OPT_TRY_OPTIMIZE;   {    struct pike_string *hex;
944:    p = (unsigned char *)hex->str;    l = s->len;    +  if( uppercase && uppercase->u.integer > 0) +  {    for (i=0; i<l; i++) { -  +  *p++ = HEXCHAR[*st>>4]; +  *p++ = HEXCHAR[*st&15]; +  st++; +  } +  } +  else +  { +  for (i=0; i<l; i++) {    *p++ = hexchar[*st>>4];    *p++ = hexchar[*st&15];    st++;    } -  +  }       RETURN end_shared_string(hex);   }