ipv4_compare()ipv4_compare()

比较两个 IPv4 字符串。Compares two IPv4 strings. 分析并比较两个 IPv4 字符串,同时考虑根据参数前缀和可选的 PrefixMask 参数计算出的组合 IP 前缀掩码。The two IPv4 strings are parsed and compared while accounting for the combined IP-prefix mask calculated from argument prefixes, and the optional PrefixMask argument.

ipv4_compare("127.0.0.1", "127.0.0.1") == 0
ipv4_compare('192.168.1.1', '192.168.1.255') < 0
ipv4_compare('192.168.1.1/24', '192.168.1.255/24') == 0
ipv4_compare('192.168.1.1', '192.168.1.255', 24) == 0

语法Syntax

ipv4_compare(Expr1, Expr2[ ,PrefixMask])ipv4_compare(Expr1, Expr2[ ,PrefixMask])

参数Arguments

  • Expr1、Expr2:表示 IPv4 地址的字符串表达式。Expr1, Expr2: A string expression representing an IPv4 address. 可以使用 IP 前缀表示法对 IPv4 字符串进行掩码操作。IPv4 strings can be masked using IP-prefix notation.
  • PrefixMask:从 0 到 32 的整数,表示所考虑的最有效位的数目。PrefixMask: An integer from 0 to 32 representing the number of most-significant bits that are taken into account.

IP 前缀表示法IP-prefix notation

IP 地址可以通过斜杠 (/) 字符使用 IP-prefix notation 进行定义。IP addresses can be defined with IP-prefix notation using a slash (/) character. 斜杠 (/) 左边的 IP 地址是基本 IP 地址。The IP address to the LEFT of the slash (/) is the base IP address. 斜杠 (/) 右边的数字(1 到 32)是网络掩码中连续位的数目。The number (1 to 32) to the RIGHT of the slash (/) is the number of contiguous 1 bit in the netmask.

示例: 192.168.2.0/24 将具有关联的网络/子网掩码,其中包含 24 个连续位或点分十进制格式的 255.255.255.0。Example: 192.168.2.0/24 will have an associated net/subnetmask containing 24 contiguous bits or 255.255.255.0 in dotted decimal format.

返回Returns

  • 0:如果第一个 IPv4 字符串参数的长表示形式等于第二个 IPv4 字符串参数0: If the long representation of the first IPv4 string argument is equal to the second IPv4 string argument
  • 1:如果第一个 IPv4 字符串参数的长表示形式大于第二个 IPv4 字符串参数1: If the long representation of the first IPv4 string argument is greater than the second IPv4 string argument
  • -1:如果第一个 IPv4 字符串参数的长表示形式小于第二个 IPv4 字符串参数-1: If the long representation of the first IPv4 string argument is less than the second IPv4 string argument
  • null:如果两个 IPv4 字符串之一转换不成功。null: If conversion for one of the two IPv4 strings wasn't successful.

示例:IPv4 比较相等的情况Examples: IPv4 comparison equality cases

使用 IPv4 字符串中指定的 IP 前缀表示法比较 IPCompare IPs using the IP-prefix notation specified inside the IPv4 strings

datatable(ip1_string:string, ip2_string:string)
[
 '192.168.1.0',    '192.168.1.0',       // Equal IPs
 '192.168.1.1/24', '192.168.1.255',     // 24 bit IP-prefix is used for comparison
 '192.168.1.1',    '192.168.1.255/24',  // 24 bit IP-prefix is used for comparison
 '192.168.1.1/30', '192.168.1.255/24',  // 24 bit IP-prefix is used for comparison
]
| extend result = ipv4_compare(ip1_string, ip2_string)
ip1_stringip1_string ip2_stringip2_string resultresult
192.168.1.0192.168.1.0 192.168.1.0192.168.1.0 00
192.168.1.1/24192.168.1.1/24 192.168.1.255192.168.1.255 00
192.168.1.1192.168.1.1 192.168.1.255/24192.168.1.255/24 00
192.168.1.1/30192.168.1.1/30 192.168.1.255/24192.168.1.255/24 00

使用 IPv4 字符串中指定的 IP 前缀表示法作为 ipv4_compare() 函数的附加参数比较 IPCompare IPs using IP-prefix notation specified inside the IPv4 strings and as additional argument of the ipv4_compare() function

datatable(ip1_string:string, ip2_string:string, prefix:long)
[
 '192.168.1.1',    '192.168.1.0',   31, // 31 bit IP-prefix is used for comparison
 '192.168.1.1/24', '192.168.1.255', 31, // 24 bit IP-prefix is used for comparison
 '192.168.1.1',    '192.168.1.255', 24, // 24 bit IP-prefix is used for comparison
]
| extend result = ipv4_compare(ip1_string, ip2_string, prefix)
ip1_stringip1_string ip2_stringip2_string 前缀prefix resultresult
192.168.1.1192.168.1.1 192.168.1.0192.168.1.0 3131 00
192.168.1.1/24192.168.1.1/24 192.168.1.255192.168.1.255 3131 00
192.168.1.1192.168.1.1 192.168.1.255192.168.1.255 2424 00