|
-
- function toboolean(str)
- return str == "1" or str == "true" or str == 1 or str == true;
- end
- -- 把字符串转换成数组,并用itemConvertFunc转换所有元素
- function toArray(szSeparator , itemConvertFunc)
- return function(str)
- local strs = string.split(str , szSeparator)
- if itemConvertFunc then
- local items = {}
- for i , v in ipairs(strs) do
- table.insert(items , itemConvertFunc(v))
- end
- return items;
- else
- return strs;
- end
- end
- end
-
-
- function createEvalFunction(evalFunction , exp)
- local evalTable = {}
- evalTable.__call = function(self , ...)return evalFunction(...)end;
- evalTable.__index = evalTable;
- evalTable.exp = exp;
- setmetatable(evalTable , evalTable);
- return evalTable;
- end
-
- -- 把公式字符串转换成返回整数的函数, 结果是小数的情况就用向上取整
- function toInterEval(str)
- local function _tmp(...)
- local arg = {...}
- local env = {};
- local index = 1;
-
- for k, v in pairs(arg) do
- env["x" .. index] = v;
- index = index + 1;
- end
-
- return evalServerScript(str ,env)
- end
-
- local function retFunc(...)
- local value = _tmp(...);
- --return math.ceil(value);
- -- 是否取整由使用者决定吧
- return value;
- end
-
- return createEvalFunction(retFunc , str);
- end
-
- -- 把公式字符串转换成lua函数
- function toEval(str)
- local function retFunc(...)
- local arg = {...}
- local env = {};
- local index = 1;
-
- for k, v in pairs(arg) do
- env["x" .. index] = v;
- index = index + 1;
- end
-
- return evalServerScript(str ,env)
- end
- return createEvalFunction(retFunc , str);
- end;
-
-
- -- 把字符串转换成数值数组
- function _toMultiArray(startIndex , ...)
- local arg = {...}
- if #arg == startIndex then
- local desc = arg[startIndex];
- return toArray(desc.sep , desc.converter);
- else
- local function run(str)
- local desc = arg[startIndex];
- local item = string.split(str , desc.sep)
- local index = startIndex + 1;
- for ii , vv in ipairs(item) do
- item[ii] = _toMultiArray(index , unpack(arg))(vv);
- end
-
- return item;
- end
- return run;
- end
- end
-
- -- 把字符串转换成多维数组
- function toStringArray(...)
- local arg = {...}
- local numArg = {};
- for i , v in ipairs(arg) do
- numArg[i] = {sep = v , converter = nil};
- end
- return _toMultiArray(1 , unpack(numArg));
- end
-
- -- 把字符串转换成数值数组
- function toNumberArray(...)
- local arg = {...}
- local numArg = {};
- for i , v in ipairs(arg) do
- numArg[i] = {sep = v , converter = tonumber};
- end
- return _toMultiArray(1 , unpack(numArg));
- end
-
- -- 把字符串转换成数值数组
- function autoMultiArray(str)
- local run = nil;
- local sing_0 = string.find(str, "_");
- local sing_1 = string.find(str, "|");
- local sing_2 = string.find(str, ",");
- if sing_0 and sing_1 and sing_2 then
- run = toNumberArray(",", "|", "_");
- elseif sing_0 and sing_1 then
- run = toNumberArray("|", "_");
- elseif sing_0 then
- run = toNumberArray("_");
- end
- if run then
- local array = {}; --newBindableArray();
- local t = run(str);
- for i, v in pairs(t) do
- array[i] = v;
- end
- return array;
- else
- return str;
- end
- end
-
-
- -- 把字符串转换成数值数组
- function autoMultiArray2(str)
- local run = nil;
- local sing_0 = string.find(str, ",");
- local sing_1 = string.find(str, ":");
- local sing_2 = string.find(str, "_");
- if sing_0 and sing_1 and sing_2 then
- run = toNumberArray(",", ":", "_");
- elseif sing_0 and sing_1 then
- run = toNumberArray(",", ":");
- elseif sing_2 then
- run = toNumberArray("_");
- end
- if run then
- local array = {}; --newBindableArray();
- local t = run(str);
- for i, v in pairs(t) do
- array[i] = v;
- end
- return array;
- else
- return str;
- end
- end
-
- function autoAllMultiArray(str)
- local type = string.find(str, ":");
- if type then
- return autoMultiArray2(str);
- else
- return autoMultiArray(str);
- end
- end
-
- --str是这种格式的 2016-11-1 23:00:00
- function stringToTimeStamp(str)
- local data = string.split(str , " ");
- local timeInfo = string.split(data[2] , ":");
- local dateInfo = string.split(data[1] , "-");
- local time = {};
- time.year = tonumber(dateInfo[1]);
- time.month = tonumber(dateInfo[2]);
- time.day = tonumber(dateInfo[3]);
- time.hour = tonumber(timeInfo[1]);
- time.min = tonumber(timeInfo[2]);
- time.sec = tonumber(timeInfo[3]);
-
- return BeijingTime.timeFunc(time);
- end
-
-
-
- function toNumber(str)
- return tonumber(str) or 0
- end
-
- -- 把255,255,255格式的字符串转换成cc.Color
- function toColor3B(colorString)
- local numberArray = toNumberArray(",")(colorString)
- if #numberArray ~= 3 then
- return nil
- else
- local color = ccColor3B();
- color.r = numberArray[1];
- color.g = numberArray[2];
- color.b = numberArray[3];
- return color;
- end
- end
-
-
-
- -- 把多重参数转换成表返回
- local function packMultiArgToTable(...)
- return {...}
- end
-
- -- 使用lua模式匹配把一个字符串转换成数组
- function toArrayPattern(pattern)
- return function(str)
- local result = packMultiArgToTable(string.find(str,pattern));
- if result[1] == nil then
- error("模式匹配失败:" , pattern);
- return;
- else
- return result;
- end
- end
-
- end
-
- -- XML翻译器
- TextTranslator = {};
- -- 收集所有可以翻译的文本,返回string正则表达式
- function TextTranslator:collect(sourceString)
- print("收集一般文本" , sourceString);
- local text = string.trim(sourceString);
-
- local index = 1;
- local function getReplace(s)
- local ret = "[ctln" .. index .. "]";
- index = index + 1;
- return ret;
- end
- text = string.gsub(text , "[0-9]+" , getReplace);
- return text;
- end
-
- -- 翻译文本
- -- sourceString 源文本
- -- translated 第三方使用collect翻译后的文本
- -- @return 返回原文翻译后的文本
- function TextTranslator:translate(sourceString , translated)
- local index = 1;
- local function getReplace(s)
- local num = "%[ctln" .. index .. "%]";
- index = index + 1;
-
- translated = string.gsub(translated , num , s);
- return s;
- end
- string.gsub(sourceString , "[0-9]+" , getReplace);
- return translated;
- end
-
- -- XML翻译器
- XmlTranslator = {};
- -- 收集所有可以翻译的文本,返回string正则表达式
- function XmlTranslator:collect(sourceString)
- local function onStartElement(name , atts)
- end
- local function onEndElement(name , atts)
- end
-
- local numIndex = 1;
- local tagIndex = 1;
- local texts = {};
- local function getNumReplace(s)
- local ret = "[ctln" .. numIndex .. "]";
- numIndex = numIndex + 1;
- return ret;
- end
- local function onText(text)
- local re = string.trim(text);
- if re ~= "" then
- re = string.gsub(re , "[0-9]+" , getNumReplace);
- table.insert(texts , re);
- table.insert(texts , "[ctlt" .. tostring(tagIndex) .. "]");
- tagIndex = tagIndex + 1;
- end
- end
- local success , err = tiny.eval("<font>" .. sourceString .. "</font>" , onStartElement , onEndElement , onText)
- if not success then
- return TextTranslator:collect(sourceString);
- end
-
- if #texts > 1 then
- texts[#texts] = nil;
- end
- return table.concat(texts);
- end
-
- -- 翻译文本
- -- sourceString 源文本
- -- translated 第三方使用collect翻译后的文本
- -- @return 返回原文翻译后的文本
- function XmlTranslator:translate(sourceString , translated)
- local function onStartElement(name , atts)
- end
- local function onEndElement(name , atts)
- end
-
- local xmlText = "<font>" .. sourceString .. "</font>";
-
- local lastParseIndex = 1;
- local nums = {};
- local tags = {};
-
- local function onText(text , currentByteIndex)
-
- local re = string.trim(text);
- if re ~= "" then
-
- local ltrimLen = #text - #string.ltrim(text);
- local function getNumReplace(s)
- table.insert(nums , s);
- return s;
- end
- string.gsub(re , "[0-9]+" , getNumReplace);
-
- table.insert(tags , string.sub(xmlText , lastParseIndex , currentByteIndex + ltrimLen));
- lastParseIndex = currentByteIndex + ltrimLen + string.len(re) + 1;
- end
- end
-
- -- 解析xml
- local success , err = tiny.eval(xmlText , onStartElement , onEndElement , onText)
- if not success then
- return TextTranslator:translate(sourceString , translated);
- end
- -- 尾字符串
- if lastParseIndex <= #xmlText - string.len("</font>") then
- table.insert(tags , string.sub(xmlText , lastParseIndex , #xmlText - string.len("</font>")));
- end
-
- local count = #tags;
-
- -- 替换数字
- for i , v in ipairs(nums) do
- local num = "%[ctln" .. tostring(i) .. "%]";
- translated = string.gsub(translated , num , v);
- end
-
-
- -- 替换控制字符串,去头去尾
- for i = 1 , #tags - 2 do
- local tag = "%[ctlt" .. tostring(i) .. "%]";
- local v = string.gsub(tags[i + 1] , "%%" , "%%%%");
- translated = string.gsub(translated , tag , v);
- end
-
- -- 头
- if #tags > 0 then
- translated = string.sub(tags[1] , string.len("<font>") + 1) .. translated;
- end
-
- -- 尾
- if #tags > 1 then
- translated = translated .. tags[#tags];
- end
- return translated;
- end
|