Browse DevX
Sign up for e-mail newsletters from DevX


Elegant Isn't Always Best : Page 2




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Brute-Force String Handling
This situation reminds me of a coding problem that came up recently. My pal, Brian Randell, was trying to quickly rip out some code that could retrieve a URL from an Internet Explorer favorite (*.FAV) file. These files contain information about your selected favorites, and contain data in old-fashioned INI format, like this:

[DEFAULT] BASEURL=http://www.code-magazine.com/ [InternetShortcut] URL=http://www.code-magazine.com/ Modified=30A88EE7CBBAC40197

Although the contents of this file aren't generally documented, Brian wanted to retrieve just the URL from the file, using Visual Basic .NET. He was in a rush, and asked if I had code lying about to parse INI files.

I had written a set of classes to handle this issue for VBA applications, but hadn't really ever had a need to handle the INI file format in managed code. I'm sure a quick search online would have turned up a similar set of managed classes, but since his only goal was to retrieve a value given a specified key, I figured that I could rip out some brute-force string handling code quicker than searching online.

Listing 1 shows the first pass at solving this string parsing problem. This code does its job by converting both the INI text (which Brian had already managed to load, using the System.IO namespace) and the key value to be located into upper case, and then using the IndexOf method to find the location of the string:

Dim startPos As Integer = _ iniText.ToUpper.IndexOf(keyName.ToUpper())

If the sought item was found, the code attempts to find the end of the text line containing the item, searching for a CR/LF or the end of the text:

If startPos > 0 Then Dim textLine As String Dim endPos As Integer = iniText.IndexOf( _ ControlChars.CrLf, startPos + 1) If endPos > 0 Then textLine = iniText.Substring( _ startPos, endPos - startPos) Else textLine = iniText.Substring(startPos) End If ' code removed here. End If

Finally, the code splits the found line at the "=", and trims the results to remove extra spaces:

Dim items() As String = _ textLine.Split("=".ToCharArray) If items.Length > 1 Then returnValue = items(1).Trim() End If End If Return returnValue

Although this solution isn't elegant, it was quick to write and returned the required value. But it bothered me. As I was in the shower one morning, I was struck. "Isn't this a perfect scenario in which to use regular expressions?" I asked myself. Armed with the urge to make working code more elegant, I started again. This time, I did a quick bit of research into regular expressions in the System.Text.RegularExpression namespace. This feature provides a meta language that makes it possible for developers to craft incredibly rich search and replace operations based on complex write-only (that's my thought, anyway—you try reading one of these and figuring out what its purpose is) text expressions.

Thanks for your registration, follow us on our social networks to keep up-to-date