New string functions.

This commit is contained in:
Stephen Chung 2021-06-17 13:31:58 +08:00
parent 425e038f4b
commit 4093727dcb
2 changed files with 47 additions and 0 deletions

View File

@ -24,6 +24,7 @@ New features
* The `bits` method of an integer can be used to iterate through its bits. * The `bits` method of an integer can be used to iterate through its bits.
* New `$bool$`, `$int$`, `$float$` and `$string$` expression types for custom syntax. * New `$bool$`, `$int$`, `$float$` and `$string$` expression types for custom syntax.
* New methods `to_hex`, `to_octal` and `to_binary` for integer numbers. * New methods `to_hex`, `to_octal` and `to_binary` for integer numbers.
* New methods `to_upper`, `to_lower`, `make_upper`, `make_lower` for strings/characters.
Version 0.20.2 Version 0.20.2

View File

@ -100,6 +100,52 @@ mod string_functions {
} }
} }
pub fn to_upper(string: &str) -> ImmutableString {
string.to_uppercase().into()
}
pub fn make_upper(string: &mut ImmutableString) {
*string = to_upper(string);
}
pub fn to_lower(string: &str) -> ImmutableString {
string.to_lowercase().into()
}
pub fn make_lower(string: &mut ImmutableString) {
*string = to_lower(string);
}
#[rhai_fn(name = "to_upper")]
pub fn to_upper_char(character: char) -> char {
let mut stream = character.to_uppercase();
let ch = stream
.next()
.expect("never fails because there should be at least one character");
if stream.next().is_some() {
character
} else {
ch
}
}
#[rhai_fn(name = "make_upper")]
pub fn make_upper_char(character: &mut char) {
*character = to_upper_char(*character)
}
#[rhai_fn(name = "to_lower")]
pub fn to_lower_char(character: char) -> char {
let mut stream = character.to_lowercase();
let ch = stream
.next()
.expect("never fails because there should be at least one character");
if stream.next().is_some() {
character
} else {
ch
}
}
#[rhai_fn(name = "make_lower")]
pub fn make_lower_char(character: &mut char) {
*character = to_lower_char(*character)
}
#[rhai_fn(name = "index_of")] #[rhai_fn(name = "index_of")]
pub fn index_of_char_starting_from(string: &str, character: char, start: INT) -> INT { pub fn index_of_char_starting_from(string: &str, character: char, start: INT) -> INT {
let start = if start < 0 { let start = if start < 0 {