Branch: Tag:

2016-10-17

2016-10-17 10:51:25 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Stdio.Buffer [search]: Support searching for substrings.

1858:    push_undefined();    }    +  /*! @decl protected int(-1..) _search(string(8bit) substring, int|void start, @ +  *! int|void end) +  *! +  *! Search forward from the indicated @[start] position for the specified +  *! @[substring]. +  *! +  *! @param substring +  *! Substring to search for. +  *! +  *! @param start +  *! Start position relative to the current read position of the buffer. +  *! +  *! Negative @[start] values are supported and indicate positions +  *! prior to the current read position. +  *! +  *! @param end +  *! Don't search past this position of the buffer. +  *! +  *! @returns +  *! Returns the first found position of @[substring] relative to the +  *! current read position of the buffer on success, and @[UNDEFINED] +  *! on not found. The read position is not advanced. +  *! +  *! @seealso +  *! @[read_cstring()], @[search()], @[lfun::_search()] +  */ +  PIKEFUN int _search(string(8bit) substring, int|void start, int|void end) +  flags ID_PROTECTED; +  { +  Buffer *io = THIS; +  unsigned char *buf = io_read_pointer(io); +  unsigned char *buf_end = buf + io_len(io); +  SearchMojt mojt; +  +  if (end) { +  INT_TYPE bytes = end->u.integer; +  +  if (bytes < 0) { +  push_undefined(); +  return; +  } +  if (((size_t)bytes) < io_len(io)) { +  buf_end = buf + bytes + 1; +  } +  } +  +  if (start) { +  INT_TYPE bytes = start->u.integer; +  +  if (bytes >= 0) { +  if (((size_t)bytes) >= io_len(io)) { +  push_undefined(); +  return; +  } +  } else if (((size_t)(-bytes)) > io->offset) { +  bytes = -(ptrdiff_t)io->offset; +  } +  buf += bytes; +  } +  +  if (substring->len > (buf_end - buf)) { +  push_undefined(); +  return; +  } +  if (!substring->len) { +  push_int64(buf - io_read_pointer(io)); +  return; +  } +  +  mojt = compile_memsearcher(MKPCHARP_STR(substring), substring->len, +  buf_end - buf, substring); +  +  buf = mojt.vtab->func0(mojt.data, buf, buf_end - buf); +  +  if (mojt.container) free_object(mojt.container); +  +  if (!buf) { +  push_undefined(); +  } else { +  push_int64(buf - io_read_pointer(io)); +  } +  } +     /*! @decl Buffer read_hbuffer( int n )    *! @decl Buffer read_hbuffer( int n, bool copy )    *!