istring.h

Go to the documentation of this file.
00001 #ifndef ISTRING_CLASS
00002 #define ISTRING_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : istring                                                           *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1993-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "definitions.h"
00019 
00020 #include <stdarg.h>
00021 
00023 
00028 class istring
00029 {
00030 public:
00031   istring();
00033 
00034   istring(const char *initial);
00036 
00037   istring(char c, int repeat);
00039 
00042   istring(const istring &s);
00044 
00045   enum special_flag { UNTERMINATED = 62, SPRINTF = 84 };
00046   istring(special_flag way, const char *s, ...);
00048 
00064   ~istring();
00066 
00067   int length() const;
00069 
00072   inline int end() const { return length() - 1; }
00074 
00077   inline bool empty() const { return !length(); }
00079   inline bool non_empty() const { return !empty(); }
00081   inline bool operator ! () const { return empty(); }
00083 
00085   inline bool t() const { return !empty(); }
00087 
00090   static const istring &empty_string();
00092 
00096   const char *observe() const;
00098 
00100   inline const char *c_str() const { return observe(); }
00102   inline const char *s() const { return observe(); }
00104 
00105   char get(int index) const;
00107 
00108   char *access();
00110 
00113   inline char *c_str() { return access(); }
00115   inline char *s() { return access(); }
00117 
00118   char &operator [] (int position);
00120 
00122   const char &operator [] (int position) const;
00124 
00127   inline void put(int position, char to_put) { (*this)[position] = to_put; }
00129 
00130   istring &sprintf(const char *s, ...);
00132 
00134   int convert(int default_value) const;
00136 
00141   long convert(long default_value) const;
00143   float convert(float default_value) const;
00145   double convert(double default_value) const;
00147 
00148   bool operator < (const istring &that) const;
00150   bool operator > (const istring &that) const;
00152   bool operator <= (const istring &that) const;
00154   bool operator >= (const istring &that) const;
00156   bool operator != (const istring &that) const;
00158   bool operator == (const istring &that) const;
00160 
00161   bool operator == (const char *that) const;
00163   bool operator != (const char *that) const;
00165 
00166   bool iequals(const istring &that) const;
00168   bool iequals(const char *that) const;
00170 
00171   bool compare(const istring &to_compare, int start_first,
00172           int start_second, int count) const;
00174 
00182   bool oy_icompare(const istring &to_compare, int start_first,
00183           int start_second, int count) const;
00185 
00186   inline bool begins(const istring &maybe_prefix) const
00187         { return compare(maybe_prefix, 0, 0, maybe_prefix.length()); }
00189   inline bool operator *= (const istring &maybe_prefix) const
00190         { return begins(maybe_prefix); }
00192 
00193   inline bool ibegins(const istring &maybe_prefix) const
00194         { return oy_icompare(maybe_prefix, 0, 0, maybe_prefix.length()); }
00196 
00198   inline bool ends(const istring &maybe_suffix) const {
00199     const int diff = length() - maybe_suffix.length();
00200     return (diff >= 0) && compare(maybe_suffix, diff,
00201         0, maybe_suffix.length());
00202   }
00204   inline bool iends(const istring &maybe_suffix) const {
00205     const int diff = length() - maybe_suffix.length();
00206     return (diff >= 0) && oy_icompare(maybe_suffix, diff,
00207         0, maybe_suffix.length());
00208   }
00209 
00210   istring &operator = (const istring &s);
00212   istring &operator = (const char *s);
00214 
00215   inline void reset() { zap(0, end()); }
00217 
00218   void reset(special_flag way, const char *s, ...);
00220 
00221   void copy(char *to_stuff, int count) const;
00223 
00229   inline void stuff(char *to_stuff, int count) const { copy(to_stuff, count); }
00231 
00232   istring operator + (const istring &s) const;
00234 
00235   istring &operator += (const istring &s);
00237 
00238   istring &operator += (const char *s);  // this is efficient.
00240   inline istring operator + (const char *s) const { return *this + istring(s); }
00242     // this method is not efficient.
00243 
00244   istring &operator += (char c);  
00245 
00246   int find(char to_find, int position = 0, bool reverse = false) const;
00248 
00251   int find(const istring &to_find, int posn = 0, bool reverse = false) const;
00253 
00254   int ifind(char to_find, int position = 0, bool reverse = false) const;
00256   int ifind(const istring &to_find, int posn = 0, bool reverse = false) const;
00258 
00259   int find_any(const istring &to_find, int position = 0,
00260           bool reverse = false) const;
00262 
00264   int ifind_any(const istring &to_find, int position = 0,
00265           bool reverse = false) const;
00267 
00269   int find_non_match(const istring &to_find, int position = 0,
00270           bool reverse = false) const;
00272 
00273   bool contains(const istring &to_find) const;
00275   inline bool operator ^= (const istring &to_find) const
00276           { return contains(to_find); }
00278 
00279   istring substring(int start, int end) const;
00281 
00283   void substring(istring &target, int start, int end) const;
00285 
00286   void pad(int length, char padding = ' ');
00288 
00290   void trim(int length);
00292 
00293   void insert(int position, const istring &to_insert);
00295 
00296   void zap(int start, int end);
00298 
00301   void to_lower();
00303 
00305   void to_upper();
00307   istring lower() const;
00309   istring upper() const;
00311 
00312   bool replace(const istring &tag, const istring &replacement);
00314 
00315   bool replace_all(char to_replace, char new_char);
00317   bool replace_all(const istring &to_replace, const istring &new_string);
00319 
00320   void shrink();
00322 
00327   enum how_to_strip { FROM_FRONT = 1, FROM_END = 2, FROM_BOTH_SIDES = 3 };
00329 
00330   void strip(const istring &strip_list, how_to_strip way = FROM_BOTH_SIDES);
00332 
00333   inline void strip_spaces(how_to_strip way = FROM_BOTH_SIDES)
00334           { strip(" ", way); }
00336 
00337   inline void strip_white_spaces(how_to_strip way = FROM_BOTH_SIDES)
00338           { strip(" \t", way); }
00340 
00341   static bool matches(const istring &match_list, char to_match);
00343 
00344   void pack(byte_array &target) const;
00346   bool unpack(byte_array &source);
00348 
00352 //hmmm: rename this--it is not a simple icompare, but a strncasecmp analogue.
00353   int icompare(const istring &to_compare, int length = -1) const;
00355 
00365   int icompare(const char *to_compare, int length = -1) const;
00367 
00368   static int slow_strncasecmp(const char *first, const char *second,
00369           int length = -1);
00371 
00380   const byte_sequence &low_level() const;
00382 
00383 private:
00384   byte_sequence *_implementation;
00386 
00387   // the real find methods.
00388   int char_find(char to_find, int position, bool reverse,
00389           bool case_sense) const;
00390   // if "invert_find" is true, then non-matches are reported instead of matches.
00391   int char_find_any(const istring &to_find, int position, bool reverse,
00392           bool case_sense, bool invert_find = false) const;
00393   int str_find(const istring &to_find, int posn, bool reverse,
00394           bool case_s) const;
00395 
00396   // the functions below are used in the formatting string constructor.
00397 public:  // only for base_sprintf.
00398   istring &base_sprintf(const char *s, va_list &args);
00399 private:
00400   char *const *_held_string;  
00401 
00402   void seek_flag(const char *&traverser, char *flag_chars, bool &failure);
00404   void seek_width(const char *&traverser, char *width_chars);
00406   void seek_precision(const char *&traverser, char *precision_chars);
00408   void seek_modifier(const char *&traverser, char *modifier_char);
00410   void get_type_character(const char *&traverser, va_list &args,
00411         istring &output_string, const char *flag_chars,
00412         const char *width_chars, const char *precision_chars,
00413         const char *modifier_chars);
00419   public: byte_sequence &get_implem(); private: // for test programs only....
00420 };
00421 
00423 
00425 
00428 class isprintf : public istring
00429 {
00430 public:
00431   isprintf();
00432   isprintf(const char *initial, ...);
00433   isprintf(const istring &s);
00434 };
00435 
00437 
00438 typedef bool string_comparator_function(const istring &a, const istring &b);
00440 
00444 bool istring_comparator(const istring &a, const istring &b);
00446 
00447 #endif
00448 

Generated on Fri Nov 28 04:29:08 2008 for HOOPLE Libraries by  doxygen 1.5.1