From 0c7c741130006cf6e2305279530cfc909bf7e22b Mon Sep 17 00:00:00 2001 From: mikee47 Date: Wed, 24 Oct 2018 15:13:25 +0100 Subject: [PATCH 1/2] Revise SplitString * Doesn't need to be a friend of String, but #included inside WString.h for convenience * Use unsigned instead of int where appropriate * Minor improvements --- Sming/Wiring/SplitString.cpp | 86 +++++++++++------------------------- Sming/Wiring/SplitString.h | 28 ++++++++---- Sming/Wiring/WString.h | 4 +- 3 files changed, 47 insertions(+), 71 deletions(-) diff --git a/Sming/Wiring/SplitString.cpp b/Sming/Wiring/SplitString.cpp index 566029ce91..1e49203841 100644 --- a/Sming/Wiring/SplitString.cpp +++ b/Sming/Wiring/SplitString.cpp @@ -1,94 +1,62 @@ -#include "SplitString.h" - -int splitString(String &what, int delim, Vector &splits) -{ - what.trim(); - splits.removeAllElements(); - const char *chars = what.buffer; - int splitCount = 0; //1; - for (int i = 0; i < what.length(); i++) - { - if (chars[i] == delim) splitCount++; - } - if (splitCount == 0) - { - splits.addElement(atol(what.buffer)); - return 1; - } - - int pieceCount = splitCount + 1; +/* $Id: WMemory.cpp 1156 2011-06-07 04:01:16Z bhagman $ +|| +|| @author Hernando Barragan +|| @url http://wiring.org.co/ +|| @contribution Brett Hagman +|| @contribution Alexander Brevig +|| +|| @license Please see cores/Common/License.txt. +|| +*/ - int splitIndex = 0; - int startIndex = 0; - for (int i = 0; i < what.length(); i++) - { - if (chars[i] == delim) - { - splits.addElement(atol(what.substring(startIndex, i).buffer)); - splitIndex++; - startIndex = i + 1; - } - } - splits.addElement(atol(what.substring(startIndex, what.length()).buffer)); - - return pieceCount; -} +#include "SplitString.h" -int splitString(String &what, int delim, Vector &splits) +int splitString(String &what, char delim, Vector &splits) { what.trim(); splits.removeAllElements(); - const char *chars = what.buffer; - int splitCount = 0; //1; - for (int i = 0; i < what.length(); i++) + const char *chars = what.c_str(); + unsigned splitCount = 0; + for (unsigned i = 0; i < what.length(); i++) { if (chars[i] == delim) splitCount++; } if (splitCount == 0) { - splits.addElement(atoi(what.buffer)); + splits.addElement(what.toInt()); return(1); } - int pieceCount = splitCount + 1; - - int splitIndex = 0; int startIndex = 0; - for (int i = 0; i < what.length(); i++) + for (unsigned i = 0; i < what.length(); i++) { - if (chars[i] == delim) + if(what[i] == delim) { - splits.addElement(atoi(what.substring(startIndex, i).buffer)); - splitIndex++; + splits.addElement(what.substring(startIndex, i).toInt()); startIndex = i + 1; } } - splits.addElement(atoi(what.substring(startIndex, what.length()).buffer)); + splits.addElement(what.substring(startIndex).toInt()); - return pieceCount; + return splits.count(); } -int splitString(String &what, int delim, Vector &splits) +int splitString(String &what, char delim, Vector &splits) { what.trim(); splits.removeAllElements(); - const char *chars = what.buffer; - int splitCount = 0; + const char *chars = what.c_str(); - int splitIndex = 0; int startIndex = 0; - for (int i = 0; i < what.length(); i++) + for (unsigned i = 0; i < what.length(); i++) { if (chars[i] == delim) { - splits.addElement(what.substring(startIndex, i).buffer); - splitIndex++; + splits.addElement(what.substring(startIndex, i)); startIndex = i + 1; - splitCount++; } } - splits.addElement(what.substring(startIndex, what.length()).buffer); - - return (splitCount + 1); + splits.addElement(what.substring(startIndex)); + return splits.count(); } diff --git a/Sming/Wiring/SplitString.h b/Sming/Wiring/SplitString.h index 25bc20ac1b..89fab8f076 100644 --- a/Sming/Wiring/SplitString.h +++ b/Sming/Wiring/SplitString.h @@ -5,12 +5,6 @@ || @contribution Brett Hagman || @contribution Alexander Brevig || -|| @description -|| | Implementation of c++ new/delete operators. -|| | -|| | Wiring Common API -|| # -|| || @license Please see cores/Common/License.txt. || */ @@ -22,8 +16,24 @@ #include "WString.h" #include "WiringFrameworkDependencies.h" -int splitString(String &what, int delim, Vector &splits); -int splitString(String &what, int delim, Vector &splits); -int splitString(String &what, int delim, Vector &splits); +/** @brief split a delimited string list of integers into an array + * @param what + * @param delim + * @param splits + * @retval number of items returned in splits (same as splits.count()) + * @note leading/trailing whitespace is removed from 'what' before parsing + * example: " 1,2,3,4,5" returns [1, 2, 3, 4, 5] + */ +int splitString(String &what, char delim, Vector &splits); + +/** @brief split a delimited string list into an array + * @param what + * @param delim + * @param splits + * @retval number of items returned in splits (same as splits.count()) + * @note leading/trailing whitespace is removed from 'what' before parsing + * example: " a,b,c,d,e" returns ["a", "b", "c", "d", "e"] + */ +int splitString(String &what, char delim, Vector &splits); #endif diff --git a/Sming/Wiring/WString.h b/Sming/Wiring/WString.h index 7af156d1b7..299ea0b63c 100644 --- a/Sming/Wiring/WString.h +++ b/Sming/Wiring/WString.h @@ -344,9 +344,6 @@ class String long toInt(void) const; float toFloat(void) const; - friend int splitString(String &what, int delim, Vector &splits); - friend int splitString(String &what, int delim, Vector &splits); - friend int splitString(String &what, int delim, Vector &splits); //void printTo(Print &p) const; @@ -385,6 +382,7 @@ class StringSumHelper : public String }; #include "FlashString.h" +#include "SplitString.h" #endif // __cplusplus #endif From 9eb7c94fefb02fdc5012c19119b6d2cb6b48a664 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Wed, 24 Oct 2018 20:32:11 +0100 Subject: [PATCH 2/2] Fix header comment filename --- Sming/Wiring/SplitString.cpp | 2 +- Sming/Wiring/SplitString.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sming/Wiring/SplitString.cpp b/Sming/Wiring/SplitString.cpp index 1e49203841..2598599dd5 100644 --- a/Sming/Wiring/SplitString.cpp +++ b/Sming/Wiring/SplitString.cpp @@ -1,4 +1,4 @@ -/* $Id: WMemory.cpp 1156 2011-06-07 04:01:16Z bhagman $ +/* $Id: SplitString.cpp 1156 2011-06-07 04:01:16Z bhagman $ || || @author Hernando Barragan || @url http://wiring.org.co/ diff --git a/Sming/Wiring/SplitString.h b/Sming/Wiring/SplitString.h index 89fab8f076..2badb00792 100644 --- a/Sming/Wiring/SplitString.h +++ b/Sming/Wiring/SplitString.h @@ -1,4 +1,4 @@ -/* $Id: WMemory.cpp 1156 2011-06-07 04:01:16Z bhagman $ +/* $Id: SplitString.h 1156 2011-06-07 04:01:16Z bhagman $ || || @author Hernando Barragan || @url http://wiring.org.co/