wxString split function

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

wxString split function

Post by Ryan Norton »

Code: Select all

//Split (Tokenize) string at specified intervals
//s == string to split
//retArray == split up string (out)
//cpszExp == expression to split at
//crnStart == start postion to split 
//crnCount == max number of split of strings
//crbCIComp == true if case insensitive
void Split(  const wxString& s, wxArrayString& retArray,  const wxChar* cpszExp,  
                                const size_t& crnStart = 0,    const size_t& crnCount = (size_t)-1, 
                                const bool& crbCIComp = false)
{
    //sanity checks
    wxASSERT_MSG(cpszExp != NULL, wxT("Invalid value for First Param of wxString::Split (cpszExp)"));
//    wxASSERT_MSG(crnCount >= (size_t)-1, wxT("Invalid value for Third Param of wxString::Split (crnCount)"));
  
    retArray.Clear();

    size_t  nOldPos = crnStart,      //Current start position in this string
            nPos = crnStart;      //Current end position in this string 

    wxString szComp,            //this string as-is (if bCIComp is false) or converted to lowercase
             szExp = cpszExp;   //Expression string, normal or lowercase

    if (crbCIComp)
    {
        szComp = s.Lower();
        szExp.MakeLower();
    }
    else
        szComp = s;

    if(crnCount == (size_t)-1)
    {
    for (; (nPos = szComp.find(szExp, nPos)) != wxString::npos;)//Is there another token in the string 
        {
        retArray.Add(Slice(s, nOldPos, nPos)); //Insert the token in the array
        nOldPos = nPos += szExp.Length();//Move up the start slice position
        }
    
    }
    else
    {
    for (int i = crnCount; 
            (nPos = szComp.find(szExp, nPos)) != wxString::npos &&
            i != 0;
                --i)//Is there another token in the string && have we met nCount?
    {
        retArray.Add(Slice(s, nOldPos, nPos)); //Insert the token in the array
        nOldPos = nPos += szExp.Length();//Move up the start slice position
    }
    }
    if (nOldPos != s.Length())
        retArray.Add( Slice(s, nOldPos, s.Length()) ); //Add remaining characters in string
}
[Mostly retired moderator, still check in to clean up some stuff]
daddydave
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 214
Joined: Wed Jun 15, 2005 3:31 am
Location: United States
Contact:

Post by daddydave »

Handy! I'll probably use this in the first iteration of my current project. (Second iteration will probably store data as XML)
daddydave
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 214
Joined: Wed Jun 15, 2005 3:31 am
Location: United States
Contact:

Post by daddydave »

Code: Select all

retArray.Add(Slice(s, nOldPos, nPos)); //Insert the token in the array 
Where's the Slice function coming from? I don't see it in the wxWidgets docs
daddydave
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 214
Joined: Wed Jun 15, 2005 3:31 am
Location: United States
Contact:

Post by daddydave »

Figured it out. Added this to the top of your code

Code: Select all

#define Slice(str, start, end) (str.Mid(start, end))
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton »

daddydave wrote:Figured it out. Added this to the top of your code

Code: Select all

#define Slice(str, start, end) (str.Mid(start, end))
Good catch.... it was a function from my sources

Code: Select all

  wxString Slice(const wxString& s, size_t from, size_t to) 
      { return s.Mid(from, (to - from) ); }
[Mostly retired moderator, still check in to clean up some stuff]
Post Reply