comment_strip-ruby 0.1.2.1 → 0.2.1

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.
@@ -0,0 +1,297 @@
1
+ # frozen_string_literal: true
2
+ # ######################################################################## #
3
+ # File: comment_strip/language_families/hash_line.rb
4
+ #
5
+ # Purpose: Definition of strip() function for languages that support single
6
+ # line comments beginning at the # character.
7
+ #
8
+ # Created: 1st December 2023
9
+ # Updated: 30th August 2026
10
+ #
11
+ # Home: https://github.com/synesissoftware/comment_strip.r
12
+ #
13
+ # Copyright (c) 2023-2026, Matthew Wilson and Synesis Information Systems
14
+ # All rights reserved.
15
+ #
16
+ # Redistribution and use in source and binary forms, with or without
17
+ # modification, are permitted provided that the following conditions are
18
+ # met:
19
+ #
20
+ # * Redistributions of source code must retain the above copyright notice,
21
+ # this list of conditions and the following disclaimer.
22
+ #
23
+ # * Redistributions in binary form must reproduce the above copyright
24
+ # notice, this list of conditions and the following disclaimer in the
25
+ # documentation and/or other materials provided with the distribution.
26
+ #
27
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
+ #
39
+ # ######################################################################## #
40
+
41
+
42
+ require 'xqsr3/quality/parameter_checking'
43
+
44
+ =begin
45
+ =end
46
+
47
+ module CommentStrip
48
+ module LanguageFamilies
49
+
50
+ module HashLine
51
+
52
+ def self.identifier_character? character
53
+
54
+ return false unless character
55
+
56
+ case character
57
+ when ?a..?z, ?A..?Z, ?0..?9, '_'
58
+ true
59
+ else
60
+ false
61
+ end
62
+ end
63
+
64
+ def self.strip input, lf, **options
65
+
66
+ return input if input.nil?
67
+ return input if input.empty?
68
+
69
+ line = 0
70
+ column = 0
71
+
72
+ # States:
73
+ #
74
+ # - :dq_string - within a double-quoted string
75
+ # - :dq_string_escape - within a double-quoted string when just received a '"', e.g. from immediately after '"the next word is quoted \'
76
+ # - :hash_comment - in a #-comment, i.e. from immediately after "#"
77
+ # - :heredoc_body - within a Ruby heredoc body
78
+ # - :heredoc_header - within a Ruby heredoc header
79
+ # - :percent_string - within a Ruby percent string
80
+ # - :percent_string_open - waiting for the opening delimiter of a Ruby percent string
81
+ # - :sq_string_closing - waiting for final '\'' in a single-quoted string
82
+ # - :sq_string_escape - within a escaped single-quoted string, i.e. from immediately after "'\"
83
+ # - :sq_string_open - within a single-quoted string, i.e. from immediately after "'"
84
+ # - :text - regular part of the code
85
+
86
+ state = :text
87
+
88
+ r = String.new
89
+ r.force_encoding input.encoding
90
+
91
+ percent_open = nil
92
+ percent_closing = nil
93
+ heredoc_terminator = nil
94
+ at_line_start = true
95
+
96
+ i = 0
97
+ while i < input.length
98
+ c = input[i]
99
+ percent_start = false
100
+
101
+ if :text == state && '%' == c
102
+ percent_type = input[i + 1]
103
+ percent_delimiter = input[i + 2]
104
+
105
+ if [ 'q', 'w', ].include?(percent_type) && percent_delimiter
106
+ percent_open = percent_delimiter
107
+ percent_closing = case percent_delimiter
108
+ when '{' then '}'
109
+ when '[' then ']'
110
+ when '(' then ')'
111
+ else percent_open
112
+ end
113
+ percent_start = true
114
+ end
115
+ end
116
+
117
+ if :text == state && '<' == c && '<' == input[i + 1]
118
+ heredoc_offset = i + 2
119
+ heredoc_offset += 1 if '~' == input[heredoc_offset]
120
+ heredoc_quote = input[heredoc_offset]
121
+ heredoc_quote = nil unless [ '\'', '"', ].include?(heredoc_quote)
122
+ heredoc_offset += 1 if [ '\'', '"', ].include?(heredoc_quote)
123
+ heredoc_start = heredoc_offset
124
+
125
+ while heredoc_offset < input.length && identifier_character?(input[heredoc_offset])
126
+ heredoc_offset += 1
127
+ end
128
+
129
+ if heredoc_start < heredoc_offset && (!heredoc_quote || heredoc_quote == input[heredoc_offset])
130
+ heredoc_terminator = input[heredoc_start...heredoc_offset]
131
+ state = :heredoc_header
132
+ end
133
+ end
134
+
135
+ case c
136
+ when ?\r, ?\n
137
+
138
+ line += 1
139
+ column = 0
140
+ else
141
+
142
+ column += 1
143
+ end
144
+
145
+ skip = false
146
+
147
+ case c
148
+ when ?\r, ?\n
149
+
150
+ case state
151
+ when :hash_comment
152
+
153
+ state = :text
154
+ when :sq_string_escape, :sq_string_closing
155
+
156
+ state = :text
157
+ when :dq_string_escape
158
+
159
+ state = :dq_string
160
+ when :heredoc_header
161
+
162
+ state = :heredoc_body
163
+ end
164
+ else
165
+
166
+ # special cases:
167
+ #
168
+ # - for escaped single/double quote
169
+ # - for slash-start
170
+
171
+ case state
172
+ when :heredoc_body
173
+
174
+ if at_line_start
175
+ heredoc_match = true
176
+ heredoc_index = 0
177
+
178
+ while heredoc_index < heredoc_terminator.length
179
+ heredoc_match = false unless input[i + heredoc_index] ==
180
+ heredoc_terminator[heredoc_index]
181
+ heredoc_index += 1
182
+ end
183
+
184
+ if heredoc_match && [ nil, ?\r, ?\n, ].include?(input[i + heredoc_terminator.length])
185
+ state = :text
186
+ end
187
+ end
188
+ when :percent_string_open
189
+
190
+ state = :percent_string if c == percent_open
191
+ when :percent_string
192
+
193
+ state = :text if c == percent_closing
194
+ when :sq_string_open
195
+
196
+ case c
197
+ when ?\\
198
+ state = :sq_string_escape
199
+ when ?\'
200
+ state = :text
201
+ end
202
+ when :sq_string_escape
203
+
204
+ state = :sq_string_open
205
+ when :dq_string_escape
206
+
207
+ state = :dq_string
208
+ else
209
+
210
+ case c
211
+ when '#'
212
+
213
+ case state
214
+ when :text
215
+
216
+ state = :hash_comment
217
+ else
218
+
219
+ ;
220
+ end
221
+ when ?\'
222
+
223
+ case state
224
+ when :text
225
+
226
+ state = :sq_string_open
227
+ else
228
+
229
+ ;
230
+ end
231
+ when '"'
232
+
233
+ case state
234
+ when :text
235
+
236
+ state = :dq_string
237
+ when :dq_string
238
+
239
+ state = :text
240
+ else
241
+
242
+ ;
243
+ end
244
+ when ?\\
245
+
246
+ case state
247
+ when :sq_string_open
248
+
249
+ state = :sq_string_escape
250
+ when :sq_string_escape
251
+
252
+ state = :sq_string_closing
253
+ when :dq_string
254
+
255
+ state = :dq_string_escape
256
+ else
257
+
258
+ ;
259
+ end
260
+ else
261
+
262
+ case state
263
+ when :sq_string_escape
264
+
265
+ state = :sq_string_closing
266
+ else
267
+
268
+ ;
269
+ end
270
+ end
271
+ end
272
+ end
273
+
274
+
275
+ case state
276
+ when :hash_comment
277
+
278
+ ;
279
+ else
280
+
281
+ r << c unless skip
282
+ end
283
+
284
+ state = :percent_string_open if percent_start
285
+ at_line_start = ?\r == c || ?\n == c
286
+ i += 1
287
+ end
288
+
289
+ r
290
+ end
291
+ end # module HashLine
292
+
293
+ end # module LanguageFamilies
294
+ end # module CommentStrip
295
+
296
+ # ############################## end of file ############################# #
297
+
@@ -1,14 +1,15 @@
1
+ # frozen_string_literal: true
1
2
  # ######################################################################## #
