Module | Thrift::Bytes |
In: |
lib/thrift/bytes.rb
lib/thrift/bytes.rb |
A collection of utilities for working with bytes and byte buffers.
# File lib/thrift/bytes.rb, line 125 125: def self.convert_to_string(utf8_buffer) 126: # See comment in 'convert_to_utf8_byte_buffer' for relevant assumptions. 127: utf8_buffer 128: end
# File lib/thrift/bytes.rb, line 125 125: def self.convert_to_string(utf8_buffer) 126: # See comment in 'convert_to_utf8_byte_buffer' for relevant assumptions. 127: utf8_buffer 128: end
Converts the given UTF-8 byte buffer into a String
utf8_buffer - A String, with BINARY encoding, containing UTF-8 bytes
Returns a new String with UTF-8 encoding,
# File lib/thrift/bytes.rb, line 94 94: def self.convert_to_string(utf8_buffer) 95: # duplicate the buffer, force encoding to UTF-8 96: utf8_buffer.dup.force_encoding(Encoding::UTF_8) 97: end
Converts the given UTF-8 byte buffer into a String
utf8_buffer - A String, with BINARY encoding, containing UTF-8 bytes
Returns a new String with UTF-8 encoding,
# File lib/thrift/bytes.rb, line 94 94: def self.convert_to_string(utf8_buffer) 95: # duplicate the buffer, force encoding to UTF-8 96: utf8_buffer.dup.force_encoding(Encoding::UTF_8) 97: end
Converts the given String to a UTF-8 byte buffer.
string - The String to convert.
Returns a new String with BINARY encoding, containing the UTF-8 bytes of the original string.
# File lib/thrift/bytes.rb, line 78 78: def self.convert_to_utf8_byte_buffer(string) 79: if string.encoding != Encoding::UTF_8 80: # transcode to UTF-8 81: string = string.encode(Encoding::UTF_8) 82: else 83: # encoding is already UTF-8, but a duplicate is needed 84: string = string.dup 85: end 86: string.force_encoding(Encoding::BINARY) 87: end
Converts the given String to a UTF-8 byte buffer.
string - The String to convert.
Returns a new String with BINARY encoding, containing the UTF-8 bytes of the original string.
# File lib/thrift/bytes.rb, line 78 78: def self.convert_to_utf8_byte_buffer(string) 79: if string.encoding != Encoding::UTF_8 80: # transcode to UTF-8 81: string = string.encode(Encoding::UTF_8) 82: else 83: # encoding is already UTF-8, but a duplicate is needed 84: string = string.dup 85: end 86: string.force_encoding(Encoding::BINARY) 87: end
# File lib/thrift/bytes.rb, line 119 119: def self.convert_to_utf8_byte_buffer(string) 120: # This assumes $KCODE is 'UTF8'/'U', which would mean the String is already a UTF-8 byte buffer 121: # TODO consider handling other $KCODE values and transcoding with iconv 122: string 123: end
# File lib/thrift/bytes.rb, line 119 119: def self.convert_to_utf8_byte_buffer(string) 120: # This assumes $KCODE is 'UTF8'/'U', which would mean the String is already a UTF-8 byte buffer 121: # TODO consider handling other $KCODE values and transcoding with iconv 122: string 123: end
Creates and empty byte buffer (String with BINARY encoding)
size - The Integer size of the buffer (default: nil) to create
Returns a String with BINARY encoding, filled with null characters if size is greater than zero
# File lib/thrift/bytes.rb, line 31 31: def self.empty_byte_buffer(size = nil) 32: if (size && size > 0) 33: "\0".force_encoding(Encoding::BINARY) * size 34: else 35: ''.force_encoding(Encoding::BINARY) 36: end 37: end
Creates and empty byte buffer (String with BINARY encoding)
size - The Integer size of the buffer (default: nil) to create
Returns a String with BINARY encoding, filled with null characters if size is greater than zero
# File lib/thrift/bytes.rb, line 31 31: def self.empty_byte_buffer(size = nil) 32: if (size && size > 0) 33: "\0".force_encoding(Encoding::BINARY) * size 34: else 35: ''.force_encoding(Encoding::BINARY) 36: end 37: end
# File lib/thrift/bytes.rb, line 99 99: def self.empty_byte_buffer(size = nil) 100: if (size && size > 0) 101: "\0" * size 102: else 103: '' 104: end 105: end
# File lib/thrift/bytes.rb, line 99 99: def self.empty_byte_buffer(size = nil) 100: if (size && size > 0) 101: "\0" * size 102: else 103: '' 104: end 105: end
# File lib/thrift/bytes.rb, line 107 107: def self.force_binary_encoding(buffer) 108: buffer 109: end
Forces the encoding of the buffer to BINARY. If the buffer passed is frozen, then it will be duplicated.
buffer - The String to force the encoding of.
Returns the String passed with an encoding of BINARY; returned String may be a duplicate.
# File lib/thrift/bytes.rb, line 46 46: def self.force_binary_encoding(buffer) 47: buffer = buffer.dup if buffer.frozen? 48: buffer.force_encoding(Encoding::BINARY) 49: end
Forces the encoding of the buffer to BINARY. If the buffer passed is frozen, then it will be duplicated.
buffer - The String to force the encoding of.
Returns the String passed with an encoding of BINARY; returned String may be a duplicate.
# File lib/thrift/bytes.rb, line 46 46: def self.force_binary_encoding(buffer) 47: buffer = buffer.dup if buffer.frozen? 48: buffer.force_encoding(Encoding::BINARY) 49: end
# File lib/thrift/bytes.rb, line 107 107: def self.force_binary_encoding(buffer) 108: buffer 109: end
# File lib/thrift/bytes.rb, line 111 111: def self.get_string_byte(string, index) 112: string[index] 113: end
Gets the byte value of a given position in a String.
string - The String to retrive the byte value from. index - The Integer location of the byte value to retrieve.
Returns an Integer value between 0 and 255.
# File lib/thrift/bytes.rb, line 57 57: def self.get_string_byte(string, index) 58: string.getbyte(index) 59: end
# File lib/thrift/bytes.rb, line 111 111: def self.get_string_byte(string, index) 112: string[index] 113: end
Gets the byte value of a given position in a String.
string - The String to retrive the byte value from. index - The Integer location of the byte value to retrieve.
Returns an Integer value between 0 and 255.
# File lib/thrift/bytes.rb, line 57 57: def self.get_string_byte(string, index) 58: string.getbyte(index) 59: end
Sets the byte value given to a given index in a String.
string - The String to set the byte value in. index - The Integer location to set the byte value at. byte - The Integer value (0 to 255) to set in the string.
Returns an Integer value of the byte value to set.
# File lib/thrift/bytes.rb, line 68 68: def self.set_string_byte(string, index, byte) 69: string.setbyte(index, byte) 70: end
# File lib/thrift/bytes.rb, line 115 115: def self.set_string_byte(string, index, byte) 116: string[index] = byte 117: end
Sets the byte value given to a given index in a String.
string - The String to set the byte value in. index - The Integer location to set the byte value at. byte - The Integer value (0 to 255) to set in the string.
Returns an Integer value of the byte value to set.
# File lib/thrift/bytes.rb, line 68 68: def self.set_string_byte(string, index, byte) 69: string.setbyte(index, byte) 70: end