Differences between version 3 and previous revision of KnowledgeBase/Scripts/Base80.rb.
Other diffs: Previous Major Revision, Previous Author
Newer page: | version 3 | Last edited on Sunday, 14 March 2010 23:08:45 | by CyberLeo | Revert |
Older page: | version 2 | Last edited on Sunday, 14 March 2010 22:53:05 | by CyberLeo | Revert |
@@ -1,62 +1,56 @@
<code brush="ruby">
-class Base80
- @@
symbols = "!()*,-.0123456789:;@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~"
- @@radix = @@symbols.length
-
+module GenericRadix
+ # Define
symbols to be your alphabet
@int = 0
- @str = "!
"
+ @str = ""
- def self.symbols
- @@
symbols
+ def radix
+ @radix ||=
symbols.length
end
+ module_function :radix
- def self.radix
- @@radix
+ def symbols_valid?(number)
+ !!(number =~ Regexp.new("^[" << symbols.gsub(/[\[\]]/) {|m| "\\" << m } << "]*$"))
end
+ module_function :symbols_valid?
- def self.symbols_valid?(number)
- !!(number =~ Regexp.new("^[" << @@symbols.gsub(/[\[\]]/) {|m| "\\" << m } << "]*$"))
- end
-
- def self.
itos(number)
+ def itos(number)
number = number.to_i
- return @@
symbols[0, 1] if number.zero?
+ return symbols[0, 1] if number.zero?
res = ""
until number.zero?
- index = number % @@
radix
- number = number / @@
radix
- res = @@
symbols[index, 1] + res
+ index = number % radix
+ number = number / radix
+ res = symbols[index, 1] + res
end
res
end
+ module_function :itos
- def self.
stoi(number)
+ def stoi(number)
number = number.dup.to_s.split(//)
res = 0
until number.length.zero?
char = number.shift
- index = @@
symbols.index(char)
- res *= @@
radix
+ index = symbols.index(char)
+ res *= radix
res += index
end
res
end
+ module_function :stoi
def initialize(number)
if number.is_a?(Fixnum)
@int = number.to_i
- @str = self.class.
itos(@int)
- elsif self.class.
symbols_valid?(number.to_s)
+ @str = itos(@int)
+ elsif symbols_valid?(number.to_s)
@str = number.to_s
- @int = self.class.
stoi(@str)
+ @int = stoi(@str)
else
- raise "Not an Integer or a radix #{@@
radix} number"
+ raise "Not an Integer or a radix #{radix} number"
end
- end
-
- def radix
- @@radix
end
def to_i
@int
@@ -68,6 +62,20 @@
def zero?
@int.zero?
end
+end
+
+class Base80
+ def symbols
+ "!()*,-.0123456789:;@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~"
+ end
+ include GenericRadix
+end
+
+class Base256
+ def symbols
+ ( Range.new(0,255).map {|ord| ord.chr }.join )
+ end
+ include GenericRadix
end
</code>
version 3
module GenericRadix # Define symbols to be your alphabet @int = 0 @str = "" def radix @radix ||= symbols.length end module_function :radix def symbols_valid?(number) !!(number =~ Regexp.new("^[" << symbols.gsub(/[\[\]]/) {|m| "\\" << m } << "]*$")) end module_function :symbols_valid? def itos(number) number = number.to_i return symbols[0, 1] if number.zero? res = "" until number.zero? index = number % radix number = number / radix res = symbols[index, 1] + res end res end module_function :itos def stoi(number) number = number.dup.to_s.split(//) res = 0 until number.length.zero? char = number.shift index = symbols.index(char) res *= radix res += index end res end module_function :stoi def initialize(number) if number.is_a?(Fixnum) @int = number.to_i @str = itos(@int) elsif symbols_valid?(number.to_s) @str = number.to_s @int = stoi(@str) else raise "Not an Integer or a radix #{radix} number" end end def to_i @int end def to_s @str end def zero? @int.zero? end end class Base80 def symbols "!()*,-.0123456789:;@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~" end include GenericRadix end class Base256 def symbols ( Range.new(0,255).map {|ord| ord.chr }.join ) end include GenericRadix end