Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

string.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001 The AUTHORS 
00003  *   Romeu Andre' Pieritz, Ph.D.        - romeu_pieritz@hotmail.com
00004  *   Rafael Mendes, Eng.                – mendes_rafael@yahoo.com
00005  *   Rodrigo Ferraz de Andrade, Eng.    – rferraz@iname.com 
00006  * All rights reserved.
00007  *
00008  * Permission to use, copy and distribute this software and its
00009  * documentation for educational and personal use, without fee is hereby granted, 
00010  * provided that the above copyright notice and the following
00011  * two paragraphs appear in all copies of this software.
00012  *
00013  * IN NO EVENT SHALL THE AUTHORS BE LIABLE TO ANY PARTY FOR
00014  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
00015  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHORS
00016  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00017  *
00018  * THE AUTHORS SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00019  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00020  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HERE UNDER IS
00021  * ON AN "AS IS" BASIS, AND THE AUTHORS HAVE NO OBLIGATION TO
00022  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
00023  *
00024  * SINMEC Lab. - CFD Sinflow Project - http://www.sinmec.ufsc.br/cfd
00025  */
00026 
00027 
00028 // File Define
00029 #ifndef __CSFL_SYS_CORE_STRING_H__
00030 #define __CSFL_SYS_CORE_STRING_H__
00031 
00032 
00033 // Include 
00034 #include <csfl/sys/core/types.h>
00035 
00036 
00037 // Namespace
00038 namespace csfl { 
00039 
00040         
00041 typedef unsigned short int Dimension;
00042 typedef ptrdiff_t          Subscript;  // Signed, machine-dependent int; from <stddef.h>
00043 
00044 
00048 class IString 
00049 
00050 {
00051 public:
00052         static const IString empty;
00053 
00054     // Constructors
00055     IString();                           // Null IString
00056     IString(const char*);                // From built-in IString
00057     IString(const char*, Subscript n);   // ... for at most n characters
00058     IString(char);                       // From single character
00059     IString(const IString&);             // From another IString
00060 
00061     // Assignment
00062     IString& operator=(const IString&);  // Assign from IString
00063     IString& operator=(const char*);     // ... from built-in IString
00064     IString& operator=(char);            // ... from single character
00065 
00066 
00067     // Destructor
00068     ~IString();
00069 
00070     // Indexing
00071     char  operator[](Subscript i) const;
00072     char& operator[](Subscript i);
00073     char  operator()(Subscript i) const;
00074     char& operator()(Subscript i);
00075 
00076 
00077     // Concatenation
00078     IString& operator+=(const IString& rhs);
00079     friend IString operator+(const IString& lhs, const IString& rhs);
00080 
00081 
00082     // Comparisons
00083     friend bool operator==(const IString&, const IString&);
00084     friend bool operator!=(const IString&, const IString&);
00085     friend bool operator< (const IString&, const IString&);
00086     friend bool operator<=(const IString&, const IString&);
00087     friend bool operator> (const IString&, const IString&);
00088     friend bool operator>=(const IString&, const IString&);
00089 
00090 
00091     // Searches
00092     const char* find(const IString& s)   const;  // First occurence of s
00093     const char* find(char c)             const;  // First occurence of c
00094     const char* findlast(char c)         const;  // Last occurence of c
00095     const char* brk(const IString& s)    const;  // First occurence of any character in s
00096     Subscript           span(const IString& s)   const;  // Size of initial span of characters in s
00097     Subscript           cspan(const IString& s)  const;  // Size of initial span of characters not in s
00098 
00099     // Sub-IStrings
00100     IString subString(Subscript start             ) const;  // From start 
00101     IString subString(Subscript start, Subscript n) const;  // ... for n characters
00102 
00103 
00104     // Miscellaneous
00105     Subscript   strlen() const;  // Length of IString (not including null)
00106     const char* c_str()  const;  // Pointer to underlying built-in IString
00107     char*       c_str();         // Pointer to underlying built-in IString
00108 
00109 
00110     // Conversion into numbers
00111     double        strtod()  const;    // Conversion to double
00112     long          strtol()  const;    // Conversion to long
00113     unsigned long strtoul() const;    // Conversion to unsigned long
00114 
00115 
00116     // I/O
00117     friend ostream& operator<<(ostream& s, const IString& cs);
00118     friend istream& operator>>(istream& s, IString& cs);
00119 
00120 
00121     // Iterators
00122     class Iterator; // Forward declaration
00123 
00124     class Browser {
00125     public:
00126         Browser(const IString& s);
00127 
00128 
00129         bool more() const;
00130         void    advance();
00131         char    current() const;
00132 
00133 
00134         Browser(const Iterator&);
00135     private:
00136         const char* cur;
00137         const char* endp;
00138     };
00139 
00140     class Iterator {
00141     public:
00142         Iterator(IString& s);
00143 
00144 
00145         bool more() const;
00146         void    advance();
00147         char&   current() const;
00148 
00149     private:
00150         friend Browser::Browser(const Iterator&);
00151         char* cur;
00152         const char* endp;
00153     };
00154 
00155 private:
00156     char* data; // Pointer to built-in character IString
00157 
00158     IString(const IString&, const IString&);
00159 };
00160 
00161 // Inline 
00162 
00163 inline const char* IString::c_str() const { 
00164     return data; 
00165 }
00166 inline char* IString::c_str() {
00167     return data;
00168 }
00169 
00170 
00171 inline char IString::operator[](Subscript i) const { return data[i]; }
00172 inline char& IString::operator[](Subscript i) { return data[i]; }
00173 inline char IString::operator()(Subscript i) const { return data[i]; }
00174 inline char& IString::operator()(Subscript i) { return data[i]; }
00175 
00176 inline Subscript IString::strlen() const { return ::strlen(data); }
00177 
00178 
00179 inline IString::Browser::Browser(const IString& s) : cur(s.c_str()), endp(cur + s.strlen()) {}
00180 inline bool IString::Browser::more() const { return cur < endp; }
00181 inline void    IString::Browser::advance()    { ++cur; }
00182 inline char    IString::Browser::current() const { return *cur; }
00183 
00184 inline IString::Iterator::Iterator(IString& s) : cur(s.c_str()), endp(cur + s.strlen()) {}
00185 inline bool IString::Iterator::more() const { return cur < endp; }
00186 inline void    IString::Iterator::advance()    { ++cur; }
00187 inline char& IString::Iterator::current() const { return *cur; }
00188 
00189 inline IString::Browser::Browser(const IString::Iterator& iter) : cur(iter.cur), endp(iter.endp) {}
00190 
00191 
00192 } // csfl namespace
00193 
00194 #endif// _SFL20_SYS_CORE_STRING_H

 

CFD Project | Documents | Downloads | Contact us | Use Terms

 

© SINMEC/EMC/UFSC, 2001.

All rights reserved.

Last Update: Jan. 18, 2002

 Webmaster