1313 assert
1414 attributeWith, attributeWithAny
1515 cleanupPath
16- countStrings, countSubStrings
16+ countString, countSubString
1717 createDirectory, isDirectoryEmpty, removeEmptyDirectories
1818 createThumbnail
1919 datetimeToTime, getDatetime
376376 local echoI2 = (doTrim and blockI1Trimmed or blockI1) - 1
377377
378378 if doTrim and blockI1 > blockI1Trimmed then
379- table.insert(out, ("\n"):rep(countSubStrings (template, blockI1Trimmed, blockI1-1, "\n", true))) -- :TrimBlocks
379+ table.insert(out, ("\n"):rep(countSubString (template, blockI1Trimmed, blockI1-1, "\n", true))) -- :TrimBlocks
380380 end
381381
382382 if pos <= echoI2 then
405405 if doTrim then
406406 local posAfterBlock;posAfterBlock, pos = template:match("^()%s*()", pos)
407407 if pos > posAfterBlock then
408- table.insert(out, ("\n"):rep(countSubStrings (template, posAfterBlock, pos, "\n", true))) -- :TrimBlocks
408+ table.insert(out, ("\n"):rep(countSubString (template, posAfterBlock, pos, "\n", true))) -- :TrimBlocks
409409 end
410410 end
411411 `
@@ -799,9 +799,9 @@ function _G.errorf(levelOrS, ...)
799799end
800800
801801do
802- local function findStartOfNonEmptyLine (s, pos)
802+ local function findStartOfLine (s, pos, canBeEmpty )
803803 while pos > 1 do
804- if s:byte(pos-1) == !(string.byte"\n") and s:byte(pos) ~= !(string.byte"\n") then break end
804+ if s:byte(pos-1) == !(string.byte"\n") and (canBeEmpty or s:byte(pos) ~= !(string.byte"\n") ) then break end
805805 pos = pos - 1
806806 end
807807 return math.max(pos, 1)
829829 end
830830
831831 if contents then
832- local lineStart = findStartOfNonEmptyLine (contents, pos)
833- local lineEnd = findEndOfLine(contents, pos-1)
834- local linePre1Start = findStartOfNonEmptyLine (contents, lineStart-1)
835- local linePre1End = findEndOfLine(contents, linePre1Start-1)
836- local linePre2Start = findStartOfNonEmptyLine (contents, linePre1Start-1)
837- local linePre2End = findEndOfLine(contents, linePre2Start-1)
838- printf("pos %d | lines %d..%d, %d..%d, %d..%d", pos, linePre2Start,linePre2End+1, linePre1Start,linePre1End+1, lineStart,lineEnd+1)
839-
840- s = F("%s\n\n%s%s> %s\n> %s^",
832+ local lineStart = findStartOfLine (contents, pos, true )
833+ local lineEnd = findEndOfLine (contents, pos-1)
834+ local linePre1Start = findStartOfLine (contents, lineStart-1, false )
835+ local linePre1End = findEndOfLine (contents, linePre1Start-1)
836+ local linePre2Start = findStartOfLine (contents, linePre1Start-1, false )
837+ local linePre2End = findEndOfLine (contents, linePre2Start-1)
838+ -- printf("pos %d | lines %d..%d, %d..%d, %d..%d", pos, linePre2Start,linePre2End+1, linePre1Start,linePre1End+1, lineStart,lineEnd+1) -- DEBUG
839+
840+ s = F("%s\n> \n%s%s%s>- %s^",
841841 s,
842842 (linePre2Start < linePre1Start and linePre2Start <= linePre2End) and F("> %s\n", (contents:sub(linePre2Start, linePre2End):gsub("\t", " "))) or "",
843843 (linePre1Start < lineStart and linePre1Start <= linePre1End) and F("> %s\n", (contents:sub(linePre1Start, linePre1End):gsub("\t", " "))) or "",
844- contents:sub(lineStart, lineEnd):gsub("\t", " "),
845- ("-"):rep(pos - lineStart + 3*countSubStrings (contents, lineStart, lineEnd, "\t", true)),
844+ ( lineStart <= lineEnd ) and F("> %s\n", ( contents:sub(lineStart, lineEnd ):gsub("\t", " "))) or ">\n" ,
845+ ("-"):rep(pos - lineStart + 3*countSubString (contents, lineStart, lineEnd, "\t", true)),
846846 nil
847847 )
848848 end
861861
862862
863863function _G.getLineNumber(s, pos)
864- return 1 + countSubStrings (s, 1, pos-1, "\n", true)
864+ return 1 + countSubString (s, 1, pos-1, "\n", true)
865865end
866866
867867
@@ -2661,8 +2661,9 @@ end
26612661
26622662
26632663
2664- -- count = countStrings( string, needle [, plain=false ] )
2665- function _G.countStrings(s, needle, plain)
2664+ --[[ Unused.
2665+ -- count = countString( haystack, needle [, plain=false ] )
2666+ function _G.countString(s, needle, plain)
26662667 local count = 0
26672668 local pos = 1
26682669
@@ -2674,9 +2675,10 @@ function _G.countStrings(s, needle, plain)
26742675 pos = i2 + 1
26752676 end
26762677end
2678+ --]]
26772679
2678- -- count = countSubStrings( string , startPosition, endPosition, needle [, plain=false ] )
2679- function _G.countSubStrings (s, pos, posEnd, needle, plain)
2680+ -- count = countSubString( haystack , startPosition, endPosition, needle [, plain=false ] )
2681+ function _G.countSubString (s, pos, posEnd, needle, plain)
26802682 local count = 0
26812683
26822684 while true do
0 commit comments