CodeSOD: A Case of Conversion

This post was originally published on this site

The Daily WTF

Mattijs‘s co-worker was responsible for an HTML-to-PDF converter that was used internally. While TRWTF is always going to be PDFs, there were some odd choices used in the converter, starting with the function named ConvertTagsToLowerCase.

private static string ConvertTagsToLowerCase(string RichText) { //remove any double tags which are not in SupportedDoubleTags /> char[] array = new char[RichText.Length]; int arrayIndex = 0; bool inside = false; StringBuilder sb = new StringBuilder(); for (int i = 0; i < RichText.Length; i++) { char let = RichText[i]; if (let == ‘<‘) { inside = true; //clear stringbuilder sb.Remove(0, sb.ToString().Length); continue; } if (let == ‘>’) { inside = false; array[arrayIndex] = ‘<‘; arrayIndex++; string HTMLInside = sb.ToString().ToLower(); for (int j = 0; j < HTMLInside.Length; j++) { array[arrayIndex] = HTMLInside[j]; arrayIndex++; } array[arrayIndex] = ‘>’; arrayIndex++; continue; } if (!inside) { array[arrayIndex] = let; arrayIndex++; } else { //append to the string inside

To read the full article click on the 'post' link at the top.