Skip to content

Commit 17a32e2

Browse files
committed
Use explicit string type in sniffer module
1 parent bcc5b1e commit 17a32e2

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

src/CSVSniffer.cls

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Public Enum FieldDataType
2323
Known = 1
2424
UnKnown = 0
2525
End Enum
26-
Private Function LikeCompare(ByRef value As Variant, _
26+
Private Function LikeCompare(ByRef value As String, _
2727
ParamArray Pattern() As Variant) As Boolean
2828
Dim iCounter As Long
2929
Dim ParamLB As Long
@@ -34,7 +34,7 @@ Private Function LikeCompare(ByRef value As Variant, _
3434
ParamUB = UBound(Pattern)
3535
iCounter = ParamLB
3636
Do
37-
tmpBool = value Like Pattern(iCounter)
37+
tmpBool = value Like CStr(Pattern(iCounter))
3838
iCounter = iCounter + 1
3939
Loop While iCounter <= ParamUB And Not tmpBool
4040
LikeCompare = tmpBool
@@ -43,28 +43,30 @@ End Function
4343
''' Attempts to detect the data type of a CSV field.
4444
''' </summary>
4545
''' <param name="value">CSV field content.</param>
46-
Public Function DetectDataType(ByRef value As Variant) As FieldDataType
46+
Public Function DetectDataType(ByRef value As String) As FieldDataType
4747
Dim tmpDataType As FieldDataType
48+
Dim tmpValue As String
4849

50+
tmpValue = Trim(value)
4951
tmpDataType = UnKnown
50-
If IsNumericData(value) Then
52+
If IsNumericData(tmpValue) Then
5153
tmpDataType = FieldDataType.Known
5254
Else
53-
If IsDateOrSpecialData(value) Then
55+
If IsDateOrSpecialData(tmpValue) Then
5456
tmpDataType = FieldDataType.Known
5557
Else
56-
If IsStructuredOrURI(value) Then
58+
If IsStructuredOrURI(tmpValue) Then
5759
tmpDataType = FieldDataType.Known
5860
Else
59-
If IsFileSystemPath(value) Then
61+
If IsFileSystemPath(tmpValue) Then
6062
tmpDataType = FieldDataType.Known
6163
End If
6264
End If
6365
End If
6466
End If
6567
DetectDataType = tmpDataType
6668
End Function
67-
Private Function dmyyyyhhmmDateTime(value As Variant) As Boolean
69+
Private Function dmyyyyhhmmDateTime(value As String) As Boolean
6870
'Match DD/MM/YYYY[YYYY/DD/MM] and MM/DD/YYYY[YYYY/MM/DD] HH:MM
6971
dmyyyyhhmmDateTime = LikeCompare(value, _
7072
"##[-/.]##[-/.]####[T]##:##", _
@@ -76,7 +78,7 @@ Private Function dmyyyyhhmmDateTime(value As Variant) As Boolean
7678
"##[-/.]#[-/.]####[T]##:##", _
7779
"####[-/.]##[-/.]#[T]##:##")
7880
End Function
79-
Private Function dmyyyyhhmmssDateTime(value As Variant) As Boolean
81+
Private Function dmyyyyhhmmssDateTime(value As String) As Boolean
8082
'Match DD/MM/YYYY[YYYY/DD/MM] and MM/DD/YYYY[YYYY/MM/DD] HH:MM:SS
8183
dmyyyyhhmmssDateTime = LikeCompare(value, _
8284
"##[-/.]##[-/.]####[T]##:##:##", _
@@ -88,7 +90,7 @@ Private Function dmyyyyhhmmssDateTime(value As Variant) As Boolean
8890
"##[-/.]#[-/.]####[T]##:##:##", _
8991
"####[-/.]##[-/.]#[T]##:##:##")
9092
End Function
91-
Private Function dmyyyyhhmmssTStampedDateTime(value As Variant) As Boolean
93+
Private Function dmyyyyhhmmssTStampedDateTime(value As String) As Boolean
9294
'Match DD/MM/YYYY[YYYY/DD/MM] and MM/DD/YYYY[YYYY/MM/DD] HH:MM:SS +/- HH:MM
9395
dmyyyyhhmmssTStampedDateTime = LikeCompare(value, _
9496
"##[-/.]##[-/.]####[T]##:##:##[+-]##:##", _
@@ -100,7 +102,7 @@ Private Function dmyyyyhhmmssTStampedDateTime(value As Variant) As Boolean
100102
"##[-/.]#[-/.]####[T]##:##:##[+-]##:##", _
101103
"####[-/.]##[-/.]#[T]##:##:##[+-]##:##")
102104
End Function
103-
Private Function dmyyyyhhmmTStampedDateTime(value As Variant) As Boolean
105+
Private Function dmyyyyhhmmTStampedDateTime(value As String) As Boolean
104106
'Match DD/MM/YYYY[YYYY/DD/MM] and MM/DD/YYYY[YYYY/MM/DD] HH:MM +/- HH:MM
105107
dmyyyyhhmmTStampedDateTime = LikeCompare(value, _
106108
"##[-/.]##[-/.]####[T]##:##[+-]##:##", _
@@ -112,13 +114,13 @@ Private Function dmyyyyhhmmTStampedDateTime(value As Variant) As Boolean
112114
"##[-/.]#[-/.]####[T]##:##[+-]##:##", _
113115
"####[-/.]##[-/.]#[T]##:##[+-]##:##")
114116
End Function
115-
Private Function hhmmssTStampedDateTime(value As Variant) As Boolean
117+
Private Function hhmmssTStampedDateTime(value As String) As Boolean
116118
'Match HH:MM:SS and HH:MM +/- 00:00
117119
hhmmssTStampedDateTime = LikeCompare(value, _
118120
"##:##:##[+-]##:##", _
119121
"##:##[+-]##:##")
120122
End Function
121-
Private Function IsAlphaNumeric(value As Variant) As Boolean
123+
Private Function IsAlphaNumeric(value As String) As Boolean
122124
'Match ABCZ10, nullString and ABCZ_10
123125
Dim StrLen As Long
124126
Dim iCounter As Long
@@ -137,7 +139,7 @@ Private Function IsAlphaNumeric(value As Variant) As Boolean
137139
Loop While iCounter <= StrLen And tmpBool
138140
IsAlphaNumeric = tmpBool
139141
End Function
140-
Private Function IsCurrency(value As Variant) As Boolean
142+
Private Function IsCurrency(value As String) As Boolean
141143
If LikeCompare(value, "[$€£¥]#*[.,]##", "[$€£¥][ ]#*[.,]##") Then
142144
IsCurrency = IsNumeric(Format(MidB(value, 3), "#,#0.00"))
143145
Else
@@ -146,7 +148,7 @@ Private Function IsCurrency(value As Variant) As Boolean
146148
End If
147149
End If
148150
End Function
149-
Private Function IsDateOrSpecialData(value As Variant) As Boolean
151+
Private Function IsDateOrSpecialData(value As String) As Boolean
150152
Dim tmpBool As Boolean
151153

152154
tmpBool = IsSpecialData(value)
@@ -161,7 +163,7 @@ Private Function IsDateOrSpecialData(value As Variant) As Boolean
161163
End If
162164
IsDateOrSpecialData = tmpBool
163165
End Function
164-
Private Function IsDateTime(value As Variant) As Boolean
166+
Private Function IsDateTime(value As String) As Boolean
165167
Dim tmpBool As Boolean
166168
If InStrB(1, value, ":") Then
167169
tmpBool = hhmmssTStampedDateTime(value)
@@ -184,7 +186,7 @@ Private Function IsDateTime(value As Variant) As Boolean
184186
End If
185187
IsDateTime = tmpBool
186188
End Function
187-
Private Function IsDotDate(value As Variant) As Boolean
189+
Private Function IsDotDate(value As String) As Boolean
188190
IsDotDate = LikeCompare(value, _
189191
"####[.]##[.]##", _
190192
"##[.]##[.]####", _
@@ -195,7 +197,7 @@ Private Function IsDotDate(value As Variant) As Boolean
195197
"####[.]#[.]##", _
196198
"##[.]#[.]####")
197199
End Function
198-
Private Function IsEmail(value As Variant) As Boolean
200+
Private Function IsEmail(value As String) As Boolean
199201
If InStrB(1, value, "@") Then
200202
If value Like "*[@]*[.]?*?" Then
201203
Dim StrLen As Long
@@ -217,7 +219,7 @@ Private Function IsEmail(value As Variant) As Boolean
217219
End If
218220
IsEmail = tmpBool
219221
End Function
220-
Private Function IsFileSystemPath(value As Variant) As Boolean
222+
Private Function IsFileSystemPath(value As String) As Boolean
221223
Dim tmpBool As Boolean
222224

223225
If IsWindowsAbsolutePath(value) Then
@@ -229,23 +231,23 @@ Private Function IsFileSystemPath(value As Variant) As Boolean
229231
End If
230232
IsFileSystemPath = tmpBool
231233
End Function
232-
Private Function IsIPv4(value As Variant) As Boolean
234+
Private Function IsIPv4(value As String) As Boolean
233235
If value Like "*.*.*.*" Then
234236
IsIPv4 = IsValidIPv4(value)
235237
End If
236238
End Function
237-
Private Function IsISOdate(value As Variant) As Boolean
239+
Private Function IsISOdate(value As String) As Boolean
238240
'Match YYYY/MM/DDTHH:MM:SSZ and YYYY/MM/DDTHH:MM:SS[+/-]HH:MM
239241
IsISOdate = LikeCompare(value, _
240242
"####[-/.]##[-/.]##T##:##:##Z", _
241243
"####[-/.]##[-/.]##T##:##:##[+-]##:##")
242244
End Function
243-
Private Function IsJSfullTextDateTime(value As Variant) As Boolean
245+
Private Function IsJSfullTextDateTime(value As String) As Boolean
244246
'Match JavaScript full text date and time
245247
IsJSfullTextDateTime = LikeCompare(value, _
246248
"??? ??? ## #### ##:##:## *-* (*)")
247249
End Function
248-
Private Function IsLongOrStampedDateTime(value As Variant) As Boolean
250+
Private Function IsLongOrStampedDateTime(value As String) As Boolean
249251
Dim tmpBool As Boolean
250252
tmpBool = IsISOdate(value)
251253
If Not tmpBool Then
@@ -259,7 +261,7 @@ Private Function IsLongOrStampedDateTime(value As Variant) As Boolean
259261
End If
260262
IsLongOrStampedDateTime = tmpBool
261263
End Function
262-
Private Function IsNumericData(value As Variant) As Boolean
264+
Private Function IsNumericData(value As String) As Boolean
263265
Dim tmpBool As Boolean
264266

265267
If IsNumeric(value) Then
@@ -275,7 +277,7 @@ Private Function IsNumericData(value As Variant) As Boolean
275277
End If
276278
IsNumericData = tmpBool
277279
End Function
278-
Private Function IsOtherDateTime(value As Variant) As Boolean
280+
Private Function IsOtherDateTime(value As String) As Boolean
279281
Dim tmpBool As Boolean
280282

281283
'Match YYYY/MM/DD[ ][T]HH:MM:SS.ss and MM/DD/YYYY[ ][T]HH:MM:SS.ss
@@ -304,12 +306,12 @@ Private Function IsOtherDateTime(value As Variant) As Boolean
304306
End If
305307
IsOtherDateTime = tmpBool
306308
End Function
307-
Private Function IsPercentage(value As Variant) As Boolean
309+
Private Function IsPercentage(value As String) As Boolean
308310
If LikeCompare(value, "*#[%]") Then
309311
IsPercentage = IsNumeric(Format(MidB(value, 1, LenB(value) - 2), "#,#0.00"))
310312
End If
311313
End Function
312-
Private Function IsSpanishDate(value As Variant) As Boolean
314+
Private Function IsSpanishDate(value As String) As Boolean
313315
'Match [Lun Dic 31 01:41:00 2001 | Lun Dic 1 01:41:00 2001]
314316
'and [Lun Dic 31 01:41:00 21 | Lun Dic 1 01:41:00 21]
315317
IsSpanishDate = LikeCompare(value, _
@@ -318,7 +320,7 @@ Private Function IsSpanishDate(value As Variant) As Boolean
318320
"[DLMJVS][ouai][mnreb][ ][EFMAJSOND][neabugcoi][ebrynloptvc][ ]##[ ]##:##:##[ ]##", _
319321
"[DLMJVS][ouai][mnreb][ ][EFMAJSOND][neabugcoi][ebrynloptvc][ ]#[ ]##:##:##[ ]##")
320322
End Function
321-
Private Function IsSpecialData(value As Variant) As Boolean
323+
Private Function IsSpecialData(value As String) As Boolean
322324
Dim tmpBool As Boolean
323325

324326
If LenB(value) = 0 Then
@@ -334,7 +336,7 @@ Private Function IsSpecialData(value As Variant) As Boolean
334336
End If
335337
IsSpecialData = tmpBool
336338
End Function
337-
Private Function IsStampedDateTime(value As Variant) As Boolean
339+
Private Function IsStampedDateTime(value As String) As Boolean
338340
Dim tmpBool As Boolean
339341
tmpBool = dmyyyyhhmmssTStampedDateTime(value)
340342
If Not tmpBool Then
@@ -345,7 +347,7 @@ Private Function IsStampedDateTime(value As Variant) As Boolean
345347
End If
346348
IsStampedDateTime = tmpBool
347349
End Function
348-
Private Function IsStructuredData(value As Variant) As Boolean
350+
Private Function IsStructuredData(value As String) As Boolean
349351
Dim tmpBool As Boolean
350352
If InStrB(1, value, "[") Then
351353
If LikeCompare(value, "[[]*]") Then
@@ -373,7 +375,7 @@ Private Function IsStructuredData(value As Variant) As Boolean
373375
End If
374376
IsStructuredData = tmpBool
375377
End Function
376-
Private Function IsStructuredOrURI(value As Variant) As Boolean
378+
Private Function IsStructuredOrURI(value As String) As Boolean
377379
Dim tmpBool As Boolean
378380

379381
If IsStructuredData(value) Then
@@ -393,10 +395,10 @@ Private Function IsStructuredOrURI(value As Variant) As Boolean
393395
End If
394396
IsStructuredOrURI = tmpBool
395397
End Function
396-
Private Function IsUnixAbsolutePath(value As Variant) As Boolean
398+
Private Function IsUnixAbsolutePath(value As String) As Boolean
397399
IsUnixAbsolutePath = LikeCompare(value, "/*")
398400
End Function
399-
Private Function IsURL(value As Variant) As Boolean
401+
Private Function IsURL(value As String) As Boolean
400402
If InStrB(1, value, "://") Then
401403
If value Like "[a-z][a-z]*[a-z]://*" Then
402404
If value Like "http://*" Or value Like "https://*" _
@@ -421,7 +423,7 @@ Private Function IsURL(value As Variant) As Boolean
421423
End If
422424
IsURL = tmpBool
423425
End Function
424-
Private Function IsValidIPv4(value As Variant) As Boolean
426+
Private Function IsValidIPv4(value As String) As Boolean
425427
Dim tmpData() As String
426428
tmpData() = Split(value, ".")
427429
If UBound(tmpData) - LBound(tmpData) + 1 = 4 Then
@@ -447,7 +449,7 @@ Private Function IsValidIPv4(value As Variant) As Boolean
447449
End If
448450
End If
449451
End Function
450-
Private Function IsValidIPv4Range(valuesArray As Variant) As Boolean
452+
Private Function IsValidIPv4Range(valuesArray() As String) As Boolean
451453
Dim iCounter As Long
452454
Dim tmpBool As Boolean
453455

@@ -462,7 +464,7 @@ Private Function IsValidIPv4Range(valuesArray As Variant) As Boolean
462464
Loop While iCounter <= UBound(valuesArray) And tmpBool
463465
IsValidIPv4Range = tmpBool
464466
End Function
465-
Private Function IsWindowsAbsolutePath(value As Variant) As Boolean
467+
Private Function IsWindowsAbsolutePath(value As String) As Boolean
466468
IsWindowsAbsolutePath = LikeCompare(value, "[A-Za-z]:\*")
467469
End Function
468470
Private Function RecordsAvgFields(ArrayList As CSVArrayList) As Double
@@ -492,7 +494,7 @@ Private Function RecordScore(ByRef strArray As Variant) As Double
492494
FieldsCount = 1 + UBound(strArray) - LBound(strArray)
493495
tmpSUM = 0
494496
For L0 = LBound(strArray) To UBound(strArray)
495-
Select Case DetectDataType(strArray(L0))
497+
Select Case DetectDataType(CStr(strArray(L0)))
496498
Case FieldDataType.Known
497499
tmpSUM = tmpSUM + 100
498500
Case Else
@@ -544,7 +546,7 @@ Public Function TableScore(ByRef ArrayList As CSVArrayList) As Double
544546
If ArrayList.count > 1 Then
545547
TableScore = RecordsConsistencyFactor(ArrayList, SumRecScores) * SumRecScores / ArrayList.count
546548
Else
547-
TableScore = RecordsConsistencyFactor(ArrayList, SumRecScores) * SumRecScores / 2
549+
TableScore = RecordsConsistencyFactor(ArrayList, SumRecScores) * SumRecScores / 10 'Supossed number of records to be imported
548550
End If
549551
End If
550552
End If

src/CSVinterface.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3258,7 +3258,7 @@ Private Function SniffInString(ByRef confObject As CSVparserConfig, _
32583258
GuesserHelper.TableScore(ImportedTable)
32593259
Else
32603260
ScoreArray.AddIndexedItem AppendIndexesToKey(DialectToString(.dialect) & CHR_CARET, i, j), _
3261-
GuesserHelper.TableScore(ImportedTable) '/ 2
3261+
GuesserHelper.TableScore(ImportedTable)
32623262
End If
32633263
Next k
32643264
Next j

0 commit comments

Comments
 (0)