Skip to content

Commit 7e81d5c

Browse files
Merge pull request #255 from sisimai/reduce-regexp-7e75
Reduce Regular Expressions 7e75
2 parents 4b01c91 + ef3b2db commit 7e81d5c

24 files changed

Lines changed: 241 additions & 292 deletions

lib/sisimai/datetime.rb

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -171,40 +171,6 @@ class << self
171171
#'YEKT' => '+0500', # Yekaterinburg Time UTC+05:00
172172
}.freeze
173173

174-
# Convert to second
175-
# @param [String] argv1 Digit and a unit of time
176-
# @return [Integer] n: seconds
177-
# 0: 0 or invalid unit of time
178-
# @example Get the value of seconds
179-
# to_second('1d') #=> 86400
180-
# to_second('2h') #=> 7200
181-
def to_second(argv1)
182-
return 0 unless argv1.is_a?(::String)
183-
184-
getseconds = 0
185-
unitoftime = TimeUnit.keys.join
186-
mathconsts = MathematicalConstant.keys.join
187-
188-
if cr = argv1.match(/\A(\d+|\d+[.]\d+)([#{unitoftime}])?\z/)
189-
# 1d, 1.5w
190-
n = cr[1].to_f
191-
u = cr[2] || 'd'
192-
getseconds = n * TimeUnit[u].to_f
193-
194-
elsif cr = argv1.match(/\A(\d+|\d+[.]\d+)?([#{mathconsts}])([#{unitoftime}])?\z/)
195-
# 1pd, 1.5pw
196-
n = cr[1].to_f || 1
197-
n = 1 if n.to_i == 0
198-
m = MathematicalConstant[cr[2]].to_f
199-
u = cr[3] || 'd'
200-
getseconds = n * m * TimeUnit[u].to_f
201-
else
202-
getseconds = 0
203-
end
204-
205-
return getseconds
206-
end
207-
208174
# Month name list
209175
# @param [Boolean] argv1 Require full name or not
210176
# @return [Array, String] Month name list or month name
@@ -216,17 +182,6 @@ def monthname(argv1 = false)
216182
return MonthName[value]
217183
end
218184

219-
# List of day of week
220-
# @param [Boolean] argv1 Require full name
221-
# @return [Array, String] List of day of week or day of week
222-
# @example Get the names of each day of week
223-
# dayofweek() #=> [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]
224-
# dayofweek(true) #=> [ 'Sunday', 'Monday', 'Tuesday', ... ]
225-
def dayofweek(argv1 = false)
226-
value = argv1 ? :full : :abbr
227-
return DayOfWeek[value]
228-
end
229-
230185
# Parse date string; strptime() wrapper
231186
# @param [String] argv1 Date string
232187
# @return [String] Converted date string
@@ -258,7 +213,7 @@ def parse(argv1)
258213
# Parse each piece of time
259214
if p =~ /\A[A-Z][a-z]{2,}[,]?\z/
260215
# Day of week or Day of week; Thu, Apr, ...
261-
p.gsub!(/,\z/, '') if p.end_with?(',') # "Thu," => "Thu"
216+
p[-1, 1] = '' if p.end_with?(',') # "Thu," => "Thu"
262217
p = p[0,3] if p.size > 3
263218

264219
if DayOfWeek[:abbr].include?(p)
@@ -301,7 +256,7 @@ def parse(argv1)
301256
# Time: 1:4 => 01:04:00
302257
v[:T] = sprintf('%02d:%02d:00', cr[1].to_i, cr[2].to_i)
303258

304-
elsif p =~ /\A[APap][Mm]\z/
259+
elsif p.downcase == 'am' || p.downcase == 'pm'
305260
# AM or PM
306261
afternoon1 = 1
307262
else

lib/sisimai/lhost/amazonses.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,19 @@ def inquire(mhead, mbody)
121121
v['action'] = e['action']
122122
v['status'] = e['status']
123123

124-
if cv = e['diagnosticCode'].match(/\A(.+?);[ ](.+)\z/)
124+
if p0 = e['diagnosticCode'].index(';') || -1; p0 > 3
125125
# Diagnostic-Code: SMTP; 550 5.1.1 <userunknown@example.jp>... User Unknown
126-
v['spec'] = cv[1].upcase
127-
v['diagnosis'] = cv[2]
126+
v['spec'] = e['diagnosticCode'][0, p0].upcase
127+
v['diagnosis'] = e['diagnosticCode'][p0 + 2, e['diagnosticCode'].size]
128128
else
129129
v['diagnosis'] = e['diagnosticCode']
130130
end
131131

132132
# 'reportingMTA' => 'dsn; a27-23.smtp-out.us-west-2.amazonses.com',
133-
if cv = o['reportingMTA'].match(/\Adsn;[ ](.+)\z/) then v['lhost'] = cv[1] end
133+
p0 = o['reportingMTA'].index('dsn; ')
134+
v['lhost'] = Sisimai::String.sweep(o['reportingMTA'][p0 + 5, o['reportingMTA'].size]) if p0
134135

135-
if bouncetype[o['bounceType']] &&
136-
bouncetype[o['bounceType']][o['bounceSubType']]
136+
if bouncetype[o['bounceType']] && bouncetype[o['bounceType']][o['bounceSubType']]
137137
# 'bounce' => {
138138
# 'bounceType' => 'Permanent',
139139
# 'bounceSubType' => 'General'
@@ -285,8 +285,8 @@ def inquire(mhead, mbody)
285285
else
286286
# Continued line of the value of Diagnostic-Code field
287287
next unless readslices[-2].start_with?('Diagnostic-Code:')
288-
next unless cv = e.match(/\A[ ]+(.+)\z/)
289-
v['diagnosis'] << ' ' << cv[1]
288+
next unless e.start_with?(' ')
289+
v['diagnosis'] << ' ' << e
290290
readslices[-1] = 'Diagnostic-Code: ' << e
291291
end
292292
end
@@ -311,12 +311,11 @@ def inquire(mhead, mbody)
311311
e['diagnosis'] = Sisimai::String.sweep(e['diagnosis'].to_s.tr("\n", ' '))
312312
if e['status'].to_s.end_with?('.0.0', '.1.0')
313313
# Get other D.S.N. value from the error message
314+
# 5.1.0 - Unknown address error 550-'5.7.1 ...
314315
errormessage = e['diagnosis']
315-
316-
if cv = e['diagnosis'].match(/["'](\d[.]\d[.]\d.+)['"]/)
317-
# 5.1.0 - Unknown address error 550-'5.7.1 ...
318-
errormessage = cv[1]
319-
end
316+
p1 = e['diagnosis'].index("-'"); p1 = e['diagnosis'].index('-"') unless p1
317+
p2 = e['diagnosis'].rindex("' "); p2 = e['diagnosis'].rindex('" ') unless p2
318+
errormessage = e['diagnosis'][p1 + 2, p2 - p1 - 2] if p1 && p2
320319
e['status'] = Sisimai::SMTP::Status.find(errormessage) || e['status']
321320
end
322321
e['replycode'] ||= Sisimai::SMTP::Reply.find(e['diagnosis'], e['status'])

lib/sisimai/lhost/aol.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ def inquire(mhead, mbody)
9191
else
9292
# Continued line of the value of Diagnostic-Code field
9393
next unless readslices[-2].start_with?('Diagnostic-Code:')
94-
next unless cv = e.match(/\A[ ]+(.+)\z/)
95-
v['diagnosis'] << ' ' << cv[1]
94+
next unless e.start_with?(' ')
95+
v['diagnosis'] << ' ' << Sisimai::String.sweep(e)
9696
readslices[-1] = 'Diagnostic-Code: ' << e
9797
end
9898
end

lib/sisimai/lhost/biglobe.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def inquire(mhead, mbody)
6060
#
6161
v = dscontents[-1]
6262

63-
if cv = e.match(/\A([^ ]+[@][^ ]+)\z/)
63+
if e.include?('@') && e.include?(' ') == false
6464
# ----- The following addresses had delivery problems -----
6565
# ********@***.biglobe.ne.jp
6666
if v['recipient']
@@ -69,12 +69,11 @@ def inquire(mhead, mbody)
6969
v = dscontents[-1]
7070
end
7171

72-
r = Sisimai::Address.s3s4(cv[1])
73-
next unless Sisimai::Address.is_emailaddress(r)
74-
v['recipient'] = r
72+
next unless Sisimai::Address.is_emailaddress(e)
73+
v['recipient'] = e
7574
recipients += 1
7675
else
77-
next if e =~ /\A[^\w]/
76+
next if e.include?('--')
7877
v['diagnosis'] ||= ''
7978
v['diagnosis'] << e + ' '
8079
end

lib/sisimai/lhost/courier.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ def inquire(mhead, mbody)
115115
else
116116
# Continued line of the value of Diagnostic-Code field
117117
next unless readslices[-2].start_with?('Diagnostic-Code:')
118-
next unless cv = e.match(/\A[ ]+(.+)\z/)
119-
v['diagnosis'] << ' ' << cv[1]
118+
next unless e.start_with?(' ')
119+
v['diagnosis'] << ' ' << Sisimai::String.sweep(e)
120120
readslices[-1] = 'Diagnostic-Code: ' << e
121121
end
122122
end

lib/sisimai/lhost/einsundeins.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,14 @@ def inquire(mhead, mbody)
8383
e['diagnosis'] ||= ''
8484
e['diagnosis'] = e['alterrors'] if e['diagnosis'].empty?
8585

86-
if cv = e['diagnosis'].match(/host:[ ]+(.+?)[ ]+.+[ ]+reason:.+/)
86+
if Sisimai::String.aligned(e['diagnosis'], ['host: ', ' reason:'])
8787
# SMTP error from remote server for TEXT command,
8888
# host: smtp-in.orange.fr (193.252.22.65)
8989
# reason: 550 5.2.0 Mail rejete. Mail rejected. ofr_506 [506]
90-
e['rhost'] = cv[1]
90+
p1 = e['diagnosis'].index('host: ')
91+
p2 = e['diagnosis'].index(' reason:')
92+
93+
e['rhost'] = Sisimai::String.sweep(e['diagnosis'][p1 + 6, p2 - p1 - 6])
9194
e['command'] = 'DATA' if e['diagnosis'].include?('for TEXT command')
9295
e['spec'] = 'SMTP' if e['diagnosis'].include?('SMTP error')
9396
e['status'] = Sisimai::SMTP::Status.find(e['diagnosis'])

lib/sisimai/lhost/exchange2003.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,17 @@ def inquire(mhead, mbody)
122122
# MSEXCH:IMS:KIJITORA CAT:EXAMPLE:EXCHANGE 0 (000C05A6) Unknown Recipient
123123
v = dscontents[-1]
124124

125-
if cv = e.match(/\A[ ]*([^ ]+[@][^ ]+) on[ ]*.*\z/) ||
126-
e.match(/\A[ ]*.+(?:SMTP|smtp)=([^ ]+[@][^ ]+) on[ ]*.*\z/)
125+
if Sisimai::String.aligned(e, ['@', ' on '])
127126
# kijitora@example.co.jp on Thu, 29 Apr 2007 16:51:51 -0500
128127
# kijitora@example.com on 4/29/99 9:19:59 AM
129128
if v['recipient']
130129
# There are multiple recipient addresses in the message body.
131130
dscontents << Sisimai::Lhost.DELIVERYSTATUS
132131
v = dscontents[-1]
133132
end
134-
v['recipient'] = cv[1]
133+
p1 = e.downcase.index('smtp='); p1 = p1.nil? ? 0 : p1 + 5
134+
p2 = e.index(' on ') + 1
135+
v['recipient'] = Sisimai::Address.s3s4(e[p1, p2])
135136
v['msexch'] = false
136137
recipients += 1
137138

lib/sisimai/lhost/exchange2007.rb

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,25 @@ def inquire(mhead, mbody)
104104
v['diagnosis'] = ''
105105
recipients += 1
106106

107-
elsif cv = e.match(/([45]\d{2})[ ]([45][.]\d[.]\d+)?[ ]?.+\z/)
108-
# #550 5.1.1 RESOLVER.ADR.RecipNotFound; not found ##
109-
# #550 5.2.3 RESOLVER.RST.RecipSizeLimit; message too large for this recipient ##
110-
# Remote Server returned '550 5.1.1 RESOLVER.ADR.RecipNotFound; not found'
111-
# 3/09/2016 8:05:56 PM - Remote Server at mydomain.com (10.1.1.3) returned '550 4.4.7 QUEUE.Expired; message expired'
112-
v['replycode'] = cv[1]
113-
v['status'] = cv[2]
114-
v['diagnosis'] = e
115107
else
116108
# Continued line of error messages
117-
next if v['diagnosis'].to_s.empty?
118-
next unless v['diagnosis'].end_with?('=')
119-
v['diagnosis'] = v['diagnosis'].chomp('=')
120-
v['diagnosis'] << e
109+
cr = Sisimai::SMTP::Reply.find(e)
110+
cs = Sisimai::SMTP::Status.find(e)
111+
if cr || cs
112+
# #550 5.1.1 RESOLVER.ADR.RecipNotFound; not found ##
113+
# #550 5.2.3 RESOLVER.RST.RecipSizeLimit; message too large for this recipient ##
114+
# Remote Server returned '550 5.1.1 RESOLVER.ADR.RecipNotFound; not found'
115+
# 3/09/2016 8:05:56 PM - Remote Server at mydomain.com (10.1.1.3) returned '550 4.4.7 QUEUE.Expired; message expired'
116+
v['replycode'] = cr || ''
117+
v['status'] = cs || ''
118+
v['diagnosis'] = e
119+
else
120+
# Continued line of error messages
121+
next if v['diagnosis'].to_s.empty?
122+
next unless v['diagnosis'].end_with?('=')
123+
v['diagnosis'] = v['diagnosis'].chomp('=')
124+
v['diagnosis'] << e
125+
end
121126
end
122127
else
123128
# Diagnostic information for administrators:

0 commit comments

Comments
 (0)