str_quote 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b7a6a1612b395c4d61c9e953e99c576b9e0f2548
4
- data.tar.gz: a4c0766846005e9f2a69f6e173cb896009fc78ea
3
+ metadata.gz: c7eae570eb1a81fb9456dcfb260b9064a8bafd70
4
+ data.tar.gz: 4d95cf1a67d2e01db773ddf0cf7566f8a5a50110
5
5
  SHA512:
6
- metadata.gz: add3fd56a271d3cfbad75c28cbb420541bd4d7a80f6282840bc527c507a2754eafad642851a0c59dafd9178fc8f07a83353b22c17c4f95c5a44ebca50cff36cb
7
- data.tar.gz: 07de6d0458702b2b060c19102a80511c478fa629c26c6d607875f486f02a5e0ba0cee8ceb3cfea8c70bc87497b017e8878802ee6bd0549dc954e9fc88d308ee3
6
+ metadata.gz: 5be8d4a907594103f1d8a5d055c4e8b3d2af81b12bddad6539349365945e29fc104cf06bf52bd3bde523407c907e5a2c8c4fcd60968a4fba2af2e106fa98eee0
7
+ data.tar.gz: 636d916099d7bc518ce99246490bdb46a6780e21953a2c84ef1aee35828270636285e402ae411423913898d4d9a76cf808a0c1c766d5033c5d97c8b376d79344
@@ -1,22 +1,46 @@
1
1
  module StrQuote
2
2
  module Quote
3
- def quote(quote_char = '"', escape_char = '\\')
4
- unless escape_char.nil?
5
- e = self.gsub(quote_char, "#{escape_char}#{quote_char}")
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(quote_char = '"', escape_char = '\\')
13
- return self if self.length < 2 or self[0] != quote_char or self[-1] != quote_char
14
-
15
- unless escape_char.nil?
16
- self[1..-2].gsub("#{escape_char}#{quote_char}", quote_char)
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[1..-2]
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
@@ -1,3 +1,3 @@
1
1
  module StrQuote
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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.2
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-08-29 00:00:00.000000000 Z
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.6
48
+ rubygems_version: 2.0.7
49
49
  signing_key:
50
50
  specification_version: 4
51
51
  summary: String quoting