head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.41; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.58; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.05; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.22.02; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# strutils.cxx

    00001 // String utilities.
    00002 //
    00003 // Written by Bernie Bright, started 1998
    00004 //
    00005 // Copyright (C) 1998  Bernie Bright - bbright@@bigpond.net.au
    00006 //
    00007 // This library is free software; you can redistribute it and/or
    00008 // modify it under the terms of the GNU Library General Public
    00009 // License as published by the Free Software Foundation; either
    00010 // version 2 of the License, or (at your option) any later version.
    00011 //
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015 // Library General Public License for more details.
    00016 //
    00017 // You should have received a copy of the GNU Library General Public
    00018 // License along with this library; if not, write to the
    00019 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00020 // Boston, MA  02111-1307, USA.
    00021 //
    00022 // $Id: strutils.cxx,v 1.4 2002/05/10 13:32:44 curt Exp $
    00023
    00024 #include <ctype.h>
    00025 #include "strutils.hxx"
    00026
    00027 namespace simgear {
    00028     namespace strutils {
    00029
    00033         static vector<string>
    00034         split_whitespace( const string& str, int maxsplit )
    00035         {
    00036             vector<string> result;
    00037             string::size_type len = str.length();
    00038             string::size_type i = 0;
    00039             string::size_type j;
    00040             int countsplit = 0;
    00041
    00042             while (i < len)
    00043             {
    00044                 while (i < len && isspace(str[i]))
    00045                 {
    00046                     ++i;
    00047                 }
    00048
    00049                 j = i;
    00050
    00051                 while (i < len && !isspace(str[i]))
    00052                 {
    00053                     ++i;
    00054                 }
    00055
    00056                 if (j < i)
    00057                 {
    00058                     result.push_back( str.substr(j, i-j) );
    00059                     ++countsplit;
    00060                     while (i < len && isspace(str[i]))
    00061                     {
    00062                         ++i;
    00063                     }
    00064
    00065                     if (maxsplit && (countsplit >= maxsplit) && i < len)
    00066                     {
    00067                         result.push_back( str.substr( i, len-i ) );
    00068                         i = len;
    00069                     }
    00070                 }
    00071             }
    00072
    00073             return result;
    00074         }
    00075
    00079         vector<string>
    00080         split( const string& str, const char* sep, int maxsplit )
    00081         {
    00082             if (sep == 0)
    00083                 return split_whitespace( str, maxsplit );
    00084
    00085             vector<string> result;
    00086             int n = strlen( sep );
    00087             if (n == 0)
    00088             {
    00089                 // Error: empty separator string
    00090                 return result;
    00091             }
    00092             const char* s = str.c_str();
    00093             string::size_type len = str.length();
    00094             string::size_type i = 0;
    00095             string::size_type j = 0;
    00096             int splitcount = 0;
    00097
    00098             while (i+n <= len)
    00099             {
    00100                 if (s[i] == sep[0] && (n == 1 || memcmp(s+i, sep, n) == 0))
    00101                 {
    00102                     result.push_back( str.substr(j,i-j) );
    00103                     i = j = i + n;
    00104                     ++splitcount;
    00105                     if (maxsplit && (splitcount >= maxsplit))
    00106                         break;
    00107                 }
    00108                 else
    00109                 {
    00110                     ++i;
    00111                 }
    00112             }
    00113
    00114             result.push_back( str.substr(j,len-j) );
    00115             return result;
    00116         }
    00117
    00123         const int LEFTSTRIP = 0;
    00124         const int RIGHTSTRIP = 1;
    00125         const int BOTHSTRIP = 2;
    00126
    00127         static string
    00128         do_strip( const string& s, int striptype )
    00129         {
    00130             //     if (s.empty())
    00131             //      return s;
    00132
    00133             string::size_type len = s.length();
    00134             string::size_type i = 0;
    00135             if (striptype != RIGHTSTRIP)
    00136             {
    00137                 while (i < len && isspace(s[i]))
    00138                 {
    00139                     ++i;
    00140                 }
    00141             }
    00142
    00143             string::size_type j = len;
    00144             if (striptype != LEFTSTRIP)
    00145             {
    00146                 do
    00147                 {
    00148                     --j;
    00149                 }
    00150                 while (j >= 1 && isspace(s[j]));
    00151                 ++j;
    00152             }
    00153
    00154             if (i == 0 && j == len)
    00155             {
    00156                 return s;
    00157             }
    00158             else
    00159             {
    00160                 return s.substr( i, j - i );
    00161             }
    00162         }
    00163
    00164         string
    00165         lstrip( const string& s )
    00166         {
    00167             return do_strip( s, LEFTSTRIP );
    00168         }
    00169
    00170         string
    00171         rstrip( const string& s )
    00172         {
    00173             return do_strip( s, RIGHTSTRIP );
    00174         }
    00175
    00176         string
    00177         strip( const string& s )
    00178         {
    00179             return do_strip( s, BOTHSTRIP );
    00180         }
    00181
    00182     } // end namespace strutils
    00183 } // end namespace simgear

@ 1.3 log @Oops, try again. @ text @d181 1 a181 1