Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit has no build smarts. Here's the process: ``` $ cd ext/java/ $ javac -cp ../../../jruby/lib/jruby.jar fast_blank.java $ jar cvf ../../lib/fast_blank.jar fast_blank.class added manifest adding: fast_blank.class(in = 2182) (out= 1121)(deflated 48%) $ cd ../.. $ jruby -Ilib -e 'require "fast_blank"; p "".blank?' true ```
- Loading branch information
Showing
4 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
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.util.ByteList; | ||
import org.jruby.util.io.EncodingUtils; | ||
|
||
public class fast_blank { | ||
@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: | ||
case 0x180e: | ||
/* 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 (!Character.isSpaceChar(cc) && cc != 0) return context.fals; | ||
s += n[0]; | ||
} | ||
|
||
return context.tru; | ||
} | ||
|
||
public static void init(Ruby runtime) { | ||
runtime.getString().defineAnnotatedMethods(fast_blank.class); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
case RUBY_ENGINE | ||
when 'jruby' | ||
require 'fast_blank.jar' | ||
Java::fast_blank.init(JRuby.runtime) | ||
else | ||
require 'fast_blank.so' | ||
end |