string.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // $Id: string.h,v 1.56 2005/12/23 17:01:43 amoll Exp $
00005 //
00006 
00007 #ifndef BALL_DATATYPE_STRING_H
00008 #define BALL_DATATYPE_STRING_H
00009 
00010 #ifndef BALL_CONFIG_CONFIG_H
00011 # include <BALL/CONFIG/config.h>
00012 #endif
00013 #ifndef BALL_COMMON_GLOBAL_H
00014 # include <BALL/COMMON/global.h>
00015 #endif
00016 #ifndef BALL_COMMON_CREATE_H
00017 # include <BALL/COMMON/create.h>
00018 #endif
00019 #ifndef BALL_COMMON_MACROS_H
00020 # include <BALL/COMMON/macros.h>
00021 #endif
00022 #ifndef BALL_COMMON_EXCEPTION_H
00023 # include <BALL/COMMON/exception.h>
00024 #endif
00025 #ifndef BALL_COMMON_DEBUG_H
00026 # include <BALL/COMMON/debug.h>
00027 #endif
00028 
00029 #include <string>
00030 #include <ctype.h>
00031 #include <errno.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <iostream>
00035 #include <vector>
00036 
00037 #ifdef BALL_HAS_SSTREAM
00038 # include <sstream>
00039 #else
00040 # include <strstream>
00041 #endif
00042 
00043 using std::string;
00044 
00045 namespace BALL 
00046 {
00047   // forward declaration
00048   class Substring;
00049 
00056         
00060   class BALL_EXPORT String
00061     : public string
00062   {
00064     friend class Substring;
00065 
00066     public:
00067         
00068     // String has no copy constructor taking String&, bool as arguments.
00069     // the compiler would confuse it with another copy constructor,
00070     // cast true to 1 and copy only the string from the second character
00071     // on! We could use BALL_CREATE_NODEEP, but this leads to trouble with
00072     // inline constructors, so we code it by hand (here and in string.C)
00073     virtual void* create(bool /* deep */ = true, bool empty = false) const;
00074   
00077 
00085     enum CompareMode
00086     { 
00088       CASE_SENSITIVE   = 0, 
00089 
00091       CASE_INSENSITIVE = 1 
00092     };
00093 
00099     static const Size EndPos;
00100 
00102 
00108 
00110     static const char* CHARACTER_CLASS__ASCII_ALPHA;
00111 
00113     static const char* CHARACTER_CLASS__ASCII_ALPHANUMERIC;
00114 
00116     static const char* CHARACTER_CLASS__ASCII_LOWER;
00117 
00119     static const char* CHARACTER_CLASS__ASCII_UPPER;
00120 
00122     static const char* CHARACTER_CLASS__ASCII_NUMERIC;
00123 
00125     static const char* CHARACTER_CLASS__ASCII_FLOAT;
00126 
00138     static const char* CHARACTER_CLASS__WHITESPACE;
00139 
00142     static const char* CHARACTER_CLASS__QUOTES;
00143 
00145 
00148 
00150     String();
00151 
00153     String(const String& string);
00154 
00156     String(const string& string);
00157 
00163     String(const String& s, Index from, Size len = EndPos)
00164       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00165   
00172     String(const char* char_ptr, Index from = 0, Size len = EndPos)
00173       throw(Exception::NullPointer, Exception::IndexUnderflow, Exception::IndexOverflow);
00174 
00185     String(Size buffer_size, const char* format, ... )
00186       throw(Exception::IndexUnderflow, Exception::NullPointer);
00187 
00193 #ifdef BALL_HAS_SSTREAM
00194     String(std::stringstream& s);
00195 #else
00196     String(std::strstream& s);
00197 #endif
00198 
00201     String(const char c, Size len = 1);
00202 
00204     String(const unsigned char uc);
00205 
00207     String(short s);
00208 
00210     String(unsigned short us);
00211 
00213     String(int i);
00214 
00216     String(unsigned int ui);
00217 
00219     String(long l);
00220 
00222     String(unsigned long);
00223 
00225     String(float f);
00226 
00228     String(double d);
00229 
00231     virtual ~String();
00232 
00234     void destroy();
00235 
00237     virtual void clear();
00239 
00243 
00245     void set(const String& s);
00246 
00251     void set(const String& string, Index from, Size len = EndPos)
00252       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00253 
00258     void set(const char* char_ptr, Index from = 0, Size len = EndPos)
00259       throw(Exception::NullPointer, Exception::IndexUnderflow, Exception::IndexOverflow);
00260   
00265     void set(Size buffer_size, const char *format, ...)
00266       throw(Exception::IndexUnderflow, Exception::NullPointer);
00267 
00271 #ifdef BALL_HAS_SSTREAM
00272     void set(std::stringstream& s);
00273 #else
00274     void set(std::strstream& s);
00275 #endif
00276 
00278     void set(char c, Size len = 1);
00279 
00281     void set(unsigned char uc);
00282 
00284     void set(short s);
00285 
00287     void set(unsigned short us);
00288 
00290     void set(int i);
00291 
00293     void set(unsigned int ui);
00294 
00296     void set(long l);
00297 
00299     void set(unsigned long ul);
00300 
00302     void set(float f);
00303 
00305     void set(double d);
00306 
00308     void get(char* char_ptr, Index from = 0, Size len = EndPos) const
00309       throw(Exception::NullPointer, Exception::IndexUnderflow, Exception::IndexOverflow);
00310 
00312     const String& operator = (const String& s);
00313 
00315     const String& operator = (const char* pc)
00316       throw(Exception::NullPointer);
00317 
00321 #ifdef BALL_HAS_SSTREAM
00322     const String& operator = (std::stringstream& s);
00323 #else
00324     const String& operator = (std::strstream& s);
00325 #endif
00326 
00328     const String& operator = (char c);
00329 
00331     const String& operator = (unsigned char uc);
00332 
00334     const String& operator = (short s);
00335 
00337     const String& operator = (unsigned short us);
00338 
00340     const String& operator = (int i);
00341 
00343     const String& operator = (unsigned int ui);
00344 
00346     const String& operator = (long l);
00347 
00349     const String& operator = (unsigned long ul);
00350 
00352     const String& operator = (float f);
00353 
00355     const String& operator = (double d);
00357 
00364 
00365     static void setCompareMode(CompareMode compare_mode);
00366 
00368     static CompareMode getCompareMode();
00370 
00374 
00379     bool toBool() const;
00380 
00382     char toChar() const;
00383 
00385     unsigned char toUnsignedChar() const;
00386 
00388     short toShort() const throw(Exception::InvalidFormat);
00389 
00391     unsigned short toUnsignedShort() const throw(Exception::InvalidFormat);
00392 
00394     int toInt() const throw(Exception::InvalidFormat);
00395 
00397     unsigned int toUnsignedInt() const throw(Exception::InvalidFormat);
00398 
00400     long toLong() const throw(Exception::InvalidFormat);
00401 
00403     unsigned long toUnsignedLong() const throw(Exception::InvalidFormat);
00404 
00406     float toFloat() const throw(Exception::InvalidFormat);
00407 
00409     double toDouble() const throw(Exception::InvalidFormat);
00411 
00412 
00416 
00418     void toLower(Index from = 0, Size len = EndPos)
00419       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00420       
00422     void toUpper(Index from = 0, Size len = EndPos)
00423       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00424 
00426 
00429 
00431     Substring getSubstring(Index from = 0, Size len = EndPos) const
00432       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00433 
00435     Substring operator () (Index from, Size len = EndPos) const
00436       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00437 
00440     Substring before(const String& s, Index from = 0) const;
00441  
00444     Substring through(const String& s, Index from = 0) const;
00445  
00448     Substring from(const String& s, Index from = 0) const;
00449 
00452     Substring after(const String& s, Index from = 0) const;
00453 
00455 
00458 
00460     Size countFields(const char* delimiters = CHARACTER_CLASS__WHITESPACE) const
00461       throw(Exception::NullPointer);
00462 
00464     Size countFieldsQuoted(const char* delimiters = CHARACTER_CLASS__WHITESPACE, 
00465                            const char* quotes = CHARACTER_CLASS__QUOTES) const
00466       throw(Exception::NullPointer);
00467 
00469     String getField(Index index, const char* delimiters = CHARACTER_CLASS__WHITESPACE, Index* from = 0) const
00470       throw(Exception::IndexUnderflow, Exception::NullPointer);
00471 
00473     String getFieldQuoted(Index index, const char* delimiters = CHARACTER_CLASS__WHITESPACE, 
00474                           const char* quotes = CHARACTER_CLASS__QUOTES, Index* from = 0) const
00475       throw(Exception::IndexUnderflow, Exception::NullPointer);
00476 
00479     Size split(String string_array[], Size array_size, const char* delimiters = CHARACTER_CLASS__WHITESPACE, Index from = 0) const
00480       throw(Exception::IndexUnderflow, Exception::NullPointer);
00481 
00487     Size split(std::vector<String>& strings, const char* delimiters = CHARACTER_CLASS__WHITESPACE, Index from = 0) const
00488       throw(Exception::IndexUnderflow, Exception::NullPointer);
00489 
00497     Size splitQuoted(std::vector<String>& strings, const char* delimiters = CHARACTER_CLASS__WHITESPACE, 
00498                const char* quotes = CHARACTER_CLASS__QUOTES, Index from = 0) const
00499       throw(Exception::IndexUnderflow, Exception::NullPointer);
00500 
00502 
00505 
00512     String& trimLeft(const char* trimmed = CHARACTER_CLASS__WHITESPACE);
00513 
00520     String& trimRight(const char* trimmed = CHARACTER_CLASS__WHITESPACE);
00521 
00525     String& trim(const char* trimmed = CHARACTER_CLASS__WHITESPACE);
00526 
00527     // ?????
00531     String trim(const char* trimmed = CHARACTER_CLASS__WHITESPACE) const;
00532 
00534     String& truncate(Size size);
00535 
00537     Substring left(Size len) const;
00538 
00540     Substring right(Size len) const;
00541 
00550     Substring instr(const String& pattern, Index from = 0) const;
00551 
00553 
00556 
00558     String operator + (const string& string) const;
00559 
00561     String operator + (const char* char_ptr) const;
00562 
00564     String operator + (char c) const;
00565 
00567     BALL_EXPORT
00568     friend String operator + (const char* char_ptr, const String& s);
00569 
00571     BALL_EXPORT
00572     friend String operator + (char c, const String& s);
00573 
00575     void swap(String& s);
00576 
00584     String& reverse(Index from = 0, Size len = EndPos)
00585       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00586 
00590     Size substitute(const String& to_replace, const String& replacing);
00591 
00593 
00597 
00599     bool has(char c) const;
00600 
00602     bool hasSubstring(const String& s, Index from = 0) const;
00603 
00605     bool hasPrefix(const String& s) const;
00606 
00608     bool hasSuffix(const String& s) const;
00609 
00611     bool isEmpty() const;
00612 
00616     bool isAlpha() const;
00617 
00621     bool isAlnum() const;
00622 
00626     bool isDigit() const;
00627 
00632     bool isFloat() const;
00633 
00637     bool isSpace() const;
00638 
00643     bool isWhitespace() const;
00644 
00646     static bool isAlpha(char c);
00647   
00649     static bool isAlnum(char c);
00650 
00652     static bool isDigit(char c);
00653 
00655     static bool isSpace(char c);
00656 
00660     static bool isWhitespace(char c);
00661     
00663 
00666     
00668     String encodeBase64();
00669 
00673     String decodeBase64();
00674     
00676 
00679 
00681     int compare(const String& string, Index from = 0) const
00682       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00683 
00685     int compare(const String& string, Index from, Size len) const
00686       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00687 
00688 
00690     int compare(const char* char_ptr, Index from = 0) const
00691       throw(Exception::NullPointer, Exception::IndexUnderflow, Exception::IndexOverflow);
00692 
00694     int compare(const char* char_ptr, Index from, Size len) const
00695         throw(Exception::NullPointer, Exception::IndexUnderflow, Exception::IndexOverflow);
00696 
00698     int compare(char c, Index from = 0) const
00699       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00700 
00702     bool operator == (const String& string) const;
00703 
00705     bool operator != (const String& string) const;
00706 
00708     bool operator < (const String& string) const;
00709 
00711     bool operator <= (const String& string) const;
00712 
00714     bool operator >= (const String& string) const;
00715 
00717     bool operator > (const String& string) const;
00718 
00720     BALL_EXPORT
00721     friend bool operator == (const char* char_ptr, const String& string)
00722       throw(Exception::NullPointer);
00723 
00725     BALL_EXPORT
00726     friend bool operator != (const char* char_ptr, const String& string)
00727       throw(Exception::NullPointer);
00728 
00730     BALL_EXPORT
00731     friend bool operator < (const char* char_ptr, const String& string)
00732       throw(Exception::NullPointer);
00733 
00735     BALL_EXPORT
00736     friend bool operator <= (const char* char_ptr, const String& string)
00737       throw(Exception::NullPointer);
00738     
00740     BALL_EXPORT
00741     friend bool operator > (const char* char_ptr, const String& string)
00742       throw(Exception::NullPointer);
00743 
00745     BALL_EXPORT
00746     friend bool operator >= (const char* char_ptr, const String& string)
00747       throw(Exception::NullPointer);
00748 
00750     bool operator == (const char* char_ptr) const
00751       throw(Exception::NullPointer);
00752 
00754     bool operator != (const char* char_ptr) const
00755       throw(Exception::NullPointer);
00756 
00758     bool operator < (const char* char_ptr) const
00759       throw(Exception::NullPointer);
00760 
00762     bool operator <= (const char* char_ptr) const
00763       throw(Exception::NullPointer);
00764 
00766     bool operator > (const char* char_ptr) const
00767       throw(Exception::NullPointer);
00768 
00770     bool operator >= (const char* char_ptr) const
00771       throw(Exception::NullPointer);
00772 
00774     BALL_EXPORT
00775     friend bool operator == (char c, const String& string);
00776 
00778     BALL_EXPORT
00779     friend bool operator != (char c, const String& string);
00780 
00782     BALL_EXPORT
00783     friend bool operator < (char c, const String& string);
00784 
00786     BALL_EXPORT
00787     friend bool operator <= (char c, const String& string);
00788     
00790     BALL_EXPORT
00791     friend bool operator > (char c, const String& string);
00792 
00794     friend bool operator >= (char c, const String& string);
00795 
00797     bool operator == (char c) const;
00798 
00800     bool operator != (char c) const;
00801 
00803     bool operator < (char c) const;
00804 
00806     bool operator <= (char c) const;
00807 
00809     bool operator > (char c) const;
00810 
00812     bool operator >= (char c) const;
00813 
00815 
00818 
00820     bool isValid() const;
00821 
00823     void dump(std::ostream& s = std::cout, Size depth = 0) const;
00824 
00826 
00829 
00831     std::istream& getline(std::istream& s = std::cin, char delimiter = '\n');
00832 
00834     friend std::istream& getline(std::istream& s,  String& string,  char delimiter = '\n');
00835 
00837 
00839     static const String EMPTY;
00840 
00841     protected:
00842   
00843     // the validate...  methods check perform a thorough
00844     // index checking and an index translation
00845     // Indices below zero are interpreted as indices
00846     // relative to the end of the string
00847     // All methods throw IndexUnder|Overflow exceptions
00848     //
00849     void validateIndex_(Index& index) const
00850       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00851   
00852     void validateRange_(Index& from, Size& len) const
00853       throw(Exception::IndexUnderflow, Exception::IndexOverflow); 
00854 
00855     static void validateCharPtrRange_(Index& from, Size& len, const char* char_ptr)
00856       throw(Exception::NullPointer, Exception::IndexUnderflow, Exception::IndexOverflow);
00857     
00858     static void valudateCharPtrIndex_(Index& index);
00859     
00860     private:
00861 
00862     static int compareAscendingly_(const char* a,  const char* b);
00863 
00864     static int compareDescendingly_(const char* a,  const char* b);
00865 
00866     static CompareMode compare_mode_;
00867 
00868     static char B64Chars_[64];
00869 
00870     static int Index_64_[128];
00871   };
00872 
00881   class BALL_EXPORT Substring
00882   {
00883     friend class String;
00884 
00885     public:
00886 
00887     BALL_CREATE_DEEP(Substring)
00888 
00889     
00892 
00897     class BALL_EXPORT UnboundSubstring
00898       : public Exception::GeneralException
00899     {
00900       public:
00901       UnboundSubstring(const char* file, int line); 
00902     };
00903 
00909     class BALL_EXPORT InvalidSubstring
00910       : public Exception::GeneralException
00911     {
00912       public:
00913       InvalidSubstring(const char* file, int line); 
00914     };
00915 
00917 
00920 
00924     Substring();
00925 
00931     Substring(const Substring& substring, bool deep = true);
00932 
00938     Substring(const String& string, Index from = 0, Size len = String::EndPos)
00939       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00940 
00944     virtual ~Substring();
00945 
00950     void destroy();
00951 
00956     virtual void clear();
00957 
00959 
00962 
00966     operator String() const
00967       throw(Substring::UnboundSubstring);
00968 
00972     String toString() const
00973       throw(Substring::UnboundSubstring);
00974 
00976 
00979     
00985     Substring& bind(const String& string, Index from = 0, Size len = String::EndPos)
00986       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00987 
00991     Substring& bind(const Substring& substring, Index from = 0, Size len = String::EndPos)
00992       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00993 
00995     void unbind();
00996 
00998     String* getBoundString();
00999 
01001     const String* getBoundString() const
01002 ;
01003 
01005 
01008   
01011     void set(const String& string)
01012       throw(Substring::UnboundSubstring);
01013 
01016     void set(const Substring& s)
01017       throw(Substring::UnboundSubstring);
01018 
01020     void set(const char* char_ptr, Size size = String::EndPos)
01021       throw(Substring::UnboundSubstring, Exception::NullPointer, Exception::SizeUnderflow);
01022 
01024     const Substring& operator = (const String& string)
01025       throw(Substring::UnboundSubstring);
01026 
01028     const Substring& operator = (const Substring& substring)
01029       throw(Substring::UnboundSubstring);
01030 
01032     const Substring& operator = (const char* char_ptr)
01033       throw(Substring::UnboundSubstring, Exception::NullPointer);
01034 
01036 
01039     
01041     char* c_str()
01042       throw(Substring::UnboundSubstring);
01043 
01045     const char* c_str() const
01046       throw(Substring::UnboundSubstring);
01047 
01051     Index getFirstIndex() const
01052       throw(Substring::UnboundSubstring);
01053 
01057     Index getLastIndex() const
01058       throw(Substring::UnboundSubstring);
01059 
01061     Size size() const;
01062 
01064     char& operator [] (Index index)
01065       throw(Substring::UnboundSubstring, Exception::IndexUnderflow, Exception::IndexOverflow);
01066 
01068     char operator [] (Index index) const
01069       throw(Substring::UnboundSubstring, Exception::IndexUnderflow, Exception::IndexOverflow);
01070 
01072     Substring& toLower()  
01073       throw(Substring::UnboundSubstring);
01074 
01076     Substring& toUpper()  
01077       throw(Substring::UnboundSubstring);
01078       
01080 
01083 
01085     bool isBound() const;
01086     
01088     bool isEmpty() const;
01089 
01091 
01094 
01096     bool operator == (const Substring& substring) const
01097       throw(Substring::UnboundSubstring);
01098 
01100     bool operator != (const Substring& substring) const
01101       throw(Substring::UnboundSubstring);
01102 
01104     bool operator == (const String& string) const
01105       throw(Substring::UnboundSubstring);
01106 
01108     bool operator != (const String& string) const
01109       throw(Substring::UnboundSubstring);
01110 
01112     BALL_EXPORT
01113     friend bool operator == (const String& string, const Substring& substring)
01114       throw(Substring::UnboundSubstring);
01115 
01117     BALL_EXPORT
01118     friend bool operator != (const String& string, const Substring& substring)
01119       throw(Substring::UnboundSubstring);
01120 
01122     bool operator == (const char* char_ptr) const
01123       throw(Substring::UnboundSubstring, Exception::NullPointer);
01124 
01126     bool operator != (const char* char_ptr) const
01127       throw(Substring::UnboundSubstring, Exception::NullPointer);
01128 
01130     bool operator == (char c) const
01131       throw(Substring::UnboundSubstring);
01132 
01134     bool operator != (char c) const
01135       throw(Substring::UnboundSubstring);
01136 
01138 
01141 
01143     BALL_EXPORT
01144     friend std::ostream& operator << (std::ostream& s, const Substring& substring);
01145 
01147 
01150 
01155     bool isValid() const;
01156 
01158     void dump(std::ostream& s = std::cout, Size depth = 0) const 
01159       throw(Substring::UnboundSubstring);
01160 
01162     
01163     protected:
01164       
01165     void validateRange_(Index& from, Size& len) const
01166       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
01167 
01168     private:
01169     
01170     /*_ @name Attributes
01171     */
01172     //_@{
01173 
01174     //_ pointer to the bound String
01175     String*   bound_;
01176 
01177     //_ start index in the bound String
01178     Index     from_;
01179 
01180     //_ end index in the bound String
01181     Index     to_;
01182     //_@}
01183   };
01184   
01186 
01187 # ifndef BALL_NO_INLINE_FUNCTIONS
01188 #   include <BALL/DATATYPE/string.iC>
01189 # endif
01190 } // namespace BALL
01191 
01192 #endif // BALL_DATATYPE_STRING_H