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.
Commit
233e9328ee639b3e9bdefd87c27e5d4a9fc86c79
Parent
173cca0d7d10985…
1 file changed
+26
-31
+26
-31
| --- src/diff.c | ||
| +++ src/diff.c | ||
| @@ -137,44 +137,37 @@ | ||
| 137 | 137 | const char *z, |
| 138 | 138 | int n, |
| 139 | 139 | int *pnLine, |
| 140 | 140 | u64 diffFlags |
| 141 | 141 | ){ |
| 142 | - int nLine, i, j, k, s, x; | |
| 142 | + int nLine, i, k, nn, s, x; | |
| 143 | 143 | unsigned int h, h2; |
| 144 | 144 | DLine *a; |
| 145 | + const char *zNL, *z2; | |
| 145 | 146 | |
| 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 | |
| 148 | 152 | */ |
| 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 | + } | |
| 174 | 167 | a[i].z = z; |
| 175 | - k = j; | |
| 168 | + k = nn; | |
| 176 | 169 | if( diffFlags & DIFF_STRIP_EOLCR ){ |
| 177 | 170 | if( k>0 && z[k-1]=='\r' ){ k--; } |
| 178 | 171 | } |
| 179 | 172 | a[i].n = k; |
| 180 | 173 | s = 0; |
| @@ -202,12 +195,14 @@ | ||
| 202 | 195 | a[i].indent = s; |
| 203 | 196 | a[i].h = h = (h<<LENGTH_MASK_SZ) | (k-s); |
| 204 | 197 | h2 = h % nLine; |
| 205 | 198 | a[i].iNext = a[h2].iHash; |
| 206 | 199 | 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 ); | |
| 209 | 204 | |
| 210 | 205 | /* Return results */ |
| 211 | 206 | *pnLine = nLine; |
| 212 | 207 | return a; |
| 213 | 208 | } |
| 214 | 209 |
| --- 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 |