Ant JavaScript相同的字符串比较返回false,但是对于字符串中的每个字符进行字符比较会返回true?

4

我在我的project.xml文件中使用JavaScript - 通过<scriptdef>标签 - 来组合多个属性值来构建一个属性名。 我需要在脚本中比较两个字符串:一个是作为一个或多个属性值的扩展传递的,另一个是作为引号括起来的文字字符串传递的...

<ac:var name="buildVariant" unset="true"/>
<property name="buildVariant" value="Debug"/>
<unStrung propstring="${buildVariant}" altifmatch="Debug"/>

...但是脚本中的字符串比较并没有按照我的期望工作。逐字符比较相同的字符串会评估为"true"。一对否定的">" 和 "<" 比较AND评估为"true"。但是,简单地比较string1 == string2 的结果却是"false"。下面是一个简化的脚本,说明了这个问题(并展示了我尝试过的几种解决方法)...

<scriptdef name="unStrung" language="javascript">
    <attribute name="propstring"/>
    <attribute name="altifmatch"/>
<![CDATA[
var propstring = attributes.get("propstring");
var propvalue = project.getProperty(propstring);
var altifmatch = attributes.get("altifmatch");
var debugTheDebug = "Debug";

if ( altifmatch != null && propstring != null )
{
    var alen = 0;
    var plen = 0;
    alen = altifmatch.length();
    plen = propstring.length();

    print(' ');
    print('    altifmatch = [' + altifmatch + '] and propstring = [' + propstring + ']');
    print('    so naturally (altifmatch == propstring) = [' + (altifmatch == propstring) + '],');
    print('    just like ("Debug" == "Debug") = [' + ("Debug" == "Debug") + '].');
    print(' ');

    print(' ');
    print('    altifmatch.length() = [' + alen + '] and propstring.length() = [' + plen + ']');
    print('    altifmatch.substring(0,alen) = [' + altifmatch.substring(0,alen)
        + '] and propstring.substring(0,plen) = [' + altifmatch.substring(0,alen) + ']');
    print('    so naturally ( propstring.substring(0,plen) == propstring.substring(0,plen) ) = ['
                + (altifmatch.substring(0,alen) == propstring.substring(0,plen)) + '].');
    print(' ');

    for (var c=0; c<plen; c++)
    {
        print('    char['+c+']: altifmatch['+c+']="'+altifmatch.charCodeAt(c)+'";  propstring['+c+']="'+propstring.charCodeAt(c)
            +'".  So ... a == p = "' + (altifmatch.charCodeAt(c) == propstring.charCodeAt(c)) + '"');
    }

    print(' ');
    print('    typeof(altifmatch) = "' + typeof(altifmatch) + '", and typeof(propstring) = "' + typeof(propstring) + '"');
    print('    altifmatch.toString() = "' + altifmatch.toString() + '" and propstring.toString() = "' + propstring.toString() + '"');
    print('    ...oddly enough... debugTheDebug = "' + debugTheDebug + '"');
    print('       (debugTheDebug == altifmatch.toString()) = "' + (debugTheDebug == altifmatch.toString()) + '"');
    print('       (debugTheDebug == propstring.toString()) = "' + (debugTheDebug == propstring.toString()) + '"');
    print('    ...and still... (altifmatch.toString() == propstring.toString()) = "' + (altifmatch.toString() == propstring.toString()) + '"');

    print(' ');
    print('       (debugTheDebug == altifmatch) = "' + (debugTheDebug == altifmatch) + '"');
    print('       (debugTheDebug == propstring) = "' + (debugTheDebug == propstring) + '"');
    print('    ...and still... (altifmatch == propstring) = "' + (altifmatch == propstring) + '"');
    print('       (altifmatch < propstring) = "' + (altifmatch < propstring) + '"');
    print('       (altifmatch > propstring) = "' + (altifmatch > propstring) + '"');
    print('          (!(altifmatch < propstring) && !(altifmatch > propstring)) = "'
            + (!(altifmatch < propstring) && !(altifmatch > propstring)) + '"');
    print('    ...and of course... ( (debugTheDebug == altifmatch) && (debugTheDebug == propstring) ) = "'
            + ( (debugTheDebug == altifmatch) && (debugTheDebug == propstring) ) + '"');

    print(' ');
}
]]>
</scriptdef>

