// -*- 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: Byte.jlc,v 1.4 2003/09/27 08:10:28 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/lang/Byte.h" #include "jakelib2/lang/Integer.h" #include "jakelib2/lang/Character.h" #ifdef HAVE_ERRNO_H #include #endif using namespace jakelib::lang; JAKELIB_IMPLEMENT_CLASS_1("jakelib.lang.Byte", Byte, Number, Comparable); const jbyte Byte::MAX_VALUE = 127; const jbyte Byte::MIN_VALUE = -128; /*****************************************************************************\ * Byte | *****************************************************************************/ Byte::Byte(jbyte value) { this->value = value; } Byte::Byte(String* s) { value = parseByte(s); } /*****************************************************************************\ * hashCode | *****************************************************************************/ int Byte::hashCode() { return value; } /*****************************************************************************\ * equals | *****************************************************************************/ jboolean Byte::equals(Object* obj) { if (obj == null || !obj->getClass()->isInstance(this)) return false; Byte* object = (Byte*) obj; return ( value == object->value ); } /*****************************************************************************\ * toString | *****************************************************************************/ String* Byte::toString() { return toString(value); } String* Byte::toString(jbyte value) { if (value == 0) return new String("0"); char buf[13]; int pos = 13; jboolean negative = (value < 0); buf[--pos] = '\0'; if (!negative) { value = -value; } while (value < 0) { buf[--pos] = Integer::digits[-(value % 10)]; value /= 10; } if (negative) buf[--pos] = '-'; return new String(&buf[pos]); } String* Byte::toString(jbyte 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); }; char buf[9]; int pos = 9; jboolean negative = (value < 0); if (!negative) { value = -value; } while (value < 0) { buf[--pos] = Integer::digits[-(value % radix)]; value /= radix; } if (negative) buf[--pos] = '-'; return new String(buf, pos, 9-pos); } /*****************************************************************************\ * parseByte | *****************************************************************************/ jbyte Byte::parseByte(String* str) { return parseByte(str, 10); } jbyte Byte::parseByte(String* str, int radix) { if (str == null) throw new NumberFormatException(`"null"` .. JAKELIB_AT2("jakelib.lang.Byte.parseByte")); if (radix < Character::MIN_RADIX) throw new NumberFormatException(`"radix "` .. radix .. `" less than Character.MIN_RADIX"` .. JAKELIB_AT2("jakelib.lang.Byte.parseByte")); if (radix > Character::MAX_RADIX) throw new NumberFormatException(`"radix "` .. radix .. `" greater than Character.MAX_RADIX"` .. JAKELIB_AT2("jakelib.lang.Byte.parseByte")); if (str->charAt(0) == '\0') throw new NumberFormatException(); char* ptr; int i = strtol(str->latin1(), &ptr, radix); if (*ptr != '\0') throw new NumberFormatException(str .. JAKELIB_AT2("jakelib.lang.Byte.parseByte")); return i; } /*****************************************************************************\ * xxxValue | *****************************************************************************/ jbyte Byte::byteValue() { return value; }; jfloat Byte::floatValue() { return (jfloat) value; }; jint Byte::intValue() { return (jint) value; }; jlong Byte::longValue() { return (jlong) value; }; jdouble Byte::doubleValue() { return (jdouble) value; }; jshort Byte::shortValue() { return (jshort) value;} ; /*****************************************************************************\ * valueOf | *****************************************************************************/ Byte* Byte::valueOf(String* str, int radix) { return new Byte(parseByte(str, radix)); } Byte* Byte::valueOf(String* str) { return new Byte(parseByte(str, 10)); } /*****************************************************************************\ * decode | *****************************************************************************/ // Byte Byte::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 { // jbyte result = Byte::parseByte(str.substring(index), radix); // if (negative) // result = -result; // return Byte(result); // } // catch (NumberFormatException *e) { // // If number is Byte.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 = Byte::valueOf(constant, radix); // } // return result; // } // Byte Byte::decode(const char* str) // { // return decode(String(str)); // } /*****************************************************************************\ * compareTo | *****************************************************************************/ int Byte::compareTo(Byte* obj) { jbyte otherValue = obj->value; return (value < otherValue ? -1 : (value > otherValue ? 1 : 0)); } int Byte::compareTo(Object* obj) { return compareTo((Byte*) obj); }