1 /******************************************************************************
2 *
3 * Copyright (C) 2008, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * Author: Alexey Krivonogov ( gentee )
11 *
12 ******************************************************************************/
13
14 /*-----------------------------------------------------------------------------
15 * Id: str_replace_1 FA
16 *
17 * Summary: The method looks for strings from one array and replace to strings
18 of another array.
19 *
20 * Params: aold - The strings to be replaced.
21 anew - The new strings.
22 flags - Flags. $$[patternflags]
23 *
24 * Return: #lng/retobj#
25 *
26 -----------------------------------------------------------------------------*/
27
28 method str str.replace( arrstr aold, arrstr anew, uint flags )
29 {
30 uint i cur ok end
31 uint count = *aold
32 arr pat[ count ] of spattern
33 arr off[ count ]
34 str ret
35
36 if !count : return this
37
38 fornum i, count : pat[i].init( aold[i], flags )
39 end = *this
40 while cur < end
41 {
42 uint ioff
43 fornum i = 0, count
44 {
45 if off[i] <= cur : off[i] = pat[i].search( this, cur )
46 if off[i] < off[ ioff ] : ioff = i
47 // print("\(i)=\(off[i])\n")
48 }
49 ret.append( this.ptr() + cur, off[ ioff ] - cur )
50 cur += off[ ioff ] - cur
51 if off[ ioff ] < end
52 {
53 // print( "Find \(off[ ioff ]) \(end)\n")
54 ret.append( anew[ ioff ].ptr(), *anew[ ioff ] )
55 cur += *aold[ ioff ]
56 ok = 1
57 }
58 }
59 if ok : this = ret
60
61 return this
62 }
63
64 /*-----------------------------------------------------------------------------
65 * Id: str_replace_2 FA
66 *
67 * Summary: The method replaces one string to another string in the source
68 string.
69 *
70 * Params: sold - The string to be replaced.
71 snew - The new string.
72 flags - Flags. $$[patternflags]
73 *
74 * Return: #lng/retobj#
75 *
76 -----------------------------------------------------------------------------*/
77
78 method str str.replace( str sold, str snew, uint flags )
79 {
80 uint cur ok end
81 spattern pat
82 uint off
83 str ret
84
85 pat.init( sold, flags )
86 end = *this
87 while cur < end
88 {
89 uint ioff
90
91 off = pat.search( this, cur )
92 ret.append( this.ptr() + cur, off - cur )
93 cur += off - cur
94 if off < end
95 {
96 ret.append( snew.ptr(), *snew )
97 cur += *sold
98 ok = 1
99 }
100 }
101 if ok : this = ret
102
103 return this
104 }
105
106 /*-----------------------------------------------------------------------------
107 ** Id: arrstr_replace F2
108 *
109 * Summary: Replace substrings for the each item. The method looks for
110 strings from one array and replace to strings
111 of another array for the each string of the array.
112 *
113 * Params: aold - The strings to be replaced.
114 anew - The new strings.
115 flags - Flags. $$[patternflags]
116 *
117 * Return: #lng/retobj#
118 *
119 -----------------------------------------------------------------------------*/
120
121 method arrstr arrstr.replace( arrstr aold, arrstr anew, uint flags )
122 {
123 foreach cur, this : cur.replace( aold, anew, flags )
124 return this
125 }
126
Редактировать