Skip to content

Commit

Permalink
refactored Source and Line classes into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepyfox committed Feb 19, 2012
1 parent 166847c commit c45a012
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 61 deletions.
63 changes: 63 additions & 0 deletions 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
64 changes: 3 additions & 61 deletions 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 { }"

Expand Down Expand Up @@ -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

0 comments on commit c45a012

Please sign in to comment.