Category talk:Wren-str: Difference between revisions
Content added Content deleted
(→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: | Line 213: | ||
} |
} |
||
/* The indices (or ranges thereof) for all the following functions are measured in codepoints (not bytes). |
/* 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. */ |
As with core library methods, the indices must be within bounds or errors will be generated. */ |
||
Line 221: | Line 221: | ||
if (!(s is String)) s = "%(s)" |
if (!(s is String)) s = "%(s)" |
||
return Strs.concat(s.toList[r]) |
return Strs.concat(s.toList[r]) |
||
} |
|||
// Private helper method to check whether an index is valid. |
|||
static checkIndex_(s, index, inc) { |
|||
⚫ | |||
var c = s.count + inc |
|||
⚫ | |||
} |
} |
||
// Gets the character of 's' at index 'i'. Throws an error if 'i is out of bounds. |
// Gets the character of 's' at index 'i'. Throws an error if 'i is out of bounds. |
||
static get(s, i) { |
static get(s, i) { |
||
⚫ | |||
if (!(s is String)) s = "%(s)" |
if (!(s is String)) s = "%(s)" |
||
checkIndex_(s, i, 0) |
|||
if (i < 0) i = s.count + i |
|||
return s.toList[i] |
return s.toList[i] |
||
} |
} |
||
Line 232: | Line 240: | ||
// Gets the character of 's' at index 'i'. Returns null if 'i is out of bounds. |
// Gets the character of 's' at index 'i'. Returns null if 'i is out of bounds. |
||
static getOrNull(s, i) { |
static getOrNull(s, i) { |
||
⚫ | |||
if (!(s is String)) s = "%(s)" |
if (!(s is String)) s = "%(s)" |
||
⚫ | |||
if (i < 0) i = s.count + i |
|||
return (i >= 0 && i < s.count) ? s.toList[i] : null |
return (i >= 0 && i < s.count) ? s.toList[i] : null |
||
} |
} |
||
Line 256: | Line 265: | ||
// Changes the character of 's' at index 'i' to the string 't'. |
// Changes the character of 's' at index 'i' to the string 't'. |
||
static change(s, i, t) { |
static change(s, i, t) { |
||
⚫ | |||
if (!(t is String)) Fiber.abort("Replacement must be a string.") |
if (!(t is String)) Fiber.abort("Replacement must be a string.") |
||
if (!(s is String)) s = "%(s)" |
if (!(s is String)) s = "%(s)" |
||
checkIndex_(s, i, 0) |
|||
if (i < 0) i = s.count + i |
|||
var chars = s.toList |
var chars = s.toList |
||
chars[i] = t |
chars[i] = t |
||
Line 266: | Line 276: | ||
// Inserts at index 'i' of 's' the string 't'. |
// Inserts at index 'i' of 's' the string 't'. |
||
static insert(s, i, 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 (!(t is String)) Fiber.abort("Insertion must be a string.") |
||
if (!(s is String)) s = "%(s)" |
if (!(s is String)) s = "%(s)" |
||
checkIndex_(s, i, 1) |
|||
if (i < 0) i = s.count + i + 1 |
|||
var chars = s.toList |
var chars = s.toList |
||
chars.insert(i, t) |
chars.insert(i, t) |
||
Line 276: | Line 287: | ||
// Deletes the character of 's' at index 'i'. |
// Deletes the character of 's' at index 'i'. |
||
static delete(s, 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)" |
if (!(s is String)) s = "%(s)" |
||
checkIndex_(s, i, 0) |
|||
if (i < 0) i = s.count + i |
|||
var chars = s.toList |
var chars = s.toList |
||
chars.removeAt(i) |
chars.removeAt(i) |
||
Line 285: | Line 297: | ||
// Exchanges the characters of 's' at indices 'i' and 'j' |
// Exchanges the characters of 's' at indices 'i' and 'j' |
||
static exchange(s, i, 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)" |
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 |
if (i == j) return s |
||
var chars = s.toList |
var chars = s.toList |