From b97e8c15a58761b053aafc31720ae3f746136986 Mon Sep 17 00:00:00 2001 From: MuhamedMagdi Date: Tue, 30 Jul 2024 14:40:10 +0300 Subject: [PATCH 1/2] feat(stdlib): add new functions in `std/text` - `reverse`: reverses the given text. - `is_prefix`: checks if a string is a prefix of another string. - `is_suffix`: checks if a string is a suffix of another string. These addition of `is_prefix` & `is_suffix` complement the existing `contains` function. --- src/std/text.ab | 23 +++++++++++++++++++++++ src/tests/stdlib/is_prefix.ab | 7 +++++++ src/tests/stdlib/is_suffix.ab | 7 +++++++ src/tests/stdlib/reverse.ab | 8 ++++++++ 4 files changed, 45 insertions(+) create mode 100644 src/tests/stdlib/is_prefix.ab create mode 100644 src/tests/stdlib/is_suffix.ab create mode 100644 src/tests/stdlib/reverse.ab diff --git a/src/std/text.ab b/src/std/text.ab index 426b00e0..c9c533cf 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -82,3 +82,26 @@ pub fun contains(text: Text, phrase: Text): Bool { return result == "1" } + +/// Reverse a text using `rev` +pub fun reverse(text: Text): Text { + return unsafe $echo "{text}" | rev$ +} + +/// Check if text starts with a value +pub fun is_prefix(text: Text, prefix: Text): Bool { + let result = unsafe $if [[ "{text}" == "{prefix}"* ]]; then + echo 1 + fi$ + + return result == "1" +} + +/// Check if text ends with a value +pub fun is_suffix(text: Text, suffix: Text): Bool { + let result = unsafe $if [[ "{text}" == *"{suffix}" ]]; then + echo 1 + fi$ + + return result == "1" +} diff --git a/src/tests/stdlib/is_prefix.ab b/src/tests/stdlib/is_prefix.ab new file mode 100644 index 00000000..a193d364 --- /dev/null +++ b/src/tests/stdlib/is_prefix.ab @@ -0,0 +1,7 @@ +import * from "std/text" + +main { + if is_prefix("hello world", "hello") { + echo "Succeded" + } +} diff --git a/src/tests/stdlib/is_suffix.ab b/src/tests/stdlib/is_suffix.ab new file mode 100644 index 00000000..39c34ecf --- /dev/null +++ b/src/tests/stdlib/is_suffix.ab @@ -0,0 +1,7 @@ +import * from "std/text" + +main { + if is_suffix("hello world", "world") { + echo "Succeded" + } +} diff --git a/src/tests/stdlib/reverse.ab b/src/tests/stdlib/reverse.ab new file mode 100644 index 00000000..c0f78aad --- /dev/null +++ b/src/tests/stdlib/reverse.ab @@ -0,0 +1,8 @@ +import * from "std/text" + +// Output +// dlrow olleh + +main { + echo reverse("hello world") +} From ace421ab9654912705c969b1eb624ea9f7cf0da3 Mon Sep 17 00:00:00 2001 From: MuhamedMagdi Date: Tue, 30 Jul 2024 19:11:03 +0300 Subject: [PATCH 2/2] refactor: change `is_prefix` & `is_suffix` function names is_prefix -> starts_with is_suffix -> ends_with --- src/std/text.ab | 4 ++-- src/tests/stdlib/{is_prefix.ab => ends_with.ab} | 2 +- src/tests/stdlib/{is_suffix.ab => starts_with.ab} | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) rename src/tests/stdlib/{is_prefix.ab => ends_with.ab} (60%) rename src/tests/stdlib/{is_suffix.ab => starts_with.ab} (59%) diff --git a/src/std/text.ab b/src/std/text.ab index c9c533cf..cb43f312 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -89,7 +89,7 @@ pub fun reverse(text: Text): Text { } /// Check if text starts with a value -pub fun is_prefix(text: Text, prefix: Text): Bool { +pub fun starts_with(text: Text, prefix: Text): Bool { let result = unsafe $if [[ "{text}" == "{prefix}"* ]]; then echo 1 fi$ @@ -98,7 +98,7 @@ pub fun is_prefix(text: Text, prefix: Text): Bool { } /// Check if text ends with a value -pub fun is_suffix(text: Text, suffix: Text): Bool { +pub fun ends_with(text: Text, suffix: Text): Bool { let result = unsafe $if [[ "{text}" == *"{suffix}" ]]; then echo 1 fi$ diff --git a/src/tests/stdlib/is_prefix.ab b/src/tests/stdlib/ends_with.ab similarity index 60% rename from src/tests/stdlib/is_prefix.ab rename to src/tests/stdlib/ends_with.ab index a193d364..c483b66f 100644 --- a/src/tests/stdlib/is_prefix.ab +++ b/src/tests/stdlib/ends_with.ab @@ -1,7 +1,7 @@ import * from "std/text" main { - if is_prefix("hello world", "hello") { + if ends_with("hello world", "world") { echo "Succeded" } } diff --git a/src/tests/stdlib/is_suffix.ab b/src/tests/stdlib/starts_with.ab similarity index 59% rename from src/tests/stdlib/is_suffix.ab rename to src/tests/stdlib/starts_with.ab index 39c34ecf..103888aa 100644 --- a/src/tests/stdlib/is_suffix.ab +++ b/src/tests/stdlib/starts_with.ab @@ -1,7 +1,7 @@ import * from "std/text" main { - if is_suffix("hello world", "world") { + if starts_with("hello world", "hello") { echo "Succeded" } }