Commit 6c641343 authored by thomas.forbriger's avatar thomas.forbriger Committed by thomas.forbriger
Browse files

implemented more efficient method to strip whitespace

This is a legacy commit from before 2015-03-01.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.
(taken from libdatrwxx/tsoft/tsoftdata.cc)


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/branches/libdatrwxx.su
SVN Revision: 3465
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 73e08335
...@@ -30,11 +30,12 @@ ...@@ -30,11 +30,12 @@
* *
* REVISIONS and CHANGES * REVISIONS and CHANGES
* - 18/11/2010 V1.0 Thomas Forbriger * - 18/11/2010 V1.0 Thomas Forbriger
* - 25/11/2010 V1.1 implemented more efficient string trimming
* *
* ============================================================================ * ============================================================================
*/ */
#define FAPIDXX_HELPER_CC_VERSION \ #define FAPIDXX_HELPER_CC_VERSION \
"FAPIDXX_HELPER_CC V1.0 " "FAPIDXX_HELPER_CC V1.1"
#define FAPIDXX_HELPER_CC_CVSID \ #define FAPIDXX_HELPER_CC_CVSID \
"$Id$" "$Id$"
...@@ -74,13 +75,38 @@ namespace fapidxx { ...@@ -74,13 +75,38 @@ namespace fapidxx {
/*----------------------------------------------------------------------*/ /*----------------------------------------------------------------------*/
//! remove whitespace at begin and end of string
std::string trimws(std::string s)
{
if (s.length()>0)
{
string::size_type ib=s.find_first_not_of(" ", 0);
if (ib==string::npos)
{
s="";
}
else
{
string::size_type il=s.find_last_not_of(" \r", s.length());
string::size_type n=il>=ib ? il-ib+1 : 0;
if (n==0) { ib = 0; }
if ((ib!=0) || (n!=s.length())) { s=s.substr(ib,n); }
}
}
return(s);
} // std::string trimws(std::string s)
/*----------------------------------------------------------------------*/
//! remove whitespace at end of string //! remove whitespace at end of string
std::string trimws_end(std::string s) std::string trimws_end(std::string s)
{ {
reverse(s.begin(), s.end()); if (s.length()>0)
s.erase(s.begin(), find_if(s.begin(), s.end(), {
not1(std::ptr_fun(isspace)))); string::size_type il=s.find_last_not_of(" \r", s.length());
reverse(s.begin(), s.end()); string::size_type n=il>=0 ? il+1 : 0;
if (n!=s.length()) { s=s.substr(0,n); }
}
return(s); return(s);
} // std::string trimws_end(std::string s) } // std::string trimws_end(std::string s)
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
* *
* REVISIONS and CHANGES * REVISIONS and CHANGES
* - 18/11/2010 V1.0 Thomas Forbriger * - 18/11/2010 V1.0 Thomas Forbriger
* - 25/11/2010 V1.1 added trimws
* *
* ============================================================================ * ============================================================================
*/ */
...@@ -38,7 +39,7 @@ ...@@ -38,7 +39,7 @@
#ifndef FAPIDXX_HELPER_H_VERSION #ifndef FAPIDXX_HELPER_H_VERSION
#define FAPIDXX_HELPER_H_VERSION \ #define FAPIDXX_HELPER_H_VERSION \
"FAPIDXX_HELPER_H V1.0 " "FAPIDXX_HELPER_H V1.1"
#define FAPIDXX_HELPER_H_CVSID \ #define FAPIDXX_HELPER_H_CVSID \
"$Id$" "$Id$"
...@@ -57,6 +58,9 @@ namespace fapidxx { ...@@ -57,6 +58,9 @@ namespace fapidxx {
//! remove whitespace at end of string //! remove whitespace at end of string
std::string trimws_end(std::string s); std::string trimws_end(std::string s);
//! remove whitespace at beginning and end of string
std::string trimws(std::string s);
//! create the tanf value //! create the tanf value
float maketanf(const libtime::TAbsoluteTime& time); float maketanf(const libtime::TAbsoluteTime& time);
......
...@@ -29,11 +29,13 @@ ...@@ -29,11 +29,13 @@
* *
* REVISIONS and CHANGES * REVISIONS and CHANGES
* - 30/06/2005 V1.0 Thomas Forbriger * - 30/06/2005 V1.0 Thomas Forbriger
* - 25/11/2010 V1.1 implemented more efficient method to strip
* whitespace
* *
* ============================================================================ * ============================================================================
*/ */
#define TF_STRINGFUNC_CC_VERSION \ #define TF_STRINGFUNC_CC_VERSION \
"TF_STRINGFUNC_CC V1.0 " "TF_STRINGFUNC_CC V1.1"
#define TF_STRINGFUNC_CC_CVSID \ #define TF_STRINGFUNC_CC_CVSID \
"$Id$" "$Id$"
...@@ -48,13 +50,49 @@ namespace tfxx { ...@@ -48,13 +50,49 @@ namespace tfxx {
namespace string { namespace string {
// remove leading and trailing whitespace
std::string trimws(std::string s)
{
if (s.length()>0)
{
string::size_type ib=s.find_first_not_of(" ", 0);
if (ib==string::npos)
{
s="";
}
else
{
string::size_type il=s.find_last_not_of(" \r", s.length());
string::size_type n=il>=ib ? il-ib+1 : 0;
if (n==0) { ib = 0; }
if ((ib!=0) || (n!=s.length())) { s=s.substr(ib,n); }
}
}
return(s);
} // std::string trimws(std::string s)
/*----------------------------------------------------------------------*/
// remove leading whitespace // remove leading whitespace
std::string trimws_begin(std::string s) std::string trimws_begin(std::string s)
{ {
s.erase(s.begin(), find_if(s.begin(), s.end(), if (s.length()>0)
not1(std::ptr_fun(isspace)))); {
string::size_type ib=s.find_first_not_of(" ", 0);
if (ib==string::npos)
{
s="";
}
else if (ib!=0)
{
string::size_type n=s.length()-ib;
s=s.substr(ib,n);
}
}
return(s); return(s);
} } // std::string trimws_begin(std::string s)
/*----------------------------------------------------------------------*/
// remove trailing whitespace // remove trailing whitespace
// This is not as straight forward as trimws_begin as I did not manage to // This is not as straight forward as trimws_begin as I did not manage to
...@@ -65,12 +103,16 @@ namespace tfxx { ...@@ -65,12 +103,16 @@ namespace tfxx {
// time... // time...
std::string trimws_end(std::string s) std::string trimws_end(std::string s)
{ {
reverse(s.begin(), s.end()); if (s.length()>0)
s.erase(s.begin(), find_if(s.begin(), s.end(), {
not1(std::ptr_fun(isspace)))); string::size_type il=s.find_last_not_of(" \r", s.length());
reverse(s.begin(), s.end()); string::size_type n=il>=0 ? il+1 : 0;
if (n!=s.length()) { s=s.substr(0,n); }
}
return(s); return(s);
} } // std::string trimws_end(std::string s)
/*----------------------------------------------------------------------*/
/*! strip substring /*! strip substring
* *
...@@ -92,6 +134,8 @@ namespace tfxx { ...@@ -92,6 +134,8 @@ namespace tfxx {
return(result); return(result);
} }
/*----------------------------------------------------------------------*/
/*! count characters in string /*! count characters in string
* *
* \param s string to look for c * \param s string to look for c
...@@ -106,6 +150,8 @@ namespace tfxx { ...@@ -106,6 +150,8 @@ namespace tfxx {
return(result); return(result);
} }
/*----------------------------------------------------------------------*/
/*! replace pattern by string /*! replace pattern by string
* *
* \param s string that contains pattern * \param s string that contains pattern
......
...@@ -32,6 +32,8 @@ ...@@ -32,6 +32,8 @@
* - 30/06/2005 V1.0 Thomas Forbriger * - 30/06/2005 V1.0 Thomas Forbriger
* - 11/11/2009 V1.1 \b !! changed interface of gen_split, since * - 11/11/2009 V1.1 \b !! changed interface of gen_split, since
* new g++ does not handle template template functions * new g++ does not handle template template functions
* - 25/11/2010 V1.2 implemented more efficient method to strip
* whitespace
* *
* ============================================================================ * ============================================================================
*/ */
...@@ -40,7 +42,7 @@ ...@@ -40,7 +42,7 @@
#ifndef TF_STRINGFUNC_H_VERSION #ifndef TF_STRINGFUNC_H_VERSION
#define TF_STRINGFUNC_H_VERSION \ #define TF_STRINGFUNC_H_VERSION \
"TF_STRINGFUNC_H V1.0 " "TF_STRINGFUNC_H V1.2"
#define TF_STRINGFUNC_H_CVSID \ #define TF_STRINGFUNC_H_CVSID \
"$Id$" "$Id$"
...@@ -204,8 +206,7 @@ ...@@ -204,8 +206,7 @@
* \see trimws_begin * \see trimws_begin
* \see trimws_end * \see trimws_end
*/ */
inline std::string trimws(const std::string& s) std::string trimws(std::string s);
{ return(trimws_end(trimws_begin(s))); }
/*! strip substring /*! strip substring
* *
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment