From 2d0e3b7e0d4f2e3d804e567aca4f73f3bc620997 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Sat, 18 Jul 2020 12:23:41 -0700 Subject: [PATCH] Add an implementation of string splitting --- include/kicad_string.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/include/kicad_string.h b/include/kicad_string.h index 09cc1e5506..504d780867 100644 --- a/include/kicad_string.h +++ b/include/kicad_string.h @@ -32,6 +32,8 @@ #include "config.h" +#include +#include #include #include @@ -198,6 +200,7 @@ bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar = 0 ); extern "C" char* strtok_r( char* str, const char* delim, char** nextp ); #endif + /** * A helper for sorting strings from the rear. Useful for things like 3d model names * where they tend to be largely repetitious at the front. @@ -246,4 +249,38 @@ struct rsort_wxString } }; +/** + * Splits the input string into a vector of output strings + * + * @param aStr - Input string with 0 or more delimiters + * @param aDelim - The string of delimiter. Multiple characters here denote alternate delimiters + * + * @note Multiple delimiters are considered to be separate records with empty strings + * @return a vector of strings + */ +static inline std::vector split( const std::string& aStr, const std::string& aDelim ) +{ + size_t pos = 0; + size_t last_pos = 0; + size_t len; + + std::vector tokens; + + while( pos < aStr.size() ) + { + pos = aStr.find_first_of( aDelim, last_pos ); + + if( pos == std::string::npos ) + pos = aStr.size(); + + len = pos - last_pos; + + tokens.push_back( aStr.substr( last_pos, len ) ); + + last_pos = pos + 1; + } + + return tokens; +} + #endif // KICAD_STRING_H_