Subscribe to The Blog without a good name        RSS Feed
-----

AString 1.0

Icon Leave Comment
OMG a post from me that involves code in what, several months? Almost a year? A year?

Anyways, on to the codez. I began a little side project for myself since school was ending. I decided that I was going to extend the std::string class into something I felt I could use. I have very few complaints with std::string, but I like a little bit more intuitiveness when I use it. That being said, I have finally finished the AString class.

DESCRIPTION
AString is an extension of std::string that gets ALL of std::string's functions along with some added extras.

For now, AString version 1.0 supports built-in instr, instrall, replace and split. No more of that hassle of coding your own split or replace, I gave it to you for free. And it's not hard to understand. Also added is the TR1 Regular Expression library. That's right, you can use Regular Expressions right out of the box with AString.

FUNCTIONS
int InStr( string what, int start = 0, bool ignorecase = false )
InStr finds the first occurrence of what in the string starting at start. If ignorecase is true, it matches any occurrence of what regardless of case.

vector<int> InStrAll( string what, int start = 0, bool ignorecase = false )
This works the same way as InStr but returns all occurrences of what in the string from start.

void Replace( string old, string newS )
Replace makes every instance of old in the string change to newS.

vector<string> Split( string delim )
Split returns a vector containing the results of splitting the string by the indicating delimiter - IE: delim.
For example:
AString a("This is a test.");
a.split(" "); //results in a vector containing [ "This", "is", "a", "test." ]

REMatch( std::tr1::regex what )
This will return the result of matching the string to what.

RESearch( std::tr1::regex what )
This will return the result of searching the string for what.

REMatchC( std::tr1::regex what )
This returns the cmatch structure generated by the results of matching string to what.

RESearchC( std::tr1::regex what )
This returns the cmatch structure generated by the results of searching string for what.

Mutate( X (*func)(X) ) - Thanks to Bench
This function will perform any char function on every single element of the string.
For example, Mutate(toupper) would run toupper() on every element of the string.

Example:
AString a("this is.");
a.Mutate(toupper); //a becomes "THIS IS."

CODEZ
//#define USETR1

#include <vector>

#ifdef USETR1
#include <regex>
#endif

#ifndef ASTRING_H
#define ASTRING_H

class AString: public std::string
{

	public:
		AString(const std::string &);
		std::vector<std::string> Split( std::string );
		void Replace( std::string, std::string );
		int InStr( std::string, int, bool );
		std::vector<int> InStrAll( std::string, int, bool );

		#ifdef USETR1
			bool REMatch( std::tr1::regex what )
			{

				return std::tr1::regex_match( (*this), what );

			}
			std::tr1::cmatch REMatchC( std::tr1::regex what )
			{

				std::tr1::cmatch res;
				std::tr1::regex_match( this->c_str(), res, what );
				return res;

			}
			bool RESearch( std::tr1::regex what )
			{

				return std::tr1::regex_search( (*this), what );

			}
			std::tr1::cmatch RESearchC( std::tr1::regex what )
			{
				
				std::tr1::cmatch res;
				std::tr1::regex_search( this->c_str(), res, what );
				return res;

			}
		#endif
		
		//Mutate thanks to Bench
		template<typename X> 
		void mutate( X (*func)(X) )
		{
				
        	for(size_t i(0); i!=this->size(); ++i)
            	(*this)[i] = func( this->at(i) );
    	
		} 

};

#endif



#include <string>
#include <vector>
#include "AString.h"

AString::AString(const std::string& items) : std::string(items) {}

std::vector<std::string> AString::Split( std::string Delim )
{

	std::vector<std::string> result;
	
	int a(0),i(0),c(0);
	
	while( c != -1 )
	{
	
		int a = InStr(Delim,i,true);
		
		result.push_back((*this).substr( i,  a - i ) );
		
		i = a + ( Delim.length() );
		
		if( a == -1 ) c = -1;
		
	}
	
	return result;
	
}

void AString::Replace( std::string old, std::string newS )
{

	std::vector<int> places = InStrAll(old,0,false);

	std::vector<std::string> text;

	std::string temp;

	text = (*this).Split(old);

	temp = text[0] + newS;

	for( int i(1); i < (text.size() - 1); i++ )
		temp += text[i] + newS;

	temp += text[text.size()-1];
	
	(*this) = temp;

}

int AString::InStr( std::string toFind, int start = 0, bool ignoreCase = false )
{

	for( int i(start); i < (*this).length(); i++ )
	{
	
		if( !ignoreCase )
		{
		
			if( int((*this)[i]) == int(toFind[0]) )
			{
			
				bool found = true;
				int counter = 0;
			
				for( int j(1); j < toFind.length(); j++ )
				{
                     
					if( (i+j) < (*this).length() )
					{
                        
						if( int((*this)[i+j]) != int(toFind[0+j]) )
							found = false;
							
                    }
					else
						counter++;
						
                }
							
				if( found && !counter ) return i;
					
			}
			
		}
		else
		{
		
			if( int(tolower((*this)[i])) == int(tolower(toFind[0])) )
			{
			
				bool found = true;
				int counter = 0;
				
				for( int j(1); j < toFind.length(); j++ )
				{
                     
					if( (i+j) < (*this).length() )
					{
                        
						if( int(tolower((*this)[i+j])) != int(tolower(toFind[0+j])) )
							found = false;
							
                    }
					else
						counter++;
						
                }
							
				if( found && !counter ) return i;
			
			}
				
		}
		
	}
	
	return -1;
	
}

std::vector<int> AString::InStrAll( std::string what, int start=0, bool ignorecase=false )
{

	std::vector<int> results;

	for( int i(start); i < (*this).length(); i++ )
	{
	
		if( !ignorecase )
		{
		
			if( int((*this)[i]) == int(what[0]) )
			{
			
				bool found = true;
				int counter = 0;
			
				for( int j(1); j < what.length(); j++ )
				{
                     
					if( (i+j) < (*this).length() )
					{
                        
						if( int((*this)[i+j]) != int(what[0+j]) )
							found = false;
							
                    }
					else
						counter++;
						
                }
							
				if( found && !counter ) results.push_back(i);
					
			}
			
		}
		else
		{
		
			if( int(tolower((*this)[i])) == int(tolower(what[0])) )
			{
			
				bool found = true;
				int counter = 0;
				
				for( int j(1); j < what.length(); j++ )
				{
                     
					if( (i+j) < (*this).length() )
					{
                        
						if( int(tolower((*this)[i+j])) != int(tolower(what[0+j])) )
							found = false;
							
                    }
					else
						counter++;
						
                }
							
				if( found && !counter ) results.push_back(i);
			
			}
				
		}
		
	}
	
	return results;

}



Notes
Please note that you need to uncomment #define USETR1 to make it work. I put that so that people with compilers that currently implement TR1 can use regex and those who don't have it can have AString anyways.

FUTURE
The future of AString depends on you. I'll continue to add various functions I find neat or fun to have but in the meantime, if you know of a string function you'd like to see just pm me or post a comment with it and some documentation.

0 Comments On This Entry

 

September 2014

S M T W T F S
 123456
78910111213
14151617 18 1920
21222324252627
282930    

Tags

Recent Entries

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)