netstring 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/.yardopts +4 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +56 -0
- data/Rakefile +16 -0
- data/USAGE +1 -0
- data/lib/netstring.rb +61 -0
- data/netstring.gemspec +21 -0
- data/spec/netstring_spec.rb +88 -0
- data/spec/spec_helper.rb +11 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 58bf8216c488dc9bf16c5f2826816f32e9d326c0
|
4
|
+
data.tar.gz: fe341a3b67a1fab40772771588180ecb193712a8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4891518572806c60b05d3f4c8726c2039b0ce40a108fa68694771250075dc8f1c6a5613fa0312ab2c7c2eabdc2b40d3169c567143fd7a5930de78004feb25942
|
7
|
+
data.tar.gz: 5c2cf2d5e27208946073ec204c3b01adf3cc797763ed5567b4020c1dd6ba2d5a644736a4ba8cad282b645428bc97a8c9dc5e9c0bda1a22c3e457b4c326e3e2bc
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 James McKinney
|
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.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Netstring: A netstring parser and emitter
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/netstring.svg)](http://badge.fury.io/rb/netstring)
|
4
|
+
[![Build Status](https://secure.travis-ci.org/jpmckinney/netstring.png)](http://travis-ci.org/jpmckinney/netstring)
|
5
|
+
[![Dependency Status](https://gemnasium.com/jpmckinney/netstring.png)](https://gemnasium.com/jpmckinney/netstring)
|
6
|
+
[![Coverage Status](https://coveralls.io/repos/jpmckinney/netstring/badge.png)](https://coveralls.io/r/jpmckinney/netstring)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/jpmckinney/netstring.png)](https://codeclimate.com/github/jpmckinney/netstring)
|
8
|
+
|
9
|
+
See the [netstring](http://cr.yp.to/proto/netstrings.txt) specification for details.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require "netstring"
|
15
|
+
```
|
16
|
+
|
17
|
+
Dump:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
netstring = Netstring.dump("xyz") # "3:xyz,"
|
21
|
+
```
|
22
|
+
|
23
|
+
Load:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
string = Netstring.load("3:xyz,") # "xyz"
|
27
|
+
```
|
28
|
+
|
29
|
+
Get the netstring from which the string was loaded:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
string.netstring # "3:xyz,"
|
33
|
+
```
|
34
|
+
|
35
|
+
Load concatenated netstrings:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
netstring = "1:x,3:xyz,"
|
39
|
+
string1 = Netstring.load(netstring1) # "x"
|
40
|
+
offset = string1.netstring.size
|
41
|
+
netstring = netstring[offset..-1]
|
42
|
+
string2 = Netstring.load(netstring) # "xyz"
|
43
|
+
```
|
44
|
+
|
45
|
+
Load concatenated netstrings in a loop:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
netstring = "1:x,3:xyz,"
|
49
|
+
strings = []
|
50
|
+
until netstring.empty?
|
51
|
+
strings << Netstring.load(netstring)
|
52
|
+
netstring = netstring[strings.last.netstring.size..-1]
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
Copyright (c) 2014 James McKinney, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
|
7
|
+
task :default => :spec
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'yard'
|
11
|
+
YARD::Rake::YardocTask.new
|
12
|
+
rescue LoadError
|
13
|
+
task :yard do
|
14
|
+
abort 'YARD is not available. In order to run yard, you must: gem install yard'
|
15
|
+
end
|
16
|
+
end
|
data/USAGE
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
See README.md for full usage details.
|
data/lib/netstring.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
# A netstring parser and emitter.
|
4
|
+
#
|
5
|
+
# @see http://cr.yp.to/proto/netstrings.txt
|
6
|
+
class Netstring < SimpleDelegator
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
# Returns the netstring.
|
10
|
+
#
|
11
|
+
# @return [String] the netstring
|
12
|
+
attr_reader :netstring
|
13
|
+
|
14
|
+
# Dumps a string to a netstring.
|
15
|
+
#
|
16
|
+
# @param [String] s a string
|
17
|
+
# @return [String] a netstring
|
18
|
+
def self.dump(s)
|
19
|
+
unless String === s
|
20
|
+
raise Error, "#{s.inspect} is not a String"
|
21
|
+
end
|
22
|
+
"#{s.size}:#{s},"
|
23
|
+
end
|
24
|
+
|
25
|
+
# Loads a string from a netstring.
|
26
|
+
#
|
27
|
+
# The netstring may be multiple concatenated netstrings.
|
28
|
+
#
|
29
|
+
# The return value is a `Netstring` object, whose `#to_s` method returns the
|
30
|
+
# string, and whose `#offset` method returns the length of the netstring.
|
31
|
+
#
|
32
|
+
# @param [String] n a netstring
|
33
|
+
# @return [Netstring] a string
|
34
|
+
def self.load(n)
|
35
|
+
unless String === n
|
36
|
+
raise Error, "#{n.inspect} is not a String"
|
37
|
+
end
|
38
|
+
match = n.match(/\A(\d+):/)
|
39
|
+
unless match
|
40
|
+
raise Error, 'bad netstring header'
|
41
|
+
end
|
42
|
+
size = Integer(match[1])
|
43
|
+
unless n[match.end(0) + size] == ','
|
44
|
+
raise Error, 'expected "," delimiter'
|
45
|
+
end
|
46
|
+
Netstring.new(n, match.end(0), size)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Initializes a netstring.
|
50
|
+
#
|
51
|
+
# The netstring may be multiple concatenated netstrings.
|
52
|
+
#
|
53
|
+
# @param [String] n a netstring
|
54
|
+
# @param [Integer] start the start of the string in the netstring
|
55
|
+
# @param [Integer] length the length of the string in the netstring
|
56
|
+
def initialize(n, start, length)
|
57
|
+
super(n[start, length])
|
58
|
+
|
59
|
+
@netstring = n[0, start + length + 1]
|
60
|
+
end
|
61
|
+
end
|
data/netstring.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "netstring"
|
5
|
+
s.version = "0.0.2"
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.authors = ["James McKinney"]
|
8
|
+
s.homepage = "http://github.com/jpmckinney/netstring"
|
9
|
+
s.summary = %q{A netstring parser and emitter}
|
10
|
+
s.license = 'MIT'
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
|
17
|
+
s.add_development_dependency('coveralls')
|
18
|
+
s.add_development_dependency('json', '~> 1.8') # to silence coveralls warning
|
19
|
+
s.add_development_dependency('rake')
|
20
|
+
s.add_development_dependency('rspec', '~> 3.1')
|
21
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Netstring do
|
4
|
+
let :strings do
|
5
|
+
{
|
6
|
+
'' => '0:,',
|
7
|
+
'x' => '1:x,',
|
8
|
+
'xy' => '2:xy,',
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
let :netstrings do
|
13
|
+
strings.invert
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '.dump' do
|
17
|
+
it 'should dump a netstring' do
|
18
|
+
strings.each do |string,netstring|
|
19
|
+
expect(Netstring.dump(string)).to eq(netstring)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should not dump a non-string' do
|
24
|
+
{
|
25
|
+
nil => 'nil',
|
26
|
+
true => 'true',
|
27
|
+
false => 'false',
|
28
|
+
1 => '1',
|
29
|
+
1.1 => '1.1',
|
30
|
+
[] => '[]',
|
31
|
+
{} => '{}',
|
32
|
+
}.each do |nonstring,inspect|
|
33
|
+
expect{Netstring.dump(nonstring)}.to raise_error(Netstring::Error, "#{inspect} is not a String")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '.load' do
|
39
|
+
it 'should load a netstring' do
|
40
|
+
netstrings.each do |netstring,string|
|
41
|
+
expect(Netstring.load(netstring)).to eq(string)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should not load a nonstring' do
|
46
|
+
{
|
47
|
+
nil => 'nil',
|
48
|
+
true => 'true',
|
49
|
+
false => 'false',
|
50
|
+
1 => '1',
|
51
|
+
1.1 => '1.1',
|
52
|
+
[] => '[]',
|
53
|
+
{} => '{}',
|
54
|
+
}.each do |nonstring,inspect|
|
55
|
+
expect{Netstring.load(nonstring)}.to raise_error(Netstring::Error, "#{inspect} is not a String")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should not load a non-netstring' do
|
60
|
+
[
|
61
|
+
['1', ':', 'x', ','],
|
62
|
+
['1', ':', 'xy', ','],
|
63
|
+
['2', ':', 'x', ','],
|
64
|
+
].each do |tokens|
|
65
|
+
1.upto(4) do |n|
|
66
|
+
tokens.permutation(n) do |p|
|
67
|
+
nonnetstring = p.join
|
68
|
+
unless ['1:x,'].include?(nonnetstring)
|
69
|
+
expect{Netstring.load(nonnetstring)}.to raise_error(Netstring::Error)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe '#initialize' do
|
78
|
+
it 'should initialize a netstring' do
|
79
|
+
expect(Netstring.new('1:x,2:xy,', 2, 1)).to eq('x')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe '#netstring' do
|
84
|
+
it 'should return the netstring' do
|
85
|
+
expect(Netstring.new('1:x,2:xy,', 2, 1).netstring).to eq('1:x,')
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: netstring
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James McKinney
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: coveralls
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.8'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.1'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- ".gitignore"
|
76
|
+
- ".rspec"
|
77
|
+
- ".travis.yml"
|
78
|
+
- ".yardopts"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- USAGE
|
84
|
+
- lib/netstring.rb
|
85
|
+
- netstring.gemspec
|
86
|
+
- spec/netstring_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: http://github.com/jpmckinney/netstring
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.2.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: A netstring parser and emitter
|
112
|
+
test_files:
|
113
|
+
- spec/netstring_spec.rb
|
114
|
+
- spec/spec_helper.rb
|