diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/fns.c | 36 |
1 files changed, 27 insertions, 9 deletions
| @@ -509,31 +509,49 @@ Elements of ALIST that are not conses are also shared.") | |||
| 509 | DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0, | 509 | DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0, |
| 510 | "Return a substring of STRING, starting at index FROM and ending before TO.\n\ | 510 | "Return a substring of STRING, starting at index FROM and ending before TO.\n\ |
| 511 | TO may be nil or omitted; then the substring runs to the end of STRING.\n\ | 511 | TO may be nil or omitted; then the substring runs to the end of STRING.\n\ |
| 512 | If FROM or TO is negative, it counts from the end.") | 512 | If FROM or TO is negative, it counts from the end.\n\ |
| 513 | \n\ | ||
| 514 | This function allows vectors as well as strings.") | ||
| 513 | (string, from, to) | 515 | (string, from, to) |
| 514 | Lisp_Object string; | 516 | Lisp_Object string; |
| 515 | register Lisp_Object from, to; | 517 | register Lisp_Object from, to; |
| 516 | { | 518 | { |
| 517 | Lisp_Object res; | 519 | Lisp_Object res; |
| 520 | int size; | ||
| 521 | |||
| 522 | if (! (STRINGP (string) || VECTORP (string))) | ||
| 523 | wrong_type_argument (Qarrayp, string); | ||
| 518 | 524 | ||
| 519 | CHECK_STRING (string, 0); | ||
| 520 | CHECK_NUMBER (from, 1); | 525 | CHECK_NUMBER (from, 1); |
| 526 | |||
| 527 | if (STRINGP (string)) | ||
| 528 | size = XSTRING (string)->size; | ||
| 529 | else | ||
| 530 | size = XVECTOR (string)->size; | ||
| 531 | |||
| 521 | if (NILP (to)) | 532 | if (NILP (to)) |
| 522 | to = Flength (string); | 533 | to = size; |
| 523 | else | 534 | else |
| 524 | CHECK_NUMBER (to, 2); | 535 | CHECK_NUMBER (to, 2); |
| 525 | 536 | ||
| 526 | if (XINT (from) < 0) | 537 | if (XINT (from) < 0) |
| 527 | XSETINT (from, XINT (from) + XSTRING (string)->size); | 538 | XSETINT (from, XINT (from) + size); |
| 528 | if (XINT (to) < 0) | 539 | if (XINT (to) < 0) |
| 529 | XSETINT (to, XINT (to) + XSTRING (string)->size); | 540 | XSETINT (to, XINT (to) + size); |
| 530 | if (!(0 <= XINT (from) && XINT (from) <= XINT (to) | 541 | if (!(0 <= XINT (from) && XINT (from) <= XINT (to) |
| 531 | && XINT (to) <= XSTRING (string)->size)) | 542 | && XINT (to) <= size)) |
| 532 | args_out_of_range_3 (string, from, to); | 543 | args_out_of_range_3 (string, from, to); |
| 533 | 544 | ||
| 534 | res = make_string (XSTRING (string)->data + XINT (from), | 545 | if (STRINGP (string)) |
| 535 | XINT (to) - XINT (from)); | 546 | { |
| 536 | copy_text_properties (from, to, string, make_number (0), res, Qnil); | 547 | res = make_string (XSTRING (string)->data + XINT (from), |
| 548 | XINT (to) - XINT (from)); | ||
| 549 | copy_text_properties (from, to, string, make_number (0), res, Qnil); | ||
| 550 | } | ||
| 551 | else | ||
| 552 | res = Fvector (XINT (to) - XINT (from), | ||
| 553 | XVECTOR (string)->contents + XINT (from)); | ||
| 554 | |||
| 537 | return res; | 555 | return res; |
| 538 | } | 556 | } |
| 539 | 557 | ||