Replace string.len with #

This commit is contained in:
Jonathan Coates 2019-12-10 18:54:43 +00:00
parent b0397ed3c5
commit 03b6d2f1ab
No known key found for this signature in database
GPG Key ID: D6D4CB5BFBBB5CB8
10 changed files with 47 additions and 80 deletions

View File

@ -1,35 +0,0 @@
std = "max"
ignore = {
-- Allow access to undefined globals or their fields. In the future we'll
-- define all of CC's globals within this file
'113', '143',
-- FIXME: Ignore unused arguments and loop variables
'212', '213',
-- Disable line is too long for now. It would be possible to clean
-- this up in the future.
'631',
}
-- Only run the linter on ROM and bios for now, as the treasure disks
-- are largely unsupported.
include_files = {
'src/main/resources/assets/computercraft/lua/rom',
'src/main/resources/assets/computercraft/lua/bios.lua',
'src/test/resources/test-rom',
}
files['src/main/resources/assets/computercraft/lua/bios.lua'] = {
-- Allow declaring and mutating globals
allow_defined_top = true,
ignore = { '112', '121', '122', '131', '142' },
}
files['src/main/resources/assets/computercraft/lua/rom/apis'] = {
-- APIs may define globals on the top level. We'll ignore unused globals,
-- as obviously they may be used outside that API.
allow_defined_top = true,
ignore = { '131' },
}

View File

@ -7,20 +7,22 @@
(at /
(linters
;; It'd be nice to avoid this, but right now there's a lot of instances of it.
;; It'd be nice to avoid this, but right now there's a lot of instances of
;; it.
-var:set-loop
;; It's useful to name arguments for documentation, so we allow this. It'd
;; be good to find a compromise in the future, but this works for now.
-var:unused-arg))
;; We disable the two global linters in bios.lua and the APIs. In the future
;; We disable the unused global linter in bios.lua and the APIs. In the future
;; hopefully we'll get illuaminate to handle this.
(at
(/src/main/resources/assets/computercraft/lua/bios.lua
/src/main/resources/assets/computercraft/lua/rom/apis/)
(linters -var:set-global -var:unused-global))
(linters -var:unused-global)
(lint
(allow-toplevel-global true)))
;; These warnings are broken right now
(at completion.lua (linters -doc:malformed-type))
(at (bios.lua worm.lua) (linters -control:unreachable))

View File

