Search in sources :

Example 1 with FastMethod

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;
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) StringMemory(php.runtime.memory.StringMemory) FastMethod(php.runtime.annotation.Runtime.FastMethod)

Example 2 with FastMethod

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()));
}
Also used : Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) FastMethod(php.runtime.annotation.Runtime.FastMethod)

Example 3 with FastMethod

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;
}
Also used : ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) FastMethod(php.runtime.annotation.Runtime.FastMethod)

Example 4 with FastMethod

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();
            }
        }
    }));
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Stream(php.runtime.ext.core.classes.stream.Stream) IOException(java.io.IOException) FastMethod(php.runtime.annotation.Runtime.FastMethod)

Example 5 with FastMethod

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()));
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) BinaryMemory(php.runtime.memory.BinaryMemory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FastMethod(php.runtime.annotation.Runtime.FastMethod)

Aggregations

FastMethod (php.runtime.annotation.Runtime.FastMethod)7 IOException (java.io.IOException)3 ForeachIterator (php.runtime.lang.ForeachIterator)3 FileNotFoundException (java.io.FileNotFoundException)2 Charset (java.nio.charset.Charset)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Memory (php.runtime.Memory)2 StringMemory (php.runtime.memory.StringMemory)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 CharBuffer (java.nio.CharBuffer)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 Stream (php.runtime.ext.core.classes.stream.Stream)1 ArrayMemory (php.runtime.memory.ArrayMemory)1 BinaryMemory (php.runtime.memory.BinaryMemory)1 LongMemory (php.runtime.memory.LongMemory)1 ObjectMemory (php.runtime.memory.ObjectMemory)1