Module: TRE

Defined in:
lib/tre-ruby.rb

Defined Under Namespace

Classes: AParams

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fuzziness(max_err) ⇒ TRE::AParams

Returns a TRE::AParams object with the given fuzziness (max_err)

Parameters:

  • max_err (Fixnum)

Returns:



9
10
11
12
13
14
15
16
17
# File 'lib/tre-ruby.rb', line 9

def TRE.fuzziness max_err
	@@fuzzies ||= {}
	return @@fuzzies[max_err] if @@fuzzies.has_key? max_err

	param = TRE::AParams.new
	param.max_err = max_err
	param.freeze
	@@fuzzies[max_err] = param
end

Instance Method Details

#afind(pattern, offset = 0, params = TRE.fuzziness(0)) ⇒ String

Locates the pattern in the string and returns the first matching substring.

Parameters:

  • pattern (String/Regexp)
  • offset (Fixnum) (defaults to: 0)
  • params (TRE::AParams) (defaults to: TRE.fuzziness(0))

Returns:

  • (String)


38
39
40
41
42
# File 'lib/tre-ruby.rb', line 38

def afind pattern, offset = 0, params = TRE.fuzziness(0)
	range = aindex pattern, offset, params

	range && self[range]
end

#agsub(pattern, replacement, params = TRE.fuzziness(0), &block) ⇒ String

Returns a copy of the String with every match substituted

Parameters:

  • pattern (String/Regexp)
  • replacement (String)
  • params (TRE::AParams) (defaults to: TRE.fuzziness(0))

Returns:

  • (String)


95
96
97
# File 'lib/tre-ruby.rb', line 95

def agsub pattern, replacement, params = TRE.fuzziness(0), &block
	asub_impl pattern, replacement, params, true, &block
end

#agsub!(pattern, replacement, params = TRE.fuzziness(0), &block) ⇒ String

Substitutes every match

Parameters:

  • pattern (String/Regexp)
  • replacement (String)
  • params (TRE::AParams) (defaults to: TRE.fuzziness(0))

Returns:

  • (String)


104
105
106
# File 'lib/tre-ruby.rb', line 104

def agsub! pattern, replacement, params = TRE.fuzziness(0), &block
	self.replace agsub(pattern, replacement, params, &block)
end

#aindex(pattern, offset = 0, params = TRE.fuzziness(0)) ⇒ Range

Locates the pattern in the string and returns the Range object for the first match

Parameters:

  • pattern (String/Regexp)
  • offset (Fixnum) (defaults to: 0)
  • params (TRE::AParams) (defaults to: TRE.fuzziness(0))

Returns:

  • (Range)

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
# File 'lib/tre-ruby.rb', line 24

def aindex pattern, offset = 0, params = TRE.fuzziness(0)
	raise ArgumentError.new("Invalid parameter") unless params.is_a? TRE::AParams
	raise ArgumentError.new("Invalid offset parameter") unless offset.is_a? Fixnum

	input = parse_pattern pattern
	__aindex(input[:source], self, offset, params, 
			input[:ignore_case], input[:multi_line])
end

#ascan(pattern, params = TRE.fuzziness(0), &block) ⇒ Array

Scans for the pattern in the String and returns Array of matching substrings or Array of Array of Strings when the given pattern contains Regexp captures.

Parameters:

  • pattern (String/Regexp)
  • params (TRE::AParams) (defaults to: TRE.fuzziness(0))

Returns:

  • (Array)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tre-ruby.rb', line 49

def ascan pattern, params = TRE.fuzziness(0), &block
	result = ascan_r(pattern, params).map { |e|
		case e
		when Array
			e.map { |ee| self[ee] }.take_while { |ee| ee }
		when Range
			self[e]
		else
			raise RuntimeError.new
		end
	}
	return result unless block_given?
	yield_scan_result result, &block
end

#ascan_r(pattern, params = TRE.fuzziness(0), &block) ⇒ Array

Same as TRE#ascan, but returns Array of Range objects instead of String objects.

Parameters:

  • pattern (String/Regexp)
  • params (TRE::AParams) (defaults to: TRE.fuzziness(0))

Returns:

  • (Array)


68
69
70
# File 'lib/tre-ruby.rb', line 68

def ascan_r pattern, params = TRE.fuzziness(0), &block
	ascan_r_impl pattern, params, true, &block
end

#asub(pattern, replacement, params = TRE.fuzziness(0), &block) ⇒ String

Returns a copy of the String with the first match substituted

Parameters:

  • pattern (String/Regexp)
  • replacement (String)
  • params (TRE::AParams) (defaults to: TRE.fuzziness(0))

Returns:

  • (String)


77
78
79
# File 'lib/tre-ruby.rb', line 77

def asub pattern, replacement, params = TRE.fuzziness(0), &block
	asub_impl pattern, replacement, params, false, &block
end

#asub!(pattern, replacement, params = TRE.fuzziness(0), &block) ⇒ String

Substitutes the first match

Parameters:

  • pattern (String/Regexp)
  • replacement (String)
  • params (TRE::AParams) (defaults to: TRE.fuzziness(0))

Returns:

  • (String)


86
87
88
# File 'lib/tre-ruby.rb', line 86

def asub! pattern, replacement, params = TRE.fuzziness(0), &block
	self.replace asub(pattern, replacement, params, &block)
end