Jump to content

Category talk:Wren-str: Difference between revisions

→‎Source code: Changes to make this module more consistent with other Wren modules.
(→‎Source code: Added new Strs class & partially rewrote Str class to improve performance for large strings.)
(→‎Source code: Changes to make this module more consistent with other Wren modules.)
Line 213:
}
 
/* The indices (or ranges thereof) for all the following functions are measured in codepoints (not bytes). Negative indices count backwards from the end of the string.
As with core library methods, the indices must be within bounds or errors will be generated. */
 
Line 221:
if (!(s is String)) s = "%(s)"
return Strs.concat(s.toList[r])
}
 
// Private helper method to check whether an index is valid.
static checkIndex_(s, index, inc) {
if (index.type !(i is= Num &&|| i!index.isInteger)) Fiber.abort("Index must be an integer.")
var c = s.count + inc
if (!(iindex is>= Numc &&|| i.isIntegerindex &&< i >= 0)-c) Fiber.abort("Index mustis beout a non-negativeof integerbounds.")
}
 
// Gets the character of 's' at index 'i'. Throws an error if 'i is out of bounds.
static get(s, i) {
if (!(i is Num && i.isInteger && i >= 0)) Fiber.abort("Index must be a non-negative integer.")
if (!(s is String)) s = "%(s)"
checkIndex_(s, i, 0)
if (i < 0) i = s.count + i
return s.toList[i]
}
Line 232 ⟶ 240:
// Gets the character of 's' at index 'i'. Returns null if 'i is out of bounds.
static getOrNull(s, i) {
if (!(i is Num && i.isInteger)) Fiber.abort("Index must be an integer.")
if (!(s is String)) s = "%(s)"
if (!(i is Num && i.isInteger && i >= 0)) Fiber.abort("Index must be a non-negativean integer.")
if (i < 0) i = s.count + i
return (i >= 0 && i < s.count) ? s.toList[i] : null
}
Line 256 ⟶ 265:
// Changes the character of 's' at index 'i' to the string 't'.
static change(s, i, t) {
if (!(i is Num && i.isInteger && i >= 0)) Fiber.abort("Index must be a non-negative integer.")
if (!(t is String)) Fiber.abort("Replacement must be a string.")
if (!(s is String)) s = "%(s)"
checkIndex_(s, i, 0)
if (i < 0) i = s.count + i
var chars = s.toList
chars[i] = t
Line 266 ⟶ 276:
// Inserts at index 'i' of 's' the string 't'.
static insert(s, i, t) {
if (!(i is Num && i.isInteger && i >= 0)) Fiber.abort("Index must be a non-negative integer.")
if (!(t is String)) Fiber.abort("Insertion must be a string.")
if (!(s is String)) s = "%(s)"
checkIndex_(s, i, 1)
if (i < 0) i = s.count + i + 1
var chars = s.toList
chars.insert(i, t)
Line 276 ⟶ 287:
// Deletes the character of 's' at index 'i'.
static delete(s, i) {
if (!(i is Num && i.isInteger && i >= 0)) Fiber.abort("Index must be a non-negative integer.")
if (!(s is String)) s = "%(s)"
checkIndex_(s, i, 0)
if (i < 0) i = s.count + i
var chars = s.toList
chars.removeAt(i)
Line 285 ⟶ 297:
// Exchanges the characters of 's' at indices 'i' and 'j'
static exchange(s, i, j) {
if (!(i is Num && i.isInteger && i >= 0)) Fiber.abort("First index must be a non-negative integer.")
if (!(j is Num && j.isInteger && j >= 0)) Fiber.abort("Second index must be a non-negative integer.")
if (!(s is String)) s = "%(s)"
checkIndex_(s, i, 0)
if (i < 0) i = s.count + i
checkIndex_(s, j, 0)
if (j < 0) j = s.count + j
if (i == j) return s
var chars = s.toList
9,485

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.