// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: Integer.jlc,v 1.5 2003/09/27 08:10:28 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/lang/Integer.h" #include "jakelib2/lang/Character.h" #include #ifdef HAVE_ERRNO_H # include #endif using namespace jakelib::lang; JAKELIB_IMPLEMENT_CLASS_1("jakelib.lang.Integer", Integer, Number, Comparable) const jint Integer::MAX_VALUE = 0x7FFFFFFF; const jint Integer::MIN_VALUE = 0x80000000; const char* Integer::digits = "0123456789abcdefghijklmnopqrstuvwxyz"; /*****************************************************************************\ * Integer | *****************************************************************************/ Integer::Integer(jint value) { this->value = value; } Integer::Integer(String* s) { value = parseInt(s); } /*****************************************************************************\ * hashCode | *****************************************************************************/ jint Integer::hashCode() { return value; } /*****************************************************************************\ * equals | *****************************************************************************/ jboolean Integer::equals(Object* obj) { if (obj == null || !obj->getClass()->isInstance(this)) return false; Integer* object = (Integer*) obj; return ( value == object->value ); } /*****************************************************************************\ * toString | *****************************************************************************/ String* Integer::toString() { return toString(value); } String* Integer::toString(jint value) { if (value == 0) return new String("0"); jchar buf[11]; int pos = 11; jboolean negative = (value < 0); if (!negative) { value = -value; } while (value < 0) { buf[--pos] = digits[-(value % 10)]; value /= 10; } if (negative) buf[--pos] = '-'; return new String(buf, pos, 11-pos); } String* Integer::toString(jint value, int radix) { if (value == 0) return new String("0"); if (radix < Character::MIN_RADIX || radix > Character::MAX_RADIX) radix = 10; if (radix == 10) { return toString(value); }; jchar buf[33]; int pos = 33; jboolean negative = (value < 0); if (!negative) { value = -value; } while (value < 0) { buf[--pos] = digits[-(value % radix)]; value /= radix; } if (negative) buf[--pos] = '-'; return new String(buf, pos, 33-pos); } /*****************************************************************************\ * parseInt | *****************************************************************************/ jint Integer::parseInt(String* str, int radix) { if (str == null) throw new NumberFormatException(`"null"` .. JAKELIB_AT2("jakelib.lang.Integer.parseInt")); if (radix < Character::MIN_RADIX) throw new NumberFormatException(`"radix "` .. radix .. `" less than Character::MIN_RADIX"` .. JAKELIB_AT2("jakelib.lang.Integer.parseInt")); if (radix > Character::MAX_RADIX) throw new NumberFormatException(`"radix "` .. radix .. `" greater than Character::MAX_RADIX"` .. JAKELIB_AT2("jakelib.lang.Integer.parseInt")); if (str->charAt(0) == '\0') throw new NumberFormatException(); char* ptr; char* in = str->latin1(); jint i = strtol(in, &ptr, radix); if (*ptr != '\0') throw new NumberFormatException(str .. JAKELIB_AT2("jakelib.lang.Integer.parseInt")); return i; } jint Integer::parseInt(const char* str, int radix) { if (str == null) throw new NumberFormatException(`"null"` .. JAKELIB_AT2("jakelib.lang.Integer.parseInt")); if (radix < Character::MIN_RADIX) throw new NumberFormatException(`"radix "` .. radix .. `" less than Character::MIN_RADIX"` .. JAKELIB_AT2("jakelib.lang.Integer.parseInt")); if (radix > Character::MAX_RADIX) throw new NumberFormatException(`"radix "` .. radix .. `" greater than Character::MAX_RADIX"` .. JAKELIB_AT2("jakelib.lang.Integer.parseInt")); if (strlen(str) == 0) throw new NumberFormatException(); char* ptr; jint i = strtol(str, &ptr, radix); if (*ptr != '\0') throw new NumberFormatException(`""` .. str .. JAKELIB_AT2("jakelib.lang.Integer.parseInt")); return i; } /*****************************************************************************\ * xxxValue | *****************************************************************************/ jbyte Integer::byteValue() { return (jbyte) value; }; jfloat Integer::floatValue() { return (jfloat) value; }; jint Integer::intValue() { return value; }; jlong Integer::longValue() { return (jlong) value; }; jdouble Integer::doubleValue() { return (jdouble) value; }; jshort Integer::shortValue() { return (jshort) value;} ; /*****************************************************************************\ * toUnsignedString | *****************************************************************************/ String* Integer::toUnsignedString(jint iin, int shift) { unsigned int i = iin; char buf[33]; int pos = 33; int radix = 1 << shift; int mask = radix - 1; buf[--pos] = '\0'; do { buf[--pos] = digits[i & mask]; i >>= shift; } while (i != 0); return new String(&buf[pos]); } String* Integer::toHexString(jint i) { return toUnsignedString(i, 4); } String* Integer::toBinaryString(jint i) { return toUnsignedString(i, 1); } String* Integer::toOctalString(jint i) { return toUnsignedString(i, 3); } /*****************************************************************************\ * valueOf | *****************************************************************************/ Integer* Integer::valueOf(String* str, int radix) { return new Integer(parseInt(str, radix)); } Integer* Integer::valueOf(String* str) { return new Integer(parseInt(str, 10)); } /*****************************************************************************\ * decode | *****************************************************************************/ // Integer Integer::decode(String& str) // { // int radix = 10; // int index = 0; // jboolean negative = false; // // Handle minus sign, if present // if (str.startsWith("-")) { // negative = true; // index++; // } // // Handle radix specifier, if present // if (str.startsWith("0x", index) || str.startsWith("0X", index)) { // index += 2; // radix = 16; // } // else if (str.startsWith("#", index)) { // index ++; // radix = 16; // } // else if (str.startsWith("0", index) && str.length() > 1 + index) { // index ++; // radix = 8; // } // if (str.startsWith("-", index)) // throw new NumberFormatException("Negative sign in wrong position"); // try { // int result = Integer::parseInt(str.substring(index), radix); // if (negative) // result = -result; // return Integer(result); // } // catch (NumberFormatException *e) { // // If number is Integer.MIN_VALUE, we'll end up here. The next line // // handles this case, and causes any genuine format error to be // // rethrown. // String constant = negative ? String("-" + str.substring(index)) // : str.substring(index); // result = Integer::valueOf(constant, radix); // } // return result; // } // Integer Integer::decode(const char* str) // { // return decode(String(str)); // } /*****************************************************************************\ * compareTo | *****************************************************************************/ int Integer::compareTo(Integer* obj) { jint otherValue = obj->value; return (value < otherValue ? -1 : (value > otherValue ? 1 : 0)); } int Integer::compareTo(Object* obj) { return compareTo((Integer*) obj); }