You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2023/12/10 15:01:20 UTC

(freemarker) branch 3 updated: Forward ported from 2.3-gae: PR #87 - Fix string comparison to avoid using the collator

This is an automated email from the ASF dual-hosted git repository.

ddekany pushed a commit to branch 3
in repository https://gitbox.apache.org/repos/asf/freemarker.git


The following commit(s) were added to refs/heads/3 by this push:
     new 474c6077 Forward ported from 2.3-gae: PR #87 - Fix string comparison to avoid using the collator
474c6077 is described below

commit 474c6077d0192cea32315209e5dac2250037c714
Author: ddekany <dd...@apache.org>
AuthorDate: Sun Dec 10 16:01:01 2023 +0100

    Forward ported from 2.3-gae: PR #87 - Fix string comparison to avoid using the collator
---
 .../core/templatesuite/expected/comparisons.txt    |  1 -
 .../core/templatesuite/templates/comparisons.f3ac  | 22 ++++++++++++++++++++++
 .../org/apache/freemarker/core/_EvalUtils.java     |  5 +++--
 3 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/freemarker-core-test/src/test/resources/org/apache/freemarker/core/templatesuite/expected/comparisons.txt b/freemarker-core-test/src/test/resources/org/apache/freemarker/core/templatesuite/expected/comparisons.txt
index 8d9d372d..149fc957 100644
--- a/freemarker-core-test/src/test/resources/org/apache/freemarker/core/templatesuite/expected/comparisons.txt
+++ b/freemarker-core-test/src/test/resources/org/apache/freemarker/core/templatesuite/expected/comparisons.txt
@@ -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>
diff --git a/freemarker-core-test/src/test/resources/org/apache/freemarker/core/templatesuite/templates/comparisons.f3ac b/freemarker-core-test/src/test/resources/org/apache/freemarker/core/templatesuite/templates/comparisons.f3ac
index 33147658..bb178359 100644
--- a/freemarker-core-test/src/test/resources/org/apache/freemarker/core/templatesuite/templates/comparisons.f3ac
+++ b/freemarker-core-test/src/test/resources/org/apache/freemarker/core/templatesuite/templates/comparisons.f3ac
@@ -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 -->
@@ -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>
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/_EvalUtils.java b/freemarker-core/src/main/java/org/apache/freemarker/core/_EvalUtils.java
index 2d01bf50..a7ef63d4 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/_EvalUtils.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/_EvalUtils.java
@@ -33,6 +33,7 @@ import org.apache.freemarker.core.valueformat.TemplateValueFormatException;
 
 import java.io.IOException;
 import java.io.Writer;
+import java.text.Normalizer;
 import java.util.Date;
 
 import static org.apache.freemarker.core.MessageUtils.*;
@@ -267,8 +268,8 @@ public class _EvalUtils {
             }
             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,