1*e2eeea75SSimon J. Gerraty# $NetBSD: cond-cmp-string.mk,v 1.13 2020/11/15 14:07:53 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 22*e2eeea75SSimon J. Gerraty# The left-hand side of the comparison requires that any variable expression 23*e2eeea75SSimon J. Gerraty# is defined. 24*e2eeea75SSimon J. Gerraty# 25*e2eeea75SSimon J. Gerraty# The variable named "" is never defined, nevertheless it can be used as a 26*e2eeea75SSimon J. Gerraty# starting point for variable expressions. Applying the :U modifier to such 27*e2eeea75SSimon J. Gerraty# an undefined expression turns it into a defined expression. 28*e2eeea75SSimon J. Gerraty# 29*e2eeea75SSimon 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 41*e2eeea75SSimon 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 44*e2eeea75SSimon J. Gerraty.else 45*e2eeea75SSimon 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 98*e2eeea75SSimon J. Gerraty 99*e2eeea75SSimon J. Gerraty# If at least one side of the comparison is a string literal, the string 100*e2eeea75SSimon J. Gerraty# comparison is performed. 101*e2eeea75SSimon J. Gerraty.if 12345 != "12345" 102*e2eeea75SSimon J. Gerraty. error 103*e2eeea75SSimon J. Gerraty.endif 104*e2eeea75SSimon J. Gerraty 105*e2eeea75SSimon J. Gerraty# If at least one side of the comparison is a string literal, the string 106*e2eeea75SSimon J. Gerraty# comparison is performed. The ".0" in the left-hand side makes the two 107*e2eeea75SSimon J. Gerraty# sides of the equation unequal. 108*e2eeea75SSimon J. Gerraty.if 12345.0 == "12345" 109*e2eeea75SSimon J. Gerraty. error 110*e2eeea75SSimon J. Gerraty.endif 111