Module: CharacterSet::RubyFallback::CharacterSetMethods

Included in:
CharacterSet::RubyFallback
Defined in:
lib/character_set/ruby_fallback/character_set_methods.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#case_insensitiveObject



28
29
30
31
32
33
34
35
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 28

def case_insensitive
  new_set = dup
  each do |cp|
    swapped_cps = cp.chr('utf-8').swapcase.codepoints
    swapped_cps.size == 1 && new_set << swapped_cps[0]
  end
  new_set
end

#count_in(string) ⇒ Object



46
47
48
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 46

def count_in(string)
  str!(string).each_codepoint.count { |cp| include?(cp) }
end

#count_in_section(from:, upto: 0x10FFFF) ⇒ Object



89
90
91
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 89

def count_in_section(from:, upto: 0x10FFFF)
  count { |cp| cp >= from && cp <= upto }
end

#cover?(string) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 50

def cover?(string)
  str!(string).each_codepoint { |cp| return false unless include?(cp) }
  true
end

#delete_in(string) ⇒ Object



55
56
57
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 55

def delete_in(string)
  make_new_str(string) { |cp, new_str| include?(cp) || (new_str << cp) }
end

#delete_in!(string) ⇒ Object



59
60
61
62
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 59

def delete_in!(string)
  result = delete_in(string)
  result.size == string.size ? nil : string.replace(result)
end

#inversion(include_surrogates: false, upto: 0x10FFFF) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 19

def inversion(include_surrogates: false, upto: 0x10FFFF)
  new_set = self.class.new
  0.upto(upto) do |cp|
    next unless include_surrogates || cp > 0xDFFF || cp < 0xD800
    new_set << cp unless include?(cp)
  end
  new_set
end

#keep_in(string) ⇒ Object



64
65
66
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 64

def keep_in(string)
  make_new_str(string) { |cp, new_str| include?(cp) && (new_str << cp) }
end

#keep_in!(string) ⇒ Object



68
69
70
71
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 68

def keep_in!(string)
  result = keep_in(string)
  result.size == string.size ? nil : string.replace(result)
end

#member_in_plane?(num) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
114
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 111

def member_in_plane?(num)
  validate_plane_number(num)
  ((num * 0x10000)...((num + 1) * 0x10000)).any? { |cp| include?(cp) }
end

#plane(num) ⇒ Object



106
107
108
109
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 106

def plane(num)
  validate_plane_number(num)
  section(from: (num * 0x10000), upto: ((num + 1) * 0x10000) - 1)
end

#planesObject



101
102
103
104
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 101

def planes
  plane_size = 0x10000.to_f
  inject({}) { |hash, cp| hash.merge((cp / plane_size).floor => 1) }.keys
end

#rangesObject



37
38
39
40
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 37

def ranges
  CharacterSet.require_optional_dependency('range_compressor', __method__)
  RangeCompressor.compress(self)
end

#sample(count = nil) ⇒ Object



42
43
44
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 42

def sample(count = nil)
  count.nil? ? to_a(true).sample : to_a(true).sample(count)
end

#scan(string) ⇒ Object



73
74
75
76
77
78
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 73

def scan(string)
  encoding = str!(string).encoding
  string.each_codepoint.inject([]) do |arr, cp|
    include?(cp) ? arr.push(cp.chr(encoding)) : arr
  end
end

#section(from:, upto: 0x10FFFF) ⇒ Object



85
86
87
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 85

def section(from:, upto: 0x10FFFF)
  dup.keep_if { |cp| cp >= from && cp <= upto }
end

#section?(from:, upto: 0x10FFFF) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 93

def section?(from:, upto: 0x10FFFF)
  any? { |cp| cp >= from && cp <= upto }
end

#section_ratio(from:, upto: 0x10FFFF) ⇒ Object



97
98
99
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 97

def section_ratio(from:, upto: 0x10FFFF)
  section(from: from, upto: upto).count / count.to_f
end

#used_by?(string) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 80

def used_by?(string)
  str!(string).each_codepoint { |cp| return true if include?(cp) }
  false
end