Trimming C++ strings

Posted in Uncategorized by tux4life on July 26, 2009

The c++ string class is one of the most complete implementation of the string library and has functions for every possible string operation one can think of.  But one of the most dearly missed function is the trim() function (to remove the leading and trailing spaces/tabs).

Fortunately, it is very easy to implement an efficient version  the trim function using other member functions of the string class.  This is illustrated in the code snippet shown below.

void trim(std::string& str)
{
std::string trim_chars = ” \t”;
std::string::size_type pos = str.find_last_not_of(trim_chars);
if(pos != std::string::npos) {
str.erase(pos + 1);
pos = str.find_first_not_of(trim_chars);
if(pos != std::string::npos) str.erase(0, pos);
}
else
str.erase(str.begin(), str.end());
}

The logic is pretty simple.  Find the first/last character that is not a space/tab, then remove everything that precedes/follows it.  The else clause handles the case where the string only has spaces or tabs and erases everything.

As a small optimisation, the “trim_chars” variable can be made static so that it is not created and deleted every time the function is called.  This would be particularly helpful if the function called multiple times.

void trim(std::string& str)

{

std::string trim_chars = ” \t”;

std::string::size_type pos = str.find_last_not_of(trim_chars);

if(pos != std::string::npos) {

str.erase(pos + 1);

pos = str.find_first_not_of(trim_chars);

if(pos != std::string::npos) str.erase(0, pos);

}

else

str.erase(str.begin(), str.end());

}

Advertisements

Singleton Pattern

Posted in Programming by tux4life on July 19, 2009

Using design patterns is a very effective way of making the code look neat, robust, reduce maintenance overhead.  Apart from these obvious advantages, design patterns also help convey certain special meaning and restrict programmers from doing things that they are not supposed to do.

For example consider a case where your application needs a Logger.  One can always write a Logger class but ideally you would want only one instance of the Logger class throughout your application.  This restriction can be forced by using the singleton pattern.  The following piece of code illustrates a singleton Logger class.

class Logger {
public:

// The only way to get a reference for the logger class
// is to use the Instance() method
static Logger& Instance() {
static Logger theLog;
return theLog;
}

//     Expose the public methods of the Logger class here
//     void Write(char const *logline);
//     bool SaveTo(char const *filename);
private:
// Hide the constructor
Logger();

// Hide the copy constructor
Logger(Logger const&);

// Hide the assignment operator
Logger& operator=(Logger const&);
};

In the above implementation of the singleton pattern, the default constructor, copy constructor and the assignment operator are hidden.

You can also notice a static instance of the Logger class ‘theLog’ inside the Instance() function.  This function can be used to get a reference for the instance of the Logger class. The one and only instance of the Logger class is created when the Instance() method is called for the first time.  Creation of further instances are restricted as the constructor is hidden and the Instance() function returns a reference to a already created object.  And more over as there is no dynamic memory used, no explicit de-allocations are required.

To summarise, for creating the singleton object,

  1. Hide the default constructor
  2. Hide the copy constructor
  3. Hide the assignment operator
  4. Declare a public static method that returns a reference to a static instance of the class