All Downloads are FREE. Search and download functionalities are using the official Maven repository.

templates.jsIO.java.lang.LongParser.vm Maven / Gradle / Ivy

There is a newer version: 3.5.12
Show newest version
package java.lang;

public object LongParser {

    var DIGIT : java.util.HashMap? = null
    private fun digit(c: Char) : jet.Long {
        if(DIGIT == null){
            DIGIT = java.util.HashMap()
            DIGIT!!.put('0',0)
            DIGIT!!.put('1',1)
            DIGIT!!.put('2',2)
            DIGIT!!.put('3',3)
            DIGIT!!.put('4',4)
            DIGIT!!.put('5',5)
            DIGIT!!.put('6',6)
            DIGIT!!.put('7',7)
            DIGIT!!.put('8',8)
            DIGIT!!.put('9',9)
        }
        if(DIGIT!!.containsKey(c)){
            return DIGIT!!.get(c)!!
        } else {
            return -1 as jet.Long
        }
    }

    fun parseLong(s:jet.String) : jet.Long? {
        var radix : Int = 10
        var result : jet.Long = 0
        var negative = false
        var i = 0
        var len = s.length()
        var digit : jet.Long = 0
        if (len > 0) {
            var firstChar = s.charAt(0);
            if (firstChar < '0') { // Possible leading "-"
                if (firstChar == '-') {
                    negative = true;
                } else {
                    return null
                }
                if (len == 1){ // Cannot have lone "-"
                    return null
                }
                i++;
            }
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = digit(s.charAt(i++));
                if (digit < 0) {
                    return null
                }
                result *= radix;
                result -= digit;
            }
        } else {
            return null
        }
        if(negative){
            return result
        } else {
            return -result
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy