整理代码,新增string扩展模块,把一些小模块合并进string和table扩展模块中
This commit is contained in:
@@ -40,7 +40,7 @@ return function(L,t)
|
||||
T=type(v)
|
||||
if T=="number"then v=tostring(v)
|
||||
elseif T=="string"then v="\""..v.."\""
|
||||
elseif T=="table"then v=DUMPTABLE(v,t+1)
|
||||
elseif T=="table"then v=TABLE.dump(v,t+1)
|
||||
elseif T=="boolean"then v=tostring(v)
|
||||
else error("Error data type!")
|
||||
end
|
||||
|
||||
@@ -28,7 +28,7 @@ function FILE.save(data,name,mode)
|
||||
if not mode then mode=""end
|
||||
if type(data)=="table"then
|
||||
if mode:find("l")then
|
||||
data=DUMPTABLE(data)
|
||||
data=TABLE.dump(data)
|
||||
if not data then
|
||||
LOG.print(name.." "..text.saveError.."dump error","error")
|
||||
return
|
||||
|
||||
@@ -13,14 +13,10 @@ MDRAW=require"Zframework.mDraw"
|
||||
mText=MDRAW.simpX
|
||||
mDraw=MDRAW.draw
|
||||
|
||||
-- UPPERCHAR=require"Zframework.upperChar"
|
||||
JSON=require"Zframework.json"
|
||||
DUMPTABLE=require"Zframework.dumpTable"
|
||||
URLENCODE=require"Zframework.urlEncode"
|
||||
|
||||
TABLE=require"Zframework.tableExtend"
|
||||
SPLITSTR=require"Zframework.splitStr"
|
||||
TIMESTR=require"Zframework.timeStr"
|
||||
STRING=require"Zframework.stringExtend"
|
||||
|
||||
VIB= require"Zframework.vibrate"
|
||||
SFX= require"Zframework.sfx"
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
local find,sub=string.find,string.sub
|
||||
return function(s,sep,regex)
|
||||
local L={}
|
||||
local p1,p2=1--start,target
|
||||
if regex then
|
||||
while p1<=#s do
|
||||
p2=find(s,sep,p1)or #s+1
|
||||
L[#L+1]=sub(s,p1,p2-1)
|
||||
p1=p2+#sep
|
||||
end
|
||||
else
|
||||
while p1<=#s do
|
||||
p2=find(s,sep,p1,true)or #s+1
|
||||
L[#L+1]=sub(s,p1,p2-1)
|
||||
p1=p2+#sep
|
||||
end
|
||||
end
|
||||
return L
|
||||
end
|
||||
77
Zframework/stringExtend.lua
Normal file
77
Zframework/stringExtend.lua
Normal file
@@ -0,0 +1,77 @@
|
||||
local STRING={}
|
||||
local int,format=math.floor,string.format
|
||||
local find,sub,upper=string.find,string.sub,string.upper
|
||||
|
||||
do--function STRING.shiftChar(c)
|
||||
local shiftMap={
|
||||
["1"]="!",["2"]="@",["3"]="#",["4"]="$",["5"]="%",
|
||||
["6"]="^",["7"]="&",["8"]="*",["9"]="(",["0"]=")",
|
||||
["`"]="~",["-"]="_",["="]="+",
|
||||
["["]="{",["]"]="}",["\\"]="|",
|
||||
[";"]=":",["'"]="\"",
|
||||
[","]="<",["."]=">",["/"]="?",
|
||||
}
|
||||
function STRING.shiftChar(c)
|
||||
return shiftMap[c]or upper(c)
|
||||
end
|
||||
end
|
||||
|
||||
function STRING.split(s,sep,regex)
|
||||
local L={}
|
||||
local p1,p2=1--start,target
|
||||
if regex then
|
||||
while p1<=#s do
|
||||
p2=find(s,sep,p1)or #s+1
|
||||
L[#L+1]=sub(s,p1,p2-1)
|
||||
p1=p2+#sep
|
||||
end
|
||||
else
|
||||
while p1<=#s do
|
||||
p2=find(s,sep,p1,true)or #s+1
|
||||
L[#L+1]=sub(s,p1,p2-1)
|
||||
p1=p2+#sep
|
||||
end
|
||||
end
|
||||
return L
|
||||
end
|
||||
|
||||
function STRING.simpEmailCheck(e)
|
||||
e=STRING.split(e,"@")
|
||||
if #e~=2 then return false end
|
||||
if e[1]:sub(-1)=="."or e[2]:sub(-1)=="."then return false end
|
||||
local e1,e2=STRING.split(e[1],"."),STRING.split(e[2],".")
|
||||
if #e1*#e2==0 then return false end
|
||||
for _,v in next,e1 do if #v==0 then return false end end
|
||||
for _,v in next,e2 do if #v==0 then return false end end
|
||||
return true
|
||||
end
|
||||
|
||||
function STRING.time(s)
|
||||
if s<60 then
|
||||
return format("%.3f\"",s)
|
||||
elseif s<3600 then
|
||||
return format("%d'%05.2f\"",int(s/60),s%60)
|
||||
else
|
||||
local h=int(s/3600)
|
||||
return format("%d:%.2d'%05.2f\"",h,int(s/60%60),s%60)
|
||||
end
|
||||
end
|
||||
|
||||
do--function STRING.urlEncode(str)
|
||||
local rshift=bit.rshift
|
||||
local b16={[0]="0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}
|
||||
function STRING.urlEncode(str)
|
||||
local out=""
|
||||
for i=1,#str do
|
||||
if str:sub(i,i):match("[a-zA-Z0-9]")then
|
||||
out=out..str:sub(i,i)
|
||||
else
|
||||
local b=str:byte(i)
|
||||
out=out.."%"..b16[rshift(b,4)]..b16[b%16]
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
end
|
||||
|
||||
return STRING
|
||||
@@ -73,4 +73,60 @@ function TABLE.reIndex(org)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--Dump a simple lua table
|
||||
do--function TABLE.dump(L,t)
|
||||
local find=string.find
|
||||
local tabs={
|
||||
[0]="",
|
||||
"\t",
|
||||
"\t\t",
|
||||
"\t\t\t",
|
||||
"\t\t\t\t",
|
||||
"\t\t\t\t\t",
|
||||
}
|
||||
function dump(L,t)
|
||||
local s
|
||||
if t then
|
||||
s="{\n"
|
||||
else
|
||||
s="return{\n"
|
||||
t=1
|
||||
if type(L)~="table"then
|
||||
return
|
||||
end
|
||||
end
|
||||
local count=1
|
||||
for k,v in next,L do
|
||||
local T=type(k)
|
||||
if T=="number"then
|
||||
if k==count then
|
||||
k=""
|
||||
count=count+1
|
||||
else
|
||||
k="["..k.."]="
|
||||
end
|
||||
elseif T=="string"then
|
||||
if find(k,"[^0-9a-zA-Z_]")then
|
||||
k="[\""..k.."\"]="
|
||||
else
|
||||
k=k.."="
|
||||
end
|
||||
elseif T=="boolean"then k="["..k.."]="
|
||||
else error("Error key type!")
|
||||
end
|
||||
T=type(v)
|
||||
if T=="number"then v=tostring(v)
|
||||
elseif T=="string"then v="\""..v.."\""
|
||||
elseif T=="table"then v=dump(v,t+1)
|
||||
elseif T=="boolean"then v=tostring(v)
|
||||
else error("Error data type!")
|
||||
end
|
||||
s=s..tabs[t]..k..v..",\n"
|
||||
end
|
||||
return s..tabs[t-1].."}"
|
||||
end
|
||||
TABLE.dump=dump
|
||||
end
|
||||
|
||||
return TABLE
|
||||
@@ -1,11 +0,0 @@
|
||||
local int,format=math.floor,string.format
|
||||
return function(s)
|
||||
if s<60 then
|
||||
return format("%.3f\"",s)
|
||||
elseif s<3600 then
|
||||
return format("%d'%05.2f\"",int(s/60),s%60)
|
||||
else
|
||||
local h=int(s/3600)
|
||||
return format("%d:%.2d'%05.2f\"",h,int(s/60%60),s%60)
|
||||
end
|
||||
end
|
||||
@@ -1,12 +0,0 @@
|
||||
local upper=string.upper
|
||||
local upperList={
|
||||
["1"]="!",["2"]="@",["3"]="#",["4"]="$",["5"]="%",
|
||||
["6"]="^",["7"]="&",["8"]="*",["9"]="(",["0"]=")",
|
||||
["`"]="~",["-"]="_",["="]="+",
|
||||
["["]="{",["]"]="}",["\\"]="|",
|
||||
[";"]=":",["'"]="\"",
|
||||
[","]="<",["."]=">",["/"]="?",
|
||||
}
|
||||
return function(c)
|
||||
return upperList[c]or upper(c)
|
||||
end
|
||||
@@ -1,14 +0,0 @@
|
||||
local rshift=bit.rshift
|
||||
local b16={[0]="0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}
|
||||
return function(str)
|
||||
local out=""
|
||||
for i=1,#str do
|
||||
if str:sub(i,i):match("[a-zA-Z0-9]")then
|
||||
out=out..str:sub(i,i)
|
||||
else
|
||||
local b=str:byte(i)
|
||||
out=out.."%"..b16[rshift(b,4)]..b16[b%16]
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
Reference in New Issue
Block a user