use of php.runtime.Memory in project jphp by jphp-compiler.
the class ClosuresTest method testStaticVars.
@Test
public void testStaticVars() {
Memory memory = includeResource("closures/static_vars.php");
Assert.assertEquals("success", memory.toString());
}
use of php.runtime.Memory in project jphp by jphp-compiler.
the class StringFunctions method printf.
public static int printf(Environment env, TraceInfo trace, String format, Memory... args) {
Memory str = sprintf(env, trace, format, args);
if (str.isNull())
return 0;
else {
String value = str.toString();
env.echo(value);
return value.length();
}
}
use of php.runtime.Memory in project jphp by jphp-compiler.
the class StringFunctions method html_entity_decode.
@Immutable
public static Memory html_entity_decode(Environment env, TraceInfo trace, Memory _string, int flags, String encoding) {
try {
String string = new String(_string.getBinaryBytes(env.getDefaultCharset()), encoding);
int len = string.length();
int htmlEntityStart = -1;
StringBuilder result = new StringBuilder();
for (int i = 0; i < len; i++) {
char ch = string.charAt(i);
if (ch == '&' && htmlEntityStart < 0) {
htmlEntityStart = i;
} else if (htmlEntityStart < 0) {
result.append(ch);
} else if (ch == ';') {
String entity = string.substring(htmlEntityStart, i + 1);
Memory value = HTML_ENTITIES.getByScalar(entity);
if (value == null) {
result.append(entity);
} else {
result.append(value);
}
htmlEntityStart = -1;
} else if (!(('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z'))) {
result.append('&');
i = htmlEntityStart;
htmlEntityStart = -1;
}
}
if (htmlEntityStart > 0) {
result.append(string, htmlEntityStart, len);
}
return new StringMemory(result.toString());
} catch (UnsupportedEncodingException e) {
env.warning(trace, "html_entity_decode(): unsupported encoding - %s", encoding);
return Memory.FALSE;
}
}
use of php.runtime.Memory 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.Memory 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;
}
Aggregations