Skip to content

Commit

Permalink
Merge pull request #33 from drmpf/V4_0_3
Browse files Browse the repository at this point in the history
V4_0_3
  • Loading branch information
PowerBroker2 authored Apr 4, 2021
2 parents 7bf51b0 + 912814c commit 670487a
Show file tree
Hide file tree
Showing 9 changed files with 340 additions and 48 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ See the top of each file for its license
Note, this is NOT my work, I am simply hosting it for easy access. The original code belongs to [Forward Computing and Control Pty. Ltd](https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html).

# Revisions
V4.0.3 allow createSafeString for small sizes, fixed bool for DUE (ARDUINO_ARCH_SAM)
V4.0.2 added flushInput() method to SafeStringReader
V4.0.1 fixed SafeStringReader timeout and NanoBLE F() macro
V4.0.0 changes method returns to better match Arduino String methods, main change is indexOf now returns int and returns -1 if not found
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ void setup() {
Serial.println(F(" (to wrap a char* use createSafeStringFromCharPtr (cSFP) instead) "));
char *charArrayPtr = charArray;
Serial.println(F("Check passing char* (NULL or otherwise) as the char[] to createSafeStringFromCharArray, prints error msg but does not blow up program."));
Serial.println(F(" Note: the sizeof(char*) varies between different Arduino boards Uno/Megs2560 sizeof(char*) == 2, more powerful boards like ESP8266/ESP32,etc sizeof(char*) == 4"));
Serial.println(F(" So for small char[] use 5 as the mininum size, ie. char cArray[5];"));
Serial.println();

Serial.println(F("char *charArrayPtr = charArray;"));
Serial.println(F("cSFA(testStr1,charArrayPtr); // using the typing shortcut name"));
cSFA(testStr1, charArrayPtr);
Expand Down Expand Up @@ -118,6 +122,63 @@ void setup() {
Serial.println();

Serial.println(F("You can now safely access and update buffers.buffer_1 via the SafeString, sfBuffer_1."));
Serial.println();

Serial.println(F("Check wrapping small char[] "));
char ch0[0];
Serial.println(F("char ch0[0];"));
Serial.println(F(" createSafeStringFromCharArray(sfStr0, ch0); // or cSFA(sfStr0, ch0); "));
createSafeStringFromCharArray(sfStr0, ch0);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharArray(sfStr0, ch0);"));
} else {
sfStr0.debug(F("No errors"));
}
Serial.println();

char ch1[1] = "";
Serial.println(F("char ch1[1]=\"\";"));
Serial.println(F(" createSafeStringFromCharArray(sfStr1, ch1); // or cSFA(sfStr1, ch1); "));
createSafeStringFromCharArray(sfStr1, ch1);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharArray(sfStr1, ch1);"));
} else {
sfStr1.debug(F("No errors"));
}
Serial.println();

char ch2[2] = "";
Serial.println(F("char ch2[2]=\"\";"));
Serial.println(F(" createSafeStringFromCharArray(sfStr2, ch2); // or cSFA(sfStr2, ch2); "));
createSafeStringFromCharArray(sfStr2, ch2);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharArray(sfStr2, ch2);"));
} else {
sfStr2.debug(F("No errors"));
}
Serial.println();

char ch3[3] = "a";
strcat(ch3,"12");
Serial.println(F("char ch3[3]=\"a\"; strcat(ch3,\"12\");"));
Serial.println(F(" createSafeStringFromCharArray(sfStr3, ch3); // or cSFA(sfStr3, ch3); "));
createSafeStringFromCharArray(sfStr3, ch3);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharArray(sfStr3, ch3);"));
} else {
sfStr3.debug(F("No errors"));
}
Serial.println();

