1*dba7b0efSSimon J. Gerraty# $NetBSD: cond-cmp-string.mk,v 1.14 2021/01/19 19:54:57 rillig Exp $ 22c3632d1SSimon J. Gerraty# 32c3632d1SSimon J. Gerraty# Tests for string comparisons in .if conditions. 42c3632d1SSimon J. Gerraty 52c3632d1SSimon J. Gerraty# This is a simple comparison of string literals. 62c3632d1SSimon J. Gerraty# Nothing surprising here. 72c3632d1SSimon J. Gerraty.if "str" != "str" 82c3632d1SSimon J. Gerraty. error 92c3632d1SSimon J. Gerraty.endif 102c3632d1SSimon J. Gerraty 112c3632d1SSimon J. Gerraty# The right-hand side of the comparison may be written without quotes. 122c3632d1SSimon J. Gerraty.if "str" != str 132c3632d1SSimon J. Gerraty. error 142c3632d1SSimon J. Gerraty.endif 152c3632d1SSimon J. Gerraty 162c3632d1SSimon J. Gerraty# The left-hand side of the comparison must be enclosed in quotes. 172c3632d1SSimon J. Gerraty# This one is not enclosed in quotes and thus generates an error message. 182c3632d1SSimon J. Gerraty.if str != str 192c3632d1SSimon J. Gerraty. error 202c3632d1SSimon J. Gerraty.endif 212c3632d1SSimon J. Gerraty 22e2eeea75SSimon J. Gerraty# The left-hand side of the comparison requires that any variable expression 23e2eeea75SSimon J. Gerraty# is defined. 24e2eeea75SSimon J. Gerraty# 25e2eeea75SSimon J. Gerraty# The variable named "" is never defined, nevertheless it can be used as a 26e2eeea75SSimon J. Gerraty# starting point for variable expressions. Applying the :U modifier to such 27e2eeea75SSimon J. Gerraty# an undefined expression turns it into a defined expression. 28e2eeea75SSimon J. Gerraty# 29e2eeea75SSimon J. Gerraty# See ApplyModifier_Defined and VEF_DEF. 302c3632d1SSimon J. Gerraty.if ${:Ustr} != "str" 312c3632d1SSimon J. Gerraty. error 322c3632d1SSimon J. Gerraty.endif 332c3632d1SSimon J. Gerraty 342c3632d1SSimon J. Gerraty# Any character in a string literal may be escaped using a backslash. 352c3632d1SSimon J. Gerraty# This means that "\n" does not mean a newline but a simple "n". 362c3632d1SSimon J. Gerraty.if "string" != "\s\t\r\i\n\g" 372c3632d1SSimon J. Gerraty. error 382c3632d1SSimon J. Gerraty.endif 392c3632d1SSimon J. Gerraty 402c3632d1SSimon J. Gerraty# It is not possible to concatenate two string literals to form a single 41e2eeea75SSimon J. Gerraty# string. In C, Python and the shell this is possible, but not in make. 422c3632d1SSimon J. Gerraty.if "string" != "str""ing" 432c3632d1SSimon J. Gerraty. error 44e2eeea75SSimon J. Gerraty.else 45e2eeea75SSimon J. Gerraty. error 462c3632d1SSimon J. Gerraty.endif 47956e45f6SSimon J. Gerraty 48956e45f6SSimon J. Gerraty# There is no = operator for strings. 49956e45f6SSimon J. Gerraty.if !("value" = "value") 50956e45f6SSimon J. Gerraty. error 51956e45f6SSimon J. Gerraty.else 52956e45f6SSimon J. Gerraty. error 53956e45f6SSimon J. Gerraty.endif 54956e45f6SSimon J. Gerraty 55956e45f6SSimon J. Gerraty# There is no === operator for strings either. 56956e45f6SSimon J. Gerraty.if !("value" === "value") 57956e45f6SSimon J. Gerraty. error 58956e45f6SSimon J. Gerraty.else 59956e45f6SSimon J. Gerraty. error 60956e45f6SSimon J. Gerraty.endif 61956e45f6SSimon J. Gerraty 62956e45f6SSimon J. Gerraty# A variable expression can be enclosed in double quotes. 63956e45f6SSimon J. Gerraty.if ${:Uword} != "${:Uword}" 64956e45f6SSimon J. Gerraty. error 65956e45f6SSimon J. Gerraty.endif 66956e45f6SSimon J. Gerraty 67956e45f6SSimon J. Gerraty# Between 2003-01-01 (maybe even earlier) and 2020-10-30, adding one of the 68956e45f6SSimon J. Gerraty# characters " \t!=><" directly after a variable expression resulted in a 69956e45f6SSimon J. Gerraty# "Malformed conditional", even though the string was well-formed. 70956e45f6SSimon J. Gerraty.if ${:Uword } != "${:Uword} " 71956e45f6SSimon J. Gerraty. error 72956e45f6SSimon J. Gerraty.endif 73956e45f6SSimon J. Gerraty# Some other characters worked though, and some didn't. 74956e45f6SSimon J. Gerraty# Those that are mentioned in is_separator didn't work. 75956e45f6SSimon J. Gerraty.if ${:Uword0} != "${:Uword}0" 76956e45f6SSimon J. Gerraty. error 77956e45f6SSimon J. Gerraty.endif 78956e45f6SSimon J. Gerraty.if ${:Uword&} != "${:Uword}&" 79956e45f6SSimon J. Gerraty. error 80956e45f6SSimon J. Gerraty.endif 81956e45f6SSimon J. Gerraty.if ${:Uword!} != "${:Uword}!" 82956e45f6SSimon J. Gerraty. error 83956e45f6SSimon J. Gerraty.endif 84956e45f6SSimon J. Gerraty.if ${:Uword<} != "${:Uword}<" 85956e45f6SSimon J. Gerraty. error 86956e45f6SSimon J. Gerraty.endif 87956e45f6SSimon J. Gerraty 88956e45f6SSimon J. Gerraty# Adding another variable expression to the string literal works though. 89956e45f6SSimon J. Gerraty.if ${:Uword} != "${:Uwo}${:Urd}" 90956e45f6SSimon J. Gerraty. error 91956e45f6SSimon J. Gerraty.endif 92956e45f6SSimon J. Gerraty 93956e45f6SSimon J. Gerraty# Adding a space at the beginning of the quoted variable expression works 94956e45f6SSimon J. Gerraty# though. 95956e45f6SSimon J. Gerraty.if ${:U word } != " ${:Uword} " 96956e45f6SSimon J. Gerraty. error 97956e45f6SSimon J. Gerraty.endif 98e2eeea75SSimon J. Gerraty 99e2eeea75SSimon J. Gerraty# If at least one side of the comparison is a string literal, the string 100e2eeea75SSimon J. Gerraty# comparison is performed. 101e2eeea75SSimon J. Gerraty.if 12345 != "12345" 102e2eeea75SSimon J. Gerraty. error 103e2eeea75SSimon J. Gerraty.endif 104e2eeea75SSimon J. Gerraty 105e2eeea75SSimon J. Gerraty# If at least one side of the comparison is a string literal, the string 106e2eeea75SSimon J. Gerraty# comparison is performed. The ".0" in the left-hand side makes the two 107e2eeea75SSimon J. Gerraty# sides of the equation unequal. 108e2eeea75SSimon J. Gerraty.if 12345.0 == "12345" 109e2eeea75SSimon J. Gerraty. error 110e2eeea75SSimon J. Gerraty.endif 111*dba7b0efSSimon J. Gerraty 112*dba7b0efSSimon J. Gerraty# Strings cannot be compared relationally, only for equality. 113*dba7b0efSSimon J. Gerraty.if "string" < "string" 114*dba7b0efSSimon J. Gerraty. error 115*dba7b0efSSimon J. Gerraty.else 116*dba7b0efSSimon J. Gerraty. error 117*dba7b0efSSimon J. Gerraty.endif 118*dba7b0efSSimon J. Gerraty 119*dba7b0efSSimon J. Gerraty# Strings cannot be compared relationally, only for equality. 120*dba7b0efSSimon J. Gerraty.if "string" <= "string" 121*dba7b0efSSimon J. Gerraty. error 122*dba7b0efSSimon J. Gerraty.else 123*dba7b0efSSimon J. Gerraty. error 124*dba7b0efSSimon J. Gerraty.endif 125*dba7b0efSSimon J. Gerraty 126*dba7b0efSSimon J. Gerraty# Strings cannot be compared relationally, only for equality. 127*dba7b0efSSimon J. Gerraty.if "string" > "string" 128*dba7b0efSSimon J. Gerraty. error 129*dba7b0efSSimon J. Gerraty.else 130*dba7b0efSSimon J. Gerraty. error 131*dba7b0efSSimon J. Gerraty.endif 132*dba7b0efSSimon J. Gerraty 133*dba7b0efSSimon J. Gerraty# Strings cannot be compared relationally, only for equality. 134*dba7b0efSSimon J. Gerraty.if "string" >= "string" 135*dba7b0efSSimon J. Gerraty. error 136*dba7b0efSSimon J. Gerraty.else 137*dba7b0efSSimon J. Gerraty. error 138*dba7b0efSSimon J. Gerraty.endif 139