diff --git a/Sming/Wiring/SplitString.cpp b/Sming/Wiring/SplitString.cpp index 566029ce91..2598599dd5 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: SplitString.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..2badb00792 100644 --- a/Sming/Wiring/SplitString.h +++ b/Sming/Wiring/SplitString.h @@ -1,16 +1,10 @@ -/* $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/ || @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