From c45a01221255c241c636ba6165e0f4a1028d8fde Mon Sep 17 00:00:00 2001 From: sleepyfox Date: Sun, 19 Feb 2012 19:40:01 +0000 Subject: [PATCH] refactored Source and Line classes into separate module --- loc.coffee | 63 ++++++++++++++++++++++++++++++++++++++ test-lines-of-code.coffee | 64 ++------------------------------------- 2 files changed, 66 insertions(+), 61 deletions(-) create mode 100644 loc.coffee diff --git a/loc.coffee b/loc.coffee new file mode 100644 index 0000000..d5d49cb --- /dev/null +++ b/loc.coffee @@ -0,0 +1,63 @@ +class Line + constructor: (@string) -> + + isAllOneLineComment: -> + if @string.match(/^\s*\/\//) is null + false + else + true + + isStartOfBlockComment: -> + if @string.match(/\/\*/) is null + false + else + true + + isEndOfBlockComment: -> + if @string.match(/\*\//) is null + false + else + true + + hasNonWhitespaceBeforeBlockCommentStart: -> + if @string.match(/\S+\s*\/\*/) is null + false + else + true + + hasNonWhitespaceAfterBlockCommentEnd: -> + if @string.match(/\*\/\s*\S+/) is null + false + else + true + +class Source + constructor: (string) -> + @array = string.split '\n' + + lines: -> + @array.length + + linesOfCode: -> + lineCounter = 0 + inBlockComment = false + for sourceLine in @array + line = new Line sourceLine + # start of hideous nested if/else block + if inBlockComment + if line.isEndOfBlockComment() + inBlockComment = false + if line.hasNonWhitespaceAfterBlockCommentEnd() + lineCounter++ + else # not in block comment + if line.isStartOfBlockComment() + inBlockComment = true + if line.hasNonWhitespaceBeforeBlockCommentStart() + lineCounter++ + else # not start of block comment + unless line.isAllOneLineComment() + lineCounter++ + lineCounter + +exports.Source = Source +exports.Line = Line diff --git a/test-lines-of-code.coffee b/test-lines-of-code.coffee index 98b9891..f86074b 100644 --- a/test-lines-of-code.coffee +++ b/test-lines-of-code.coffee @@ -1,5 +1,8 @@ vows = require 'vows' assert = require 'assert' +loc = require './loc' +Source = loc.Source +Line = loc.Line ONELINESOURCE = "public static final void main { }" @@ -86,64 +89,3 @@ vows .export(module) -class Line - constructor: (@string) -> - - isAllOneLineComment: -> - if @string.match(/^\s*\/\//) is null - false - else - true - - isStartOfBlockComment: -> - if @string.match(/\/\*/) is null - false - else - true - - isEndOfBlockComment: -> - if @string.match(/\*\//) is null - false - else - true - - hasNonWhitespaceBeforeBlockCommentStart: -> - if @string.match(/\S+\s*\/\*/) is null - false - else - true - - hasNonWhitespaceAfterBlockCommentEnd: -> - if @string.match(/\*\/\s*\S+/) is null - false - else - true - -class Source - constructor: (string) -> - @array = string.split '\n' - - lines: -> - @array.length - - linesOfCode: -> - lineCounter = 0 - inBlockComment = false - for sourceLine in @array - line = new Line sourceLine - # start of hideous nested if/else block - if inBlockComment - if line.isEndOfBlockComment() - inBlockComment = false - if line.hasNonWhitespaceAfterBlockCommentEnd() - lineCounter++ - else # not in block comment - if line.isStartOfBlockComment() - inBlockComment = true - if line.hasNonWhitespaceBeforeBlockCommentStart() - lineCounter++ - else # not start of block comment - unless line.isAllOneLineComment() - lineCounter++ - lineCounter -