]> Shamusworld >> Repos - architektonas/blob - src/base/rs_string.cpp.old
3189f07151bc157c838021346d97cd533953aac2
[architektonas] / src / base / rs_string.cpp.old
1 /****************************************************************************
2 ** $Id: rs_string.cpp 1769 2003-10-22 19:46:31Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the qcadlib Library project.
7 **
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
12 **
13 ** Licensees holding valid qcadlib Professional Edition licenses may use
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27 #include "rs_string.h"
28
29 #include <iostream>
30
31 RS_String RS_StringCompat::replace(const RS_String & str, RS_Char c1, RS_Char c2)
32 {
33     RS_String ret = str;
34
35     for(uint i=0; i<ret.length(); ++i)
36         if (ret.at(i) == c1)
37             ret.ref(i) = c2;
38
39     return ret;
40 }
41
42 RS_String RS_StringCompat::replace(const RS_String & str, const RS_String & s1, const RS_String & s2)
43 {
44     if (s1.isEmpty())
45         return str;
46
47     RS_String ret = "";
48
49     for(uint i=0; i<str.length(); ++i)
50         {
51         if (str.mid(i, s1.length()) == s1)
52                 {
53             ret += s2;
54                         i += s1.length() - 1;
55         }
56         else
57                 {
58             ret += str.at(i);
59         }
60     }
61
62     return ret;
63 }
64
65 void RS_StringCompat::test()
66 {
67     RS_String res;
68     RS_String s1 = "abcdefg";
69     res = RS_StringCompat::replace(s1, 'a', 'A');
70     assert(res == "Abcdefg");
71     res = RS_StringCompat::replace(s1, 'b', 'B');
72     assert(res == "aBcdefg");
73     res = RS_StringCompat::replace(s1, 'g', 'G');
74     assert(res == "abcdefG");
75
76     res = RS_StringCompat::replace(s1, "", "blah");
77     assert(res == "abcdefg");
78     res = RS_StringCompat::replace(s1, "ab", "AB");
79     assert(res == "ABcdefg");
80     res = RS_StringCompat::replace(s1, "def", "DEF");
81     assert(res == "abcDEFg");
82     res = RS_StringCompat::replace(s1, "g", "G");
83     assert(res == "abcdefG");
84     res = RS_StringCompat::replace(s1, "fg", "FG");
85     assert(res == "abcdeFG");
86
87     s1 = "a";
88     res = RS_StringCompat::replace(s1, "a", "ABC");
89     assert(res == "ABC");
90     s1 = "ab";
91     res = RS_StringCompat::replace(s1, "ab", "");
92     assert(res == "");
93 }