From a42801d100c2ec33b93af89f7fd2b17cfe5d4fbc Mon Sep 17 00:00:00 2001 From: Simon Krauter Date: Fri, 3 Mar 2017 01:31:17 +0100 Subject: [PATCH] Make countLines() the same as len(splitLines(s)) (#5470) The result of countLines() is now increased by 1 compared to the old version. Fixes #5460. --- lib/packages/docutils/rstgen.nim | 2 +- lib/pure/strutils.nim | 12 ++++-------- tests/manyloc/keineschweine/lib/sg_gui.nim | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/packages/docutils/rstgen.nim b/lib/packages/docutils/rstgen.nim index 47247dd7cf..ce63d780c8 100644 --- a/lib/packages/docutils/rstgen.nim +++ b/lib/packages/docutils/rstgen.nim @@ -870,7 +870,7 @@ proc buildLinesHTMLTable(d: PDoc; params: CodeBlockParams, code: string): d.config.getOrDefault"doc.listing_end" % id) return - var codeLines = 1 + code.strip.countLines + var codeLines = code.strip.countLines assert codeLines > 0 result.beginTable = """
"""
   var line = params.startLine
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim
index 9b25263377..a87df7f529 100644
--- a/lib/pure/strutils.nim
+++ b/lib/pure/strutils.nim
@@ -767,20 +767,16 @@ proc splitLines*(s: string): seq[string] {.noSideEffect,
 
 proc countLines*(s: string): int {.noSideEffect,
   rtl, extern: "nsuCountLines".} =
-  ## Returns the number of new line separators in the string `s`.
+  ## Returns the number of lines in the string `s`.
   ##
   ## This is the same as ``len(splitLines(s))``, but much more efficient
   ## because it doesn't modify the string creating temporal objects. Every
   ## `character literal `_ newline combination
   ## (CR, LF, CR-LF) is supported.
   ##
-  ## Despite its name this proc might not actually return the *number of lines*
-  ## in `s` because the concept of what a line is can vary. For example, a
-  ## string like ``Hello world`` is a line of text, but the proc will return a
-  ## value of zero because there are no newline separators.  Also, text editors
-  ## usually don't count trailing newline characters in a text file as a new
-  ## empty line, but this proc will.
-  var i = 0
+  ## In this context, a line is any string seperated by a newline combination.
+  ## A line can be an empty string.
+  var i = 1
   while i < s.len:
     case s[i]
     of '\c':
diff --git a/tests/manyloc/keineschweine/lib/sg_gui.nim b/tests/manyloc/keineschweine/lib/sg_gui.nim
index aae51baec3..ffc4e8215b 100644
--- a/tests/manyloc/keineschweine/lib/sg_gui.nim
+++ b/tests/manyloc/keineschweine/lib/sg_gui.nim
@@ -240,7 +240,7 @@ proc add*(m: PMessageArea, msg: ScChat) =
   of CPriv, CSystem: mmm.color = Green
   of CError: mmm.color = Red
 
-  mmm.lines = countLines(mmm.text)+1
+  mmm.lines = countLines(mmm.text)
 
   m.messages.add mmm
   update m