Skip to content

Commit d24b8a0

Browse files
kpumukJens-G
authored andcommitted
Cleaned up code supporting Ruby < 2.0
1 parent ded241d commit d24b8a0

File tree

7 files changed

+264
-408
lines changed

7 files changed

+264
-408
lines changed

lib/rb/lib/thrift/bytes.rb

Lines changed: 68 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# encoding: ascii-8bit
2-
#
2+
#
33
# Licensed to the Apache Software Foundation (ASF) under one
44
# or more contributor license agreements. See the NOTICE file
55
# distributed with this work for additional information
66
# regarding copyright ownership. The ASF licenses this file
77
# to you under the Apache License, Version 2.0 (the
88
# "License"); you may not use this file except in compliance
99
# with the License. You may obtain a copy of the License at
10-
#
10+
#
1111
# http://www.apache.org/licenses/LICENSE-2.0
12-
#
12+
#
1313
# Unless required by applicable law or agreed to in writing,
1414
# software distributed under the License is distributed on an
1515
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -21,111 +21,78 @@
2121
module Thrift
2222
# A collection of utilities for working with bytes and byte buffers.
2323
module Bytes
24-
if RUBY_VERSION >= '1.9'
25-
# Creates and empty byte buffer (String with BINARY encoding)
26-
#
27-
# size - The Integer size of the buffer (default: nil) to create
28-
#
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)
32-
if (size && size > 0)
33-
"\0".b * size
34-
else
35-
"".b
36-
end
37-
end
38-
39-
# Forces the encoding of the buffer to BINARY. If the buffer
40-
# passed is frozen, then it will be duplicated.
41-
#
42-
# buffer - The String to force the encoding of.
43-
#
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)
49-
end
50-
51-
# Gets the byte value of a given position in a String.
52-
#
53-
# string - The String to retrive the byte value from.
54-
# index - The Integer location of the byte value to retrieve.
55-
#
56-
# Returns an Integer value between 0 and 255.
57-
def self.get_string_byte(string, index)
58-
string.getbyte(index)
59-
end
60-
61-
# Sets the byte value given to a given index in a String.
62-
#
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.
66-
#
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)
70-
end
71-
72-
# Converts the given String to a UTF-8 byte buffer.
73-
#
74-
# string - The String to convert.
75-
#
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
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
88-
89-
# Converts the given UTF-8 byte buffer into a String
90-
#
91-
# utf8_buffer - A String, with BINARY encoding, containing UTF-8 bytes
92-
#
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)
97-
end
98-
else
99-
def self.empty_byte_buffer(size = nil)
100-
if (size && size > 0)
101-
"\0" * size
102-
else
103-
''
104-
end
24+
# Creates and empty byte buffer (String with BINARY encoding)
25+
#
26+
# size - The Integer size of the buffer (default: nil) to create
27+
#
28+
# Returns a String with BINARY encoding, filled with null characters
29+
# if size is greater than zero
30+
def self.empty_byte_buffer(size = nil)
31+
if (size && size > 0)
32+
"\0".b * size
33+
else
34+
''.b
10535
end
36+
end
10637

107-
def self.force_binary_encoding(buffer)
108-
buffer
109-
end
38+
# Forces the encoding of the buffer to BINARY. If the buffer
39+
# passed is frozen, then it will be duplicated.
40+
#
41+
# buffer - The String to force the encoding of.
42+
#
43+
# Returns the String passed with an encoding of BINARY; returned
44+
# String may be a duplicate.
45+
def self.force_binary_encoding(buffer)
46+
buffer = buffer.dup if buffer.frozen?
47+
buffer.force_encoding(Encoding::BINARY)
48+
end
11049

111-
def self.get_string_byte(string, index)
112-
string[index]
113-
end
50+
# Gets the byte value of a given position in a String.
51+
#
52+
# string - The String to retrive the byte value from.
53+
# index - The Integer location of the byte value to retrieve.
54+
#
55+
# Returns an Integer value between 0 and 255.
56+
def self.get_string_byte(string, index)
57+
string.getbyte(index)
58+
end
11459

