kleene 0.7.0 → 0.8.0

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: 674bbda22ddfbc6c4ec1624de621b96c24576cbe8aa656d228697e1d98549cdb
4
- data.tar.gz: ddca6b95201b21359dd23c5d6b4d9591561e9045764ce90b470b6add1c4518b8
3
+ metadata.gz: 03ad3b5293809eb768a2bb47eb3f1a9ccee680d7d71873d75c7511eb9fc711be
4
+ data.tar.gz: cb0e8f6600e878153bf2454cea8e3dd4dc67d1872660bd1a77b0648a5f91efa5
5
5
  SHA512:
6
- metadata.gz: 9392d0b56aa48b8cef4f0337be625d6160d49c3fb0214b034f379a505ee3a142347e8bb5e62a82ad0cb982bb3ca00ad6f9b12f951e00cf48b0447a1b6fb78320
7
- data.tar.gz: 1521365696f470bc249dac8aa77038bc9121ff60499dc9368aaf86224e64af14a06e092aef191bc9571d62d2dd63567a7051f4b07490e4f377eac8d24de83025
6
+ metadata.gz: f7043fb3024741baf02ed48de44d14c616d4bb83315ad23481d0908c962fbd41a56a9ccc8d0444577526baa94933621b43203c29b2cba14192d98d8f5a7246ef
7
+ data.tar.gz: a78f7aee47db290800efa79cb6096d604f30dc249907db8759bb319b29f01d378801395557a9a8760fc3b6d06e2e447335398de053e1594233da62128a3aafb9
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ StyleGuideBaseURL: https://rubystyle.guide
3
+
4
+ Layout/SpaceInsideBlockBraces:
5
+ SpaceBeforeBlockParameters: false
6
+
7
+ Layout/LineLength:
8
+ Max: 160
9
+
10
+ Style/AccessorGrouping:
11
+ EnforcedStyle: separated
12
+
13
+ Style/Encoding:
14
+ Enabled: true
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kleene (0.6.0)
4
+ kleene (0.7.0)
5
5
  activesupport (~> 7.1)
6
6
  regexp_parser (~> 2.8)
7
7
 
data/lib/kleene/kleene.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # this is a port and extension of https://github.com/davidkellis/kleene/
2
2
 
3
- require_relative "./dsl"
4
- require_relative "./nfa"
5
- require_relative "./dfa"
3
+ require_relative './dsl'
4
+ require_relative './nfa'
5
+ require_relative './dfa'
6
6
 
7
7
  module Kleene
8
8
  # The default alphabet consists of the following:
@@ -28,7 +28,6 @@ module Kleene
28
28
  State.new(final, true)
29
29
  end
30
30
 
31
-
32
31
  attr_reader :id # : Int32
33
32
  attr_accessor :final # : Bool
34
33
  attr_accessor :error # : Bool
@@ -76,13 +75,11 @@ module Kleene
76
75
  end
77
76
 
78
77
  def ==(other)
79
- @string == other.string &&
80
- @range == other.range
78
+ @string == other.string && @range == other.range
81
79
  end
82
80
 
83
81
  def eql?(other)
84
82
  self == other
85
83
  end
86
84
  end
87
-
88
85
  end
@@ -1,6 +1,5 @@
1
- require "set"
2
- require "stringio"
3
- require_relative "./kleene"
1
+ require 'set'
2
+ require_relative './kleene'
4
3
 
5
4
  module Kleene
6
5
  class NaiveOnlineRegex
@@ -12,52 +11,92 @@ module Kleene
12
11
  end
13
12
 
14
13
  def reset
15
- @buffer = ""
16
- @matches_per_regex = Hash.new # Hash(Regexp, Set(MatchData))
14
+ @buffer = ''
15
+ @matches_per_regex = {} # Hash(Regexp, Set(OnlineMatch))
17
16
  end
18
17
 
19
18
  # #ingest(input) is the online-style matching interface
20
- def ingest(input, debug = false) # : Set(OnlineMatch)
19
+ def ingest(input, _debug = false) # : Set(OnlineMatch)
21
20
  @buffer << input
22
21
  new_online_matches = Set.new
23
22
  @regexen.each do |regex|
24
23
  existing_matches_for_regex = (@matches_per_regex[regex] ||= Set.new)
25
- scan_matches = @buffer.scan_matches(regex).to_set
26
- new_matches = scan_matches - existing_matches_for_regex # new_matches : Set(MatchData)
24
+ scan_matches = @buffer.scan_matches(regex)
25
+ scan_online_matches = scan_matches.map {|match_data| OnlineMatch.new(regex, match_data) }.to_set
26
+ new_matches = scan_online_matches - existing_matches_for_regex # new_matches : Set(OnlineMatch)
27
27
  existing_matches_for_regex.merge(new_matches)
28
- new_online_matches.merge(new_matches.map {|match_data| OnlineMatch.new(regex, match_data) })
28
+ new_online_matches.merge(new_matches)
29
29
  end
