1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 // -*- c++ -*- 19 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 20 21 // O S C L _ S T R I N G C L A S S 22 23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 24 25 // - - Inclusion - - - - - - - - - - - - - - - - - - - - - - - - - - - - 26 27 #include "oscl_string.h" 28 #include "oscl_error.h" 29 #include "oscl_stdstring.h" 30 31 #ifndef OSCL_COMBINED_DLL 32 #include "oscl_dll.h" 33 OSCL_DLL_ENTRY_POINT_DEFAULT() 34 #endif 35 36 // ************************************************************** 37 // OSCL_String Implementation 38 // OSCL_wString Implementation 39 // ************************************************************** 40 41 OSCL_EXPORT_REF OSCL_String::OSCL_String() 42 {} 43 44 OSCL_EXPORT_REF OSCL_wString::OSCL_wString() 45 {} 46 47 // ************************************************************** 48 OSCL_EXPORT_REF OSCL_String::~OSCL_String() 49 {} 50 51 OSCL_EXPORT_REF OSCL_wString::~OSCL_wString() 52 {} 53 54 // ************************************************************** 55 OSCL_EXPORT_REF OSCL_String::chartype OSCL_String::read(uint32 index) const 56 //default implementation-- may be replaced by derived class 57 { 58 //allow reading entire string incl null terminator. 59 if (index > get_size()) 60 OsclError::Leave(OsclErrGeneral);//invalid index. 61 return get_cstr()[index]; 62 } 63 64 OSCL_EXPORT_REF OSCL_wString::chartype OSCL_wString::read(uint32 index) const 65 //default implementation-- may be replaced by derived class 66 { 67 //allow reading entire string incl null terminator. 68 if (index > get_size()) 69 OsclError::Leave(OsclErrGeneral);//invalid index. 70 return get_cstr()[index]; 71 } 72 73 // ************************************************************** 74 OSCL_EXPORT_REF bool OSCL_String::operator==(const OSCL_String& a) const 75 { 76 return (get_size() == a.get_size() 77 && oscl_strncmp(get_cstr(), a.get_cstr(), a.get_size()) == 0); 78 } 79 80 OSCL_EXPORT_REF bool OSCL_wString::operator==(const OSCL_wString& a) const 81 { 82 return (get_size() == a.get_size() 83 && oscl_strncmp(get_cstr(), a.get_cstr(), a.get_size()) == 0); 84 } 85 86 // ************************************************************** 87 OSCL_EXPORT_REF bool OSCL_String::operator==(const chartype* a) const 88 { 89 uint32 len = (a) ? oscl_strlen(a) : 0; 90 return (get_size() == len 91 && oscl_strncmp(get_cstr(), a, len) == 0); 92 } 93 94 OSCL_EXPORT_REF bool OSCL_wString::operator==(const chartype* a) const 95 { 96 uint32 len = (a) ? oscl_strlen(a) : 0; 97 return (get_size() == len 98 && oscl_strncmp(get_cstr(), a, len) == 0); 99 } 100 101 // ************************************************************** 102 OSCL_EXPORT_REF bool OSCL_String::operator!=(const OSCL_String& a) const 103 { 104 return (get_size() != a.get_size() 105 || oscl_strncmp(get_cstr(), a.get_cstr(), a.get_size()) != 0); 106 } 107 108 OSCL_EXPORT_REF bool OSCL_wString::operator!=(const OSCL_wString& a) const 109 { 110 return (get_size() != a.get_size() 111 || oscl_strncmp(get_cstr(), a.get_cstr(), a.get_size()) != 0); 112 } 113 114 // ************************************************************** 115 OSCL_EXPORT_REF bool OSCL_String::operator>(const OSCL_String& a) const 116 { 117 return (oscl_strncmp(get_cstr(), a.get_cstr(), a.get_size()) > 0); 118 } 119 120 OSCL_EXPORT_REF bool OSCL_wString::operator>(const OSCL_wString& a) const 121 { 122 return (oscl_strncmp(get_cstr(), a.get_cstr(), a.get_size()) > 0); 123 } 124 125 // ************************************************************** 126 OSCL_EXPORT_REF bool OSCL_String::operator>=(const OSCL_String& a) const 127 { 128 return (oscl_strncmp(get_cstr(), a.get_cstr(), a.get_size()) >= 0); 129 } 130 131 OSCL_EXPORT_REF bool OSCL_wString::operator>=(const OSCL_wString& a) const 132 { 133 return (oscl_strncmp(get_cstr(), a.get_cstr(), a.get_size()) >= 0); 134 } 135 136 // ************************************************************** 137 OSCL_EXPORT_REF bool OSCL_String::operator<=(const OSCL_String& a) const 138 { 139 return (oscl_strncmp(get_cstr(), a.get_cstr(), a.get_size()) <= 0); 140 } 141 142 OSCL_EXPORT_REF bool OSCL_wString::operator<=(const OSCL_wString& a) const 143 { 144 return (oscl_strncmp(get_cstr(), a.get_cstr(), a.get_size()) <= 0); 145 } 146 147 // ************************************************************** 148 OSCL_EXPORT_REF bool OSCL_String::operator<(const OSCL_String& a) const 149 { 150 return (oscl_strncmp(get_cstr(), a.get_cstr(), a.get_size()) < 0); 151 } 152 153 OSCL_EXPORT_REF bool OSCL_wString::operator<(const OSCL_wString& a) const 154 { 155 return (oscl_strncmp(get_cstr(), a.get_cstr(), a.get_size()) < 0); 156 } 157 158 // ************************************************************** 159 OSCL_EXPORT_REF OSCL_String::chartype OSCL_String::operator[](uint32 index) const 160 { 161 if (index > get_size()) 162 OsclError::Leave(OsclErrGeneral);//invalid index. 163 return get_cstr()[index]; 164 } 165 166 OSCL_EXPORT_REF OSCL_wString::chartype OSCL_wString::operator[](uint32 index) const 167 { 168 if (index > get_size()) 169 OsclError::Leave(OsclErrGeneral);//invalid index. 170 return get_cstr()[index]; 171 } 172 173 // ************************************************************** 174 OSCL_EXPORT_REF bool OSCL_String::is_writable() const 175 //default implementation-- may be replaced by derived class 176 { 177 return (get_str() != NULL); 178 } 179 180 OSCL_EXPORT_REF bool OSCL_wString::is_writable() const 181 //default implementation-- may be replaced by derived class 182 { 183 return (get_str() != NULL); 184 } 185 186 // ************************************************************** 187 OSCL_EXPORT_REF void OSCL_String::write(uint32 index, chartype c) 188 //default implementation-- may be replaced by derived class 189 { 190 chartype* curbuf = get_str(); 191 if (!curbuf) 192 OsclError::Leave(OsclErrGeneral);//not writable. 193 194 if (c == '\0') 195 { 196 if (index > get_size()) 197 OsclError::Leave(OsclErrGeneral);//invalid index. 198 curbuf[index] = c; 199 set_len(index); 200 } 201 else 202 { 203 if (index >= get_size()) 204 OsclError::Leave(OsclErrGeneral);//invalid index. 205 curbuf[index] = c; 206 } 207 } 208 209 OSCL_EXPORT_REF void OSCL_wString::write(uint32 index, chartype c) 210 //default implementation-- may be replaced by derived class 211 { 212 chartype* curbuf = get_str(); 213 if (!curbuf) 214 OsclError::Leave(OsclErrGeneral);//not writable. 215 216 if (c == '\0') 217 { 218 if (index > get_size()) 219 OsclError::Leave(OsclErrGeneral);//invalid index. 220 curbuf[index] = c; 221 set_len(index); 222 } 223 else 224 { 225 if (index >= get_size()) 226 OsclError::Leave(OsclErrGeneral);//invalid index. 227 curbuf[index] = c; 228 } 229 } 230 231 // ************************************************************** 232 OSCL_EXPORT_REF void OSCL_String::write(uint32 offset, uint32 length, const chartype* ptr) 233 //default implementation-- may be replaced by derived class 234 { 235 if (!ptr || length == 0) 236 return; 237 238 chartype* curbuf = get_str(); 239 if (!curbuf) 240 OsclError::Leave(OsclErrGeneral);//not writable. 241 242 if (offset > get_size()) 243 OsclError::Leave(OsclErrGeneral);//invalid offset. 244 245 if (length + offset > get_size()) 246 { 247 //extend length... 248 uint32 ncopy = length; 249 if (length + offset > get_maxsize()) 250 ncopy = get_maxsize() - offset;//truncate 251 oscl_strncpy(curbuf + offset, ptr, ncopy); 252 curbuf[offset+ncopy] = '\0'; 253 set_len(oscl_strlen(curbuf)); 254 } 255 else 256 { 257 //write within current length 258 oscl_strncpy(curbuf + offset, ptr, length); 259 } 260 } 261 262 OSCL_EXPORT_REF void OSCL_wString::write(uint32 offset, uint32 length, const chartype* ptr) 263 //default implementation-- may be replaced by derived class 264 { 265 if (!ptr || length == 0) 266 return; 267 268 chartype* curbuf = get_str(); 269 if (!curbuf) 270 OsclError::Leave(OsclErrGeneral);//not writable. 271 272 if (offset > get_size()) 273 OsclError::Leave(OsclErrGeneral);//invalid offset. 274 275 if (length + offset > get_size()) 276 { 277 //extend length... 278 uint32 ncopy = length; 279 if (length + offset > get_maxsize()) 280 ncopy = get_maxsize() - offset;//truncate 281 oscl_strncpy(curbuf + offset, ptr, ncopy); 282 curbuf[offset+ncopy] = '\0'; 283 set_len(oscl_strlen(curbuf)); 284 } 285 else 286 { 287 //write within current length 288 oscl_strncpy(curbuf + offset, ptr, length); 289 } 290 } 291 292 // ************************************************************** 293 OSCL_EXPORT_REF int8 OSCL_String::hash() const 294 //default implementation-- may be replaced by derived class 295 { 296 if (!get_str()) 297 OsclError::Leave(OsclErrGeneral);//not writable. 298 299 uint32 h = 0; 300 int8 uc = 0; 301 uint32 ii; 302 chartype* ptr; 303 304 for (ii = 0, ptr = get_str() ; ii < get_size(); ++ii, ++ptr) 305 { 306 h = 5 * h + *ptr; 307 } 308 309 for (ii = 0; ii < 4; ++ii) 310 { 311 uc ^= h & 0xFF; 312 h >>= 8; 313 } 314 315 return (uc); 316 } 317 318 OSCL_EXPORT_REF int8 OSCL_wString::hash() const 319 //default implementation-- may be replaced by derived class 320 { 321 if (!get_str()) 322 OsclError::Leave(OsclErrGeneral);//not writable. 323 324 uint32 h = 0; 325 int8 uc = 0; 326 uint32 ii; 327 chartype* ptr; 328 329 for (ii = 0, ptr = get_str() ; ii < get_size(); ++ii, ++ptr) 330 { 331 h = 5 * h + *ptr; 332 } 333 334 for (ii = 0; ii < 4; ++ii) 335 { 336 uc ^= h & 0xFF; 337 h >>= 8; 338 } 339 340 return (uc); 341 } 342 343 // ************************************************************** 344 OSCL_EXPORT_REF OSCL_String& OSCL_String::operator=(const OSCL_String & src) 345 { 346 set_rep(src); 347 return *this; 348 } 349 350 OSCL_EXPORT_REF OSCL_wString& OSCL_wString::operator=(const OSCL_wString & src) 351 { 352 set_rep(src); 353 return *this; 354 } 355 356 // ************************************************************** 357 OSCL_EXPORT_REF OSCL_String& OSCL_String::operator=(const chartype * cp) 358 { 359 set_rep(cp); 360 return *this; 361 } 362 363 OSCL_EXPORT_REF OSCL_wString& OSCL_wString::operator=(const chartype * cp) 364 { 365 set_rep(cp); 366 return *this; 367 } 368 369 // ************************************************************** 370 OSCL_EXPORT_REF OSCL_String& OSCL_String::operator+=(const OSCL_String & src) 371 { 372 append_rep(src); 373 return (*this); 374 } 375 376 OSCL_EXPORT_REF OSCL_wString& OSCL_wString::operator+=(const OSCL_wString & src) 377 { 378 append_rep(src); 379 return (*this); 380 } 381 382 // ************************************************************** 383 OSCL_EXPORT_REF OSCL_String& OSCL_String::operator+=(const chartype c) 384 { 385 chartype tmp_str[2]; 386 tmp_str[0] = c; 387 tmp_str[1] = '\0'; 388 append_rep(&tmp_str[0]); 389 return (*this); 390 } 391 392 OSCL_EXPORT_REF OSCL_wString& OSCL_wString::operator+=(const chartype c) 393 { 394 chartype tmp_str[2]; 395 tmp_str[0] = c; 396 tmp_str[1] = '\0'; 397 append_rep(&tmp_str[0]); 398 return (*this); 399 } 400 401 // ************************************************************** 402 OSCL_EXPORT_REF OSCL_String& OSCL_String::operator+=(const chartype * cp) 403 { 404 append_rep(cp); 405 return (*this); 406 } 407 408 OSCL_EXPORT_REF OSCL_wString& OSCL_wString::operator+=(const chartype * cp) 409 { 410 append_rep(cp); 411 return (*this); 412 } 413 414 415 416 417 418 419