Class: Chars::CharSet
- Inherits:
-
Set
- Object
- Set
- Chars::CharSet
- Defined in:
- lib/chars/char_set.rb
Class Method Summary collapse
-
.[](*arguments) ⇒ CharSet
Creates a new CharSet.
Instance Method Summary collapse
-
#<<(other) ⇒ CharSet
Adds a character to the set.
-
#===(other) ⇒ Boolean
(also: #=~)
Compares the bytes within a given string with the bytes of the CharSet.
-
#chars ⇒ Array<String>
The characters within the CharSet.
-
#each_char {|char| ... } ⇒ Enumerator
Iterates over every character within the CharSet.
-
#each_random_byte(n, **kwargs) {|byte| ... } ⇒ Enumerator
Pass random bytes to a given block.
-
#each_random_char(n, **kwargs) {|char| ... } ⇒ Enumerator
Pass random characters to a given block.
-
#each_string_of_length(length) {|string| ... } ⇒ Enumerator
Enumerates through every possible string belonging to the CharSet and of the given length.
-
#each_substring(data, **kwargs) ⇒ Enumerator
Enumerates over all substrings within the given string, of minimum length and that are made up of characters from the CharSet.
-
#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 CharSet.
-
#include_char?(char) ⇒ Boolean
Determines if a character is contained within the CharSet.
-
#initialize(*arguments) ⇒ CharSet
constructor
Creates a new CharSet object.
-
#initialize_copy(other) ⇒ Object
Initializes the copy of another CharSet object.
-
#inspect ⇒ String
Inspects the CharSet.
-
#map_chars {|char| ... } ⇒ Array<String>
Maps the characters of the CharSet.
-
#random_byte(random: Random) ⇒ Integer
Returns a random byte from the CharSet.
-
#random_bytes(length, random: Random) ⇒ Array<Integer>
Creates an Array of random bytes from the CharSet.
-
#random_char(**kwargs) ⇒ String
Returns a random character from the CharSet.
-
#random_chars(length, **kwargs) ⇒ Array<String>
Creates an Array of random characters from the CharSet.
-
#random_distinct_bytes(length, random: Random) ⇒ Array<Integer>
Creates an Array of random non-repeating bytes from the CharSet.
-
#random_distinct_chars(length, **kwargs) ⇒ Array<Integer>
Creates an Array of random non-repeating characters from the CharSet.
-
#random_distinct_string(length, **kwargs) ⇒ String
Creates a String containing randomly selected non-repeating characters from the CharSet.
-
#random_string(length, **kwargs) ⇒ String
Creates a String containing randomly selected characters from the CharSet.
-
#select_chars {|char| ... } ⇒ Array<String>
Selects characters from the CharSet.
-
#strings_in(data, options = {}) {|match, (index)| ... } ⇒ Array, Hash
deprecated
Deprecated.
Use #each_substring_with_index, #substrings_with_indexes, #each_substring, or #substrings instead.
-
#strings_of_length(length) ⇒ Enumerator
Returns an Enumerator that enumerates through every possible string belonging to the CharSet and of the given length.
-
#substrings(data, **kwargs) ⇒ Array<String>
Returns an Array of all substrings within the given string, of minimum length and that are made up of characters from the CharSet.
-
#substrings_with_indexes(data, **kwargs) ⇒ Array<(String, Integer)>
Returns an Array of all substrings and their indices within the given string, of minimum length and that are made up of characters from the CharSet.
- #|(set) ⇒ CharSet (also: #+)
Constructor Details
#initialize(*arguments) ⇒ CharSet
Creates a new CharSet object.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/chars/char_set.rb', line 17 def initialize(*arguments) super() @chars = Hash.new do |hash,key| hash[key] = if key > 0xff key.chr(Encoding::UTF_8) else key.chr(Encoding::ASCII_8BIT) end end arguments.each do |subset| case subset when String, Integer self << subset when Enumerable subset.each { |char| self << char } else raise(TypeError,"arguments must be a String, Integer or Enumerable") end end end |
Class Method Details
.[](*arguments) ⇒ CharSet
Creates a new Chars::CharSet.
65 66 67 |
# File 'lib/chars/char_set.rb', line 65 def self.[](*arguments) new(*arguments) end |
Instance Method Details
#<<(other) ⇒ CharSet
Adds a character to the set.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/chars/char_set.rb', line 83 def <<(other) case other when String other.each_char do |char| byte = char.ord @chars[byte] = char super(byte) end return self when Integer super(other) else raise(TypeError,"can only append Strings and Integers") end end |
#===(other) ⇒ Boolean Also known as: =~
Compares the bytes within a given string with the bytes of the Chars::CharSet.
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 |
# File 'lib/chars/char_set.rb', line 686 def ===(other) case other when String other.each_char.all? { |char| include_char?(char) } when Enumerable other.all? do |element| case element when String include_char?(element) when Integer include_byte?(element) end end else false end end |
#chars ⇒ Array<String>
The characters within the Chars::CharSet.
131 132 133 |
# File 'lib/chars/char_set.rb', line 131 def chars map { |byte| @chars[byte] } end |
#each_char {|char| ... } ⇒ Enumerator
Iterates over every character within the Chars::CharSet.
148 149 150 151 152 |
# File 'lib/chars/char_set.rb', line 148 def each_char return enum_for(__method__) unless block_given? each { |byte| yield @chars[byte] } end |
#each_random_byte(n, **kwargs) {|byte| ... } ⇒ Enumerator
Pass random bytes to a given block.
238 239 240 241 242 243 244 245 |
# File 'lib/chars/char_set.rb', line 238 def each_random_byte(n,**kwargs,&block) return enum_for(__method__,n,**kwargs) unless block_given? n.times do yield random_byte(**kwargs) end return nil end |
#each_random_char(n, **kwargs) {|char| ... } ⇒ Enumerator
Pass random characters to a given block.
268 269 270 271 272 273 274 |
# File 'lib/chars/char_set.rb', line 268 def each_random_char(n,**kwargs,&block) return enum_for(__method__,n,**kwargs) unless block_given? each_random_byte(n,**kwargs) do |byte| yield @chars[byte] end end |
#each_string_of_length(length) {|string| ... } ⇒ Enumerator
Enumerates through every possible string belonging to the Chars::CharSet and of the given length.
625 626 627 628 629 630 631 632 633 634 635 636 |
# File 'lib/chars/char_set.rb', line 625 def each_string_of_length(length,&block) return enum_for(__method__,length) unless block case length when Range, Array length.each do |len| StringEnumerator.new(self,len).each(&block) end else StringEnumerator.new(self,length).each(&block) end end |
#each_substring(data, **kwargs) ⇒ Enumerator
Enumerates over all substrings within the given string, of minimum length and that are made up of characters from the Chars::CharSet.
521 522 523 524 525 526 527 |
# File 'lib/chars/char_set.rb', line 521 def each_substring(data,**kwargs) return enum_for(__method__,data,**kwargs) unless block_given? each_substring_with_index(data,**kwargs) do |substring,index| yield substring end end |
#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.
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 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'lib/chars/char_set.rb', line 435 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 |
#include_char?(char) ⇒ Boolean
Determines if a character is contained within the Chars::CharSet.
117 118 119 120 121 122 123 |
# File 'lib/chars/char_set.rb', line 117 def include_char?(char) unless char.empty? @chars.has_value?(char) || include_byte?(char.ord) else false end end |
#initialize_copy(other) ⇒ Object
Initializes the copy of another Chars::CharSet object.
46 47 48 49 50 |
# File 'lib/chars/char_set.rb', line 46 def initialize_copy(other) super(other) @chars = other.instance_variable_get('@chars').dup end |
#inspect ⇒ String
Inspects the Chars::CharSet.
712 713 714 715 716 717 718 719 720 721 722 723 724 725 |
# File 'lib/chars/char_set.rb', line 712 def inspect "#<#{self.class.name}: {" + map { |byte| case byte when (0x07..0x0d), (0x20..0x7e) @chars[byte].dump when 0x00 # sly hack to make char-sets more friendly # to us C programmers '"\0"' else sprintf("0x%02x",byte) end }.join(', ') + "}>" end |
#map_chars {|char| ... } ⇒ Array<String>
Maps the characters of the Chars::CharSet.
184 185 186 |
# File 'lib/chars/char_set.rb', line 184 def map_chars(&block) each_char.map(&block) end |
#random_byte(random: Random) ⇒ Integer
Returns a random byte from the Chars::CharSet.
197 198 199 |
# File 'lib/chars/char_set.rb', line 197 def random_byte(random: Random) self.entries[random.rand(self.length)] end |
#random_bytes(length, random: Random) ⇒ Array<Integer>
Creates an Array of random bytes from the Chars::CharSet.
288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/chars/char_set.rb', line 288 def random_bytes(length, random: Random) case length when Array Array.new(length.sample(random: random)) do random_byte(random: random) end when Range Array.new(random.rand(length)) do random_byte(random: random) end else Array.new(length) { random_byte(random: random) } end end |
#random_char(**kwargs) ⇒ String
Returns a random character from the Chars::CharSet.
213 214 215 |
# File 'lib/chars/char_set.rb', line 213 def random_char(**kwargs) @chars[random_byte(**kwargs)] end |
#random_chars(length, **kwargs) ⇒ Array<String>
Creates an Array of random characters from the Chars::CharSet.
343 344 345 |
# File 'lib/chars/char_set.rb', line 343 def random_chars(length,**kwargs) random_bytes(length,**kwargs).map { |byte| @chars[byte] } end |
#random_distinct_bytes(length, random: Random) ⇒ Array<Integer>
Creates an Array of random non-repeating bytes from the Chars::CharSet.
315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/chars/char_set.rb', line 315 def random_distinct_bytes(length, random: Random) shuffled_bytes = bytes.shuffle(random: random) case length when Array shuffled_bytes[0,length.sample(random: random)] when Range shuffled_bytes[0,random.rand(length)] else shuffled_bytes[0,length] end end |
#random_distinct_chars(length, **kwargs) ⇒ Array<Integer>
Creates an Array of random non-repeating characters from the Chars::CharSet.
385 386 387 |
# File 'lib/chars/char_set.rb', line 385 def random_distinct_chars(length,**kwargs) random_distinct_bytes(length,**kwargs).map { |byte| @chars[byte] } end |
#random_distinct_string(length, **kwargs) ⇒ String
Creates a String containing randomly selected non-repeating characters from the Chars::CharSet.
407 408 409 |
# File 'lib/chars/char_set.rb', line 407 def random_distinct_string(length,**kwargs) random_distinct_chars(length,**kwargs).join end |
#random_string(length, **kwargs) ⇒ String
Creates a String containing randomly selected characters from the Chars::CharSet.
365 366 367 |
# File 'lib/chars/char_set.rb', line 365 def random_string(length,**kwargs) random_chars(length,**kwargs).join end |
#select_chars {|char| ... } ⇒ Array<String>
Selects characters from the Chars::CharSet.
167 168 169 |
# File 'lib/chars/char_set.rb', line 167 def select_chars(&block) each_char.select(&block) end |
#strings_in(data, options = {}) {|match, (index)| ... } ⇒ Array, Hash
Use #each_substring_with_index, #substrings_with_indexes, #each_substring, or #substrings instead.
Finds sub-strings within given data that are made of characters within the Chars::CharSet.
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
# File 'lib/chars/char_set.rb', line 588 def strings_in(data,={},&block) kwargs = {min_length: .fetch(:length,4)} unless block if [:offsets] return Hash[substrings_with_indexes(data,**kwargs)] else return substrings(data,**kwargs) end end case block.arity when 2 each_substring_with_index(data,**kwargs,&block) else each_substring(data,**kwargs,&block) end end |
#strings_of_length(length) ⇒ Enumerator
Returns an Enumerator that enumerates through every possible string belonging to the Chars::CharSet and of the given length.
649 650 651 |
# File 'lib/chars/char_set.rb', line 649 def strings_of_length(length) each_string_of_length(length) end |
#substrings(data, **kwargs) ⇒ Array<String>
Returns an Array of all substrings within the given string, of minimum length and that are made up of characters from the Chars::CharSet.
549 550 551 |
# File 'lib/chars/char_set.rb', line 549 def substrings(data,**kwargs) each_substring(data,**kwargs).to_a end |
#substrings_with_indexes(data, **kwargs) ⇒ Array<(String, Integer)>
Returns an Array of all substrings and their indices within the given string, of minimum length and that are made up of characters from the Chars::CharSet.
497 498 499 |
# File 'lib/chars/char_set.rb', line 497 def substrings_with_indexes(data,**kwargs) each_substring_with_index(data,**kwargs).to_a end |
#|(set) ⇒ CharSet Also known as: +
Creates a new CharSet object by unioning the Chars::CharSet with another Chars::CharSet.
663 664 665 666 667 |
# File 'lib/chars/char_set.rb', line 663 def |(set) set = CharSet.new(set) unless set.kind_of?(CharSet) return super(set) end |