Turn the string into a smaller (or equal) string containing only the * substring specified by the 'start' and 'end' indexes. * * start and end can be negative, where -1 means the last character of the * string, -2 the penultimate character, and so forth. * * The interval is inclusive, so the start and end characters will be part * of the resulting string. * * The string is modified in-pla
| 737 | * sdsrange(s,1,-1); => "ello World" |
| 738 | */ |
| 739 | void sdsrange(sds s, ssize_t start, ssize_t end) { |
| 740 | size_t newlen, len = sdslen(s); |
| 741 | |
| 742 | if (len == 0) return; |
| 743 | if (start < 0) { |
| 744 | start = len+start; |
| 745 | if (start < 0) start = 0; |
| 746 | } |
| 747 | if (end < 0) { |
| 748 | end = len+end; |
| 749 | if (end < 0) end = 0; |
| 750 | } |
| 751 | newlen = (start > end) ? 0 : (end-start)+1; |
| 752 | if (newlen != 0) { |
| 753 | if (start >= (ssize_t)len) { |
| 754 | newlen = 0; |
| 755 | } else if (end >= (ssize_t)len) { |
| 756 | end = len-1; |
| 757 | newlen = (start > end) ? 0 : (end-start)+1; |
| 758 | } |
| 759 | } else { |
| 760 | start = 0; |
| 761 | } |
| 762 | if (start && newlen) memmove(s, s+start, newlen); |
| 763 | s[newlen] = 0; |
| 764 | sdssetlen(s,newlen); |
| 765 | } |
| 766 | |
| 767 | /* Apply tolower() to every character of the sds string 's'. */ |
| 768 | void sdstolower(sds s) { |