Fossil SCM

Simplification and optimization of the break_into_lines() helper routine in the diff logic. Use the strchr() routine from the standard library to help locate \n characters.

drh 2016-09-28 21:24 trunk
Commit 233e9328ee639b3e9bdefd87c27e5d4a9fc86c79
1 file changed +26 -31
+26 -31
--- src/diff.c
+++ src/diff.c
@@ -137,44 +137,37 @@
137137
const char *z,
138138
int n,
139139
int *pnLine,
140140
u64 diffFlags
141141
){
142
- int nLine, i, j, k, s, x;
142
+ int nLine, i, k, nn, s, x;
143143
unsigned int h, h2;
144144
DLine *a;
145
+ const char *zNL, *z2;
145146
146
- /* Count the number of lines. Allocate space to hold
147
- ** the returned array.
147
+ /* Early-out for the degenerate case */
148
+ if( n==0 ) return 0;
149
+
150
+ /* Count the number of lines in the input file. Include the last line
151
+ ** in the count even if it lacks the \n terminator
148152
*/
149
- for(i=j=0, nLine=1; i<n; i++, j++){
150
- if( z[i]<='\n' ){
151
- if( z[i]==0 ) return 0;
152
- if( z[i]=='\n' && z[i+1]!=0 ){
153
- nLine++;
154
- if( j>LENGTH_MASK ){
155
- return 0;
156
- }
157
- j = 0;
158
- }
159
- }
160
- }
161
- if( j>LENGTH_MASK ){
162
- return 0;
163
- }
164
- a = fossil_malloc( nLine*sizeof(a[0]) );
165
- memset(a, 0, nLine*sizeof(a[0]) );
166
- if( n==0 ){
167
- *pnLine = 0;
168
- return a;
169
- }
170
-
171
- /* Fill in the array */
172
- for(i=0; i<nLine; i++){
173
- for(j=0; z[j]>'\n' || (z[j]!=0 && z[j]!='\n'); j++){}
153
+ for(nLine=0, z2=z; (zNL = strchr(z2,'\n'))!=0; z2=zNL+1, nLine++){}
154
+ if( z2[0]!=0 ) nLine++;
155
+
156
+ a = fossil_malloc( sizeof(a[0])*nLine );
157
+ memset(a, 0, sizeof(a[0])*nLine);
158
+ i = 0;
159
+ do{
160
+ zNL = strchr(z,'\n');
161
+ if( zNL==0 ) zNL = z+strlen(z);
162
+ nn = (int)(zNL - z);
163
+ if( nn>LENGTH_MASK ){
164
+ fossil_free(a);
165
+ return 0;
166
+ }
174167
a[i].z = z;
175
- k = j;
168
+ k = nn;
176169
if( diffFlags & DIFF_STRIP_EOLCR ){
177170
if( k>0 && z[k-1]=='\r' ){ k--; }
178171
}
179172
a[i].n = k;
180173
s = 0;
@@ -202,12 +195,14 @@
202195
a[i].indent = s;
203196
a[i].h = h = (h<<LENGTH_MASK_SZ) | (k-s);
204197
h2 = h % nLine;
205198
a[i].iNext = a[h2].iHash;
206199
a[h2].iHash = i+1;
207
- z += j+1;
208
- }
200
+ z += nn+1;
201
+ i++;
202
+ }while( zNL[0] && zNL[1] );
203
+ assert( i==nLine );
209204
210205
/* Return results */
211206
*pnLine = nLine;
212207
return a;
213208
}
214209
--- src/diff.c
+++ src/diff.c
@@ -137,44 +137,37 @@
137 const char *z,
138 int n,
139 int *pnLine,
140 u64 diffFlags
141 ){
142 int nLine, i, j, k, s, x;
143 unsigned int h, h2;
144 DLine *a;
 
145
146 /* Count the number of lines. Allocate space to hold
147 ** the returned array.
 
 
 
148 */
149 for(i=j=0, nLine=1; i<n; i++, j++){
150 if( z[i]<='\n' ){
151 if( z[i]==0 ) return 0;
152 if( z[i]=='\n' && z[i+1]!=0 ){
153 nLine++;
154 if( j>LENGTH_MASK ){
155 return 0;
156 }
157 j = 0;
158 }
159 }
160 }
161 if( j>LENGTH_MASK ){
162 return 0;
163 }
164 a = fossil_malloc( nLine*sizeof(a[0]) );
165 memset(a, 0, nLine*sizeof(a[0]) );
166 if( n==0 ){
167 *pnLine = 0;
168 return a;
169 }
170
171 /* Fill in the array */
172 for(i=0; i<nLine; i++){
173 for(j=0; z[j]>'\n' || (z[j]!=0 && z[j]!='\n'); j++){}
174 a[i].z = z;
175 k = j;
176 if( diffFlags & DIFF_STRIP_EOLCR ){
177 if( k>0 && z[k-1]=='\r' ){ k--; }
178 }
179 a[i].n = k;
180 s = 0;
@@ -202,12 +195,14 @@
202 a[i].indent = s;
203 a[i].h = h = (h<<LENGTH_MASK_SZ) | (k-s);
204 h2 = h % nLine;
205 a[i].iNext = a[h2].iHash;
206 a[h2].iHash = i+1;
207 z += j+1;
208 }
 
 
209
210 /* Return results */
211 *pnLine = nLine;
212 return a;
213 }
214
--- src/diff.c
+++ src/diff.c
@@ -137,44 +137,37 @@
137 const char *z,
138 int n,
139 int *pnLine,
140 u64 diffFlags
141 ){
142 int nLine, i, k, nn, s, x;
143 unsigned int h, h2;
144 DLine *a;
145 const char *zNL, *z2;
146
147 /* Early-out for the degenerate case */
148 if( n==0 ) return 0;
149
150 /* Count the number of lines in the input file. Include the last line
151 ** in the count even if it lacks the \n terminator
152 */
153 for(nLine=0, z2=z; (zNL = strchr(z2,'\n'))!=0; z2=zNL+1, nLine++){}
154 if( z2[0]!=0 ) nLine++;
155
156 a = fossil_malloc( sizeof(a[0])*nLine );
157 memset(a, 0, sizeof(a[0])*nLine);
158 i = 0;
159 do{
160 zNL = strchr(z,'\n');
161 if( zNL==0 ) zNL = z+strlen(z);
162 nn = (int)(zNL - z);
163 if( nn>LENGTH_MASK ){
164 fossil_free(a);
165 return 0;
166 }
 
 
 
 
 
 
 
 
 
 
 
167 a[i].z = z;
168 k = nn;
169 if( diffFlags & DIFF_STRIP_EOLCR ){
170 if( k>0 && z[k-1]=='\r' ){ k--; }
171 }
172 a[i].n = k;
173 s = 0;
@@ -202,12 +195,14 @@
195 a[i].indent = s;
196 a[i].h = h = (h<<LENGTH_MASK_SZ) | (k-s);
197 h2 = h % nLine;
198 a[i].iNext = a[h2].iHash;
199 a[h2].iHash = i+1;
200 z += nn+1;
201 i++;
202 }while( zNL[0] && zNL[1] );
203 assert( i==nLine );
204
205 /* Return results */
206 *pnLine = nLine;
207 return a;
208 }
209

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button