Another 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
ssigala
Earned some good credits
Earned some good credits
Posts: 109
Joined: Fri Sep 03, 2004 9:30 am
Location: Brescia, Italy

Another wxString split function

Post by ssigala » Fri Aug 12, 2005 9:52 am

YASF (Yet another split function...): :wink:

Code: Select all

// prototype

bool SplitString(const wxString& str, char sep, int nofields, bool trim,
     wxString* field1, wxString* field2, wxString* field3=0, wxString* field4=0, wxString* field5=0,
     wxString* field6=0, wxString* field7=0, wxString* field8=0, wxString* field9=0, wxString* field10=0);

// examples:
//
//     wxString first, second, third;
//     SplitString("foo:bar:baz", ':', 3, false, &first, &second, &third);
//     SplitString("foo : bar: baz", ':', 3, true, &first, &second, &third);

bool SplitString(const wxString& str, char sep, int nofields, bool trim,
     wxString* field1, wxString* field2, wxString* field3, wxString* field4, wxString* field5,
     wxString* field6, wxString* field7, wxString* field8, wxString* field9, wxString* field10)
{
	wxString s = str;
	wxString* fields[10] = {field1, field2, field3, field4, field5, field6, field7, field8, field9, field10};
	int count=0;
	while (nofields >= 2) {
		int pos = s.Find(sep);
		if (pos == -1)
			return false;
		wxASSERT(fields[count]);
		if (fields[count]) *fields[count] = (trim ? s.Left(pos).Trim().Trim(false) : s.Left(pos));
		s = s.Mid(pos+1);
		++count;
		--nofields;
	}
	wxASSERT(fields[count]);
	if (fields[count]) *fields[count] = (trim ? s.Trim().Trim(false) : s);
	return true;
}
Last edited by ssigala on Fri Aug 12, 2005 11:06 am, edited 1 time in total.
Sandro Sigala - Kynosoft, Brescia

AkiraDev
Knows some wx things
Knows some wx things
Posts: 48
Joined: Tue May 24, 2005 9:13 am

Post by AkiraDev » Fri Aug 12, 2005 10:51 am

Hi!

This piece of code can nicely be used in home-made parsers... although there is something I don't quite understand.
Why use 10 pointers to wxStrings as argument, instead of just passing a reference to a (resisable) wxArrayString - which extends the maximum number of extractable tokens from 10 to the available memory?
Back, just not as often

Windows Vista SP1, Ubuntu 8.04
MinGW 3.4.5, GCC 4.2.1
Code::Blocks
wxWidgets 2.8.9

ssigala
Earned some good credits
Earned some good credits
Posts: 109
Joined: Fri Sep 03, 2004 9:30 am
Location: Brescia, Italy

Post by ssigala » Fri Aug 12, 2005 11:03 am

AkiraDev wrote: This piece of code can nicely be used in home-made parsers... although there is something I don't quite understand.
Why use 10 pointers to wxStrings as argument, instead of just passing a reference to a (resisable) wxArrayString - which extends the maximum number of extractable tokens from 10 to the available memory?
I agree with you, using a wxArrayString is better and more general, for a string split function.

Actually I have written the SplitString function for splitting fields in configuration-like files:

Code: Select all

color = red
title = "hello world"
foo = 1235
The usage is really simple and compact:

Code: Select all

wxString line;
// ... read line
wxString variable, value;
SplitString(line, '=', 2, true, &variable, &value);
Sandro Sigala - Kynosoft, Brescia

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Fri Aug 12, 2005 12:37 pm

It should already be part of wxWidgets because it is that handy!

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

go_micha
In need of some credit
In need of some credit
Posts: 3
Joined: Thu Dec 08, 2005 4:06 pm

Post by go_micha » Thu Dec 08, 2005 4:22 pm

for same purpose I use the next very simple function:

Code: Select all

// returns false if separator was not found. In this case rLeft contains input string
bool SplitToken(const wxString& input, const wxChar& sep, wxString& rLeft, wxString& rRight)
{
  rLeft = input.BeforeFirst(sep);
  if (rLeft == input)
    return false;
  rRight= input.AfterFirst(sep);
  if (rRight.IsEmpty())
    return false;

  return true;
}
Usage:
for pairs like this:
tag=123
param=Abc
...

Code: Select all

wxString sValue("tag=123");
wxString sLeft, sRight;
SplitToken(sValue, "=", sLeft, sRight);

Takeshi Miya
Earned a small fee
Earned a small fee
Posts: 12
Joined: Sun Jan 22, 2006 11:43 pm

Post by Takeshi Miya » Sat Jan 28, 2006 1:48 pm

I just use wxStringTokenizer :)

Post Reply