Jump to content

Category talk:Wren-str: Difference between revisions

→‎Source code: Made Str.lshift/rshift methods more general.
(→‎Source code: Added more versatile Str.replace and minor edit to Str.exchange method.)
(→‎Source code: Made Str.lshift/rshift methods more general.)
Line 244:
}
 
// Performs a circular shift of the characters of 's' one'n' placeplaces to the left.
// If 'n' is negative performs a circular right shift by '-n' places instead.
static lshift(s, n) {
if (!(s is String)) s = "%(s)"
if (!(n is Num) || !n.isInteger) Fiber.abort("'n' must be an integer.")
var chars = s.toList
var count = chars.count
if (count < 2) return s
varif t(n =< chars[0]) return rshift(s, -n)
forn (i= inn % 0..count-2) chars[i] = chars[i+1]
chars[-1]if (n == 0) return ts
for (i in 1..n) {
var t = chars[0]
for (j in 0..count-2) chars[j] = chars[j+1]
chars[-1] = t
}
return (count < 1000) ? Strs.concat_(chars) : Strs.concat(chars, 1000)
}
 
// Performs a circular shift of the characters of 's' one'n' placeplaces to the right.
// If 'n' is negative performs a circular left shift by '-n' places instead.
static rshift(s, n) {
if (!(s is String)) s = "%(s)"
if (!(n is Num) || !n.isInteger) Fiber.abort("'n' must be an integer.")
var chars = s.toList
var count = chars.count
if (count < 2) return s
varif t(n =< 0) return lshift(s, chars[-1]n)
forn (i= inn % count-2..0) chars[i+1] = chars[i]
chars[0]if (n == 0) return ts
for (i in 1..n) {
var t = chars[-1]
for (j in count-2..0) chars[j+1] = chars[j]
chars[0] = t
}
return (count < 1000) ? Strs.concat_(chars) : Strs.concat(chars, 1000)
}
 
// Convenience versions of the above methods which shift by just 1 place.
static lshift(s) { lshift(s, 1) }
static rshift(s) { rshift(s, 1) }
 
/* The indices (or ranges thereof) for all the following functions are measured in codepoints
9,483

edits

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