30
30
  resize_buffer!
31
31
  new_online_matches
32
32
  end
33
33
 
34
- def matches # Hash(Regexp, Set(MatchData))
34
+ def matches # Hash(Regexp, Set(OnlineMatch))
35
35
  @matches_per_regex
36
36
  end
37
37
 
38
- def matches_for(regex) # Set(MatchData) | Nil
38
+ def matches_for(regex) # Set(OnlineMatch) | Nil
39
39
  @matches_per_regex[regex]
40
40
  end
41
41
 
42
42
  def resize_buffer!
43
- if @buffer.size > @window_size
44
- @buffer = @buffer[-@window_size..-1]
43
+ return unless @buffer.size > @window_size
44
+
45
+ number_of_chars_at_front_of_buffer_that_should_roll_off = @buffer.size - @window_size
46
+
47
+ @buffer = @buffer[-@window_size..-1]
48
+ drop_matches_that_have_rolled_off(number_of_chars_at_front_of_buffer_that_should_roll_off)
49
+ end
50
+
51
+ def drop_matches_that_have_rolled_off(number_of_chars_at_front_of_buffer_that_rolled_off)
52
+ @matches_per_regex.each do |regex, match_set|
53
+ match_set.reject! {|online_match| online_match.offsets.first < number_of_chars_at_front_of_buffer_that_rolled_off }
45
54
  end
46
55
  end
47
56
  end
48
57
 
49
58
  # A {Regexp, MatchData} pair
50
59
  class OnlineMatch
51
- attr_reader :regex # Regexp
52
- attr_reader :match # MatchData
60
+ # Regexp # MatchData # Array(Int) -> [start, end] # excludes the end offset
61
+ attr_reader :regex
62
+ attr_reader :match
63
+ attr_reader :offsets # Regexp # MatchData # Array(Int) -> [start, end] # excludes the end offset
64
+
53
65
  def initialize(regex, match)
54
- @regex, @match = regex, match
66
+ @regex, @match, @offsets = regex, match
67
+ @offsets = match.offset(0)
55
68
  end
69
+
70
+ def identity
71
+ [@regex, @offsets, to_a]
72
+ end
73
+
74
+ def ==(other)
75
+ identity == other.identity
76
+ end
77
+
78
+ def eql?(other)
79
+ self == other
80
+ end
81
+
82
+ def hash
83
+ identity.hash
84
+ end
85
+
56
86
  def to_a
57
87
  @match.to_a
58
88
  end
89
+
59
90
  def to_h
60
- {@regex => to_a}
91
+ { @regex => to_a, :offsets => @offsets }
92
+ end
93
+
94
+ def captures
95
+ @match.captures
96
+ end
97
+
98
+ def [](*args)
99
+ @match.method(:[]).call(*args)
61
100
  end
62
101
  end
63
102
  end
@@ -12,14 +12,12 @@ module Enumerable
12
12
  ary = []
13
13
  each do |e|
14
14
  v = block.call(e)
15
- unless v.nil?
16
- ary << v
17
- end
15
+ ary << v unless v.nil?
18
16
  end
19
17
  ary
20
18
  end
21
19
 
22
- alias_method :includes?, :include?
20
+ alias includes? include?
23
21
  end
24
22
 
25
23
  class String
@@ -1,3 +1,3 @@
1
1
  module Kleene
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
data/lib/kleene.rb CHANGED
@@ -1,18 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_support"
4
- require "active_support/core_ext"
5
- require "regexp_parser"
6
- require_relative "kleene/version"
7
- require_relative "kleene/patches"
8
- require_relative "kleene/kleene"
9
- require_relative "kleene/dsl"
10
- require_relative "kleene/nfa"
11
- require_relative "kleene/dfa"
12
- require_relative "kleene/multi_match_dfa"
13
- require_relative "kleene/online_dfa"
14
- require_relative "kleene/naive_online_regex"
15
- require_relative "kleene/parser"
3
+ require 'active_support'
4
+ require 'active_support/core_ext'
5
+ require 'regexp_parser'
6
+ require_relative 'kleene/version'
7
+ require_relative 'kleene/patches'
8
+ require_relative 'kleene/kleene'
9
+ require_relative 'kleene/dsl'
10
+ require_relative 'kleene/nfa'
11
+ require_relative 'kleene/dfa'
12
+ require_relative 'kleene/multi_match_dfa'
13
+ require_relative 'kleene/online_dfa'
14
+ require_relative 'kleene/naive_online_regex'
15
+ require_relative 'kleene/parser'
16
16
 
17
17
  module Kleene
18
18
  class Error < StandardError; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kleene
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Ellis
@@ -47,6 +47,7 @@ extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
49
  - ".rspec"
50
+ - ".rubocop.yml"
50
51
  - Gemfile
51
52
  - Gemfile.lock
52
53
  - LICENSE