Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: de3129a9bcee
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: d9e837967ec5
Choose a head ref
  • 2 commits
  • 7 files changed
  • 2 contributors

Commits on Mar 6, 2015

  1. Copy the full SHA
    72619f0 View commit details
  2. Merge pull request #2660 from bjfish/truffle_string_to_c_to_c

    [Truffle] Added String#to_c String#to_r in ruby.
    chrisseaton committed Mar 6, 2015
    Copy the full SHA
    d9e8379 View commit details
24 changes: 0 additions & 24 deletions spec/truffle/tags/core/string/to_c_tags.txt

This file was deleted.

11 changes: 0 additions & 11 deletions spec/truffle/tags/core/string/to_r_tags.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
fails:String#to_r returns a Rational object
fails:String#to_r returns (0/1) for the empty String
fails:String#to_r returns (n/1) for a String starting with a decimal _n_
fails:String#to_r ignores trailing characters
fails:String#to_r ignores leading spaces
fails:String#to_r does not ignore arbitrary, non-numeric leading characters
fails:String#to_r treats leading hypens as minus signs
fails:String#to_r does not treat a leading period without a numeric prefix as a decimal point
fails:String#to_r understands decimal points
fails:String#to_r ignores underscores between numbers
fails:String#to_r understands a forward slash as separating the numerator from the denominator
fails:String#to_r returns (0/1) for Strings it can't parse
3 changes: 3 additions & 0 deletions truffle/src/main/ruby/core.rb
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@
require_relative 'core/rubinius/common/identity_map'
require_relative 'core/rubinius/common/comparable'
require_relative 'core/rubinius/common/numeric'
require_relative 'core/rubinius/common/ctype'
require_relative 'core/rubinius/common/integer'
require_relative 'core/rubinius/common/bignum'
require_relative 'core/rubinius/common/fixnum'
@@ -71,7 +72,9 @@
require_relative 'core/rubinius/common/true'

require_relative 'core/rubinius/common/rational'
require_relative 'core/rubinius/common/rationalizer'
require_relative 'core/rubinius/common/complex'
require_relative 'core/rubinius/common/complexifier'
require_relative 'core/rubinius/common/gc'

# Load JRuby+Truffle classes
92 changes: 92 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/complexifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Copyright (c) 2007-2014, Evan Phoenix and contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Rubinius nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

class String
class Complexifier
SPACE = Rationalizer::SPACE
NUMERATOR = Rationalizer::NUMERATOR
DENOMINATOR = Rationalizer::DENOMINATOR
NUMBER = "[-+]?#{NUMERATOR}(?:\\/#{DENOMINATOR})?"
NUMBERNOS = "#{NUMERATOR}(?:\\/#{DENOMINATOR})?"
PATTERN0 = Regexp.new "\\A#{SPACE}(#{NUMBER})@(#{NUMBER})#{SPACE}"
PATTERN1 = Regexp.new "\\A#{SPACE}([-+])?(#{NUMBER})?[iIjJ]#{SPACE}"
PATTERN2 = Regexp.new "\\A#{SPACE}(#{NUMBER})(([-+])(#{NUMBERNOS})?[iIjJ])?#{SPACE}"

def initialize(value)
@value = value
end

def convert
if m = PATTERN0.match(@value)
sr = m[1]
si = m[2]
re = m.post_match
po = true
elsif m = PATTERN1.match(@value)
sr = nil
si = (m[1] || "") + (m[2] || "1")
re = m.post_match
po = false
elsif m = PATTERN2.match(@value)
sr = m[1]
si = m[2] ? m[3] + (m[4] || "1") : nil
re = m.post_match
po = false
else
return Complex.new(0, 0)
end

r = 0
i = 0

if sr
if sr.include?("/")
r = sr.to_r
elsif sr.match(/[.eE]/)
r = sr.to_f
else
r = sr.to_i
end
end

if si
if si.include?("/")
i = si.to_r
elsif si.match(/[.eE]/)
i = si.to_f
else
i = si.to_i
end
end

if po
Complex.polar(r, i)
else
Complex.rect(r, i)
end
end
end
end
77 changes: 77 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/ctype.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright (c) 2007-2014, Evan Phoenix and contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Rubinius nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

##
# Mixin containing byte classification methods.
#--
# isspace, islower, ... are not in MRI core library.

module Rubinius::CType
# The character literals (?x) are Fixnums in 1.8 and Strings in 1.9
# so we use literal values instead so this is 1.8/1.9 compatible.

# \n \r \t \f \v \a \b \e
def self.isctrl(num)
(num >= 7 and num <= 13) or num == 27
end

# ' ' \n \t \r \f \v
def self.isspace(num)
num == 32 or (num >= 9 and num <= 13)
end

def self.isupper(num)
num >= 65 and num <= 90
end

def self.islower(num)
num >= 97 and num <= 122
end

def self.isdigit(num)
num >= 48 and num <= 57
end

def self.isalnum(num)
islower(num) or isupper(num) or isdigit(num)
end

def self.toupper!(num)
num - 32
end

def self.toupper(num)
islower(num) ? toupper!(num) : num
end

def self.tolower!(num)
num + 32
end

def self.tolower(num)
isupper(num) ? tolower!(num) : num
end
end
83 changes: 83 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/rationalizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright (c) 2007-2014, Evan Phoenix and contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Rubinius nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Float#rationalize and String#to_r require complex algorithms. The Regexp
# required for String#to_r cannot be created at class scope when loading the
# kernel because Regexp needs to load after String and making a simpler
# Regexp#initialize for bootstrapping isn't really feasible. So these
# algorithms are located here.
#
class String
class Rationalizer
SPACE = "\\s*"
DIGITS = "(?:[0-9](?:_[0-9]|[0-9])*)"
NUMERATOR = "(?:#{DIGITS}?\\.)?#{DIGITS}(?:[eE][-+]?#{DIGITS})?"
DENOMINATOR = DIGITS
RATIONAL = "\\A#{SPACE}([-+])?(#{NUMERATOR})(?:\\/(#{DENOMINATOR}))?#{SPACE}"
PATTERN = Regexp.new RATIONAL

def initialize(value)
@value = value
end

def convert
if m = PATTERN.match(@value)
si = m[1]
nu = m[2]
de = m[3]
re = m.post_match

ifp, exp = nu.split /[eE]/
ip, fp = ifp.split /\./

value = Rational.new(ip.to_i, 1)

if fp
ctype = Rubinius::CType
i = count = 0
size = fp.size
while i < size
count += 1 if ctype.isdigit fp.getbyte(i)
i += 1
end

l = 10 ** count
value *= l
value += fp.to_i
value = value.quo(l)
end

value = -value if si == "-"
value *= 10 ** exp.to_i if exp
value = value.quo(de.to_i) if de

value
else
Rational(0, 1)
end
end
end
end
8 changes: 8 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/string.rb
Original file line number Diff line number Diff line change
@@ -77,6 +77,14 @@ def slice!(one, two=undefined)
result
end

def to_c
Complexifier.new(self).convert
end

def to_r
Rationalizer.new(self).convert
end

def each_line(sep=$/)
return to_enum(:each_line, sep) unless block_given?