feisty meow concerns codebase  2.140
astring.h
Go to the documentation of this file.
1 #ifndef ASTRING_CLASS
2 #define ASTRING_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : astring *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 1992-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include "base_string.h"
19 #include "byte_array.h"
20 #include "contracts.h"
21 
22 #include <stdarg.h>
23 
24 namespace basis {
25 
27 
32 class astring
33 : public virtual base_string,
34  public virtual hoople_standard
35 {
36 public:
37  astring();
39 
40  astring(const char *initial);
42 
43  astring(char c, int repeat);
45 
48  astring(const astring &s);
50 
51  astring(const base_string &initial);
53 
54  enum special_flag { UNTERMINATED = 62, SPRINTF = 84 };
55  astring(special_flag way, const char *s, ...);
57 
73  virtual ~astring();
75 
76  DEFINE_CLASS_NAME("astring");
77 
78  virtual int comparator(const astring &s2) const;
80 
81  int length() const;
83 
86  int end() const { return length() - 1; }
88 
91  bool empty() const { return !length(); }
93  bool non_empty() const { return !empty(); }
95  bool operator ! () const { return empty(); }
97 
99  bool t() const { return !empty(); }
101 
104  static const astring &empty_string();
106 
110  virtual const char *observe() const;
112 
114  const char *c_str() const { return observe(); }
116  const char *s() const { return observe(); }
118 
119  virtual char get(int index) const;
121 
122  virtual char *access();
124 
127  char *c_str() { return access(); }
129  char *s() { return access(); }
131 
132  char &operator [] (int position);
134 
136  const char &operator [] (int position) const;
138 
141  virtual void put(int position, char to_put) { (*this)[position] = to_put; }
143 
144  astring &sprintf(const char *s, ...);
146 
148  int convert(int default_value) const;
150 
155  long convert(long default_value) const;
157  float convert(float default_value) const;
159  double convert(double default_value) const;
161 
162  bool equal_to(const char *that) const;
164 
165  bool iequals(const astring &that) const;
167  bool iequals(const char *that) const;
169 
170  bool compare(const astring &to_compare, int start_first,
171  int start_second, int count, bool case_sensitive) const;
173 
181  bool begins(const astring &maybe_prefix) const
182  { return compare(maybe_prefix, 0, 0, maybe_prefix.length(), true); }
184 
185  bool ibegins(const astring &maybe_prefix) const
186  { return compare(maybe_prefix, 0, 0, maybe_prefix.length(), false); }
188 
190  bool ends(const astring &maybe_suffix) const {
191  const int diff = length() - maybe_suffix.length();
192  return (diff >= 0) && compare(maybe_suffix, diff, 0, maybe_suffix.length(), true);
193  }
195  bool iends(const astring &maybe_suffix) const {
196  const int diff = length() - maybe_suffix.length();
197  return (diff >= 0) && compare(maybe_suffix, diff, 0, maybe_suffix.length(), false);
198  }
199 
200  astring &operator = (const astring &s);
202  astring &operator = (const char *s);
204 
205  void reset() { zap(0, end()); }
207 
208  void reset(special_flag way, const char *s, ...);
210 
211  void copy(char *to_stuff, int count) const;
213 
219  void stuff(char *to_stuff, int count) const { copy(to_stuff, count); }
221 
222  astring operator + (const astring &s) const;
224 
225  astring &operator += (const astring &s);
227 
228  astring &operator += (const char *s); // this is efficient.
230  astring operator + (const char *s) const { return *this + astring(s); }
232  // this method is not efficient.
233 
234  astring &operator += (char c);
235 
236  int find(char to_find, int position = 0, bool reverse = false) const;
238 
241  int find(const astring &to_find, int posn = 0, bool reverse = false) const;
243 
244  int ifind(char to_find, int position = 0, bool reverse = false) const;
246  int ifind(const astring &to_find, int posn = 0, bool reverse = false) const;
248 
249  int find_any(const char *to_find, int position = 0,
250  bool reverse = false) const;
252 
254  int ifind_any(const char *to_find, int position = 0,
255  bool reverse = false) const;
257 
259  int find_non_match(const char *to_find, int position = 0,
260  bool reverse = false) const;
262 
263  bool contains(const astring &to_find) const;
265 
266  bool substring(astring &target, int start, int end) const;
268 
269  astring substring(int start, int end) const;
271 
273  // helper methods similar to other string's choppers.
274  astring middle(int start, int count);
276  astring left(int count);
278  astring right(int count);
280 
281  void pad(int length, char padding = ' ');
283 
285  void trim(int length);
287 
288  void insert(int position, const astring &to_insert);
290 
291  virtual void zap(int start, int end);
293 
296  void to_lower();
298 
300  void to_upper();
302  astring lower() const;
304  astring upper() const;
306 
307  bool replace(const astring &tag, const astring &replacement);
309 
310  bool replace_all(char to_replace, char new_char);
312  bool replace_all(const astring &to_replace, const astring &new_string);
314 
315  void shrink();
317 
322  enum how_to_strip { FROM_FRONT = 1, FROM_END = 2, FROM_BOTH_SIDES = 3 };
324 
325  void strip(const astring &strip_list, how_to_strip way = FROM_BOTH_SIDES);
327 
329  { strip(" ", way); }
331 
333  { strip(" \t", way); }
335 
336  static bool matches(const astring &match_list, char to_match);
338 
339  int packed_size() const;
341 
342  void pack(byte_array &target) const;
344  bool unpack(byte_array &source);
346 
350 //hmmm: rename this--it is not a simple icompare, but a strncasecmp analogue.
351 // int icompare(const astring &to_compare, int length = -1) const;
353 
365 
369 
378  // play-yard for implementing base class requirements.
379 
380  // these implement the orderable and equalizable interfaces.
381  virtual bool equal_to(const equalizable &s2) const;
382  virtual bool less_than(const orderable &s2) const;
383 
384  virtual base_string &concatenate_string(const base_string &s);
385  virtual base_string &concatenate_char(char c);
386  virtual base_string &assign(const base_string &s);
387  virtual base_string &upgrade(const char *s);
388  virtual bool sub_string(base_string &target, int start, int end) const;
389  virtual bool sub_compare(const base_string &to_compare, int start_first,
390  int start_second, int count, bool case_sensitive) const;
391  virtual void insert(int position, const base_string &to_insert);
392  virtual void text_form(base_string &state_fill) const;
393 
394 private:
395  byte_array c_character_manager;
397 
398  // the real find methods.
399  int char_find(char to_find, int position, bool reverse,
400  bool case_sense) const;
401  // if "invert_find" is true, then non-matches are reported instead of matches.
402  int char_find_any(const astring &to_find, int position, bool reverse,
403  bool case_sense, bool invert_find = false) const;
404  int str_find(const astring &to_find, int posn, bool reverse,
405  bool case_s) const;
406 
407  // the functions below are used in the formatting string constructor.
408 public: // only for base_sprintf.
409  astring &base_sprintf(const char *s, va_list &args);
410 private:
411  char *const *c_held_string;
412 
413  void seek_flag(const char *&traverser, char *flag_chars, bool &failure);
415  void seek_width(const char *&traverser, char *width_chars);
417  void seek_precision(const char *&traverser, char *precision_chars);
419  void seek_modifier(const char *&traverser, char *modifier_char);
421  void get_type_character(const char *&traverser, va_list &args,
422  astring &output_string, const char *flag_chars,
423  const char *width_chars, const char *precision_chars,
424  const char *modifier_chars);
430  public: byte_array &get_implementation(); private: // for test programs only....
431 };
432 
434 
436 
439 class a_sprintf : public astring
440 {
441 public:
442  a_sprintf();
443  a_sprintf(const char *initial, ...);
444  a_sprintf(const astring &s);
445 };
446 
448 
449 typedef bool string_comparator_function(const astring &a, const astring &b);
451 
455 bool astring_comparator(const astring &a, const astring &b);
457 
459 
460 void attach(byte_array &packed_form, const char *to_attach);
462 bool detach(byte_array &packed_form, astring &to_detach);
464 
465 } //namespace.
466 
467 #endif
468 
a_sprintf is a specialization of astring that provides printf style support.
Definition: astring.h:440
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
virtual ~astring()
destroys any storage for the string.
Definition: astring.cpp:126
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
Definition: astring.h:113
astring & operator=(const astring &s)
Sets the contents of this string to "s".
Definition: astring.cpp:507
astring lower() const
like to_lower(), but returns a new string rather than modifying this.
Definition: astring.cpp:542
bool t() const
t() is a shortcut for the string being "true", as in non-empty.
Definition: astring.h:97
astring middle(int start, int count)
returns the middle of the string from "start" with "count" characters.
Definition: astring.cpp:883
static bool matches(const astring &match_list, char to_match)
returns true if "to_match" is found in the "match_list" string.
Definition: astring.cpp:941
void pack(byte_array &target) const
stores this string in the "target". it can later be unpacked again.
Definition: astring.cpp:961
bool replace(const astring &tag, const astring &replacement)
replaces the first occurrence of "tag" text with the "replacement".
Definition: astring.cpp:905
int packed_size() const
Reports the size required to pack this string into a byte array.
Definition: astring.cpp:959
int ifind_any(const char *to_find, int position=0, bool reverse=false) const
searches case-insensitively for any of the characters in "to_find".
Definition: astring.cpp:580
void trim(int length)
shortens the string to "length" if it's longer than that.
Definition: astring.cpp:501
byte_array & get_implementation()
Definition: astring.cpp:134
bool ends(const astring &maybe_suffix) const
returns true if this string ends with "maybe_suffix".
Definition: astring.h:187
virtual base_string & assign(const base_string &s)
Sets the contents of this string to "s".
Definition: astring.cpp:1064
virtual char get(int index) const
a constant peek at the string's internals at the specified index.
Definition: astring.cpp:138
astring operator+(const astring &s) const
Returns the concatenation of "this" and "s".
Definition: astring.cpp:732
int convert(int default_value) const
Converts the string into a corresponding integer.
Definition: astring.cpp:757
astring & operator+=(const astring &s)
Modifies "this" by concatenating "s" onto it.
Definition: astring.cpp:165
bool non_empty() const
non_empty() reports if the string has some contents.
Definition: astring.h:92
void copy(char *to_stuff, int count) const
Copies a maximum of "count" characters from this into "to_stuff".
Definition: astring.cpp:556
virtual void zap(int start, int end)
Deletes the characters between "start" and "end" inclusively.
Definition: astring.cpp:521
astring()
constructs an empty string.
Definition: astring.cpp:59
bool substring(astring &target, int start, int end) const
a version that stores the substring in an existing "target" string.
Definition: astring.cpp:865
DEFINE_CLASS_NAME("astring")
void strip_white_spaces(how_to_strip way=FROM_BOTH_SIDES)
like strip_spaces, but includes tabs in the list to strip.
Definition: astring.h:329
void strip(const astring &strip_list, how_to_strip way=FROM_BOTH_SIDES)
strips all chars from "strip_list" out of "this" given the "way".
Definition: astring.cpp:948
static const astring & empty_string()
useful wherever empty strings are needed, e.g., function defaults.
Definition: astring.cpp:128
const char * c_str() const
synonym for observe. mimics the STL method name.
Definition: astring.h:111
astring & base_sprintf(const char *s, va_list &args)
Definition: astring.cpp:183
void shrink()
changes all occurrences of "to_replace" into "new_string".
Definition: astring.cpp:168
bool operator!() const
the negation operator returns true if the string is empty.
Definition: astring.h:94
bool begins(const astring &maybe_prefix) const
Returns true if "this" string begins with "maybe_prefix".
Definition: astring.h:178
void strip_spaces(how_to_strip way=FROM_BOTH_SIDES)
removes excess space characters from string's beginning, end or both.
Definition: astring.h:325
astring & sprintf(const char *s,...)
similar to the SPRINTF constructor, but works on an existing string.
Definition: astring.cpp:174
virtual base_string & concatenate_string(const base_string &s)
Modifies "this" by concatenating "s" onto it.
Definition: astring.cpp:1050
void to_upper()
to_upper does the opposite of to_lower (that is, q becomes Q).
Definition: astring.cpp:535
virtual void text_form(base_string &state_fill) const
Provides a text view of all the important info owned by this object.
Definition: astring.cpp:130
bool iequals(const astring &that) const
returns true if this is case-insensitively equal to "that".
Definition: astring.cpp:565
astring left(int count)
returns the left "count" characters from the string.
Definition: astring.cpp:886
void stuff(char *to_stuff, int count) const
a synonym for copy().
Definition: astring.h:216
void insert(int position, const astring &to_insert)
Copies "to_insert" into "this" at the "position".
Definition: astring.cpp:892
void pad(int length, char padding=' ')
makes the string "length" characters long.
Definition: astring.cpp:493
virtual base_string & concatenate_char(char c)
concatenater for single characters.
Definition: astring.cpp:1058
void reset()
clears out the contents string.
Definition: astring.h:202
int find_non_match(const char *to_find, int position=0, bool reverse=false) const
searches for any character that is not in "to_find" and returns index.
Definition: astring.cpp:583
bool equal_to(const char *that) const
returns true if "that" is equal to this.
Definition: astring.cpp:159
int end() const
returns the index of the last (non-null) character in the string.
Definition: astring.h:86
virtual bool less_than(const orderable &s2) const
Definition: astring.cpp:149
virtual int comparator(const astring &s2) const
helps to fulfill orderable contract.
Definition: astring.cpp:156
char & operator[](int position)
accesses individual characters in "this" string.
Definition: astring.cpp:739
int find_any(const char *to_find, int position=0, bool reverse=false) const
searches for any of the characters in "to_find".
Definition: astring.cpp:577
astring right(int count)
returns the right "count" characters from the string.
Definition: astring.cpp:889
bool empty() const
empty() reports if the string is empty, that is, of zero length().
Definition: astring.h:90
virtual void put(int position, char to_put)
stores the character "to_put" at index "position" in the string.
Definition: astring.h:138
bool replace_all(char to_replace, char new_char)
changes all occurrences of "to_replace" with "new_char".
Definition: astring.cpp:929
bool ibegins(const astring &maybe_prefix) const
a case-insensitive method similar to begins().
Definition: astring.h:182
int length() const
Returns the current length of the string.
Definition: astring.cpp:132
bool unpack(byte_array &source)
retrieves a string (packed with pack()) from "source" into this string.
Definition: astring.cpp:964
virtual char * access()
provides access to the actual string held.
Definition: astring.cpp:136
astring upper() const
like to_upper(), but returns a new string rather than modifying this.
Definition: astring.cpp:549
bool compare(const astring &to_compare, int start_first, int start_second, int count, bool case_sensitive) const
Compares "this" string with "to_compare".
Definition: astring.cpp:810
virtual bool sub_string(base_string &target, int start, int end) const
Gets the segment of "this" between the indices "start" and "end".
Definition: astring.cpp:1078
int find(char to_find, int position=0, bool reverse=false) const
Locates "to_find" in "this".
Definition: astring.cpp:574
bool contains(const astring &to_find) const
Returns true if "to_find" is contained in this string or false if not.
Definition: astring.cpp:162
virtual bool sub_compare(const base_string &to_compare, int start_first, int start_second, int count, bool case_sensitive) const
Compares "this" string with "to_compare".
Definition: astring.cpp:1085
bool iends(const astring &maybe_suffix) const
Definition: astring.h:192
int ifind(char to_find, int position=0, bool reverse=false) const
like the find() methods above, but case-insensitive.
Definition: astring.cpp:571
virtual const char * observe() const
observes the underlying pointer to the zero-terminated string.
Definition: astring.cpp:140
void to_lower()
to_lower modifies "this" by replacing capitals with lower-case.
Definition: astring.cpp:528
virtual base_string & upgrade(const char *s)
Sets the contents of this string to "s".
Definition: astring.cpp:1072
Defines the base class for all string processing objects in hoople.
Definition: base_string.h:28
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
the base class of the most easily used and tested objects in the library.
Definition: contracts.h:161
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
bool string_comparator_function(const astring &a, const astring &b)
returns true if the strings "a" and "b" are considered equal.
Definition: astring.h:449
void attach(byte_array &packed_form, const char *to_attach)
Packs a character string "to_attach" into "packed_form".
Definition: astring.cpp:1015
bool detach(byte_array &packed_form, astring &to_detach)
Unpacks a character string "to_attach" from "packed_form".
Definition: astring.cpp:1023
bool astring_comparator(const astring &a, const astring &b)
implements a string comparator that just does simple astring ==.
Definition: astring.cpp:53