use of php.runtime.annotation.Runtime.FastMethod in project jphp by jphp-compiler.
the class StrUtils method decode.
@FastMethod
@Signature({ @Arg("string"), @Arg("charset") })
public static Memory decode(Environment env, Memory... args) {
Charset charset;
try {
charset = Charset.forName(args[1].toString());
} catch (Exception e) {
return Memory.FALSE;
}
CharBuffer charBuffer = charset.decode(ByteBuffer.wrap(args[0].getBinaryBytes(env.getDefaultCharset())));
return StringMemory.valueOf(charBuffer.toString());
}
use of php.runtime.annotation.Runtime.FastMethod in project jphp by jphp-compiler.
the class StrUtils method join.
@FastMethod
@Signature({ @Arg("collection"), @Arg("separator"), @Arg(value = "limit", optional = @Optional(value = "0", type = HintType.INT)) })
public static Memory join(Environment env, Memory... args) {
String separator = args[1].toString();
StringBuilder builder = new StringBuilder();
int limit = args[2].toInteger();
int i = 0;
if (args[0].isArray()) {
ArrayMemory array = args[0].toValue(ArrayMemory.class);
int size = array.size();
if (limit > 0 && limit < size)
size = limit;
for (Memory el : array) {
builder.append(el);
if (i != size - 1)
builder.append(separator);
i++;
if (i == size)
break;
}
return new StringMemory(builder.toString());
} else {
ParameterEntity.validateTypeHinting(env, 1, args, HintType.TRAVERSABLE, false);
ForeachIterator iterator = args[0].getNewIterator(env);
while (iterator.next()) {
builder.append(iterator.getValue());
builder.append(separator);
i++;
if (limit > 0 && i == limit)
break;
}
int length = builder.length();
if (length > 0) {
builder.delete(length - separator.length(), length);
} else
return Memory.CONST_EMPTY_STRING;
return new StringMemory(builder.toString());
}
}
Aggregations