]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/rb/lib/thrift/bytes.rb
3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing,
14 # software distributed under the License is distributed on an
15 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 # KIND, either express or implied. See the License for the
17 # specific language governing permissions and limitations
22 # A collection of utilities for working with bytes and byte buffers.
24 if RUBY_VERSION >= '1.9'
25 # Creates and empty byte buffer (String with BINARY encoding)
27 # size - The Integer size of the buffer (default: nil) to create
29 # Returns a String with BINARY encoding, filled with null characters
30 # if size is greater than zero
31 def self.empty_byte_buffer(size
= nil)
33 "\0".force_encoding(Encoding
::BINARY) * size
35 ''.force_encoding(Encoding
::BINARY)
39 # Forces the encoding of the buffer to BINARY. If the buffer
40 # passed is frozen, then it will be duplicated.
42 # buffer - The String to force the encoding of.
44 # Returns the String passed with an encoding of BINARY; returned
45 # String may be a duplicate.
46 def self.force_binary_encoding(buffer
)
47 buffer
= buffer
.dup
if buffer
.frozen
?
48 buffer
.force_encoding(Encoding
::BINARY)
51 # Gets the byte value of a given position in a String.
53 # string - The String to retrive the byte value from.
54 # index - The Integer location of the byte value to retrieve.
56 # Returns an Integer value between 0 and 255.
57 def self.get_string_byte(string
, index
)
61 # Sets the byte value given to a given index in a String.
63 # string - The String to set the byte value in.
64 # index - The Integer location to set the byte value at.
65 # byte - The Integer value (0 to 255) to set in the string.
67 # Returns an Integer value of the byte value to set.
68 def self.set_string_byte(string
, index
, byte
)
69 string
.setbyte(index
, byte
)
72 # Converts the given String to a UTF-8 byte buffer.
74 # string - The String to convert.
76 # Returns a new String with BINARY encoding, containing the UTF-8
77 # bytes of the original string.
78 def self.convert_to_utf8_byte_buffer(string
)
79 if string
.encoding
!= Encoding
::UTF_8
81 string
= string
.encode(Encoding
::UTF_8)
83 # encoding is already UTF-8, but a duplicate is needed
86 string
.force_encoding(Encoding
::BINARY)
89 # Converts the given UTF-8 byte buffer into a String
91 # utf8_buffer - A String, with BINARY encoding, containing UTF-8 bytes
93 # Returns a new String with UTF-8 encoding,
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)
99 def self.empty_byte_buffer(size
= nil)
100 if (size
&& size
> 0)
107 def self.force_binary_encoding(buffer
)
111 def self.get_string_byte(string
, index
)
115 def self.set_string_byte(string
, index
, byte
)
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
125 def self.convert_to_string(utf8_buffer
)
126 # See comment in 'convert_to_utf8_byte_buffer' for relevant assumptions.