Is this ugly/bad code?

Hi there! *Waves*

I'm making a program to sort all the series I got on my computer in a sexy way so I can search and stuffs, and I wrote this method to find the episode number in the season.

All the filenames follow the same order, which is "Serie name - 'season'x'episode in season' - Episode name", so for example "Stargate SG-1 - 2x06 - Thor's Chariot.avi" is one.

The method I wrote is supposed to find the 06 number in the example filename, but when I look at it, I think it looks horrible :D And I wonder how you guys would do it, if there is a more elegant way.

It does the job, so I guess I'll stick with it for this program, but I don't want to only be able to write code that works, I want to learn how to write good code =)

Anyways, here's the method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
std::wstring Episode::FindEpisodeNumber(std::wstring fileName)
{
	// Index start from the end of the filename and goes backwards
	for (int index = fileName.size(); index > 7; --index)
	{
		// If it finds "'digit'x'digit'digit' - " which means "2x06 - " from the example
		if ((fileName[index - 1] == L' ') &&
			(fileName[index - 2] == L'-') &&
			(fileName[index - 3] == L' ') &&
			(isdigit(fileName[index - 4])) &&
			(isdigit(fileName[index - 5])) &&
			(fileName[index - 6] == L'x') &&
			(isdigit(fileName[index - 7])))
		{
			// Remove everything except the episode number (06 from the example) return it
			fileName.erase(index - 3, fileName.size() - (index - 4));
			fileName.erase(0, index - 5);
			return fileName;
		}
	}
}
This sounds like the job for regular expressions. modern C++ has them under <regex> header (see MSDN for example: http://msdn.microsoft.com/en-us/library/bb982382 ), and even if you're on a pre-TR1 system, third-party regex libraries are available everywhere.
Topic archived. No new replies allowed.