Skip to content

Commit 77fd248

Browse files
committed
[Truffle] Re-implement String#include? using Rubinius.
1 parent 9e53642 commit 77fd248

File tree

6 files changed

+108
-3
lines changed

6 files changed

+108
-3
lines changed

core/src/main/java/org/jruby/truffle/nodes/core/StringNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ public int getByte(RubyString string, int index) {
737737
}
738738
}
739739

740-
@CoreMethod(names = "include?", required = 1)
740+
@CoreMethod(names = "__include?", required = 1)
741741
public abstract static class IncludeQueryNode extends CoreMethodNode {
742742

743743
public IncludeQueryNode(RubyContext context, SourceSection sourceSection) {

core/src/main/java/org/jruby/truffle/nodes/rubinius/StringPrimitiveNodes.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.oracle.truffle.api.utilities.ConditionProfile;
1515
import org.jruby.truffle.runtime.RubyContext;
1616
import org.jruby.truffle.runtime.core.*;
17+
import org.jruby.util.StringSupport;
1718

1819
/**
1920
* Rubinius primitives associated with the Ruby {@code String} class.
@@ -64,4 +65,30 @@ public RubyString stringToF(RubyString string) {
6465

6566
}
6667

68+
@RubiniusPrimitive(name = "string_index")
69+
public static abstract class StringIndexPrimitiveNode extends RubiniusPrimitiveNode {
70+
71+
public StringIndexPrimitiveNode(RubyContext context, SourceSection sourceSection) {
72+
super(context, sourceSection);
73+
}
74+
75+
public StringIndexPrimitiveNode(StringIndexPrimitiveNode prev) {
76+
super(prev);
77+
}
78+
79+
@Specialization
80+
public Object stringIndex(RubyString string, RubyString pattern, int start) {
81+
final int index = StringSupport.index(string, string.getBytes(), string.length(),
82+
pattern, pattern.getBytes(), pattern.length(),
83+
start, string.getBytes().getEncoding());
84+
85+
if (index == -1) {
86+
return getContext().getCoreLibrary().getNilObject();
87+
}
88+
89+
return index;
90+
}
91+
92+
}
93+
6794
}

core/src/main/ruby/jruby/truffle/core.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
require_relative 'core/rubinius/kernel/bootstrap/time'
2929
require_relative 'core/rubinius/kernel/bootstrap/type'
30+
require_relative 'core/rubinius/kernel/bootstrap/string'
3031

3132
require_relative 'core/rubinius/kernel/common/kernel'
3233
require_relative 'core/rubinius/kernel/common/array'
@@ -39,5 +40,6 @@
3940
require_relative 'core/rubinius/kernel/common/integer'
4041
require_relative 'core/rubinius/kernel/common/float'
4142
require_relative 'core/rubinius/kernel/common/numeric'
43+
require_relative 'core/rubinius/kernel/common/string'
4244

4345
require_relative 'core/shims'
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) 2007-2014, Evan Phoenix and contributors
2+
# All rights reserved.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# * Redistributions of source code must retain the above copyright notice, this
8+
# list of conditions and the following disclaimer.
9+
# * Redistributions in binary form must reproduce the above copyright notice
10+
# this list of conditions and the following disclaimer in the documentation
11+
# and/or other materials provided with the distribution.
12+
# * Neither the name of Rubinius nor the names of its contributors
13+
# may be used to endorse or promote products derived from this software
14+
# without specific prior written permission.
15+
#
16+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
# Only part of Rubinius' string.rb
28+
29+
class String
30+
31+
def find_string(pattern, start)
32+
Rubinius.primitive :string_index
33+
raise PrimitiveFailure, "String#find_string primitive failed"
34+
end
35+
36+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (c) 2007-2014, Evan Phoenix and contributors
2+
# All rights reserved.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# * Redistributions of source code must retain the above copyright notice, this
8+
# list of conditions and the following disclaimer.
9+
# * Redistributions in binary form must reproduce the above copyright notice
10+
# this list of conditions and the following disclaimer in the documentation
11+
# and/or other materials provided with the distribution.
12+
# * Neither the name of Rubinius nor the names of its contributors
13+
# may be used to endorse or promote products derived from this software
14+
# without specific prior written permission.
15+
#
16+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
# Only part of Rubinius' string.rb
28+
29+
class String
30+
31+
def include?(needle)
32+
if needle.kind_of? Fixnum
33+
needle = needle % 256
34+
str_needle = needle.chr
35+
else
36+
str_needle = StringValue(needle)
37+
end
38+
39+
!!find_string(str_needle, 0)
40+
end
41+
42+
end

spec/truffle/tags/core/string/include_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)