//#DOC Main, program entry and main loop
#include "main.h"
#include <otSerial.h>
#include <otTimer.h>
#include <otMalloc.h>
#include <otPrintf.h>
#include <otStringList.h>

otString    str;

void dump(int line, const char * _class, const char * method, U32 size, const char * contents)
{
    printf("[%5d] %15s::%15s size = %4u, -> \"%s\"\n", line, _class, method, size, contents);
}

void setup()
{
    UartEnableSignals(UART0);
    UartSetBaud(UART0, 115200);
    _initMalloc();
    setStdout(4096, UART0); // printf output is now directed on UART0
    DelayMs(1000);          // Wait terminal start
}

void loop()
{
    otString txt("A quick brown fox jumps over the lazy dog");
    dump(__LINE__, "otString", "size", str.size(), str.cstr());
    dump(__LINE__, "otString", "size", txt.size(), txt.cstr());
    
    str.append("Hello world!");
    dump(__LINE__, "otString", "size", str.size(), str.cstr());
    
    str.append(txt);
    dump(__LINE__, "otString", "size", str.size(), str.cstr());
    
    printf("indexOf = %d\n", str.indexOf("world"));
    
    otString    left = txt.left(17);
    dump(__LINE__, "otString", "left", left.size(), left.cstr());
    
    otString    right = txt.right(3);
    dump(__LINE__, "otString", "right", right.size(), right.cstr());
    
    otString    mid = txt.mid(14, 3);
    dump(__LINE__, "otString", "mid", mid.size(), mid.cstr());
    
    otString    fieldx("\t\t000 100 200 \t\t\t300 400 \t\t500 600 700     800 900  ");
    dump(__LINE__, "otString", "size", fieldx.size(), fieldx.cstr());
    fieldx.simplified();
    dump(__LINE__, "otString", "simplified", fieldx.size(), fieldx.cstr());
    
    otString    fields("000 100 200 300 400 500 600 700 800 900 ABC");
    dump(__LINE__, "otString", "size", fields.size(), fields.cstr());
    
    otStringList    f = fields.split(" ");
    dump(__LINE__, "otStringList", "size", f.size(), f.cstr(0));
    f.remove(10);
    dump(__LINE__, "otStringList", "remove", f.size(), f.cstr(0));
    
    otString        j = f.join(",");
    dump(__LINE__, "otString", "join", j.size(), j.cstr());
    
    printf("\n");
    otStringList    s;
    s.append("Hello");
    s.append("Bye");
    s.append("Understand");
    s.append("Brake");
    s.append("Zenith");
    s.append("Fork");
    s.append("Kind");
    s.append("Joi");
    
    for(U32 i = 0; i < s.size(); i++)
        printf("%d = %s\n", i, s.cstr(i));
    
    s.sort();
    printf("\n");
    
    for(U32 i = 0; i < s.size(); i++)
        printf("%d = %s\n", i, s.cstr(i));
    
    otStringList    list;
    list.append(str);
    list.append(txt);
    list.append(left);
    list.append(right);
    list.append(mid);
    dump(__LINE__, "otStringList", "append", list.size(), list.cstr(0));
    for(U32 i = 0; i < list.size(); i++)
        printf("%d = %s\n", i, list.cstr(i));
    
    IDLE;
}

/* OUTPUT

[   28]        otString::           size size =    0, -> "(null)"
[   29]        otString::           size size =   41, -> "A quick brown fox jumps over the lazy dog"
[   32]        otString::           size size =   12, -> "Hello world!"
[   35]        otString::           size size =   53, -> "Hello world!A quick brown fox jumps over the lazy dog"
indexOf = 6
[   40]        otString::           left size =   17, -> "A quick brown fox"
[   43]        otString::          right size =    3, -> "dog"
[   46]        otString::            mid size =    3, -> "fox"
[   49]        otString::           size size =   52, -> "      000 100 200             300 400         500 600 700     800 900  "
[   51]        otString::     simplified size =   52, -> "000 100 200 300 400 500 600 700 800 900"
[   54]        otString::           size size =   43, -> "000 100 200 300 400 500 600 700 800 900 ABC"
[   57]    otStringList::           size size =   11, -> "000"
[   59]    otStringList::         remove size =   10, -> "000"
[   62]        otString::           join size =   39, -> "000,100,200,300,400,500,600,700,800,900"

0 = Hello
1 = Bye
2 = Understand
3 = Brake
4 = Zenith
5 = Fork
6 = Kind
7 = Joi

0 = Brake
1 = Bye
2 = Fork
3 = Hello
4 = Joi
5 = Kind
6 = Understand
7 = Zenith
[   90]    otStringList::         append size =    5, -> "Hello world!A quick brown fox jumps over the lazy dog"
0 = Hello world!A quick brown fox jumps over the lazy dog
1 = A quick brown fox jumps over the lazy dog
2 = A quick brown fox
3 = dog
4 = fox

*/