Search in sources :

Example 6 with FastMethod

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

Example 7 with FastMethod

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());
    }
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory) 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