patternMinor
Deserialize an email header into key-value pairs
Viewed 0 times
headeremailintovaluepairsdeserializekey
Problem
I've created a function that will deserialize an email header into a list of key-value pairs. I've run numerous tests using MS Office Outlook 2010 and MS Office 14.0 Object Library, all of which were successful.
However, I'd still like to know if this function could be optimized/improved.
Note
However, I'd still like to know if this function could be optimized/improved.
Note
- I didn't use the Dictionary(Of TKey, TValue) class because a
keycan appear multiple times.
- The reason as to why I wrote this RegEx pattern is because a
valuecan also contain anew lineand/or:.
Public Shared Function Deserialize(header As String) As List(Of KeyValuePair(Of String, String))
Const pattern As String = "(\n|^)[a-z|A-Z|-]*:\s"
If (header Is Nothing) Then Throw New ArgumentNullException("header")
Dim result As New List(Of KeyValuePair(Of String, String))
Dim matches As MatchCollection = Regex.Matches(header, pattern)
If ((Not matches Is Nothing) AndAlso (matches.Count > 0)) Then
Dim endIndex As Integer = header.Length
Dim startIndex As Integer = Nothing
Dim item As Match = Nothing
Dim key As String = Nothing
Dim value As String = Nothing
For index As Integer = (matches.Count - 1) To 0 Step -1
item = matches.Item(index)
startIndex = (item.Index + item.Length)
key = item.Value.Trim().Replace(":", "")
value = header.Substring(startIndex, (endIndex - startIndex))
result.Insert(0, New KeyValuePair(Of String, String)(key, value))
endIndex = item.Index
Next
End If
Return result
End Function
Solution
There are a couple of things that I find odd about this code. This is the first thing that jumps out at me.
I see no purpose in initializing variable to
I also find your
Personally, I would rewrite this to loop forward like this.
You could also use a comment here.
Regular Expression patterns are rarely obvious. Tell Mr. Maintainer in English what you're matching on.
Dim startIndex As Integer = Nothing
Dim item As Match = Nothing
Dim key As String = NothingI see no purpose in initializing variable to
Nothing. Simply declaring them would be cleaner in my opinion. Dim startIndex As Integer
Dim item as Match
Dim key As StringI also find your
For loop a bit strange. Is there a reason you're stepping backwards? If there is, I don't see it and you should comment on why in the code. For index As Integer = (matches.Count - 1) To 0 Step -1Personally, I would rewrite this to loop forward like this.
For index As Integer = 0 To matches.Count - 1
'...
Next indexYou could also use a comment here.
Const pattern As String = "(\n|^)[a-z|A-Z|-]*:\s"Regular Expression patterns are rarely obvious. Tell Mr. Maintainer in English what you're matching on.
Code Snippets
Dim startIndex As Integer = Nothing
Dim item As Match = Nothing
Dim key As String = NothingDim startIndex As Integer
Dim item as Match
Dim key As StringFor index As Integer = (matches.Count - 1) To 0 Step -1For index As Integer = 0 To matches.Count - 1
'...
Next indexConst pattern As String = "(\n|^)[a-z|A-Z|-]*:\s"Context
StackExchange Code Review Q#63357, answer score: 4
Revisions (0)
No revisions yet.