Module: LZString::Uint8Array
- Included in:
- LZString
- Defined in:
- lib/lzstring/uint8_array.rb
Overview
Module for Uint8Array encoding/decoding
Class Method Summary collapse
-
.compress_to_uint8_array(input) ⇒ Array<Integer>
Compress a string to a Uint8Array.
-
.convert_from_uint8_array(uint8array, legacy = false) ⇒ String
Convert a Uint8Array to a string.
-
.convert_to_uint8_array(string, _legacy = false) ⇒ Array<Integer>
Convert a string to a Uint8Array.
-
.decompress_from_uint8_array(uint8array, legacy = false) ⇒ String?
Decompress a Uint8Array to a string.
Class Method Details
.compress_to_uint8_array(input) ⇒ Array<Integer>
Compress a string to a Uint8Array
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/lzstring/uint8_array.rb', line 7 def self.compress_to_uint8_array(input) return [] if input.nil? || input.empty? # Compress the string compressed = LZString.compress(input) return [] if compressed.nil? || compressed.empty? # Calculate buffer length buf_len = compressed.length * 2 # Create the result array result = Array.new(buf_len, 0) # Convert the compressed string to an array of bytes compressed.each_char.with_index do |char, i| code = char.ord result[i * 2] = (code >> 8) & 0xFF # High byte result[(i * 2) + 1] = code & 0xFF # Low byte end result end |
.convert_from_uint8_array(uint8array, legacy = false) ⇒ String
Convert a Uint8Array to a string
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 |
# File 'lib/lzstring/uint8_array.rb', line 34 def self.convert_from_uint8_array(uint8array, legacy = false) return "" if uint8array.nil? || uint8array.empty? # Ensure the array has an even length for legacy mode if legacy && uint8array.length.odd? return nil # Legacy mode requires even length end # Use original length for modern mode, ensure even length for legacy mode length = if legacy uint8array.length else uint8array.length.even? ? uint8array.length : uint8array.length - 1 end # Combine pairs of bytes to form characters result = "" i = 0 while i < length begin high_byte = uint8array[i] || 0 low_byte = uint8array[i + 1] || 0 value = (high_byte << 8) | low_byte # Convert to character with error handling begin result += begin value.chr(Encoding::UTF_8) rescue (value % 256).chr(Encoding::UTF_8) end rescue "?" end i += 2 rescue # Skip problematic pairs i += 2 end end result end |
.convert_to_uint8_array(string, _legacy = false) ⇒ Array<Integer>
Convert a string to a Uint8Array
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/lzstring/uint8_array.rb', line 83 def self.convert_to_uint8_array(string, _legacy = false) return [] if string.nil? || string.empty? # Ensure the string has proper encoding string = string.dup.force_encoding(Encoding::UTF_8) # Create the result array result = Array.new(string.length * 2, 0) # Convert each character to a pair of bytes string.each_char.with_index do |char, i| code = char.ord result[i * 2] = (code >> 8) & 0xFF # High byte result[(i * 2) + 1] = code & 0xFF # Low byte rescue # Use fallback for problematic characters result[i * 2] = 0 result[(i * 2) + 1] = "?".ord end result end |
.decompress_from_uint8_array(uint8array, legacy = false) ⇒ String?
Decompress a Uint8Array to a string
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/lzstring/uint8_array.rb', line 110 def self.decompress_from_uint8_array(uint8array, legacy = false) return "" if uint8array.nil? || uint8array.empty? # Ensure all array elements are valid bytes uint8array = uint8array.map { |byte| byte.is_a?(Integer) ? byte & 0xFF : 0 } # Check for even length in legacy mode return nil if legacy && uint8array.length.odd? begin # Convert Uint8Array to string format first compressed = convert_from_uint8_array(uint8array, legacy) return nil if compressed.nil? || compressed.empty? # Then decompress LZString.decompress(compressed) rescue nil end end |