Method: Chars::CharSet#each_substring_with_index
- Defined in:
- lib/chars/char_set.rb
#each_substring_with_index(data, min_length: 4) {|match, index| ... } ⇒ Enumerator
Enumerates over all substrings and their indices within the given string, of minimum length and that are made up of characters from the Chars::CharSet.
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/chars/char_set.rb', line 421 def each_substring_with_index(data, min_length: 4) unless block_given? return enum_for(__method__,data, min_length: min_length) end return if data.size < min_length index = 0 match_start = nil match_end = nil while index < data.size unless match_start if self.include_char?(data[index]) match_start = index end else unless self.include_char?(data[index]) match_end = index match_length = (match_end - match_start) if match_length >= min_length match = data[match_start,match_length] yield match, match_start end match_start = match_end = nil end end index += 1 end # yield the remaining match if match_start yield data[match_start, data.size - match_start], match_start end end |