/* Generated automatically by jlpp - do not edit. */ #include static jakelib::lang::String* jakelib2_strings[] = {null, null, null, null, null, null, null, null, null, null, null}; // "null" static jchar chars_jakelib2_str_0[] = {110,117,108,108}; // "radix " static jchar chars_jakelib2_str_1[] = {114,97,100,105,120,32}; // " less than Character::MIN_RADIX" static jchar chars_jakelib2_str_2[] = {32,108,101,115,115,32,116,104,97,110,32,67,104,97,114,97,99,116,101,114,58,58,77,73,78,95,82,65,68,73,88}; // "radix " static jchar chars_jakelib2_str_3[] = {114,97,100,105,120,32}; // " greater than Character::MAX_RADIX" static jchar chars_jakelib2_str_4[] = {32,103,114,101,97,116,101,114,32,116,104,97,110,32,67,104,97,114,97,99,116,101,114,58,58,77,65,88,95,82,65,68,73,88}; // "null" static jchar chars_jakelib2_str_5[] = {110,117,108,108}; // "radix " static jchar chars_jakelib2_str_6[] = {114,97,100,105,120,32}; // " less than Character::MIN_RADIX" static jchar chars_jakelib2_str_7[] = {32,108,101,115,115,32,116,104,97,110,32,67,104,97,114,97,99,116,101,114,58,58,77,73,78,95,82,65,68,73,88}; // "radix " static jchar chars_jakelib2_str_8[] = {114,97,100,105,120,32}; // " greater than Character::MAX_RADIX" static jchar chars_jakelib2_str_9[] = {32,103,114,101,97,116,101,114,32,116,104,97,110,32,67,104,97,114,97,99,116,101,114,58,58,77,65,88,95,82,65,68,73,88}; // "" static jchar chars_jakelib2_str_10[] = {0}; #line 1 "lang/Integer.jlc" // -*- 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(JAKELIB_ONDEMAND(jakelib2_strings[0], new jakelib::lang::String(chars_jakelib2_str_0, 0, 4)) ->plus( JAKELIB_AT2("jakelib.lang.Integer.parseInt"))); if (radix < Character::MIN_RADIX) throw new NumberFormatException(JAKELIB_ONDEMAND(jakelib2_strings[1], new jakelib::lang::String(chars_jakelib2_str_1, 0, 6)) ->plus( radix )->plus( JAKELIB_ONDEMAND(jakelib2_strings[2], new jakelib::lang::String(chars_jakelib2_str_2, 0, 31)) )->plus( JAKELIB_AT2("jakelib.lang.Integer.parseInt"))); if (radix > Character::MAX_RADIX) throw new NumberFormatException(JAKELIB_ONDEMAND(jakelib2_strings[3], new jakelib::lang::String(chars_jakelib2_str_3, 0, 6)) ->plus( radix )->plus( JAKELIB_ONDEMAND(jakelib2_strings[4], new jakelib::lang::String(chars_jakelib2_str_4, 0, 34)) )->plus( 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 ->plus( JAKELIB_AT2("jakelib.lang.Integer.parseInt"))); return i; } jint Integer::parseInt(const char* str, int radix) { if (str == null) throw new NumberFormatException(JAKELIB_ONDEMAND(jakelib2_strings[5], new jakelib::lang::String(chars_jakelib2_str_5, 0, 4)) ->plus( JAKELIB_AT2("jakelib.lang.Integer.parseInt"))); if (radix < Character::MIN_RADIX) throw new NumberFormatException(JAKELIB_ONDEMAND(jakelib2_strings[6], new jakelib::lang::String(chars_jakelib2_str_6, 0, 6)) ->plus( radix )->plus( JAKELIB_ONDEMAND(jakelib2_strings[7], new jakelib::lang::String(chars_jakelib2_str_7, 0, 31)) )->plus( JAKELIB_AT2("jakelib.lang.Integer.parseInt"))); if (radix > Character::MAX_RADIX) throw new NumberFormatException(JAKELIB_ONDEMAND(jakelib2_strings[8], new jakelib::lang::String(chars_jakelib2_str_8, 0, 6)) ->plus( radix )->plus( JAKELIB_ONDEMAND(jakelib2_strings[9], new jakelib::lang::String(chars_jakelib2_str_9, 0, 34)) )->plus( 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(JAKELIB_ONDEMAND(jakelib2_strings[10], new jakelib::lang::String(chars_jakelib2_str_10, 0, 0)) ->plus( str )->plus( 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); }