diff options
Diffstat (limited to 'common.go')
-rw-r--r-- | common.go | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/common.go b/common.go new file mode 100644 index 0000000..51486b8 --- /dev/null +++ b/common.go @@ -0,0 +1,18 @@ +package git + +import "strings" + +// CountLines returns the number of lines in a string à la git, this is +// The newline character is assumed to be '\n'. The empty string +// contains 0 lines. If the last line of the string doesn't end with a +// newline, it will still be considered a line. +func CountLines(s string) int { + if s == "" { + return 0 + } + nEol := strings.Count(s, "\n") + if strings.HasSuffix(s, "\n") { + return nEol + } + return nEol + 1 +} |