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 @@ ...@@ -3,6 +3,9 @@
* search.c (Freplace_match): Use make_specified_string. * search.c (Freplace_match): Use make_specified_string.
* xterm.c, w32term.c (x_set_glyph_string_gc): Use emacs_abort * xterm.c, w32term.c (x_set_glyph_string_gc): Use emacs_abort
to catch bogus override face of glyph strings. 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> 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. */) ...@@ -1127,7 +1127,39 @@ Elements of ALIST that are not conses are also shared. */)
return alist; 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. doc: /* Return a new string whose contents are a substring of STRING.
The returned string consists of the characters between index FROM The returned string consists of the characters between index FROM
\(inclusive) and index TO (exclusive) of STRING. FROM and TO are \(inclusive) and index TO (exclusive) of STRING. FROM and TO are
...@@ -1137,36 +1169,23 @@ to the end of STRING. ...@@ -1137,36 +1169,23 @@ to the end of STRING.
The STRING argument may also be a vector. In that case, the return 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 value is a new vector that contains the elements between index FROM
\(inclusive) and index TO (exclusive) of that vector argument. */) \(inclusive) and index TO (exclusive) of that vector argument.
(Lisp_Object string, register Lisp_Object from, Lisp_Object to)
With one argument, just copy STRING (with properties, if any). */)
(Lisp_Object string, Lisp_Object from, Lisp_Object to)
{ {
Lisp_Object res; Lisp_Object res;
ptrdiff_t size; ptrdiff_t size;
EMACS_INT from_char, to_char; EMACS_INT from_char, to_char;
CHECK_VECTOR_OR_STRING (string);
CHECK_NUMBER (from);
if (STRINGP (string)) if (STRINGP (string))
size = SCHARS (string); size = SCHARS (string);
else else if (VECTORP (string))
size = ASIZE (string); size = ASIZE (string);
if (NILP (to))
to_char = size;
else else
{ wrong_type_argument (Qarrayp, string);
CHECK_NUMBER (to);
if (!validate_substring (from, to, size, &from_char, &to_char))
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))
args_out_of_range_3 (string, make_number (from_char), args_out_of_range_3 (string, make_number (from_char),
make_number (to_char)); make_number (to_char));
...@@ -1206,27 +1225,7 @@ With one argument, just copy STRING without its properties. */) ...@@ -1206,27 +1225,7 @@ With one argument, just copy STRING without its properties. */)
size = SCHARS (string); size = SCHARS (string);
if (NILP (from)) if (!validate_substring (from, to, size, &from_char, &to_char))
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))
args_out_of_range_3 (string, make_number (from_char), args_out_of_range_3 (string, make_number (from_char),
make_number (to_char)); make_number (to_char));
...@@ -4614,29 +4613,7 @@ secure_hash (Lisp_Object algorithm, Lisp_Object object, Lisp_Object start, Lisp_ ...@@ -4614,29 +4613,7 @@ secure_hash (Lisp_Object algorithm, Lisp_Object object, Lisp_Object start, Lisp_
size = SCHARS (object); size = SCHARS (object);
if (!NILP (start)) if (!validate_substring (start, end, size, &start_char, &end_char))
{
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))
args_out_of_range_3 (object, make_number (start_char), args_out_of_range_3 (object, make_number (start_char),
make_number (end_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