2023-04-24 23:44:30 +00:00
|
|
|
# $NetBSD: cond-cmp-string.mk,v 1.17 2023/03/28 14:38:29 rillig Exp $
|
2020-09-05 16:11:04 +00:00
|
|
|
#
|
|
|
|
# Tests for string comparisons in .if conditions.
|
|
|
|
|
|
|
|
# This is a simple comparison of string literals.
|
|
|
|
# Nothing surprising here.
|
|
|
|
.if "str" != "str"
|
2020-11-07 19:39:21 +00:00
|
|
|
. error
|
2020-09-05 16:11:04 +00:00
|
|
|
.endif
|
|
|
|
|
|
|
|
# The right-hand side of the comparison may be written without quotes.
|
|
|
|
.if "str" != str
|
2020-11-07 19:39:21 +00:00
|
|
|
. error
|
2020-09-05 16:11:04 +00:00
|
|
|
.endif
|
|
|
|
|
|
|
|
# The left-hand side of the comparison must be enclosed in quotes.
|
|
|
|
# This one is not enclosed in quotes and thus generates an error message.
|
2023-04-24 23:44:30 +00:00
|
|
|
# expect+1: Malformed conditional (str != str)
|
2020-09-05 16:11:04 +00:00
|
|
|
.if str != str
|
2020-11-07 19:39:21 +00:00
|
|
|
. error
|
2020-09-05 16:11:04 +00:00
|
|
|
.endif
|
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
# The left-hand side of the comparison requires that any variable expression
|
|
|
|
# is defined.
|
|
|
|
#
|
|
|
|
# The variable named "" is never defined, nevertheless it can be used as a
|
|
|
|
# starting point for variable expressions. Applying the :U modifier to such
|
|
|
|
# an undefined expression turns it into a defined expression.
|
|
|
|
#
|
2021-12-18 07:27:22 +00:00
|
|
|
# See ApplyModifier_Defined and DEF_DEFINED.
|
2020-09-05 16:11:04 +00:00
|
|
|
.if ${:Ustr} != "str"
|
2020-11-07 19:39:21 +00:00
|
|
|
. error
|
2020-09-05 16:11:04 +00:00
|
|
|
.endif
|
|
|
|
|
|
|
|
# Any character in a string literal may be escaped using a backslash.
|
|
|
|
# This means that "\n" does not mean a newline but a simple "n".
|
|
|
|
.if "string" != "\s\t\r\i\n\g"
|
2020-11-07 19:39:21 +00:00
|
|
|
. error
|
2020-09-05 16:11:04 +00:00
|
|
|
.endif
|
|
|
|
|
|
|
|
# It is not possible to concatenate two string literals to form a single
|
2020-11-20 03:54:37 +00:00
|
|
|
# string. In C, Python and the shell this is possible, but not in make.
|
2023-04-24 23:44:30 +00:00
|
|
|
# expect+1: Malformed conditional ("string" != "str""ing")
|
2020-09-05 16:11:04 +00:00
|
|
|
.if "string" != "str""ing"
|
2020-11-07 19:39:21 +00:00
|
|
|
. error
|
2020-11-20 03:54:37 +00:00
|
|
|
.else
|
|
|
|
. error
|
2020-11-07 19:39:21 +00:00
|
|
|
.endif
|
|
|
|
|
|
|
|
# There is no = operator for strings.
|
2023-04-24 23:44:30 +00:00
|
|
|
# expect+1: Malformed conditional (!("value" = "value"))
|
2020-11-07 19:39:21 +00:00
|
|
|
.if !("value" = "value")
|
|
|
|
. error
|
|
|
|
.else
|
|
|
|
. error
|
|
|
|
.endif
|
|
|
|
|
|
|
|
# There is no === operator for strings either.
|
2023-04-24 23:44:30 +00:00
|
|
|
# expect+1: Malformed conditional (!("value" === "value"))
|
2020-11-07 19:39:21 +00:00
|
|
|
.if !("value" === "value")
|
|
|
|
. error
|
|
|
|
.else
|
|
|
|
. error
|
|
|
|
.endif
|
|
|
|
|
|
|
|
# A variable expression can be enclosed in double quotes.
|
|
|
|
.if ${:Uword} != "${:Uword}"
|
|
|
|
. error
|
|
|
|
.endif
|
|
|
|
|
|
|
|
# Between 2003-01-01 (maybe even earlier) and 2020-10-30, adding one of the
|
|
|
|
# characters " \t!=><" directly after a variable expression resulted in a
|
|
|
|
# "Malformed conditional", even though the string was well-formed.
|
|
|
|
.if ${:Uword } != "${:Uword} "
|
|
|
|
. error
|
|
|
|
.endif
|
|
|
|
# Some other characters worked though, and some didn't.
|
|
|
|
# Those that are mentioned in is_separator didn't work.
|
|
|
|
.if ${:Uword0} != "${:Uword}0"
|
|
|
|
. error
|
|
|
|
.endif
|
|
|
|
.if ${:Uword&} != "${:Uword}&"
|
|
|
|
. error
|
|
|
|
.endif
|
|
|
|
.if ${:Uword!} != "${:Uword}!"
|
|
|
|
. error
|
|
|
|
.endif
|
|
|
|
.if ${:Uword<} != "${:Uword}<"
|
|
|
|
. error
|
|
|
|
.endif
|
|
|
|
|
|
|
|
# Adding another variable expression to the string literal works though.
|
|
|
|
.if ${:Uword} != "${:Uwo}${:Urd}"
|
|
|
|
. error
|
|
|
|
.endif
|
|
|
|
|
|
|
|
# Adding a space at the beginning of the quoted variable expression works
|
|
|
|
# though.
|
|
|
|
.if ${:U word } != " ${:Uword} "
|
|
|
|
. error
|
2020-09-05 16:11:04 +00:00
|
|
|
.endif
|
2020-11-20 03:54:37 +00:00
|
|
|
|
|
|
|
# If at least one side of the comparison is a string literal, the string
|
|
|
|
# comparison is performed.
|
|
|
|
.if 12345 != "12345"
|
|
|
|
. error
|
|
|
|
.endif
|
|
|
|
|
|
|
|
# If at least one side of the comparison is a string literal, the string
|
|
|
|
# comparison is performed. The ".0" in the left-hand side makes the two
|
|
|
|
# sides of the equation unequal.
|
|
|
|
.if 12345.0 == "12345"
|
|
|
|
. error
|
|
|
|
.endif
|
2021-02-11 01:51:11 +00:00
|
|
|
|
|
|
|
# Strings cannot be compared relationally, only for equality.
|
2023-04-24 23:44:30 +00:00
|
|
|
# expect+1: Comparison with '<' requires both operands 'string' and 'string' to be numeric
|
2021-02-11 01:51:11 +00:00
|
|
|
.if "string" < "string"
|
|
|
|
. error
|
|
|
|
.else
|
|
|
|
. error
|
|
|
|
.endif
|
|
|
|
|
|
|
|
# Strings cannot be compared relationally, only for equality.
|
2023-04-24 23:44:30 +00:00
|
|
|
# expect+1: Comparison with '<=' requires both operands 'string' and 'string' to be numeric
|
2021-02-11 01:51:11 +00:00
|
|
|
.if "string" <= "string"
|
|
|
|
. error
|
|
|
|
.else
|
|
|
|
. error
|
|
|
|
.endif
|
|
|
|
|
|
|
|
# Strings cannot be compared relationally, only for equality.
|
2023-04-24 23:44:30 +00:00
|
|
|
# expect+1: Comparison with '>' requires both operands 'string' and 'string' to be numeric
|
2021-02-11 01:51:11 +00:00
|
|
|
.if "string" > "string"
|
|
|
|
. error
|
|
|
|
.else
|
|
|
|
. error
|
|
|
|
.endif
|
|
|
|
|
|
|
|
# Strings cannot be compared relationally, only for equality.
|
2023-04-24 23:44:30 +00:00
|
|
|
# expect+1: Comparison with '>=' requires both operands 'string' and 'string' to be numeric
|
2021-02-11 01:51:11 +00:00
|
|
|
.if "string" >= "string"
|
|
|
|
. error
|
|
|
|
.else
|
|
|
|
. error
|
|
|
|
.endif
|
2022-07-26 15:52:53 +00:00
|
|
|
|
|
|
|
# Two variables with different values compare unequal.
|
|
|
|
VAR1= value1
|
|
|
|
VAR2= value2
|
|
|
|
.if ${VAR1} != ${VAR2}
|
|
|
|
.else
|
|
|
|
. error
|
|
|
|
.endif
|