-
-
Notifications
You must be signed in to change notification settings - Fork 925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'あ'.to_sym == :'あ' is false #3035
Comments
I just ran this on |
Evaluates to |
|
The bug is that Also, in both cases, |
@trejkaz |
Hi all, The following patch fixed this issue for me on the master branch. diff --git a/core/src/main/java/org/jruby/RubySymbol.java b/core/src/main/java/org/jruby/RubySymbol.java
index c4f8186..5a37881 100644
--- a/core/src/main/java/org/jruby/RubySymbol.java
+++ b/core/src/main/java/org/jruby/RubySymbol.java
@@ -42,6 +42,7 @@ import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.compiler.Constantizable;
+import org.jruby.RubyEncoding;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Binding;
@@ -956,13 +957,21 @@ public class RubySymbol extends RubyObject implements MarshalEncoding, Constanti
}
private static int javaStringHashCode(String str) {
- return str.hashCode();
+ byte val[] = str.getBytes(RubyEncoding.ISO);
+ int h = 0;
+ int length = val.length;
+ if (length > 0) {
+ for (int i = 0; i < length; i++) {
+ h = 31 * h + val[i];
+ }
+ }
+ return h;
}
private static int javaStringHashCode(ByteList iso8859) {
int h = 0;
int length = iso8859.length();
- if (h == 0 && length > 0) {
+ if (length > 0) {
byte val[] = iso8859.getUnsafeBytes();
int begin = iso8859.begin(); I will test and send a pull request with the bug. Please let me know what you think. |
I think the name of the methods would have to change because it no longer matches the behaviour of Java's String class, but the two implementations match, so it would certainly fix it. I don't know whether there is a particular point in having it match String's hash (and if I remember correctly, weren't there issues with String's hashing algorithm anyway? I don't remember the details.) |
@trejkaz thanks. I change the above my patch. new patch: diff --git a/core/src/main/java/org/jruby/RubySymbol.java b/core/src/main/java/o
rg/jruby/RubySymbol.java
index c4f8186..8f3aa9f 100644
--- a/core/src/main/java/org/jruby/RubySymbol.java
+++ b/core/src/main/java/org/jruby/RubySymbol.java
@@ -42,6 +42,7 @@ import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.compiler.Constantizable;
+import org.jruby.RubyEncoding;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Binding;
@@ -65,6 +66,7 @@ import org.jruby.util.SipHashInline;
import java.lang.ref.WeakReference;
import java.util.concurrent.locks.ReentrantLock;
+import java.util.Arrays;
import static org.jruby.util.StringSupport.codeLength;
import static org.jruby.util.StringSupport.codePoint;
@@ -962,13 +964,11 @@ public class RubySymbol extends RubyObject implements MarshalEncoding, Constanti
private static int javaStringHashCode(ByteList iso8859) {
int h = 0;
int length = iso8859.length();
- if (h == 0 && length > 0) {
+ if (length > 0) {
byte val[] = iso8859.getUnsafeBytes();
int begin = iso8859.begin();
-
- for (int i = 0; i < length; i++) {
- h = 31 * h + val[begin + i];
- }
+ byte copyVal[] = Arrays.copyOfRange(val, begin, begin + length);
+ h = new String(copyVal, RubyEncoding.ISO).hashCode();
}
return h;
} |
Patch looks right. Will merge after review. |
Thanks for the fix, @k77ch7! |
jruby version is 9.0.0.0.rc1.
'あ'.to_sym
and:'あ'
should be the same object, but I got the following result.The text was updated successfully, but these errors were encountered: