casa
$Rev:20696$
|
00001 //# String.h: String class 00002 //# Copyright (C) 2001,2002,2003 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: String.h 21285 2012-11-14 15:36:59Z gervandiepen $ 00027 00028 #ifndef CASA_STRING_H 00029 #define CASA_STRING_H 00030 00031 //# Includes 00032 #include <casa/aips.h> 00033 00034 //# Includes 00035 #include <string> 00036 00037 using std::string; 00038 00039 #include <casa/iosstrfwd.h> 00040 #include <casa/sstream.h> 00041 00042 namespace casa { //# NAMESPACE CASA - BEGIN 00043 00044 //# Forward Declarations 00045 class String; 00046 class RegexBase; 00047 00048 // <summary> SubString help class to be used in at, before, ... </summary> 00049 // <synopsis> 00050 // The SubString class can only be used by the String class to be able to 00051 // operate the aips++ defined replacement operators at, before, after, through, 00052 // from. The class is used transparently in operations like: 00053 // <srcblock> 00054 // string.at(2,3) = "five"; 00055 // </srcblock> 00056 // If the SubString starts at a position outside the length of the 00057 // original string (like e.g. in after(1000000)), a zero length string is 00058 // created (not an exception thrown like in standard string operations). 00059 // </synopsis> 00060 00061 class SubString { 00062 public: 00063 //# Friends 00064 friend class String; 00065 // Make a string 00066 operator const string() const { return string(ref_p, pos_p, len_p); } 00067 // Assignment 00068 // <group> 00069 SubString &operator=(const SubString &str); 00070 SubString &operator=(const String &str); 00071 SubString &operator=(const Char *s); 00072 SubString &operator=(const Char c); 00073 // </group> 00074 // Get as (const) C array 00075 const Char *chars() const; 00076 // Obtain length 00077 string::size_type length() const { return len_p; } 00078 00079 private: 00080 //# Constructors 00081 // Constructor (there are no public constructors) 00082 SubString(const string &str, string::size_type pos, 00083 string::size_type len); 00084 //# Data 00085 // Referenced string 00086 const string &ref_p; 00087 // Start of sub-string 00088 string::size_type pos_p; 00089 // Length of sub-string 00090 string::size_type len_p; 00091 }; 00092 00093 // <summary> 00094 // String: the storage and methods of handling collections of characters. 00095 // </summary> 00096 00097 // <use visibility=export> 00098 00099 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tString.cc" demos=""> 00100 // </reviewed> 00101 00102 // <prerequisite> 00103 // <li> RegexBase - the regular expressions class 00104 // <li> the std string class 00105 // </prerequisite> 00106 // 00107 // <etymology> 00108 // The String class name is a continuation of the "C" language custom of 00109 // refering to collections of characters as "strings of characters". 00110 // </etymology> 00111 // 00112 // <synopsis> 00113 // The String class is the aips++ implementation of a string class. It is 00114 // closely based on the standard library string class, and all operations 00115 // and behaviour of strings as defined in the standard are available for 00116 // a String. The only difference is the extension with additional functions 00117 // in the aips++ String class as compared to the standard string class. 00118 // 00119 // The String class may be instantiated in many ways: 00120 // <ol> 00121 // <li> A single character - <src>String myChar('C');</src> 00122 // <li> A Char* argument - <src>String myWord("Yowza");</src> 00123 // <li> The first n chararcters of a pre-existing string - 00124 // <src>String myFoo("fooey", 3);</src> 00125 // </ol> As well as the copy and default constructors and iterator based ones. 00126 // 00127 // A String may be concatinated with another object (String, or 00128 // char*) with either prepending or postpending. A search for the position 00129 // of a character within a String may return its position, a Bool that it 00130 // is contained within or a Bool confirming your guess at the character's 00131 // position is correct. A check of the frequency of occurance of a string 00132 // within a String will return the number of occurances. 00133 // 00134 // Strings may be extracted from Strings at, before, through, from and 00135 // after a starting position within the String. Deletion of characters is 00136 // possible after a given position within the String. Global substitution 00137 // of characters within a String is provided, as well. Splitting of Strings 00138 // into a carray of Strings is possible, based upon a given separator 00139 // character, with a return value of the number of elements split. The joining 00140 // together of the elements of an array of Strings into one String is possible. 00141 // 00142 // Finally, transformations of case and conversions of type are provided. 00143 // 00144 // The standard string class provides the following functionality: 00145 // <ol> 00146 // <li> Construction from (part of) String, (part of) Char*, 00147 // (repeating) Char, iterator pair. 00148 // <li> Assignment from String, Char*, Char 00149 // <li> Iterators: begin() and end(); rbegin() and rend() (Note: gcc reverse 00150 // iterators still weak) 00151 // <li> Capacity: size, length, max_size, resize, capacity, reserve, clear, 00152 // empty 00153 // <li> Special size: String::size_type, with indicator: String::npos 00154 // <li> Element access: [pos] and at(pos) (both const and non-const) 00155 // <li> Modifiers: += of String, Char*, Char; append of (part of) String, 00156 // Char*, Char and iterator defined; assign() of (part of) 00157 // String, Char* and (repeating) Char and iterator; 00158 // insertion of same; replacing of same; erase of part of 00159 // String; a copy and a swap. 00160 // <li> C-string: get Char* with c_str() or data() and get the relevant 00161 // Allocator used (Note: not fully supported in gcc) 00162 // <li> Operations: find, rfind, find_first_of, find_last_of, find_first_not_of, 00163 // find_last_not_of; substr (Note only readable substring); 00164 // compare with (part of) String, Char* 00165 // <li> Globals: Addition operators for String, Char*, Char; all comparison 00166 // operators for String and Char*; getline; input and output 00167 // stream operators 00168 // <li> Typedef: All relevant typedefs for standard containers and iterator 00169 // handling 00170 // </ol> 00171 // The aips++ addition are: 00172 // <ol> 00173 // <li> To standard: some Char function arguments where appropriate; RegexBase 00174 // arguments in search like methods. 00175 // <li> Substring additions: at, before, after, from, through functions taking 00176 // search String, Char* as arguments can give (hidden) substrings 00177 // which can be assigned (as in <src> at(1,2) = ";"</src>) 00178 // <li> Methods: prepend (in addition to standard append); del (as erase); 00179 // global substitution of String and patterns; 00180 // freq (count of occurance); split/join of strings at separator 00181 // or pattern; upcase, downcase, reverse; 00182 // common_suffix and _prefix; replicate; case insensitive 00183 // compare; creation from stream 00184 // </ol> 00185 00186 // </synopsis> 00187 // 00188 // <example> 00189 // <srcblock> 00190 // // Let's start with a simple string. 00191 // String myString("the time"); 00192 // // add some more on the end... 00193 // myString += " for all good men"; 00194 // // prepend some on the front... 00195 // myString.prepend("Now is "); 00196 // // do some concatination... 00197 // String evenMore; 00198 // evenMore += myString + " to come to"; 00199 // // do some three way concatination 00200 // String allKeys, finishIt(" their country."); 00201 // allKeys = evenMore + "the aid of" + finishIt; 00202 // // find the spot where we put something earlier 00203 // String::size_type position = allKeys.index(finishIt); 00204 // // find if the word is in the String... 00205 // Bool query = myString.contains("good men"); 00206 // // ask if the position we think is true is correct... 00207 // Bool answer = allKeys.matches(finishIt, position); 00208 // // How many spaces are in our phrase? 00209 // Int spacesCount = allKeys.freq(" "); 00210 // </srcblock> 00211 // </example> 00212 // 00213 // <motivation> 00214 // The String class eases the handling of characters within the AIPS++ 00215 // environment. 00216 // </motivation> 00217 // 00218 // <todo asof=2000/12/05"> 00219 // <li> if old string disappeared; remove the alloc() call. 00220 // <li> add more tests (for string methods) when old String disappears 00221 // </todo> 00222 00223 class String : public string { 00224 00225 public: 00226 00227 //# Basic container typedefs 00228 typedef string::traits_type traits_type; 00229 typedef string::value_type value_type; 00230 typedef string::allocator_type allocator_type; 00231 typedef string::size_type size_type; 00232 typedef string::difference_type difference_type; 00233 00234 typedef string::reference reference; 00235 typedef string::const_reference const_reference; 00236 typedef string::pointer pointer; 00237 typedef string::const_pointer const_pointer; 00238 00239 typedef string::iterator iterator; 00240 typedef string::const_iterator const_iterator; 00241 typedef string::reverse_iterator reverse_iterator; 00242 typedef string::const_reverse_iterator const_reverse_iterator; 00243 //# Next cast necessary to stop warning in gcc 00244 static const size_type npos = static_cast<size_type>(-1); 00245 00246 //# Constructors 00247 // Default constructor 00248 String() : string("") {} 00249 // Construct from std string 00250 // Construct from (part of) other string: acts as copy constructor 00251 // <thrown> 00252 // <li> out_of_range if pos > str.size() 00253 // </thrown> 00254 String(const string& str, size_type pos=0, size_type n=npos) : 00255 string(str, pos, n) {} 00256 // Construct from char* with given length 00257 // <thrown> 00258 // <li> length_error if n == npos 00259 // </thrown> 00260 String(const Char* s, size_type n) : string(s, n) {} 00261 // Construct from char array 00262 String(const Char* s) : string(s) {} 00263 // Construct from a single char (repeated n times) 00264 // <thrown> 00265 // <li> length_error if n == npos 00266 // </thrown> 00267 String(size_type n, Char c) : string(n, c) {} 00268 // Construct from iterator 00269 template<class InputIterator> 00270 String(InputIterator begin, InputIterator end) : string(begin, end) {} 00271 // From single char (** aips++ addition). 00272 // <note role=warning> Note that there is no automatic Char-to-String 00273 // conversion available. This stops inadvertent conversions of 00274 // integer to string. </note> 00275 explicit String(Char c) : string(1, c) {} 00276 // Construct from a SubString 00277 String(const SubString &str) : string(str.ref_p, str.pos_p, str.len_p) {} 00278 // Construct from a stream. 00279 String(ostringstream &os); 00280 00281 //# Destructor 00282 // Destructor 00283 ~String() {} 00284 00285 //# Operators 00286 // Assignments (they are all deep copies according to standard) 00287 // <group> 00288 String& operator=(const string& str) { 00289 return static_cast<String&>(string::operator=(str)); } 00290 String& operator=(const SubString &str) { 00291 return (*this = String(str)); } 00292 String& operator=(const Char* s) { 00293 return static_cast<String&>(string::operator=(s)); } 00294 String& operator=(Char c) { 00295 return static_cast<String&>(string::operator=(c)); } 00296 // </group> 00297 // ** aips++ addition: synonym for at(pos, len) 00298 SubString operator()(size_type pos, size_type len); 00299 // Concatenate 00300 // <group> 00301 String& operator+=(const string& str) { 00302 return static_cast<String&>(string::operator+=(str)); } 00303 String& operator+=(const Char* s) { 00304 return static_cast<String&>(string::operator+=(s)); } 00305 String& operator+=(Char c) { 00306 return static_cast<String&>(string::operator+=(c)); } 00307 // </group> 00308 00309 // Indexing. The standard version is undefined if <src>pos > size()</src>, or 00310 // <src>pos >= size()</src> for non-const version. 00311 // <note role=warning> The const_reference version needs the at() version 00312 // for the gcc compiler: no const [] exists. </note> 00313 // <group> 00314 const_reference operator[](size_type pos) const { 00315 return string::at(pos); } 00316 reference operator[](size_type pos) { 00317 return string::operator[](pos); } 00318 // *** aips++ addition 00319 // <group> 00320 const_reference elem(size_type pos) const { 00321 return string::at(pos); } 00322 Char firstchar() const { return at(static_cast<size_type>(0)); } 00323 Char lastchar() const { return at(length()-1); } 00324 // </group> 00325 // </group> 00326 00327 //# Member functions 00328 // Iterators 00329 // <group> 00330 iterator begin() { return string::begin(); } 00331 const_iterator begin() const { return string::begin(); } 00332 iterator end() { return string::end(); } 00333 const_iterator end() const { return string::end(); } 00334 reverse_iterator rbegin() { return string::rbegin(); } 00335 const_reverse_iterator rbegin() const { return string::rbegin(); } 00336 reverse_iterator rend() { return string::rend(); } 00337 const_reverse_iterator rend() const { return string::rend(); } 00338 // </group> 00339 00340 // Capacity, size 00341 // <group> 00342 size_type size() const { return string::size(); } 00343 size_type length() const { return string::length(); } 00344 size_type max_size() const { return string::max_size(); } 00345 size_type capacity() const { return string::capacity(); } 00346 // ** aips++ addition -- works as a capacity(n) -- Note Int 00347 Int allocation() const { return string::capacity(); } 00348 // </group> 00349 00350 // Resize by truncating or extending with copies of <src>c</src> (default 00351 // Char()) 00352 // <thrown> 00353 // <li> length_error if n > max_size() 00354 // <li> length_error if res_arg > max_size() 00355 // </thrown> 00356 // <group> 00357 // <note role=tip> The reserve length given is non-binding on the 00358 // implementation </note> 00359 String& resize(size_type n) { 00360 string::resize(n); return *this; } 00361 String& resize(size_type n, Char c) { 00362 string::resize(n, c); return *this; } 00363 String& reserve(size_type res_arg = 0) { 00364 string::reserve(res_arg); return *this; } 00365 // ** aips++ addition -- works as a resize(n) 00366 void alloc(size_type n) { string::resize(n); } 00367 // </group> 00368 00369 // Clear the string 00370 // <note role=warning> clear() executed as erase() due to missing clear() in 00371 // gcc </note> 00372 void clear() { string::erase(begin(), end()); } 00373 00374 // Test for empty 00375 Bool empty() const { return string::empty(); } 00376 00377 // Addressing 00378 // <thrown> 00379 // <li> out_of_range if pos >= size() 00380 // </thrown> 00381 // <group> 00382 const_reference at(size_type n) const { return string::at(n); } 00383 reference at(size_type n) { return string::at(n); } 00384 // </group> 00385 00386 // Append 00387 // <thrown> 00388 // <li> out_of_range if pos > str.size() 00389 // <li> length_error if new size() >= npos 00390 // </thrown> 00391 // <note role=warning> The standard has a 00392 // <src>void push_back(const Char) </src> which is completely undefined. It 00393 // probably is a remnant of the full list of container functions pop/push 00394 // back/front. </note> 00395 // <group> 00396 String& append(const string& str) { 00397 return static_cast<String&>(string::append(str)); } 00398 String& append(const string& str, size_type pos, size_type n) { 00399 return static_cast<String&>(string::append(str, pos, n)); } 00400 String& append(const Char* s, size_type n) { 00401 return static_cast<String&>(string::append(s, n)); } 00402 String& append(const Char* s) { 00403 return static_cast<String&>(string::append(s)); } 00404 String& append(size_type n, Char c) { 00405 return static_cast<String&>(string::append(n, c)); } 00406 template<class InputIterator> 00407 String& append(InputIterator first, InputIterator last) { 00408 return static_cast<String&>(string::append(first, last)); } 00409 // ** aips++ addition 00410 String& append(Char c) { 00411 return static_cast<String&>(string::append(1, c)); } 00412 // </group> 00413 00414 // Assign 00415 // <thrown> 00416 // <li> out_of_range if pos > str.size() 00417 // </thrown> 00418 // <group> 00419 String& assign(const string& str) { 00420 return static_cast<String&>(string::assign(str)); } 00421 String& assign(const string& str, size_type pos, size_type n) { 00422 return static_cast<String&>(string::assign(str, pos, n)); } 00423 String& assign(const Char* s, size_type n) { 00424 return static_cast<String&>(string::assign(s, n)); } 00425 String& assign(const Char* s) { 00426 return static_cast<String&>(string::assign(s)); } 00427 String& assign(size_type n, Char c) { 00428 return static_cast<String&>(string::assign(n, c)); } 00429 template<class InputIterator> 00430 String& assign(InputIterator first, InputIterator last) { 00431 return static_cast<String&>(string::assign(first, last)); } 00432 // ** aips++ addition 00433 String& assign(Char c) { 00434 return static_cast<String&>(string::assign(1, c)); } 00435 // </group> 00436 00437 // Insert 00438 // <thrown> 00439 // <li> out_of_range if pos1 > str.size() or pos2 > str.size() 00440 // <li> length_error if new size() >= npos 00441 // </thrown> 00442 // <group> 00443 String& insert(size_type pos1, const string& str) { 00444 return static_cast<String&>(string::insert(pos1, str)); } 00445 String& insert(size_type pos1, const string& str, 00446 size_type pos2, size_type n) { 00447 return static_cast<String&>(string::insert(pos1, str, pos2, n)); } 00448 String& insert(size_type pos, const Char* s, size_type n) { 00449 return static_cast<String&>(string::insert(pos, s, n)); } 00450 String& insert(size_type pos, const Char* s) { 00451 return static_cast<String&>(string::insert(pos, s)); } 00452 String& insert(size_type pos, size_type n, Char c) { 00453 return static_cast<String&>(string::insert(pos, n, c)); } 00454 // ** aips++ addition 00455 String& insert(size_type pos, Char c) { 00456 return static_cast<String&>(string::insert(pos, 1, c)); } 00457 00458 iterator insert(iterator p, Char c) { 00459 return string::insert(p, c); } 00460 void insert(iterator p, size_type n, Char c) { 00461 string::insert(p, n, c); } 00462 template<class InputIterator> 00463 void insert(iterator p, InputIterator first, InputIterator last) { 00464 string::insert(p, first, last); } 00465 // ** aips++ additions 00466 // <group> 00467 String& insert(iterator p, const string& str) { 00468 return static_cast<String&>(string::insert(p-begin(), str)); } 00469 String& insert(iterator p, const Char* s, size_type n) { 00470 return static_cast<String&>(string::insert(p-begin(), s, n)); } 00471 String& insert(iterator p, const Char* s) { 00472 return static_cast<String&>(string::insert(p-begin(), s)); } 00473 // </group> 00474 // </group> 00475 00476 // Compare. Returns 0 if strings equal and of equal size; else positive if 00477 // str larger or longer; else negative. 00478 // <note role=warning> The gcc compiler does not have the proper standard 00479 // compare functions. Hence they are locally implemented. </note> 00480 // <group> 00481 Int compare(const string& str) const { 00482 return string::compare(str); } 00483 Int compare(size_type pos1, size_type n1, const string& str) const { 00484 return String(*this, pos1, n1).compare(str); } 00485 Int compare(size_type pos1, size_type n1, const string& str, 00486 size_type pos2, size_type n2) const { 00487 return String(*this, pos1, n1).compare(String(str, pos2, n2)); } 00488 Int compare(const Char* s) const { 00489 return string::compare(s); } 00490 Int compare(size_type pos1, size_type n1, const Char* s, 00491 size_type n2=npos) const { 00492 return String(*this, pos1, n1).compare(String(s, n2)); } 00493 // </group> 00494 00495 // Erase 00496 // <group> 00497 String& erase(size_type pos, size_type n = npos) { 00498 return static_cast<String&>(string::erase(pos, n)); } 00499 iterator erase(iterator position) { 00500 return string::erase(position); } 00501 iterator erase(iterator first, iterator last) { 00502 return string::erase(first, last); } 00503 // </group> 00504 00505 // Replace 00506 // <thrown> 00507 // <li> out_of_range if pos1 > str.size() or pos2 > str.size() 00508 // <li> length_error if new size() > npos 00509 // </thrown> 00510 // <group> 00511 String& replace(size_type pos1, size_type n1, const string& str) { 00512 return static_cast<String&>(string::replace(pos1, n1, str)); } 00513 String& replace(size_type pos1, size_type n1, const string& str, 00514 size_type pos2, size_type n2) { 00515 return static_cast<String&>(string::replace(pos1, n1, str, pos2, n2)); } 00516 String& replace(size_type pos, size_type n1, const Char* s, size_type n2) { 00517 return static_cast<String&>(string::replace(pos, n1, s, n2)); } 00518 String& replace(size_type pos, size_type n1, const Char* s) { 00519 return static_cast<String&>(string::replace(pos, n1, s)); } 00520 String& replace(size_type pos, size_type n1, size_type n2, Char c) { 00521 return static_cast<String&>(string::replace(pos, n1, n2, c)); } 00522 // ** aips++ addition 00523 String& replace(size_type pos, size_type n1, Char c) { 00524 return static_cast<String&>(string::replace(pos, n1, 1, c)); } 00525 String& replace(iterator i1, iterator i2, const string& str) { 00526 return static_cast<String&>(string::replace(i1, i2, str)); } 00527 String& replace(iterator i1, iterator i2, const Char* s, size_type n) { 00528 return static_cast<String&>(string::replace(i1, i2, s, n)); } 00529 String& replace(iterator i1, iterator i2, const Char* s) { 00530 return static_cast<String&>(string::replace(i1, i2, s)); } 00531 String& replace(iterator i1, iterator i2, size_type n, Char c) { 00532 return static_cast<String&>(string::replace(i1, i2, n, c)); } 00533 // ** aips++ addition 00534 String& replace(iterator i1, iterator i2, Char c) { 00535 return static_cast<String&>(string::replace(i1, i2, 1, c)); } 00536 template<class InputIterator> 00537 String& replace(iterator i1, iterator i2, InputIterator j1, 00538 InputIterator j2) { 00539 return static_cast<String&>(string::replace(i1, i2, j1, j2)); } 00540 // </group> 00541 00542 // Copy 00543 // <thrown> 00544 // <li> out_of_range if pos > size() 00545 // </thrown> 00546 size_type copy(Char* s, size_type n, size_type pos = 0) const { 00547 return string::copy(s, n, pos); } 00548 00549 // Swap 00550 void swap(string& s) { string::swap(s); } 00551 00552 // Get char array 00553 // <group> 00554 // As a proper null terminated C-string 00555 const Char *c_str() const { return string::c_str(); } 00556 // As pointer to char array 00557 const Char *data() const { return string::data(); } 00558 // ** aips++ synonym 00559 const Char *chars() const { return string::c_str(); } 00560 // </group> 00561 00562 // Get allocator used 00563 // <note role=warning> gcc has no get_allocator() </note> 00564 allocator_type get_allocator() const { return string::allocator_type(); } 00565 00566 // Get a sub string 00567 // <thrown> 00568 // <li> out_of_range if pos > size() 00569 // </thrown> 00570 String substr(size_type pos=0, size_type n=npos) const { 00571 return String(*this, pos, n); } 00572 00573 00574 // Convert a string to a Double. If the specified string does not represent 00575 // a Double, 0 is returned. 00576 static Double toDouble(const String& string); 00577 00578 // Convert a string to a Float. If the specified string does not represent 00579 // a Float, 0 is returned. 00580 static Float toFloat(const String& string); 00581 00582 // Convert a string to an Int. If the specified string does not represent 00583 // an Int, 0 is returned. 00584 static Int toInt(const String& string); 00585 00586 // Create a formatted string using the given printf format string. 00587 static String format (const char* picture, ...); 00588 00589 // Remove beginning and ending whitespace. 00590 void trim(); 00591 00592 // Remove specified chars from beginning and end of string. 00593 void trim(char c[], uInt n); 00594 00595 // Remove specified character from beginning of string. 00596 // If the character is repeated more than once on the left, all instances 00597 // will be removed; e.g. ltrim(',') results in ",,xy" becoming "xy". 00598 void ltrim(char c); 00599 00600 // Remove specified character from end of string. 00601 // If the character is repeated more than once on the right, all instances 00602 // will be removed; e.g. rtrim(',') results in "xy,," becoming "xy". 00603 void rtrim(char c); 00604 00605 // Does the string start with the specified string? 00606 Bool startsWith(const string& beginString) const 00607 { return find(beginString) == 0; } 00608 00609 // Search functions. Returns either npos (if not found); else position. 00610 // <note role=warning> The RegexBase ones are ** aips++ additions</note> 00611 // <group> 00612 size_type find(const string &str, size_type pos=0) const { 00613 return string::find(str, pos); } 00614 size_type find(const Char *s, size_type pos=0) const { 00615 return string::find(s, pos); } 00616 size_type find(const Char *s, size_type pos, size_type n) const { 00617 return string::find(s, pos, n); } 00618 size_type find(Char c, size_type pos=0) const { 00619 return string::find(c, pos); } 00620 size_type find(const RegexBase &r, size_type pos=0) const; 00621 size_type rfind(const string &str, size_type pos=0) const { 00622 return string::find(str, pos); } 00623 size_type rfind(const Char *s, size_type pos=0) const { 00624 return string::rfind(s, pos); } 00625 size_type rfind(const Char *s, size_type pos, size_type n) const { 00626 return string::rfind(s, pos, n); } 00627 size_type rfind(Char c, size_type pos=0) const { 00628 return string::rfind(c, pos); } 00629 size_type rfind(const RegexBase &r, size_type pos=0) const; 00630 size_type find_first_of(const string &str, size_type pos=0) const { 00631 return string::find_first_of(str, pos); } 00632 size_type find_first_of(const Char *s, size_type pos=0) const { 00633 return string::find_first_of(s, pos); } 00634 size_type find_first_of(const Char *s, size_type pos, size_type n) const { 00635 return string::find_first_of(s, pos, n); } 00636 size_type find_first_of(Char c, size_type pos=0) const { 00637 return string::find_first_of(c, pos); } 00638 size_type find_last_of(const string &str, size_type pos=npos) const { 00639 return string::find_last_of(str, pos); } 00640 size_type find_last_of(const Char *s, size_type pos=npos) const { 00641 return string::find_last_of(s, pos); } 00642 size_type find_last_of(const Char *s, size_type pos, size_type n) const { 00643 return string::find_last_of(s, pos, n); } 00644 size_type find_last_of(Char c, size_type pos=npos) const { 00645 return string::find_last_of(c, pos); } 00646 size_type find_first_not_of(const string &str, size_type pos=0) const { 00647 return string::find_first_not_of(str, pos); } 00648 size_type find_first_not_of(const Char *s, size_type pos=0) const { 00649 return string::find_first_not_of(s, pos); } 00650 size_type find_first_not_of(const Char *s, size_type pos, size_type n) const { 00651 return string::find_first_not_of(s, pos, n); } 00652 size_type find_first_not_of(Char c, size_type pos=0) const { 00653 return string::find_first_not_of(c, pos); } 00654 size_type find_last_not_of(const string &str, size_type pos=npos) const { 00655 return string::find_last_not_of(str, pos); } 00656 size_type find_last_not_of(const Char *s, size_type pos=npos) const { 00657 return string::find_last_not_of(s, pos); } 00658 size_type find_last_not_of(const Char *s, size_type pos, size_type n) const { 00659 return string::find_last_not_of(s, pos, n); } 00660 size_type find_last_not_of(Char c, size_type pos=npos) const { 00661 return string::find_last_not_of(c, pos); } 00662 // </group> 00663 00664 // Containment. ** aips++ addition 00665 // <group name=contains> 00666 Bool contains(Char c) const { 00667 return (find(c) != npos); } 00668 Bool contains(const string &str) const { 00669 return (find(str) != npos); } 00670 Bool contains(const Char *s) const { 00671 return (find(s) != npos); } 00672 Bool contains(const RegexBase &r) const; 00673 // </group> 00674 // Containment after (or before if pos negative) pos. ** aips++ addition 00675 // <group name=contains_pos> 00676 Bool contains(Char c, Int pos) const; 00677 Bool contains(const string &str, Int pos) const; 00678 Bool contains(const Char *s, Int pos) const; 00679 Bool contains(const RegexBase &r, Int pos) const; 00680 // </group> 00681 00682 // Matches entire string from pos 00683 // (or till pos if negative pos). ** aips++ addition 00684 // <group name=matches> 00685 Bool matches(const string &str, Int pos = 0) const; 00686 Bool matches(Char c, Int pos = 0) const { 00687 return matches(String(c), pos); }; 00688 Bool matches(const Char *s, Int pos = 0) const { 00689 return matches(String(s), pos); }; 00690 Bool matches(const RegexBase &r, Int pos = 0) const; 00691 // </group> 00692 00693 // Concatenate by prepending the argument onto String. ** aips++ addition 00694 // <group name=concatenation_method> 00695 void prepend(const string &str); 00696 void prepend(const Char *s); 00697 void prepend(Char c); 00698 // </group> 00699 00700 // Return the position of the target in the string or npos for failure. 00701 // ** aips++ addition 00702 // <group name=index> 00703 size_type index(Char c, Int startpos = 0) const { 00704 return ((startpos >= 0) ? find(c, startpos) : 00705 rfind(c, length() + startpos - 1)); } 00706 size_type index(const string &str, Int startpos = 0) const { 00707 return ((startpos >= 0) ? find(str, startpos) : 00708 rfind(str, length() + startpos - str.length())); } 00709 size_type index(const Char *s, Int startpos = 0) const { 00710 return ((startpos >= 0) ? find(s, startpos) : 00711 rfind(s, length() + startpos - traits_type::length(s))); } 00712 size_type index(const RegexBase &r, Int startpos = 0) const; 00713 // </group> 00714 00715 // Return the number of occurences of target in String. ** aips++ addition 00716 // <group name=freq> 00717 Int freq(Char c) const; 00718 Int freq(const string &str) const; 00719 Int freq(const Char *s) const; 00720 // </group> 00721 00722 // Extract the string "at" the argument's position. ** aips++ addition 00723 // <group name=at> 00724 SubString at(size_type pos, size_type len); 00725 String at(size_type pos, size_type len) const { 00726 return String(*this, pos, len); } 00727 SubString at(const string &str, Int startpos = 0); 00728 String at(const string &str, Int startpos = 0) const; 00729 SubString at(const Char *s, Int startpos = 0); 00730 String at(const Char *s, Int startpos = 0) const; 00731 SubString at(Char c, Int startpos = 0); 00732 String at(Char c, Int startpos = 0) const; 00733 SubString at(const RegexBase &r, Int startpos = 0); 00734 String at(const RegexBase &r, Int startpos = 0) const; 00735 // Next ones for overloading reasons. 00736 // <note role=tip> It is better to use the <src>substr()</src> method 00737 // in stead. </note> 00738 // <group> 00739 SubString at(Int pos, Int len) { 00740 return at(static_cast<size_type>(pos), static_cast<size_type>(len)); 00741 }; 00742 String at(Int pos, Int len) const { 00743 return at(static_cast<size_type>(pos), static_cast<size_type>(len)); 00744 }; 00745 SubString at(Int pos, size_type len) { 00746 return at(static_cast<size_type>(pos), len); 00747 }; 00748 String at(Int pos, size_type len) const { 00749 return at(static_cast<size_type>(pos), len); 00750 }; 00751 // </group> 00752 // </group> 00753 00754 // Start at startpos and extract the string "before" the argument's 00755 // position, exclusive. ** aips++ addition 00756 // <group name=before> 00757 SubString before(size_type pos); 00758 SubString before(const string &str, Int startpos = 0); 00759 SubString before(const Char *s, Int startpos = 0); 00760 SubString before(Char c, Int startpos = 0); 00761 SubString before(const RegexBase &r, Int startpos = 0); 00762 // Next one for overloading reasons 00763 SubString before(Int pos) { 00764 return before(static_cast<size_type>(pos)); }; 00765 // </group> 00766 00767 // Start at startpos and extract the SubString "through" to the argument's 00768 // position, inclusive. ** aips++ addition 00769 // <group name=through> 00770 SubString through(size_type pos); 00771 SubString through(const string &str, Int startpos = 0); 00772 SubString through(const Char *s, Int startpos = 0); 00773 SubString through(Char c, Int startpos = 0); 00774 SubString through(const RegexBase &r, Int startpos = 0); 00775 // Next one for overloading reasons 00776 SubString through(Int pos) { 00777 return through(static_cast<size_type>(pos)); } 00778 // </group> 00779 00780 // Start at startpos and extract the SubString "from" the argument's 00781 // position, inclusive, to the String's end. ** aips++ addition 00782 // <group name=from> 00783 SubString from(size_type pos); 00784 SubString from(const string &str, Int startpos = 0); 00785 SubString from(const Char *s, Int startpos = 0); 00786 SubString from(Char c, Int startpos = 0); 00787 SubString from(const RegexBase &r, Int startpos = 0); 00788 // Next one for overloading reasons 00789 SubString from(Int pos) { 00790 return from(static_cast<size_type>(pos)); 00791 }; 00792 // </group> 00793 00794 // Start at startpos and extract the SubString "after" the argument's 00795 // position, exclusive, to the String's end. ** aips++ addition 00796 // <group name=after> 00797 SubString after(size_type pos); 00798 SubString after(const string &str, Int startpos = 0); 00799 SubString after(const Char *s, Int startpos = 0); 00800 SubString after(Char c, Int startpos = 0); 00801 SubString after(const RegexBase &r, Int startpos = 0); 00802 // Next one for overloading reasons 00803 SubString after(Int pos) { 00804 return after(static_cast<size_type>(pos)); 00805 }; 00806 // </group> 00807 00808 // Maybe forget some. ** aips++ addition 00809 // <group> 00810 // internal transformation to reverse order of String. 00811 void reverse(); 00812 // internal transformation to capitalization of String. 00813 void capitalize(); 00814 // internal transformation to uppercase of String 00815 void upcase(); 00816 // internal transformation to lowercase of String 00817 void downcase(); 00818 // </group> 00819 00820 // Delete len chars starting at pos. ** aips++ addition 00821 void del(size_type pos, size_type len); 00822 00823 // Delete the first occurrence of target after startpos. ** aips++ addition 00824 //<group name=del_after> 00825 void del(const string &str, size_type startpos = 0); 00826 void del(const Char *s, size_type startpos = 0); 00827 void del(Char c, size_type startpos = 0); 00828 void del(const RegexBase &r, size_type startpos = 0); 00829 // Overload problem 00830 void del(Int pos, Int len) { 00831 del(static_cast<size_type>(pos), static_cast<size_type>(len)); } 00832 //</group> 00833 00834 // Global substitution: substitute all occurrences of pat with repl, and 00835 // return the number of replacements. 00836 // ** aips++ addition 00837 //<group name=gsub> 00838 Int gsub(const string &pat, const string &repl); 00839 Int gsub(const Char *pat, const string &repl); 00840 Int gsub(const Char *pat, const Char *repl); 00841 Int gsub(const RegexBase &pat, const string &repl); 00842 //</group> 00843 00844 // Convert a value to a String. 00845 // It uses a shift into an ostringstream, so that operator must be 00846 // defined for the data type used. 00847 template<typename T> 00848 static String toString(const T& value) 00849 { 00850 std::ostringstream os; 00851 os << value; 00852 return os.str(); 00853 } 00854 00855 private: 00856 // Helper functions for at, before etc 00857 // <group> 00858 SubString _substr(size_type first, size_type l) { 00859 return SubString(*this, first, l); } 00860 // </group> 00861 }; 00862 00863 // <summary> 00864 // Global concatenation operators 00865 // </summary> 00866 00867 // The global concatenation operators 00868 // <group name=concatenator> 00869 inline String operator+(const String &lhs, const String &rhs) { 00870 String str(lhs); str.append(rhs); return str; } 00871 inline String operator+(const Char *lhs, const String &rhs) { 00872 String str(lhs); str.append(rhs); return str; } 00873 inline String operator+(Char lhs, const String &rhs) { 00874 String str(lhs); str.append(rhs); return str; } 00875 inline String operator+(const String &lhs, const Char *rhs) { 00876 String str(lhs); str.append(rhs); return str; } 00877 inline String operator+(const String &lhs, Char rhs) { 00878 String str(lhs); str.append(rhs); return str; } 00879 // </group> 00880 00881 // <summary> 00882 // Global comparison operators 00883 // </summary> 00884 00885 // The global comparison operators 00886 // <group name=comparitor> 00887 inline Bool operator==(const String &x, const String &y) { 00888 return x.compare(y) == 0; } 00889 inline Bool operator!=(const String &x, const String &y) { 00890 return x.compare(y) != 0; } 00891 inline Bool operator>(const String &x, const String &y) { 00892 return x.compare(y) > 0; } 00893 inline Bool operator>=(const String &x, const String &y) { 00894 return x.compare(y) >= 0; } 00895 inline Bool operator<(const String &x, const String &y) { 00896 return x.compare(y) < 0; } 00897 inline Bool operator<=(const String &x, const String &y) { 00898 return x.compare(y) <= 0; } 00899 inline Bool operator==(const String &x, const Char *t) { 00900 return x.compare(t) == 0; } 00901 inline Bool operator!=(const String &x, const Char *t) { 00902 return x.compare(t) != 0; } 00903 inline Bool operator>(const String &x, const Char *t) { 00904 return x.compare(t) > 0; } 00905 inline Bool operator>=(const String &x, const Char *t) { 00906 return x.compare(t) >= 0; } 00907 inline Bool operator<(const String &x, const Char *t) { 00908 return x.compare(t) < 0; } 00909 inline Bool operator<=(const String &x, const Char *t) { 00910 return x.compare(t) <= 0; } 00911 inline Bool operator==(const String &x, const Char t) { 00912 return x.compare(String(t)) == 0; } 00913 inline Bool operator!=(const String &x, const Char t) { 00914 return x.compare(String(t)) != 0; } 00915 inline Bool operator>(const String &x, const Char t) { 00916 return x.compare(String(t)) > 0; } 00917 inline Bool operator>=(const String &x, const Char t) { 00918 return x.compare(String(t)) >= 0; } 00919 inline Bool operator<(const String &x, const Char t) { 00920 return x.compare(String(t)) < 0; } 00921 inline Bool operator<=(const String &x, const Char t) { 00922 return x.compare(String(t)) <= 0; } 00923 // ** aips++ additions of global compares. Returns 0 if equal; lt or gt 0 if 00924 // strings unequal or of unequal lengths. 00925 // <group> 00926 inline Int compare(const string &x, const string &y) { 00927 return x.compare(y); } 00928 inline Int compare(const string &x, const Char *y) { 00929 return x.compare(y); } 00930 inline Int compare(const string &x, const Char y) { 00931 return x.compare(String(y)); } 00932 // this version ignores case. ** aips++ addition. Result is 0 if equal 00933 // strings of equal lengths; else lt or gt 0 to indicate differences. 00934 Int fcompare(String x, String y); 00935 // </group> 00936 // </group> 00937 00938 // <summary> Splitting </summary> 00939 // Global function which splits the String into string array res at separator 00940 // and returns the number of elements. ** aips++ addition 00941 // <group name=split> 00942 Int split(const string &str, string res[], Int maxn, 00943 const string &sep); 00944 Int split(const string &str, string res[], Int maxn, 00945 const Char sep); 00946 Int split(const string &str, string res[], Int maxn, 00947 const RegexBase &sep); 00948 //</group> 00949 00950 // <summary> Some general functions </summary> 00951 // Functions to find special patterns, join and replicate 00952 // <group name=common> 00953 String common_prefix(const string &x, const string &y, 00954 Int startpos = 0); 00955 String common_suffix(const string &x, const string &y, 00956 Int startpos = -1); 00957 String replicate(Char c, String::size_type n); 00958 String replicate(const string &str, String::size_type n); 00959 String join(string src[], Int n, const string &sep); 00960 // </group> 00961 00962 // <summary> Casing and related functions </summary> 00963 // Case conversion and rearrangement functions 00964 // <group name=case> 00965 // Global function which returns a transformation to reverse order of String. 00966 String reverse(string str); 00967 // Global function which returns a transformation to uppercase of String. 00968 String upcase(string str); 00969 // Global function which returns a transformation to lowercase of String. 00970 String downcase(string str); 00971 // Global function which returns a transformation to capitalization of 00972 // String. 00973 String capitalize(string str); 00974 // </group> 00975 00976 // <summary> IO </summary> 00977 // <group name=io> 00978 // Output 00979 ostream &operator<<(ostream &s, const String &x); 00980 // </group> 00981 00982 //# Inlines 00983 inline SubString::SubString(const string &str, string::size_type pos, 00984 string::size_type len) : 00985 ref_p(str), pos_p((pos > str.length()) ? str.length() : pos), 00986 len_p((len == string::npos || pos_p+len > str.length()) ? 00987 str.length()-pos_p : len) {} 00988 00989 inline SubString String::operator()(size_type pos, size_type len) { 00990 return at(pos, len); } 00991 inline const Char *SubString::chars() const { 00992 return String(*this).c_str(); } 00993 00994 inline Bool String::contains(Char c, Int pos) const { 00995 return (index(c, pos) != npos); } 00996 inline Bool String::contains(const string &str, Int pos) const { 00997 return (index(str, pos) != npos); } 00998 inline Bool String::contains(const Char *s, Int pos) const { 00999 return (index(s, pos) != npos); } 01000 inline Bool String::contains(const RegexBase &r, Int pos) const { 01001 return (index(r, pos) != npos); } 01002 01003 inline ostream &operator<<(ostream &s, const String &x) { 01004 s << x.c_str(); return s; } 01005 01006 01007 } //# NAMESPACE CASA - END 01008 01009 #endif