ffi_wide_char 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ffi_wide_char/api.rb +1 -1
- data/lib/ffi_wide_char/version.rb +1 -1
- data/lib/ffi_wide_char.rb +66 -44
- metadata +16 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19b32012cde9e95923464d15b12c156176c51c97e02a9d4d8aeed19d6a886839
|
4
|
+
data.tar.gz: '0772497956c010ad1be1d6cea9eaa586de0256f1c6b7bf303ea5ff13095f2dbc'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ecd7aa2746e6ba7e3ae41d13c0237971a15aa156f2ad2489c6e2fd2bc9366195c2a4f0cbfdb18e536e828b85ff6bdfbacfa5511ae3032720265cdbadd77600d7
|
7
|
+
data.tar.gz: bafdddf658523047680c82d129fead2899cfcc1d3b98bb8e4f9c877cd8b8de8f89b74fc87c4c67ab50df78379330088fe688d1694f43337b5fcf20a981913a41
|
data/lib/ffi_wide_char/api.rb
CHANGED
@@ -7,6 +7,6 @@ module FfiWideChar
|
|
7
7
|
|
8
8
|
# Converts a string of multibyte characters to a wide character array
|
9
9
|
# size_t mbstowcs (wchar_t *wstring, const char *string, size_t size)
|
10
|
-
attach_function :mbstowcs,
|
10
|
+
attach_function :mbstowcs, [:pointer, :string, :size_t], :size_t
|
11
11
|
end
|
12
12
|
end
|
data/lib/ffi_wide_char.rb
CHANGED
@@ -21,49 +21,71 @@ module FfiWideChar
|
|
21
21
|
W_CHAR_SIZE = 2
|
22
22
|
W_CHAR_FUNC = :get_uint16
|
23
23
|
|
24
|
-
#
|
25
|
-
|
26
|
-
|
27
|
-
return
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
24
|
+
# All methods in this block are static
|
25
|
+
class << self
|
26
|
+
# Detect `wchar_t` encoding.
|
27
|
+
# @return [String] Name of the wide-string encoding.
|
28
|
+
def detect_encoding
|
29
|
+
return W_CHAR_ENC if W_CHAR_ENC
|
30
|
+
|
31
|
+
ptr = FFI::MemoryPointer.new(:uint8, 6)
|
32
|
+
ret = LibC.mbstowcs(ptr, '!@', 3)
|
33
|
+
raise InvalidMultibyteCharError unless ret.is_a?(Numeric) && ret >= 0
|
34
|
+
|
35
|
+
enc = ENCODINGS[str = ptr.get_bytes(0, 6)]
|
36
|
+
raise UnsupportedWCharEncodingError, str.inspect unless enc
|
37
|
+
|
38
|
+
redefine_consts enc
|
39
|
+
end
|
40
|
+
|
41
|
+
# Look for a null terminating characters; if found, read up to that null (exclusive)
|
42
|
+
# @param ptr [Pointer] A wide-string pointer.
|
43
|
+
# @return [String] Decoded wide-string.
|
44
|
+
def read_wide_string(ptr)
|
45
|
+
return nil unless ptr && (get_object_attr(ptr, :address) || 0) != 0
|
46
|
+
|
47
|
+
len = get_wide_string_len ptr
|
48
|
+
ptr.get_bytes(0, len).force_encoding(W_CHAR_ENC || DEFAULT_ENC)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Convert a Ruby string into a C native wide-string.
|
52
|
+
# @param str [String] A Ruby string.
|
53
|
+
# @return [String] C native wide-string.
|
54
|
+
def to_wide_string(str)
|
55
|
+
str ? str.encode(W_CHAR_ENC || DEFAULT_ENC) : str
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# @param enc [String]
|
61
|
+
# @return [String]
|
62
|
+
def redefine_consts(enc)
|
63
|
+
[:W_CHAR_ENC, :W_CHAR_SIZE, :W_CHAR_FUNC].each { |name| send :remove_const, name }
|
64
|
+
|
65
|
+
const_set :W_CHAR_ENC, enc
|
66
|
+
const_set :W_CHAR_SIZE, enc.include?('32') ? 4 : 2
|
67
|
+
const_set :W_CHAR_FUNC, W_CHAR_SIZE == 4 ? :get_int32 : :get_uint16
|
68
|
+
|
69
|
+
enc
|
70
|
+
end
|
71
|
+
|
72
|
+
# @param obj [Object]
|
73
|
+
# @param name [Symbol]
|
74
|
+
# @return [Object]
|
75
|
+
def get_object_attr(obj, name)
|
76
|
+
obj.respond_to?(name) ? obj.send(name) : nil
|
77
|
+
end
|
78
|
+
|
79
|
+
# @param ptr [Pointer]
|
80
|
+
# @return [Integer]
|
81
|
+
def get_wide_string_len(ptr)
|
82
|
+
sz = get_object_attr(ptr, :size)
|
83
|
+
sz -= W_CHAR_SIZE if sz
|
84
|
+
|
85
|
+
len = 0
|
86
|
+
len += W_CHAR_SIZE while (!sz || len < sz) && ptr.send(W_CHAR_FUNC, len) != 0
|
87
|
+
|
88
|
+
len
|
89
|
+
end
|
41
90
|
end
|
42
|
-
|
43
|
-
# Look for a null terminating characters; if found, read up to that null (exclusive)
|
44
|
-
# @param ptr [Pointer] A wide-string pointer.
|
45
|
-
# @return [String] Decoded wide-string.
|
46
|
-
def self.read_wide_string(ptr)
|
47
|
-
return nil unless ptr&.respond_to?(:address) && ptr.address.nonzero?
|
48
|
-
|
49
|
-
sz = (ptr.size || 0) - W_CHAR_SIZE
|
50
|
-
len = 0
|
51
|
-
len += W_CHAR_SIZE while (sz.negative? || len < sz) && ptr.send(W_CHAR_FUNC, len) != 0
|
52
|
-
|
53
|
-
ptr.get_bytes(0, len).force_encoding(W_CHAR_ENC || DEFAULT_ENC)
|
54
|
-
end
|
55
|
-
|
56
|
-
# Convert a Ruby string into a C native wide-string.
|
57
|
-
# @param str [String] A Ruby string.
|
58
|
-
# @return [String] C native wide-string.
|
59
|
-
def self.to_wide_string(str)
|
60
|
-
str&.encode(W_CHAR_ENC || DEFAULT_ENC)
|
61
|
-
end
|
62
|
-
|
63
|
-
def self.redefine_const(name, value)
|
64
|
-
send(:remove_const, name) if const_defined?(name)
|
65
|
-
const_set name, value
|
66
|
-
end
|
67
|
-
|
68
|
-
private_class_method :redefine_const
|
69
91
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ffi_wide_char
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thach Nguyen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffi
|
@@ -39,47 +39,47 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.7'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '10.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '10.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '5.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '5.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rubocop
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0.
|
75
|
+
version: '0.41'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0.
|
82
|
+
version: '0.41'
|
83
83
|
description: Convert from/to C native `wchar_t` string to used for FFI binding libraries.
|
84
84
|
email:
|
85
85
|
- nthachus@gmail.com
|
@@ -94,10 +94,7 @@ files:
|
|
94
94
|
homepage: https://github.com/nthachus/ffi_wide_char
|
95
95
|
licenses:
|
96
96
|
- MIT
|
97
|
-
metadata:
|
98
|
-
homepage_uri: https://github.com/nthachus/ffi_wide_char
|
99
|
-
source_code_uri: https://github.com/nthachus/ffi_wide_char
|
100
|
-
changelog_uri: https://github.com/nthachus/ffi_wide_char
|
97
|
+
metadata: {}
|
101
98
|
post_install_message:
|
102
99
|
rdoc_options: []
|
103
100
|
require_paths:
|
@@ -106,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
106
103
|
requirements:
|
107
104
|
- - ">="
|
108
105
|
- !ruby/object:Gem::Version
|
109
|
-
version: '
|
106
|
+
version: '0'
|
110
107
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
108
|
requirements:
|
112
109
|
- - ">="
|