/* cstring.c

   David Beazley  */

#include "cstring.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>

String::String(const char *cs) {
  if (!cs) cs = "";
  text = new char[strlen(cs)+1];
  strcpy(text,cs);
  size = strlen(cs);
  maxlen = size;
}

String::~String() {
  delete [] text;
}

int String::len() {
  return size;
}

const char *String::cstr() {
  return (const char *) text;
}

int String::strcmp(const String *t) {
  assert(t);
  return ::strcmp(text, t->text);
}

int String::strstr(const String *t) {
  char *f;

  assert(t != NULL);
  f = ::strstr(text,t->text);
  if (!f) return -1;
  return (int) (f - text);
}


int main () {

  String *s = new String("hello");
  String *t = new String("world");

  if (s->strcmp(t) == 0) {
    printf("same\n");
    } else {
      printf("different\n");
    }

  delete s;
  delete t;
}

void String::panic() {
  printf("ARGH!\n");
}
