string_splitter 0.7.2 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa94b66f61dc3f1970d92b7fa4fb01ba0932262338c124592f539a2f3b4e7ca3
4
- data.tar.gz: 0b415f82cfe372bdc9fd0e72d26beeb5071fc02f31e6fa5c02606d3837680506
3
+ metadata.gz: d922735ed5c3b8acdc9f0fa0d0c439f0293e1b72739dd1f6b9ef9018a332f6c9
4
+ data.tar.gz: b61f3b6e827675abd5fe1457a000735c4ae4a4a11dc858fc705b783820230fce
5
5
  SHA512:
6
- metadata.gz: 5efaba21a21e25b4b3f54a505f35cf16bba346427dfe4694554de6a1e917160e08cab9be65e80fd7d7407ba82cf5e876fff21ca2607e2da4fc137f3907b2322a
7
- data.tar.gz: 37ac5c38859a8ed4036e68d9e588c58c8aa63e6ce2a47066dc1d02b115564cc187c505bec4b066a20981eedb21d210f01d019715ab9d91c9dc88f6e320763a3c
6
+ metadata.gz: f226e28ffb81f405ac986c01bf0cdb4270512d0a595d89d7d77ad1b53ed25dd122dac084887de26e26293d214906709724f018b83353a9928209257f6c80bc9e
7
+ data.tar.gz: f56357c60d8d52ff577a1ee5c4d16bbd4082bf29853dc3593fad3eefd6670a345ed245534944f5972fca1909e7e16526351eab898e4832d7cb2054ec86be4851
@@ -1,8 +1,14 @@
1
+ ## 0.7.3 - 2020-08-24
2
+
3
+ #### Changes
4
+
5
+ - avoid exposing an internal Split method inside blocks
6
+
1
7
  ## 0.7.2 - 2020-08-22
2
8
 
3
9
  #### Fixes
4
10
 
5
- - fix/test default delimiter + `remove_empty_fields`
11
+ - fix/test default delimiter + `remove_empty_fields`
6
12
 
7
13
  ## 0.7.1 - 2020-08-22
8
14
 
data/README.md CHANGED
@@ -90,8 +90,8 @@ ss.split("1:2:3:4:5:6", ":", reject: [1, -1])
90
90
  **Split from the right**
91
91
 
92
92
  ```ruby
93
- ss.rsplit("1:2:3:4:5:6:7:8:9", ":", at: [1..3, 5])
94
- # => ["1:2:3:4", "5:6", "7", "8", "9"]
93
+ ss.rsplit("1:2:3:4:5:6:7:8:9", ":", at: [1..3, -1])
94
+ # => ["1", "2:3:4:5:6", "7", "8", "9"]
95
95
  ```
96
96
 
97
97
  **Split with negative, descending, and infinite ranges**
@@ -297,7 +297,7 @@ currently, Ruby 2.5 and above.
297
297
 
298
298
  # VERSION
299
299
 
300
- 0.7.2
300
+ 0.7.3
301
301
 
302
302
  # SEE ALSO
303
303
 
@@ -19,7 +19,7 @@ require_relative 'string_splitter/version'
19
19
  #
20
20
  # Implementation-wise, we split the string either with String#split, or with a custom
21
21
  # scanner if the delimiter may contain captures (since String#split doesn't handle
22
- # them correctly) and parse the resulting tokens into an array of Split objects with
22
+ # them correctly), and parse the resulting tokens into an array of Split objects with
23
23
  # the following attributes:
24
24
  #
25
25
  # - captures: separator substrings captured by parentheses in the delimiter pattern
@@ -40,6 +40,9 @@ class StringSplitter
40
40
  #
41
41
  # the delimiter is /(\W)/ and the separators are ":" and "::"
42
42
 
43
+ # pull in the StringSplitter::Split#update! method
44
+ using Split::Refinements
45
+
43
46
  ACCEPT_ALL = ->(_split) { true }
44
47
  DEFAULT_DELIMITER = /\s+/.freeze
45
48
  REMOVE = [].freeze
@@ -160,8 +163,8 @@ class StringSplitter
160
163
  # if the splits array is empty, the caller returns this array immediately
161
164
  # without any further processing
162
165
  #
163
- # - splits: an array of hashes containing the lhs, rhs, separator and captured
164
- # separator substrings for each split
166
+ # - splits: an array of Split objects exposing the lhs, rhs, separator and
167
+ # captured separator substrings for each split
165
168
  #
166
169
  # - count: the number of splits
167
170
  #
