File: set_methods.rb

package info (click to toggle)
ruby-character-set 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 416 kB
  • sloc: ansic: 2,597; ruby: 1,290; makefile: 7; sh: 4
file content (96 lines) | stat: -rw-r--r-- 2,792 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class CharacterSet
  module RubyFallback
    module SetMethods
      (Enumerable.instance_methods -
        %i[include? member? to_a] +
        %i[empty? hash length size]).each do |mthd|
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{mthd}(*args, &block)
            @__set.#{mthd}(*args, &block)
          end
        RUBY
      end

      %i[< <= <=> > >= === disjoint? include? intersect? member?
         proper_subset? proper_superset? subset? superset?].each do |mthd|
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{mthd}(enum, &block)
            if enum.is_a?(CharacterSet) || enum.is_a?(CharacterSet::Pure)
              enum = enum.instance_variable_get(:@__set)
            end
            @__set.#{mthd}(enum, &block)
          end
        RUBY
      end

      %i[<< add add? clear delete delete? delete_if each filter! keep_if
         reject! select! subtract].each do |mthd|
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{mthd}(*args, &block)
            result = @__set.#{mthd}(*args, &block)
            result.is_a?(Set) ? self : result
          end
        RUBY
      end

      %i[& + - ^ | difference intersection union].each do |mthd|
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{mthd}(enum, &block)
            if enum.respond_to?(:map)
              enum = enum.map { |el| el.is_a?(String) ? el.ord : el }
            end
            self.class.new(@__set.#{mthd}(enum, &block).to_a)
          end
        RUBY
      end

      unless RUBY_PLATFORM[/java/i]
        def freeze
          @__set.to_a
          @__set.freeze
          super
        end
      end

      def merge(other)
        raise ArgumentError, 'pass an Enumerable' unless other.respond_to?(:each)
        # pass through #add to use the checks in SetMethodAdapters
        other.each { |e| add(e) }
        self
      end

      def ==(other)
        if equal?(other)
          true
        elsif other.instance_of?(self.class)
          @__set == other.instance_variable_get(:@__set)
        elsif other.is_a?(CharacterSet) || other.is_a?(CharacterSet::Pure)
          size == other.size && other.all? { |cp| @__set.include?(cp) }
        else
          false
        end
      end

      def eql?(other)
        return false unless other.is_a?(self.class)

        @__set.eql?(other.instance_variable_get(:@__set))
      end

      def initialize_dup(orig)
        super
        @__set = orig.instance_variable_get(:@__set).dup
      end

      def initialize_clone(orig)
        super
        @__set = orig.instance_variable_get(:@__set).clone
      end

      def to_a(stringify = false)
        result = @__set.to_a
        stringify ? result.map { |cp| cp.chr('utf-8') } : result
      end
    end
  end
end