dot-properties 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -58,6 +58,7 @@ See the spec tests and fixture data for more examples.
58
58
 
59
59
  ## History
60
60
 
61
+ - <b>0.1.2</b> - Improved escaping and Unicode (\uXXXX) support
61
62
  - <b>0.1.1</b> - Fix mishandled keys with leading whitespace (#1)
62
63
  - <b>0.1.0</b> - Initial release
63
64
 
@@ -111,7 +111,7 @@ class DotProperties
111
111
  if item[:value].nil? or item[:value].empty?
112
112
  escape(item[:key])
113
113
  else
114
- "#{escape(item[:key])}#{item[:delimiter]}#{item[:value]}"
114
+ "#{escape(item[:key])}#{item[:delimiter]}#{encode(item[:value])}"
115
115
  end
116
116
  else
117
117
  item[:value]
@@ -125,7 +125,7 @@ class DotProperties
125
125
  { type: :blank, value: item }
126
126
  else
127
127
  key, delimiter, value = item.strip.split /(\s*(?<!\\)[\s:=]\s*)/, 2
128
- { type: :value, key: unescape(key), delimiter: delimiter, value: value.to_s.gsub(/\\\n\s*/,'') }
128
+ { type: :value, key: unescape(decode(key)), delimiter: delimiter, value: unescape(decode(value.to_s.gsub(/\\\n\s*/,''))) }
129
129
  end
130
130
  end
131
131
 
@@ -133,12 +133,20 @@ class DotProperties
133
133
  @content.find { |item| item[:type] == :value and item[:key] == key }
134
134
  end
135
135
 
136
- def escape(key)
137
- key.gsub(/[\s:=]/) { |m| "\\#{m}" }
136
+ def encode(v)
137
+ v.gsub(/[\r\n]/) { |m| "\\u#{'%4.4X' % m.codepoints.to_a}" }.gsub(/\\/,'\\'*3)
138
138
  end
139
139
 
140
- def unescape(key)
141
- key.gsub(/\\/,'')
140
+ def decode(v)
141
+ v.gsub(/\\u([0-9A-Fa-f]{4})/) { |m| $1.hex.chr('UTF-8') }
142
+ end
143
+
144
+ def escape(v)
145
+ v.gsub(/[\s:=\\]/) { |m| "\\#{m}" }
146
+ end
147
+
148
+ def unescape(v)
149
+ v.gsub(/(?<!\\)\\/,'')
142
150
  end
143
151
 
144
152
  end
@@ -1,3 +1,3 @@
1
1
  class DotProperties
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -10,9 +10,9 @@ describe DotProperties do
10
10
  let(:propfile) { File.expand_path('../fixtures/sample.properties', __FILE__) }
11
11
  subject { DotProperties.load(propfile) }
12
12
 
13
- let(:properties) { 19 }
14
- let(:comments) { 15 }
15
- let(:blanks) { 10 }
13
+ let(:properties) { 21 }
14
+ let(:comments) { 16 }
15
+ let(:blanks) { 11 }
16
16
 
17
17
  it { should be_an_instance_of(DotProperties) }
18
18
 
@@ -110,6 +110,16 @@ describe DotProperties do
110
110
  end
111
111
  end
112
112
 
113
+ describe "content escapes" do
114
+ it "should parse Java-escaped unicode" do
115
+ expect(subject['unicode']).to eq("Command\t\u2318\nOption\t\u2325")
116
+ end
117
+
118
+ it "should unescape escaped entities" do
119
+ expect(subject['key with:several=escapes']).to eq('value with#several\escapes')
120
+ end
121
+ end
122
+
113
123
  describe "additional methods" do
114
124
  it "#<<" do
115
125
  subject << ""
@@ -43,6 +43,10 @@ bar.empty=
43
43
  baz.empty:
44
44
  quux.empty =
45
45
 
46
+ # Escapes
47
+ unicode=Command\u0009\u2318\u000AOption\u0009\u2325
48
+ key\ with\:several\=es\c\apes:value\ with\#several\\escapes
49
+
46
50
  # References
47
51
  present = This value contains two resolvable references, ${foo.normal} and ${bar.extraspace}
48
52
  missing = This value contains one resolvable reference, ${foo.normal}, and one unresolvable reference, ${quux.missing}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dot-properties
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: