mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 22:28:45 +01:00
Make it an error to skip or take a negative count.
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
@ -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"
|
||||
|
||||
@ -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]
|
||||
|
||||
1
test/core/sequence/skip_negative.wren
Normal file
1
test/core/sequence/skip_negative.wren
Normal file
@ -0,0 +1 @@
|
||||
[1, 2, 3].skip(-1) // expect runtime error: Count must be a non-negative integer.
|
||||
1
test/core/sequence/skip_not_int.wren
Normal file
1
test/core/sequence/skip_not_int.wren
Normal file
@ -0,0 +1 @@
|
||||
[1, 2, 3].skip(1.2) // expect runtime error: Count must be a non-negative integer.
|
||||
1
test/core/sequence/skip_not_num.wren
Normal file
1
test/core/sequence/skip_not_num.wren
Normal file
@ -0,0 +1 @@
|
||||
[1, 2, 3].skip("s") // expect runtime error: Count must be a non-negative integer.
|
||||
@ -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
|
||||
|
||||
1
test/core/sequence/take_negative.wren
Normal file
1
test/core/sequence/take_negative.wren
Normal file
@ -0,0 +1 @@
|
||||
[1, 2, 3].take(-1) // expect runtime error: Count must be a non-negative integer.
|
||||
1
test/core/sequence/take_not_int.wren
Normal file
1
test/core/sequence/take_not_int.wren
Normal file
@ -0,0 +1 @@
|
||||
[1, 2, 3].take(1.2) // expect runtime error: Count must be a non-negative integer.
|
||||
1
test/core/sequence/take_not_num.wren
Normal file
1
test/core/sequence/take_not_num.wren
Normal file
@ -0,0 +1 @@
|
||||
[1, 2, 3].take("s") // expect runtime error: Count must be a non-negative integer.
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
Reference in New Issue
Block a user