#include <List.hpp>
int maxNumberOfCharsToShow = 10;
void setup (void)
{
Serial.begin (115200);
delay(200);
ProcessSentance("Hello World");
//ProcessSentance("Hi You");
ProcessSentance("Hi With More Words");
ProcessSentance("reallytest");
//ProcessSentance("Hi \nWith More Words");
//ProcessSentance("Hi \n\nWit\nh More Words");
//ProcessSentance("This is longword with more text");
//ProcessSentance("This is reallyword");
}
void loop () {
delay(100);
}
void ProcessSentance(String sentance)
{
List<String> words;
Serial.println("--------------------");
//Remove trailing and preceding whitespace
sentance.trim();
//Replace any double newlines with a single one, not allowing anymore then one
while(sentance.indexOf("\n\n") != -1)
{
sentance.replace("\n\n", "\n");
}
//Remove pre-ceding space to a newline
while(sentance.indexOf(" \n") != -1)
{
sentance.replace(" \n", "\n");
}
//Replace all double spaces
while(sentance.indexOf(" ") != -1)
{
sentance.replace(" ", " ");
}
// Split the string into substrings based on spaces
while (sentance.length() > 0)
{
//Find the next space
int indexOfSpace = sentance.indexOf(' ');
//If not space, take the rest of the string that is left
//Else, process the part of the string
if (indexOfSpace == -1)
{
words.add(sentance);
break;
}
else
{
words.add(sentance.substring(0, indexOfSpace));
sentance = sentance.substring(indexOfSpace + 1);
}
}
//Now process the words into lines where possible
List<String> lines;
String inProgressLine;
while(!words.isEmpty())
{
//Always get the first word
String wordItem = words[0];
//Only measure the word up until the first newline
int indexOfFirstNewline = wordItem.indexOf('\n');
//If the first character is not a newline see if we can fit it on the current line, otherwise don't bother
if (indexOfFirstNewline != 0)
{
//Determine the word length up till the first newline as to see if it will fit on the current line
int wordLength = indexOfFirstNewline == -1 ? wordItem.length() : indexOfFirstNewline;
//If we cannot fit everything on the same line, start a new line
if (inProgressLine.length() + wordLength + 1 > maxNumberOfCharsToShow && inProgressLine != "")
{
lines.add(inProgressLine);
inProgressLine = "";
}
//Only if we have a line that has letters in it, do we want a space on the end to split up the words
if (inProgressLine.length() > 0)
{
inProgressLine = inProgressLine + " ";
}
}
//Process the current word
int currentWordLength = wordItem.length();
for(int letterIndex = 0; letterIndex < currentWordLength; letterIndex++)
{
char wordItemLetter = wordItem[letterIndex];
if (wordItemLetter == '\n')
{
lines.add(inProgressLine);
inProgressLine = "";
}
else
{
//If we need to break up the word as its too long, then we need to add a "-"
if (inProgressLine.length() == maxNumberOfCharsToShow - 1 && letterIndex < currentWordLength - 1)
{
inProgressLine = inProgressLine + "-";
lines.add(inProgressLine);
inProgressLine = "";
}
//Business as usual, add the letter onto the in progress line
inProgressLine = inProgressLine + wordItemLetter;
if (inProgressLine.length() == maxNumberOfCharsToShow)
{
lines.add(inProgressLine);
inProgressLine = "";
}
}
}
//Remove now we've processed that word
words.remove(0);
//Don't forgot the last processed item...
if (words.getSize() == 0 && inProgressLine != "")
{
lines.add(inProgressLine);
inProgressLine = "";
}
}
//Show the resulting lines
for (int i = 0; i < lines.getSize(); i++)
{
Serial.print("Line ");
Serial.print(i + 1);
Serial.print(": \"");
Serial.print(lines[i]);
Serial.println("\"");
}
}