Category talk:Wren-str: Difference between revisions

→‎Source code: Added new Strs class & partially rewrote Str class to improve performance for large strings.
(→‎Source code: Added a Str.indexOf method and a Utf8 class.)
(→‎Source code: Added new Strs class & partially rewrote Str class to improve performance for large strings.)
Line 119:
if (!(s is String)) s = "%(s)"
if (s == "") return s
var cpschars = s.codePoints.toList
forvar (icount in= 0...cpschars.count) {
var ci = cps[i]0
iffor (c >=in 65 && c <= 90s.codePoints) cps[i] = c + 32{
if (c >= 65 && c <= 90) chars[i] = String.fromCodePoint(c + 32)
i = i + 1
}
return cps.reduce("")count {< |acc,1000) c|? accStrs.concat_(chars) +: StringStrs.fromCodePointconcat(c)chars, }1000)
}
 
Line 131 ⟶ 133:
if (!(s is String)) s = "%(s)"
if (s == "") return s
var cpschars = s.codePoints.toList
forvar (icount in= 0...cpschars.count) {
var ci = cps[i]0
iffor (c >=in 97 && c <= 122s.codePoints) cps[i] = c - 32{
if (c >= 97 && c <= 122) chars[i] = String.fromCodePoint(c - 32)
i = i + 1
}
return cps.reduce("")count {< |acc,1000) c|? accStrs.concat_(chars) +: StringStrs.fromCodePointconcat(c)chars, }1000)
}
 
Line 143 ⟶ 147:
if (!(s is String)) s = "%(s)"
if (s == "") return s
var cpschars = s.codePoints.toList
forvar (icount in= 0...cpschars.count) {
var ci = cps[i]0
for (c in s.codePoints) {
if (c >= 65 && c <= 90) {
cpschars[i] = String.fromCodePoint(c + 32)
} else if (c >= 97 && c <= 122) {
cpschars[i] = String.fromCodePoint(c - 32)
}
i = i + 1
}
return cps.reduce("")count {< |acc,1000) c|? accStrs.concat_(chars) +: StringStrs.fromCodePointconcat(c)chars, }1000)
}
 
Line 159 ⟶ 165:
if (!(s is String)) s = "%(s)"
if (s == "") return s
var cpsstart = (s.codePointsstartsWith("[") && s.toListcount > 1) ? 1 : 0
var startc = (s[start].startsWith("codePoints[") && cps.count > 1) ? 1 : 0]
var c = cps[start]
if (c >= 97 && c <= 122) {
cps[start]var cs = String.fromCodePoint(c - 32) + s[start+1..-1]
returnif cps.reduce(""start == 1) {cs |acc,= c| acc"[" + String.fromCodePoint(c) }cs
return cs
}
return s
Line 174 ⟶ 180:
if (s == "") return s
var words = s.split(" ")
return Strs.join(words.map { |w| capitalize(w) }.join(toList, " ")
}
 
Line 192 ⟶ 198:
for (i in 0..count-2) chars[i] = chars[i+1]
chars[-1] = t
return (count < 1000) ? Strs.concat_(chars) : Strs.joinconcat(chars, 1000)
}
 
Line 204 ⟶ 210:
for (i in count-2..0) chars[i+1] = chars[i]
chars[0] = t
return (count < 1000) ? Strs.concat_(chars) : Strs.joinconcat(chars, 1000)
}
 
Line 214 ⟶ 220:
if (!(r is Range)) Fiber.abort("Second argument must be a range.")
if (!(s is String)) s = "%(s)"
return Strs.concat(s.toList[r].join()
}
 
Line 255 ⟶ 261:
var chars = s.toList
chars[i] = t
return charsStrs.joinconcat(chars)
}
 
Line 265 ⟶ 271:
var chars = s.toList
chars.insert(i, t)
return charsStrs.joinconcat(chars)
}
 
Line 274 ⟶ 280:
var chars = s.toList
chars.removeAt(i)
return charsStrs.joinconcat(chars)
}
 
Line 287 ⟶ 293:
chars[i] = chars[j]
chars[j] = t
return charsStrs.joinconcat(chars)
}
 
// Private helper method for 'repeat'.
static repeat_(s, reps) {
var rs = ""
for (i in 0...reps) rs = rs + s
return rs
}
 
