| 1 | /// \ingroup newran |
| 2 | ///@{ |
| 3 | |
| 4 | /// \file simpstr.cpp |
| 5 | /// A very simple string class - bodies. |
| 6 | |
| 7 | #include "include.h" |
| 8 | #include "myexcept.h" |
| 9 | #include "simpstr.h" |
| 10 | |
| 11 | #ifdef use_namespace |
| 12 | namespace RBD_COMMON { |
| 13 | #endif |
| 14 | |
| 15 | |
| 16 | /// Construct an empty SimpleString. |
| 17 | SimpleString::SimpleString() : n(0) |
| 18 | { |
| 19 | S = new char [1]; if (!S) Throw(Bad_alloc()); |
| 20 | *S = 0; |
| 21 | } |
| 22 | |
| 23 | /// Construct SimpleString equal to a SimpleString. |
| 24 | SimpleString::SimpleString(const SimpleString& s) : n(s.n) |
| 25 | { |
| 26 | S = new char [n+1]; if (!S) Throw(Bad_alloc()); |
| 27 | char* s1 = S; const char* s2 = s.S; unsigned int i = n+1; |
| 28 | while (i--) *s1++ = *s2++; |
| 29 | } |
| 30 | |
| 31 | /// Construct SimpleString equal to a C character string. |
| 32 | SimpleString::SimpleString(const char* c) |
| 33 | { |
| 34 | const char* s2 = c; |
| 35 | n = 0; while (*s2++ != 0) ++n; |
| 36 | S = new char [n+1]; if (!S) Throw(Bad_alloc()); |
| 37 | char* s1 = S; s2 = c; unsigned int i = n+1; |
| 38 | while (i--) *s1++ = *s2++; |
| 39 | } |
| 40 | |
| 41 | /// Construct SimpleString equal to i copies of a character, c. |
| 42 | SimpleString::SimpleString(unsigned int i, char c) : n(i) |
| 43 | { |
| 44 | S = new char [n+1]; if (!S) Throw(Bad_alloc()); |
| 45 | char* s1 = S; |
| 46 | while (i--) *s1++ = c; *s1 = 0; |
| 47 | } |
| 48 | |
| 49 | /// Set SimpleString equal to a SimpleString. |
| 50 | void SimpleString::operator=(const SimpleString& s) |
| 51 | { |
| 52 | char* old_S = S; n = s.n; |
| 53 | S = new char [n+1]; if (!S) Throw(Bad_alloc()); |
| 54 | char* s1 = S; const char* s2 = s.S; unsigned int i = n+1; |
| 55 | while (i--) *s1++ = *s2++; |
| 56 | delete [] old_S; |
| 57 | } |
| 58 | |
| 59 | /// Set SimpleString equal to a C character string. |
| 60 | void SimpleString::operator=(const char* c) |
| 61 | { |
| 62 | const char* s2 = c; |
| 63 | n = 0; while (*s2++ != 0) ++n; |
| 64 | char* old_S = S; |
| 65 | S = new char [n+1]; if (!S) Throw(Bad_alloc()); |
| 66 | char* s1 = S; s2 = c; unsigned int i = n+1; |
| 67 | while (i--) *s1++ = *s2++; |
| 68 | delete [] old_S; |
| 69 | } |
| 70 | |
| 71 | /// Set SimpleString equal to a single character. |
| 72 | void SimpleString::operator=(char c) |
| 73 | { |
| 74 | char* old_S = S; n = 1; |
| 75 | S = new char [2]; if (!S) Throw(Bad_alloc()); |
| 76 | S[0] = c; S[1] = 0; |
| 77 | delete [] old_S; |
| 78 | } |
| 79 | |
| 80 | /// Append SimpleString to SimpleString. |
| 81 | void SimpleString::operator+=(const SimpleString& s) |
| 82 | { |
| 83 | char* old_S = S; unsigned int old_n = n; n += s.n; |
| 84 | S = new char [n+1]; if (!S) Throw(Bad_alloc()); |
| 85 | char* s1 = S; const char* s2 = old_S; unsigned int i = old_n; |
| 86 | while (i--) *s1++ = *s2++; |
| 87 | s2 = s.S; i = s.n+1; |
| 88 | while (i--) *s1++ = *s2++; |
| 89 | delete [] old_S; |
| 90 | } |
| 91 | |
| 92 | /// Append C character string to SimpleString. |
| 93 | void SimpleString::operator+=(const char* c) |
| 94 | { |
| 95 | char* old_S = S; unsigned int old_n = n; |
| 96 | const char* s2 = c; |
| 97 | int n2 = 0; while (*s2++ != 0) ++n2; n += n2; |
| 98 | S = new char [n+1]; if (!S) Throw(Bad_alloc()); |
| 99 | char* s1 = S; s2 = old_S; unsigned int i = old_n; |
| 100 | while (i--) *s1++ = *s2++; |
| 101 | s2 = c; i = n2+1; |
| 102 | while (i--) *s1++ = *s2++; |
| 103 | delete [] old_S; |
| 104 | } |
| 105 | |
| 106 | /// Append character to SimpleString. |
| 107 | void SimpleString::operator+=(char c) |
| 108 | { |
| 109 | char* old_S = S; unsigned int old_n = n; ++n; |
| 110 | S = new char [n+1]; if (!S) Throw(Bad_alloc()); |
| 111 | char* s1 = S; const char* s2 = old_S; unsigned int i = old_n; |
| 112 | while (i--) *s1++ = *s2++; |
| 113 | *s1++ = c; *s1 = 0; |
| 114 | delete [] old_S; |
| 115 | } |
| 116 | |
| 117 | /// Concatenate two SimpleStrings. |
| 118 | SimpleString operator+(const SimpleString& s1, const SimpleString& s2) |
| 119 | { |
| 120 | SimpleString s(s1.n + s2.n, (char)0); |
| 121 | const char* c_in = s1.S; unsigned int i = s1.n; char* c_out = s.S; |
| 122 | while (i--) *c_out++ = *c_in++; |
| 123 | c_in = s2.S; i = s2.n; |
| 124 | while (i--) *c_out++ = *c_in++; |
| 125 | return s; |
| 126 | } |
| 127 | |
| 128 | /// Concatenate C character string and SimpleString. |
| 129 | SimpleString operator+(const char* c1, const SimpleString& s2) |
| 130 | { |
| 131 | const char* c_in = c1; |
| 132 | int n1 = 0; while (*c_in++ != 0) ++n1; |
| 133 | SimpleString s(n1 + s2.n, (char)0); |
| 134 | c_in = c1; unsigned int i = n1; char* c_out = s.S; |
| 135 | while (i--) *c_out++ = *c_in++; |
| 136 | c_in = s2.S; i = s2.n; |
| 137 | while (i--) *c_out++ = *c_in++; |
| 138 | return s; |
| 139 | } |
| 140 | |
| 141 | /// Concatenate SimpleString and C character string. |
| 142 | SimpleString operator+(const SimpleString& s1, const char* c2) |
| 143 | { |
| 144 | const char* c_in = c2; |
| 145 | int n2 = 0; while (*c_in++ != 0) ++n2; |
| 146 | SimpleString s(s1.n + n2, (char)0); |
| 147 | c_in = s1.S; unsigned int i = s1.n; char* c_out = s.S; |
| 148 | while (i--) *c_out++ = *c_in++; |
| 149 | c_in = c2; i = n2; |
| 150 | while (i--) *c_out++ = *c_in++; |
| 151 | return s; |
| 152 | } |
| 153 | |
| 154 | /// Concatenate character and SimpleString. |
| 155 | SimpleString operator+(char c1, const SimpleString& s2) |
| 156 | { |
| 157 | SimpleString s(1 + s2.n, (char)0); |
| 158 | char* c_out = s.S; *c_out++ = c1; |
| 159 | const char* c_in = s2.S; unsigned int i = s2.n; |
| 160 | while (i--) *c_out++ = *c_in++; |
| 161 | return s; |
| 162 | } |
| 163 | |
| 164 | /// Concatenate SimpleString and character. |
| 165 | SimpleString operator+(const SimpleString& s1, char c2) |
| 166 | { |
| 167 | SimpleString s(s1.n + 1, (char)0); |
| 168 | const char* c_in = s1.S; unsigned int i = s1.n; char* c_out = s.S; |
| 169 | while (i--) *c_out++ = *c_in++; |
| 170 | *c_out = c2; |
| 171 | return s; |
| 172 | } |
| 173 | |
| 174 | #ifdef use_namespace |
| 175 | } |
| 176 | #endif |
| 177 | |
| 178 | // Descriptions of inline functions for Doxygen |
| 179 | |
| 180 | /// \fn unsigned int SimpleString::length() const |
| 181 | /// Number of charcters in a string excluding the terminal 0. |
| 182 | |
| 183 | /// \fn unsigned int SimpleString::size() const |
| 184 | /// Number of charcters in a string excluding the terminal 0. |
| 185 | |
| 186 | /// \fn const char* SimpleString::c_str() const |
| 187 | /// Pointer to the character string containing the string itself. |
| 188 | |
| 189 | /// \fn const char* SimpleString::data() const |
| 190 | /// Pointer to the character string containing the string itself. |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | ///@} |
| 199 | |
| 200 | |
| 201 | |
| 202 |