forked from SamSaffron/fast_blank
/
FastBlankLibrary.java
108 lines (92 loc) · 3 KB
/
FastBlankLibrary.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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');
}
}