|
| 1 | +USAGE := Usage: please provide a string |
| 2 | + |
| 3 | +# Letter constants |
| 4 | +LOWERCASE_LETTERS := a b c d e f g h i j k l m n o p q r s t u v w x y z |
| 5 | +UPPERCASE_LETTERS := A B C D E F G H I J K L M N O P Q R S T U V W X Y Z |
| 6 | +LETTER_INDEXES := 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
| 7 | + |
| 8 | +# Space replacement constants |
| 9 | +EMPTY := |
| 10 | +SPACE := $(EMPTY) $(EMPTY) |
| 11 | +SPACE_REPLACEMENT := « |
| 12 | + |
| 13 | +# Get rest of words in a list |
| 14 | +# Arg 1: The list |
| 15 | +# Return: List with first word removed |
| 16 | +REST = $(wordlist 2,$(words $(1)),$(1)) |
| 17 | + |
| 18 | +# Repeatedly apply a function |
| 19 | +# Arg 1: Name of function. This function must take two arguments: |
| 20 | +# - Function Arg1: Current value |
| 21 | +# - Function Arg2: New value |
| 22 | +# Arg 2: The list to repeated apply the function to |
| 23 | +# Arg 3: Initial value |
| 24 | +_REDUCE = $(if $(4),$(call _REDUCE,$(1),$(call $(1),$(2),$(3)),$(firstword $(4)),$(call REST,$(4))),$(call $(1),$(2),$(3))) |
| 25 | +REDUCE = $(call _REDUCE,$(1),$(3),$(firstword $(2)),$(call REST,$(2))) |
| 26 | + |
| 27 | +# Replace spaces in a string with a replacement character to prevent it from being treated |
| 28 | +# as a separate word |
| 29 | +# Arg 1: The string |
| 30 | +# Return: The string with space replaced |
| 31 | +REPLACE_SPACE = $(subst $(SPACE),$(SPACE_REPLACEMENT),$(1)) |
| 32 | + |
| 33 | +# Restore spaces in a string |
| 34 | +# Arg 1: The string with space replacements |
| 35 | +# Return: The string with space replacements converted to spaces |
| 36 | +RESTORE_SPACE = $(subst $(SPACE_REPLACEMENT),$(SPACE),$(1)) |
| 37 | + |
| 38 | +# Capitalize the first letter of a string |
| 39 | +# Arg 1: The string |
| 40 | +# Return First letter of string capitalized if it is a lowercase letter. Otherwise, |
| 41 | +# string is unchanged |
| 42 | +_CAPITALIZE_LETTER = $(patsubst $(word $(2),$(LOWERCASE_LETTERS))%,$(word $(2),$(UPPERCASE_LETTERS))%,$(1)) |
| 43 | +CAPITALIZE = $(call RESTORE_SPACE,$(call REDUCE,_CAPITALIZE_LETTER,$(LETTER_INDEXES),$(call REPLACE_SPACE,$(1)))) |
| 44 | + |
| 45 | +# If arguments, display usage statement |
| 46 | +# Else display capitalized string |
| 47 | +ifeq (,$(ARGV1)) |
| 48 | +$(info $(USAGE)) |
| 49 | +else |
| 50 | +$(info $(call CAPITALIZE,$(ARGV1))) |
| 51 | +endif |
| 52 | + |
| 53 | +.PHONY: |
| 54 | +all: ;@: |
0 commit comments