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;
}
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;
}
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;
}
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();
}
}
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();
}
Aggregations