-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrUtil.rb
More file actions
151 lines (138 loc) · 3.7 KB
/
StrUtil.rb
File metadata and controls
151 lines (138 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Copyright (C) 2021, 2022, 2023 hidenorly
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
class StrUtil
def self.ensureUtf8(str, replaceChr="_")
str = str.to_s
str.encode!("UTF-8", :invalid=>:replace, :undef=>:replace, :replace=>replaceChr) if !str.valid_encoding?
return str
end
=begin
theStr= "(abc(def(g(h)())))"
puts "0:"+StrUtil.getBlacket(theStr, "(", ")", 0)
puts "1:"+StrUtil.getBlacket(theStr, "(", ")", 1)
puts "4:"+StrUtil.getBlacket(theStr, "(", ")", 4)
puts "5:"+StrUtil.getBlacket(theStr, "(", ")", 5)
puts "9:"+StrUtil.getBlacket(theStr, "(", ")", 9)
exit(0)
=end
def self.getBlacket(theStr, blacketBegin="(", blacketEnd=")", startPos=0)
theLength = theStr.length
blacketLength = [blacketBegin.length.to_i, blacketEnd.length.to_i].max
result = theStr.slice(startPos, theLength-startPos)
pos = theStr.index(blacketBegin, startPos)
nCnt = pos ? 1 : 0
target_pos1 = pos ? pos : startPos
target_pos2 = nil
while pos!=nil && pos<theLength && nCnt>0
pos = pos + 1
theChr = theStr.slice(pos, blacketLength)
if theChr.start_with?(blacketBegin) then
nCnt = nCnt + 1
else
pos2 = theChr.index(blacketEnd)
if pos2 then
nCnt = nCnt - 1
target_pos2 = pos + pos2
end
end
end
if target_pos1 && target_pos2 && target_pos2>target_pos1 then
result = theStr.slice( target_pos1 + blacketBegin.length, target_pos2 - target_pos1 - blacketEnd.length )
end
return result
end
DEF_SEPARATOR_CONDITIONS=[
" ",
"{",
"}",
",",
"[",
"]",
"\"",
" ",
":"
]
def getJsonKey(body, curPos = 0 , lastFound)
identifier = ":"
result = body
pos = body.index(identifier, curPos)
searchLimit = lastFound ? pos-lastFound : pos
lastFound = lastFound ? lastFound : 0
foundPos = nil
if pos then
for i in 1..searchLimit do
theTarget = body.slice(pos-i)
DEF_SEPARATOR_CONDITIONS.each do |aCondition|
if theTarget == aCondition then
foundPos = pos - i
break
end
end
=begin
if body.slice(pos-i).match(/( |\"|\'|\[|\]|,'|{|})/) then
foundPos = pos-i
break
end
=end
break if foundPos
end
end
if foundPos then
result = body.slice(lastFound, foundPos-lastFound) + "\"" + body.slice(foundPos+1,pos-foundPos-1) + "\""
else
result = body.slice(lastFound, curPos-lastFound)
end
return result
end
def ensureJson(body)
return "{ #{body} }".gsub(/(\w+)\s*:/, '"\1":').gsub(/,(?= *\])/, '').gsub(/,(?= *\})/, '')
result = ""
i = 0
lastFound = nil
theLength = body.length
pos = body.index(":", i)
while i<theLength && pos!=nil
pos = body.index(":", i)
if pos then
i = pos + 1
result = result + getJsonKey(body, pos, lastFound) + ":"
lastFound = i
else
result = result + body.slice(i, theLength)
break
end
end
result = body if result.empty?
return result
end
def self.getRegexpArrayFromArray(regArray)
result = []
regArray.each do | aRegExp |
aRegExp = aRegExp.to_s.strip
result << Regexp.new( aRegExp ) if !aRegExp.empty?
end
return result
end
def self.matches?(target, regArray)
result = false
target = target.to_s
regArray.each do | aRegExp |
if target.match?(aRegExp) then
result = true
break
end
end
return result
end
end