comment_strip-ruby 0.0.13 → 0.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 365f9e298b8b89aa08070e00af4b79218bff7a4f
4
- data.tar.gz: ab83b0154b585c48128815611fb7d095d2bcb111
2
+ SHA256:
3
+ metadata.gz: 7b8c5c74b1296b3feccf05d110b933c7c6c2a024931dacd9080b0d59f25623b8
4
+ data.tar.gz: bf8eb299b541fb6dba2407408f0b1a663de7adc7b891aa544fa0c2f115ba9831
5
5
  SHA512:
6
- metadata.gz: b2330a16d54e0b3675d76883fb85a03e970ba59aedc37e232763a2b999efae4b10a53ce6fcabb317f3606dfe0e45fa38be3670792e88462fddadcd10a5a4819b
7
- data.tar.gz: a5f0d316237a029cba4cd959ba8276147192cb7d62226654806ad08f60fb9c8d482c8f192ab8f010152250d8dafdf7ab3313846e4c673ae5e845a24677d83e2e
6
+ metadata.gz: d1ee8150a44c22dfca14eb74bc09b1af58c85c698e8efb8f0794762b413418b0996fc2548dfeb297d8f58a54a4763900eb9e1da139c9f2c216024e51d919e324
7
+ data.tar.gz: f6bdf317e9704995260334a4b30a880a003a7c4aedbb6bca83bfc16ca98bb875de7fbcbbbc9081367bfad787a55c802bb94181a35d70c76cc092aba6c77ef0bc
@@ -4,11 +4,11 @@
4
4
  # Purpose: Definition of strip() function
5
5
  #
6
6
  # Created: 14th September 2020
7
- # Updated: 14th September 2020
7
+ # Updated: 10th February 2021
8
8
  #
9
9
  # Home: http://github.com/synesissoftware/comment_strip.r
10
10
  #
11
- # Copyright (c) 2020, Matthew Wilson and Synesis Information Systems
11
+ # Copyright (c) 2020-2021, Matthew Wilson and Synesis Information Systems
12
12
  # All rights reserved.
13
13
  #
14
14
  # Redistribution and use in source and binary forms, with or without
@@ -43,209 +43,229 @@
43
43
 
44
44
  module CommentStrip
45
45
 
46
- # Strips comments
47
- def strip s, lf, *options
46
+ # Strips comments from an input string, according to the rules and
47
+ # conventions of a given language-family
48
+ #
49
+ # === Signature
50
+ #
51
+ # * *Parameters:*
52
+ # - +s+ (::String) the source code
53
+ # - +lf+ (::String) the name of the language family. Currently only
54
+ # the value +'C'+ is accepted
55
+ # - +options+ (::Hash) options that moderate the behaviour
56
+ #
57
+ # * *Options:*
58
+ #
59
+ def self.strip s, lf, **options
60
+
61
+ case lf.upcase
62
+ when 'C'
63
+
64
+ ;
65
+ else
48
66
 
49
- case lf.upcase
50
- when 'C'
67
+ raise "language family '#{lf}' unrecognised"
68
+ end
51
69
 
52
- ;
53
- else
70
+ return nil if s.nil?
71
+ return s if s.empty?
54
72
 
55
- raise "language family '#{lf}' unrecognised"
56
- end
73
+ line = 0
74
+ column = 0
57
75
 
58
- return nil if s.nil?
59
- return s if s.empty?
76
+ state = :text
60
77
 
61
- line = 0
62
- column = 0
78
+ r = ''
63
79
 
64
- state = :text
80
+ cc_lines = 0
65
81
 
66
- r = ''
82
+ s.each_char do |c|
67
83
 
68
- cc_lines = 0
84
+ case c
85
+ when ?\r, ?\n
69
86
 
70
- s.each_char do |c|
87
+ line += 1
88
+ column = 0
89
+ else
71
90
 
72
- case c
73
- when ?\r, ?\n
91
+ column += 1
92
+ end
74
93
 
75
- line += 1
76
- column = 0
77
- else
94
+ skip = false
78
95
 
79
- column += 1
80
- end
96
+ case c
97
+ when ?\r, ?\n
81
98
 
82
- skip = false
99
+ case state
100
+ when :c_comment, :c_comment_star
83
101
 
84
- case c
85
- when ?\r, ?\n
102
+ cc_lines += 1
86
103
 
87
- case state
88
- when :c_comment, :c_comment_star
89
-
90
- cc_lines += 1
104
+ state = :c_comment
105
+ when :cpp_comment
91
106
 
92
- state = :c_comment
93
- when :cpp_comment
107
+ state = :text
108
+ when :sq_string, :sq_string_escape, :sq_string_closing
94
109
 
95
- state = :text
96
- when :sq_string, :sq_string_escape, :sq_string_closing
110
+ state = :text
111
+ when :dq_string_escape
97
112
 
98
- state = :text
99
- when :dq_string_escape
113
+ state = :dq_string
114
+ when :slash_start
100
115
 
