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
- #case_insensitive ⇒ Object
- #count_in(string) ⇒ Object
- #count_in_section(from:, upto: 0x10FFFF) ⇒ Object
- #cover?(string) ⇒ Boolean
- #delete_in(string) ⇒ Object
- #delete_in!(string) ⇒ Object
- #inversion(include_surrogates: false, upto: 0x10FFFF) ⇒ Object
- #keep_in(string) ⇒ Object
- #keep_in!(string) ⇒ Object
- #member_in_plane?(num) ⇒ Boolean
- #plane(num) ⇒ Object
- #planes ⇒ Object
- #ranges ⇒ Object
- #sample(count = nil) ⇒ Object
- #scan(string) ⇒ Object
- #section(from:, upto: 0x10FFFF) ⇒ Object
- #section?(from:, upto: 0x10FFFF) ⇒ Boolean
- #section_ratio(from:, upto: 0x10FFFF) ⇒ Object
- #used_by?(string) ⇒ Boolean
Instance Method Details
#case_insensitive ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 24 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
42 43 44 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 42 def count_in(string) utf8_str!(string).each_codepoint.count { |cp| include?(cp) } end |
#count_in_section(from:, upto: 0x10FFFF) ⇒ Object
88 89 90 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 88 def count_in_section(from:, upto: 0x10FFFF) count { |cp| cp >= from && cp <= upto } end |
#cover?(string) ⇒ Boolean
46 47 48 49 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 46 def cover?(string) utf8_str!(string).each_codepoint { |cp| return false unless include?(cp) } true end |
#delete_in(string) ⇒ Object
51 52 53 54 55 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 51 def delete_in(string) utf8_str!(string).each_codepoint.with_object('') do |cp, new_str| include?(cp) || (new_str << cp) end.encode(string.encoding) end |
#delete_in!(string) ⇒ Object
57 58 59 60 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 57 def delete_in!(string) result = delete_in(string) result.size == string.size ? nil : string.replace(result) end |
#inversion(include_surrogates: false, upto: 0x10FFFF) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 15 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
62 63 64 65 66 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 62 def keep_in(string) utf8_str!(string).each_codepoint.with_object('') do |cp, new_str| include?(cp) && (new_str << cp) end.encode(string.encoding) 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
110 111 112 113 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 110 def member_in_plane?(num) validate_plane_number(num) ((num * 0x10000)...((num + 1) * 0x10000)).any? { |cp| include?(cp) } end |
#plane(num) ⇒ Object
105 106 107 108 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 105 def plane(num) validate_plane_number(num) section(from: (num * 0x10000), upto: ((num + 1) * 0x10000) - 1) end |
#planes ⇒ Object
100 101 102 103 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 100 def planes plane_size = 0x10000.to_f inject({}) { |hash, cp| hash.merge((cp / plane_size).floor => 1) }.keys end |
#ranges ⇒ Object
33 34 35 36 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 33 def ranges CharacterSet.require_optional_dependency('range_compressor', __method__) RangeCompressor.compress(self) end |
#sample(count = nil) ⇒ Object
38 39 40 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 38 def sample(count = nil) count.nil? ? to_a(true).sample : to_a(true).sample(count) end |
#scan(string) ⇒ Object
73 74 75 76 77 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 73 def scan(string) utf8_str!(string).each_codepoint.with_object([]) do |cp, arr| arr.push(cp.chr('utf-8')) if include?(cp) end end |
#section(from:, upto: 0x10FFFF) ⇒ Object
84 85 86 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 84 def section(from:, upto: 0x10FFFF) dup.keep_if { |cp| cp >= from && cp <= upto } end |
#section?(from:, upto: 0x10FFFF) ⇒ Boolean
92 93 94 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 92 def section?(from:, upto: 0x10FFFF) any? { |cp| cp >= from && cp <= upto } end |
#section_ratio(from:, upto: 0x10FFFF) ⇒ Object
96 97 98 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 96 def section_ratio(from:, upto: 0x10FFFF) section(from: from, upto: upto).count / count.to_f end |
#used_by?(string) ⇒ Boolean
79 80 81 82 |
# File 'lib/character_set/ruby_fallback/character_set_methods.rb', line 79 def used_by?(string) utf8_str!(string).each_codepoint { |cp| return true if include?(cp) } false end |