astring.h

Go to the documentation of this file.
00001 #ifndef ASTRING_CLASS
00002 #define ASTRING_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : astring                                                           *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1992-$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 "base_string.h"
00019 #include "byte_array.h"
00020 #include "contracts.h"
00021 
00022 #include <stdarg.h>
00023 
00024 namespace basis {
00025 
00027 
00032 class astring
00033 : public virtual base_string,
00034   public virtual hoople_standard
00035 {
00036 public:
00037   astring();
00039 
00040   astring(const char *initial);
00042 
00043   astring(char c, int repeat);
00045 
00048   astring(const astring &s);
00050 
00051   astring(const base_string &initial);
00053 
00054   enum special_flag { UNTERMINATED = 62, SPRINTF = 84 };
00055   astring(special_flag way, const char *s, ...);
00057 
00073   virtual ~astring();
00075 
00076   DEFINE_CLASS_NAME("astring");
00077 
00078   virtual int comparator(const astring &s2) const;
00080 
00081   int length() const;
00083 
00086   int end() const { return length() - 1; }
00088 
00091   bool empty() const { return !length(); }
00093   bool non_empty() const { return !empty(); }
00095   bool operator ! () const { return empty(); }
00097 
00099   bool t() const { return !empty(); }
00101 
00104   static const astring &empty_string();
00106 
00110   virtual const char *observe() const;
00112 
00114   const char *c_str() const { return observe(); }
00116   const char *s() const { return observe(); }
00118 
00119   virtual char get(int index) const;
00121 
00122   virtual char *access();
00124 
00127   char *c_str() { return access(); }
00129   char *s() { return access(); }
00131 
00132   char &operator [] (int position);
00134 
00136   const char &operator [] (int position) const;
00138 
00141   virtual void put(int position, char to_put) { (*this)[position] = to_put; }
00143 
00144   astring &sprintf(const char *s, ...);
00146 
00148   int convert(int default_value) const;
00150 
00155   long convert(long default_value) const;
00157   float convert(float default_value) const;
00159   double convert(double default_value) const;
00161 
00162   bool equal_to(const char *that) const;
00164 
00165   bool iequals(const astring &that) const;
00167   bool iequals(const char *that) const;
00169 
00170   bool compare(const astring &to_compare, int start_first,
00171           int start_second, int count, bool case_sensitive) const;
00173 
00181   bool begins(const astring &maybe_prefix) const
00182         { return compare(maybe_prefix, 0, 0, maybe_prefix.length(), true); }
00184 
00185   bool ibegins(const astring &maybe_prefix) const
00186         { return compare(maybe_prefix, 0, 0, maybe_prefix.length(), false); }
00188 
00190   bool ends(const astring &maybe_suffix) const {
00191     const int diff = length() - maybe_suffix.length();
00192     return (diff >= 0) && compare(maybe_suffix, diff, 0, maybe_suffix.length(), true);
00193   }
00195   bool iends(const astring &maybe_suffix) const {
00196     const int diff = length() - maybe_suffix.length();
00197     return (diff >= 0) && compare(maybe_suffix, diff, 0, maybe_suffix.length(), false);
00198   }
00199 
00200   astring &operator = (const astring &s);
00202   astring &operator = (const char *s);
00204 
00205   void reset() { zap(0, end()); }
00207 
00208   void reset(special_flag way, const char *s, ...);
00210 
00211   void copy(char *to_stuff, int count) const;
00213 
00219   void stuff(char *to_stuff, int count) const { copy(to_stuff, count); }
00221 
00222   astring operator + (const astring &s) const;
00224 
00225   astring &operator += (const astring &s);
00227 
00228   astring &operator += (const char *s);  // this is efficient.
00230   astring operator + (const char *s) const { return *this + astring(s); }
00232     // this method is not efficient.
00233 
00234   astring &operator += (char c);  
00235 
00236   int find(char to_find, int position = 0, bool reverse = false) const;
00238 
00241   int find(const astring &to_find, int posn = 0, bool reverse = false) const;
00243 
00244   int ifind(char to_find, int position = 0, bool reverse = false) const;
00246   int ifind(const astring &to_find, int posn = 0, bool reverse = false) const;
00248 
00249   int find_any(const char *to_find, int position = 0,
00250           bool reverse = false) const;
00252 
00254   int ifind_any(const char *to_find, int position = 0,
00255           bool reverse = false) const;
00257 
00259   int find_non_match(const char *to_find, int position = 0,
00260           bool reverse = false) const;
00262 
00263   bool contains(const astring &to_find) const;
00265 
00266   bool substring(astring &target, int start, int end) const;
00268 
00269   astring substring(int start, int end) const;
00271 
00273   // helper methods similar to other string's choppers.
00274   astring middle(int start, int count);
00276   astring left(int count);
00278   astring right(int count);
00280 
00281   void pad(int length, char padding = ' ');
00283 
00285   void trim(int length);
00287 
00288   void insert(int position, const astring &to_insert);
00290 
00291   virtual void zap(int start, int end);
00293 
00296   void to_lower();
00298 
00300   void to_upper();
00302   astring lower() const;
00304   astring upper() const;
00306 
00307   bool replace(const astring &tag, const astring &replacement);
00309 
00310   bool replace_all(char to_replace, char new_char);
00312   bool replace_all(const astring &to_replace, const astring &new_string);
00314 
00315   void shrink();
00317 
00322   enum how_to_strip { FROM_FRONT = 1, FROM_END = 2, FROM_BOTH_SIDES = 3 };
00324 
00325   void strip(const astring &strip_list, how_to_strip way = FROM_BOTH_SIDES);
00327 
00328   void strip_spaces(how_to_strip way = FROM_BOTH_SIDES)
00329           { strip(" ", way); }
00331 
00332   void strip_white_spaces(how_to_strip way = FROM_BOTH_SIDES)
00333           { strip(" \t", way); }
00335 
00336   static bool matches(const astring &match_list, char to_match);
00338 
00339   int packed_size() const;
00341 
00342   void pack(byte_array &target) const;
00344   bool unpack(byte_array &source);
00346 
00350 //hmmm: rename this--it is not a simple icompare, but a strncasecmp analogue.
00351 //  int icompare(const astring &to_compare, int length = -1) const;
00353 
00363 
00364 
00365 
00369 
00378   // play-yard for implementing base class requirements.
00379 
00380   // these implement the orderable and equalizable interfaces.
00381   virtual bool equal_to(const equalizable &s2) const;
00382   virtual bool less_than(const orderable &s2) const;
00383 
00384   virtual base_string &concatenate_string(const base_string &s);
00385   virtual base_string &concatenate_char(char c);
00386   virtual base_string &assign(const base_string &s);
00387   virtual base_string &upgrade(const char *s);
00388   virtual bool sub_string(base_string &target, int start, int end) const;
00389   virtual bool sub_compare(const base_string &to_compare, int start_first,
00390       int start_second, int count, bool case_sensitive) const;
00391   virtual void insert(int position, const base_string &to_insert);
00392   virtual void text_form(base_string &state_fill) const;
00393 
00394 private:
00395   byte_array c_character_manager;
00397 
00398   // the real find methods.
00399   int char_find(char to_find, int position, bool reverse,
00400           bool case_sense) const;
00401   // if "invert_find" is true, then non-matches are reported instead of matches.
00402   int char_find_any(const astring &to_find, int position, bool reverse,
00403           bool case_sense, bool invert_find = false) const;
00404   int str_find(const astring &to_find, int posn, bool reverse,
00405           bool case_s) const;
00406 
00407   // the functions below are used in the formatting string constructor.
00408 public:  // only for base_sprintf.
00409   astring &base_sprintf(const char *s, va_list &args);
00410 private:
00411   char *const *c_held_string;  
00412 
00413   void seek_flag(const char *&traverser, char *flag_chars, bool &failure);
00415   void seek_width(const char *&traverser, char *width_chars);
00417   void seek_precision(const char *&traverser, char *precision_chars);
00419   void seek_modifier(const char *&traverser, char *modifier_char);
00421   void get_type_character(const char *&traverser, va_list &args,
00422         astring &output_string, const char *flag_chars,
00423         const char *width_chars, const char *precision_chars,
00424         const char *modifier_chars);
00430   public: byte_array &get_implementation(); private: // for test programs only....
00431 };
00432 
00434 
00436 
00439 class a_sprintf : public astring
00440 {
00441 public:
00442   a_sprintf();
00443   a_sprintf(const char *initial, ...);
00444   a_sprintf(const astring &s);
00445 };
00446 
00448 
00449 typedef bool string_comparator_function(const astring &a, const astring &b);
00451 
00455 bool astring_comparator(const astring &a, const astring &b);
00457 
00459 
00460 void attach(byte_array &packed_form, const char *to_attach);
00462 bool detach(byte_array &packed_form, astring &to_detach);
00464 
00465 } //namespace.
00466 
00467 #endif
00468 
Generated on Sat Jan 28 04:22:11 2012 for hoople2 project by  doxygen 1.6.3