rvs 0.1.1 → 0.1.2

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 (4) hide show
  1. data/lib/rvs/parse.rb +48 -31
  2. data/lib/rvs/to_rvs.rb +28 -20
  3. data/test/rvs.dt.rb +41 -21
  4. metadata +4 -4
data/lib/rvs/parse.rb CHANGED
@@ -12,20 +12,58 @@ class Parser
12
12
  end
13
13
 
14
14
  def parse_item
15
- next_char = @scan.peek(1)
15
+ next_char = @scan.getch
16
16
  if next_char == '['
17
17
  parse_array
18
18
  elsif next_char == '{'
19
19
  parse_hash
20
+ elsif next_char == '"'
21
+ if @scan.peek(1) == '"'
22
+ get_chars(1)
23
+ ''
24
+ else
25
+ reverse_special_chars(@scan.scan(/.*?"/).chop)
26
+ end
27
+ elsif next_char == 's'
28
+ reverse_special_chars(get_chars(@scan.scan_until(/:/).chop.to_i))
29
+ elsif next_char == ':'
30
+ @scan.scan(/[a-zA-z_]+\w*/).to_sym
31
+ elsif next_char == 'd'
32
+ strval = @scan.scan(/[0-9-]+/)
33
+ if @scan.peek(1) == ' '
34
+ strval += get_chars(9)
35
+ DateTime.strptime(strval, '%Y-%m-%d %H:%M:%S')
36
+ else
37
+ Date.strptime(strval, '%Y-%m-%d')
38
+ end
39
+ elsif next_char == 'f'
40
+ if @scan.peek(1) == 'a'
41
+ get_chars(4)
42
+ false
43
+ else
44
+ @scan.scan(/-?[0-9\.]+/).to_f
45
+ end
46
+ elsif next_char == 't'
47
+ if @scan.peek(1) == 'r'
48
+ get_chars(3)
49
+ true
50
+ else
51
+ Time.strptime(get_chars(19), '%Y-%m-%d %H:%M:%S')
52
+ end
53
+ elsif next_char == 'c'
54
+ BigDecimal(@scan.scan(/-?[0-9\.]+/))
55
+ elsif next_char == 'n'
56
+ get_chars(2)
57
+ nil
58
+ elsif next_char =~ /[0-9-]/
59
+ @scan.pos = @scan.pos - 1
60
+ @scan.scan(/-?\d+/).to_i
20
61
  else
21
- parse_value
62
+ raise "unexpected type identifier *#{next_char}*; string remainder is #{@scan.rest}"
22
63
  end
23
64
  end
24
65
 
25
66
  def parse_array
26
- # throw away the opening [
27
- @scan.getch
28
-
29
67
  retval = []
30
68
  while !@scan.eos?
31
69
  next_char = @scan.peek(1)
@@ -42,9 +80,6 @@ class Parser
42
80
  end
43
81
 
44
82
  def parse_hash
45
- # throw away the opening {
46
- @scan.getch
47
-
48
83
  retval = {}
49
84
  curr_key = nil
50
85
  while !@scan.eos?
@@ -54,8 +89,9 @@ class Parser
54
89
  @scan.getch
55
90
  break
56
91
  end
57
- # throw away the comma , or >
58
- @scan.getch if [',','>'].include?(next_char)
92
+ # throw away the following delimiter chars
93
+ @scan.getch if next_char == ','
94
+ get_chars(2) if next_char == '='
59
95
 
60
96
  next_value = parse_item
61
97
  if curr_key
@@ -68,27 +104,8 @@ class Parser
68
104
  retval
69
105
  end
70
106
 
71
- def parse_value
72
- type_identifier = @scan.getch
73
- if type_identifier == 'b'
74
- @scan.getch == '1'
75
- elsif type_identifier == 'd'
76
- Date.strptime(get_chars(8), '%Y%m%d')
77
- elsif type_identifier == 'e'
78
- DateTime.strptime(get_chars(14), '%Y%m%d%H%M%S')
79
- elsif type_identifier == 't'
80
- Time.strptime(get_chars(14), '%Y%m%d%H%M%S')
81
- elsif type_identifier == 's'
82
- get_chars(@scan.scan_until(/:/).chop.to_i)
83
- elsif type_identifier == 'w'
84
- @scan.scan(/\d+/).to_i
85
- elsif type_identifier == 'c'
86
- BigDecimal(@scan.scan(/[0-9\.]+/))
87
- elsif type_identifier == 'z'
88
- nil
89
- else
90
- raise "unexpected type identifier"
91
- end
107
+ def reverse_special_chars(str)
108
+ str.gsub(/\\n/, "\n").gsub(/\\r/, "\r").gsub(/\\t/, "\t")
92
109
  end
93
110
 
94
111
  def get_chars(count)
data/lib/rvs/to_rvs.rb CHANGED
@@ -4,34 +4,30 @@ require 'time'
4
4
 
5
5
  class String
6
6
  def to_rvs
7
- "s#{size}:#{self}"
8
- end
9
- end
10
-
11
- module WholeNumberToRvs
12
- def to_rvs
13
- "w#{self}"
7
+ raise "non-ascii strings aren't currently supported" unless ascii_only?
8
+ no_specials = gsub(/\n/, '\n').gsub(/\r/, '\r').gsub(/\t/, '\t')
9
+ if index('"') || index('\\')
10
+ "s#{size}:#{no_specials}"
11
+ else
12
+ %Q{"#{no_specials}"}
13
+ end
14
14
  end
15
15
  end
16
16
 
17
17
  class Fixnum
18
- include WholeNumberToRvs
18
+ alias :to_rvs :to_s
19
19
  end
20
20
 
21
21
  class Bignum
22
- include WholeNumberToRvs
22
+ alias :to_rvs :to_s
23
23
  end
24
24
 
25
25
  class FalseClass
26
- def to_rvs
27
- 'b0'
28
- end
26
+ alias :to_rvs :to_s
29
27
  end
30
28
 
31
29
  class TrueClass
32
- def to_rvs
33
- 'b1'
34
- end
30
+ alias :to_rvs :to_s
35
31
  end
36
32
 
37
33
  class BigDecimal
@@ -42,19 +38,19 @@ end
42
38
 
43
39
  class Date
44
40
  def to_rvs
45
- "d#{strftime('%Y%m%d')}"
41
+ "d#{strftime('%Y-%m-%d')}"
46
42
  end
47
43
  end
48
44
 
49
45
  class DateTime
50
46
  def to_rvs
51
- "e#{strftime('%Y%m%d%H%M%S')}"
47
+ "d#{strftime('%Y-%m-%d %H:%M:%S')}"
52
48
  end
53
49
  end
54
50
 
55
51
  class Time
56
52
  def to_rvs
57
- "t#{strftime('%Y%m%d%H%M%S')}"
53
+ "t#{strftime('%Y-%m-%d %H:%M:%S')}"
58
54
  end
59
55
  end
60
56
 
@@ -69,7 +65,7 @@ class Hash
69
65
  def to_rvs
70
66
  ary = []
71
67
  each_pair do |key, value|
72
- ary.push("#{key.to_rvs}>#{value.to_rvs}")
68
+ ary.push("#{key.to_rvs}=>#{value.to_rvs}")
73
69
  end
74
70
  "{#{ary.join(',')}}"
75
71
  end
@@ -77,6 +73,18 @@ end
77
73
 
78
74
  class NilClass
79
75
  def to_rvs
80
- 'z'
76
+ 'nil'
77
+ end
78
+ end
79
+
80
+ class Symbol
81
+ def to_rvs
82
+ ":#{to_s}"
83
+ end
84
+ end
85
+
86
+ class Float
87
+ def to_rvs
88
+ "f#{to_s}"
81
89
  end
82
90
  end
data/test/rvs.dt.rb CHANGED
@@ -12,63 +12,83 @@ class Test_rvs < DohTest::TestGroup
12
12
  def test_whole_numbers
13
13
  fixnum = 1000
14
14
  assert(fixnum.is_a?(Fixnum))
15
- verify(fixnum, 'w1000')
15
+ verify(fixnum, '1000')
16
16
 
17
17
  bignum = 9999999999999999999999999999999
18
18
  assert(bignum.is_a?(Bignum))
19
- verify(bignum, 'w9999999999999999999999999999999')
19
+ verify(bignum, '9999999999999999999999999999999')
20
+
21
+ verify(-5, '-5')
20
22
  end
21
23
 
22
24
  def test_boolean
23
- verify(true, 'b1')
24
- verify(false, 'b0')
25
+ verify(true, 'true')
26
+ verify(false, 'false')
25
27
  end
26
28
 
27
29
  def test_bigdecimal
28
30
  verify(BigDecimal('100'), 'c100.0')
29
31
  verify(BigDecimal('100.14'), 'c100.14')
32
+ verify(BigDecimal('-100.14'), 'c-100.14')
30
33
  verify(BigDecimal('100.143912981212381923'), 'c100.143912981212381923')
31
34
  end
32
35
 
36
+ def test_float
37
+ verify(100.0, 'f100.0')
38
+ verify(100.14, 'f100.14')
39
+ verify(-100.14, 'f-100.14')
40
+ end
41
+
33
42
  def test_date
34
- verify(Date.new(2012,2,9), 'd20120209')
43
+ verify(Date.new(2012,2,9), 'd2012-02-09')
35
44
  end
36
45
 
37
46
  def test_datetime
38
- verify(DateTime.new(2012,2,9,1,5,7), 'e20120209010507')
39
- verify(DateTime.new(2012,2,9,17,39,45), 'e20120209173945')
47
+ verify(DateTime.new(2012,2,9,1,5,7), 'd2012-02-09 01:05:07')
48
+ verify(DateTime.new(2012,2,9,17,39,45), 'd2012-02-09 17:39:45')
40
49
  end
41
50
 
42
51
  def test_time
43
- verify(Time.new(2012,2,9,1,5,7), 't20120209010507')
44
- verify(Time.new(2012,2,9,17,39,45), 't20120209173945')
52
+ verify(Time.new(2012,2,9,1,5,7), 't2012-02-09 01:05:07')
53
+ verify(Time.new(2012,2,9,17,39,45), 't2012-02-09 17:39:45')
45
54
  end
46
55
 
47
56
  def test_array
48
- verify([1, 2], '[w1,w2]')
49
- verify([1, [2,3]], '[w1,[w2,w3]]')
50
- verify([1, [2,[3,4]]], '[w1,[w2,[w3,w4]]]')
57
+ verify([1, 2], '[1,2]')
58
+ verify([1, [2,3]], '[1,[2,3]]')
59
+ verify([1, [2,[3,4]]], '[1,[2,[3,4]]]')
51
60
  end
52
61
 
53
62
  def test_hash
54
- verify({1 => 2}, '{w1>w2}')
55
- verify({1 => 2, 3 => 4}, '{w1>w2,w3>w4}')
56
- verify({1 => 2, 3 => {4 => 5}}, '{w1>w2,w3>{w4>w5}}')
63
+ verify({1 => 2}, '{1=>2}')
64
+ verify({1 => 2, 3 => 4}, '{1=>2,3=>4}')
65
+ verify({1 => 2, 3 => {4 => 5}}, '{1=>2,3=>{4=>5}}')
57
66
  end
58
67
 
59
68
  def test_mixed
60
- verify({1 => ['blah'], ['blee',nil] => {4 => 5}}, '{w1>[s4:blah],[s4:blee,z]>{w4>w5}}')
69
+ verify({1 => ['blah'], ['blee',nil, -4] => {:yeh => 5}}, '{1=>["blah"],["blee",nil,-4]=>{:yeh=>5}}')
61
70
  end
62
71
 
63
72
  def test_nil
64
- verify(nil, 'z')
73
+ verify(nil, 'nil')
74
+ end
75
+
76
+ def test_symbol
77
+ verify(:blah, ':blah')
78
+ end
79
+
80
+ def test_nonempty_string_following_empty_string
81
+ verify(['', 'blah'], '["","blah"]')
65
82
  end
66
83
 
67
84
  def test_string
68
- verify('', 's0:')
69
- verify('blah', 's4:blah')
70
- verify('blahblee', 's8:blahblee')
71
- verify('blahb\'lee', "s9:blahb'lee")
85
+ verify('', %q{""})
86
+ verify('blah', %q{"blah"})
87
+ verify('blahblee', %q{"blahblee"})
88
+ verify("blahblee\n\r\t", %q{"blahblee\n\r\t"})
89
+ verify('blahb\'lee', %q{"blahb'lee"})
90
+ verify('blahb\\lee', %q{s9:blahb\lee})
91
+ verify('blahb"lee', %q{s9:blahb"lee})
72
92
  verify('blahb\'le"e', %q{s10:blahb'le"e})
73
93
  end
74
94
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rvs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-02-23 00:00:00.000000000Z
13
+ date: 2012-02-24 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: dohtest
17
- requirement: &70093553266680 !ruby/object:Gem::Requirement
17
+ requirement: &70310398318060 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: 0.1.4
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70093553266680
25
+ version_requirements: *70310398318060
26
26
  description: serialization of common ruby value classes to ascii text
27
27
  email:
28
28
  - devinfo@atpsoft.com