ffi_wide_char 1.0.0
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 +7 -0
- data/LICENSE.txt +21 -0
- data/lib/ffi_wide_char/api.rb +12 -0
- data/lib/ffi_wide_char/version.rb +5 -0
- data/lib/ffi_wide_char.rb +69 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 35e9d63072d1d64f405b5afcbadccdf5a7f4b9588bc29df8b4c7408dcc7ffa2b
|
4
|
+
data.tar.gz: 706e4afc78179893f6373b56bae428673658e576991630fe7cd9434ba289eac1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 312c5b9691424a0accf6b2757c3721e5281c92cf5b162a4fc4dd4d4f729cbc4694b18bc80c845ab70d93a9c4857fd1c5c371e402481669d1ee1e2b57cb7a4197
|
7
|
+
data.tar.gz: e6dd3b9c58ca7c992ddb048c89f7d7fb1b2cb45d95bbb1cceee669c4aef14bcea32be450c4254e8960f9a331149fe7533422bfed94b2213741a452a21de9703d
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Thach Nguyen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FfiWideChar
|
4
|
+
module LibC
|
5
|
+
extend FFI::Library
|
6
|
+
ffi_lib FFI::Library::LIBC
|
7
|
+
|
8
|
+
# Converts a string of multibyte characters to a wide character array
|
9
|
+
# size_t mbstowcs (wchar_t *wstring, const char *string, size_t size)
|
10
|
+
attach_function :mbstowcs, %i[pointer string size_t], :size_t
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ffi'
|
4
|
+
|
5
|
+
require 'ffi_wide_char/api'
|
6
|
+
require 'ffi_wide_char/version'
|
7
|
+
|
8
|
+
module FfiWideChar
|
9
|
+
class InvalidMultibyteCharError < StandardError; end
|
10
|
+
class UnsupportedWCharEncodingError < StandardError; end
|
11
|
+
|
12
|
+
DEFAULT_ENC = 'UTF-16LE'
|
13
|
+
ENCODINGS = {
|
14
|
+
"!\x00@\x00\x00\x00" => DEFAULT_ENC,
|
15
|
+
"\x00!\x00@\x00\x00" => 'UTF-16BE',
|
16
|
+
"!\x00\x00\x00@\x00" => 'UTF-32LE',
|
17
|
+
"\x00\x00\x00!\x00\x00" => 'UTF-32BE'
|
18
|
+
}.freeze
|
19
|
+
|
20
|
+
W_CHAR_ENC = nil
|
21
|
+
W_CHAR_SIZE = 2
|
22
|
+
W_CHAR_FUNC = :get_uint16
|
23
|
+
|
24
|
+
# Detect `wchar_t` encoding.
|
25
|
+
# @return [String] Name of the wide-string encoding.
|
26
|
+
def self.detect_encoding
|
27
|
+
return W_CHAR_ENC if W_CHAR_ENC
|
28
|
+
|
29
|
+
ptr = FFI::MemoryPointer.new(:uint8, 6)
|
30
|
+
ret = LibC.mbstowcs(ptr, '!@', 3)
|
31
|
+
raise InvalidMultibyteCharError if ret.negative?
|
32
|
+
|
33
|
+
enc = ENCODINGS[str = ptr.get_bytes(0, 6)]
|
34
|
+
raise UnsupportedWCharEncodingError, str.inspect unless enc
|
35
|
+
|
36
|
+
redefine_const(:W_CHAR_ENC, enc)
|
37
|
+
redefine_const(:W_CHAR_SIZE, enc.include?('32') ? 4 : 2)
|
38
|
+
redefine_const(:W_CHAR_FUNC, W_CHAR_SIZE == 4 ? :get_int32 : :get_uint16)
|
39
|
+
|
40
|
+
enc
|
41
|
+
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
|
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
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ffi_wide_char
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thach Nguyen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-09-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ffi
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.4'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.64'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.64'
|
83
|
+
description: Convert from/to C native `wchar_t` string to used for FFI binding libraries.
|
84
|
+
email:
|
85
|
+
- nthachus@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- LICENSE.txt
|
91
|
+
- lib/ffi_wide_char.rb
|
92
|
+
- lib/ffi_wide_char/api.rb
|
93
|
+
- lib/ffi_wide_char/version.rb
|
94
|
+
homepage: https://github.com/nthachus/ffi_wide_char
|
95
|
+
licenses:
|
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
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '2.3'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.7.6.2
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Provides methods to convert from/to FFI wide-string.
|
121
|
+
test_files: []
|