#import "stdlib.h"

/*
Objective-C String Class Implementation

Author: Jacob I. Torrey
Email: discipleofranok@gmail.com
Date: 3/15/07
*/

@implementation String {
  int length;
  char * string;
}

- init {
  [super init];
  length = 0;
  string = 0;
  return self;
}

- init: (int) size {
  [super init];
  length = size;
  string = (char *) malloc(length*sizeof(char));
  return self;
}

- initFromChar: (char *) toString {
  [super init];
  length = strlen(toString);
  string = (char *) malloc(length*sizeof(char));
  strcpy(string, toString);
  return self;
}

- (int) find: (char) search {
  char * outchar = 0;
  int i;
  for(i = 0; i < length; i++) {
    if(string[i] == search) {
      outchar = &string[i];
      break;
    }
  }
  return outchar - string;;
}

- (int) size {
  return length;
}

- initFromString: (String *) toString {
  length = [toString size];
  string = (char *) malloc(length*sizeof(char));
  strcpy(string, [toString toCStr]);
  return self;
}

- print {
  printf("%s", string);
}

- (char *) toCStr {
  return string;
}

- nl {
  printf("\n");
}

- append: (String *) toAppend {
  char * temp = (char *) malloc((length + [toAppend size])*sizeof(char));
  char * tas = [toAppend toCStr];
  int i, j = 0;
  for(i = 0; i < length; i++) {
    temp[j] = string[i];
    j++;
  }
  for(i = 0; i < [toAppend size]; i++) {
    temp[j] = tas[i];
    j++;
  }
  free(string);
  string = temp;
}

@end
