collapstring 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fc45012860694f408de5cb72005462292eed86df
4
+ data.tar.gz: c6fd4d29cbc2900f6fe2d3a53da1825c633d36a7
5
+ SHA512:
6
+ metadata.gz: aa73236ac7c45e13861d1c1a130c0109468cc3519415de19308a86a51cf14b541331875d0edec53aaa1d1c95bcdf65cf7624658cc5634ab5cfd3e4732e6e888c
7
+ data.tar.gz: 2b777d32c8628b4ee77288354beb7c19d8fb7508cf867cc6592b9d802d8a89d857d2d8c276c15d3e47fbb46796f658b0b7f4551e633ae160b40d9770c23a2e24
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Airbnb, Inc.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Collapstring
2
+
3
+ Collapse quoted strings in strings. Does not support JRuby.
4
+
5
+ ## Why?
6
+
7
+ It's a simple, reliable and fast way to start making MySQL
8
+ requests shorter in a SQL logging pipeline.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'collapstring'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install collapstring
23
+
24
+ ## Usage
25
+
26
+ require 'collapstring'
27
+ Collapstring.collapse 'SELECT "hello \"world\"";'
28
+ Collapstring.collapse! 'SELECT "hello \"world\"";' # _could_ be in-place
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ require 'rake/extensiontask'
5
+
6
+ Rake::ExtensionTask.new 'collapstring'
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs = %w[lib test]
10
+ t.test_files = FileList['test/**/test*.rb']
11
+ end
12
+
13
+ Rake::Task[:test].prerequisites.unshift :compile
14
+
15
+ desc 'Run tests'
16
+ task :default => :test
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path '../lib', __FILE__
3
+ $LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'collapstring'
7
+ spec.version = '0.1.0'
8
+ spec.authors = ['Pierre Carrier']
9
+ spec.email = ['pierre@gcarrier.fr']
10
+ spec.description = 'Collapse quoted strings in strings'
11
+ spec.summary = 'Collapse quoted strings in strings. Does not support JRuby.'
12
+ spec.homepage = 'https://github.com/airbnb/collapstring'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files`.split $/
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename f }
17
+ spec.test_files = spec.files.grep %r{^(test|spec|features)/}
18
+ spec.require_paths = %w[lib]
19
+
20
+ spec.extensions = %w[ext/collapstring/extconf.rb]
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.3'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rake-compiler'
25
+ end
@@ -0,0 +1,130 @@
1
+ #include <assert.h>
2
+ #include <ruby.h>
3
+
4
+ typedef enum {
5
+ C_S_OUT,
6
+ C_S_OUT_BACKSLASH,
7
+ C_S_IN_SINGLE,
8
+ C_S_IN_SINGLE_BACKSLASH,
9
+ C_S_IN_DOUBLE,
10
+ C_S_IN_DOUBLE_BACKSLASH,
11
+ } collapse_state;
12
+
13
+ static VALUE collapstring_collapse_bang(VALUE self, VALUE src)
14
+ {
15
+ Check_Type(src, T_STRING);
16
+
17
+ char *str = RSTRING_PTR(src);
18
+ const size_t len = RSTRING_LEN(src);
19
+
20
+ size_t dst_idx = 0;
21
+ collapse_state state = C_S_OUT;
22
+
23
+ for (size_t src_idx = 0; src_idx < len; src_idx++) {
24
+ const char cur = str[src_idx];
25
+ switch (cur) {
26
+ case '\"':
27
+ switch (state) {
28
+ case C_S_OUT:
29
+ str[dst_idx++] = cur;
30
+ state = C_S_IN_DOUBLE;
31
+ break;
32
+ case C_S_OUT_BACKSLASH:
33
+ case C_S_IN_DOUBLE:
34
+ str[dst_idx++] = cur;
35
+ state = C_S_OUT;
36
+ break;
37
+ case C_S_IN_DOUBLE_BACKSLASH:
38
+ state = C_S_IN_DOUBLE;
39
+ /* fallthrough */
40
+ case C_S_IN_SINGLE:
41
+ break;
42
+ case C_S_IN_SINGLE_BACKSLASH:
43
+ state = C_S_IN_SINGLE;
44
+ break;
45
+ }
46
+ break;
47
+ case '\'':
48
+ switch (state) {
49
+ case C_S_OUT:
50
+ str[dst_idx++] = cur;
51
+ state = C_S_IN_SINGLE;
52
+ break;
53
+ case C_S_OUT_BACKSLASH:
54
+ case C_S_IN_SINGLE:
55
+ str[dst_idx++] = cur;
56
+ state = C_S_OUT;
57
+ break;
58
+ case C_S_IN_SINGLE_BACKSLASH:
59
+ state = C_S_IN_SINGLE;
60
+ break;
61
+ case C_S_IN_DOUBLE_BACKSLASH:
62
+ state = C_S_IN_DOUBLE;
63
+ /* fallthrough */
64
+ case C_S_IN_DOUBLE:
65
+ break;
66
+ }
67
+ break;
68
+ case '\\':
69
+ switch (state) {
70
+ case C_S_IN_SINGLE:
71
+ state = C_S_IN_SINGLE_BACKSLASH;
72
+ break;
73
+ case C_S_IN_SINGLE_BACKSLASH:
74
+ state = C_S_IN_SINGLE;
75
+ break;
76
+ case C_S_IN_DOUBLE:
77
+ state = C_S_IN_DOUBLE_BACKSLASH;
78
+ break;
79
+ case C_S_IN_DOUBLE_BACKSLASH:
80
+ state = C_S_IN_DOUBLE;
81
+ break;
82
+ case C_S_OUT:
83
+ str[dst_idx++] = cur;
84
+ state = C_S_OUT_BACKSLASH;
85
+ break;
86
+ case C_S_OUT_BACKSLASH:
87
+ str[dst_idx++] = cur;
88
+ state = C_S_OUT;
89
+ break;
90
+ }
91
+ break;
92
+ default:
93
+ switch (state) {
94
+ case C_S_IN_SINGLE_BACKSLASH:
95
+ state = C_S_IN_SINGLE;
96
+ break;
97
+ case C_S_IN_DOUBLE_BACKSLASH:
98
+ state = C_S_IN_DOUBLE;
99
+ break;
100
+ case C_S_OUT:
101
+ str[dst_idx++] = cur;
102
+ break;
103
+ case C_S_OUT_BACKSLASH:
104
+ str[dst_idx++] = cur;
105
+ state = C_S_OUT;
106
+ break;
107
+ case C_S_IN_SINGLE:
108
+ case C_S_IN_DOUBLE:
109
+ break;
110
+ }
111
+ }
112
+ }
113
+
114
+ return rb_str_resize(src, dst_idx);
115
+ }
116
+
117
+ static VALUE collapstring_collapse(VALUE self, VALUE src) {
118
+ VALUE dst = rb_obj_clone(src);
119
+ assert(dst);
120
+ collapstring_collapse_bang(self, dst);
121
+ return dst;
122
+ }
123
+
124
+ void Init_collapstring(void)
125
+ {
126
+ VALUE module = rb_define_module("Collapstring");
127
+ assert(module);
128
+ rb_define_module_function(module, "collapse!", collapstring_collapse_bang, 1);
129
+ rb_define_module_function(module, "collapse", collapstring_collapse, 1);
130
+ }
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile 'collapstring/collapstring'
@@ -0,0 +1,63 @@
1
+ require 'collapstring'
2
+ require 'test/unit'
3
+
4
+ class TestCollapstring < Test::Unit::TestCase
5
+ def test_arity
6
+ assert_raise ArgumentError do
7
+ Collapstring.collapse
8
+ end
9
+
10
+ assert_raise ArgumentError do
11
+ Collapstring.collapse('foo', 'bar')
12
+ end
13
+ end
14
+
15
+ def test_type_safety
16
+ assert_raise TypeError do
17
+ Collapstring.collapse 1
18
+ end
19
+ end
20
+
21
+ def test_cases
22
+ assert_equal %[], Collapstring.collapse(%[])
23
+
24
+ assert_equal %[noop], Collapstring.collapse(%[noop])
25
+
26
+ assert_equal %[''], Collapstring.collapse(%['foo'])
27
+ assert_equal %[""], Collapstring.collapse(%["foo"])
28
+
29
+ assert_equal %[single \\], Collapstring.collapse(%[single \\])
30
+ assert_equal %[single '], Collapstring.collapse(%[single '])
31
+ assert_equal %[single "], Collapstring.collapse(%[single "])
32
+
33
+ assert_equal %[''], Collapstring.collapse(%['a\\'b'])
34
+ assert_equal %[''], Collapstring.collapse(%['a\\"b'])
35
+ assert_equal %[""], Collapstring.collapse(%["a\\'b"])
36
+ assert_equal %[""], Collapstring.collapse(%["a\\'b"])
37
+
38
+ assert_equal %[a\\b], Collapstring.collapse(%[a\\b])
39
+ assert_equal %[a\\\\b], Collapstring.collapse(%[a\\\\b])
40
+
41
+ assert_equal %[\\'], Collapstring.collapse(%[\\'])
42
+ assert_equal %[\\"], Collapstring.collapse(%[\\"])
43
+
44
+ assert_equal %[""], Collapstring.collapse(%["\\a"])
45
+ assert_equal %[''], Collapstring.collapse(%['\\a'])
46
+
47
+ assert_equal %[\\a '], Collapstring.collapse(%[\\a '])
48
+ assert_equal %[\\a "], Collapstring.collapse(%[\\a "])
49
+ end
50
+
51
+ def test_long_str
52
+ long_str = 'a' * 1_000_000
53
+ assert_equal long_str, Collapstring.collapse!(long_str)
54
+ assert_equal long_str, Collapstring.collapse(long_str)
55
+ end
56
+
57
+ def test_preserves_original_string
58
+ original = 'foo "bar" baz'
59
+ copy = original.dup
60
+ assert_equal 'foo "" baz', Collapstring.collapse(copy)
61
+ assert_equal original, copy
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: collapstring
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pierre Carrier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
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
+ description: Collapse quoted strings in strings
56
+ email:
57
+ - pierre@gcarrier.fr
58
+ executables: []
59
+ extensions:
60
+ - ext/collapstring/extconf.rb
61
+ extra_rdoc_files: []
62
+ files:
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - collapstring.gemspec
68
+ - ext/collapstring/collapstring.c
69
+ - ext/collapstring/extconf.rb
70
+ - test/collapstring/tests.rb
71
+ homepage: https://github.com/airbnb/collapstring
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.0.14
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Collapse quoted strings in strings. Does not support JRuby.
95
+ test_files:
96
+ - test/collapstring/tests.rb