From a9442712f28f3e55a3f94fe2101c0be993f9cfb1 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Mon, 13 Nov 2017 00:52:13 -0600 Subject: [PATCH] JRuby support This commit has no build smarts. Here's the process: ``` $ javac -cp ../jruby/lib/jruby.jar -d ext/java ext/java/com/headius/jruby/fast_blank/FastBlankLibrary.java $ jar cvf lib/fast_blank.jar -C ext/java/ com $ jruby -Ilib -e 'require "fast_blank"; p "".blank?' true ``` Proper support will use `rake-compiler` to build the jar. --- .../jruby/fast_blank/FastBlankLibrary.java | 108 ++++++++++++++++++ lib/fast_blank.jar | Bin 0 -> 1587 bytes lib/fast_blank.rb | 7 ++ 3 files changed, 115 insertions(+) create mode 100644 ext/java/com/headius/jruby/fast_blank/FastBlankLibrary.java create mode 100644 lib/fast_blank.jar create mode 100644 lib/fast_blank.rb diff --git a/ext/java/com/headius/jruby/fast_blank/FastBlankLibrary.java b/ext/java/com/headius/jruby/fast_blank/FastBlankLibrary.java new file mode 100644 index 0000000..a7e6ff2 --- /dev/null +++ b/ext/java/com/headius/jruby/fast_blank/FastBlankLibrary.java @@ -0,0 +1,108 @@ +package com.headius.jruby.fast_blank; + +import org.jcodings.Encoding; +import org.jruby.Ruby; +import org.jruby.RubyString; +import org.jruby.anno.JRubyMethod; +import org.jruby.runtime.ThreadContext; +import org.jruby.runtime.builtin.IRubyObject; +import org.jruby.runtime.load.Library; +import org.jruby.util.ByteList; +import org.jruby.util.io.EncodingUtils; + +public class FastBlankLibrary implements Library { + public void load(Ruby runtime, boolean wrap) { + runtime.getString().defineAnnotatedMethods(FastBlankLibrary.class); + } + + @JRubyMethod(name = "blank_as?") + public static IRubyObject blank_as_p(ThreadContext context, IRubyObject self) { + Encoding enc; + int s, e; + byte[] sBytes; + + Ruby runtime = context.runtime; + + RubyString str = (RubyString) self; + enc = str.getEncoding(); + ByteList sByteList = str.getByteList(); + sBytes = sByteList.unsafeBytes(); + s = sByteList.begin(); + if (str.size() == 0) return context.tru; + + e = s + sByteList.realSize(); + int[] n = {0}; + while (s < e) { + int cc = EncodingUtils.encCodepointLength(runtime, sBytes, s, e, n, enc); + + switch (cc) { + case 9: + case 0xa: + case 0xb: + case 0xc: + case 0xd: + case 0x20: + case 0x85: + case 0xa0: + case 0x1680: + case 0x2000: + case 0x2001: + case 0x2002: + case 0x2003: + case 0x2004: + case 0x2005: + case 0x2006: + case 0x2007: + case 0x2008: + case 0x2009: + case 0x200a: + case 0x2028: + case 0x2029: + case 0x202f: + case 0x205f: + case 0x3000: + /* found */ + break; + default: + return context.fals; + } + s += n[0]; + } + return context.tru; + } + + @JRubyMethod(name = "blank?") + public static IRubyObject blank_p(ThreadContext context, IRubyObject self) { + Encoding enc; + int s, e; + byte[] sBytes; + + Ruby runtime = context.runtime; + + RubyString str = (RubyString) self; + + enc = str.getEncoding(); + ByteList sByteList = str.getByteList(); + sBytes = sByteList.unsafeBytes(); + s = sByteList.begin(); + + if (str.size() == 0) return context.tru; + + e = s + sByteList.realSize(); + int[] n = {0}; + while (s < e) { + int cc = EncodingUtils.encCodepointLength(runtime, sBytes, s, e, n, enc); + + if (!isSpace(cc) && cc != 0) return context.fals; + s += n[0]; + } + + return context.tru; + } + + // MRI: rb_isspace + private static boolean isSpace(int codepoint) { + long c = codepoint & 0xFFFFFFFF; + return c == ' ' || ('\t' <= c && c <= '\r'); + } +} diff --git a/lib/fast_blank.jar b/lib/fast_blank.jar new file mode 100644 index 0000000000000000000000000000000000000000..a5b92ad530ef05514bad919088fc90a681cc1456 GIT binary patch literal 1587 zcmWIWW@Zs#;Nak3aAnK&W*(j{<{BKL=j-;__snS@Z(Y5MyxzK6=gyqp9At3C_`%a6JuhD!Pv48Bt5`TA zUPvC1mXg-W_#v*U_I!z!#dC4dC*rEp7_Mf2D*9N&2zEsvmzIwU&<MLWr)~mQ5Mj*IGUWXEZZL zF>O)R%9*69!@GON-I6u?ems&-oG%mq>p}N9i!GN6W#-p@xheUdJ$_E1%!fO7vzbMX zpUX)5R`-2g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