1*38268e38SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*38268e38SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*38268e38SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*38268e38SAndrew Rist * distributed with this work for additional information 6*38268e38SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*38268e38SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*38268e38SAndrew Rist * "License"); you may not use this file except in compliance 9*38268e38SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*38268e38SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*38268e38SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*38268e38SAndrew Rist * software distributed under the License is distributed on an 15*38268e38SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*38268e38SAndrew Rist * KIND, either express or implied. See the License for the 17*38268e38SAndrew Rist * specific language governing permissions and limitations 18*38268e38SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*38268e38SAndrew Rist *************************************************************/ 21*38268e38SAndrew Rist 22*38268e38SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef SOLTOOLS_SIMSTR_HXX 25cdf0e10cSrcweir #define SOLTOOLS_SIMSTR_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir 28cdf0e10cSrcweir class Simstr /// Simple string class. 29cdf0e10cSrcweir { 30cdf0e10cSrcweir // INTERFACE 31cdf0e10cSrcweir public: 32cdf0e10cSrcweir // LIFECYCLE 33cdf0e10cSrcweir Simstr( 34cdf0e10cSrcweir const char * str = 0); 35cdf0e10cSrcweir Simstr( /** Creates Simstr out of a copy of the first 36cdf0e10cSrcweir 'nrOfBytes' bytes of 'anyBytes'. 37cdf0e10cSrcweir Adds a '\0' at the end. */ 38cdf0e10cSrcweir const char * anybytes, 39cdf0e10cSrcweir int nrOfBytes); 40cdf0e10cSrcweir Simstr( /** Creates Simstr out of a copy of the described bytes within 'anyBytes'. 41cdf0e10cSrcweir Adds a '\0' at the end. */ 42cdf0e10cSrcweir const char * anybytes, 43cdf0e10cSrcweir int firstBytesPos, 44cdf0e10cSrcweir int nrOfBytes ); 45cdf0e10cSrcweir Simstr( /// Creates Simstr of 'anzahl' times 'c'. 46cdf0e10cSrcweir char c, 47cdf0e10cSrcweir int anzahl); 48cdf0e10cSrcweir Simstr( 49cdf0e10cSrcweir const Simstr & S); 50cdf0e10cSrcweir ~Simstr(); 51cdf0e10cSrcweir 52cdf0e10cSrcweir 53cdf0e10cSrcweir // OPERATORS 54cdf0e10cSrcweir operator const char*() const; 55cdf0e10cSrcweir 56cdf0e10cSrcweir Simstr & operator=( 57cdf0e10cSrcweir const Simstr & S ); 58cdf0e10cSrcweir 59cdf0e10cSrcweir Simstr operator+( 60cdf0e10cSrcweir const Simstr & S ) const; 61cdf0e10cSrcweir Simstr & operator+=( 62cdf0e10cSrcweir const Simstr & S ); 63cdf0e10cSrcweir Simstr & operator+=( 64cdf0e10cSrcweir const char * s ); 65cdf0e10cSrcweir 66cdf0e10cSrcweir bool operator==( 67cdf0e10cSrcweir const Simstr & S ) const; 68cdf0e10cSrcweir bool operator!=( 69cdf0e10cSrcweir const Simstr & S ) const; 70cdf0e10cSrcweir bool operator<( 71cdf0e10cSrcweir const Simstr & S ) const; 72cdf0e10cSrcweir bool operator>( 73cdf0e10cSrcweir const Simstr & S ) const; 74cdf0e10cSrcweir bool operator<=( 75cdf0e10cSrcweir const Simstr & S ) const; 76cdf0e10cSrcweir bool operator>=( 77cdf0e10cSrcweir const Simstr & S ) const; 78cdf0e10cSrcweir // INFO 79cdf0e10cSrcweir static const Simstr & 80cdf0e10cSrcweir null_(); 81cdf0e10cSrcweir 82cdf0e10cSrcweir const char * str() const; 83cdf0e10cSrcweir int l() const; // Length of string without '\0' at end. 84cdf0e10cSrcweir char * s(); // ATTENTION !!! // Only to be used, when a function needs a 'char*' but 85cdf0e10cSrcweir // nevertheless THAT WILL BE NOT CHANGED! 86cdf0e10cSrcweir // Typecasts to 'const char*' are performed automatically. 87cdf0e10cSrcweir char get( 88cdf0e10cSrcweir int n) const; 89cdf0e10cSrcweir char get_front() const; 90cdf0e10cSrcweir char get_back() const; 91cdf0e10cSrcweir Simstr get( 92cdf0e10cSrcweir int startPos, 93cdf0e10cSrcweir int anzahl ) const; 94cdf0e10cSrcweir Simstr get_front( 95cdf0e10cSrcweir int anzahl ) const; 96cdf0e10cSrcweir Simstr get_back( 97cdf0e10cSrcweir int anzahl ) const; 98cdf0e10cSrcweir 99cdf0e10cSrcweir int pos_first( 100cdf0e10cSrcweir char c ) const; 101cdf0e10cSrcweir int pos_first_after( 102cdf0e10cSrcweir char c, 103cdf0e10cSrcweir int startSearchPos ) const; 104cdf0e10cSrcweir int pos_last( 105cdf0e10cSrcweir char c ) const; 106cdf0e10cSrcweir int pos_first( 107cdf0e10cSrcweir const Simstr & S ) const; 108cdf0e10cSrcweir int pos_last( 109cdf0e10cSrcweir const Simstr & S ) const; 110cdf0e10cSrcweir int count( 111cdf0e10cSrcweir char c ) const; 112cdf0e10cSrcweir bool is_empty() const; // Only true if object == "". 113cdf0e10cSrcweir bool is_no_text() const; // String may contain spaces or tabs. 114cdf0e10cSrcweir 115cdf0e10cSrcweir Simstr get_first_token( 116cdf0e10cSrcweir char c ) const; 117cdf0e10cSrcweir Simstr get_last_token( 118cdf0e10cSrcweir char c ) const; 119cdf0e10cSrcweir 120cdf0e10cSrcweir // ACCESS 121cdf0e10cSrcweir char & ch( /** Reference to sz[n]. Allows change of this char. 122cdf0e10cSrcweir !!! No safety, if n is out of the allowed range! */ 123cdf0e10cSrcweir int n ); 124cdf0e10cSrcweir 125cdf0e10cSrcweir // OPERATIONS 126cdf0e10cSrcweir void insert( 127cdf0e10cSrcweir int pos, 128cdf0e10cSrcweir char c ); 129cdf0e10cSrcweir void push_front( 130cdf0e10cSrcweir char c ); 131cdf0e10cSrcweir void push_back( 132cdf0e10cSrcweir char c ); 133cdf0e10cSrcweir void insert( 134cdf0e10cSrcweir int pos, 135cdf0e10cSrcweir const Simstr & S ); 136cdf0e10cSrcweir void push_front( 137cdf0e10cSrcweir const Simstr & S ); 138cdf0e10cSrcweir void push_back( 139cdf0e10cSrcweir const Simstr & S ); 140cdf0e10cSrcweir 141cdf0e10cSrcweir void remove( 142cdf0e10cSrcweir int pos, 143cdf0e10cSrcweir int anzahl = 1 ); 144cdf0e10cSrcweir void remove_trailing_blanks(); 145cdf0e10cSrcweir void pop_front( 146cdf0e10cSrcweir int anzahl = 1 ); 147cdf0e10cSrcweir void pop_back( 148cdf0e10cSrcweir int anzahl = 1 ); 149cdf0e10cSrcweir void rem_back_from( 150cdf0e10cSrcweir int removeStartPos ); 151cdf0e10cSrcweir void remove_all( 152cdf0e10cSrcweir char c ); 153cdf0e10cSrcweir void remove_all( // Starts search left. 154cdf0e10cSrcweir const Simstr & S ); 155cdf0e10cSrcweir void strip( 156cdf0e10cSrcweir char c ); // Removes all characters == c from front and back. 157cdf0e10cSrcweir // c == ' ' removes also TABs !!! 158cdf0e10cSrcweir void empty(); // Changes object to the value "". 159cdf0e10cSrcweir 160cdf0e10cSrcweir void replace( 161cdf0e10cSrcweir int pos, 162cdf0e10cSrcweir char c ); 163cdf0e10cSrcweir void replace( 164cdf0e10cSrcweir int startPos, 165cdf0e10cSrcweir int anzahl, 166cdf0e10cSrcweir const Simstr & S ); 167cdf0e10cSrcweir void replace_all( 168cdf0e10cSrcweir char oldCh, 169cdf0e10cSrcweir char newCh ); 170cdf0e10cSrcweir void replace_all( 171cdf0e10cSrcweir const Simstr & oldS, 172cdf0e10cSrcweir const Simstr & newS ); 173cdf0e10cSrcweir void to_lower(); 174cdf0e10cSrcweir 175cdf0e10cSrcweir Simstr take_first_token( /// Token is removed from the Simstr. 176cdf0e10cSrcweir char c ); 177cdf0e10cSrcweir Simstr take_last_token( /// Token is removed from the Simstr. 178cdf0e10cSrcweir char c ); 179cdf0e10cSrcweir private: 180cdf0e10cSrcweir // DATA 181cdf0e10cSrcweir char * sz; 182cdf0e10cSrcweir int len; 183cdf0e10cSrcweir }; 184cdf0e10cSrcweir 185cdf0e10cSrcweir // Simstr - char* / char - concatenations 186cdf0e10cSrcweir Simstr operator+(const char * str, const Simstr & S); 187cdf0e10cSrcweir Simstr operator+(const Simstr & S, const char * str); 188cdf0e10cSrcweir Simstr operator+(char c, const Simstr & S); 189cdf0e10cSrcweir Simstr operator+(const Simstr & S, char c); 190cdf0e10cSrcweir 191cdf0e10cSrcweir // Simstr - char* - comparison operators 192cdf0e10cSrcweir bool operator==(const Simstr & S, const char * str); 193cdf0e10cSrcweir bool operator!=(const Simstr & S, const char * str); 194cdf0e10cSrcweir bool operator<(const Simstr & S, const char * str); 195cdf0e10cSrcweir bool operator>(const Simstr & S, const char * str); 196cdf0e10cSrcweir bool operator<=(const Simstr & S, const char * str); 197cdf0e10cSrcweir bool operator>=(const Simstr & S, const char * str); 198cdf0e10cSrcweir bool operator==(const char * str, const Simstr & S); 199cdf0e10cSrcweir bool operator!=(const char * str, const Simstr & S); 200cdf0e10cSrcweir bool operator<(const char * str, const Simstr & S); 201cdf0e10cSrcweir bool operator>(const char * str, const Simstr & S); 202cdf0e10cSrcweir bool operator<=(const char * str, const Simstr & S); 203cdf0e10cSrcweir bool operator>=(const char * str, const Simstr & S); 204cdf0e10cSrcweir 205cdf0e10cSrcweir 206cdf0e10cSrcweir inline const char * 207cdf0e10cSrcweir Simstr::str() const { return sz; } 208cdf0e10cSrcweir inline char * 209cdf0e10cSrcweir Simstr::s() { return sz; } 210cdf0e10cSrcweir inline int 211cdf0e10cSrcweir Simstr::l() const { return len; } 212cdf0e10cSrcweir inline 213cdf0e10cSrcweir Simstr::operator const char*() const { return sz; } 214cdf0e10cSrcweir inline bool 215cdf0e10cSrcweir Simstr::is_empty() const { return len == 0; } 216cdf0e10cSrcweir 217cdf0e10cSrcweir 218cdf0e10cSrcweir #endif 219cdf0e10cSrcweir 220