fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
a8034e9
v2.1.4
1local floor =math . floor 2local random =math . random 3 4local byte =string . byte 5local char =string . char 6local find =string . find 7local format =string . format 8local gmatch =string . gmatch 9local gsub =string . gsub 10local match =string . match 11local reverse =string . reverse 12local sub =string . sub 13local upper =string . upper 14 15local concat =table . concat 16local insert =table . insert 17 18function string . getFilePath ( path ) 19return match ( path , "^(.*[/\\])[^/\\]-$" ) or "" 20end 21 22function string . getFileExtension ( path ) 23return match ( path , "%.([^%.]+)$" ) or "" 24end 25 26function string . stripFileExtension ( path ) 27local i =match ( path , ".+()%.[^%.]+$" ) 28if i then return sub ( path , 1 , i - 1 ) end 29return path 30end 31 32do 33-- With special chars: 33-126 34-- without: 48-57, 65-90, 97-122 (this is going to be a bit messy without continue) 35local num =0 --recycling these up here since they get called a lot 36local function numroll () 37num =random ( 48 , 122 ) 38if ( num > 57 and num < 65 ) or ( num > 90 and num < 97 ) then 39return numroll () 40else 41return num 42end 43end 44 45local tab ={} 46function string . random ( len , specialchars ) 47tab ={} 48if specialchars then 49for i =1 , len do 50insert ( tab , char ( random ( 33 , 126 ))) 51end 52return concat ( tab ) 53else 54for i =1 , len do 55insert ( tab , char ( numroll ())) 56end 57return concat ( tab ) 58end 59end 60end 61 62function string . toTable ( str ) 63local tbl ={} 64 65for i =1 , # str do 66tbl [ i ] =sub ( str , i , i ) 67end 68 69return tbl 70end 71 72function string . explode ( str , sep , pat ) 73if sep == "" then return string . toTable ( str ) end 74 75local ret ={} 76local i , pos =1 , 1 77 78if not pat then sep =gsub ( sep , "[%-%^%$%(%)%%%.%[%]%*%+%-%?]" , "%%%1" ) end 79 80for startpos , endpos in gmatch ( str , "()" .. sep .. "()" ) do 81ret [ i ] =sub ( str , pos , startpos - 1 ) 82i =i + 1 83pos =endpos 84end 85 86ret [ i ] =sub ( str , pos ) 87return ret 88end 89 90-- remove trailing and leading whitespace from string. 91function string . trim ( s ) 92return gsub ( s , "^%s*(.-)%s*$" , "%1" ) 93end 94 95local trim =string . trim 96 97-- remove leading whitespace from string. 98function string . ltrim ( s ) 99return gsub ( s , "^%s*" , "" ) 100end 101 102-- remove trailing whitespace from string. 103function string . rtrim ( s ) 104local n =# s 105while n > 0 and find ( s , "^%s" , n ) do n =n - 1 end 106return sub ( s , 1 , n ) 107end 108 109function string . parseArgs ( line ) 110local cmd , val =match ( line , "(%S-)%s-=%s+(.+)" ) 111if cmd and val then 112return { trim ( cmd ), trim ( val ) } 113end 114local quote =sub ( line , 1 , 1 ) ~= '"' 115local ret ={} 116for chunk in gmatch ( line , '[^"]+' ) do 117quote =not quote 118if quote then 119insert ( ret , chunk ) 120else 121for chunk in gmatch ( chunk , "%S+" ) do -- changed %w to %S to allow all characters except space 122insert ( ret , chunk ) 123end 124end 125end 126return ret 127end 128 129local pattern_escape_replacements ={ 130[ "(" ] ="%(" , 131[ ")" ] ="%)" , 132[ "." ] ="%." , 133[ "%" ] ="%%" , 134[ "+" ] ="%+" , 135[ "-" ] ="%-" , 136[ "*" ] ="%*" , 137[ "?" ] ="%?" , 138[ "[" ] ="%[" , 139[ "]" ] ="%]" , 140[ "^" ] ="%^" , 141[ "$" ] ="%$" , 142[ "\0" ] ="%z" 143} 144 145function string . escape ( str ) 146return gsub ( str , "." , pattern_escape_replacements ) 147end 148 149function string . toSize ( size ) 150size =tonumber ( size ) or 0 151 152if size <= 0 then return "0" end 153if size < 1024 then return format ( "%.2f Bytes" , size ) end 154if size < 1024 * 1024 then return format ( "%.2f KB" , size / 1024 ) end 155if size < 1024 * 1024 * 1024 then return format ( "%.2f MB" , size / ( 1024 * 1024 )) end 156 157return format ( "%.2f GB" , size / ( 1024 * 1024 * 1024 )) 158end 159 160function string . upperFirst ( str ) 161return gsub ( str , "^%l" , upper ) 162end 163 164function string . addCommas ( str ) 165str =tostring ( str ) 166return reverse ( gsub ( gsub ( reverse ( str ), "(...)" , "%1," ), ",$" , "" )) 167end 168 169function string . niceNumber ( num ) 170return string . addCommas ( format ( "%0.0f" , num )) 171end 172 173function string . singularOrMuliple ( str , num ) 174local fmt =format ( "%i %s" , num , str ) 175return num == 1 and fmt or ( fmt .. "s" ) 176end 177 178function string . AOrAn ( s ) 179return match ( s , "^h?[AaEeIiOoUu]" ) and "an" or "a" 180end 181 182function string . secondsToHuman ( sec , accuracy ) 183 184local accuracy =accuracy or 2 185 186local years =sec / 31536000 187local yearsRemainder =sec % 31536000 188local days =yearsRemainder / 86400 189local daysRemainder =sec % 86400 190local hours =daysRemainder / 3600 191local hourRemainder =( sec - 86400 ) % 3600 192local min =hourRemainder / 60 193local sec =sec % 60 194 195years =floor ( years ) 196days =floor ( days ) 197hours =floor ( hours ) 198min =floor ( min ) 199sec =floor ( sec ) 200 201local results ={} 202 203if years >= 1 then 204insert ( results , string . singularOrMuliple ( "year" , years )) 205end 206if days >= 1 then 207insert ( results , string . singularOrMuliple ( "day" , days )) 208end 209if hours >= 1 then 210insert ( results , string . singularOrMuliple ( "hour" , hours )) 211end 212if min >= 1 then 213insert ( results , string . singularOrMuliple ( "minute" , min )) 214end 215if sec >= 1 then 216insert ( results , string . singularOrMuliple ( "second" , sec )) 217end 218 219local result ={} 220for i =1 , accuracy do 221result [ i ] =results [ i ] 222end 223 224return concat ( result , ", " ) 225end 226 227function string . escapeURL ( url ) 228return gsub ( url , "([^A-Za-z0-9_])" , function ( c ) 229return format ( "%%%02x" , byte ( c )) 230end ) 231end 232 233function string . enescapeURL ( url ) 234return gsub ( url , "%%(%x%x)" , function ( hex ) 235return char ( tonumber ( hex , 16 )) 236end ) 237end 238 239function string . fromhex ( str ) 240return ( str : gsub ( '..' , function ( cc ) 241return string . char ( tonumber ( cc , 16 )) 242end )) 243end 244 245function string . tohex ( str ) 246if not str or str == "" then return "00" end 247return str : gsub ( '.' , function ( c ) 248return string . format ( '%02X' , string . byte ( c )) 249end ) 250end