byte_array 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 132b072d54dff2d41fa0e72b808a7351217c582a
4
+ data.tar.gz: ce15c8fbf76b381875f6e00bfbb26e7c1a51c516
5
+ SHA512:
6
+ metadata.gz: fbc70f78ddc6a0190f59e8b7761e704b590f8b4748922ed5d248d4ef4c65cbb188ca217378380d128364c150eb7c7f894ba09b0f84ecb6e4d6cf35c992574803
7
+ data.tar.gz: 64af8a2bebd5f6c2186190eb5ebb5248d02eea07b7dbf229a3c8b48a832174205c455bd17c14c3b59ebbe5e25cd422b7b145091e29d8c7f636d141f6ffaaf54f
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ **/*.swp
11
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in byte_array.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Daniel P. Clark
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # ByteArray
2
+
3
+ An implementation of Pythons bytearray() in Ruby. Provides classes Byte and ByteArray.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'byte_array'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install byte_array
20
+
21
+ ## Contributing
22
+
23
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/byte_array. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
24
+
25
+
26
+ ## License
27
+
28
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
29
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "byte_array"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'byte_array'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "byte_array"
8
+ spec.version = ByteArray::VERSION
9
+ spec.authors = ["Daniel P. Clark"]
10
+ spec.email = ["6ftdan@gmail.com"]
11
+
12
+ spec.summary = %q{A Ruby version of Pythons bytearray()}
13
+ spec.description = %q{A Ruby version of Pythons bytearray(). With Pythons array behavior added.}
14
+ spec.homepage = "https://github.com/danielpclark/byte_array"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ end
data/lib/byte_array.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "byte_array/byte"
2
+ require "byte_array/byte_array"
@@ -0,0 +1,28 @@
1
+ class Byte
2
+ def initialize val
3
+ @value = case val
4
+ when Integer
5
+ raise RangeError, "#{val} is too large for Byte" if val > 255
6
+ val
7
+ when String
8
+ raise RangeError, "#{val} is too large for Byte" if val.length > 1
9
+ val.unpack("C").first
10
+ else
11
+ raise TypeError, "Wrong type #{val.class} for Byte"
12
+ end
13
+ end
14
+
15
+ def self.[] obj; obj.is_a?(Byte) ? obj : Byte.new(obj) end
16
+ def self.to_proc; ->obj{self[obj]} end
17
+
18
+ def inspect; @value end
19
+ def to_i; @value end
20
+ def to_s; [@value].pack("C") end
21
+ def to_str; to_s end
22
+ def == o; o.to_i == @value end
23
+ def coerce o; [o.to_i, @value] end
24
+ def + o; @value + o.to_i end
25
+ def - o; @value - o.to_i end
26
+ def * o; @value * o.to_i end
27
+ def / o; @value / o.to_i end
28
+ end
@@ -0,0 +1,101 @@
1
+ class ByteArray
2
+ VERSION = "0.9.0"
3
+
4
+ attr_reader :bytes
5
+ def initialize size = 0, obj = 0
6
+ @bytes = []
7
+ size.times do @bytes << Byte[obj] end
8
+ end
9
+
10
+ def self.[] array = []
11
+ return array if array.is_a? ByteArray
12
+ b = new
13
+ b.insert 0, *array
14
+ b
15
+ end
16
+
17
+ def [] slice
18
+ case slice
19
+ when Range
20
+ ByteArray[@bytes[slice]]
21
+ when Integer
22
+ @bytes[slice]
23
+ end
24
+ end
25
+
26
+ def []= slice, *input
27
+ input = input.flat_map(&method(:to_bytes))
28
+ case slice
29
+ when Range
30
+ @bytes[slice] = *input
31
+ when Integer
32
+ insert slice, *input
33
+ end unless input.empty?
34
+ end
35
+
36
+ def length
37
+ @bytes.length
38
+ end
39
+
40
+ def inspect
41
+ "ByteArray#{@bytes}"
42
+ end
43
+
44
+ def insert position, *what
45
+ assert_in_range position
46
+
47
+ what = what.flat_map(&method(:to_bytes))
48
+ @bytes[position..position+what.length-1] = what unless what.empty?
49
+ end
50
+
51
+ def == other
52
+ return false if other.length != @bytes.length
53
+ ByteArray[other].zip(@bytes).all?{|a,b| a == b}
54
+ end
55
+
56
+ def coerce other
57
+ [ByteArray[other], self]
58
+ end
59
+
60
+ def to_ary
61
+ @bytes
62
+ end
63
+
64
+ def to_a
65
+ @bytes
66
+ end
67
+
68
+ def method_missing m, *a, &b
69
+ #puts "Method #{m} called with", *a
70
+ @bytes.send m, *a, &b
71
+ end
72
+
73
+ private
74
+ # to_bytes always returns Array of Bytes
75
+ def to_bytes input
76
+ b = case input
77
+ when String
78
+ input.unpack("C*")
79
+ when Integer
80
+ raise RangeError, "Integer #{input} is to large to be a Byte" unless input < 256
81
+ [input]
82
+ when Byte
83
+ return [input]
84
+ when Array
85
+ input
86
+ when NilClass
87
+ []
88
+ else
89
+ raise TypeError, "Unexpected type #{input.class} for ByteArray#to_bytes"
90
+ end
91
+ b.map &Byte
92
+ end
93
+
94
+ def assert_in_range index
95
+ unless (0..@bytes.length).include? index
96
+ raise RangeError,
97
+ "Range Exceeded! #{index} is outside of 0..#{@bytes.length}"
98
+ end
99
+ true
100
+ end
101
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: byte_array
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel P. Clark
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: A Ruby version of Pythons bytearray(). With Pythons array behavior added.
56
+ email:
57
+ - 6ftdan@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - byte_array.gemspec
71
+ - lib/byte_array.rb
72
+ - lib/byte_array/byte.rb
73
+ - lib/byte_array/byte_array.rb
74
+ homepage: https://github.com/danielpclark/byte_array
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.5.0
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: A Ruby version of Pythons bytearray()
98
+ test_files: []