Fixed slice() returning empty string when referring to last character (#751)

* Fixed slice() returning empty string when referring to last character
This commit is contained in:
replaceitem authored and GitHub committed 2021-03-01 22:10:49 -04:00
1 parent aa372f5480
commit ca4db433c1
3 files changed
+4 -4

No files matched your search

+2 -2
View File
@@ -187,10 +187,10 @@ other parts. In that case consecutive calls to `slice` will refer to index `0` t
cannot go back nor track where they are in the sequence (see examples). cannot go back nor track where they are in the sequence (see examples).
<pre> <pre>
slice([0,1,2,3,4,5], 1, 3) => [1, 2, 3] slice([0,1,2,3,4,5], 1, 3) => [1, 2]
slice('foobar', 0, 1) => 'f' slice('foobar', 0, 1) => 'f'
slice('foobar', 3) => 'bar' slice('foobar', 3) => 'bar'
slice(range(10), 3, 5) => [3, 4, 5] slice(range(10), 3, 5) => [3, 4]
slice(range(10), 5) => [5, 6, 7, 8, 9] slice(range(10), 5) => [5, 6, 7, 8, 9]
r = range(100); [slice(r, 5, 7), slice(r, 1, 3)] => [[5, 6], [8, 9]] r = range(100); [slice(r, 5, 7), slice(r, 1, 3)] => [[5, 6], [8, 9]]
</pre> </pre>
@@ -341,7 +341,7 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
int from = normalizeIndex(fromDesc, size); int from = normalizeIndex(fromDesc, size);
if (toDesc == null) if (toDesc == null)
return new ListValue((Collection<? extends Value>) getItems().subList(from, size)); return new ListValue((Collection<? extends Value>) getItems().subList(from, size));
int to = normalizeIndex(toDesc, size); int to = normalizeIndex(toDesc, size+1);
if (from > to) if (from > to)
return ListValue.of(); return ListValue.of();
return new ListValue((Collection<? extends Value>) getItems().subList(from, to)); return new ListValue((Collection<? extends Value>) getItems().subList(from, to));
+1 -1
View File
@@ -186,7 +186,7 @@ public abstract class Value implements Comparable<Value>, Cloneable
int size = value.length(); int size = value.length();
int from = ListValue.normalizeIndex(fromDesc, size); int from = ListValue.normalizeIndex(fromDesc, size);
if (toDesc == null) return new StringValue(value.substring(from)); if (toDesc == null) return new StringValue(value.substring(from));
int to = ListValue.normalizeIndex(toDesc, size); int to = ListValue.normalizeIndex(toDesc, size+1);
if (from > to) return StringValue.EMPTY; if (from > to) return StringValue.EMPTY;
return new StringValue(value.substring(from, to)); return new StringValue(value.substring(from, to));
} }