common.go
raw| 1 | package git |
| 2 | |
| 3 | import "strings" |
| 4 | |
| 5 | // countLines returns the number of lines in a string à la git, this is |
| 6 | // The newline character is assumed to be '\n'. The empty string |
| 7 | // contains 0 lines. If the last line of the string doesn't end with a |
| 8 | // newline, it will still be considered a line. |
| 9 | func countLines(s string) int { |
| 10 | if s == "" { |
| 11 | return 0 |
| 12 | } |
| 13 | |
| 14 | nEOL := strings.Count(s, "\n") |
| 15 | if strings.HasSuffix(s, "\n") { |
| 16 | return nEOL |
| 17 | } |
| 18 | |
| 19 | return nEOL + 1 |
| 20 | } |