Skip to content

Encoding floats creates large encoded string #9

@akeenl-sp

Description

@akeenl-sp

Due to the division in the to_id function when encoding numbers the loop continues until the results is smaller than the smallest possible float value, creating large encoded string
Ex.

irb(main):104> sqids = Sqids.new
=>
#<Sqids:0x0000ffff758d57a0
...
irb(main):105> sqids.encode([3.1])
=> "EBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBf"
irb(main):106>
  def to_id(num, alphabet)
    ...
    result = num
    loop do
      id.unshift(chars[result % chars.length])
      result /= chars.length # <- here results never reaches 0 if its a float
      break unless result.positive?
    end
    ...
  end

I have a PR for mapping all the values passed into encode from floats to integers which is what was done in the original HashIds gem
#8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions