Category talk:Wren-str: Difference between revisions
Content added Content deleted
(→Source code: Multiple bug fixes.) |
(→Source code: Added Str.bisect and also made some changes to the Greek class.) |
||
Line 469: | Line 469: | ||
return split.where { |e| !e.isEmpty }.toList |
return split.where { |e| !e.isEmpty }.toList |
||
} |
} |
||
// Splits a string 's' into two parts, before and after the first occurrence |
|||
// of 'delim' and returns a list of those parts. |
|||
// The 'delim' itself can be optionally included in the second part. |
|||
// If 'delim' does not occur in 's', returns [s, ""]. |
|||
static bisect(s, delim, include) { |
|||
if (!(delim is String)) Fiber.abort("Delimiter must be a string.") |
|||
if (!(include is Bool)) Fiber.abort("Include must be true or false.") |
|||
if (!(s is String)) s = "%(s)" |
|||
var ix = s.indexOf(delim) |
|||
if (ix == -1) return [s, ""] |
|||
if (include) return [s[0...ix], s[ix..-1]] |
|||
var len = delim.bytes.count |
|||
return [s[0...ix], s[ix + len..-1]] |
|||
} |
|||
// Convenience version of bisect method which never includes the delimiter. |
|||
static bisect(s, delim) { bisect(s, delim, false) } |
|||
// Creates and returns a string from a list of bytes. |
// Creates and returns a string from a list of bytes. |
||
Line 646: | Line 664: | ||
*/ |
*/ |
||
class Greek { |
class Greek { |
||
// Returns the Greek alphabet, lower then upper case characters. |
|||
static alphabet { "αβγδεζηθικλμνξοπρςστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡ\u03a2ΣΤΥΦΧΨΩ" } |
|||
// Returns a list of the names of all Greek letters in alphabetical order. |
// Returns a list of the names of all Greek letters in alphabetical order. |
||
static names { |
static names { |
||
Line 653: | Line 674: | ||
"rho", "sigma final", "sigma", "tau", "upsilon", "phi", "chi", "psi", "omega" |
"rho", "sigma final", "sigma", "tau", "upsilon", "phi", "chi", "psi", "omega" |
||
] |
] |
||
} |
|||
// Returns the name of a Greek character or null if not found. |
|||
// Upper case characters are returned with the initial letter capitalized. |
|||
static name(char) { |
|||
if (char.count != 1) return null |
|||
var ix = alphabet.toList.indexOf(char) |
|||
if (ix == -1) return null |
|||
if (ix < 25) return names[ix] |
|||
return Str.capitalize(names[ix-25]) |
|||
} |
} |
||
// Finds and returns a Greek lower case character from its name. |
// Finds and returns a Greek lower case character from its name. |
||
static lower(name) { |
static lower(name) { |
||
name = Str.lower(name) |
|||
var ix = names.indexOf(name) |
var ix = names.indexOf(name) |
||
if (ix == -1) Fiber.abort("Name not found.") |
if (ix == -1) Fiber.abort("Name not found.") |
||
Line 664: | Line 696: | ||
// Finds and returns a Greek upper case character from its name. |
// Finds and returns a Greek upper case character from its name. |
||
static upper(name) { |
static upper(name) { |
||
name = Str.lower(name) |
|||
var ix = names.indexOf(name) |
var ix = names.indexOf(name) |
||
if (ix == -1) Fiber.abort("Name not found.") |
if (ix == -1) Fiber.abort("Name not found.") |