2
- # File: comment_strip/strip.rb
3
+ # File: comment_strip/strip.rb
3
4
  #
4
- # Purpose: Definition of strip() function
5
+ # Purpose: Definition of strip() function
5
6
  #
6
- # Created: 14th September 2020
7
- # Updated: 11th July 2022
7
+ # Created: 14th September 2020
8
+ # Updated: 19th August 2026
8
9
  #
9
- # Home: http://github.com/synesissoftware/comment_strip.r
10
+ # Home: https://github.com/synesissoftware/comment_strip.r
10
11
  #
11
- # Copyright (c) 2020-2022, Matthew Wilson and Synesis Information Systems
12
+ # Copyright (c) 2020-2026, Matthew Wilson and Synesis Information Systems
12
13
  # All rights reserved.
13
14
  #
14
15
  # Redistribution and use in source and binary forms, with or without
@@ -37,7 +38,15 @@
37
38
  # ######################################################################## #
38
39
 
39
40
 
40
- require File.join(__dir__, 'language_families', 'c')
41
+
42
+ %w{
43
+
44
+ c
45
+ hash_line
46
+ }.each do |name|
47
+
48
+ require File.join(File.dirname(__FILE__), 'language_families', name)
49
+ end
41
50
 
42
51
  require 'xqsr3/quality/parameter_checking'
