Skip to content

Commit

Permalink
Add @q
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-levan committed Nov 22, 2024
1 parent 35afd21 commit 8586798
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/aura.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "aura/helpers"
require "aura/p"
require "aura/q"
require "hoon"

require_relative "aura/version"
Expand Down
47 changes: 47 additions & 0 deletions lib/aura/q.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require_relative("helpers")
require_relative("../hoon")
require_relative("p")

module Aura
# @q
module Q
extend Helpers

module_function

# Convert a number to a @q-encoded string.
def self.patq(arg)
n = arg.to_i
buf = n.to_s(16).scan(/../).map(&:hex)
puts buf
buf2patq(buf)
end

def self.buf2patq(buf)
# Split the buffer into chunks of 2, with a special case for odd-length buffers
chunked = if buf.length.odd? && buf.length > 1
[[buf[0]]] + buf[1..-1].each_slice(2).to_a
else
buf.each_slice(2).to_a
end

chunked.reduce("~") do |acc, elem|
acc + (acc == "~" ? "" : "-") + alg(elem, chunked)
end
end

def prefix_name(byts)
byts[1].nil? ? prefixes[0] + suffixes[byts[0]] : prefixes[byts[0]] + suffixes[byts[1]]
end

def name(byts)
byts[1].nil? ? suffixes[byts[0]] : prefixes[byts[0]] + suffixes[byts[1]]
end

def alg(pair, chunked)
pair.length.odd? && chunked.length > 1 ? prefix_name(pair) : name(pair)
end
end
end

0 comments on commit 8586798

Please sign in to comment.