Class: LEON::StringBuffer
Instance Attribute Summary collapse
-
#buffer ⇒ Object
Returns the value of attribute buffer.
Class Method Summary collapse
- .bytes(v, type) ⇒ Object
- .bytes_to_double(bytes) ⇒ Object
- .bytes_to_float(bytes) ⇒ Object
- .complement(v, bits) ⇒ Object
- .concat(arr) ⇒ Object
Instance Method Summary collapse
- #equals(otherBuffer) ⇒ Object
- #fill(*args) ⇒ Object
- #get(offset) ⇒ Object
-
#initialize(*args) ⇒ StringBuffer
constructor
A new instance of StringBuffer.
- #length ⇒ Object
- #readDoubleBE(i) ⇒ Object
- #readDoubleLE(i) ⇒ Object
- #readFloatBE(i) ⇒ Object
- #readFloatLE(i) ⇒ Object
- #readInt16BE(i) ⇒ Object
- #readInt16LE(i) ⇒ Object
- #readInt32BE(i) ⇒ Object
- #readInt32LE(i) ⇒ Object
- #readInt8(i) ⇒ Object
- #readUInt16BE(i) ⇒ Object
- #readUInt16LE(i) ⇒ Object
- #readUInt32BE(i) ⇒ Object
- #readUInt32LE(i) ⇒ Object
- #readUInt8(i) ⇒ Object
- #slice(*args) ⇒ Object
- #to_s ⇒ Object
- #write(string, offset) ⇒ Object
- #writeDoubleBE(v, i) ⇒ Object
- #writeDoubleLE(v, i) ⇒ Object
- #writeFloatBE(v, i) ⇒ Object
- #writeFloatLE(v, i) ⇒ Object
- #writeInt16BE(v, i) ⇒ Object
- #writeInt16LE(v, i) ⇒ Object
- #writeInt32BE(v, i) ⇒ Object
- #writeInt32LE(v, i) ⇒ Object
- #writeInt8(v, i) ⇒ Object
- #writeUInt16BE(v, i) ⇒ Object
- #writeUInt16LE(v, i) ⇒ Object
- #writeUInt32BE(v, i) ⇒ Object
- #writeUInt32LE(v, i) ⇒ Object
- #writeUInt8(v, i) ⇒ Object
Constructor Details
#initialize(*args) ⇒ StringBuffer
Returns a new instance of StringBuffer.
114 115 116 117 118 119 120 |
# File 'lib/string-buffer.rb', line 114 def initialize(*args) if args.length > 0 @buffer = args[0] else @buffer = '' end end |
Instance Attribute Details
#buffer ⇒ Object
Returns the value of attribute buffer.
5 6 7 |
# File 'lib/string-buffer.rb', line 5 def buffer @buffer end |
Class Method Details
.bytes(v, type) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/string-buffer.rb', line 6 def self.bytes(v, type) ret = Array.new if type === Constants::UNSIGNED_CHAR ret.push v return ret end if type === Constants::CHAR return v < 0 ? StringBuffer.bytes(StringBuffer.complement(-v, 8), Constants::UNSIGNED_CHAR) : StringBuffer.bytes(v, Constants::UNSIGNED_CHAR) end if type === Constants::UNSIGNED_SHORT ret.push(v >> 8) ret.push(v & 0xFF) return ret end if type === Constants::SHORT return v < 0 ? StringBuffer.bytes(StringBuffer.complement(-v, 16), Constants::UNSIGNED_SHORT) : StringBuffer.bytes(v, Constants::UNSIGNED_SHORT) end if type === Constants::UNSIGNED_INT ret.push(v >> 24) ret.push((v >> 16) & 0xFF) ret.push((v >> 8) & 0xFF) ret.push(v & 0xFF) return ret end if type === Constants::INT return v < 0 ? StringBuffer.bytes(StringBuffer.complement(-v, 32), Constants::UNSIGNED_INT) : StringBuffer.bytes(v, Constants::UNSIGNED_INT) end if type === Constants::FLOAT exp = 127 if v < 0 sign = 1 else sign = 0 end v = v.abs log = Math::log(v)/Math::log(2) if log < 0 log = log.ceil else log = log.floor end v *= 2**(-log + 23) exp += log v = v.round v &= 0x7FFFFF ret.push(sign << 7) ret[0] |= ((exp & 0xFE) >> 1) ret.push((exp & 0x01) << 7) ret[1] |= ((v >> 16) & 0x7F) ret.push((v >> 8) & 0xFF) ret.push(v & 0xFF) return ret end if type === Constants::DOUBLE exp = 1023 if v < 0 sign = 1 else sign = 0 end v = v.abs log = Math::log(v)/Math::log(2) if log < 0 log = log.ceil else log = log.floor end v *= 2**(-log + 52) exp += log str = v.to_int.to_s(2) v = str[1..-1].to_i(2) ret.push(sign << 7) ret[0] |= (exp >> 4) ret.push((exp & 0x0F) << 4) ret[1] |= ((v*2**(-48)).floor & 0x0F) sh = 40 for i in 0..5 ret.push((v*2**(-sh)).floor & 0xFF) sh -= 8 end return ret end end |
.bytes_to_double(bytes) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/string-buffer.rb', line 100 def self.bytes_to_double(bytes) sign = (0x80 & bytes[0]) >> 7 exp = ((bytes[0] & 0x7F) << 4) | ((bytes[1] & 0xF0) >> 4) sig = 0 bytes[1] &= 0x0F for i in 0..6 sig += bytes[i + 1]*2**((6 - i)*8) end sig += 0x10000000000000 return ((sign === 1 ? -sig : sig)*2**(exp - (1023 + 52))).to_f end |
.bytes_to_float(bytes) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/string-buffer.rb', line 89 def self.bytes_to_float(bytes) sign = (0x80 & bytes[0]) >> 7 exp = ((bytes[0] & 0x7F) << 1) | ((bytes[1] & 0x80) >> 7) sig = 0 bytes[1] &= 0x7F for i in 0..2 sig |= bytes[1 + i]*2**((2 - i)*8) end sig |= 0x800000 return ((sign === 1 ? -sig : sig)*2**(exp - (127 + 23))).to_f end |
.complement(v, bits) ⇒ Object
111 112 113 |
# File 'lib/string-buffer.rb', line 111 def self.complement(v, bits) return (v ^ ((1 << bits) - 1)) + 1 end |
.concat(arr) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/string-buffer.rb', line 121 def self.concat(arr) return arr.reduce(self.new) { |r, v| r.buffer += v.buffer r } end |
Instance Method Details
#equals(otherBuffer) ⇒ Object
374 375 376 |
# File 'lib/string-buffer.rb', line 374 def equals(otherBuffer) return @buffer == otherBuffer.buffer end |
#fill(*args) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/string-buffer.rb', line 336 def fill(*args) if args.length > 2 endOffset = args[2] else endOffset = @buffer.length end if args.length > 1 offset = args[1] else offset = 0 end val = args[0] add = '' offset = normalize(offset) endOffset = normalize(endOffset) for i in offset..(endOffset - 1) add += val.chr end @buffer = @buffer[0..(offset - 1)] + add + @buffer[endOffset..-1] self end |
#get(offset) ⇒ Object
383 384 385 386 |
# File 'lib/string-buffer.rb', line 383 def get(offset) offset = normalize(offset, true) return @buffer[offset].ord end |
#length ⇒ Object
387 388 389 |
# File 'lib/string-buffer.rb', line 387 def length return @buffer.length end |
#readDoubleBE(i) ⇒ Object
325 326 327 328 329 330 331 332 |
# File 'lib/string-buffer.rb', line 325 def readDoubleBE(i) bytez = Array.new i = normalize(i, true) for j in 0..7 bytez.push readUInt8(i + j) end return StringBuffer.bytes_to_double(bytez) end |
#readDoubleLE(i) ⇒ Object
316 317 318 319 320 321 322 323 324 |
# File 'lib/string-buffer.rb', line 316 def readDoubleLE(i) bytez = Array.new i = normalize(i, true) for j in 0..7 bytez.push readUInt8(i + j) end bytez.reverse! return StringBuffer.bytes_to_double(bytez) end |
#readFloatBE(i) ⇒ Object
308 309 310 311 312 313 314 315 |
# File 'lib/string-buffer.rb', line 308 def readFloatBE(i) bytez = Array.new i = normalize(i, true) for j in 0..3 bytez.push readUInt8(i + j) end return StringBuffer.bytes_to_float(bytez) end |
#readFloatLE(i) ⇒ Object
299 300 301 302 303 304 305 306 307 |
# File 'lib/string-buffer.rb', line 299 def readFloatLE(i) bytez = Array.new i = normalize(i, true) for j in 0..3 bytez.push readUInt8(i + j) end bytez.reverse! return StringBuffer.bytes_to_float(bytez) end |
#readInt16BE(i) ⇒ Object
270 271 272 273 274 275 276 |
# File 'lib/string-buffer.rb', line 270 def readInt16BE(i) v = readUInt16BE(i) if (v & 0x8000) != 0 return -StringBuffer.complement(v, 16) end return v end |
#readInt16LE(i) ⇒ Object
263 264 265 266 267 268 269 |
# File 'lib/string-buffer.rb', line 263 def readInt16LE(i) v = readUInt16LE(i) if (v & 0x8000) != 0 return -StringBuffer.complement(v, 16) end return v end |
#readInt32BE(i) ⇒ Object
292 293 294 295 296 297 298 |
# File 'lib/string-buffer.rb', line 292 def readInt32BE(i) v = readUInt32BE(i) if (v & 0x80000000) != 0 return -StringBuffer.complement(v, 32) end return v end |
#readInt32LE(i) ⇒ Object
285 286 287 288 289 290 291 |
# File 'lib/string-buffer.rb', line 285 def readInt32LE(i) v = readUInt32LE(i) if (v & 0x80000000) != 0 return -StringBuffer.complement(v, 32) end return v end |
#readInt8(i) ⇒ Object
248 249 250 251 252 253 254 |
# File 'lib/string-buffer.rb', line 248 def readInt8(i) v = readUInt8(i) if (0x80 & v) != 0 return -StringBuffer.complement(v, 8) end return v end |
#readUInt16BE(i) ⇒ Object
259 260 261 262 |
# File 'lib/string-buffer.rb', line 259 def readUInt16BE(i) i = normalize(i, true) return (@buffer[i].ord << 8) | @buffer[i + 1].ord end |
#readUInt16LE(i) ⇒ Object
255 256 257 258 |
# File 'lib/string-buffer.rb', line 255 def readUInt16LE(i) i = normalize(i, true) return @buffer[i].ord | (@buffer[i + 1].ord << 8) end |
#readUInt32BE(i) ⇒ Object
281 282 283 284 |
# File 'lib/string-buffer.rb', line 281 def readUInt32BE(i) i = normalize(i, true) return (@buffer[i].ord << 24) | (@buffer[i + 1].ord << 16) | (@buffer[i + 2].ord << 8) | @buffer[i + 3].ord end |
#readUInt32LE(i) ⇒ Object
277 278 279 280 |
# File 'lib/string-buffer.rb', line 277 def readUInt32LE(i) i = normalize(i, true) return @buffer[i].ord | (@buffer[i + 1].ord << 8) | (@buffer[i + 2].ord << 16) | (@buffer[i + 3].ord << 24) end |
#readUInt8(i) ⇒ Object
244 245 246 247 |
# File 'lib/string-buffer.rb', line 244 def readUInt8(i) i = normalize(i, true) return @buffer[i].ord end |
#slice(*args) ⇒ Object
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/string-buffer.rb', line 357 def slice(*args) if args.length > 1 endOffset = args[1] else endOffset = @buffer.length end if args.length > 0 start = args[0] else start = 0 end endOffset = normalize(endOffset, true) start = normalize(start, true) ret = LEON::StringBuffer.new ret.buffer = @buffer[start...(endOffset - start)] ret end |
#to_s ⇒ Object
333 334 335 |
# File 'lib/string-buffer.rb', line 333 def to_s() return @buffer end |
#write(string, offset) ⇒ Object
377 378 379 380 381 382 |
# File 'lib/string-buffer.rb', line 377 def write(string, offset) offset = normalize(offset) for i in 0...string.length writeUInt8(string[i].ord, offset + i) end end |
#writeDoubleBE(v, i) ⇒ Object
236 237 238 239 240 241 242 243 |
# File 'lib/string-buffer.rb', line 236 def writeDoubleBE(v, i) i = normalize(i) bytez = StringBuffer.bytes(v, Constants::DOUBLE) bytez.to_enum.with_index.each { |v, idx| writeUInt8(v, i + idx) } return i + 8 end |
#writeDoubleLE(v, i) ⇒ Object
228 229 230 231 232 233 234 235 |
# File 'lib/string-buffer.rb', line 228 def writeDoubleLE(v, i) i = normalize(i) bytez = StringBuffer.bytes(v, Constants::DOUBLE) bytez.to_enum.with_index.reverse_each { |v, idx| writeUInt8(v, i + (7 - idx)) } return i + 8 end |
#writeFloatBE(v, i) ⇒ Object
220 221 222 223 224 225 226 227 |
# File 'lib/string-buffer.rb', line 220 def writeFloatBE(v, i) i = normalize(i) bytez = StringBuffer.bytes(v, Constants::FLOAT) bytez.to_enum.with_index.each { |v, idx| writeUInt8(v, i + idx) } return i + 4 end |
#writeFloatLE(v, i) ⇒ Object
212 213 214 215 216 217 218 219 |
# File 'lib/string-buffer.rb', line 212 def writeFloatLE(v, i) bytez = StringBuffer.bytes(v, Constants::FLOAT) i = normalize(i) bytez.to_enum.with_index.reverse_each { |v, idx| writeUInt8(v, i + (3 - idx)) } return i + 4 end |
#writeInt16BE(v, i) ⇒ Object
172 173 174 175 |
# File 'lib/string-buffer.rb', line 172 def writeInt16BE(v, i) v = (v < 0 ? StringBuffer.complement(-v, 16) : v) return writeUInt16BE(v, i) end |
#writeInt16LE(v, i) ⇒ Object
168 169 170 171 |
# File 'lib/string-buffer.rb', line 168 def writeInt16LE(v, i) v = (v < 0 ? StringBuffer.complement(-v, 16) : v) return writeUInt16LE(v, i) end |
#writeInt32BE(v, i) ⇒ Object
208 209 210 211 |
# File 'lib/string-buffer.rb', line 208 def writeInt32BE(v, i) v = (v < 0 ? StringBuffer.complement(-v, i) : v) return writeUInt32BE(v, i) end |
#writeInt32LE(v, i) ⇒ Object
204 205 206 207 |
# File 'lib/string-buffer.rb', line 204 def writeInt32LE(v, i) v = (v < 0 ? StringBuffer.complement(-v, i) : v) return writeUInt32LE(v, i) end |
#writeInt8(v, i) ⇒ Object
136 137 138 139 |
# File 'lib/string-buffer.rb', line 136 def writeInt8(v, i) v = (v < 0 ? StringBuffer.complement(-v, 8) : v) return writeUInt8(v, i) end |
#writeUInt16BE(v, i) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/string-buffer.rb', line 154 def writeUInt16BE(v, i) bytez = StringBuffer.bytes(v, Constants::UNSIGNED_SHORT) add = '' bytez.each { |v| add += "#{v.chr}" } i = normalize(i) if i >= @buffer.length @buffer += add else @buffer = @buffer[0..i] + add + @buffer[(i + 2)..-1] end return i + 2 end |
#writeUInt16LE(v, i) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/string-buffer.rb', line 140 def writeUInt16LE(v, i) bytez = StringBuffer.bytes(v, Constants::UNSIGNED_SHORT) add = '' bytez.reverse_each { |v| add += "#{v.chr}" } i = normalize(i) if i >= @buffer.length @buffer += add else @buffer = @buffer[0..i] + add + @buffer[(i + 2)..-1] end return i + 2 end |
#writeUInt32BE(v, i) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/string-buffer.rb', line 190 def writeUInt32BE(v, i) bytez = StringBuffer.bytes(v, Constants::UNSIGNED_INT) add = '' bytez.each { |v| add += "#{v.chr}" } i = normalize(i) if i >= @buffer.length @buffer += add else @buffer = @buffer[0..i] + add + @buffer[(i + 4)..-1] end return i + 4 end |
#writeUInt32LE(v, i) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/string-buffer.rb', line 176 def writeUInt32LE(v, i) bytez = StringBuffer.bytes(v, Constants::UNSIGNED_INT) add = '' bytez.reverse_each { |v| add += "#{v.chr}" } i = normalize(i) if i >= @buffer.length @buffer += add else @buffer = @buffer[0..i] + add + @buffer[(i + 4)..-1] end return i + 4 end |
#writeUInt8(v, i) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/string-buffer.rb', line 127 def writeUInt8(v, i) i = normalize(i) if i >= @buffer.length @buffer += v.chr else @buffer = @buffer[0..i] + v.chr + @buffer[(i+1)..-1] end return i + 1 end |