Commit fbc87aea authored by Dmitry Antipov's avatar Dmitry Antipov
Browse files

* fns.c (Fsubstring, Fsubstring_no_properties, secure_hash):

Move common substring range checking code to...
(validate_substring): ...this function.
parent 68712e08
......@@ -3,6 +3,9 @@
* search.c (Freplace_match): Use make_specified_string.
* xterm.c, w32term.c (x_set_glyph_string_gc): Use emacs_abort
to catch bogus override face of glyph strings.
* fns.c (Fsubstring, Fsubstring_no_properties, secure_hash):
Move common substring range checking code to...
(validate_substring): ...this function.
2014-03-31 Jan Djärv <jan.h.d@swipnet.se>
......
......@@ -1127,7 +1127,39 @@ Elements of ALIST that are not conses are also shared. */)
return alist;
}
DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0,
/* True if [FROM..TO) specifies a valid substring of SIZE-characters string.
If FROM is nil, 0 assumed. If TO is nil, SIZE assumed. Negative
values are counted from the end. *FROM_CHAR and *TO_CHAR are updated
with corresponding C values of TO and FROM. */
static bool
validate_substring (Lisp_Object from, Lisp_Object to, ptrdiff_t size,
EMACS_INT *from_char, EMACS_INT *to_char)
{
if (NILP (from))
*from_char = 0;
else
{
CHECK_NUMBER (from);
*from_char = XINT (from);
if (*from_char < 0)
*from_char += size;
}
if (NILP (to))
*to_char = size;
else
{
CHECK_NUMBER (to);
*to_char = XINT (to);
if (*to_char < 0)
*to_char += size;
}
return (0 <= *from_char && *from_char <= *to_char && *to_char <= size);
}
DEFUN ("substring", Fsubstring, Ssubstring, 1, 3, 0,
doc: /* Return a new string whose contents are a substring of STRING.
The returned string consists of the characters between index FROM
\(inclusive) and index TO (exclusive) of STRING. FROM and TO are
......@@ -1137,36 +1169,23 @@ to the end of STRING.
The STRING argument may also be a vector. In that case, the return
value is a new vector that contains the elements between index FROM
\(inclusive) and index TO (exclusive) of that vector argument. */)
(Lisp_Object string, register Lisp_Object from, Lisp_Object to)
\(inclusive) and index TO (exclusive) of that vector argument.
With one argument, just copy STRING (with properties, if any). */)
(Lisp_Object string, Lisp_Object from, Lisp_Object to)
{
Lisp_Object res;
ptrdiff_t size;
EMACS_INT from_char, to_char;
CHECK_VECTOR_OR_STRING (string);
CHECK_NUMBER (from);
if (STRINGP (string))
size = SCHARS (string);
else
else if (VECTORP (string))
size = ASIZE (string);
if (NILP (to))
to_char = size;
else
{
CHECK_NUMBER (to);
wrong_type_argument (Qarrayp, string);
to_char = XINT (to);
if (to_char < 0)
to_char += size;
}
from_char = XINT (from);
if (from_char < 0)
from_char += size;
if (!(0 <= from_char && from_char <= to_char && to_char <= size))
if (!validate_substring (from, to, size, &from_char, &to_char))
args_out_of_range_3 (string, make_number (from_char),
make_number (to_char));
......@@ -1206,27 +1225,7 @@ With one argument, just copy STRING without its properties. */)
size = SCHARS (string);
if (NILP (from))
from_char = 0;
else
{
CHECK_NUMBER (from);
from_char = XINT (from);
if (from_char < 0)
from_char += size;
}
if (NILP (to))
to_char = size;
else
{
CHECK_NUMBER (to);
to_char = XINT (to);
if (to_char < 0)
to_char += size;
}
if (!(0 <= from_char && from_char <= to_char && to_char <= size))
if (!validate_substring (from, to, size, &from_char, &to_char))
args_out_of_range_3 (string, make_number (from_char),
make_number (to_char));
......@@ -4614,29 +4613,7 @@ secure_hash (Lisp_Object algorithm, Lisp_Object object, Lisp_Object start, Lisp_
size = SCHARS (object);
if (!NILP (start))
{
CHECK_NUMBER (start);
start_char = XINT (start);
if (start_char < 0)
start_char += size;
}
if (NILP (end))
end_char = size;
else
{
CHECK_NUMBER (end);
end_char = XINT (end);
if (end_char < 0)
end_char += size;
}
if (!(0 <= start_char && start_char <= end_char && end_char <= size))
if (!validate_substring (start, end, size, &start_char, &end_char))
args_out_of_range_3 (object, make_number (start_char),
make_number (end_char));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment