use of org.luaj.vm2.Varargs in project LuaViewSDK by alibaba.
the class StringLib method gsub.
/**
* string.gsub (s, pattern, repl [, n])
* Returns a copy of s in which all (or the first n, if given) occurrences of the
* pattern have been replaced by a replacement string specified by repl, which
* may be a string, a table, or a function. gsub also returns, as its second value,
* the total number of matches that occurred.
*
* If repl is a string, then its value is used for replacement.
* The character % works as an escape character: any sequence in repl of the form %n,
* with n between 1 and 9, stands for the value of the n-th captured substring (see below).
* The sequence %0 stands for the whole match. The sequence %% stands for a single %.
*
* If repl is a table, then the table is queried for every match, using the first capture
* as the key; if the pattern specifies no captures, then the whole match is used as the key.
*
* If repl is a function, then this function is called every time a match occurs,
* with all captured substrings passed as arguments, in order; if the pattern specifies
* no captures, then the whole match is passed as a sole argument.
*
* If the value returned by the table query or by the function call is a string or a number,
* then it is used as the replacement string; otherwise, if it is false or nil,
* then there is no replacement (that is, the original match is kept in the string).
*
* Here are some examples:
* x = string.gsub("hello world", "(%w+)", "%1 %1")
* --> x="hello hello world world"
*
* x = string.gsub("hello world", "%w+", "%0 %0", 1)
* --> x="hello hello world"
*
* x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
* --> x="world hello Lua from"
*
* x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
* --> x="home = /home/roberto, user = roberto"
*
* x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
* return loadstring(s)()
* end)
* --> x="4+5 = 9"
*
* local t = {name="lua", version="5.1"}
* x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
* --> x="lua-5.1.tar.gz"
*/
static Varargs gsub(Varargs args) {
LuaString src = args.checkstring(1);
final int srclen = src.length();
LuaString p = args.checkstring(2);
LuaValue repl = args.arg(3);
int max_s = args.optint(4, srclen + 1);
final boolean anchor = p.length() > 0 && p.charAt(0) == '^';
Buffer lbuf = new Buffer(srclen);
MatchState ms = new MatchState(args, src, p);
int soffset = 0;
int n = 0;
while (n < max_s) {
ms.reset();
int res = ms.match(soffset, anchor ? 1 : 0);
if (res != -1) {
n++;
ms.add_value(lbuf, soffset, res, repl);
}
if (res != -1 && res > soffset)
soffset = res;
else if (soffset < srclen)
lbuf.append((byte) src.luaByte(soffset++));
else
break;
if (anchor)
break;
}
lbuf.append(src.substring(soffset, srclen));
return varargsOf(lbuf.tostring(), valueOf(n));
}
use of org.luaj.vm2.Varargs in project LuaViewSDK by alibaba.
the class UDBaseListOrRecyclerView method callCellSize.
/**
* return width, height
*
* @param cell
* @param position
* @return
*/
public int[] callCellSize(LuaValue cell, int position, int... defaultSize) {
final Varargs size = invokeCellSize(cell, position);
int defaultWidth = (defaultSize != null && defaultSize.length > 1) ? defaultSize[0] : AndroidUtil.getScreenWidth(getContext());
int width = 0, height = 0;
if (size != null) {
if (size.narg() > 1) {
// width & height
width = DimenUtil.dpiToPx(size.arg(1), defaultWidth);
height = DimenUtil.dpiToPx(size.arg(2));
} else {
width = defaultWidth;
height = DimenUtil.dpiToPx(size.arg(1));
}
}
return new int[] { width, height };
}
use of org.luaj.vm2.Varargs in project LuaViewSDK by alibaba.
the class UDBaseRecyclerView method getId.
/**
* 由于该函数的特殊性,有则获取无则生成。
* 对于生成,有Pinned.YES标记的Id,会先加后缀(".PINNED"+position),再存放到mIdCache中;
* 对于获取,用mPinnedPositionCellId缓存的Lua定义的真实的Id。
*
* @param position
* @param section
* @param row
* @return
*/
@Override
protected String getId(int position, int section, int row) {
final String cacheId = mIdCache != null ? mIdCache.get(position) : null;
if (cacheId != null) {
if (this.mIsPinnedSparseArray.get(position)) {
// 获取CellId的时候,要用lua定义的真正的Id
return mPinnedPositionCellId.get(position);
}
return cacheId;
} else {
String id = null;
Varargs args = LuaUtil.invokeFunction(mCellDelegate.get("Id"), LuaUtil.toLuaInt(section), LuaUtil.toLuaInt(row));
if (args != null) {
if (args.narg() > 1) {
if (args.arg(2).toint() == UDPinned.PINNED_YES) {
mHasPinnedCell = true;
mIsPinnedSparseArray.put(position, true);
id = args.arg(1).optjstring("");
/**
* 构造唯一的id,使得在lua用同一种Cell作为多个position的PinnedCell时,也会有不同的viewType.
* 见 {@link UDBaseRecyclerView#getItemViewType(int)}
*/
mPinnedPositionCellId.put(position, id);
id = new StringBuffer(id).append(".PINNED").append(position).toString();
} else {
id = args.arg(1).optjstring("");
}
} else {
// 兼容旧版本的写法,只有一个String参数的情况
id = ((LuaValue) args).optjstring("");
}
}
if (mIdCache != null) {
mIdCache.put(position, id);
}
return id;
}
}
use of org.luaj.vm2.Varargs in project LuaViewSDK by alibaba.
the class UDSpannableString method init.
/**
* 初始化数据
*/
public void init(Varargs initParams) {
LuaValue text = NIL, config = NIL;
if (initParams != null) {
text = getInitParam1();
config = getInitParam2();
}
initSpannableStringBuilder(text, config);
}
use of org.luaj.vm2.Varargs in project LuaViewSDK by alibaba.
the class UIViewMethodMapper method matrix.
@LuaViewApi(since = SdkVersion.V_051)
public LuaValue matrix(U view, Varargs varargs) {
if (varargs.narg() > 1) {
if (varargs.istable(2)) {
LuaTable table = LuaUtil.getTable(varargs, 2);
int n = table.length();
if (n > 9) {
float[] values = new float[9];
for (int i = 0; i < 9; i++) {
values[i] = LuaUtil.getFloat(table, 0F, i + 2);
}
return view.setMatrix(values);
} else if (n > 6) {
float[] values = new float[9];
for (int i = 0; i < 6; i++) {
values[i] = LuaUtil.getFloat(table, 0F, i + 2);
}
values[6] = 0;
values[7] = 0;
values[8] = 1;
return view.setMatrix(values);
}
} else {
int n = varargs.narg();
if (n > 9) {
float[] values = new float[9];
for (int i = 0; i < 9; i++) {
values[i] = LuaUtil.getFloat(varargs, 0F, i + 2);
}
return view.setMatrix(values);
} else if (n > 6) {
float[] values = new float[9];
for (int i = 0; i < 6; i++) {
values[i] = LuaUtil.getFloat(varargs, 0F, i + 2);
}
values[6] = 0;
values[7] = 0;
values[8] = 1;
return view.setMatrix(values);
}
}
} else {
float[] values = view.getMatrix();
if (values != null) {
LuaTable table = new LuaTable();
for (int i = 0; i < 6; i++) {
table.set(i + 1, valueOf(values[i]));
}
return table;
}
}
return view;
}
Aggregations