@@ -274,7 +277,7 @@ class StringSplitter
274
277
  end
275
278
 
276
279
  # takes a string and a delimiter pattern (regex or string) and splits it along
277
- # the delimiter, returning an array of objects (hashes) representing each split.
280
+ # the delimiter, returning an array of objects representing each split.
278
281
  # e.g. for:
279
282
  #
280
283
  # parse("foo:bar:baz:quux", ":")
@@ -282,14 +285,14 @@ class StringSplitter
282
285
  # we return:
283
286
  #
284
287
  # [
285
- # { lhs: "foo", rhs: "bar", separator: ":", captures: [] },
286
- # { lhs: "bar", rhs: "baz", separator: ":", captures: [] },
287
- # { lhs: "baz", rhs: "quux", separator: ":", captures: [] },
288
+ # Split.new(lhs: "foo", rhs: "bar", separator: ":", captures: []),
289
+ # Split.new(lhs: "bar", rhs: "baz", separator: ":", captures: []),
290
+ # Split.new(lhs: "baz", rhs: "quux", separator: ":", captures: []),
288
291
  # ]
289
292
  #
290
293
  def parse(string, delimiter)
291
294
  # has_names = delimiter.is_a?(Regexp) && !delimiter.names.empty?
292
- result = []
295
+ splits = []
293
296
  start = 0
294
297
 
295
298
  # we don't use the argument passed to the +scan+ block here because it's a
@@ -304,15 +307,15 @@ class StringSplitter
304
307
  next if separator.empty? && (index.zero? || after == string.length)
305
308
 
306
309
  lhs = string.slice(start, index - start)
307
- result.last.rhs = lhs unless result.empty?
310
+ splits.last.rhs = lhs unless splits.empty?
308
311
 
309
312
  # this is correct for the last/only match, but gets updated to the next
310
313
  # match's lhs for other matches
311
314
  rhs = match.post_match
312
315
 
313
- # captures = (has_names ? Captures.new(match) : match.captures)
316
+ # captures = has_names ? Captures.new(match) : match.captures
314
317
 
315
- result << Split.new(
318
+ splits << Split.new(
316
319
  captures: match.captures,
317
320
  lhs: lhs,
318
321
  rhs: rhs,
@@ -324,7 +327,7 @@ class StringSplitter
324
327
  start = after
325
328
  end
326
329
 
327
- result
330
+ splits
328
331
  end
329
332
 
330
333
  # returns a lambda which splits at (i.e. accepts or rejects splits at, depending
@@ -341,11 +344,11 @@ class StringSplitter
341
344
  #
342
345
  # and
343
346
  #
344
- # ss.split("1:2:3:4:5:6:7:8:9", ":", -3..)
347
+ # ss.split("1:2:3:4:5:6:7:8:9", ":", at: -3..)
345
348
  #
346
349
  # translates to:
347
350
  #
348
- # ss.split("foo:bar:baz:quux", ":", at: 6..8)
351
+ # ss.split("1:2:3:4:5:6:7:8:9", ":", at: 6..8)
349
352
  #
350
353
  def compile(positions, action, count)
351
354
  # XXX note: we don't use modulo, because we don't want
@@ -2,8 +2,25 @@
2
2
 
3
3
  class StringSplitter
4
4
  class Split
5
+ # expose the +update!+ method as a refinement to StringSplitter but don't
6
+ # expose it to blocks
7
+ #
8
+ # idea based on a suggestion here (as an alternative to a `friend` modifier):
9
+ # https://bugs.ruby-lang.org/issues/12962#note-5
10
+ module Refinements
11
+ refine Split do
12
+ def update!(count:, index:)
13
+ @count = count
14
+ @index = index
15
+ @position = index + 1
16
+ freeze
17
+ end
18
+ end
19
+ end
20
+
5
21
  attr_reader :captures, :count, :index, :lhs, :position, :rhs, :separator
6
22
  attr_writer :rhs
23
+
7
24
  alias pos position
8
25
 
9
26
  def initialize(captures:, lhs:, rhs:, separator:)
@@ -40,12 +57,5 @@ class StringSplitter
40
57
  end
41
58
 
42
59
  alias rpos rposition
43
-
44
- def update!(count:, index:)
45
- @count = count
46
- @index = index
47
- @position = index + 1
48
- freeze
49
- end
50
60
  end
51
61
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class StringSplitter
4
- VERSION = '0.7.2'
4
+ VERSION = '0.7.3'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string_splitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - chocolateboy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-22 00:00:00.000000000 Z
11
+ date: 2020-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler