@@ -25,53 +25,37 @@ NormalisePath::NormalisePath(const std::string &action)
25
25
}
26
26
27
27
bool NormalisePath::transform (std::string &value, const Transaction *trans) const {
28
- int _changed = 0 ;
29
-
30
- char *tmp = reinterpret_cast <char *>(
31
- malloc (sizeof (char ) * value.size () + 1 ));
32
- memcpy (tmp, value.c_str (), value.size () + 1 );
33
- tmp[value.size ()] = ' \0 ' ;
34
-
35
- int i = normalize_path_inplace ((unsigned char *)tmp,
36
- value.size (), 0 , &_changed);
37
-
38
- std::string ret (" " );
39
- ret.assign (tmp, i);
40
- free (tmp);
41
-
42
- const auto changed = ret != value;
43
- value = ret;
44
- return changed;
28
+ return normalize_path_inplace (value, false );
45
29
}
46
30
47
31
48
32
/* *
49
33
*
50
34
* IMP1 Assumes NUL-terminated
51
35
*/
52
- int NormalisePath::normalize_path_inplace (unsigned char *input, int input_len,
53
- int win, int *changed) {
36
+ bool NormalisePath::normalize_path_inplace (std::string &val, const bool win) {
54
37
unsigned char *src;
55
38
unsigned char *dst;
56
39
unsigned char *end;
57
- int ldst = 0 ;
58
40
int hitroot = 0 ;
59
41
int done = 0 ;
60
42
int relative;
61
43
int trailing;
62
44
63
- * changed = 0 ;
45
+ bool changed = false ;
64
46
65
47
/* Need at least one byte to normalize */
66
- if (input_len <= 0 ) return 0 ;
48
+ if (val.empty ()) return false ;
49
+
50
+ auto input = reinterpret_cast <unsigned char *>(val.data ());
51
+ const auto input_len = val.length ();
67
52
68
53
/*
69
54
* ENH: Deal with UNC and drive letters?
70
55
*/
71
56
72
57
src = dst = input;
73
58
end = input + (input_len - 1 );
74
- ldst = 1 ;
75
59
76
60
relative = ((*input == ' /' ) || (win && (*input == ' \\ ' ))) ? 0 : 1 ;
77
61
trailing = ((*end == ' /' ) || (win && (*end == ' \\ ' ))) ? 1 : 0 ;
@@ -82,11 +66,11 @@ int NormalisePath::normalize_path_inplace(unsigned char *input, int input_len,
82
66
if (win) {
83
67
if (*src == ' \\ ' ) {
84
68
*src = ' /' ;
85
- * changed = 1 ;
69
+ changed = true ;
86
70
}
87
71
if ((src < end) && (*(src + 1 ) == ' \\ ' )) {
88
72
*(src + 1 ) = ' /' ;
89
- * changed = 1 ;
73
+ changed = true ;
90
74
}
91
75
}
92
76
@@ -104,7 +88,7 @@ int NormalisePath::normalize_path_inplace(unsigned char *input, int input_len,
104
88
/* Could it be an empty path segment? */
105
89
if ((src != end) && *src == ' /' ) {
106
90
/* Ignore */
107
- * changed = 1 ;
91
+ changed = true ;
108
92
goto copy; /* Copy will take care of this. */
109
93
} else if (*src == ' .' ) {
110
94
/* Could it be a back or self reference? */
@@ -141,25 +125,25 @@ int NormalisePath::normalize_path_inplace(unsigned char *input, int input_len,
141
125
}
142
126
}
143
127
144
- if (done) goto length ; /* Skip the copy. */
128
+ if (done) goto skip_copy ; /* Skip the copy. */
145
129
src++;
146
130
147
- * changed = 1 ;
131
+ changed = true ;
148
132
} else if (dst == input) {
149
133
/* Relative Self-reference? */
150
- * changed = 1 ;
134
+ changed = true ;
151
135
152
136
/* Ignore. */
153
137
154
- if (done) goto length ; /* Skip the copy. */
138
+ if (done) goto skip_copy ; /* Skip the copy. */
155
139
src++;
156
140
} else if (*(dst - 1 ) == ' /' ) {
157
141
/* Self-reference? */
158
- * changed = 1 ;
142
+ changed = true ;
159
143
160
144
/* Ignore. */
161
145
162
- if (done) goto length ; /* Skip the copy. */
146
+ if (done) goto skip_copy ; /* Skip the copy. */
163
147
dst--;
164
148
src++;
165
149
}
@@ -179,35 +163,35 @@ int NormalisePath::normalize_path_inplace(unsigned char *input, int input_len,
179
163
&& ((*(src + 1 ) == ' /' ) || (win && (*(src + 1 ) == ' \\ ' ))) ) {
180
164
src++;
181
165
}
182
- if (oldsrc != src) * changed = 1 ;
166
+ if (oldsrc != src) changed = true ;
183
167
184
168
/* Do not copy the forward slash to the root
185
169
* if it is not a relative path. Instead
186
170
* move over the slash to the next segment.
187
171
*/
188
172
if (relative && (dst == input)) {
189
173
src++;
190
- goto length ; /* Skip the copy */
174
+ goto skip_copy ; /* Skip the copy */
191
175
}
192
176
}
193
177
194
178
*(dst++) = *(src++);
195
179
196
- length :
197
- ldst = (dst - input);
180
+ skip_copy :
181
+ ; // nop for the goto label to work
198
182
}
199
183
/* Make sure that there is not a trailing slash in the
200
184
* normalized form if there was not one in the original form.
201
185
*/
202
186
if (!trailing && (dst > input) && *(dst - 1 ) == ' /' ) {
203
- ldst--;
204
187
dst--;
205
188
}
206
189
207
190
/* Always NUL terminate */
208
191
*dst = ' \0 ' ;
209
192
210
- return ldst;
193
+ val.resize (dst - input);
194
+ return changed;
211
195
}
212
196
213
197
0 commit comments