101
- state = :dq_string
102
- when :slash_start
116
+ r << '/'
103
117
 
104
- r << '/'
118
+ state = :text
119
+ end
120
+ else
105
121
 
106
- state = :text
107
- end
108
- else
122
+ # special cases:
123
+ #
124
+ # - for escaped single/double quote
125
+ # - for slash-start
126
+ # - for comment-star
109
127
 
110
- # special cases:
111
- #
112
- # - for escaped single/double quote
113
- # - for slash-start
114
- # - for comment-star
128
+ case state
129
+ when :sq_string_open
115
130
 
116
- case state
117
- when :sq_string_open
131
+ state = (?\\ == c) ? :sq_string_escape : :sq_string_closing
132
+ when :sq_string_escape
118
133
 
119
- state = (?\\ == c) ? :sq_string_escape : :sq_string_closing
120
- when :sq_string_escape
134
+ state = :sq_string_closing
135
+ when :dq_string_escape
121
136
 
122
- state = :sq_string_closing
123
- when :dq_string_escape
137
+ state = :dq_string
138
+ when :c_comment_star
124
139
 
125
- state = :dq_string
126
- when :c_comment_star
140
+ case c
141
+ when ?/
127
142
 
128
- case c
129
- when ?/
143
+ r << ?\n * cc_lines
144
+ cc_lines = 0
130
145
 
131
- r << ?\n * cc_lines
132
- cc_lines = 0
146
+ state = :text
147
+ skip = true
148
+ when '*'
133
149
 
134
- state = :text
135
- skip = true
136
- when '*'
150
+ ;
151
+ else
137
152
 
138
- ;
153
+ state = :c_comment
154
+ end
139
155
  else
140
156
 
141
- state = :c_comment
142
- end
143
- else
157
+ if false
158
+ elsif state == :slash_start && ('/' != c && '*' != c)
144
159
 
145
- if false
146
- elsif state == :slash_start && ('/' != c && '*' != c)
160
+ state = :text
161
+ r << '/'
162
+ else
147
163
 
148
- state = :text
149
- r << '/'
150
- else
164
+ case c
165
+ when '/'
151
166
 
152
- case c
153
- when '/'
167
+ case state
168
+ when :text
154
169
 
155
- case state
156
- when :text
170
+ state = :slash_start
171
+ when :slash_start
157
172
 
158
- state = :slash_start
159
- when :slash_start
173
+ state = :cpp_comment
174
+ when :c_comment_star
160
175
 
161
- state = :cpp_comment
162
- when :c_comment_star
176
+ r << ?\n * cc_lines
177
+ cc_lines = 0
163
178
 
164
- r << ?\n * cc_lines
165
- cc_lines = 0
179
+ state = :text
180
+ skip = true
181
+ end
182
+ when '*'
166
183
 
167
- state = :text
168
- skip = true
169
- end
170
- when '*'
184
+ case state
185
+ when :slash_start
171
186
 
172
- case state
173
- when :slash_start
187
+ state = :c_comment
188
+ when :c_comment
174
189
 
175
- state = :c_comment
176
- when :c_comment
190
+ state = :c_comment_star
191
+ else
177
192
 
178
- state = :c_comment_star
179
- else
193
+ end
180
194
 
181
- end
195
+ when ?\'
182
196
 
183
- when ?\'
197
+ case state
198
+ when :text
184
199
 
185
- case state
186
- when :text
200
+ state = :sq_string_open
201
+ when :sq_string_closing
187
202
 
188
- state = :sq_string_open
189
- when :sq_string_closing
203
+ state = :text
204
+ else
190
205
 
191
- state = :text
192
- else
206
+ end
207
+ when '"'
193
208
 
194
- end
195
- when '"'
209
+ case state
210
+ when :text
196
211
 
197
- case state
198
- when :text
212
+ state = :dq_string
213
+ when :dq_string
199
214
 
200
- state = :dq_string
201
- when :dq_string
215
+ state = :text
216
+ else
217
+ end
218
+ when ?\\
202
219
 
203
- state = :text
204
- else
205
- end
206
- when ?\\
220
+ case state
221
+ when :sq_string_open
207
222
 
208
- case state
209
- when :sq_string_open
223
+ state = :sq_string_escape
224
+ when :sq_string_escape
210
225
 
211
- state = :sq_string_escape
212
- when :sq_string_escape
226
+ state = :sq_string
227
+ when :dq_string
213
228
 
214
- state = :sq_string
215
- when :dq_string
229
+ state = :dq_string_escape
230
+ else
216
231
 
217
- state = :dq_string_escape
232
+ end
218
233
  else
219
234
 
220
- end
221
- else
235
+ case state
236
+ when :sq_string_escape
222
237
 
223
- case state
224
- when :sq_string_escape
225
-
226
- state = :sq_string_closing
227
- else
238
+ state = :sq_string_closing
239
+ else
228
240
 
241
+ end
229
242
  end
230
243
  end
231
244
  end
232
245
  end
233
- end
234
246
 
