Skip to content

Commit

Permalink
Forward ported from 2.3-gae: PR #87 - Fix string comparison to avoid …
Browse files Browse the repository at this point in the history
…using the collator
  • Loading branch information
ddekany committed Dec 10, 2023
1 parent 63d7c85 commit 474c607
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,5 @@
<p>Item is: 12</p>
<p>Item is greater than two.</p>
<p>Item is greater than or equal to ten.</p>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<p>Item is greater than or equal to ten.</p>
</#if>
</#list>
<@noOutput>

<#-- Signum-based optimization test, all 9 permutations: -->
<#-- 1 -->
Expand Down Expand Up @@ -214,5 +215,26 @@
<@assert (p3 >= m3) />
<@assert !(p3 < m3) />
<@assert !(p3 <= m3) />

<#-- String comparison: -->
<#assign s = 'a'>
<@assert '' == '' />
<@assert 'a' == 'a' />
<@assert s == 'a' />
<@assert s + 'b' == 'ab' />
<@assert 'á' == 'a\x0301' />
<@assert 'a\x0301' == 'á'/>
<@assert 'a' != 'A' />
<@assert s != 'A' />
<@assert 'A' != 'a' />
<@assert '' != 'a' />
<@assert 'a' != '' />
<@assert 'ab' != 'ac' />
<@assertFails message="Can't use operator \"<\" on string values.">${s < s}</@>
<@assertFails message="Can't use operator \">\" on string values.">${s > s}</@>
<@assertFails message="Can't use operator \"<=\" on string values.">${s <= s}</@>
<@assertFails message="Can't use operator \">=\" on string values.">${(s >= s)}</@>

</@noOutput>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import java.io.IOException;
import java.io.Writer;
import java.text.Normalizer;
import java.util.Date;

import static org.apache.freemarker.core.MessageUtils.*;
Expand Down Expand Up @@ -267,8 +268,8 @@ static boolean compare(
}
String leftString = _EvalUtils.modelToString((TemplateStringModel) leftValue, leftExp);
String rightString = _EvalUtils.modelToString((TemplateStringModel) rightValue, rightExp);
// FIXME NBC: Don't use the Collator here. That's locale-specific, but ==/!= should not be.
cmpResult = env.getCollator().compare(leftString, rightString);
cmpResult = Normalizer.normalize(leftString, Normalizer.Form.NFKC)
.compareTo(Normalizer.normalize(rightString, Normalizer.Form.NFKC));
} else if (leftValue instanceof TemplateBooleanModel && rightValue instanceof TemplateBooleanModel) {
if (operator != CMP_OP_EQUALS && operator != CMP_OP_NOT_EQUALS) {
throw new TemplateException(defaultBlamed, env,
Expand Down

0 comments on commit 474c607

Please sign in to comment.