// Returns 's' repeated 'reps' times.
// If 'chunkSize' is chosen appropriately, this should be much faster than String's * operator
// for a large number of repetitions.
static repeat(s, reps, chunkSize) {
if (!(s is String)) s = "%(s)"
if (!(reps is Num && reps.isInteger && reps > 0)) {
Fiber.abort("Repetitions must be a positive integer.")
}
if (!(chunkSize is Num && chunkSize.isInteger && chunkSize > 0)) {
Fiber.abort("Chunk size must be a positive integer.")
}
if (reps == 0) return ""
var chunks = (reps/chunkSize).floor
if (chunks == 0) return repeat_(s, reps)
var lastSize = reps % chunkSize
if (lastSize == 0) {
lastSize = chunkSize
} else {
chunks = chunks + 1
}
var rs = ""
var chunk = repeat_(s, chunkSize)
var lastChunk = repeat_(s, lastSize)
for (i in 0...chunks) {
rs = rs + ((i < chunks - 1) ? chunk : lastChunk)
}
return rs
}
 
// Convenience version of the above which uses a 'chunkSize' of 8000. This usually gives a good result.
static repeat(s, reps) { repeat(s, reps, 8000) }
 
// Splits a string 's' into chunks of not more than 'size' characters.
Line 314 ⟶ 359:
}
 
/*
Strs contains routines applicable to lists of strings.
*/
class Strs {
// Private helper method for 'concat'.
static concat_(ls) {
var s = ""
for (e in ls) {
s = s + e
}
return s
}
 
// Returns the strings in the list 'ls' concatenated together.
// If 'chunkSize' is chosen appropriately, this should be much faster than Sequence.join()
// for a large list of strings. For extra speed, only minimal type checks are made.
static concat(ls, chunkSize) {
if (!(ls is List)) Fiber.abort("First argument must be a list of strings.")
if (chunkSize.type != Num || !chunkSize.isInteger || chunkSize < 1) {
Fiber.abort("Second argument must be a positive integer.")
}
var count = ls.count
if (count == 0) return ""
if (ls[0].type != String) Fiber.abort("First argument must be a list of strings.")
var chunks = (count/chunkSize).floor
if (chunks == 0) return concat_(ls)
var lastSize = count % chunkSize
if (lastSize == 0) {
lastSize = chunkSize
} else {
chunks = chunks + 1
}
var s = ""
for (i in 0...chunks) {
var endSize = (i < chunks-1) ? chunkSize : lastSize
s = s + concat_(ls[i*chunkSize...(i*chunkSize + endSize)])
}
return s
}
 
// Convenience version of the above which uses a 'chunkSize' of 1000. This usually gives a good result.
static concat(ls) { concat(ls, 1000) }
 
// Private helper method for 'join'.
static join_(ls, sep) {
var first = true
var s = ""
for (e in ls) {
if (!first) s = s + sep
first = false
s = s + e
}
return s
}
 
// Returns the strings in the list 'ls' joined together using the separator 'sep'.
// If 'chunkSize' is chosen appropriately, this should be much faster than Sequence.join(sep)
// for a large list of strings. For extra speed, only minimal type checks are made.
static join(ls, sep, chunkSize) {
if (!(ls is List)) Fiber.abort("First argument must be a list of strings.")
if (sep.type != String) Fiber.abort("Second argument must be a string")
if (sep == "") return concat(ls, chunkSize)
if (chunkSize.type != Num || !chunkSize.isInteger || chunkSize < 1) {
Fiber.abort("Third argument must be a positive integer.")
}
var count = ls.count
if (count == 0) return ""
if (ls[0].type != String) Fiber.abort("First argument must be a list of strings.")
var chunks = (count/chunkSize).floor
if (chunks == 0) return join_(ls, sep)
var lastSize = count % chunkSize
if (lastSize == 0) {
lastSize = chunkSize
} else {
chunks = chunks + 1
}
var s = ""
for (i in 0...chunks) {
if (i > 0) s = s + sep
var endSize = (i < chunks-1) ? chunkSize : lastSize
s = s + join_(ls[i*chunkSize...(i*chunkSize + endSize)], sep)
}
return s
}
 
// Convenience version of the above which uses a 'chunkSize' of 1000. This usually gives a good result.
static join(ls, sep) { join(ls, sep, 1000) }
}
/*
Utf8 contains routines which are specific to the UTF-8 encoding of a string's bytes or codepoints.
Line 355 ⟶ 489:
var Str_Char = Char
var Str_Str = Str
var Str_Strs = Strs
var Str_Utf8 = Utf8</lang>
9,476

edits