1*956e45f6SSimon J. Gerraty# $NetBSD: cond-cmp-string.mk,v 1.11 2020/10/30 14:53:31 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 222c3632d1SSimon J. Gerraty# The left-hand side of the comparison requires a defined variable. 232c3632d1SSimon J. Gerraty# The variable named "" is not defined, but applying the :U modifier to it 242c3632d1SSimon J. Gerraty# makes it "kind of defined" (see VAR_KEEP). Therefore it is ok here. 252c3632d1SSimon J. Gerraty.if ${:Ustr} != "str" 262c3632d1SSimon J. Gerraty. error 272c3632d1SSimon J. Gerraty.endif 282c3632d1SSimon J. Gerraty 292c3632d1SSimon J. Gerraty# Any character in a string literal may be escaped using a backslash. 302c3632d1SSimon J. Gerraty# This means that "\n" does not mean a newline but a simple "n". 312c3632d1SSimon J. Gerraty.if "string" != "\s\t\r\i\n\g" 322c3632d1SSimon J. Gerraty. error 332c3632d1SSimon J. Gerraty.endif 342c3632d1SSimon J. Gerraty 352c3632d1SSimon J. Gerraty# It is not possible to concatenate two string literals to form a single 362c3632d1SSimon J. Gerraty# string. 372c3632d1SSimon J. Gerraty.if "string" != "str""ing" 382c3632d1SSimon J. Gerraty. error 392c3632d1SSimon J. Gerraty.endif 40*956e45f6SSimon J. Gerraty 41*956e45f6SSimon J. Gerraty# There is no = operator for strings. 42*956e45f6SSimon J. Gerraty.if !("value" = "value") 43*956e45f6SSimon J. Gerraty. error 44*956e45f6SSimon J. Gerraty.else 45*956e45f6SSimon J. Gerraty. error 46*956e45f6SSimon J. Gerraty.endif 47*956e45f6SSimon J. Gerraty 48*956e45f6SSimon J. Gerraty# There is no === operator for strings either. 49*956e45f6SSimon J. Gerraty.if !("value" === "value") 50*956e45f6SSimon J. Gerraty. error 51*956e45f6SSimon J. Gerraty.else 52*956e45f6SSimon J. Gerraty. error 53*956e45f6SSimon J. Gerraty.endif 54*956e45f6SSimon J. Gerraty 55*956e45f6SSimon J. Gerraty# A variable expression can be enclosed in double quotes. 56*956e45f6SSimon J. Gerraty.if ${:Uword} != "${:Uword}" 57*956e45f6SSimon J. Gerraty. error 58*956e45f6SSimon J. Gerraty.endif 59*956e45f6SSimon J. Gerraty 60*956e45f6SSimon J. Gerraty# Between 2003-01-01 (maybe even earlier) and 2020-10-30, adding one of the 61*956e45f6SSimon J. Gerraty# characters " \t!=><" directly after a variable expression resulted in a 62*956e45f6SSimon J. Gerraty# "Malformed conditional", even though the string was well-formed. 63*956e45f6SSimon J. Gerraty.if ${:Uword } != "${:Uword} " 64*956e45f6SSimon J. Gerraty. error 65*956e45f6SSimon J. Gerraty.endif 66*956e45f6SSimon J. Gerraty# Some other characters worked though, and some didn't. 67*956e45f6SSimon J. Gerraty# Those that are mentioned in is_separator didn't work. 68*956e45f6SSimon J. Gerraty.if ${:Uword0} != "${:Uword}0" 69*956e45f6SSimon J. Gerraty. error 70*956e45f6SSimon J. Gerraty.endif 71*956e45f6SSimon J. Gerraty.if ${:Uword&} != "${:Uword}&" 72*956e45f6SSimon J. Gerraty. error 73*956e45f6SSimon J. Gerraty.endif 74*956e45f6SSimon J. Gerraty.if ${:Uword!} != "${:Uword}!" 75*956e45f6SSimon J. Gerraty. error 76*956e45f6SSimon J. Gerraty.endif 77*956e45f6SSimon J. Gerraty.if ${:Uword<} != "${:Uword}<" 78*956e45f6SSimon J. Gerraty. error 79*956e45f6SSimon J. Gerraty.endif 80*956e45f6SSimon J. Gerraty 81*956e45f6SSimon J. Gerraty# Adding another variable expression to the string literal works though. 82*956e45f6SSimon J. Gerraty.if ${:Uword} != "${:Uwo}${:Urd}" 83*956e45f6SSimon J. Gerraty. error 84*956e45f6SSimon J. Gerraty.endif 85*956e45f6SSimon J. Gerraty 86*956e45f6SSimon J. Gerraty# Adding a space at the beginning of the quoted variable expression works 87*956e45f6SSimon J. Gerraty# though. 88*956e45f6SSimon J. Gerraty.if ${:U word } != " ${:Uword} " 89*956e45f6SSimon J. Gerraty. error 90*956e45f6SSimon J. Gerraty.endif 91