00001 /* 00002 * TwoByteString.cc 00003 * 00004 * Smalltalk like class library for C++ 00005 * String implementor using c-array of words. 00006 * 00007 * Copyright (c) 2004 Milan Cermak 00008 */ 00009 /* 00010 * This library is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or (at your option) any later version. 00014 * 00015 * This library is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with this library; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 */ 00024 #include <stlib/TwoByteString.h> 00025 #include <stlib/Character.h> 00026 #include <stlib/String.h> 00027 00028 #include <string.h> 00029 00030 using namespace Core; 00031 00032 TwoByteString::TwoByteString(int size) 00033 { 00034 content = (unsigned short *) GC_malloc(size * sizeof(unsigned short)); 00035 tally = size; 00036 } 00037 00038 TwoByteString::~TwoByteString(void) 00039 { 00040 GC_free(content); 00041 } 00042 00043 /* Class-accessing protocol */ 00044 String *TwoByteString::className(void) const 00045 { 00046 return new String("TwoByteString"); 00047 } 00048 00049 /* Accessing protocol */ 00050 Character *TwoByteString::at(int index) const 00051 { 00052 return Character::value(content[index]); 00053 } 00054 00055 void TwoByteString::put(int index, Character *object) 00056 { 00057 content[index] = object->asInteger(); 00058 } 00059 00060 int TwoByteString::byteSize(void) 00061 { 00062 return 2; 00063 } 00064 00065 void TwoByteString::putRawValue(int index, unsigned short value) 00066 { 00067 content[index] = value; 00068 } 00069 00070 /* Comparing protocol */ 00071 bool TwoByteString::isEqual(StringImpl *string) 00072 { 00073 return string->isEqualToShorts(this->content); 00074 } 00075 00076 bool TwoByteString::isEqualToBytes(unsigned char *string) 00077 { 00078 for (int i = 0; i < tally; i++) { 00079 if (this->content[i] != string[i]) 00080 return false; 00081 } 00082 return true; 00083 } 00084 00085 bool TwoByteString::isEqualToShorts(unsigned short *string) 00086 { 00087 for (int i = 0; i < tally; i++) { 00088 if (this->content[i] != string[i]) 00089 return false; 00090 } 00091 return true; 00092 } 00093 00094 /* Adding protocol */ 00095 void TwoByteString::changeSize(long newSize) 00096 { 00097 unsigned short *oldContent = content; 00098 00099 content = (unsigned short *) GC_malloc(newSize * sizeof(unsigned short)); 00100 bzero(content, newSize); 00101 memcpy(content, oldContent, size() * sizeof(unsigned short)); 00102 tally = newSize; 00103 } 00104 00105 /* Converting protocol */ 00106 TwoByteString *TwoByteString::asTwoByteString(void) 00107 { 00108 return this; 00109 } 00110 00111 /* Testing protocol */ 00112 bool TwoByteString::isTwoByteString(void) const 00113 { 00114 return true; 00115 }