mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 02:53:37 +08:00
d0d5a138c3
We have the `# frozen_string_literal: true` comment on all our files. This means all string literals are frozen. There is no need to call #freeze on any literals. For files with `# frozen_string_literal: true` ``` puts %w{a b}[0].frozen? => true puts "hi".frozen? => true puts "a #{1} b".frozen? => true puts ("a " + "b").frozen? => false puts (-("a " + "b")).frozen? => true ``` For more details see: https://samsaffron.com/archive/2018/02/16/reducing-string-duplication-in-ruby
38 lines
853 B
Ruby
38 lines
853 B
Ruby
# frozen_string_literal: true
|
|
|
|
# Modified version of: https://github.com/steventen/base62-rb
|
|
|
|
module Base62
|
|
KEYS ||= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
KEYS_HASH ||= KEYS.each_char.with_index.to_h
|
|
BASE ||= KEYS.length
|
|
|
|
# Encodes base10 (decimal) number to base62 string.
|
|
def self.encode(num)
|
|
return "0" if num == 0
|
|
return nil if num < 0
|
|
|
|
str = ""
|
|
while num > 0
|
|
# prepend base62 charaters
|
|
str = KEYS[num % BASE] + str
|
|
num = num / BASE
|
|
end
|
|
str
|
|
end
|
|
|
|
# Decodes base62 string to a base10 (decimal) number.
|
|
def self.decode(str)
|
|
num = 0
|
|
i = 0
|
|
len = str.length - 1
|
|
# while loop is faster than each_char or other 'idiomatic' way
|
|
while i < str.length
|
|
pow = BASE**(len - i)
|
|
num += KEYS_HASH[str[i]] * pow
|
|
i += 1
|
|
end
|
|
num
|
|
end
|
|
end
|