Module: Aura::Q

Extended by:
Aura
Defined in:
lib/aura.rb

Constant Summary

Constants included from Aura

PREFIXES, SUFFIXES, VERSION

Class Method Summary collapse

Methods included from Aura

bex, end_bits, met, patp2syls, rsh, valid_pat?, version

Class Method Details

.alg(pair, chunked) ⇒ Object



330
331
332
# File 'lib/aura.rb', line 330

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

.buf2patq(buf) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/aura.rb', line 239

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..].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

.eq_mod_leading_zero_bytes(hex1, hex2) ⇒ Object

Helper method to compare hex strings ignoring leading zero bytes



315
316
317
318
# File 'lib/aura.rb', line 315

def self.eq_mod_leading_zero_bytes(hex1, hex2)
  # Remove any leading zeros and compare
  hex1.gsub(/^0+/, "") == hex2.gsub(/^0+/, "")
end

.eq_patq(p, q) ⇒ Object

Validate @q-encoded string equality.



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/aura.rb', line 295

def self.eq_patq(p, q)
  # Convert p to hex, raise an exception if not valid
  phex = begin
    Aura::Q.patq2hex(p)
  rescue ArgumentError
    raise ArgumentError, "eq_patq: not a valid @q"
  end

  # Convert q to hex, raise an exception if not valid
  qhex = begin
    Aura::Q.patq2hex(q)
  rescue ArgumentError
    raise ArgumentError, "eq_patq: not a valid @q"
  end

  # Assuming eq_mod_leading_zero_bytes is defined elsewhere in your Ruby code
  eq_mod_leading_zero_bytes(phex, qhex)
end

.hex2patq(arg) ⇒ Object

Convert a hex-encoded string to a @q-encoded string.

Note that this preserves leading zero bytes.

Raises:

  • (ArgumentError)


255
256
257
258
259
260
261
262
# File 'lib/aura.rb', line 255

def self.hex2patq(arg)
  raise ArgumentError, "hex2patq: input must be a string" unless arg.is_a?(String)

  arg = arg.delete_prefix("0x")
  hex = arg.length.odd? ? arg.rjust(arg.length + 1, "0") : arg
  buf = hex.to_s.scan(/../).map(&:hex)
  buf2patq(buf)
end

.name(byts) ⇒ Object



326
327
328
# File 'lib/aura.rb', line 326

def name(byts)
  byts[1].nil? ? SUFFIXES[byts[0]] : PREFIXES[byts[0]] + SUFFIXES[byts[1]]
end

.patq(arg) ⇒ Object

Convert a number to a @q-encoded string.



232
233
234
235
236
237
# File 'lib/aura.rb', line 232

def self.patq(arg)
  n = arg.to_i
  hex = n.to_s(16)
  buf = hex.rjust((hex.length + 1) & ~1, "0").scan(/../).map(&:hex)
  buf2patq(buf)
end

.patq2dec(name) ⇒ Object

Convert a @q-encoded string to an integer.



265
266
267
# File 'lib/aura.rb', line 265

def self.patq2dec(name)
  patq2hex(name).to_i(16)
end

.patq2hex(name) ⇒ Object

Convert a @q-encoded string to a hex-encoded string.

Note that this preservers leading zero bytes.

Raises:

  • (ArgumentError)


272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/aura.rb', line 272

def self.patq2hex(name)
  raise ArgumentError, "patq2hex: not a valid @q" unless valid_pat?(name)

  chunks = name.delete_prefix("~").split("-")
  dec2hex = ->(dec) { format("%02x", dec) }
  splat = chunks.map do |chunk|
    syls = chunk[0..2], chunk[3..]
    if syls[1].nil? || syls[1].empty?
      dec2hex.call(SUFFIXES.index(syls[0]))
    else
      dec2hex.call(PREFIXES.index(syls[0])) + dec2hex.call(SUFFIXES.index(syls[1]))
    end
  end

  name.empty? ? "00" : splat.join("")
end

.prefix_name(byts) ⇒ Object



322
323
324
# File 'lib/aura.rb', line 322

def prefix_name(byts)
  byts[1].nil? ? PREFIXES[0] + SUFFIXES[byts[0]] : PREFIXES[byts[0]] + SUFFIXES[byts[1]]
end

.valid_patq?(str) ⇒ Boolean

Validate a @q-encoded string.

Returns:

  • (Boolean)


290
291
292
# File 'lib/aura.rb', line 290

def self.valid_patq?(str)
  valid_pat?(str) && eq_patq(str, patq(patq2dec(str)))
end