Split and replace in wren.
This commit is contained in:
@@ -132,6 +132,57 @@ 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.")
|
||||
}
|
||||
|
||||
var result = []
|
||||
|
||||
var last = 0
|
||||
var index = 0
|
||||
|
||||
var delimSize = delim.byteCount_
|
||||
var size = byteCount_
|
||||
|
||||
while (last < size && (index = indexOf(delim, last)) != -1) {
|
||||
result.add(this[last...index])
|
||||
last = index + delimSize
|
||||
}
|
||||
|
||||
if (last < size) {
|
||||
result.add(this[last..-1])
|
||||
} else {
|
||||
result.add("")
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
replace(from, to) {
|
||||
if (!(from is String) || from.isEmpty) {
|
||||
Fiber.abort("From must be a non-empty string.")
|
||||
} else if (!(to is String)) {
|
||||
Fiber.abort("To must be a string.")
|
||||
}
|
||||
|
||||
var result = ""
|
||||
|
||||
var last = 0
|
||||
var index = 0
|
||||
|
||||
var fromSize = from.byteCount_
|
||||
var size = byteCount_
|
||||
|
||||
while (last < size && (index = indexOf(from, last)) != -1) {
|
||||
result = result + this[last...index] + to
|
||||
last = index + fromSize
|
||||
}
|
||||
|
||||
if (last < size) result = result + this[last..-1]
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
*(count) {
|
||||
if (!(count is Num) || !count.isInteger || count < 0) {
|
||||
Fiber.abort("Count must be a non-negative integer.")
|
||||
|
||||
@@ -134,6 +134,57 @@ 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"
|
||||
" }\n"
|
||||
"\n"
|
||||
" var result = []\n"
|
||||
"\n"
|
||||
" var last = 0\n"
|
||||
" var index = 0\n"
|
||||
"\n"
|
||||
" var delimSize = delim.byteCount_\n"
|
||||
" var size = byteCount_\n"
|
||||
"\n"
|
||||
" while (last < size && (index = indexOf(delim, last)) != -1) {\n"
|
||||
" result.add(this[last...index])\n"
|
||||
" last = index + delimSize\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" if (last < size) {\n"
|
||||
" result.add(this[last..-1])\n"
|
||||
" } else {\n"
|
||||
" result.add(\"\")\n"
|
||||
" }\n"
|
||||
" return result\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" replace(from, to) {\n"
|
||||
" if (!(from is String) || from.isEmpty) {\n"
|
||||
" Fiber.abort(\"From must be a non-empty string.\")\n"
|
||||
" } else if (!(to is String)) {\n"
|
||||
" Fiber.abort(\"To must be a string.\")\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" var result = \"\"\n"
|
||||
"\n"
|
||||
" var last = 0\n"
|
||||
" var index = 0\n"
|
||||
"\n"
|
||||
" var fromSize = from.byteCount_\n"
|
||||
" var size = byteCount_\n"
|
||||
"\n"
|
||||
" while (last < size && (index = indexOf(from, last)) != -1) {\n"
|
||||
" result = result + this[last...index] + to\n"
|
||||
" last = index + fromSize\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" if (last < size) result = result + this[last..-1]\n"
|
||||
"\n"
|
||||
" return result\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" *(count) {\n"
|
||||
" if (!(count is Num) || !count.isInteger || count < 0) {\n"
|
||||
" Fiber.abort(\"Count must be a non-negative integer.\")\n"
|
||||
|
||||
Reference in New Issue
Block a user