#ifndef STRING_C
#define STRING_C

#include <stdlib.h>
#include <string.h>

typedef struct {
  char *data;
  size_t length;
} string_t;

string_t string_create(const char *cstr) {
  string_t s;
  s.length = strlen(cstr);
  s.data = malloc((s.length + 1) * sizeof(char));
  if (s.data != NULL) {
    strcpy(s.data, cstr);
  }
  return s;
}

void string_free(string_t *s) {
  if (s->data != NULL) {
    free(s->data);
    s->data = NULL;
    s->length = 0;
  }
}

string_t string_adapt(char *cstr) {
  string_t s;
  s.length = strlen(cstr);
  s.data = cstr;
  return s;
}

string_t string_concat(string_t s1, string_t s2) {
  string_t result;
  result.length = s1.length + s2.length;
  result.data = malloc((result.length + 1) * sizeof(char));
  if (result.data != NULL) {
    strcpy(result.data, s1.data);
    strcat(result.data, s2.data);
  }
  return result;
}

#endif