From 8592b24528b2241d57dd52034621ec2187858682 Mon Sep 17 00:00:00 2001 From: Takahiro Ueda Date: Sun, 7 Jan 2024 10:16:52 +0900 Subject: [PATCH] feat(macro): add uppercase/lowercase/sanitize --- Makefile | 15 +++++++++++++++ tests/macro/latex.mk | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/Makefile b/Makefile index 2582646..e786019 100644 --- a/Makefile +++ b/Makefile @@ -417,6 +417,9 @@ assert_true = \ assert_false = \ $(if $(strip $1),false,:) +assert_eq = \ + $(if $(and $(findstring $1,$2),$(findstring $2,$1)),echo '$1==$2';:,echo '$1!=$2';false) + assert_success = \ $(strip $1) @@ -448,6 +451,18 @@ has_gnu_grep_impl = $(strip \ rule_exists = \ $(shell $(MAKE) -n $1 >/dev/null 2>&1 && echo 1) +# $(call uppercase,STR) converts STR to uppercase. +uppercase = \ + $(shell echo '$1' | tr a-z A-Z) + +# $(call lowercase,STR) converts STR to lowercase. +lowercase = \ + $(shell echo '$1' | tr A-Z a-z) + +# $(call sanitize,STR) replaces special characters in STR with underscores. +sanitize = \ + $(shell echo '$1' | sed 's/[^a-zA-Z0-9]/_/g') + # $(init_toolchain) initializes the toolchain. init_toolchain = $(call cache,init_toolchain_impl) diff --git a/tests/macro/latex.mk b/tests/macro/latex.mk index bf2e805..fa49eff 100644 --- a/tests/macro/latex.mk +++ b/tests/macro/latex.mk @@ -36,3 +36,12 @@ test_mv_target: $(call assert_success,$(call mv_target,1.tmp)) $(call assert_fail, $(call mv_target,2.tmp)) $(call assert_success,$(call mv_target,3.tmp,false)) + +test_uppercase: + $(call assert_eq,$(call uppercase,abcxyzABCXYZ0189 !"#%&*+-./x:;<=>?@x[]^_`x{|}~x y),ABCXYZABCXYZ0189 !"#%&*+-./X:;<=>?@X[]^_`X{|}~X Y) + +test_lowercase: + $(call assert_eq,$(call lowercase,abcxyzABCXYZ0189 !"#%&*+-./X:;<=>?@X[]^_`X{|}~X Y),abcxyzabcxyz0189 !"#%&*+-./x:;<=>?@x[]^_`x{|}~x y) + +test_sanitize: + $(call assert_eq,$(call sanitize,abcxyzABCXYZ0189 !"#%&*+-./x:;<=>?@x[]^_`x{|}~x y),abcxyzABCXYZ0189____________x_______x_____x____x__y)