bytes 0.1.1 → 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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +301 -1
  3. data/Rakefile +1 -1
  4. data/lib/bytes/version.rb +3 -3
  5. data/lib/bytes.rb +23 -13
  6. metadata +6 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a17c54ac2788c884d6e1d8605b54621fe1707cf3252bbc6c81ad062e4dda6ca2
4
- data.tar.gz: 23189f40e8c5459ccd8d53a2796524d0b3b217dd13749e48f406652683b3367d
3
+ metadata.gz: 4d93d96204652bc075816b6b9a32220b7f1f72fa3765379012ecea3dfc4485c3
4
+ data.tar.gz: ba15bcb1f6939fd5ddb4d42da815a9ecb7edfc42e785c81bc8845101dc819b9a
5
5
  SHA512:
6
- metadata.gz: 6203bdf2072f6fc54b01029a1de95896bc7d5eb37690b30f0186dc5615bc87ed098fb9b2542d9e9c29e6c2571f3bb8bc1793d802851de0da4910163fa74a75c1
7
- data.tar.gz: e1aa00e67da7a420a6f791a7ed7954a0c229d553b1c1b9e284968e69b860c85150409cacd79fe1e4bf3e16a1d954b6c2ed8dee6c380404f4e89841835cbd00b7
6
+ metadata.gz: d3f41e967326e54022df7c16257fab59dc8bfe3d0402ba74670b16532d086162b74156937e3b4f40f2a09aee5a73521e1399b0329232da418a18599a48cfcfe1
7
+ data.tar.gz: 71726839412c32893a6f1a243cf4d00d84ba1558379f39e2e355940b645a5970a72c07a249481f11046486853926d8afd44aa28b4ef7ebc90afc9674534eaeb0
data/README.md CHANGED
@@ -8,10 +8,310 @@ bytes - bits 'n' bytes made easy/easier incl. new buffer helper / wrapper class
8
8
  * rdoc :: [rubydoc.info/gems/bytes](http://rubydoc.info/gems/bytes)
9
9
 
10
10
 
11
+ ## Background - Programming Bits, Bytes 'n' Blocks Step-by-Step Book / Guide
12
+
13
+ _Let's start with the three types of strings, that is, bytes, (string) buffers, and (frozen) strings, ..._
14
+
15
+
16
+ ### Byte vs. Character
17
+
18
+
19
+ #### What's a Byte?
20
+
21
+ A byte is a 8-bit integer number (that is, unsigned from 0 to 255 or
22
+ signed from -128 to 127 using 2's complement).
23
+ Example:
24
+
25
+ ``` ruby
26
+ 0b01000001 #=> 65 - base 2 - binary bits
27
+ 65 #=> 65 - base 10 - decimal numbers
28
+ 0x41 #=> 65 - base 16 - hexadecimal numbers
29
+ [0b01000001, 65, 0x41]
30
+ #=> [65, 65, 65]
31
+ ```
32
+
33
+ Or with conversions to 8-bit integer numbers. Example:
34
+
35
+ ``` ruby
36
+ "01000001".to_i(2) #=> 65
37
+ "65".to_i(10) #=> 65
38
+ "65".to_i #=> 65 - same as to_i(10)
39
+ "41".to_i(16) #=> 65
40
+ "0x41".to_i(16) #=> 65 - same as "41" - 0x hex prefix gets skipped
41
+ ["01000001".to_i(2), "65".to_i(10), "41".to_i(16)]
42
+ #=> [65, 65, 65]
43
+ ```
44
+
45
+ Note: `String#hex` is a built-in short-cut / alias for `String#to_i(16)`. Example:
46
+
47
+ ``` ruby
48
+ "41".hex #=> 65
49
+ "0x41".hex #=> 65 - same as "41" - 0x hex prefix gets
50
+ ```
51
+
52
+
53
+
54
+ #### What's a Character?
55
+
56
+ A character (or char) used to be a byte
57
+ and, thus, a string (array) of characters
58
+ was also an array of bytes. Example:
59
+
60
+ ``` ruby
61
+ ?A.ord #=> 65 - ASCII character
62
+ "A".ord #=> 65 - ASCII character
63
+ "\x41".ord #=> 65 - ASCII character
64
+ [?A.ord, "A".ord, "\x41".ord]
65
+ #=> [65, 65, 65]
66
+ ```
67
+
68
+
69
+
70
+ History Nostalgia Corner: ASCII 7-Bit Character Encoding
71
+
72
+ ![](i/us_ascii_code_chart.png)
73
+
74
+ > ASCII abbreviated from American Standard Code for Information Interchange,
75
+ > is a character encoding standard for electronic communication. ASCII codes
76
+ > represent text in computers, telecommunications equipment, and other devices.
77
+ > Most modern character-encoding schemes are based on ASCII,
78
+ > although they support many additional characters.
79
+ >
80
+ > ASCII is the traditional name for the encoding system; the Internet Assigned
81
+ > Numbers Authority (IANA) prefers the updated name US-ASCII, which clarifies
82
+ > that this system was developed in the US and based on the typographical
83
+ > symbols predominantly in use there.
84
+ >
85
+ > (Source: [ASCII @ Wikipedia](https://en.wikipedia.org/wiki/ASCII))
86
+
87
+
88
+
89
+ Nowadays a character can have one, two or even more bytes.
90
+ Let's try:
91
+
92
+ ``` ruby
93
+ ## Latin Capital Letter A (Unicode) - 1 Byte
94
+ "A".bytes #=> [65]
95
+ "\x41".bytes #=> [65] - same as "A"
96
+ "\u0041".bytes #=> [65]
97
+ "\u{41}".bytes #=> [65] - same as "\u0041" - leading zeros can be dropped
98
+ "A".bytes.size #=> 1
99
+ "A".chars.size #=> 1
100
+
101
+ ## Cyrillic Capital Letter A (Unicode) - 2 Bytes
102
+ "А".bytes #=> [208, 144]
103
+ "\u0410".bytes #=> [208, 144] - same "А"
104
+ "\u{410}".bytes #=> [208, 144] - same as "\u0410" - leading zeros can be dropped
105
+ "А".bytes.size #=> 2
106
+ "А".chars.size #=> 1
107
+
108
+ # Old Persian Number One (Unicode) - 4 Bytes
109
+ "𐏑".bytes #=> [240, 144, 143, 145]
110
+ "\u{103D1}".bytes #=> [240, 144, 143, 145]
111
+ "\u{103d1}".bytes #=> [240, 144, 143, 145] same as "\u{103D1}"
112
+ "𐏑".bytes.size #=> 4
113
+ "𐏑".chars.size #=> 1
114
+
115
+ # ...
116
+ ```
117
+
118
+
119
+
120
+ ### String of Bytes or String of Characters? Yes, Yes, Yes
121
+
122
+
123
+ In ruby the String class can morph into three types:
124
+
125
+ - Bytes
126
+ - Mutable String a.k.a String Buffer
127
+ - Immutable String a.k.a. Frozen String
128
+
129
+
130
+ #### Bytes / Binary
131
+
132
+
133
+ `String.new` or `"".b` creates new bytes, that is, a new binary string
134
+ buffer with the ASCII_8BIT encoding also known as BINARY.
135
+ Let's try:
136
+
137
+ ``` ruby
138
+ String.new.encoding #=> <Encoding::ASCII_8BIT>
139
+ String.new("".b).encoding #=> <Encoding::ASCII_8BIT>
140
+ "".b.encoding #=> <Encoding::ASCII_8BIT>
141
+
142
+ Encoding::BINARY == Encoding::ASCII_8BIT #=> true
143
+
144
+ # or using the "type-safe" Bytes class
145
+
146
+ Bytes.new.encoding #=> <Encoding::ASCII_8BIT>
147
+ Bytes.new("").encoding #=> <Encoding::ASCII_8BIT>
148
+ Bytes.new("abc").encoding #=> <Encoding::ASCII_8BIT>
149
+ Bytes.new("\x61\x62\x63").encoding #=> <Encoding::ASCII_8BIT>
150
+ ```
151
+
152
+
153
+
154
+ #### String Buffer
155
+
156
+ If you use `String.new("")` (note the `""` passed in) or
157
+ the string literal `""` that creates a new string buffer
158
+ with the default encoding (usually UTF-8).
159
+ Let's try:
160
+
161
+ ``` ruby
162
+ # encoding: utf-8
163
+ String.new("").encoding #=> <Encoding::UTF_8>
164
+ "".encoding #=> <Encoding::UTF_8>
165
+ ```
166
+
167
+ <!--
168
+
169
+ # or using the StringBuffer c'tor helper (returning a String)
170
+
171
+ StringBuffer.new.encoding #=> <Encoding::UTF_8>
172
+ StringBuffer.new("").encoding #=> <Encoding::UTF_8>
173
+ StringBuffer.new("abc").encoding #=> <Encoding::UTF_8>
174
+
175
+ -->
176
+
177
+
178
+ #### Frozen String
179
+
180
+ If you use the recommended `# frozen_string_literal: true` magic comment
181
+ or pragma you can automagically turn all string literals into
182
+ frozen (immutable) strings with the default encoding (usually UTF-8).
183
+ Let's try:
184
+
185
+ ``` ruby
186
+ # frozen_string_literal: true
187
+ "".frozen? #=> true
188
+ "Hello, World!".frozen? #=> true
189
+ String.new.frozen? #=> false
190
+ String.new("").frozen? #=> false
191
+ ```
192
+
193
+
194
+
195
+ <!--
196
+
197
+ ### Bytes
198
+
199
+ bytes from hexstring
200
+
201
+ bytes to hexstring
202
+
203
+ bytes from string
204
+
205
+ bytes to string
206
+
207
+ bytes to array of integers
208
+
209
+ bytes from array of integers
210
+
211
+
212
+
213
+ #### Bytes to Integer Numbers - Little-Endian vs Big-Endian
214
+
215
+ 4 byte unsigned integer -
216
+
217
+ Example - 1
218
+
219
+ bytes to integer
220
+
221
+ integer to bytes
222
+
223
+ Big-End first or Little-End first?
224
+ Least significant bit (lsm) or most significant bit (msb) first?
225
+
226
+
227
+
228
+
229
+ #### Bytes Helper
230
+
231
+
232
+
233
+ ### Buffer
234
+
235
+ #### Buffer Helper
236
+
237
+ -->
238
+
239
+
240
+
241
+
242
+ To be continued ...
243
+
244
+ ---
245
+
246
+
247
+
11
248
 
12
249
  ## Usage
13
250
 
14
- To be done
251
+ ### BytesHelper - From Bytes (Binary String) to Hex(adecimal) String and Back
252
+
253
+ ``` ruby
254
+ require 'bytes
255
+
256
+ Bytes.bin_to_hex( "\x61\x62".b )
257
+ #=> '6162'
258
+
259
+ Bytes.hex_to_bin( '6162' ) # or
260
+ Bytes.hex_to_bin( '0x6162' ) # or
261
+ #=> "\x61\x62".b
262
+
263
+ Bytes.is_hex?( '6162' )
264
+ Bytes.is_hex?( '0x6162' )
265
+ Bytes.is_hex?( '' ) # empty string or
266
+ Bytes.is_hex?( '0x' ) # empty hex string
267
+ #=> true
268
+
269
+ Bytes.is_hex?( 'xyz' )
270
+ Bytes.is_hex?( '0xyz' )
271
+ #=> false
272
+
273
+ Bytes.hex_to_bin( 'xzy' ) # or
274
+ Bytes.hex_to_bin( '0xxzy' )
275
+ #=> raises TypeError - non-hexadecimal digit found
276
+ ```
277
+
278
+ Note: You can use the shorter alternate alias
279
+ names `btoh` or `htob`
280
+ for `bin_to_hex` and `hex_to_bin`.
281
+
282
+
283
+ Or use the mixed-in String class variants. Example:
284
+
285
+ ``` ruby
286
+ "\x61\x62".b.bin_to_hex # or
287
+ "\x61\x62".b.btoh
288
+ #=> '6162'
289
+
290
+ '6162'.hex_to_bin # or
291
+ '6162'.htob # or
292
+ '0x6162'.hex_to_bin # or
293
+ '0x6162'.htob # or
294
+ #=> "\x61\x62".b
295
+
296
+ '6162'.is_hex?
297
+ '0x6162'is_hex?
298
+ ''.is_hex? # empty string or
299
+ '0x'.is_hex? # empty hex string
300
+ #=> true
301
+
302
+ 'xyz'is_hex?
303
+ '0xyz'.is_hex?
304
+ #=> false
305
+
306
+ 'xzy'.htob # or
307
+ '0xxzy'.htob
308
+ #=> raises TypeError - non-hexadecimal digit found
309
+ ```
310
+
311
+ and so on.
312
+
313
+
314
+
15
315
 
16
316
 
17
317
  ## License
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ Hoe.spec 'bytes' do
6
6
 
7
7
  self.version = Cocos::Module::Bytes::VERSION
8
8
 
9
- self.summary = "bytes"
9
+ self.summary = "bytes - bits 'n' bytes made easy/easier incl. new buffer helper / wrapper class to help with the string byte vs character dichotomy"
10
10
  self.description = summary
11
11
 
12
12
 
data/lib/bytes/version.rb CHANGED
@@ -3,9 +3,9 @@ module Cocos
3
3
  module Module
4
4
  module Bytes
5
5
 
6
- MAJOR = 0
7
- MINOR = 1
8
- PATCH = 1
6
+ MAJOR = 1
7
+ MINOR = 0
8
+ PATCH = 0
9
9
  VERSION = [MAJOR,MINOR,PATCH].join('.')
10
10
 
11
11
  def self.version
data/lib/bytes.rb CHANGED
@@ -6,6 +6,7 @@ require 'forwardable'
6
6
  require_relative 'bytes/version' # note: let version always go first
7
7
 
8
8
 
9
+
9
10
  module BytesHelper
10
11
  def hex_to_bin( hex )
11
12
  ## todo/fix: do an argument regex hex check!!!!
@@ -18,8 +19,8 @@ module BytesHelper
18
19
  [hex].pack('H*')
19
20
  end
20
21
  end
21
- alias_method :hex_to_bytes, :hex_to_bin
22
- alias_method :h_to_b, :hex_to_bin
22
+ ## alias_method :hex_to_bytes, :hex_to_bin
23
+ ## alias_method :h_to_b, :hex_to_bin
23
24
  alias_method :htob, :hex_to_bin
24
25
 
25
26
 
@@ -32,8 +33,8 @@ module BytesHelper
32
33
  hex.encode!( Encoding::UTF_8 )
33
34
  hex
34
35
  end
35
- alias_method :bytes_to_hex, :bin_to_hex
36
- alias_method :b_to_h, :bin_to_hex
36
+ ## alias_method :bytes_to_hex, :bin_to_hex
37
+ ## alias_method :b_to_h, :bin_to_hex
37
38
  alias_method :btoh, :bin_to_hex
38
39
 
39
40
 
@@ -64,8 +65,14 @@ class Bytes
64
65
  def initialize( bin=String.new )
65
66
  ## note: for now will NOT dup(licate) passed in binary array
66
67
  ## you only get a new binary array if no arg passed in e.g. Bytes.new
67
- raise ArgumentError, "Bytes.new - BINARY/ASCII-8BIT encoding expected; got: #{bin.encoding} for string >#{bin}<" if bin.encoding != Encoding::ASCII_8BIT
68
- @bin = bin
68
+ @bin = if bin.encoding != Encoding::ASCII_8BIT
69
+ puts "!! WARN - Bytes.new - BINARY/ASCII-8BIT encoding expected; got: #{bin.encoding} for string:"
70
+ pp bin
71
+
72
+ bin.b
73
+ else
74
+ bin
75
+ end
69
76
  end
70
77
 
71
78
 
@@ -75,8 +82,8 @@ class Bytes
75
82
  :size, :length
76
83
 
77
84
 
78
- def to_s() Bytes.bin_to_hex( @bin ); end
79
- alias_method :to_hex, :to_s
85
+ def to_hex() Bytes.bin_to_hex( @bin ); end
86
+ alias_method :to_s, :to_hex
80
87
 
81
88
  def b() @bin; end
82
89
 
@@ -113,12 +120,15 @@ end
113
120
 
114
121
 
115
122
  class String
116
- def to_hex() Bytes.bin_to_hex( self ); end
117
- ## add more aliases e.g. bin_to_hex or btoh or b_to_h or such - why? why not?
118
-
123
+ def bin_to_hex() Bytes.bin_to_hex( self ); end
124
+ alias_method :btoh, :bin_to_hex
125
+ alias_method :to_hex, :bin_to_hex
119
126
  ## note: built-in String#hex returns string converted
120
- ## to Integer -same as String.to_i(16) !!!!
121
- end
127
+ ## to Integer - same as String.to_i(16) !!!!
128
+
129
+ def hex_to_bin() Bytes.hex_to_bin( self ); end
130
+ alias_method :htob, :hex_to_bin
131
+ end # class String
122
132
 
123
133
 
124
134
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bytes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-10 00:00:00.000000000 Z
11
+ date: 2022-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdoc
@@ -44,7 +44,8 @@ dependencies:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
46
  version: '3.23'
47
- description: bytes
47
+ description: bytes - bits 'n' bytes made easy/easier incl. new buffer helper / wrapper
48
+ class to help with the string byte vs character dichotomy
48
49
  email: wwwmake@googlegroups.com
49
50
  executables: []
50
51
  extensions: []
@@ -83,5 +84,6 @@ requirements: []
83
84
  rubygems_version: 3.3.7
84
85
  signing_key:
85
86
  specification_version: 4
86
- summary: bytes
87
+ summary: bytes - bits 'n' bytes made easy/easier incl. new buffer helper / wrapper
88
+ class to help with the string byte vs character dichotomy
87
89
  test_files: []