#ifndef __AMSSTRING4_HPP__
#define __AMSSTRING4_HPP__

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdarg.h>
#include <vector>
#include <string>
#include <limits>
#include <locale>

#include <amsmathutil25/amsmathutil25.hpp>


namespace ams
{

static const int amsstring_success = 1;
static const int amsstring_failure = -1;

//wraps the functions strcpy_s and strncpy in a portable manner
//between linux and microsoft standard C libraries.
int amsstrcpy_s(char *dest, int size, const char *src);

//wraps the functions sprintf_s and snprintf in a portable manner
//between linux and microsoft standard C libraries.
int amssprintf_s(char *s, int n, const char *format, ...);

//Using the C library's sscanf function is more robust
//than atod or atof. It returns valid numbers for infs and nans
//Returns nan for any uninterpretable string
double amsstrtonum(const char *str);

typedef char ams_chartype;
static const ams_chartype ams_char_cr = (ams_chartype) '\r'; //carriage return
static const ams_chartype ams_char_lf = (ams_chartype) '\n'; //newline
static const ams_chartype ams_char_tb = (ams_chartype) '\t'; //tab
static const ams_chartype ams_char_nt = (ams_chartype) '\0'; //null terminator

class amsstring
{
public:
    ams_chartype *cstring;
    ams_chartype blank; // null terminator returned for accessing index out of bounds
    int length;
    //length will be set to the length of the cstring not including the null terminating char

    //Basic functions
    amsstring();
    ~amsstring();

    amsstring(const amsstring &other);
    amsstring& operator=(const amsstring& other);
    amsstring(amsstring &&other) noexcept;
    amsstring& operator=(amsstring &&other) noexcept;
    amsstring(const ams_chartype *other);
    const amsstring& operator=(const ams_chartype *other);
    
    //amsstring(int length);
    //amsstring(int length, const ams_chartype initchar);

    int resize(const int newlen);
    int size() const;

    ams_chartype& operator[](const int ind);
    const ams_chartype& operator[](const int ind) const;
    ams_chartype& at(const int ind);
    const ams_chartype& at(const int ind) const;

    void clear();
    void setall(const ams_chartype val, const int newlen);
    void shrinktofit();

    //string comparisons
    bool operator==(const amsstring &other) const;
    bool operator==(const char *other) const;
    bool operator!=(const amsstring &other) const;
    bool operator!=(const char *other) const;

    //string ordering comparison
    //alphebetizes strings by ASCII character
    //longer strings compare larger than shorter ones
    bool operator<(const amsstring &other) const;
    bool operator>(const amsstring &other) const;
    bool operator<(const ams_chartype *other) const;
    bool operator>(const ams_chartype *other) const;

    //Insert, Remove, and Substring
    void insert(const int ind, const amsstring other);
    void insert(const int ind, const ams_chartype *other);
    void remove(const int ind);
    void remove(const int ind1, const int ind2);
    void substring(const int ind1, const int ind2, amsstring *sout) const;

    //Append
    void append(const amsstring &other);
    void append(const ams_chartype *other);
    void append(const ams_chartype other);

    amsstring operator+(const amsstring &other);
    const amsstring operator+(const amsstring &other) const;
    amsstring operator+(const ams_chartype *other);
    const amsstring operator+(const ams_chartype *other) const;
    amsstring operator+(const ams_chartype other);
    const amsstring operator+(const ams_chartype other) const;

    //Find
    int find(const amsstring findstr, const int indstart=0, const bool casesens=1) const;
    int find(const ams_chartype *findstr, const int indstart=0, const bool casesens=1) const;
    int find(const ams_chartype c, const int indstart=0, const bool casesens=1) const;

    //formatted input
    int sprintf(int bufflen, const ams_chartype *formatstring, ...);

    void tolower();
    void toupper();

    bool isvalidnumber();
    double strtonum();
};

//needs work
void splitlines(amsstring *s, std::vector<amsstring> *lns);
void splitlines(amsstring *s, ams::amsarray<amsstring> *lns);

void split(amsstring *s, const ams_chartype delimitchar, std::vector<amsstring> *lns);
void split(amsstring *s, const ams_chartype delimitchar, ams::amsarray<amsstring> *lns);

void split(amsstring *s, const ams_chartype *delimitstr, std::vector<amsstring> *lns);
void split(amsstring *s, const ams_chartype *delimitstr, ams::amsarray<amsstring> *lns);

void split(amsstring *s, amsstring *delimitstr, std::vector<amsstring> *lns);
void split(amsstring *s, amsstring *delimitstr, ams::amsarray<amsstring> *lns);

//splits a string, not counting whitespaces between non-whitespace characters
void splitwhitespace(amsstring *s, std::vector<amsstring> *lns);
void splitwhitespace(amsstring *s, ams::amsarray<amsstring> *lns);

//removes all whitespace characters '\t','\r','\n' included
//to the left and right of the string (but not in the middle)
void stripwhitespace(amsstring *s);

//completely removes all whitespace entirely
void stripallwhitespace(amsstring *s);

void freadline(FILE *fp, amsstring *s);
void freadlines(FILE *fp, std::vector<amsstring> *lines);
void fwritelines(FILE *fp, amsstring *s);
void fwritelines(FILE *fp, std::vector<amsstring> *lines);
void freadtxtfile(FILE *fp, amsstring *s);

}; //end namespace ams

#include <amsstring4/amsstring4_unicode.hpp>
#include <amsstring4/amsstring4_bintextencoding.hpp>
#include <amsstring4/amsstring4_tests.hpp>


#endif