bychar 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +26 -0
- data/Rakefile +48 -0
- data/bychar.gemspec +57 -0
- data/lib/bychar.rb +49 -0
- data/test/helper.rb +27 -0
- data/test/test_bychar.rb +48 -0
- metadata +123 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rdoc", "~> 3.12"
|
10
|
+
gem "bundler"
|
11
|
+
gem "jeweler", "~> 1.8.4"
|
12
|
+
gem "flexmock", "~>0.8"
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Julik Tarkhanov
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
= bychar
|
2
|
+
|
3
|
+
A simple IO wrapper for libraries that tend to read the IO in the following way:
|
4
|
+
|
5
|
+
* Only forward
|
6
|
+
* By a single character
|
7
|
+
* Without skipping
|
8
|
+
|
9
|
+
The wrapper will cache some bytes from the passed IO object which will make parsing
|
10
|
+
faster when you advance your parser char by char.
|
11
|
+
|
12
|
+
== Contributing to bychar
|
13
|
+
|
14
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
15
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
16
|
+
* Fork the project.
|
17
|
+
* Start a feature/bugfix branch.
|
18
|
+
* Commit and push until you are happy with your contribution.
|
19
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
20
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
21
|
+
|
22
|
+
== Copyright
|
23
|
+
|
24
|
+
Copyright (c) 2013 Julik Tarkhanov. See LICENSE.txt for
|
25
|
+
further details.
|
26
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
require File.dirname(__FILE__) + "/lib/bychar"
|
6
|
+
|
7
|
+
begin
|
8
|
+
Bundler.setup(:default, :development)
|
9
|
+
rescue Bundler::BundlerError => e
|
10
|
+
$stderr.puts e.message
|
11
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
12
|
+
exit e.status_code
|
13
|
+
end
|
14
|
+
require 'rake'
|
15
|
+
|
16
|
+
require 'jeweler'
|
17
|
+
Jeweler::Tasks.new do |gem|
|
18
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
19
|
+
gem.version = Bychar::VERSION
|
20
|
+
gem.name = "bychar"
|
21
|
+
gem.homepage = "http://github.com/julik/bychar"
|
22
|
+
gem.license = "MIT"
|
23
|
+
gem.summary = %Q{ Helps parsing IO char by char }
|
24
|
+
gem.description = %Q{ Helps parsing IO char by char }
|
25
|
+
gem.email = "me@julik.nl"
|
26
|
+
gem.authors = ["Julik Tarkhanov"]
|
27
|
+
# dependencies defined in Gemfile
|
28
|
+
end
|
29
|
+
Jeweler::RubygemsDotOrgTasks.new
|
30
|
+
|
31
|
+
require 'rake/testtask'
|
32
|
+
Rake::TestTask.new(:test) do |test|
|
33
|
+
test.libs << 'lib' << 'test'
|
34
|
+
test.pattern = 'test/**/test_*.rb'
|
35
|
+
test.verbose = true
|
36
|
+
end
|
37
|
+
|
38
|
+
task :default => :test
|
39
|
+
|
40
|
+
require 'rdoc/task'
|
41
|
+
Rake::RDocTask.new do |rdoc|
|
42
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
43
|
+
|
44
|
+
rdoc.rdoc_dir = 'rdoc'
|
45
|
+
rdoc.title = "bychar #{version}"
|
46
|
+
rdoc.rdoc_files.include('README*')
|
47
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
48
|
+
end
|
data/bychar.gemspec
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "bychar"
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Julik Tarkhanov"]
|
12
|
+
s.date = "2013-03-15"
|
13
|
+
s.description = " Helps parsing IO char by char "
|
14
|
+
s.email = "me@julik.nl"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"bychar.gemspec",
|
26
|
+
"lib/bychar.rb",
|
27
|
+
"test/helper.rb",
|
28
|
+
"test/test_bychar.rb"
|
29
|
+
]
|
30
|
+
s.homepage = "http://github.com/julik/bychar"
|
31
|
+
s.licenses = ["MIT"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = "1.8.24"
|
34
|
+
s.summary = "Helps parsing IO char by char"
|
35
|
+
|
36
|
+
if s.respond_to? :specification_version then
|
37
|
+
s.specification_version = 3
|
38
|
+
|
39
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
40
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
41
|
+
s.add_development_dependency(%q<bundler>, [">= 0"])
|
42
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
43
|
+
s.add_development_dependency(%q<flexmock>, ["~> 0.8"])
|
44
|
+
else
|
45
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
46
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
47
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
48
|
+
s.add_dependency(%q<flexmock>, ["~> 0.8"])
|
49
|
+
end
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
52
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
53
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
54
|
+
s.add_dependency(%q<flexmock>, ["~> 0.8"])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
data/lib/bychar.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
module Bychar
|
5
|
+
VERSION = '1.0.0'
|
6
|
+
|
7
|
+
# Default buffer size is 512k
|
8
|
+
DEFAULT_BUFFER_SIZE = 512 * 1024
|
9
|
+
|
10
|
+
# This object helps you build parsers that parse an IO byte by byte without having to
|
11
|
+
# read byte by byte.
|
12
|
+
# Reading byte by byte is very inefficient, but we want to parse byte by byte since
|
13
|
+
# this makes parser construction much easier. So what we do is cache some chunk of the
|
14
|
+
# passed buffer and read from that. Once exhausted there will be some caching again,
|
15
|
+
# and ad infinitum until the passed buffer is exhausted
|
16
|
+
class Reader
|
17
|
+
|
18
|
+
|
19
|
+
def initialize(with_io, buffer_size = DEFAULT_BUFFER_SIZE)
|
20
|
+
@io = with_io
|
21
|
+
@bufsize = buffer_size
|
22
|
+
@buf = StringIO.new
|
23
|
+
end
|
24
|
+
|
25
|
+
# Will transparently read one byte off the contained IO, maintaining the internal cache.
|
26
|
+
# If the cache has been depleted it will read a big chunk from the IO and cache it and then
|
27
|
+
# return the byte
|
28
|
+
def read_one_byte
|
29
|
+
cache if @buf.pos == @buf.size
|
30
|
+
|
31
|
+
return nil if @buf.size.zero?
|
32
|
+
return @buf.read(1)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Tells whether all the data has been both read from the passed buffer
|
36
|
+
# and from the internal cache buffer (checks whether there is anything that
|
37
|
+
# can be retreived using read_one_byte)
|
38
|
+
def eof?
|
39
|
+
@buf.eof? && @io.eof?
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def cache
|
45
|
+
data = @io.read(@bufsize)
|
46
|
+
@buf = StringIO.new(data.to_s) # Make nil become ""
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'flexmock'
|
12
|
+
require 'flexmock/test_unit'
|
13
|
+
|
14
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
15
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
16
|
+
|
17
|
+
require 'bychar'
|
18
|
+
|
19
|
+
# http://redmine.ruby-lang.org/issues/4882
|
20
|
+
# https://github.com/jimweirich/flexmock/issues/4
|
21
|
+
# https://github.com/julik/flexmock/commit/4acea00677e7b558bd564ec7c7630f0b27d368ca
|
22
|
+
class FlexMock::PartialMockProxy
|
23
|
+
def singleton?(method_name)
|
24
|
+
@obj.singleton_methods.include?(method_name.to_s)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
data/test/test_bychar.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestBychar < Test::Unit::TestCase
|
4
|
+
def test_reads_once
|
5
|
+
s = StringIO.new("This is a string")
|
6
|
+
|
7
|
+
reader = Bychar::Reader.new(s)
|
8
|
+
assert_equal "T", reader.read_one_byte
|
9
|
+
assert_equal "h", reader.read_one_byte
|
10
|
+
assert !reader.eof?
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_reads_set_buffer_size
|
14
|
+
s = StringIO.new("abcd")
|
15
|
+
flexmock(s).should_receive(:read).with(4).once.and_return("abcd")
|
16
|
+
reader = Bychar::Reader.new(s, 4)
|
17
|
+
reader.read_one_byte
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_reads_in_64kb_chunks_by_default
|
21
|
+
s = StringIO.new("abcd")
|
22
|
+
flexmock(s).should_receive(:read).with(Bychar::DEFAULT_BUFFER_SIZE).once.and_return("abcd")
|
23
|
+
reader = Bychar::Reader.new(s)
|
24
|
+
reader.read_one_byte
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_eof_with_empty
|
28
|
+
s = StringIO.new
|
29
|
+
reader = Bychar::Reader.new(s)
|
30
|
+
assert s.eof?
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_eof_with_io_at_eof
|
34
|
+
s = StringIO.new("foo")
|
35
|
+
s.read(3)
|
36
|
+
reader = Bychar::Reader.new(s)
|
37
|
+
assert reader.eof?
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_eof_with_string_to_size
|
41
|
+
s = "Foobarboo another"
|
42
|
+
s = StringIO.new(s)
|
43
|
+
reader = Bychar::Reader.new(s, 1)
|
44
|
+
s.length.times { reader.read_one_byte }
|
45
|
+
assert reader.eof?
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bychar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Julik Tarkhanov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rdoc
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.12'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.12'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jeweler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.8.4
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.8.4
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: flexmock
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.8'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0.8'
|
78
|
+
description: ! ' Helps parsing IO char by char '
|
79
|
+
email: me@julik.nl
|
80
|
+
executables: []
|
81
|
+
extensions: []
|
82
|
+
extra_rdoc_files:
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.rdoc
|
85
|
+
files:
|
86
|
+
- .document
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.rdoc
|
90
|
+
- Rakefile
|
91
|
+
- bychar.gemspec
|
92
|
+
- lib/bychar.rb
|
93
|
+
- test/helper.rb
|
94
|
+
- test/test_bychar.rb
|
95
|
+
homepage: http://github.com/julik/bychar
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
hash: -3036736534217565560
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 1.8.24
|
120
|
+
signing_key:
|
121
|
+
specification_version: 3
|
122
|
+
summary: Helps parsing IO char by char
|
123
|
+
test_files: []
|