char ch4[4] = "ab";
Serial.println(F("char ch4[4]=\"ab\";"));
Serial.println(F(" createSafeStringFromCharArray(sfStr4, ch4); // or cSFA(sfStr4, ch4); "));
createSafeStringFromCharArray(sfStr4, ch4);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharArray(sfStr4, ch4);"));
} else {
sfStr4.debug(F("No errors"));
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,51 @@ void setup() {
Serial.println(F(" or createSafeStringFromCharPtrWithSize(sfBuffer_1,buffers.buffer_1,8), cSFPS(sfBuffer_1,buffers.buffer_1,8)"));
Serial.println(F(" Note: the 8 allows for the terminating '\\0' the char buffer_1[8] can only hold a 7 char c-string"));

Serial.println();
Serial.println(F("Check wrapping small char[] as char*"));
char ch1[1] = "";
Serial.println(F("char ch1[1]=\"\";"));
Serial.println(F(" createSafeStringFromCharPtr(sfStr1, ch1); // or cSFP(sfStr1, ch1); "));
createSafeStringFromCharPtr(sfStr1, ch1);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharPtr(sfStr1, ch1);"));
} else {
sfStr1.debug(F("No errors"));
}
Serial.println();

char ch2[2] = "a";
Serial.println(F("char ch2[2]=\"a\";"));
Serial.println(F(" createSafeStringFromCharPtr(sfStr2, ch2); // or cSFP(sfStr2, ch2); "));
createSafeStringFromCharPtr(sfStr2, ch2);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharPtr(sfStr2, ch2);"));
} else {
sfStr2.debug(F("No errors"));
}
Serial.println();

char ch3[3] = "a";
Serial.println(F("char ch3[3]=\"a\";"));
Serial.println(F(" createSafeStringFromCharPtr(sfStr3, ch3); // or cSFP(sfStr3, ch3); "));
createSafeStringFromCharPtr(sfStr3, ch3);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharPtr(sfStr3, ch3);"));
} else {
sfStr3.debug(F("No errors"));
}
Serial.println();

char ch4[4] = "ab";
Serial.println(F("char ch4[4]=\"ab\";"));
Serial.println(F(" createSafeStringFromCharPtr(sfStr4, ch4); // or cSFP(sfStr4, ch4); "));
createSafeStringFromCharPtr(sfStr4, ch4);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharPtr(sfStr4, ch4);"));
} else {
sfStr4.debug(F("No errors"));
}

}

void loop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,63 @@ void setup() {
sfBuffer_1.debug();
Serial.println();
Serial.println(F("When working with char*, SafeString always terminates the array at its capacity(), in this case overwriting the 7 with a terminating '\\0'"));
Serial.println();

Serial.println(F("Check wrapping small char[] as char* with size"));
char ch0[0];
Serial.println(F("char ch0[0];"));
Serial.println(F(" createSafeStringFromCharPtrWithSize(sfStr0, ch0, 0); // or cSFPS(sfStr0, ch0, 0); "));
createSafeStringFromCharPtrWithSize(sfStr0, ch0, 0);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharPtrWithSize(sfStr0, ch0, 0);"));
} else {
sfStr0.debug(F("No errors"));
}
Serial.println();

char ch1[1] = "";
Serial.println(F("char ch1[1]=\"\";"));
Serial.println(F(" createSafeStringFromCharPtrWithSize(sfStr1, ch1, 1); // or cSFPS(sfStr1, ch1, 1); "));
createSafeStringFromCharPtrWithSize(sfStr1, ch1, 1);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharPtrWithSize(sfStr1, ch1, 1);"));
} else {
sfStr1.debug(F("No errors"));
}
Serial.println();

char ch2[2] = "";
Serial.println(F("char ch2[2]=\"\";"));
Serial.println(F(" createSafeStringFromCharPtrWithSize(sfStr2, ch2,2); // or cSFPS(sfStr2, ch2, 2); "));
createSafeStringFromCharPtrWithSize(sfStr2, ch2, 2);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharPtrWithSize(sfStr2, ch2, 2);"));
} else {
sfStr2.debug(F("No errors"));
}
Serial.println();

char ch3[3] = "a";
strcat(ch3,"12");
Serial.println(F("char ch3[3] = \"a\"; strcat(ch3,\"12\");"));
Serial.println(F(" createSafeStringFromCharPtrWithSize(sfStr3, ch3, 3); // or cSFPS(sfStr3, ch3, 3); "));
createSafeStringFromCharPtrWithSize(sfStr3, ch3, 3);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharPtrWithSize(sfStr3, ch3, 3);"));
} else {
sfStr3.debug(F("No errors"));
}
Serial.println();

