Changeset 24110


Ignore:
Timestamp:
2011-06-03T09:19:39+12:00 (13 years ago)
Author:
sjm84
Message:

Added a trim function to text_t that removes leading and trailing whitespace

Location:
main/trunk/greenstone2/common-src/src/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/common-src/src/lib/text_t.cpp

    r22141 r24110  
    5151
    5252#include "unitool.h"
     53#include <iostream>
    5354
    5455const text_t g_EmptyText("");
     
    711712}
    712713
     714//Trims the whitespace off the beginning and end of a given string
     715text_t trim (const text_t& text) {
     716
     717  if(text.size() == 0) {
     718    return text;
     719  }
     720
     721  text_t::const_iterator firstLetter = text.begin();
     722  text_t::const_iterator lastLetter = text.end();
     723 
     724  //Find the start
     725  while (firstLetter != lastLetter) {
     726    if(!is_unicode_space(*firstLetter)) {
     727      break;
     728    }
     729    firstLetter++;
     730  }
     731
     732  //Find the end
     733  while (lastLetter != firstLetter) {
     734    if(!is_unicode_space(*lastLetter)) {
     735      break;
     736    }
     737    lastLetter--;
     738  }
     739
     740  return substr(firstLetter, lastLetter+1);
     741}
    713742
    714743////////////////////////////////////
  • main/trunk/greenstone2/common-src/src/lib/text_t.h

    r22141 r24110  
    289289bool ends_with(const text_t& text, const text_t& suffix);
    290290
     291// trims whitespace of the front and end of the string
     292text_t trim(const text_t& text);
     293
    291294// conversion classes used for getting information in to and out of
    292295// the text_t class.
Note: See TracChangeset for help on using the changeset viewer.