On 6/22/10, Don <nos...@nospam.com> wrote: > Did you consider situations where the last thing before the case is > actually a 'goto' ? Walter does that a lot.
Yeah, me too. I counted them the same as break (and continue, return, and throw). Here's my source. I know it has some false negatives and some false positives, which is why I rounded so vigorously. ======= import std.string; import std.stdio; void main(string[] args) { int totalSwitchCount, totalFallthroughCount, totalCaseCount, totalCaseFallthroughCount; foreach(arg; args[1..$]) { auto f = File(arg); int fallthroughCount = 0; int switchCount = 0; bool caseAlreadyOpen; bool fallthroughAlready; int caseCount = 0; int caseFallthroughCount; string lastLine; try foreach(line; f.byLine) { // if it says "case .*:", but not goto, we'll call it a switch case if(line.indexOf("case ") != -1 && line.indexOf("goto") == -1 && line.indexOf(":") != -1) { caseCount++; totalCaseCount++; if(caseAlreadyOpen) { // the first one doesn't count if( // If this line, or the last line (with a semicolon) breaks, it isn't fallthrough lastLine.indexOf("break") == -1 && line.indexOf("break") == -1 && // ditto for goto, continue, and return lastLine.indexOf("goto") == -1 && line.indexOf("goto") == -1 && lastLine.indexOf("continue") == -1 && line.indexOf("continue") == -1 && lastLine.indexOf("return") == -1 && line.indexOf("return") == -1 && // and of course, exceptions lastLine.indexOf("throw") == -1 && line.indexOf("throw") == -1 ) { totalCaseFallthroughCount++; caseFallthroughCount++; if(!fallthroughAlready) { fallthroughCount++; totalFallthroughCount++; } fallthroughAlready = true; } } else caseAlreadyOpen = true; } if(line.indexOf("switch") != -1) { totalSwitchCount++; switchCount++; caseAlreadyOpen = false; fallthroughAlready = false; } if(line.indexOf(";") != -1) // only consider lines with semicolons as a crude way to find non-empty cases and ignore whitespace lastLine = line.idup; } catch(Exception e) { } // i don't care writefln("%s: %d/%d (%d/%d cases)", arg, fallthroughCount, switchCount, caseFallthroughCount, caseCount); } writefln("Total switches: %d/%d (%.1f%%)", totalFallthroughCount, totalSwitchCount, cast(float) totalFallthroughCount / totalSwitchCount * 100); writefln(" Total cases: %d/%d (%.1f%%)", totalCaseFallthroughCount, totalCaseCount, cast(float) totalCaseFallthroughCount / totalCaseCount * 100); } ======= I used the try without {} there too, which was mentioned in another thread. I do this often; it never even occurred to me that it wouldn't be allowed. It isn't for if and for, etc, so why would it be for try? I love it.