Make it an error to skip or take a negative count.

This commit is contained in:
Bob Nystrom
2017-03-15 07:22:44 -07:00
parent 9f93119377
commit 8d313be3ce
12 changed files with 52 additions and 28 deletions
+19 -7
View File
@@ -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
}
+19 -7
View File
@@ -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"