You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/09/19 10:37:48 UTC

[groovy] branch danielsun/tweak-compare-bigdecimal updated: Trivial tweak for comparing `CharSequence` instances

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

sunlan pushed a commit to branch danielsun/tweak-compare-bigdecimal
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/danielsun/tweak-compare-bigdecimal by this push:
     new c4bce18  Trivial tweak for comparing `CharSequence` instances
c4bce18 is described below

commit c4bce18482b32bb4811c29f43060f32ff84a31b6
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Sep 19 18:36:27 2021 +0800

    Trivial tweak for comparing `CharSequence` instances
---
 .../org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java  | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java b/src/main/java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java
index 02bfbf2..7bc3dae 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java
@@ -728,8 +728,10 @@ public class ScriptBytecodeAdapter {
 
     public static boolean compareEqual(Object left, Object right) {
         if (left==right) return true;
+
         Class<?> leftClass = left==null?null:left.getClass();
         Class<?> rightClass = right==null?null:right.getClass();
+
         if (leftClass ==Integer.class && rightClass==Integer.class) {
             return left.equals(right);
         }
@@ -745,6 +747,17 @@ public class ScriptBytecodeAdapter {
         if (leftClass == BigInteger.class && rightClass == BigInteger.class) {
             return ((BigInteger) left).compareTo((BigInteger) right) == 0;
         }
+
+        if (left instanceof CharSequence && right instanceof CharSequence) {
+            if (leftClass != String.class) {
+                left = left.toString();
+            }
+            if (rightClass != String.class) {
+                right = right.toString();
+            }
+            return left.equals(right);
+        }
+
         return DefaultTypeTransformation.compareEqual(left, right);
     }