Search in sources :

Example 1 with Immutable

use of php.runtime.annotation.Runtime.Immutable 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 2 with Immutable

use of php.runtime.annotation.Runtime.Immutable in project jphp by jphp-compiler.

the class StringFunctions method crc32.

@Immutable
public static int crc32(Environment env, Memory value) {
    CRC32 crc = new CRC32();
    crc.update(value.getBinaryBytes(env.getDefaultCharset()));
    return (int) crc.getValue();
}
Also used : CRC32(java.util.zip.CRC32) Immutable(php.runtime.annotation.Runtime.Immutable)

Example 3 with Immutable

use of php.runtime.annotation.Runtime.Immutable in project jphp by jphp-compiler.

the class StringFunctions method number_format.

@Immutable
public static String number_format(double number, int decimals, char decPoint, char thousandsSep) {
    String pattern;
    if (decimals > 0) {
        StringBuilder patternBuilder = new StringBuilder(6 + decimals);
        patternBuilder.append(thousandsSep == 0 ? "###0." : "#,##0.");
        for (int i = 0; i < decimals; i++) {
            patternBuilder.append('0');
        }
        pattern = patternBuilder.toString();
    } else {
        pattern = thousandsSep == 0 ? "###0" : "#,##0";
    }
    DecimalFormatSymbols decimalFormatSymbols;
    if (decPoint == '.' && thousandsSep == ',') {
        decimalFormatSymbols = DEFAULT_DECIMAL_FORMAT_SYMBOLS;
    } else {
        decimalFormatSymbols = new DecimalFormatSymbols();
        decimalFormatSymbols.setDecimalSeparator(decPoint);
        decimalFormatSymbols.setGroupingSeparator(thousandsSep);
        decimalFormatSymbols.setZeroDigit('0');
    }
    DecimalFormat format = new DecimalFormat(pattern, decimalFormatSymbols);
    String result = format.format(number);
    if (decPoint == 0 && decimals > 0) {
        // no way to get DecimalFormat to output nothing for the point,
        // so remove it here
        int i = result.lastIndexOf(decPoint);
        return result.substring(0, i) + result.substring(i + 1, result.length());
    } else {
        return result;
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) Immutable(php.runtime.annotation.Runtime.Immutable)

Example 4 with Immutable

use of php.runtime.annotation.Runtime.Immutable in project jphp by jphp-compiler.

the class StringFunctions method implode.

@Immutable
public static Memory implode(Environment env, TraceInfo trace, Memory glue, Memory pieces) {
    ArrayMemory array;
    String delimiter;
    if (glue.isArray()) {
        array = (ArrayMemory) glue;
        delimiter = pieces.toString();
    } else if (pieces.isArray()) {
        array = (ArrayMemory) pieces;
        delimiter = glue.toString();
    } else {
        env.warning(trace, "Argument must be an array");
        return Memory.NULL;
    }
    StringBuilder builder = new StringBuilder();
    int i = 0, size = array.size();
    for (Memory el : array) {
        builder.append(el.toString());
        if (i != size - 1)
            builder.append(delimiter);
        i++;
    }
    return new StringMemory(builder.toString());
}
Also used : Memory(php.runtime.Memory) Immutable(php.runtime.annotation.Runtime.Immutable)

Example 5 with Immutable

use of php.runtime.annotation.Runtime.Immutable in project jphp by jphp-compiler.

the class StringFunctions method substr_replace.

@Immutable
public static Memory substr_replace(Environment env, TraceInfo trace, Memory string, Memory replacementM, Memory startM, Memory lengthM) {
    int start = 0;
    int length = Integer.MAX_VALUE / 2;
    String replacement = "";
    ForeachIterator replacementIterator = null;
    if (replacementM.isArray())
        replacementIterator = replacementM.getNewIterator(env, false, false);
    else
        replacement = replacementM.toString();
    ForeachIterator startIterator = null;
    if (startM.isArray())
        startIterator = startM.getNewIterator(env, false, false);
    else
        start = startM.toInteger();
    ForeachIterator lengthIterator = null;
    if (lengthM.isArray())
        lengthIterator = lengthM.getNewIterator(env, false, false);
    else
        length = lengthM.toInteger();
    if (string.isArray()) {
        ArrayMemory resultArray = new ArrayMemory();
        ForeachIterator iterator = string.getNewIterator(env, false, false);
        while (iterator.next()) {
            String value = iterator.getValue().toString();
            if (replacementIterator != null && replacementIterator.next())
                replacement = replacementIterator.getValue().toString();
            if (lengthIterator != null && lengthIterator.next())
                length = lengthIterator.getValue().toInteger();
            if (startIterator != null && startIterator.next())
                start = startIterator.getValue().toInteger();
            String result = _substr_replace(value, replacement, start, length);
            resultArray.add(new StringMemory(result));
        }
        return resultArray.toConstant();
    } else {
        if (replacementIterator != null && replacementIterator.next())
            replacement = replacementIterator.getValue().toString();
        if (lengthIterator != null && lengthIterator.next())
            length = lengthIterator.getValue().toInteger();
        if (startIterator != null && startIterator.next())
            start = startIterator.getValue().toInteger();
        return new StringMemory(_substr_replace(string.toString(), replacement, start, length));
    }
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Immutable(php.runtime.annotation.Runtime.Immutable)

Aggregations

Immutable (php.runtime.annotation.Runtime.Immutable)7 Memory (php.runtime.Memory)3 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 CRC32 (java.util.zip.CRC32)1 MemorySerializer (org.develnext.jphp.json.gson.MemorySerializer)1 ForeachIterator (php.runtime.lang.ForeachIterator)1 ArrayMemory (php.runtime.memory.ArrayMemory)1 DoubleMemory (php.runtime.memory.DoubleMemory)1 LongMemory (php.runtime.memory.LongMemory)1