
#include "stdafx.h" // comment if not using precompiled headers in MVC++
#include "StringUtils.h"

int StringUtils::SplitString(const cmString& input, const cmString& delimiter, vector<cmString>& results)
{
	int iPos = 0;
	int newPos = -1;
	int sizeS2 = delimiter.GetLength();
	int isize = input.GetLength();

	vector<int> positions;

	newPos = input.Find(delimiter);

	if( newPos < 0 ) { return 0; }

	int numFound = 0;

	while( newPos > iPos )
	{
		numFound++;
		positions.push_back(newPos);
		iPos = newPos;
		newPos = input.Find(delimiter, iPos+sizeS2+1);
	}

	for( int i=0; i <= positions.size(); i++ )
	{
		cmString s;
		if( i == 0 ) { s = input.Mid(i, positions[i]); }
		int offset = positions[i-1] + sizeS2;
		if( offset < isize )
		{
			if( i == positions.size() )
			{
				s = input.Mid(offset);
			}
			else if( i > 0 )
			{
				s = input.Mid( positions[i-1] + sizeS2, positions[i] - positions[i-1] - sizeS2 );
			}
		}
		if( s.GetLength() > 0 )
		{
			results.push_back(s);
		}
	}
	return numFound;
}

int StringUtils::SplitString(const cmString& input, const cmString& delimiter, set<cmString>& results)
{
	vector<cmString> vect;
	int ret;

	results.clear();

	ret = SplitString(input, delimiter, vect);

	for (int i = 0; i < vect.size(); i++)
		results.insert(vect[i]);

	return ret;
}