1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
// This file 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 3 of
// the License, or (at your option) any later version.
//
// This file 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 libGDF. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2010 Martin Billinger
#ifndef __STRING_H_INCLUDED__
#define __STRING_H_INCLUDED__
#include <iostream>
class String
{
public:
String( )
{
len = 0;
str = new char[1];
str[0] = 0;
}
String( const char *ch )
{
len = 0;
while( ch[len] != 0 )
{
len++;
}
str = new char[len+1];
for( size_t i=0; i<len; i++ )
str[i] = ch[i];
str[len] = 0;
}
String( const char *ch, size_t length )
{
len = length;
str = new char[len+1];
for( size_t i=0; i<len; i++ )
str[i] = ch[i];
str[len] = 0;
}
String( const String &src )
{
len = src.len;
str = new char[len+1];
for( size_t i=0; i<len; i++ )
str[i] = src.str[i];
str[len] = 0;
}
String( const String &a, const String &b )
{
len = a.len + b.len;
str = new char[len+1];
for( size_t i=0; i<a.len; i++ )
str[i] = a.str[i];
for( size_t i=0; i<b.len; i++ )
str[a.len+i] = b.str[i];
str[len] = 0;
}
~String( )
{
delete[] str;
}
size_t find( char ch ) const
{
for( size_t i=0; i<len; i++ )
if( str[i] == ch )
return i;
return len;
}
inline char &operator[]( size_t idx ) { return str[idx]; }
inline const char &operator[]( size_t idx ) const { return str[idx]; }
inline const char *c_str( ) const { return str; }
inline size_t length( ) const { return len; }
bool operator<( const String &other ) const
{
size_t n = 0;
while( n < len && n < other.len )
{
if( str[n] < other[n] )
return true;
else if( str[n] > other[n] )
return false;
n++;
}
return len < other.len;
}
String substr( size_t start, size_t length )
{
return String( &str[start], length );
}
private:
char *str;
size_t len;
};
String operator+( const String &a, const String &b )
{
return String( a, b );
}
String operator+( const char c[], const String &s )
{
return String(c) + s;
}
std::ostream& operator<<( std::ostream& out, const String &s )
{
return out << s.c_str();
}
#endif // STRING_H
|