Skip to content

Commit 2534a4f

Browse files
authored
Merge pull request #39 from ruifonseca/add_string_lower_core_method
Add String.lower() core method
2 parents f1a7d98 + 1eb8725 commit 2534a4f

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

docs/language.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ render_text("Hello world");
204204
- `parse_int(radix)`: Try to parse the entire string as an integer of the given `radix`. Returns `nil` on failure.
205205
- `trim()`: Produce a new string without whitespace at the beginning or end.
206206
- `upper()`: Produce a new string as the uppercase version of the string.
207+
- `lower()`: Produce a new string as the lowercase version of the string.
207208
- `split(sep)`: Given a separator string, split a string into an array of parts.
208209
- **Array**
209210
- `with_size(size, value)`: Creates a new array of the given size, filled with the given value.

src/runtime.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,15 @@ fn string_upper(actor: &mut Actor, s: Value) -> Result<Value, String>
273273
Ok(actor.alloc.str_val(&s).unwrap())
274274
}
275275

276+
/// Lowercase a String
277+
fn string_lower(actor: &mut Actor, s: Value) -> Result<Value, String>
278+
{
279+
let s = unwrap_str!(s);
280+
let s = s.to_lowercase();
281+
actor.gc_check(size_of::<Str>() + s.len(), &mut []);
282+
Ok(actor.alloc.str_val(&s).unwrap())
283+
}
284+
276285
/// Split a string by a separator and return an array of strings
277286
fn string_split(actor: &mut Actor, mut input: Value, sep: Value) -> Result<Value, String>
278287
{
@@ -396,6 +405,7 @@ pub fn get_method(val: Value, method_name: &str) -> Value
396405
static STRING_PARSE_INT: HostFn = HostFn { name: "parse_int", f: Fn2(string_parse_int) };
397406
static STRING_TRIM: HostFn = HostFn { name: "trim", f: Fn1(string_trim) };
398407
static STRING_UPPER: HostFn = HostFn { name: "upper", f: Fn1(string_upper) };
408+
static STRING_LOWER: HostFn = HostFn { name: "lower", f: Fn1(string_lower) };
399409
static STRING_SPLIT: HostFn = HostFn { name: "split", f: Fn2(string_split) };
400410
static STRING_TO_S: HostFn = HostFn { name: "to_s", f: Fn1(identity_method) };
401411

@@ -458,6 +468,7 @@ pub fn get_method(val: Value, method_name: &str) -> Value
458468
(Value::String(_), "parse_int") => &STRING_PARSE_INT,
459469
(Value::String(_), "trim") => &STRING_TRIM,
460470
(Value::String(_), "upper") => &STRING_UPPER,
471+
(Value::String(_), "lower") => &STRING_LOWER,
461472
(Value::String(_), "split") => &STRING_SPLIT,
462473
(Value::String(_), "to_s") => &STRING_TO_S,
463474

tests/strings.psh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,12 @@ assert("Cabaça".upper() == "CABAÇA");
9090
assert("λambda".upper() == "ΛAMBDA");
9191
assert("Γεια σας!".upper() == "ΓΕΙΑ ΣΑΣ!");
9292

93+
// Test lower
94+
assert("Hello, World!".lower() == "hello, world!");
95+
assert("lower".lower() == "lower");
96+
assert("TSCHÜSS".lower() == "tschüss");
97+
assert("CABAÇA".lower() == "cabaça");
98+
assert("ΛAMBDA".lower() == "λambda");
99+
assert("ΓΕΙΑ ΣΑΣ!".lower() == "γεια σας!");
100+
93101
$println("string tests passed");

0 commit comments

Comments
 (0)