Category talk:Wren-str: Difference between revisions
Content added Content deleted
(→Source code: Added Str.bisect and also made some changes to the Greek class.) |
(→Source code: Added indent, dedent and tidy methods to Str class.) |
||
Line 417: | Line 417: | ||
// Convenience version of 'replace' where 'skip' is always zero. |
// Convenience version of 'replace' where 'skip' is always zero. |
||
static replace(s, from, to, n) { replace(s, from, to, n, 0) } |
static replace(s, from, to, n) { replace(s, from, to, n, 0) } |
||
// Adds 'by' to the start of each line of 's' |
|||
// and returns the result. |
|||
static indent(s, by) { |
|||
if (!(s is String)) Fiber.abort("First argument must be a string.") |
|||
if (!(by is String)) Fiber.abort("Second argument must be a string.") |
|||
var lines = s.split("\n") |
|||
return lines.map { |line| by + line }.join("\n") |
|||
} |
|||
// Removes 'by' from the start of each line of 's' which begins with it |
|||
// and returns the result. |
|||
static dedent(s, by) { |
|||
if (!(s is String)) Fiber.abort("First argument must be a string.") |
|||
if (!(by is String)) Fiber.abort("Second argument must be a string.") |
|||
var lines = s.split("\n") |
|||
var c = by.bytes.count |
|||
return lines.map { |line| |
|||
if (line.startsWith(by)) return line[c..-1] |
|||
return line |
|||
}.join("\n") |
|||
} |
|||
// Removes all spaces and tabs from the end of each line of s |
|||
// and returns the result. |
|||
static tidy(s) { |
|||
if (!(s is String)) Fiber.abort("Argument must be a string.") |
|||
var lines = s.split("\n") |
|||
return lines.map { |line| line.trimEnd(" \t") }.join("\n") |
|||
} |
|||
// Returns 's' repeated 'reps' times. |
// Returns 's' repeated 'reps' times. |