Search in sources :

Example 1 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class StringFunctions method _str_replace.

protected static Memory _str_replace(Environment env, TraceInfo trace, Memory search, Memory replace, Memory string, @Reference Memory count, boolean isInsensitive) {
    if (count.isReference()) {
        count.assign(0);
    }
    if (string.isNull()) {
        return Memory.CONST_EMPTY_STRING;
    }
    if (string.isArray()) {
        ForeachIterator iterator = string.getNewIterator(env);
        ArrayMemory result = new ArrayMemory();
        while (iterator.next()) {
            Memory key = iterator.getMemoryKey();
            Memory value = iterator.getValue();
            if (value.isArray()) {
                result.refOfIndex(key).assign(value.toImmutable());
            } else {
                Memory ret = _str_replace(env, trace, search, replace, StringMemory.valueOf(value.toString()), count, isInsensitive);
                result.refOfIndex(key).assign(ret);
            }
        }
        return result.toConstant();
    } else {
        if (!search.isArray()) {
            String searchStr = search.toString();
            if (searchStr.isEmpty()) {
                return string;
            }
            if (replace.isArray()) {
                env.warning(trace, "str_replace(): Array to string conversion");
            }
            string = _str_replace_impl(env, trace, StringMemory.valueOf(searchStr), StringMemory.valueOf(replace.toString()), string, count, isInsensitive);
        } else if (replace.isArray()) {
            ForeachIterator searchIterator = search.getNewIterator(env);
            ForeachIterator replaceIterator = replace.getNewIterator(env);
            while (searchIterator.next()) {
                Memory searchValue = searchIterator.getValue();
                Memory replaceValue;
                if (replaceIterator.next()) {
                    replaceValue = replaceIterator.getValue();
                } else {
                    replaceValue = Memory.NULL;
                }
                string = _str_replace(env, trace, StringMemory.valueOf(searchValue.toString()), StringMemory.valueOf(replaceValue.toString()), string, count, isInsensitive);
            }
        } else {
            ForeachIterator searchIterator = search.getNewIterator(env);
            while (searchIterator.next()) {
                string = _str_replace(env, trace, StringMemory.valueOf(searchIterator.getValue().toString()), replace, string, count, isInsensitive);
            }
        }
    }
    return string;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory)

Example 2 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ArrayFunctions method array_unique.

public static Memory array_unique(Environment env, TraceInfo trace, Memory array, int flag) {
    if (expecting(env, trace, 1, array, ARRAY)) {
        Set<Object> keys = new TreeSet<>();
        ArrayMemory newArray = new ArrayMemory();
        ForeachIterator iterator = array.getNewIterator(env);
        while (iterator.next()) {
            Object key;
            switch(flag) {
                case 1:
                    key = iterator.getValue().toLong();
                    break;
                case 2:
                    key = iterator.getValue().toBinaryString();
                    break;
                case 5:
                    key = iterator.getValue().toString();
                    break;
                case 0:
                default:
                    key = iterator.getValue().toString();
                    break;
            }
            if (keys.add(key)) {
                newArray.put(iterator.getKey(), iterator.getValue().toImmutable());
            }
        }
        return newArray.toConstant();
    }
    return Memory.NULL;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator)

Example 3 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ArrayFunctions method array_sum.

public static Memory array_sum(Environment env, TraceInfo trace, Memory input) {
    if (expecting(env, trace, 1, input, ARRAY)) {
        ForeachIterator iterator = input.getNewIterator(env, false, false);
        Memory result = Memory.CONST_INT_0;
        while (iterator.next()) {
            result = result.plus(iterator.getValue());
        }
        return result;
    } else
        return Memory.NULL;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) LongMemory(php.runtime.memory.LongMemory) KeyValueMemory(php.runtime.memory.KeyValueMemory)

Example 4 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ArrayFunctions method array_rand.

public static Memory array_rand(Environment env, TraceInfo trace, Memory input, int numReq) {
    if (!expecting(env, trace, 1, input, ARRAY))
        return Memory.NULL;
    ArrayMemory array = input.toValue(ArrayMemory.class);
    int size = array.size();
    if (size < numReq || numReq < 1) {
        env.warning(trace, "array_rand(): Second argument has to be between 1 and the number of elements in the array");
        return Memory.NULL;
    }
    int i;
    if (numReq == 1) {
        return array.getRandomElementKey(MathFunctions.RANDOM);
    } else {
        ForeachIterator iterator = input.getNewIterator(env, false, false);
        Set<Integer> rands = new HashSet<Integer>();
        for (i = 0; i < numReq; i++) {
            while (!rands.add(MathFunctions.rand(0, size - 1).toInteger())) ;
        }
        ArrayMemory result = new ArrayMemory();
        i = -1;
        while (iterator.next()) {
            i++;
            if (!rands.contains(i))
                continue;
            result.add(iterator.getMemoryKey());
            if (result.size() >= numReq)
                break;
        }
        return result.toConstant();
    }
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator)

Example 5 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ArrayFunctions method array_keys.

public static Memory array_keys(Environment env, TraceInfo trace, Memory input, Memory search, boolean strict) {
    if (!expecting(env, trace, 1, input, ARRAY))
        return Memory.NULL;
    ArrayMemory result = new ArrayMemory();
    ForeachIterator iterator = input.getNewIterator(env, false, false);
    while (iterator.next()) {
        if (search == null) {
            result.add(iterator.getMemoryKey());
        } else {
            if (strict && iterator.getValue().identical(search)) {
                result.add(iterator.getMemoryKey());
            } else if (iterator.getValue().equal(search)) {
                result.add(iterator.getMemoryKey());
            }
        }
    }
    return result.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator)

Aggregations

ForeachIterator (php.runtime.lang.ForeachIterator)100 ArrayMemory (php.runtime.memory.ArrayMemory)49 Memory (php.runtime.Memory)47 LongMemory (php.runtime.memory.LongMemory)22 KeyValueMemory (php.runtime.memory.KeyValueMemory)18 ReferenceMemory (php.runtime.memory.ReferenceMemory)16 Invoker (php.runtime.invoke.Invoker)12 ObjectMemory (php.runtime.memory.ObjectMemory)12 IObject (php.runtime.lang.IObject)9 ArrayKeyMemory (php.runtime.memory.helper.ArrayKeyMemory)6 ArrayValueMemory (php.runtime.memory.helper.ArrayValueMemory)6 ShortcutMemory (php.runtime.memory.helper.ShortcutMemory)5 Signature (php.runtime.annotation.Reflection.Signature)4 StringMemory (php.runtime.memory.StringMemory)4 File (java.io.File)3 HashSet (java.util.HashSet)3 Element (org.w3c.dom.Element)3 FastMethod (php.runtime.annotation.Runtime.FastMethod)3 ClassEntity (php.runtime.reflection.ClassEntity)3 PropertyEntity (php.runtime.reflection.PropertyEntity)3