diff options
author | Lars Kanis <lars@greiz-reinsdorf.de> | 2020-12-19 21:36:04 +0100 |
---|---|---|
committer | Lars Kanis <lars@greiz-reinsdorf.de> | 2020-12-19 21:48:21 +0100 |
commit | 82602ec1e5aa800f049d3a6f6ab3b54f01d90394 (patch) | |
tree | c1e9c61ff993869bb977c159a0c7d991e04b844d /lib | |
parent | 0d8e5815716eeeb8fad11205646dfedd8bf23f03 (diff) | |
download | ffi-82602ec1e5aa800f049d3a6f6ab3b54f01d90394.tar.gz |
Revert "Merge pull request #806 from eregon/fix-write_string"
.. but add test cases for write_sting semantics.
This reverts commit 2b44904f8323dcad3eaa978c1fd2b5298bd04663, reversing
changes made to 2918fdfc7fde6456d5ddc1090a783de0e82d6e39.
Fixes #857
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ffi/pointer.rb | 28 |
1 files changed, 7 insertions, 21 deletions
diff --git a/lib/ffi/pointer.rb b/lib/ffi/pointer.rb index 43a547b..a5ee655 100644 --- a/lib/ffi/pointer.rb +++ b/lib/ffi/pointer.rb @@ -85,37 +85,23 @@ module FFI # @param [String] str string to write # @param [Numeric] len length of string to return # @return [self] - # Write +len+ first bytes of +str+ in pointer's contents and a final \0 byte. + # Write +len+ first bytes of +str+ in pointer's contents. # # Same as: # ptr.write_string(str, len) # with len not nil def write_string_length(str, len) - write_string(str, len) + put_bytes(0, str, 0, len) end unless method_defined?(:write_string_length) # @param [String] str string to write # @param [Numeric] len length of string to return # @return [self] - # Write +str+ in pointer's contents. - # If +len+ is given, write the first +len+ bytes of +str+. - # In both cases a final \0 byte is written after the string. + # Write +str+ in pointer's contents, or first +len+ bytes if + # +len+ is not +nil+. def write_string(str, len=nil) - if len - if len == size - warn "[DEPRECATION] Memory too small to write a final 0-byte in #{caller(1, 1)[0]}. This will raise an error in ffi-2.0. Please use write_bytes instead or enlarge the memory region." - write_bytes(str, 0, len) - else - put_char(len, 0) # Check size before writing str - write_bytes(str, 0, len) - end - else - if str.bytesize == size - warn "[DEPRECATION] Memory too small to write a final 0-byte in #{caller(1, 1)[0]}. This will raise an error in ffi-2.0. Please use write_bytes instead or enlarge the memory region." - write_bytes(str) - else - put_string(0, str) - end - end + len = str.bytesize unless len + # Write the string data without NUL termination + put_bytes(0, str, 0, len) end unless method_defined?(:write_string) # @param [Type] type type of data to read from pointer's contents |