char ch4[4] = "ab";
Serial.println(F("char ch4[4]=\"ab\";"));
Serial.println(F(" createSafeStringFromCharPtrWithSize(sfStr4, ch4, 4); // or cSFPS(sfStr4, ch4, 4); "));
createSafeStringFromCharPtrWithSize(sfStr4, ch4, 4);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeStringFromCharPtrWithSize(sfStr4, ch4, 4);"));
} else {
sfStr4.debug(F("No errors"));
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,53 @@ void setup() {
Serial.println();
Serial.println(F(" Any error in any SafeString sets the class global error flag."));
Serial.print(F(" SafeString::errorDetected() => ")); Serial.println(SafeString::errorDetected() ? "true" : "false");
Serial.println();


Serial.println(F("Check small SafeString creation"));
Serial.println(F(" createSafeString(sfStr0, 0); // or cSF(sfStr0, 0); "));
createSafeString(sfStr0, 0);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeString(sfStr0, 0);"));
} else {
sfStr0.debug(F("No errors"));
}
Serial.println();

Serial.println(F(" createSafeString(sfStr1, 1); // or cSF(sfStr1, 1); "));
createSafeString(sfStr1, 1);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeString(sfStr1, 1);"));
} else {
sfStr1.debug(F("No errors"));
}
Serial.println();

Serial.println(F(" createSafeString(sfStr2, 2); // or cSF(sfStr2, 2); "));
createSafeString(sfStr2, 2);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeString(sfStr2, 2);"));
} else {
sfStr2.debug(F("No errors"));
}
Serial.println();

Serial.println(F(" createSafeString(sfStr3, 3); // or cSF(sfStr3, 3); "));
createSafeString(sfStr3, 3);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeString(sfStr3, 3);"));
} else {
sfStr3.debug(F("No errors"));
}
Serial.println();

Serial.println(F(" createSafeString(sfStr4, 4); // or cSF(sfStr4, 4); "));
createSafeString(sfStr4, 4);
if (SafeString::errorDetected()) {
Serial.println(F(" Error in createSafeString(sfStr4, 4);"));
} else {
sfStr4.debug(F("No errors"));
}

}

Expand Down
30 changes: 23 additions & 7 deletions examples/SafeString_Tests/SafeString_stoken/SafeString_stoken.ino
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,32 @@ void setup() {
validChars.debug(F(" Valid token chars are => "));
Serial.println();

Serial.println(F(" First use "));
Serial.println(F("nextIdx = stringOne.stoken(field, 0, validChars);"));
nextIdx = stringOne.stoken(field, 0, validChars);
Serial.print(F(" to find the first digit. This returns nextIdx : ")); Serial.println(nextIdx);
Serial.println(F(" Then use validChars not as delimiters but as the valid token chars to extract the number"));

nextIdx = 0;
Serial.println(F(" Use validChars not as delimiters but as the valid token chars to extract the number"));
Serial.println(F(" the last false does this nextIdx = stringOne.stoken(field, nextIdx, validChars, false, false); "));
Serial.println(F(" The first char not in validChars terminates the token."));
Serial.println(F(" Leading non-validChars are skipped and then the first char not in validChars terminates the token."));
nextIdx = stringOne.stoken(field, nextIdx, validChars, false, false); // first false => do not return empty fields, second false => do NOT use delimiters as delimiters but use them as valid token chars
field.debug(F("stringOne.stoken(field, nextIdx, validChars, false, false); => "));
//field.debug(F("stringOne.stoken(field, nextIdx, validChars, false, false); => "));
Serial.print(F(" Returned nextIdx is ")); Serial.println(nextIdx);
Serial.print(F(" Number is ")); Serial.println(field);
Serial.println();

char line2[] = " ,23.5,some text,335, some more text, ";
cSFP(sfLine2, line2); // wrap the line in a SafeString for processing
Serial.print(F("Input line is '")); Serial.print(sfLine2); Serial.println('\'');
Serial.print(F("validChars '")); Serial.print(validChars); Serial.println("'");
Serial.println(F("Using nextIdx = sfLine2.stoken(field, nextIdx, validChars, false,false);"));
Serial.println(F(" // false, false, => skip multiple delimiters, delimit on complement of delimiters"));
Serial.println(F(" to extract number fields :-"));
nextIdx = 0; // start from beginning of sfLine
while (nextIdx >= 0) {
nextIdx = sfLine2.stoken(field, nextIdx, validChars, false, false); // false, false, => skip multiple delimiters, delimit on complement of delimiters
if (field.isEmpty()) {
break; // no more digits found
}
Serial.print(F(" Digit Field ")); Serial.println(field);
}
Serial.println();

Serial.println(F("Error checking.."));
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SafeString
version=4.0.2
version=4.0.3
author=Matthew Ford
maintainer=Matthew Ford
sentence=A Safe, Static String library to replace Arduino String, plus non-blocking Serial I/O, I/O buffering, loopTimer and millisDelay
Expand Down
Loading

0 comments on commit 670487a

Please sign in to comment.