115-
def self.set_string_byte(string, index, byte)
116-
string[index] = byte
117-
end
60+
# Sets the byte value given to a given index in a String.
61+
#
62+
# string - The String to set the byte value in.
63+
# index - The Integer location to set the byte value at.
64+
# byte - The Integer value (0 to 255) to set in the string.
65+
#
66+
# Returns an Integer value of the byte value to set.
67+
def self.set_string_byte(string, index, byte)
68+
string.setbyte(index, byte)
69+
end
11870

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
71+
# Converts the given String to a UTF-8 byte buffer.
72+
#
73+
# string - The String to convert.
74+
#
75+
# Returns a new String with BINARY encoding, containing the UTF-8
76+
# bytes of the original string.
77+
def self.convert_to_utf8_byte_buffer(string)
78+
if string.encoding != Encoding::UTF_8
79+
# transcode to UTF-8
80+
string = string.encode(Encoding::UTF_8)
81+
else
82+
# encoding is already UTF-8, but a duplicate is needed
83+
string = string.dup
12384
end
85+
string.force_encoding(Encoding::BINARY)
86+
end
12487

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
88+
# Converts the given UTF-8 byte buffer into a String
89+
#
90+
# utf8_buffer - A String, with BINARY encoding, containing UTF-8 bytes
91+
#
92+
# Returns a new String with UTF-8 encoding,
93+
def self.convert_to_string(utf8_buffer)
94+
# duplicate the buffer, force encoding to UTF-8
95+
utf8_buffer.dup.force_encoding(Encoding::UTF_8)
12996
end
13097
end
13198
end

lib/rb/lib/thrift/protocol/base_protocol.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
#
1+
#
22
# Licensed to the Apache Software Foundation (ASF) under one
33
# or more contributor license agreements. See the NOTICE file
44
# distributed with this work for additional information
55
# regarding copyright ownership. The ASF licenses this file
66
# to you under the Apache License, Version 2.0 (the
77
# "License"); you may not use this file except in compliance
88
# with the License. You may obtain a copy of the License at
9-
#
9+
#
1010
# http://www.apache.org/licenses/LICENSE-2.0
11-
#
11+
#
1212
# Unless required by applicable law or agreed to in writing,
1313
# software distributed under the License is distributed on an
1414
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1515
# KIND, either express or implied. See the License for the
1616
# specific language governing permissions and limitations
1717
# under the License.
18-
#
18+
#
1919

2020
# this require is to make generated struct definitions happy
2121
require 'set'
@@ -116,7 +116,7 @@ def write_double(dub)
116116
raise NotImplementedError
117117
end
118118

119-
# Writes a Thrift String. In Ruby 1.9+, the String passed will be transcoded to UTF-8.
119+
# Writes a Thrift String. The String passed will be transcoded to UTF-8.
120120
#
121121
# str - The String to write.
122122
#
@@ -127,7 +127,7 @@ def write_string(str)
127127
raise NotImplementedError
128128
end
129129

130-
# Writes a Thrift Binary (Thrift String with no encoding). In Ruby 1.9+, the String passed
130+
# Writes a Thrift Binary (Thrift String with no encoding). The String passed
131131
# will forced into BINARY encoding.
132132
#
133133
# buf - The String to write.
@@ -206,14 +206,14 @@ def read_double
206206
raise NotImplementedError
207207
end
208208

209-
# Reads a Thrift String. In Ruby 1.9+, all Strings will be returned with an Encoding of UTF-8.
209+
# Reads a Thrift String. All Strings will be returned with an Encoding of UTF-8.
210210
#
211211
# Returns a String.
212212
def read_string
213213
raise NotImplementedError
214214
end
215215

