diff options
| author | Dmitry Antipov | 2014-03-31 16:06:34 +0400 |
|---|---|---|
| committer | Dmitry Antipov | 2014-03-31 16:06:34 +0400 |
| commit | fbc87aeaafe7ccdcfd2e4d1c0417c747587b8ab9 (patch) | |
| tree | 2926becec9440de3ace06fc8811ab09e8b69201b | |
| parent | 68712e08458dabb4c2b48b6f6879466e476bd5ce (diff) | |
| download | emacs-fbc87aeaafe7ccdcfd2e4d1c0417c747587b8ab9.tar.gz emacs-fbc87aeaafe7ccdcfd2e4d1c0417c747587b8ab9.zip | |
* fns.c (Fsubstring, Fsubstring_no_properties, secure_hash):
Move common substring range checking code to...
(validate_substring): ...this function.
| -rw-r--r-- | src/ChangeLog | 3 | ||||
| -rw-r--r-- | src/fns.c | 109 |
2 files changed, 46 insertions, 66 deletions
diff --git a/src/ChangeLog b/src/ChangeLog index ea34def5ded..773a09121fd 100644 --- a/src/ChangeLog +++ b/src/ChangeLog | |||
| @@ -3,6 +3,9 @@ | |||
| 3 | * search.c (Freplace_match): Use make_specified_string. | 3 | * search.c (Freplace_match): Use make_specified_string. |
| 4 | * xterm.c, w32term.c (x_set_glyph_string_gc): Use emacs_abort | 4 | * xterm.c, w32term.c (x_set_glyph_string_gc): Use emacs_abort |
| 5 | to catch bogus override face of glyph strings. | 5 | to catch bogus override face of glyph strings. |
| 6 | * fns.c (Fsubstring, Fsubstring_no_properties, secure_hash): | ||
| 7 | Move common substring range checking code to... | ||
| 8 | (validate_substring): ...this function. | ||
| 6 | 9 | ||
| 7 | 2014-03-31 Jan Djärv <jan.h.d@swipnet.se> | 10 | 2014-03-31 Jan Djärv <jan.h.d@swipnet.se> |
| 8 | 11 | ||
| @@ -1127,7 +1127,39 @@ Elements of ALIST that are not conses are also shared. */) | |||
| 1127 | return alist; | 1127 | return alist; |
| 1128 | } | 1128 | } |
| 1129 | 1129 | ||
| 1130 | DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0, | 1130 | /* True if [FROM..TO) specifies a valid substring of SIZE-characters string. |
| 1131 | If FROM is nil, 0 assumed. If TO is nil, SIZE assumed. Negative | ||
| 1132 | values are counted from the end. *FROM_CHAR and *TO_CHAR are updated | ||
| 1133 | with corresponding C values of TO and FROM. */ | ||
| 1134 | |||
| 1135 | static bool | ||
| 1136 | validate_substring (Lisp_Object from, Lisp_Object to, ptrdiff_t size, | ||
| 1137 | EMACS_INT *from_char, EMACS_INT *to_char) | ||
| 1138 | { | ||
| 1139 | if (NILP (from)) | ||
| 1140 | *from_char = 0; | ||
| 1141 | else | ||
| 1142 | { | ||
| 1143 | CHECK_NUMBER (from); | ||
| 1144 | *from_char = XINT (from); | ||
| 1145 | if (*from_char < 0) | ||
| 1146 | *from_char += size; | ||
| 1147 | } | ||
| 1148 | |||
| 1149 | if (NILP (to)) | ||
| 1150 | *to_char = size; | ||
| 1151 | else | ||
| 1152 | { | ||
| 1153 | CHECK_NUMBER (to); | ||
| 1154 | *to_char = XINT (to); | ||
| 1155 | if (*to_char < 0) | ||
| 1156 | *to_char += size; | ||
| 1157 | } | ||
| 1158 | |||
| 1159 | return (0 <= *from_char && *from_char <= *to_char && *to_char <= size); | ||
| 1160 | } | ||
| 1161 | |||
| 1162 | DEFUN ("substring", Fsubstring, Ssubstring, 1, 3, 0, | ||
| 1131 | doc: /* Return a new string whose contents are a substring of STRING. | 1163 | doc: /* Return a new string whose contents are a substring of STRING. |
| 1132 | The returned string consists of the characters between index FROM | 1164 | The returned string consists of the characters between index FROM |
| 1133 | \(inclusive) and index TO (exclusive) of STRING. FROM and TO are | 1165 | \(inclusive) and index TO (exclusive) of STRING. FROM and TO are |
| @@ -1137,36 +1169,23 @@ to the end of STRING. | |||
| 1137 | 1169 | ||
| 1138 | The STRING argument may also be a vector. In that case, the return | 1170 | The STRING argument may also be a vector. In that case, the return |
| 1139 | value is a new vector that contains the elements between index FROM | 1171 | value is a new vector that contains the elements between index FROM |
| 1140 | \(inclusive) and index TO (exclusive) of that vector argument. */) | 1172 | \(inclusive) and index TO (exclusive) of that vector argument. |
| 1141 | (Lisp_Object string, register Lisp_Object from, Lisp_Object to) | 1173 | |
| 1174 | With one argument, just copy STRING (with properties, if any). */) | ||
| 1175 | (Lisp_Object string, Lisp_Object from, Lisp_Object to) | ||
| 1142 | { | 1176 | { |
| 1143 | Lisp_Object res; | 1177 | Lisp_Object res; |
| 1144 | ptrdiff_t size; | 1178 | ptrdiff_t size; |
| 1145 | EMACS_INT from_char, to_char; | 1179 | EMACS_INT from_char, to_char; |
| 1146 | 1180 | ||
| 1147 | CHECK_VECTOR_OR_STRING (string); | ||
| 1148 | CHECK_NUMBER (from); | ||
| 1149 | |||
| 1150 | if (STRINGP (string)) | 1181 | if (STRINGP (string)) |
| 1151 | size = SCHARS (string); | 1182 | size = SCHARS (string); |
| 1152 | else | 1183 | else if (VECTORP (string)) |
| 1153 | size = ASIZE (string); | 1184 | size = ASIZE (string); |
| 1154 | |||
| 1155 | if (NILP (to)) | ||
| 1156 | to_char = size; | ||
| 1157 | else | 1185 | else |
| 1158 | { | 1186 | wrong_type_argument (Qarrayp, string); |
| 1159 | CHECK_NUMBER (to); | 1187 | |
| 1160 | 1188 | if (!validate_substring (from, to, size, &from_char, &to_char)) | |
| 1161 | to_char = XINT (to); | ||
| 1162 | if (to_char < 0) | ||
| 1163 | to_char += size; | ||
| 1164 | } | ||
| 1165 | |||
| 1166 | from_char = XINT (from); | ||
| 1167 | if (from_char < 0) | ||
| 1168 | from_char += size; | ||
| 1169 | if (!(0 <= from_char && from_char <= to_char && to_char <= size)) | ||
| 1170 | args_out_of_range_3 (string, make_number (from_char), | 1189 | args_out_of_range_3 (string, make_number (from_char), |
| 1171 | make_number (to_char)); | 1190 | make_number (to_char)); |
| 1172 | 1191 | ||
| @@ -1206,27 +1225,7 @@ With one argument, just copy STRING without its properties. */) | |||
| 1206 | 1225 | ||
| 1207 | size = SCHARS (string); | 1226 | size = SCHARS (string); |
| 1208 | 1227 | ||
| 1209 | if (NILP (from)) | 1228 | if (!validate_substring (from, to, size, &from_char, &to_char)) |
| 1210 | from_char = 0; | ||
| 1211 | else | ||
| 1212 | { | ||
| 1213 | CHECK_NUMBER (from); | ||
| 1214 | from_char = XINT (from); | ||
| 1215 | if (from_char < 0) | ||
| 1216 | from_char += size; | ||
| 1217 | } | ||
| 1218 | |||
| 1219 | if (NILP (to)) | ||
| 1220 | to_char = size; | ||
| 1221 | else | ||
| 1222 | { | ||
| 1223 | CHECK_NUMBER (to); | ||
| 1224 | to_char = XINT (to); | ||
| 1225 | if (to_char < 0) | ||
| 1226 | to_char += size; | ||
| 1227 | } | ||
| 1228 | |||
| 1229 | if (!(0 <= from_char && from_char <= to_char && to_char <= size)) | ||
| 1230 | args_out_of_range_3 (string, make_number (from_char), | 1229 | args_out_of_range_3 (string, make_number (from_char), |
| 1231 | make_number (to_char)); | 1230 | make_number (to_char)); |
| 1232 | 1231 | ||
| @@ -4614,29 +4613,7 @@ secure_hash (Lisp_Object algorithm, Lisp_Object object, Lisp_Object start, Lisp_ | |||
| 4614 | 4613 | ||
| 4615 | size = SCHARS (object); | 4614 | size = SCHARS (object); |
| 4616 | 4615 | ||
| 4617 | if (!NILP (start)) | 4616 | if (!validate_substring (start, end, size, &start_char, &end_char)) |
| 4618 | { | ||
| 4619 | CHECK_NUMBER (start); | ||
| 4620 | |||
| 4621 | start_char = XINT (start); | ||
| 4622 | |||
| 4623 | if (start_char < 0) | ||
| 4624 | start_char += size; | ||
| 4625 | } | ||
| 4626 | |||
| 4627 | if (NILP (end)) | ||
| 4628 | end_char = size; | ||
| 4629 | else | ||
| 4630 | { | ||
| 4631 | CHECK_NUMBER (end); | ||
| 4632 | |||
| 4633 | end_char = XINT (end); | ||
| 4634 | |||
| 4635 | if (end_char < 0) | ||
| 4636 | end_char += size; | ||
| 4637 | } | ||
| 4638 | |||
| 4639 | if (!(0 <= start_char && start_char <= end_char && end_char <= size)) | ||
| 4640 | args_out_of_range_3 (object, make_number (start_char), | 4617 | args_out_of_range_3 (object, make_number (start_char), |
| 4641 | make_number (end_char)); | 4618 | make_number (end_char)); |
| 4642 | 4619 | ||