235
- case state
236
- when :slash_start
237
- when :cpp_comment
238
- when :c_comment
239
- when :c_comment_star
247
+ $stderr.puts "[#{line}, #{column}] state=#{state}, c=#{c} (#{c.ord}); skip=#{skip} [#cc lines: #{cc_lines}]\n" if $DEBUG
240
248
 
241
- else
249
+ case state
250
+ when :slash_start
251
+ when :cpp_comment
252
+ when :c_comment
253
+ when :c_comment_star
254
+
255
+ else
242
256
 
243
- r << c unless skip
257
+ r << c unless skip
258
+ end
244
259
  end
260
+
261
+ r
245
262
  end
246
263
 
247
- r
248
- end
264
+ # Strips comments
265
+ def strip s, lf, **options
266
+
267
+ ::CommentStrip.strip s, lf, **options
268
+ end
249
269
 
250
270
  end # module CommentStrip
251
271
 
@@ -4,11 +4,11 @@
4
4
  # Purpose: Version for comment_strip.r library
5
5
  #
6
6
  # Created: 14th September 2020
7
- # Updated: 14th September 2020
7
+ # Updated: 10th February 2021
8
8
  #
9
9
  # Home: http://github.com/synesissoftware/comment_strip.r
10
10
  #
11
- # Copyright (c) 2020, Matthew Wilson and Synesis Information Systems
11
+ # Copyright (c) 2020-2021, Matthew Wilson and Synesis Information Systems
12
12
  # All rights reserved.
13
13
  #
14
14
  # Redistribution and use in source and binary forms, with or without
@@ -43,7 +43,7 @@
43
43
  module CommentStrip
44
44
 
45
45
  # Current version of the comment_strip.r library
46
- VERSION = '0.0.13'
46
+ VERSION = '0.1.0'
47
47
 
48
48
  private
49
49
  VERSION_PARTS_ = VERSION.split(/[.]/).collect { |n| n.to_i } # :nodoc:
@@ -16,6 +16,8 @@ class Test_strip_1 < Test::Unit::TestCase
16
16
  def test_nil
17
17
 
18
18
  assert_nil strip(nil, 'C')
19
+
20
+ assert_nil ::CommentStrip.strip(nil, 'C')
19
21
  end
20
22
 
21
23
  def test_unrecognised_families
@@ -36,12 +38,14 @@ class Test_strip_1 < Test::Unit::TestCase
36
38
  unrecognised_families.each do |family|
37
39
 
38
40
  assert_raise_with_message(::RuntimeError, /family.*unrecognised/) { strip('', family) }
41
+ assert_raise_with_message(::RuntimeError, /family.*unrecognised/) { ::CommentStrip.strip('', family) }
39
42
  end
40
43
  end
41
44
 
42
45
  def test_empty
43
46
 
44
47
  assert_equal "", strip('', 'C')
48
+ assert_equal "", ::CommentStrip.strip('', 'C')
45
49
  end
46
50
 
47
51
  def test_simple_main
@@ -56,6 +60,7 @@ EOF_main
56
60
  expected = input
57
61
 
58
62
  assert_equal expected, strip(input, 'C')
63
+ assert_equal expected, ::CommentStrip.strip(input, 'C')
59
64
  end
60
65
 
61
66
  def test_x_1
@@ -88,6 +93,7 @@ EOF_main
88
93
  EOF_main
89
94
 
90
95
  actual = strip(input, 'C')
96
+ actual = ::CommentStrip.strip(input, 'C')
91
97
 
92
98
  assert_equal expected, actual
93
99
  end
@@ -104,6 +110,7 @@ EOF_main
104
110
  EOF_main
105
111
 
106
112
  actual = strip(input, 'C')
113
+ actual = ::CommentStrip.strip(input, 'C')
107
114
 
108
115
  assert_equal expected, actual
109
116
  end
@@ -120,6 +127,7 @@ EOF_main
120
127
  EOF_main
121
128
 
122
129
  actual = strip(input, 'C')
130
+ actual = ::CommentStrip.strip(input, 'C')
123
131
 
124
132
  assert_equal expected, actual
125
133
  end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comment_strip-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Wilson
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2021-02-10 00:00:00.000000000 Z
@@ -85,7 +85,7 @@ homepage: https://github.com/synesissoftware/comment_strip.r
85
85
  licenses:
86
86
  - BSD-3-Clause
87
87
  metadata: {}
88
- post_install_message:
88
+ post_install_message:
89
89
  rdoc_options: []
90
90
  require_paths:
91
91
  - lib
@@ -100,9 +100,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  - !ruby/object:Gem::Version
101
101
  version: '0'
102
102
  requirements: []
103
- rubyforge_project:
104
- rubygems_version: 2.5.2.3
105
- signing_key:
103
+ rubyforge_project:
104
+ rubygems_version: 2.7.6.2
105
+ signing_key:
106
106
  specification_version: 4
107
107
  summary: comment_strip.r
108
108
  test_files: []