Search in sources :

Example 16 with Memory

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());
}
Also used : Memory(php.runtime.Memory) Test(org.junit.Test)

Example 17 with Memory

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

Example 18 with Memory

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;
    }
}
Also used : Memory(php.runtime.Memory) Immutable(php.runtime.annotation.Runtime.Immutable)

Example 19 with Memory

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

Example 20 with Memory

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

Aggregations

Memory (php.runtime.Memory)346 Test (org.junit.Test)124 ArrayMemory (php.runtime.memory.ArrayMemory)122 ObjectMemory (php.runtime.memory.ObjectMemory)75 LongMemory (php.runtime.memory.LongMemory)58 StringMemory (php.runtime.memory.StringMemory)58 ForeachIterator (php.runtime.lang.ForeachIterator)47 ReferenceMemory (php.runtime.memory.ReferenceMemory)40 KeyValueMemory (php.runtime.memory.KeyValueMemory)25 Invoker (php.runtime.invoke.Invoker)21 ArrayKeyMemory (php.runtime.memory.helper.ArrayKeyMemory)21 ArrayValueMemory (php.runtime.memory.helper.ArrayValueMemory)21 ShortcutMemory (php.runtime.memory.helper.ShortcutMemory)21 Environment (php.runtime.env.Environment)20 IObject (php.runtime.lang.IObject)19 UndefinedMemory (php.runtime.memory.helper.UndefinedMemory)16 ClassEntity (php.runtime.reflection.ClassEntity)16 TraceInfo (php.runtime.env.TraceInfo)12 File (java.io.File)7 LocalVariable (org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)7