Skip to content

Commit

Permalink
Fixing up Kernel.Integer, all specs passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarsa committed Aug 7, 2009
1 parent 270c5bf commit 2e40b9e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
7 changes: 5 additions & 2 deletions kernel/common/kernel.rb
Expand Up @@ -33,9 +33,12 @@ def Integer(obj)
else
return obj.to_int
end
elsif obj.respond_to? :to_int
int_value = obj.to_int
return int_value unless int_value.nil?
end
method = obj.respond_to?(:to_int) ? :to_int : :to_i
Type.coerce_to(obj, Integer, method)

Type.coerce_to(obj, Integer, :to_i)
end
module_function :Integer

Expand Down
4 changes: 2 additions & 2 deletions kernel/common/string.rb
Expand Up @@ -1699,7 +1699,7 @@ def to_inum(base, check)
"invalid value for Integer: #{inspect}") if check and self =~ /__/

s = if check then
self.strip
self
else
self.delete('_').strip
end
Expand Down Expand Up @@ -1746,7 +1746,7 @@ def to_inum(base, check)
sign, data = $1, $2 if s =~ match_re

raise ArgumentError, "error in impl parsing: #{self.inspect} with #{match_re.source}" if
data.nil? || (check && (s =~ /^_/ || data.empty? ))
data.nil? || (check && (s =~ /^_|_$/ || data.empty? ))

negative = sign == "-"
result = 0
Expand Down
18 changes: 0 additions & 18 deletions spec/tags/frozen/core/kernel/Integer_tags.txt
@@ -1,19 +1 @@
incomplete:Kernel#Integer needs to be reviewed for spec completeness
fails:Kernel.Integer uncritically return the value of to_int even if it is not an Integer
fails:Kernel.Integer returns the value of to_int if the result is a Bignum
fails:Kernel.Integer calls to_i on an object whose to_int returns nil
fails:Kernel.Integer raises an ArgumentError if the String is a null byte
fails:Kernel.Integer raises an ArgumentError if the String starts with a null byte
fails:Kernel.Integer raises an ArgumentError if the String ends with a null byte
fails:Kernel.Integer raises an ArgumentError if the String contains a null byte
fails:Kernel.Integer raises an ArgumentError if there are trailing _s
fails:Kernel.Integer raises an ArgumentError if there are multiple embedded _s
fails:Kernel#Integer uncritically return the value of to_int even if it is not an Integer
fails:Kernel#Integer returns the value of to_int if the result is a Bignum
fails:Kernel#Integer calls to_i on an object whose to_int returns nil
fails:Kernel#Integer raises an ArgumentError if the String is a null byte
fails:Kernel#Integer raises an ArgumentError if the String starts with a null byte
fails:Kernel#Integer raises an ArgumentError if the String ends with a null byte
fails:Kernel#Integer raises an ArgumentError if the String contains a null byte
fails:Kernel#Integer raises an ArgumentError if there are trailing _s
fails:Kernel#Integer raises an ArgumentError if there are multiple embedded _s
16 changes: 16 additions & 0 deletions vm/builtin/string.cpp
Expand Up @@ -519,6 +519,11 @@ namespace rubinius {
bool negative = false;
Integer* value = Fixnum::from(0);

if(strict == Qtrue) {
// In strict mode the string can't have null bytes.
if(size() > strlen(str)) return (Integer*)Qnil;
}

if(base < 0 || base == 1 || base > 36) return (Integer*)Qnil;
// Strict mode can only be invoked from Ruby via Kernel#Integer()
// which does not allow bases other than 0.
Expand Down Expand Up @@ -627,6 +632,10 @@ namespace rubinius {
// If it's an underscore, remember that. An underscore is valid iff
// it followed by a valid character for this base.
if(chr == '_') {
// Double underscore is forbidden in strict mode.
if(underscore && strict == Qtrue) {
return (Integer*)Qnil;
}
underscore = true;
continue;
} else {
Expand All @@ -640,6 +649,13 @@ namespace rubinius {
chr -= ('A' - 10);
} else if(chr >= 'a' && chr <= 'z') {
chr -= ('a' - 10);
} else {
//Invalid character, stopping right here.
if(strict == Qtrue) {
return (Integer*)Qnil;
} else {
break;
}
}

// Bail if the current chr is greater or equal to the base,
Expand Down

0 comments on commit 2e40b9e

Please sign in to comment.