Class: StringSplitter

Inherits:
Object
  • Object
show all
Defined in:
lib/string_splitter.rb,
lib/string_splitter/split.rb,
lib/string_splitter/version.rb

Overview

This class extends the functionality of String#split by:

- providing full control over which splits are accepted or rejected

- adding support for splitting from right-to-left

- encapsulating splitting options/preferences in the splitter rather
  than trying to cram them into overloaded method parameters

These enhancements allow splits to handle many cases that otherwise require bigger guns, e.g. regex matching or parsing.

Implementation-wise, we split the string either with String#split, or with a custom scanner if the delimiter may contain captures (since String#split doesn’t handle them correctly), and parse the resulting tokens into an array of Split objects with the following attributes:

- captures:  separator substrings captured by parentheses in the delimiter pattern
- count:     the number of splits
- index:     the 0-based index of the split in the array
- lhs:       the string to the left of the separator (back to the previous split candidate)
- position:  the 1-based index of the split in the array (alias: pos)
- rhs:       the string to the right of the separator (up to the next split candidate)
- rindex:    the 0-based index of the split relative to the end of the array
- rposition: the 1-based index of the split relative to the end of the array (alias: rpos)
- separator: the string matched by the delimiter pattern/string

Defined Under Namespace

Classes: Split

Constant Summary collapse

ACCEPT_ALL =
->(_split) { true }
DEFAULT_DELIMITER =
/\s+/.freeze
REMOVE =
[].freeze
VERSION =
'0.7.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_delimiter: DEFAULT_DELIMITER, include_captures: true, remove_empty: false, remove_empty_fields: remove_empty, spread_captures: true) ⇒ StringSplitter

Returns a new instance of StringSplitter.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/string_splitter.rb', line 59

def initialize(
  default_delimiter: DEFAULT_DELIMITER,
  include_captures: true,
  remove_empty: false, # TODO remove this
  remove_empty_fields: remove_empty,
  spread_captures: true
)
  @default_delimiter = default_delimiter
  @include_captures = include_captures
  @remove_empty_fields = remove_empty_fields
  @spread_captures = spread_captures
end

Instance Attribute Details

#default_delimiterObject (readonly)

Returns the value of attribute default_delimiter.



72
73
74
# File 'lib/string_splitter.rb', line 72

def default_delimiter
  @default_delimiter
end

#include_capturesObject (readonly)

Returns the value of attribute include_captures.



72
73
74
# File 'lib/string_splitter.rb', line 72

def include_captures
  @include_captures
end

#remove_empty_fieldsObject (readonly) Also known as: remove_empty

Returns the value of attribute remove_empty_fields.



72
73
74
# File 'lib/string_splitter.rb', line 72

def remove_empty_fields
  @remove_empty_fields
end

#spread_capturesObject (readonly)

Returns the value of attribute spread_captures.



72
73
74
# File 'lib/string_splitter.rb', line 72

def spread_captures
  @spread_captures
end

Instance Method Details

#rsplit(string, delimiter = @default_delimiter, at: nil, except: nil, select: at, reject: except, &block) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/string_splitter.rb', line 119

def rsplit(
  string,
  delimiter = @default_delimiter,
  at: nil, # alias for select
  except: nil, # alias for reject
  select: at,
  reject: except,
  &block
)
  result, splits, count, accept = init(
    string: string,
    delimiter: delimiter,
    select: select,
    reject: reject,
    block: block
  )

  return result unless splits

  result.unshift(splits.last.rhs)

  splits.reverse_each.with_index do |split, index|
    split.update!(count: count, index: index)

    if accept.call(split)
      # [lhs + captures] + result
      result.unshift(split.lhs, split.captures)
    else
      # prepend the lhs
      result[0] = split.lhs + split.separator + result[0]
    end
  end

  render(result)
end

#split(string, delimiter = @default_delimiter, at: nil, except: nil, select: at, reject: except, &block) ⇒ Object Also known as: lsplit



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/string_splitter.rb', line 82

def split(
  string,
  delimiter = @default_delimiter,
  at: nil, # alias for select
  except: nil, # alias for reject
  select: at,
  reject: except,
  &block
)
  result, splits, count, accept = init(
    string: string,
    delimiter: delimiter,
    select: select,
    reject: reject,
    block: block
  )

  return result unless splits

  result << splits.first.lhs

  splits.each_with_index do |split, index|
    split.update!(count: count, index: index)

    if accept.call(split)
      result << split.captures << split.rhs
    else
      # append the rhs
      result[-1] = result[-1] + split.separator + split.rhs
    end
  end

  render(result)
end