string_splitter 0.7.2 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/README.md +3 -3
- data/lib/string_splitter.rb +17 -14
- data/lib/string_splitter/split.rb +17 -7
- data/lib/string_splitter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d922735ed5c3b8acdc9f0fa0d0c439f0293e1b72739dd1f6b9ef9018a332f6c9
|
4
|
+
data.tar.gz: b61f3b6e827675abd5fe1457a000735c4ae4a4a11dc858fc705b783820230fce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f226e28ffb81f405ac986c01bf0cdb4270512d0a595d89d7d77ad1b53ed25dd122dac084887de26e26293d214906709724f018b83353a9928209257f6c80bc9e
|
7
|
+
data.tar.gz: f56357c60d8d52ff577a1ee5c4d16bbd4082bf29853dc3593fad3eefd6670a345ed245534944f5972fca1909e7e16526351eab898e4832d7cb2054ec86be4851
|
data/CHANGELOG.md
CHANGED
@@ -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
|
-
|
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,
|
94
|
-
# => ["1
|
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.
|
300
|
+
0.7.3
|
301
301
|
|
302
302
|
# SEE ALSO
|
303
303
|
|
data/lib/string_splitter.rb
CHANGED
@@ -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
|
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
|
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
|
-
#
|
286
|
-
#
|
287
|
-
#
|
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
|
-
|
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
|
-
|
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 =
|
316
|
+
# captures = has_names ? Captures.new(match) : match.captures
|
314
317
|
|
315
|
-
|
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
|
-
|
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("
|
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
|
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.
|
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-
|
11
|
+
date: 2020-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|