use of php.runtime.annotation.Runtime.FastMethod in project jphp by jphp-compiler.
the class NumUtils method format.
@FastMethod
@Signature({ @Arg("number"), @Arg("pattern"), @Arg(value = "decSep", optional = @Optional(".")), @Arg(value = "groupSep", optional = @Optional(",")) })
public static Memory format(Environment env, Memory... args) {
try {
char decSep = args[2].toChar();
char groupSep = args[3].toChar();
DecimalFormat decimalFormat;
if (decSep == '.' && groupSep == ',')
decimalFormat = new DecimalFormat(args[1].toString(), DEFAULT_DECIMAL_FORMAT_SYMBOLS);
else {
DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();
formatSymbols.setZeroDigit('0');
formatSymbols.setDecimalSeparator(decSep);
formatSymbols.setGroupingSeparator(groupSep);
decimalFormat = new DecimalFormat(args[1].toString(), formatSymbols);
}
return new StringMemory(decimalFormat.format(args[0].toDouble()));
} catch (IllegalArgumentException e) {
return Memory.FALSE;
}
}
use of php.runtime.annotation.Runtime.FastMethod in project jphp by jphp-compiler.
the class StrUtils method encode.
@FastMethod
@Signature({ @Arg("string"), @Arg("charset") })
public static Memory encode(Environment env, Memory... args) {
Charset charset;
try {
charset = Charset.forName(args[1].toString());
} catch (Exception e) {
return Memory.FALSE;
}
ByteBuffer buffer = charset.encode(args[0].toString());
return new BinaryMemory(Arrays.copyOf(buffer.array(), buffer.limit()));
}
use of php.runtime.annotation.Runtime.FastMethod in project jphp by jphp-compiler.
the class WrapTimeZone method UTC.
@FastMethod
@Signature
public static Memory UTC(final Environment env, Memory... args) {
Memory r = env.getUserValue(WrapTimeZone.class.getName() + "#UTC", Memory.class);
if (r == null) {
r = new ObjectMemory(new WrapTimeZone(env, UTC));
env.setUserValue(WrapTimeZone.class.getName() + "#UTC", r);
}
return r;
}
use of php.runtime.annotation.Runtime.FastMethod in project jphp by jphp-compiler.
the class WrapFlow method ofStream.
@FastMethod
@Signature({ @Arg(value = "stream", typeClass = Stream.CLASS_NAME), @Arg(value = "chunkSize", optional = @Optional("1")) })
public static Memory ofStream(final Environment env, Memory... args) {
final Stream stream = args[0].toObject(Stream.class);
final int chunkSize = args[1].toInteger() < 1 ? 1 : args[1].toInteger();
return new ObjectMemory(new WrapFlow(env, new ForeachIterator(false, false, false) {
protected boolean eof() {
env.pushCall(stream, "eof");
try {
return stream.eof(env).toBoolean();
} finally {
env.popCall();
}
}
@Override
protected boolean init() {
currentKey = Memory.CONST_INT_M1;
return !eof();
}
@Override
protected boolean nextValue() {
if (eof())
return false;
env.pushCall(stream, "read", LongMemory.valueOf(chunkSize));
try {
currentValue = stream.read(env, LongMemory.valueOf(chunkSize));
currentKey = ((LongMemory) currentKey).inc();
} catch (IOException e) {
env.catchUncaught(e);
} finally {
env.popCall();
}
return true;
}
@Override
protected boolean prevValue() {
return false;
}
@Override
public void reset() {
currentKey = Memory.CONST_INT_M1;
env.pushCall(stream, "seek", Memory.CONST_INT_0);
try {
stream.seek(env, Memory.CONST_INT_0);
} catch (IOException e) {
env.catchUncaught(e);
} finally {
env.popCall();
}
}
}));
}
use of php.runtime.annotation.Runtime.FastMethod in project jphp by jphp-compiler.
the class BinUtils method of.
@FastMethod
@Signature(@Arg("value"))
public static Memory of(Environment env, Memory... args) {
if (ParameterEntity.checkTypeHinting(env, args[0], HintType.TRAVERSABLE)) {
ForeachIterator iterator = args[0].getNewIterator(env, false, false);
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
while (iterator.next()) {
tmp.write(iterator.getValue().toInteger());
}
return new BinaryMemory(tmp.toByteArray());
}
return new BinaryMemory(args[0].getBinaryBytes(env.getDefaultCharset()));
}
Aggregations