@ -207,13 +207,13 @@ function write( sText )
end
-- Print the line with proper word wrapping
while string.len(sText) > 0 do
while #sText > 0 do
local whitespace = string.match( sText, "^[ \t]+" )
if whitespace then
-- Print whitespace
term.write( whitespace )
x, y = term.getCursorPos()
sText = string.sub( sText, string.len(whitespace) + 1 )
sText = string.sub( sText, #whitespace + 1 )
end
local newline = string.match( sText, "^\n" )
@ -225,10 +225,10 @@ function write( sText )
local text = string.match( sText, "^[^ \t\n]+" )
if text then
sText = string.sub( sText, string.len(text) + 1 )
if string.len(text) > w then
sText = string.sub( sText, #text + 1 )
if #text > w then
-- Print a multiline word
while string.len( text ) > 0 do
while #text > 0 do
if x > w then
newLine()
end
@ -238,7 +238,7 @@ function write( sText )
end
else
-- Print a word normally
if x + string.len(text) - 1 > w then
if x + #text - 1 > w then
newLine()
end
term.write( text )
@ -299,7 +299,7 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault )
local tCompletions
local nCompletion
local function recomplete()
if _fnComplete and nPos == string.len(sLine) then
if _fnComplete and nPos == #sLine then
tCompletions = _fnComplete( sLine )
if tCompletions and #tCompletions > 0 then
nCompletion = 1

View File

@ -10,7 +10,7 @@ function slowWrite( sText, nRate )
sText = tostring( sText )
local x, y = term.getCursorPos()
local len = string.len( sText )
local len = #sText
for n = 1, len do
term.setCursorPos( x, y )
@ -116,7 +116,7 @@ local function tabulateCommon( bPaged, ... )
if type( sItem ) ~= "string" then
error( "bad argument #" .. n .. "." .. nu .. " (expected string, got " .. type( sItem ) .. ")", 3 )
end
nMaxLen = math.max( string.len( sItem ) + 1, nMaxLen )
nMaxLen = math.max( #sItem + 1, nMaxLen )
end
end
end
@ -388,7 +388,7 @@ function complete( sSearchText, tSearchTable )
end
local sPart = string.sub( sSearchText, nStart )
local nPartLength = string.len( sPart )
local nPartLength = #sPart
local tResults = {}
local tSeen = {}

View File

@ -287,7 +287,7 @@ while #tProcesses > 0 do
tabStart = 2
end
for n = nScrollPos, #tProcesses do
local tabEnd = tabStart + string.len( tProcesses[n].sTitle ) + 1
local tabEnd = tabStart + #tProcesses[n].sTitle + 1
if x >= tabStart and x <= tabEnd then
selectProcess( n )
redrawMenu()

View File

@ -62,7 +62,7 @@ end
table.insert( tMenuItems, "Exit" )
local sStatus = "Press Ctrl to access menu"
if string.len( sStatus ) > w - 5 then
if #sStatus > w - 5 then
sStatus = "Press Ctrl for menu"
end
@ -144,13 +144,13 @@ local function tryWrite( sLine, regex, colour )
end
term.write( match )
term.setTextColour( textColour )
return string.sub( sLine, string.len(match) + 1 )
return string.sub( sLine, #match + 1 )
end
return nil
end
local function writeHighlighted( sLine )
while string.len(sLine) > 0 do
while #sLine > 0 do
sLine =
tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour ) or
tryWrite( sLine, "^%-%-.*", commentColour ) or
@ -188,7 +188,7 @@ end
local function recomplete()
local sLine = tLines[y]
if not bMenu and not bReadOnly and x == string.len(sLine) + 1 then
if not bMenu and not bReadOnly and x == #sLine + 1 then
tCompletions = complete( sLine )
if tCompletions and #tCompletions > 0 then
nCompletion = 1
@ -248,7 +248,7 @@ local function redrawMenu()
term.clearLine()
-- Draw line numbers
term.setCursorPos( w - string.len( "Ln " .. y ) + 1, h )
term.setCursorPos( w - #( "Ln " .. y ) + 1, h )
term.setTextColour( highlightColour )
term.write( "Ln " )
term.setTextColour( textColour )
@ -468,7 +468,7 @@ local function acceptCompletion()
-- Append the completion
local sCompletion = tCompletions[ nCompletion ]
tLines[y] = tLines[y] .. sCompletion
setCursor( x + string.len( sCompletion ), y )
setCursor( x + #sCompletion , y )
end
end
@ -490,7 +490,7 @@ while bRunning do
elseif y > 1 then
-- Move cursor up
setCursor(
math.min( x, string.len( tLines[y - 1] ) + 1 ),
math.min( x, #tLines[y - 1] + 1 ),
y - 1
)
end
@ -511,7 +511,7 @@ while bRunning do
elseif y < #tLines then
-- Move cursor down
setCursor(
math.min( x, string.len( tLines[y + 1] ) + 1 ),
math.min( x, #tLines[y + 1] + 1 ),
y + 1
)
end
@ -520,7 +520,7 @@ while bRunning do
elseif param == keys.tab then
-- Tab
if not bMenu and not bReadOnly then
if nCompletion and x == string.len(tLines[y]) + 1 then
if nCompletion and x == #tLines[y] + 1 then
-- Accept autocomplete
acceptCompletion()
else
@ -542,7 +542,7 @@ while bRunning do
newY = 1
end
setCursor(
math.min( x, string.len( tLines[newY] ) + 1 ),
math.min( x, #tLines[newY] + 1 ),
newY
)
end
@ -557,7 +557,7 @@ while bRunning do
else
newY = #tLines
end
local newX = math.min( x, string.len( tLines[newY] ) + 1 )
local newX = math.min( x, #tLines[newY] + 1 )
setCursor( newX, newY )
end
@ -574,7 +574,7 @@ while bRunning do
-- End
if not bMenu then
-- Move cursor to the end
local nLimit = string.len( tLines[y] ) + 1
local nLimit = #tLines[y] + 1
if x < nLimit then
setCursor( nLimit, y )
end
@ -587,7 +587,7 @@ while bRunning do
-- Move cursor left
setCursor( x - 1, y )
elseif x == 1 and y > 1 then
setCursor( string.len( tLines[y - 1] ) + 1, y - 1 )
setCursor( #tLines[y - 1] + 1, y - 1 )
end
else
-- Move menu left
@ -601,11 +601,11 @@ while bRunning do
elseif param == keys.right then
-- Right
if not bMenu then
local nLimit = string.len( tLines[y] ) + 1
local nLimit = #tLines[y] + 1
if x < nLimit then
-- Move cursor right
setCursor( x + 1, y )
elseif nCompletion and x == string.len(tLines[y]) + 1 then
elseif nCompletion and x == #tLines[y] + 1 then
-- Accept autocomplete
acceptCompletion()
elseif x == nLimit and y < #tLines then
@ -624,7 +624,7 @@ while bRunning do
elseif param == keys.delete then
-- Delete
if not bMenu and not bReadOnly then
local nLimit = string.len( tLines[y] ) + 1
local nLimit = #tLines[y] + 1
if x < nLimit then
local sLine = tLines[y]
tLines[y] = string.sub(sLine, 1, x - 1) .. string.sub(sLine, x + 1)
@ -653,7 +653,7 @@ while bRunning do
end
elseif y > 1 then
-- Remove newline
local sPrevLen = string.len( tLines[y - 1] )
local sPrevLen = #tLines[y - 1]
tLines[y - 1] = tLines[y - 1] .. tLines[y]
table.remove( tLines, y )
setCursor( sPrevLen + 1, y - 1 )
@ -721,7 +721,7 @@ while bRunning do
-- Input text
local sLine = tLines[y]
tLines[y] = string.sub(sLine, 1, x - 1) .. param .. string.sub(sLine, x)
setCursor( x + string.len( param ), y )
setCursor( x + #param , y )
end
elseif sEvent == "mouse_click" then
@ -731,7 +731,7 @@ while bRunning do
local cx, cy = param2, param3
if cy < h then
local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
local newX = math.min( math.max( scrollX + cx, 1 ), #tLines[newY] + 1 )
setCursor( newX, newY )
end
end

View File

@ -148,7 +148,7 @@ local function save(path)
end
sLine = string.sub( sLine, 1, nLastChar )
tLines[y] = sLine
if string.len( sLine ) > 0 then
if #sLine > 0 then
nLastLine = y
end
end

View File

@ -60,7 +60,7 @@ local tArgs = { ... }
--Functions--
local function printCentred( yc, stg )
local xc = math.floor((TermW - string.len(stg)) / 2) + 1
local xc = math.floor((TermW - #stg) / 2) + 1
term.setCursorPos(xc, yc)
term.write( stg )
end
@ -179,7 +179,7 @@ local function loadLevel(nNum)
fLevel = fs.open(sLevelD, "r")
local wl = true
Blocks = tonumber(string.sub(fLevel.readLine(), 1, 1))
local xSize = string.len(fLevel.readLine()) + 2
local xSize = #fLevel.readLine() + 2
local Lines = 3
while wl do
local wLine = fLevel.readLine()
@ -187,7 +187,7 @@ local function loadLevel(nNum)
fLevel.close()
wl = false
else
xSize = math.max(string.len(wLine) + 2, xSize)
xSize = math.max(#wLine + 2, xSize)
Lines = Lines + 1
end
end
@ -197,7 +197,7 @@ local function loadLevel(nNum)
fLevel.readLine()
for Line = 2, Lines - 1 do
local sLine = fLevel.readLine()
local chars = string.len(sLine)
local chars = #sLine
for char = 1, chars do
local el = string.sub(sLine, char, char)
if el == "8" then

View File

@ -16,7 +16,7 @@ else
end
local function printCentred( y, s )
local x = math.floor((w - string.len(s)) / 2)
local x = math.floor((w - #s) / 2)
term.setCursorPos(x, y)
--term.clearLine()
term.write( s )

View File

@ -130,14 +130,14 @@ if sCommand == "host" then
local tCommands
tCommands = {
["me"] = function( tUser, sContent )
if string.len(sContent) > 0 then
if #sContent > 0 then
send( "* " .. tUser.sUsername .. " " .. sContent )
else
send( "* Usage: /me [words]", tUser.nUserID )
end
end,
["nick"] = function( tUser, sContent )
if string.len(sContent) > 0 then
if #sContent > 0 then
local sOldName = tUser.sUsername
tUser.sUsername = sContent
send( "* " .. sOldName .. " is now known as " .. tUser.sUsername )
@ -199,7 +199,7 @@ if sCommand == "host" then
if sCommand then
local fnCommand = tCommands[ sCommand ]
if fnCommand then
local sContent = string.sub( sMessage, string.len(sCommand) + 3 )
local sContent = string.sub( sMessage, #sCommand + 3 )
fnCommand( tUser, sContent )
else
send( "* Unrecognised command: /" .. sCommand, tUser.nUserID )
@ -300,7 +300,7 @@ elseif sCommand == "join" then
local w = titleWindow.getSize()
local sTitle = sUsername .. " on " .. sHostname
titleWindow.setTextColour( highlightColour )
titleWindow.setCursorPos( math.floor( w / 2 - string.len(sTitle) / 2 ), 1 )
titleWindow.setCursorPos( math.floor( w / 2 - #sTitle / 2 ), 1 )
titleWindow.clearLine()
titleWindow.write( sTitle )
promptWindow.restoreCursor()
@ -321,7 +321,7 @@ elseif sCommand == "join" then
term.setTextColour( highlightColour )
write( sUsernameBit )
term.setTextColour( textColour )
write( string.sub( sMessage, string.len( sUsernameBit ) + 1 ) )
write( string.sub( sMessage, #sUsernameBit + 1 ) )
else
write( sMessage )
end