diff --git a/src/vm/wren_core.wren b/src/vm/wren_core.wren index 03a000b8..2788308c 100644 --- a/src/vm/wren_core.wren +++ b/src/vm/wren_core.wren @@ -56,9 +56,21 @@ class Sequence { map(transformation) { MapSequence.new(this, transformation) } - skip(count) { SkipSequence.new(this, count) } + skip(count) { + if (!(count is Num) || !count.isInteger || count < 0) { + Fiber.abort("Count must be a non-negative integer.") + } - take(count) { TakeSequence.new(this, count) } + return SkipSequence.new(this, count) + } + + take(count) { + if (!(count is Num) || !count.isInteger || count < 0) { + Fiber.abort("Count must be a non-negative integer.") + } + + return TakeSequence.new(this, count) + } where(predicate) { WhereSequence.new(this, predicate) } @@ -173,9 +185,9 @@ class String is Sequence { bytes { StringByteSequence.new(this) } codePoints { StringCodePointSequence.new(this) } - split(delim) { - if (!(delim is String) || delim.isEmpty) { - Fiber.abort("Argument must be a non-empty string.") + split(delimiter) { + if (!(delimiter is String) || delimiter.isEmpty) { + Fiber.abort("Delimiter must be a non-empty string.") } var result = [] @@ -183,10 +195,10 @@ class String is Sequence { var last = 0 var index = 0 - var delimSize = delim.byteCount_ + var delimSize = delimiter.byteCount_ var size = byteCount_ - while (last < size && (index = indexOf(delim, last)) != -1) { + while (last < size && (index = indexOf(delimiter, last)) != -1) { result.add(this[last...index]) last = index + delimSize } diff --git a/src/vm/wren_core.wren.inc b/src/vm/wren_core.wren.inc index 079fed2f..8251f9f3 100644 --- a/src/vm/wren_core.wren.inc +++ b/src/vm/wren_core.wren.inc @@ -58,9 +58,21 @@ static const char* coreModuleSource = "\n" " map(transformation) { MapSequence.new(this, transformation) }\n" "\n" -" skip(count) { SkipSequence.new(this, count) }\n" +" skip(count) {\n" +" if (!(count is Num) || !count.isInteger || count < 0) {\n" +" Fiber.abort(\"Count must be a non-negative integer.\")\n" +" }\n" "\n" -" take(count) { TakeSequence.new(this, count) }\n" +" return SkipSequence.new(this, count)\n" +" }\n" +"\n" +" take(count) {\n" +" if (!(count is Num) || !count.isInteger || count < 0) {\n" +" Fiber.abort(\"Count must be a non-negative integer.\")\n" +" }\n" +"\n" +" return TakeSequence.new(this, count)\n" +" }\n" "\n" " where(predicate) { WhereSequence.new(this, predicate) }\n" "\n" @@ -175,9 +187,9 @@ static const char* coreModuleSource = " bytes { StringByteSequence.new(this) }\n" " codePoints { StringCodePointSequence.new(this) }\n" "\n" -" split(delim) {\n" -" if (!(delim is String) || delim.isEmpty) {\n" -" Fiber.abort(\"Argument must be a non-empty string.\")\n" +" split(delimiter) {\n" +" if (!(delimiter is String) || delimiter.isEmpty) {\n" +" Fiber.abort(\"Delimiter must be a non-empty string.\")\n" " }\n" "\n" " var result = []\n" @@ -185,10 +197,10 @@ static const char* coreModuleSource = " var last = 0\n" " var index = 0\n" "\n" -" var delimSize = delim.byteCount_\n" +" var delimSize = delimiter.byteCount_\n" " var size = byteCount_\n" "\n" -" while (last < size && (index = indexOf(delim, last)) != -1) {\n" +" while (last < size && (index = indexOf(delimiter, last)) != -1) {\n" " result.add(this[last...index])\n" " last = index + delimSize\n" " }\n" diff --git a/test/core/sequence/skip.wren b/test/core/sequence/skip.wren index b54a51fc..fabfca1e 100644 --- a/test/core/sequence/skip.wren +++ b/test/core/sequence/skip.wren @@ -15,14 +15,11 @@ var test = TestSequence.new().skip(0) System.print(test is Sequence) // expect: true System.print(test) // expect: instance of SkipSequence -// Skipping 0 changes nothing +// Skipping 0 changes nothing. System.print(test.toList) // expect: [1, 2, 3] -// Skipping 1 works +// Skipping 1 works. System.print(test.skip(1).toList) // expect: [2, 3] -// Skipping more than length of sequence produces empty list +// Skipping more than length of sequence produces empty list. System.print(test.skip(4).isEmpty) // expect: true - -// Skipping less than 0 changes nothing -System.print(test.skip(-10).toList) // expect: [1, 2, 3] diff --git a/test/core/sequence/skip_negative.wren b/test/core/sequence/skip_negative.wren new file mode 100644 index 00000000..957a9504 --- /dev/null +++ b/test/core/sequence/skip_negative.wren @@ -0,0 +1 @@ +[1, 2, 3].skip(-1) // expect runtime error: Count must be a non-negative integer. diff --git a/test/core/sequence/skip_not_int.wren b/test/core/sequence/skip_not_int.wren new file mode 100644 index 00000000..de171f58 --- /dev/null +++ b/test/core/sequence/skip_not_int.wren @@ -0,0 +1 @@ +[1, 2, 3].skip(1.2) // expect runtime error: Count must be a non-negative integer. diff --git a/test/core/sequence/skip_not_num.wren b/test/core/sequence/skip_not_num.wren new file mode 100644 index 00000000..63dfe9b2 --- /dev/null +++ b/test/core/sequence/skip_not_num.wren @@ -0,0 +1 @@ +[1, 2, 3].skip("s") // expect runtime error: Count must be a non-negative integer. diff --git a/test/core/sequence/take.wren b/test/core/sequence/take.wren index 81c188e8..a8767845 100644 --- a/test/core/sequence/take.wren +++ b/test/core/sequence/take.wren @@ -15,14 +15,11 @@ var test = TestSequence.new().take(3) System.print(test is Sequence) // expect: true System.print(test) // expect: instance of TakeSequence -// Taking 0 produces empty list +// Taking 0 produces empty list. System.print(test.take(0).isEmpty) // expect: true -// Taking 1 works +// Taking 1 works. System.print(test.take(1).toList) // expect: [1] -// Taking more than length of sequence produces whole sequence +// Taking more than length of sequence produces whole sequence. System.print(test.take(4).toList) // expect: [1, 2, 3] - -// Taking less than 0 produces empty list -System.print(test.take(-10).isEmpty) // expect: true diff --git a/test/core/sequence/take_negative.wren b/test/core/sequence/take_negative.wren new file mode 100644 index 00000000..5b98f021 --- /dev/null +++ b/test/core/sequence/take_negative.wren @@ -0,0 +1 @@ +[1, 2, 3].take(-1) // expect runtime error: Count must be a non-negative integer. diff --git a/test/core/sequence/take_not_int.wren b/test/core/sequence/take_not_int.wren new file mode 100644 index 00000000..d3e2452b --- /dev/null +++ b/test/core/sequence/take_not_int.wren @@ -0,0 +1 @@ +[1, 2, 3].take(1.2) // expect runtime error: Count must be a non-negative integer. diff --git a/test/core/sequence/take_not_num.wren b/test/core/sequence/take_not_num.wren new file mode 100644 index 00000000..0a818a27 --- /dev/null +++ b/test/core/sequence/take_not_num.wren @@ -0,0 +1 @@ +[1, 2, 3].take("s") // expect runtime error: Count must be a non-negative integer. diff --git a/test/core/string/split_argument_not_string.wren b/test/core/string/split_argument_not_string.wren index c4fdd1af..424270db 100644 --- a/test/core/string/split_argument_not_string.wren +++ b/test/core/string/split_argument_not_string.wren @@ -1 +1 @@ -"foo".split(1) // expect runtime error: Argument must be a non-empty string. +"foo".split(1) // expect runtime error: Delimiter must be a non-empty string. diff --git a/test/core/string/split_empty_seperator.wren b/test/core/string/split_empty_seperator.wren index ea211aaf..f6ff63fe 100644 --- a/test/core/string/split_empty_seperator.wren +++ b/test/core/string/split_empty_seperator.wren @@ -1 +1 @@ -"foo".split("") // expect runtime error: Argument must be a non-empty string. +"foo".split("") // expect runtime error: Delimiter must be a non-empty string.