str_quote 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/lib/str_quote/quote.rb +34 -10
- data/lib/str_quote/version.rb +1 -1
- data/test/str_quote_test.rb +4 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c7eae570eb1a81fb9456dcfb260b9064a8bafd70
|
4
|
+
data.tar.gz: 4d95cf1a67d2e01db773ddf0cf7566f8a5a50110
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5be8d4a907594103f1d8a5d055c4e8b3d2af81b12bddad6539349365945e29fc104cf06bf52bd3bde523407c907e5a2c8c4fcd60968a4fba2af2e106fa98eee0
|
7
|
+
data.tar.gz: 636d916099d7bc518ce99246490bdb46a6780e21953a2c84ef1aee35828270636285e402ae411423913898d4d9a76cf808a0c1c766d5033c5d97c8b376d79344
|
data/lib/str_quote/quote.rb
CHANGED
@@ -1,22 +1,46 @@
|
|
1
1
|
module StrQuote
|
2
2
|
module Quote
|
3
|
-
def quote(
|
4
|
-
|
5
|
-
|
3
|
+
def quote(opts = {})
|
4
|
+
opts = {quote_char: '"', escape_char: '\\'}.merge opts
|
5
|
+
unless opts[:escape_char].nil?
|
6
|
+
e = self.gsub(opts[:quote_char], "#{opts[:escape_char]}#{opts[:quote_char]}")
|
6
7
|
else
|
7
8
|
e = self
|
8
9
|
end
|
9
|
-
"#{quote_char}#{e}#{quote_char}"
|
10
|
+
"#{opts[:quote_char]}#{e}#{opts[:quote_char]}"
|
10
11
|
end
|
11
12
|
|
12
|
-
def unquote(
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
def unquote(opts = {})
|
14
|
+
opts = {quote_char: ['"',"'"], strip: true, escape_char: '\\'}.merge opts
|
15
|
+
|
16
|
+
# Should we strip this string first?
|
17
|
+
if opts[:strip]
|
18
|
+
s = self.strip
|
17
19
|
else
|
18
|
-
self
|
20
|
+
s = self
|
21
|
+
end
|
22
|
+
|
23
|
+
# Get the quote string into an array, to simplify the logic below
|
24
|
+
if opts[:quote_char].is_a? String
|
25
|
+
opts[:quote_char] = [ opts[:quote_char] ]
|
19
26
|
end
|
27
|
+
|
28
|
+
# If it's too short to quote, we're done.
|
29
|
+
return s if s.length < 2
|
30
|
+
|
31
|
+
# Now it has to be quoted before we do anything.
|
32
|
+
opts[:quote_char].each do |qc|
|
33
|
+
if s[0..qc.length-1] == qc and s[0-qc.length..-1] == qc
|
34
|
+
unless opts[:escape_char].nil?
|
35
|
+
return s[qc.length..0-(qc.length+1)].gsub("#{opts[:escape_char]}#{qc}", qc)
|
36
|
+
else
|
37
|
+
return s[qc.length..0-(qc.length+1)]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Not quoted according to the rules passed to us
|
43
|
+
s
|
20
44
|
end
|
21
45
|
|
22
46
|
alias dequote unquote
|
data/lib/str_quote/version.rb
CHANGED
data/test/str_quote_test.rb
CHANGED
@@ -10,15 +10,15 @@ class StrQuoteTest < Test::Unit::TestCase
|
|
10
10
|
assert_equal 'Hello'.quote, '"Hello"'
|
11
11
|
assert_equal 'Hello world'.quote, '"Hello world"'
|
12
12
|
assert_equal 'Embed"ded'.quote, '"Embed\"ded"'
|
13
|
-
assert_equal "zz zz".quote("'"), "'zz zz'"
|
14
|
-
assert_equal "gr&t".quote('&','#'), '&gr#&t&'
|
13
|
+
assert_equal "zz zz".quote(quote_char:"'"), "'zz zz'"
|
14
|
+
assert_equal "gr&t".quote(quote_char:'&',escape_char:'#'), '&gr#&t&'
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_dequote
|
18
18
|
assert_equal 'foobar', '"foobar"'.dequote
|
19
19
|
assert_equal '"Hello world"'.unquote, 'Hello world'
|
20
20
|
assert_equal '"Embed\"ded"'.unquote, 'Embed"ded'
|
21
|
-
assert_equal "'zz zz'".unquote("'"), "zz zz"
|
22
|
-
assert_equal '&gr#&t&'.dequote('&', '#'), "gr&t"
|
21
|
+
assert_equal "'zz zz'".unquote(quote_char:"'"), "zz zz"
|
22
|
+
assert_equal '&gr#&t&'.dequote(quote_char:'&', escape_char:'#'), "gr&t"
|
23
23
|
end
|
24
24
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: str_quote
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Doug Wiegley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Quoting/unquoting of strings, including escaping
|
14
14
|
email:
|
@@ -45,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
45
|
version: '0'
|
46
46
|
requirements: []
|
47
47
|
rubyforge_project:
|
48
|
-
rubygems_version: 2.0.
|
48
|
+
rubygems_version: 2.0.7
|
49
49
|
signing_key:
|
50
50
|
specification_version: 4
|
51
51
|
summary: String quoting
|