pike.git / src / modules / _Stdio / buffer.cmod

version» Context lines:

pike.git/src/modules/_Stdio/buffer.cmod:1776:       /*! @decl string(8bit) read_cstring( )    *!    *! Reads a \0 terminated C-string and returns the    *! string excluding the terminating \0.    *!    *! If there is not enough data available return UNDEFINED.    *!    *! Note that pike string can not be longer than 0x7fffffff bytes (~2Gb).    */ -  PIKEFUN string(0..255) read_cstring( ) +  PIKEFUN string(0..255) read_cstring()    { -  LONGEST len; +     Buffer *io = THIS; -  struct pike_string *s; -  ONERROR e; +     -  io_rewind_on_error( io, &e ); -  len = 0; -  do { -  /* search the amount of data we know we have for each call to io_avail */ -  while( io_len(io) ) +  do +  if ( LIKELY(io_len(THIS)) )    { -  if( !io_read_byte_uc(io) ) -  goto found_null; -  len++; +  const char * start = io_read_pointer(io); +  const char * end = memchr(start, 0, io_len(io)); +  if ( LIKELY(end) ) +  { +  push_string(io_read_string(io, end - start)); +  io_read_byte_uc(io); /* consume the terminating \0 byte */ +  return;    }    } -  while( io_avail( io, 1 ) ); -  goto fail; +  while ( UNLIKELY(io_range_error(THIS, 0)) );    -  found_null: -  io_rewind( io, len+1 ); -  s = io_read_string( io, len ); -  -  if( LIKELY(s) ) { -  io_read_byte_uc(io); /* consume the terminating \0 byte */ -  io_unset_rewind_on_error( io, &e ); -  push_string(s); -  } else { - fail: CALL_AND_UNSET_ONERROR(e); +     push_undefined();    } -  } +        /*! @decl Buffer read_hbuffer( int n )    *! @decl Buffer read_hbuffer( int n, bool copy )    *!    *! Same as @[read_hstring], but returns the result as an Buffer.    *!    *! No data is copied unless @[copy] is specified and true, the new    *! buffer points into the old one.    *!    *! @note