216-
# Reads a Thrift Binary (Thrift String without encoding). In Ruby 1.9+, all Strings will be returned
216+
# Reads a Thrift Binary (Thrift String without encoding). All Strings will be returned
217217
# with an Encoding of BINARY.
218218
#
219219
# Returns a String.
@@ -391,7 +391,7 @@ def skip(type)
391391
raise ProtocolException.new(ProtocolException::INVALID_DATA, 'Invalid data')
392392
end
393393
end
394-
394+
395395
def to_s
396396
"#{trans.to_s}"
397397
end
@@ -401,7 +401,7 @@ class BaseProtocolFactory
401401
def get_protocol(trans)
402402
raise NotImplementedError
403403
end
404-
404+
405405
def to_s
406406
"base"
407407
end

lib/rb/lib/thrift/protocol/json_protocol.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# encoding: UTF-8
2-
#
2+
#
33
# Licensed to the Apache Software Foundation (ASF) under one
44
# or more contributor license agreements. See the NOTICE file
55
# distributed with this work for additional information
66
# regarding copyright ownership. The ASF licenses this file
77
# to you under the Apache License, Version 2.0 (the
88
# "License"); you may not use this file except in compliance
99
# with the License. You may obtain a copy of the License at
10-
#
10+
#
1111
# http://www.apache.org/licenses/LICENSE-2.0
12-
#
12+
#
1313
# Unless required by applicable law or agreed to in writing,
1414
# software distributed under the License is distributed on an
1515
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -504,11 +504,7 @@ def read_json_escape_char
504504
str += @reader.read
505505
str += @reader.read
506506
str += @reader.read
507-
if RUBY_VERSION >= '1.9'
508-
str.hex.chr(Encoding::UTF_8)
509-
else
510-
str.hex.chr
511-
end
507+
str.hex.chr(Encoding::UTF_8)
512508
end
513509

514510
# Decodes a JSON string, including unescaping, and returns the string via str

lib/rb/lib/thrift/transport/base_transport.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# encoding: ascii-8bit
2-
#
2+
#
33
# Licensed to the Apache Software Foundation (ASF) under one
44
# or more contributor license agreements. See the NOTICE file
55
# distributed with this work for additional information
66
# regarding copyright ownership. The ASF licenses this file
77
# to you under the Apache License, Version 2.0 (the
88
# "License"); you may not use this file except in compliance
99
# with the License. You may obtain a copy of the License at
10-
#
10+
#
1111
# http://www.apache.org/licenses/LICENSE-2.0
12-
#
12+
#
1313
# Unless required by applicable law or agreed to in writing,
1414
# software distributed under the License is distributed on an
1515
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1616
# KIND, either express or implied. See the License for the
1717
# specific language governing permissions and limitations
1818
# under the License.
19-
#
19+
#
2020

2121
module Thrift
2222
class TransportException < Exception
@@ -48,12 +48,12 @@ def self.set_string_byte(string, index, byte)
4848

4949
class BaseTransport
5050
def open?; end
51-
51+
5252
def open; end
5353

5454
def close; end
5555

56-
# Reads a number of bytes from the transports. In Ruby 1.9+, the String returned will have a BINARY (aka ASCII8BIT) encoding.
56+
# Reads a number of bytes from the transports. The String returned will have a BINARY (aka ASCII-8BIT) encoding.
5757
#
5858
# sz - The number of bytes to read from the transport.
5959
#
@@ -86,11 +86,11 @@ def read_all(size)
8686
chunk = read(size - buf.length)
8787
buf << chunk
8888
end
89-
89+
9090
buf
9191
end
9292

93-
# Writes the byte buffer to the transport. In Ruby 1.9+, the buffer will be forced into BINARY encoding.
93+
# Writes the byte buffer to the transport. The buffer will be forced into BINARY encoding.
9494
#
9595
# buf - A String acting as a byte buffer.
9696
#
@@ -104,12 +104,12 @@ def to_s
104104
"base"
105105
end
106106
end
107-
107+
108108
class BaseTransportFactory
109109
def get_transport(trans)
110110
return trans
111111
end
112-
112+
113113
def to_s
114114
"base"
115115
end

0 commit comments

Comments
 (0)