Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise SplitString #1508

Merged
merged 2 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 27 additions & 59 deletions Sming/Wiring/SplitString.cpp
Original file line number Diff line number Diff line change
@@ -1,94 +1,62 @@
#include "SplitString.h"

int splitString(String &what, int delim, Vector<long> &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 $
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WMemory.cpp -> SplitString.cpp.

||
|| @author Hernando Barragan <[email protected]>
|| @url http://wiring.org.co/
|| @contribution Brett Hagman <[email protected]>
|| @contribution Alexander Brevig <[email protected]>
||
|| @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<int> &splits)
int splitString(String &what, char delim, Vector<int> &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<String> &splits)
int splitString(String &what, char delim, Vector<String> &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();
}
28 changes: 19 additions & 9 deletions Sming/Wiring/SplitString.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
|| @contribution Brett Hagman <[email protected]>
|| @contribution Alexander Brevig <[email protected]>
||
|| @description
|| | Implementation of c++ new/delete operators.
|| |
|| | Wiring Common API
|| #
||
|| @license Please see cores/Common/License.txt.
||
*/
Expand All @@ -22,8 +16,24 @@
#include "WString.h"
#include "WiringFrameworkDependencies.h"

int splitString(String &what, int delim, Vector<long> &splits);
int splitString(String &what, int delim, Vector<int> &splits);
int splitString(String &what, int delim, Vector<String> &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<int> &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<String> &splits);

#endif
4 changes: 1 addition & 3 deletions Sming/Wiring/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,6 @@ class String
long toInt(void) const;
float toFloat(void) const;

friend int splitString(String &what, int delim, Vector<long> &splits);
friend int splitString(String &what, int delim, Vector<int> &splits);
friend int splitString(String &what, int delim, Vector<String> &splits);

//void printTo(Print &p) const;

Expand Down Expand Up @@ -385,6 +382,7 @@ class StringSumHelper : public String
};

#include "FlashString.h"
#include "SplitString.h"

#endif // __cplusplus
#endif
Expand Down