Branch: Tag:

2015-10-25

2015-10-25 17:12:19 by Martin Nilsson <nilsson@fastmail.com>

We can't store UNDEFINED in arrays. Clear up some confusion.

4:   //! @returns   //! @array   //! @elem string|int|array|mapping data - //! The decoded data. UNDEFINED if no data could be decoded. + //! The decoded data. @expr{0@} if no data could be decoded.   //! @elem string remainder   //! The trailing data that wasn't decoded.   //! @endarray   array(string|int|array|mapping) _decode(string what)   { -  if (what=="") return ({UNDEFINED,what}); +  if (what=="") return ({0,what});    if (what==0) error("Cannot decode 0 (zero)\n");       int i;
20:    {    case 'i': // integer    if (sscanf(what,"i%de%s",i,what)<2) -  return ({UNDEFINED,what}); +  return ({0,what});    return ({i,what});       case '0'..'9': // string    if (sscanf(what,"%d:%s",i,s)<2 ||    strlen(s)<i) -  return ({UNDEFINED,what}); +  return ({0,what});    return ({s[..i-1],s[i..]});       case 'l': // list
42:    res+=v[..0];    s=v[1];    } -  return ({UNDEFINED,what}); +  return ({0,what});       case 'd': // dictionary    array keys=({});
62:    values+=v[..0];    s=v[1];    } -  return ({UNDEFINED,what}); +  return ({0,what});       default:    error("Error in Bencoding: unknown prefix %O...\n",
71:   }      //! Decodes a Bittorrent bencoded data chunk and ignores the remaining - //! string. Returns UNDEFINED if the data is incomplete. + //! string. Returns @expr{UNDEFINED@} if the data is incomplete.   string|int|array|mapping decode(string what)   {    array v=_decode(what);