43
52
 
@@ -49,21 +58,25 @@ module CommentStrip
49
58
  include ::Xqsr3::Quality::ParameterChecking
50
59
 
51
60
  # Strips comments from an input string, according to the rules and
52
- # conventions of a given language-family
61
+ # conventions of a given language-family.
53
62
  #
54
63
  # === Signature
55
64
  #
56
65
  # * *Parameters:*
57
- # - +input+ (::String, +nil+) the input source code
58
- # - +lf+ (::String) the name of the language family. Currently only
59
- # the value +'C'+ is accepted
60
- # - +options+ (::Hash) options that moderate the behaviour
66
+ # - +input+ (+String+, +nil+) the input source code;
67
+ # - +lf+ (+String+) the name of the language family, which must be one of the following listed in the section below;
68
+ # - +options+ (+Hash+) options that moderate the behaviour;
61
69
  #
62
70
  # * *Options:*
63
71
  # None currently defined.
64
72
  #
65
- # === Signature
66
- # (String) The stripped for of the input.
73
+ # === Return
74
+ # (+String+) The stripped for of the input.
75
+ #
76
+ # === Supported language families
77
+ # Currently supported language families:
78
+ # - +'C'+ - including C, C++, C#, Go, Java, Rust;
79
+ # - +'Hash_Line'+ - include Ruby, Python, Ruby, shell;
67
80
  def strip input, lf, **options
68
81
 
69
82
  check_parameter input, 'input', responds_to: [ :each_char, :empty?, :nil?, ], nil: true
@@ -73,9 +86,12 @@ module CommentStrip
73
86
  when 'C'
74
87
 
75
88
  LanguageFamilies::C.strip input, lf, **options
89
+ when 'HASH_LINE'
90
+
91
+ LanguageFamilies::HashLine.strip input, lf, **options
76
92
  else
77
93
 
78
- raise "language family '#{lf}' unrecognised or not supported1"
94
+ raise "language family '#{lf}' unrecognised or not supported"
79
95
  end
80
96
  end
81
97
 
@@ -1,14 +1,15 @@
1
+ # frozen_string_literal: true
1
2
  # ######################################################################## #
2
- # File: comment_strip/version.rb
3
+ # File: comment_strip/version.rb
3
4
  #
4
- # Purpose: Version for comment_strip.r library
5
+ # Purpose: Version for comment_strip.r library
5
6
  #
6
- # Created: 14th September 2020
7
- # Updated: 1st December 2023
7
+ # Created: 14th September 2020
8
+ # Updated: 30th August 2026
8
9
  #
9
- # Home: http://github.com/synesissoftware/comment_strip.r
10
+ # Home: https://github.com/synesissoftware/comment_strip.r
10
11
  #
11
- # Copyright (c) 2020-2023, Matthew Wilson and Synesis Information Systems
12
+ # Copyright (c) 2020-2026, Matthew Wilson and Synesis Information Systems
12
13
  # All rights reserved.
13
14
  #
14
15
  # Redistribution and use in source and binary forms, with or without
@@ -43,7 +44,7 @@
43
44
  module CommentStrip
44
45
 
45
46
  # Current version of the comment_strip.r library
46
- VERSION = '0.1.2.1'
47
+ VERSION = '0.2.1'
47
48
 
48
49
  private
49
50
  VERSION_PARTS_ = VERSION.split(/[.]/).collect { |n| n.to_i } # :nodoc:
@@ -59,4 +60,3 @@ end # module CommentStrip
59
60
 
60
61
  # ############################## end of file ############################# #
61
62
 
62
-
data/lib/comment_strip.rb CHANGED
@@ -1,14 +1,15 @@
1
+ # frozen_string_literal: true
1
2
  # ######################################################################## #
2
- # File: comment_strip.rb
3
+ # File: comment_strip.rb
3
4
  #
4
- # Purpose: Top-level include for comment_strip.r library
5
+ # Purpose: Top-level include for comment_strip.r library
5
6
  #
6
- # Created: 14th September 2020
7
- # Updated: 11th July 2022
7
+ # Created: 14th September 2020
8
+ # Updated: 19th August 2026
8
9
  #
9
- # Home: http://github.com/synesissoftware/comment_strip.r
10
+ # Home: https://github.com/synesissoftware/comment_strip.r
10
11
  #
11
- # Copyright (c) 2020-2022, Matthew Wilson and Synesis Information Systems
12
+ # Copyright (c) 2020-2026, Matthew Wilson and Synesis Information Systems
12
13
  # All rights reserved.
13
14
  #
14
15
  # Redistribution and use in source and binary forms, with or without