Module: Strutils

Defined in:
lib/my_utils/strutils.rb

Class Method Summary collapse

Class Method Details

.reg_iter(text, s, e) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/my_utils/strutils.rb', line 3

def self.reg_iter text, s, e
		rstart = 0; rend = 0
		rst_arry = []

		while true
  rstart = text.index(s, rend)
  break if rstart == nil

  rend = text.index(e, rstart)
  len = text.match(e, rstart)[0].length
  rend = rend + len - 1
  rst_arry.push(text[rstart..rend])
  rstart = rend
		end

		rst_arry
end

.strcmp(a, b) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/my_utils/strutils.rb', line 21

def self.strcmp a, b
		return 0 if a == nil || b == nil 

		a = a.strip; b = b.strip

		return 0 if a == "" || b == ""

		len = a.length > b.length ? a.length : b.length
		dp = Array.new(a.length) {Array.new(b.length, 0)}

		for i in 0..a.length-1
  for j in 0..b.length-1
			(dp[i][j] = (a[0] == b[0]? 1 : 0); next) if i == 0 and j == 0
			(dp[i][j] = (dp[i][j-1] == 1 || a[0] == b[j]? 1 : 0); next) if i == 0
			(dp[i][j] = (dp[i-1][j] == 1 || a[i] == b[0]? 1 : 0); next) if j == 0
			
			dp[i][j] = (dp[i-1][j-1] + (a[i] == b[j]? 1 : 0))
			dp[i][j] = dp[i-1][j] if dp[i][j] < dp[i-1][j]
			dp[i][j] = dp[i][j-1] if dp[i][j] < dp[i][j-1]
  end 
		end 
		
		return dp[a.length-1][b.length-1] / (len * 1.0)
end