KoboldAI-Client/extern/lualibs/pl/text.lua

7 lines
3.7 KiB
Lua
Raw Normal View History

-- Penlight 1.11.0-1 | /lua/pl/text.lua | https://github.com/lunarmodules/Penlight | License: MIT | Minified using https://www.npmjs.com/package/luamin/v/1.0.4
-- Copyright (C) 2009-2016 Steve Donovan, David Manura.
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
local a=string.gsub;local b,c=table.concat,table.insert;local d=require'pl.utils'local e,f,g=d.bind1,d.split,d.assert_arg;local h=require'pl.types'.is_callable;local i=d.unpack;local j={}local function k(l)return setmetatable(l,require('pl.List'))end;local function m(n)return n:gsub('^%s+','')end;local function o(n)return m(n):gsub('%s+$','')end;local function p(q,r)return k(f(q,r))end;local function s(t,u,...)local v={}for w=1,#u do v[w]=t(u[w],...)end;return v end;local function x(q,y)local z=p(q,'\n')return b(s(e('..',y),z),'\n')..'\n'end;function j.indent(q,A,B)g(1,q,'string')g(2,A,'number')return x(q,string.rep(B or' ',A))end;function j.dedent(q)g(1,q,'string')local z=p(q,'\n')local C,D=(#z>0 and z[1]or''):find('^%s*')z=s(string.sub,z,D+1)return b(z,'\n')..'\n'end;function j.wrap(q,E)g(1,q,'string')E=E or 70;q=q:gsub('\n',' ')local w,F=1;local G,H={}while w<#q do F=w+E;if q:find("[%w']",F)then F=q:find('%W',F+1)end;H=q:sub(w,F)w=w+#H;c(G,o(H))end;return k(G)end;function j.fill(q,E)return b(j.wrap(q,E),'\n')..'\n'end;local I={}j.Template=I;I.__index=I;setmetatable(I,{__call=function(J,K)return I.new(K)end})function I.new(K)g(1,K,'string')local v={}v.tmpl=K;setmetatable(v,I)return v end;local function L(q,M,N)local O;if h(M)then O=M else function O(t)local q=M[t]if not q then if N then return t else error("not present in table "..t)end else return q end end end;local v=a(q,'%${([%w_]+)}',O)return a(v,'%$([%w_]+)',O)end;function I:substitute(M)g(1,M,'table')return L(self.tmpl,M,false)end;function I:safe_substitute(M)g(1,M,'table')return L(self.tmpl,M,true)end;function I:indent_substitute(M)g(1,M,'table')if not self.strings then self.strings=p(self.tmpl,'\n')end;local function O(H)return H:gsub('(%s*)%$([%w_]+)',function(y,t)local P;local q=M[t]if not q then error("not present in table "..t)end;if getmetatable(q)==I then P=q;q=q.tmpl else q=tostring(q)end;if q:find'\n'then q=x(q,y)end;if P then return L(q,M)else return q end end)end;local G=s(O,self.strings)return b(G,'\n')..'\n'end;function j.format_operator()local Q=string.format;local function R(S,...)local T={...}local w=1;for U in S:gmatch('%%.')do if U=='%s'and type(T[w])~='string'then T[w]=tostring(T[w])end;w=w+1 end;return Q(S,i(T))end;local function V(q,u)return q:gsub('%$([%w_]+)',u)end;getmetatable("").__mod=function(W,X)if X==nil then return W elseif type(X)=="table"and getmetatable(X)==nil then if#X==0 then return L(W,X,true)else return R(W,i(X))end elseif type(X)=='function'then return V(W,X)else return R(W,X)end end end;return j