Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rbmysql/packet.rb
Views: 11766
# coding: ascii-8bit1class RbMysql2class Packet3# convert Numeric to LengthCodedBinary4def self.lcb(num)5return "\xfb" if num.nil?6return [num].pack("C") if num < 2517return [252, num].pack("Cv") if num < 655368return [253, num&0xffff, num>>16].pack("CvC") if num < 167772169return [254, num&0xffffffff, num>>32].pack("CVV")10end1112# convert String to LengthCodedString13def self.lcs(str)14str = Charset.to_binary str.dup15lcb(str.length)+str16end1718def initialize(data)19@data = data20end2122def lcb23return nil if @data.empty?24case v = utiny25when 0xfb26return nil27when 0xfc28return ushort29when 0xfd30c, v = utiny, ushort31return (v << 8)+c32when 0xfe33v1, v2 = ulong, ulong34return (v2 << 32)+v135else36return v37end38end3940def lcs41len = self.lcb42return nil unless len43@data.slice!(0, len)44end4546def read(len)47@data.slice!(0, len)48end4950def string51str = @data.unpack('Z*').first52@data.slice!(0, str.length+1)53str54end5556def utiny57@data.slice!(0, 1).unpack('C').first58end5960def ushort61@data.slice!(0, 2).unpack('v').first62end6364def ulong65@data.slice!(0, 4).unpack('V').first66end6768def eof?69@data[0] == ?\xfe && @data.length == 570end7172def to_s73@data74end7576end77end7879