Class Hierarchy   Class Index   Method Index  

CHMstring.h

00001 #ifndef _CHM_STRING_H_
00002 #define _CHM_STRING_H_
00003 //---------------------------------------------------------------------------
00004 // Copyright (C) 1997-2007 iNTERFACEWARE Inc.  All Rights Reserved
00005 //
00006 // Module: CHMstring
00007 //
00008 // Description:String class derived from work by Lynn Prentice
00009 //       and Ian Farquharson.
00010 //
00011 // Author: Greg Norman
00012 // Revision: $Revision: 1.17 $
00013 //
00014 // Last Edit Date: $Date: 2010-02-24 17:41:00 $
00015 // Source: $Source: /home/cvs/cvsroot/CHM/CHMstring.h,v $
00016 //---------------------------------------------------------------------------
00017 // This is super efficient string class used by HL7 Chameleon
00018 #include <CHM/CHMminimumInclude.h>
00019 
00020 #define COPYONWRITE
00021 
00022 #ifdef __MSC__
00023    const size_t NPOS = npos;
00024 #endif
00025 
00026 //=============================================================================
00027 
00028 class CHMstringRef;
00029 
00038 class CHMstring 
00039 {
00040 public:
00041    // Constructors
00043    CHMstring(void);
00044    
00046    CHMstring(const CHMstring& s);   
00047    
00050    CHMstring(const CHMstring& s, const size_t& orig, const size_t& n = npos);   
00051    
00053    CHMstring(const CHMchar* cp);   
00054    
00057    CHMstring(const CHMchar* cp, const size_t& orig, const size_t& n = npos );   
00058    
00060    CHMstring(CHMchar c);
00061 
00063    CHMstring(size_t n, CHMchar c);
00064    
00066    ~CHMstring(void); 
00067 
00068 #ifdef CHM_UNICODE_ON
00069    CHMstring& operator=(const char* pValue);
00070 #endif  // CHM_UNICODE_ON
00071    
00073    CHMstring& operator =(const CHMstring& s);
00074    
00076    CHMstring& operator+=(const CHMchar* cp);
00077    
00079    CHMstring& operator+=(const CHMstring &s);   
00080     
00082    const CHMchar operator[](const size_t& pos) const;
00083    
00085    CHMchar& operator[](const size_t& pos);
00086   
00091    enum StripType 
00092    { 
00093       Leading,  
00094       Trailing, 
00095       Both      
00096    };
00097    
00100    CHMstring& append(const CHMchar* cp,
00101                      const size_t& orig = 0,
00102                      const size_t& n = npos);
00103    
00108    CHMstring& append(size_t SizeOfBuffer, 
00109                      const CHMchar* Buffer);
00110    
00112    CHMstring& append(const CHMstring& s,
00113                      const size_t& orig = 0,
00114                      const size_t& n = npos);
00115    
00117    void assign(size_t n, CHMchar Filler);
00118    
00124    const CHMchar* c_str(void) const;
00125      
00127    void clear(void);
00128       
00131    int compare(const CHMchar* Compare) const;
00132    
00135    int compare(const CHMstring& s) const;
00136       
00139    const size_t find(const CHMstring& s, const size_t& pos=0) const;
00140   
00145    CHMchar* get_buffer(void); 
00146      
00149    CHMstring & insert(size_t pos,
00150                       const CHMchar* cp,
00151                       const size_t& orig = 0,
00152                       const size_t& n = npos );
00153                       
00156    CHMstring & insert(size_t pos,
00157                       const CHMstring &s,
00158                       const size_t& orig = 0,
00159                       const size_t& n = npos );
00160 
00162    const CHMboolean is_null(void) const;    
00163       
00166    CHMstring& prepend(const CHMchar* cp,
00167             const size_t& orig = 0,
00168             const size_t& n = npos );
00169    
00171    CHMstring& prepend(const CHMstring &s,
00172                       const size_t& orig = 0,
00173                       const size_t& n = npos);
00174     
00176    CHMstring& remove(const size_t& pos, const size_t& n=npos);
00177    
00180    CHMstring& replace(const size_t& pos,
00181                       const size_t& n1,
00182                       const CHMstring& s,
00183                       const size_t& orig = 0,
00184                       const size_t& n2 = npos );   
00185       
00188    const size_t rfind(const CHMstring& s, const size_t& pos) const;
00189    
00192    const size_t rfind(const CHMstring& s) const;
00193   
00195    const size_t& size(void) const;
00196    
00199    CHMstring strip(const StripType& s = Trailing, const CHMchar& c = ' ') const;
00200    
00202    void stripAll(const CHMchar& c = ' ');
00203    
00205    void subChar(const CHMchar pTarget,
00206                 const CHMchar pReplacement);
00207 
00210    CHMstring substr(const size_t& pos, const size_t& n=npos) const;
00211 
00212 protected:
00213    const CHMboolean valid_element(const size_t& pos ) const;
00214    const CHMboolean valid_index(const size_t& pos ) const;
00215 
00216 private:
00217    // provided for display purposes only...
00218    const CHMchar* Current;
00219    CHMstringRef* pReference;
00220 
00221 private:
00222    const size_t find_index(const CHMchar*,
00223                            const size_t& start) const;
00224    const size_t rfind_index(const CHMchar*,
00225                            const size_t& start) const;
00226 };
00227 
00228 // this constructor is trivial and called frequently ...
00229 inline CHMstring::CHMstring(void)
00230  : pReference(NULL), Current(NULL) {}
00231 
00232 inline const size_t CHMstring::rfind(const CHMstring& s) const
00233 {
00234    return rfind(s, size());
00235 };
00236 
00237 // Check to make sure a string index refers to a valid element
00238 inline const CHMboolean CHMstring::valid_element(const size_t& n) const
00239 {
00240    return n < size();
00241 };
00242 
00243 // Check to make sure a string index is in range
00244 inline const CHMboolean CHMstring::valid_index(const size_t& n) const
00245 {
00246    return n <= size();
00247 };
00248 
00249 CHMstring operator+(const CHMchar* cp, const CHMstring& s);
00250 CHMstring operator+(const CHMstring& s, const CHMchar* cp);
00251 
00252 inline CHMstring operator+(const CHMstring& s1, const CHMstring& s2) 
00253 {
00254    return s1 + s2.c_str();
00255 };
00256 
00257 CHMstring operator+(const CHMchar& s1, const CHMstring& s2);
00258 CHMstring operator+(const CHMstring& s1, const CHMchar& s2);
00259 
00260 // comparisons...
00261 inline int operator == (const CHMstring& s1, const CHMstring& s2) 
00262 {
00263    return s1.compare(s2) == 0;
00264 };
00265 
00266 inline int operator == (const CHMstring& s1, const CHMchar* s2) 
00267 {
00268    return s1.compare(s2) == 0;
00269 };
00270 
00271 inline int operator == (const CHMchar* cp, const CHMstring& s) 
00272 {
00273    return CHMstring(cp).compare(s) == 0;
00274 };
00275 
00276 inline int operator != (const CHMstring& s1, const CHMstring& s2) 
00277 {
00278    return !(s1==s2);
00279 };
00280 
00281 inline int operator != (const CHMstring& s, const CHMchar* cp) 
00282 {
00283    return !(s==cp);
00284 };
00285 
00286 inline int operator != (const CHMchar* cp, const CHMstring& s) 
00287 {
00288    return !(cp==s);
00289 };
00290 
00291 inline int operator < (const CHMstring& s1, const CHMstring& s2) 
00292 {
00293    return s1.compare(s2) < 0;
00294 };
00295 
00296 inline int operator < (const CHMstring& s1, const CHMchar* s2 ) 
00297 {
00298    return s1.compare(s2) < 0;
00299 };
00300 
00301 inline int operator < (const CHMchar* cp, const CHMstring& s) 
00302 {
00303    return CHMstring(cp).compare(s) < 0;
00304 };
00305 
00306 inline int operator <= (const CHMstring& s1, const CHMstring& s2) 
00307 {
00308    return s1.compare(s2) <= 0;
00309 };
00310 
00311 inline int operator <= (const CHMstring& s, const CHMchar* cp) 
00312 {
00313    return s.compare(cp) <= 0;
00314 };
00315 
00316 inline int operator <= (const CHMchar* cp, const CHMstring& s) 
00317 {
00318    return CHMstring(cp).compare(s) <= 0;
00319 };
00320 
00321 inline int operator > (const CHMstring& s1, const CHMstring& s2) 
00322 {
00323    return s1.compare(s2) > 0;
00324 };
00325 
00326 inline int operator > (const CHMstring& s, const CHMchar* cp) 
00327 {
00328    return s.compare(cp) > 0;
00329 };
00330 
00331 inline int operator > (const CHMchar* cp, const CHMstring& s) 
00332 {
00333    return CHMstring(cp).compare(s) > 0;
00334 };
00335 
00336 inline int operator >= (const CHMstring& s1, const CHMstring& s2) 
00337 {
00338    return s1.compare(s2) >= 0;
00339 };
00340 
00341 inline int operator >= (const CHMstring& s, const CHMchar* cp) 
00342 {
00343    return s.compare(cp) >= 0;
00344 };
00345 
00346 inline int operator >= (const CHMchar* cp, const CHMstring& s) 
00347 {
00348    return CHMstring(cp).compare(s) >= 0;
00349 };
00350 
00351 inline const CHMchar CHMstring::operator[](const size_t& pos) const 
00352 {
00353 #ifdef _DEBUG
00354    if (pos >= size())
00355    {
00356       throw "BOUNDSERROR";
00357    }
00358 #endif
00359    return c_str()[pos];
00360 };
00361 
00362 inline CHMstring& CHMstring::append(const CHMstring& s,
00363                         const size_t& orig,
00364                         const size_t& n) 
00365 {
00366    return append(s.c_str(), orig, n);
00367 };
00368 
00369 inline CHMstring& CHMstring::prepend(const CHMstring &s,
00370                          const size_t& orig,
00371                          const size_t& n) 
00372 {
00373    return prepend(s.c_str(), orig, n);
00374 };
00375 
00376 inline CHMstring& CHMstring::insert(size_t pos,
00377                         const CHMstring &s,
00378                         const size_t& orig,
00379                         const size_t& n) 
00380 {
00381    return insert(pos, s.c_str(), orig, n);
00382 };
00383 
00384 inline const CHMchar* CHMstring::c_str(void) const 
00385 {
00386    if (Current == NULL)
00387    {
00388       return CHM_TEXT("");
00389    }
00390    return Current;
00391 };
00392 
00393 inline const CHMboolean CHMstring::is_null() const
00394 {
00395    if (NULL == pReference) 
00396            return true;
00397    return size() == 0;
00398 }
00399 
00400 #endif