生成的输出如下所示:
altifmatch = [Debug] and propstring = [Debug]
so naturally (altifmatch == propstring) = [false],
just like ("Debug" == "Debug") = [true].


altifmatch.length() = [5] and propstring.length() = [5]
altifmatch.substring(0,alen) = [Debug] and propstring.substring(0,plen) = [Debug]
so naturally ( propstring.substring(0,plen) == propstring.substring(0,plen) ) = [false].

char[0]: altifmatch[0]="68";  propstring[0]="68".  So ... a == p = "true"
char[1]: altifmatch[1]="101";  propstring[1]="101".  So ... a == p = "true"
char[2]: altifmatch[2]="98";  propstring[2]="98".  So ... a == p = "true"
char[3]: altifmatch[3]="117";  propstring[3]="117".  So ... a == p = "true"
char[4]: altifmatch[4]="103";  propstring[4]="103".  So ... a == p = "true"

typeof(altifmatch) = "object", and typeof(propstring) = "object"
altifmatch.toString() = "Debug" and propstring.toString() = "Debug"
...oddly enough... debugTheDebug = "Debug"
   (debugTheDebug == altifmatch.toString()) = "true"
   (debugTheDebug == propstring.toString()) = "true"
...and still... (altifmatch.toString() == propstring.toString()) = "false"

   (debugTheDebug == altifmatch) = "true"
   (debugTheDebug == propstring) = "true"
...and still... (altifmatch == propstring) = "false"
   (altifmatch < propstring) = "false"
   (altifmatch > propstring) = "false"
      (!(altifmatch < propstring) && !(altifmatch > propstring)) = "true"
...and of course... ( (debugTheDebug == altifmatch) && (debugTheDebug == propstring) ) = "true"

我猜测这可能是我忽略了某些简单或愚蠢的东西(我对Ant和JavaScript的经验不是很丰富)。
你有什么想法吗?

参见:相关和有趣的内容,但并非我问题的直接答案……为什么JavaScript有两种字符串? - Kenigmatic
一个朋友刚刚指向了我一个更近但类似的值得关注的案例! https://dev59.com/HFYM5IYBdhLWcg3w3TCF - Kenigmatic
1个回答

1

那个完美地解决了问题!但我不明白的是,为什么我在大量搜索中没有找到那个选项……(好吧,也许是因为我对JavaScript不是很熟悉),但我发现很多结果都说 == 应该可以正常工作(只要没有尾随空格或隐藏字符)。谢谢!另外,我想给你的答案点赞,但我还没有足够的声望! - Kenigmatic
我承认我不是Ant专家,但据我所知,Ant是一个Java构建工具。即使您正在使用<scriptdef>设置/指令,据我所知,它只是调用一些内在机制来“将脚本处理为Java适当的方式”,这意味着,再次根据我的理解,Ant Java引擎“使Java从脚本指令中工作”--- TL:DR / Cool Story Bro - 基本上,“==”比较将在纯JavaScript中滑动,但您正在要求ant从js构建Java指令。因此,“==”由于该比较的性质而失败-很高兴它起作用并且能够帮助到您! - Brandt Solovij
我认为一些东西一定是坏了,因为许多==的实例工作正常,而那些不正常的实例似乎是不合理的,特别是如果与“正常”的情况混合使用。 (a==b)=true, (a==c)=true, (b==c)=false??? . . . 和 . . . (a>b)=false, (a<b)=false, (!(a>b) && !(a<b))=true, (a==b)=false??? 每次看到它,我就会出现奇怪的面部抽搐和紧张的笑声。 - Kenigmatic
==与.equals()的工作方式有些不同我必须承认,我不是一个如此专家,无法列出两者之间的差异 - 但可以在实践中证明这些差异。 - Brandt Solovij
如果(“different”==“broken”)JS_AiNT_JAVA = true;;-) - Kenigmatic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接