api:buffer Refactor range checks in `buffer_set_slice`

* Get rid of the old range checks for variables and move the new checks to the
  beginning of the function.