Class: String

Inherits:
Object show all
Defined in:
lib/arachni/ruby/string.rb

Overview

Overloads the String class.

Author:

Instance Method Summary collapse

Instance Method Details

#diff_ratio(other) ⇒ Float

Calculates the difference ratio (at a word level) between self and other.

Parameters:

Returns:

  • (Float)

    0.0 (identical strings) to 1.0 (completely different)



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/arachni/ruby/string.rb', line 64

def diff_ratio( other )
    return 0.0 if self == other

    s_words = self.words( true )
    o_words = other.words( true )

    common = (s_words & o_words).size.to_f
    union  = (s_words | o_words).size.to_f

    (union - common)/union
end

#rdiff(other) ⇒ String

Gets the reverse diff between self and str on a word level.

str = "This is the first test.\nNot really sure what else to put here...\n"

str2 = "This is the second test.\nNot really sure what else to put here...\nBoo-Yah!\n"

str.rdiff( str2 )
# => "This is the test.\nNot really sure what else to put here...\n"

Parameters:

Returns:



47
48
49
50
51
52
53
54
55
# File 'lib/arachni/ruby/string.rb', line 47

def rdiff( other )
    return self if self == other

    # get the words of the first text in an array
    s_words = words

    # get what hasn't changed (the rdiff, so to speak) as a string
    (s_words - (s_words - other.words)).join
end

#recodeObject



106
107
108
# File 'lib/arachni/ruby/string.rb', line 106

def recode
    dup.recode!
end

#recode!Object



102
103
104
# File 'lib/arachni/ruby/string.rb', line 102

def recode!
    encode!( 'utf-16be', invalid: :replace, undef: :replace ).encode("utf-8")
end

#repackObject



98
99
100
# File 'lib/arachni/ruby/string.rb', line 98

def repack
    unpack( 'C*' ).pack( 'U*' )
end

#substring?(string) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
# File 'lib/arachni/ruby/string.rb', line 89

def substring?( string )
    begin
        cmatch = match( Regexp.new( Regexp.escape( string ) ) )
        cmatch && !cmatch.to_s.empty?
    rescue
        nil
    end
end

#words(strict = false) ⇒ Array<String>

Returns the words in self.

Parameters:

  • strict (Bool) (defaults to: false)

    include only words, no boundary characters (like spaces, etc.)

Returns:



83
84
85
86
87
# File 'lib/arachni/ruby/string.rb', line 83

def words( strict = false )
    splits = split( /\b/ )
    splits.reject! { |w| !(w =~ /\w/) } if strict
    splits
end