tstring.cxx

Go to the documentation of this file.
00001 // @(#)root/test:$Id: tstring.cxx 31874 2009-12-14 10:21:31Z rdm $
00002 // Author: Fons Rademakers   19/08/96
00003 
00004 #include <stdlib.h>
00005 
00006 #include "Riostream.h"
00007 #include "TString.h"
00008 #include "TRegexp.h"
00009 #include "TPRegexp.h"
00010 
00011 
00012 void Ok(int i, int b)
00013 {
00014    cout << "Test " << i;
00015    if (b)
00016       cout << "   ok" << endl;
00017    else
00018       cout << "   NOT ok" << endl;
00019 }
00020 
00021 
00022 
00023 int main()
00024 {
00025    // create base string
00026    TString s = "aap noot mies";
00027    cout << s << endl;
00028 
00029    // use different constructors and excercise +, += and == operators
00030    TString s1 = TString("aa") + "p ";
00031    s1 += TString("noot ") + "mi" + "es";
00032    cout << s1 << endl;
00033    Ok(1, s1==s);
00034 
00035    // use single char constructor and excercise again += and == operators
00036    TString s2 = 'a';
00037    s2 += "ap ";
00038    s2 += "noot ";
00039    s2 += "mies";
00040    cout << s2 << endl;
00041    Ok(2, s2==s);
00042 
00043    // get and set a subrange (via TSubString)
00044    TString s3 = s(4,9);
00045    s3(0,4) = "mama papa";
00046    cout << s3 << endl;
00047    Ok(3, s3=="mama papa mies");
00048 
00049    // create a regular expression, make search in string and replace
00050    // matched substring
00051    TRegexp re(" n.*t ");
00052    TString s4 = s;
00053    s4(re) = " pipo ";
00054    cout << s4 << endl;
00055    Ok(4, s4=="aap pipo mies");
00056 
00057    // use "const char*" operator and the Data() member function to access
00058    // the string as a const char*
00059    const char *a = (const char*)s;
00060    cout << a << endl;
00061    Ok(5, !strcmp(a, s.Data()));
00062 
00063    // return 13th character and replace it by 't', getting 14th character
00064    // should result in an error message since operator[] uses range checking
00065    TString s6 = s;
00066    s6[12] = 't';
00067    cout << s6 << endl;
00068    cout << "*** Error message ok, accessing intentionaly out-of-bounds" << endl;
00069    char b = s6[13];
00070    cout << b << endl;
00071    Ok(6, s6=="aap noot miet");
00072 
00073    // return 13th character and replace it by 'p', getting 14th character
00074    // should NOT result in an error message since operator() does not use
00075    // range checking
00076    TString s7 = s;
00077    s7(12) = 'p';
00078    cout << s7 << endl;
00079    char c = s7(13);
00080    cout << c << endl;
00081    Ok(7, s7=="aap noot miep");
00082 
00083    // use Append, Remove, Prepend, Replace and Insert
00084    TString s8 = s;
00085    s8.Append(" tante ");
00086    s8.Append(s7(9,4));
00087    cout << s8 << endl;
00088 
00089    s8.Remove(0,14);
00090    cout << s8 << endl;
00091    s8.Prepend("oom jan");
00092    cout << s8 << endl;
00093    s8.Insert(7," en ");
00094    cout << s8 << endl;
00095    s8.Replace(4,3,"jaap");
00096    cout << s8 << endl;
00097    Ok(8, s8=="oom jaap en tante miep");
00098 
00099    // use CompareTo to compare char * and TString
00100    TString s9 = s;
00101    Ok(9,  !s9.CompareTo(s));
00102    Ok(10, !s9.CompareTo("AAP NOOT MIES", TString::kIgnoreCase));
00103 
00104    // use Contains to find if "string" is contained
00105    Ok(11, s9.Contains("NooT", TString::kIgnoreCase));
00106    Ok(12, !s9.Contains("pipo"));
00107 
00108    // return the index to the first and last character 'c'
00109    Ok(13, s.First('o')==5);
00110    Ok(14, s.First('z')==kNPOS);
00111    Ok(15, s.Last('a')==1);
00112    Ok(16, s.Last('z')==kNPOS);
00113 
00114    // returns the index of the start of match
00115    Ok(17, s.Index("mies")==9);
00116    Ok(18, s.Index("aa",1)==kNPOS);
00117 
00118    // returns length of string
00119    Ok(19, s.Length()==13);
00120 
00121    // test IsNull
00122    TString s10;
00123 
00124    Ok(20, s10.IsNull());
00125    Ok(21, !s.IsNull());
00126 
00127    // test IsAscii
00128    s10 = "\xb9";
00129 
00130    Ok(22, !s10.IsAscii());
00131    Ok(23, s.IsAscii());
00132 
00133    // some excercises with the Perl Compatible Regular Expressions
00134    TString s11("Food is on the foo table.");
00135    TPRegexp("\\b(foo)\\s+(\\w+)").Substitute(s11, "round $2");
00136    Ok(24, s11=="Food is on the round table.");
00137 
00138    TString s12("pepernotenkoek");
00139    TPRegexp("peper(.*)koek").Substitute(s12, "wal$1boom");
00140    Ok(25, s12=="walnotenboom");
00141 
00142    TString s13("hihi haha");
00143    TPRegexp("^([^ ]*) *([^ ]*)").Substitute(s13, "$2 $1");
00144    Ok(26, s13=="haha hihi");
00145 
00146    Ok(27, TPRegexp("^(\\w+) *(\\w+)").Match(s13) == 3);
00147 
00148    // test Resize and Strip
00149    s9.Prepend("   ");
00150    cout << s9 << endl;
00151 
00152    s9.Resize(50);
00153    cout << s9 << "<<ends here" << endl;
00154 
00155    cout << s9.Strip(TString::kBoth) << "<<ends here" << endl;
00156 
00157    Printf("Using Print: %s (%d)\n", (const char*) s9, s9.Length());
00158 
00159    return 0;
00160 }

Generated on Tue Jul 5 15:16:34 2011 for ROOT_528-00b_version by  doxygen 1.5.1