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: c1fcd39195f7
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 27c44742b1e0
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Aug 16, 2016

  1. Copy the full SHA
    79bf4be View commit details
  2. Copy the full SHA
    27c4474 View commit details
Showing with 81 additions and 33 deletions.
  1. +67 −26 lib/ruby/truffle/truffle/digest.rb
  2. +14 −7 spec/ruby/language/predefined_spec.rb
93 changes: 67 additions & 26 deletions lib/ruby/truffle/truffle/digest.rb
Original file line number Diff line number Diff line change
@@ -6,18 +6,72 @@
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

# The part enclosed between START from MRI/END from MRI is licensed
# under LICENSE.RUBY as it is derived from lib/ruby/stdlib/digest.rb.

module Digest

NO_MESSAGE = Object.new
# START from MRI
class ::Digest::Class
# Creates a digest object and reads a given file, _name_.
# Optional arguments are passed to the constructor of the digest
# class.
#
# p Digest::SHA256.file("X11R6.8.2-src.tar.bz2").hexdigest
# # => "f02e3c85572dc9ad7cb77c2a638e3be24cc1b5bea9fdbb0b0299c9668475c534"
def self.file(name, *args)
new(*args).file(name)
end

class Class
# Returns the base64 encoded hash value of a given _string_. The
# return value is properly padded with '=' and contains no line
# feeds.
def self.base64digest(str, *args)
[digest(str, *args)].pack('m0')
end
end

def self.file(file)
digest = new
digest.update File.read(file)
digest
module Instance
# Updates the digest with the contents of a given file _name_ and
# returns self.
def file(name)
File.open(name, "rb") {|f|
buf = ""
while f.read(16384, buf)
update buf
end
}
self
end

# If none is given, returns the resulting hash value of the digest
# in a base64 encoded form, keeping the digest's state.
#
# If a +string+ is given, returns the hash value for the given
# +string+ in a base64 encoded form, resetting the digest to the
# initial state before and after the process.
#
# In either case, the return value is properly padded with '=' and
# contains no line feeds.
def base64digest(str = nil)
[str ? digest(str) : digest].pack('m0')
end

# Returns the resulting hash value and resets the digest to the
# initial state.
def base64digest!
[digest!].pack('m0')
end
end
# END from MRI

NO_MESSAGE = Object.new

def Digest.hexencode(message)
StringValue(message).unpack('H*').first
end

class Class
def self.digest(message)
digest = new
digest.update message
@@ -29,18 +83,19 @@ def self.hexdigest(message)
digest.update message
digest.hexdigest
end
end

module Instance
def update(message)
Truffle::Digest.update @digest, message
end

alias_method :<<, :update

def reset
Truffle::Digest.reset @digest
end

def digest(message=NO_MESSAGE)
def digest(message = NO_MESSAGE)
if NO_MESSAGE == message
Truffle::Digest.digest @digest
else
@@ -50,10 +105,9 @@ def digest(message=NO_MESSAGE)
end
end

def hexdigest(message=NO_MESSAGE)
def hexdigest(message = NO_MESSAGE)
Digest.hexencode(digest(message))
end

alias_method :to_s, :hexdigest
alias_method :to_str, :hexdigest

@@ -72,7 +126,6 @@ def hexdigest!
def digest_length
Truffle::Digest.digest_length @digest
end

alias_method :size, :digest_length
alias_method :length, :digest_length

@@ -83,73 +136,61 @@ def ==(other)
def inspect
"#<#{self.class.name}: #{hexdigest}>"
end
end

class Class
include Instance
end

class MD5 < Class

def initialize
@digest = Truffle::Digest.md5
end

def block_length
64
end

end

class SHA1 < Class

def initialize
@digest = Truffle::Digest.sha1
end

def block_length
64
end

end

class SHA256 < Class

def initialize
@digest = Truffle::Digest.sha256
end

def block_length
64
end

end

class SHA384 < Class

def initialize
@digest = Truffle::Digest.sha384
end

def block_length
128
end

end

class SHA512 < Class

def initialize
@digest = Truffle::Digest.sha512
end

def block_length
128
end

end

def self.hexencode(message)
StringValue(message).unpack('H*').first
end

end

require 'digest/sha2'
21 changes: 14 additions & 7 deletions spec/ruby/language/predefined_spec.rb
Original file line number Diff line number Diff line change
@@ -64,19 +64,26 @@
def obj.foo; yield; end
def obj.foo2(&proc); proc.call; end

match = /foo/.match "foo"
match2 = nil
match3 = nil
match4 = nil

obj.foo { match = /bar/.match("bar") }
match1 = /foo/.match "foo"

$~.should == match
obj.foo { match2 = /bar/.match("bar") }

eval 'match = /baz/.match("baz")'
match2.should_not == nil
$~.should == match2

$~.should == match
eval 'match3 = /baz/.match("baz")'

obj.foo2 { match = /qux/.match("qux") }
match3.should_not == nil
$~.should == match3

$~.should == match
obj.foo2 { match4 = /qux/.match("qux") }

match4.should_not == nil
$~.should == match4
end

it "raises an error if assigned an object not nil or instanceof MatchData" do