From 0d1bc6cf7483d2a53c2d5f04d15a0ce3da85f29b Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Thu, 30 Oct 2025 12:00:43 -0400 Subject: str: Add to_upper() + tests Signed-off-by: Andrew Opalach --- include/al/str.h | 8 ++++++++ tests/str.c | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/al/str.h b/include/al/str.h index 6640b0f..cb7c98f 100644 --- a/include/al/str.h +++ b/include/al/str.h @@ -70,6 +70,13 @@ static inline void al_str_to_lower(str *s) } } +static inline void al_str_to_upper(str *s) +{ + for (u32 i = 0; i < s->length; i++) { + al_str_at(s, i) = al_toupper(al_str_at(s, i)); + } +} + // Returned c_str must be free'd by the user. static inline char *al_str_to_c_str(str *s) { @@ -167,6 +174,7 @@ static inline bool al_str_tok(str *result, char delim) return true; } +// djb2 hash. static inline u32 al_str_hash(str *s) { u32 hash = 5381; diff --git a/tests/str.c b/tests/str.c index e961ee9..c783673 100644 --- a/tests/str.c +++ b/tests/str.c @@ -310,6 +310,28 @@ static bool str_test_to_long(void) AL_TEST_END(); } +static bool str_test_to_lower_upper(void) +{ + AL_TEST_START("str_to_lower_upper"); + + str string0; + al_str_from(&string0, "Woopty-dOo"); + al_str_to_lower(&string0); + AL_TEST_TRUE(al_str_eq(&string0, &al_str_c("woopty-doo"))); + + al_str_to_upper(&string0); + AL_TEST_TRUE(al_str_eq(&string0, &al_str_c("WOOPTY-DOO"))); + + str string1; + al_str_from(&string1, "么造就美好生活? 最长幸福研究的AAAaaaA经验告fff诉你娜塔莉·波特曼演讲"); + al_str_to_lower(&string1); + AL_TEST_TRUE(al_str_eq(&string1, &al_str_c("么造就美好生活? 最长幸福研究的aaaaaaa经验告fff诉你娜塔莉·波特曼演讲"))); + al_str_to_upper(&string1); + AL_TEST_TRUE(al_str_eq(&string1, &al_str_c("么造就美好生活? 最长幸福研究的AAAAAAA经验告FFF诉你娜塔莉·波特曼演讲"))); + + AL_TEST_END(); +} + // Very non-exhaustive. static bool str_test_limits(void) { @@ -424,6 +446,7 @@ bool str_tests_run(u32 skip_mask) AL_TEST_RUN(str_test_replace_and_remove); AL_TEST_RUN(str_test_get_line); AL_TEST_RUN(str_test_to_long); + AL_TEST_RUN(str_test_to_lower_upper); if (skip_mask & AL_TESTS_RUN_LIMITS) { AL_TEST_RUN(str_test_limits); } -- cgit v1.2.3-101-g0448