/* This file was generated by scramble.py. */
#include "mystring.h"
#include "scramble.h"
String * string_new(void)
{
    String *s = ALLOCATE0(sizeof *s);
    s->pointer = ALLOCATE0(1);
    s->used = 1;
    s->allocated = 1;
    return s;
}
void string_add(String *s, char c)
{
    s->used++;
    if (s->used > s->allocated)
    {
        s->allocated *= 2;
        s->pointer = REALLOCATE(s->pointer, s->allocated);
    }
    s->pointer[s->used - 2] = c;
    s->pointer[s->used - 1] = '\0';
}
char * string_c(String *s)
{
    return s->pointer;
}
void string_clear(String *s)
{
    s->used = 1;
    s->pointer[0] = '\0';
}
void string_lstrip(String *s)
{
    /* 
     * Examples:
     * "" -> ""
     * " " -> ""
     * "  a" -> "a" */
    int first = 0;
    while (1)
    {
        char c = s->pointer[first];
        if (! c)
        {
            break;
        }
        if (c == ' ' || c == '\t')
        {
            first++;
        }
        else
        {
            break;
        }
    }
    s->used -= first;
    memmove(s->pointer, s->pointer + first, s->used);
}
void string_rstrip(String *s)
{
    /* 
     * Examples:
     * "" -> ""
     * " " -> ""
     * "a  " -> "a" */
    while (s->used > 1)
    {
        char c = s->pointer[s->used - 2];
        if (c == ' ' || c == '\t')
        {
            s->used--;
            s->pointer[s->used - 1] = '\0';
        }
        else
        {
            break;
        }
    }
}
void string_strip(String *s)
{
    string_rstrip(s);
    string_lstrip(s);
}
int string_char(String *s, int i)
{
    if (i >= 0)
    {
        if (i >= s->used)
        {
            return 0;
        }
        return s->pointer[i];
    }
    else
    {
        if (-i >= s->used)
        {
            return 0;
        }
        return s->pointer[s->used + i - 1];
    }
}
bool string_is_same_c(String *s, char const *c)
{
    return ! strcmp(s->pointer, c);
}
bool string_is_empty(String *s)
{
    return s->used == 1;
}
/* This file was generated by scramble.py. */
