1+ #if 0
2+
3+ # (c)keithhedger Tue 9 Jan 13:48:05 GMT 2024
[email protected] 4+
5+ if [[ $USEVALGRIND -eq 1 ]];then
6+ VALGRIND="valgrind --leak-check=full "
7+ fi
8+
9+ APPNAME=$(basename $0 .cpp)
10+
11+ g++ "$0" -O0 -ggdb -I../LFSToolKit -L../LFSToolKit/app/.libs $(pkg-config --cflags --libs x11 xft cairo ) -llfstoolkit -lImlib2 -o $APPNAME||exit 1
12+ LD_LIBRARY_PATH=../LFSToolKit/app/.libs $VALGRIND ./$APPNAME "$@"
13+
14+
15+ retval=$?
16+ echo "Exit code $retval"
17+ rm $APPNAME
18+ exit $retval
19+ #endif
20+
21+ #include " lfstk/LFSTKGlobals.h"
22+ #include < memory>
23+
24+ std::string replaceAllStr (std::string haystack,std::string needle,std::string newneedle,bool erase=true )
25+ {
26+ std::string::size_type found;
27+ std::string localhaystack=haystack;
28+ bool flag=false ;
29+
30+ do
31+ {
32+ flag=false ;
33+ found=localhaystack.find (needle);
34+ if (found!=std::string::npos)
35+ {
36+ if (erase==true )
37+ localhaystack.erase (found,needle.length ());
38+ else
39+ localhaystack.replace (found,needle.length (),newneedle);
40+ flag=true ;
41+ }
42+ }while (flag==true );
43+ return (localhaystack);
44+ }
45+
46+ std::string replaceAllChar (std::string haystack,std::string needle,std::string newneedle,bool erase=true )
47+ {
48+ std::string::size_type found;
49+ std::string localhaystack=haystack;
50+ bool flag=false ;
51+
52+ do
53+ {
54+ flag=false ;
55+ found=localhaystack.find_first_of (needle);
56+ if (found!=std::string::npos)
57+ {
58+ if (erase==true )
59+ localhaystack.erase (found,1 );
60+ else
61+ localhaystack.replace (found,1 ,newneedle);
62+ flag=true ;
63+ }
64+ }while (flag==true );
65+ return (localhaystack);
66+ }
67+
68+ int main (int argc, char **argv)
69+ {
70+ std::string x=" " ;
71+ LFSTK_applicationClass *apc=new LFSTK_applicationClass ();
72+ std::string haystack=" w1,w2,wXXXX3,.w4,XXXX,w.5,wX6" ;
73+ std::string needle=" XXXX" ;
74+ std::string newneedle=" ++" ;
75+ //
76+ // //replace ,>0
77+ // x=replaceAll(haystack,needle,std::string("")+='\0',false);
78+ // std::cout<<x<<std::endl;
79+ // fprintf(stderr,"x=%s\n",x.c_str());
80+ // for(int j=0;j<x.length();j++)
81+ // fprintf(stderr,"0x%x\n",x.at(j));
82+ //
83+ // replace str
84+ x=replaceAllStr (haystack,needle,newneedle,false );
85+ std::cout<<" Replace " <<x<<std::endl;
86+ fprintf (stderr," x=%s\n " ,x.c_str ());
87+ for (int j=0 ;j<x.length ();j++)
88+ fprintf (stderr," 0x%x\n " ,x.at (j));
89+
90+ // replace char
91+ needle=" .," ;
92+ x=replaceAllChar (haystack,needle,newneedle,false );
93+ std::cout<<" Replace Char " <<x<<std::endl;
94+ fprintf (stderr," x=%s\n " ,x.c_str ());
95+ for (int j=0 ;j<x.length ();j++)
96+ fprintf (stderr," 0x%x\n " ,x.at (j));
97+
98+ //
99+ // erase
100+ x=replaceAllChar (haystack,needle,newneedle,true );
101+ std::cout<<x<<std::endl;
102+ fprintf (stderr," x=%s\n " ,x.c_str ());
103+ for (int j=0 ;j<x.length ();j++)
104+ fprintf (stderr," 0x%x\n " ,x.at (j));
105+
106+ // libtest
107+ std::cout<<" Library test" <<std::endl;
108+ x=LFSTK_UtilityClass::LFSTK_strReplaceAllChar (haystack,needle," =!=" );
109+ std::cout<<x<<std::endl;
110+ fprintf (stderr," x=%s\n " ,x.c_str ());
111+ for (int j=0 ;j<x.length ();j++)
112+ fprintf (stderr," 0x%x\n " ,x.at (j));
113+
114+ delete apc;
115+ cairo_debug_reset_static_data ();
116+ return (0 );
117+ }
0 commit comments