view anagram/support/agstring.h @ 21:1c9dac05d040

Add lint-style FALLTHROUGH annotations to fallthrough cases. (in the parse engine and thus the output code) Document this, because the old output causes warnings with gcc10.
author David A. Holland
date Mon, 13 Jun 2022 00:04:38 -0400
parents 13d2b8934445
children
line wrap: on
line source

/**********************************************************

The AnaGram Class Library

The AgString Class
Copyright 1997 Parsifal Software. All Rights Reserved.
See the file COPYING for license and usage terms.

***********************************************************/

#ifndef AGSTRING_H
#define AGSTRING_H

#include <string.h>
#include "port.h"

#include "agcontainer.h"


class AgString : public AgIndexedContainer<char> {
protected:
  char *store;

  void lock();
  void unlock();
  void allocate(unsigned n);

public:
  class Cut {
    char *store;
    int index;

    void lock();
    void unlock();

  public:
    Cut() : store(0), index(0) {}
    Cut(const AgString &s, const int x);
    ~Cut() { unlock(); }
    int exists() const { return store != 0; }

    char &character() const;

    AgString leftI() const;
    AgString leftX() const;
    AgString rightI() const;
    AgString rightX() const;

    //AgString insertLeft(const AgString &s) const;
    //AgString insertLeft(const char *s) const;
    //AgString insertRight(const AgString &s) const;
    //AgString insertRight(const char *s) const;
    //String replace(const AgString &s) const;
    //String replace(const char *s) const;
  };

  inline AgString() : store(0) {}
  AgString(const unsigned n);

  AgString(const char *s);
  AgString(const char *s, const unsigned n);
  AgString(const AgString &s, const unsigned n);
  AgString(const AgString &s);
  AgString &operator = (const AgString &s);
  AgString &operator = (const char *s);

  char &operator [] (const unsigned x);

  const char &operator [] (const unsigned x) const;
  AgString &discardData();
  ~AgString();

  AgString concat(const char *s) const;
  AgString concat(const AgString s) const;

  inline int exists() const { return store != 0; }
  inline unsigned size() const { return store ? strlen(store) : 0; }

  AgString &toUpper();
  //AgString &toLower();

  inline char *pointer() const {
    return store;
  }
  inline unsigned char *unsignedPointer() const {
    return (unsigned char *) store;
  }

  int operator <  (const AgString &s) const;
  int operator <= (const AgString &s) const;
  int operator >  (const AgString &s) const;
  int operator >= (const AgString &s) const;
  int operator == (const AgString &s) const;
  int operator != (const AgString &s) const;
  int iEq(const AgString &s) const;

  int operator <  (const char *s) const;
  int operator <= (const char *s) const;
  int operator >  (const char *s) const;
  int operator >= (const char *s) const;
  int operator == (const char *s) const;
  int operator != (const char *s) const;
  int iEq(const char *s) const;

  static AgString format(const char *fs, ...) PRINTFFY(1,2);

  Cut firstCut(const char c) const;
  Cut lastCut(const char c) const;
  Cut firstCut(const char* s) const;
  Cut lastCut(const char* s) const;
};

inline AgString::AgString(const AgString &s)
  : AgIndexedContainer<char>()
{
  store = s.store;
  lock();
}

inline AgString::~AgString() {
  unlock();
}

inline AgString &AgString::discardData() {
  unlock();
  store = 0;
  return *this;
}

inline AgString &AgString::operator = (const char *s) {
  return *this = AgString(s);
}

#endif /* AGSTRING_H */