GNU   davin.50webs.com/research
Bringing to you notes for the ages

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Scruff the Cat       My Life Story          Smoking Cessation          Other Links      
Debugging Macros     String Class I     Linked List System I Java for C Programmers Naming Convention
    String Class II         How I use m4              Strings III                 Symmetrical I/O             Linked Lists II     
Run-Time Type Info   Virtual Methods      An Array System        Science & Religion            Submodes       
  Nested Packages      Memory Leaks    Garbage Collection      Internet & Poverty      What is Knowledge?
Limits of Evolution   Emacs Additions      Function Plotter           Romantic Love        The Next Big Thing
    Science Fiction     Faster Compilation Theory of Morality         Elisp Scoping               Elisp Advice      
  S.O.G.M. Pattern       Safe Properties         School Bullying          Charisma Control          Life and Death    
     Splitting Java          Multiple Ctors       Religious Beliefs         Conversation 1           Conversation 2    
   J.T.W. Language    Emacs Additions II      Build Counter             Relation Plotter          Lisp++ Language  
  Memory Leaks II   Super Constructors CRUD Implementation Order a Website Form There Is An Afterlife
More Occam's Razor C to Java Translator Theory of Morality II


string.hh

    
#ifndef ALREADY_INCLUDED_STRING_HH
#define ALREADY_INCLUDED_STRING_HH

#include <string.h>   /* for strcmp */
#include "../noallegro/my-allegro.hh"

//class string;
//class string_buffer;

class String_Internal
{
public:
   static const int MIN_LENGTH = 50; // was 50
   int ref_count;
   int length;
   int maxlength;
   char array[1];                       // GUARANTEED:  array[length] == '\0'
                                        // note the extra 1 here means that the
                                        // following invariant must hold: length <= maxlength
                                        // so appending can occur if length < maxlength

public:
   /* Similar to a constructor, but lower level.  Called
    * after a malloc call like so:
    *
    * ptr = (String_Internal*)malloc(sizeof(String_Internal) + newlen);
    * ptr->init(newlen);
    */
   void init(int maxlength)
   {
      this->ref_count = 1;
      this->length = 0;
      this->maxlength = maxlength;
      this->array[0] = 0;
   }
};

/* This class is a reference-counted pointer to
 * the true String_Internal object.
 */
class string
{
protected:
   String_Internal* ptr;

void worker_current_down()          { if (--ptr->ref_count == 0) { delete ptr; ptr = null; } }
   void worker_new_up(const string& s) { s.ptr->ref_count++; }
   void worker_mutator_clone();
   /* The advantage of this function over the standard
    * C library strncpy is that this one returns the
    * number of characters copied.
    */
   friend int my_strncpy(char* to, const char* from, int mem_size);
public:
   /* STANDARD FOUR BASIC OPERATIONS: */
   string();
   string(const string& s);
   string& operator = (const string& s);
   ~string();
   /* END STANDARD FOUR BASIC OPERATIONS! */



/*
    * Needed so you can write:
    * string s = 'c';
    */
   string(char ch);
   /*
    * Needed so you can write:
    * string s = 123;
    */
   string(int i);
   /*
    * Needed so you can write:
    * string s = 123.456;
    */
   string(double d);
   /*
    * Needed so you can write:
    * string s = "hello";
    */
   string(const char* s);
   /*
    * Needed so you can write:
    * string s;
    * s = "hello";
    */
    string& operator = (const char* s);
   /*
    * Needed so you can write:
    * string s;
    * s += "hello";
    */
   string& operator += (const string& s);
   string& operator += (const char* s);
   /*
    * patterned after Java's equals method.
    */
   bool equals(const string& other) const
   {
      return !(strcmp(this->ptr->array, other.ptr->array));
   }
   bool equals(const char* other) const
   {
      return !(strcmp(this->ptr->array, other));
   }
   /*
    * Needed so you can access legacy const char* functions
    *
    */
   const char* const_char_star() const {
      return ptr->array;
   }

   //operator const char*() const;
   /*
    * For accessing legacy (non-const) char* functions:
    *
    * Example:
    *
    * void foo(string s, int bar)
    * {
    *    const int MAX_LENGTH = 200
    *    char array[MAX_LENGTH];
    *    sprintf(s.char_star(MAX_LENGTH,array),"bar=%d\n", bar);
    *    cout << s;
    * }
    */
   char* char_star(int mem_size, char * s) const;
   /*
    * From Java
    */
   //string substring(int start, int stop) const;
   /*
    * From Java
    */
   char get_char_at(int index) const;
   /*
    * From Java
    */
   int  get_length() const;

friend bool strings_equal(const char* s1, const string& s2) {
      if ((s1 == null) && (&s2 == null)) {
         return true;
      } else if ((s1 == null) || (&s2 == null)) {
         return false;
      } else {
         return strcmp(s1, s2.ptr->array) == 0;
      }
   }
   friend bool strings_equal(const string& s1, const char* s2) {
      if ((&s1 == null) && (s2 == null)) {
         return true;
      } else if ((&s1 == null) || (s2 == null)) {
         return false;
      } else {
         return (0 == strcmp(s1.ptr->array, s2));
      }
   }
   friend bool strings_equal(const string& s1, const string& s2) {
      if ((&s1 == null) && (&s2 == null)) {
         return true;
      } else if ((&s1 == null) || (&s2 == null)) {
         return false;
      } else {
         return (0 == strcmp(s1.ptr->array, s2.ptr->array));
      }
   }

};

///
/// NOTE: const string& is used rather
/// than string, because the former
/// is more efficient
/// 
extern string substring(const string& s, int start, int stop);
extern string quoted(const string& s);
extern string reversed(const string& s);
extern string capitalised(const string& s);
extern int    index_of(const string& s, const string& key);

#endif /* ALREADY_INCLUDED_STRING_HH */
Back
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked Lists II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter | Romantic Love | The Next Big Thing |
| Science Fiction | Faster Compilation | Theory of Morality | Elisp Scoping | Elisp Advice |
| S.O.G.M. Pattern | Safe Properties | School Bullying | Charisma Control | Life and Death |
| Splitting Java | Multiple Ctors | Religious Beliefs | Conversation 1 | Conversation 2 |
| J.T.W. Language | Emacs Additions II | Build Counter | Relation Plotter | Lisp++ Language |
| Memory Leaks II | Super Constructors | CRUD Implementation | Order a Website Form | There Is An Afterlife |
| More Occam's Razor | C to Java Translator | Theory of Morality II
Last modified: Sun Sep 25 16:09:01 NZDT 2016
Best viewed at 800x600 or above resolution.
© Copyright 1999-2016 Davin Pearson.
Please report any broken links to