Fossil SCM
speed-up lools_like_text(), by eliminating variable "i" and handle first character separately.
Commit
204680eedc0232952480fb932897ac5947f82a4b
Parent
186405ce3abfe8b…
1 file changed
+10
-6
+10
-6
| --- src/diff.c | ||
| +++ src/diff.c | ||
| @@ -177,21 +177,25 @@ | ||
| 177 | 177 | ** contains a line that is too long |
| 178 | 178 | ** Returns -1, if the file appears text, but it contains CrLf |
| 179 | 179 | */ |
| 180 | 180 | int looks_like_text(const Blob *pContent){ |
| 181 | 181 | const char *z = blob_buffer(pContent); |
| 182 | - int n = blob_size(pContent); | |
| 183 | - int i, j; | |
| 182 | + unsigned int n = blob_size(pContent); | |
| 183 | + int j, c; | |
| 184 | 184 | int result = 1; |
| 185 | 185 | |
| 186 | 186 | /* Check individual lines. |
| 187 | 187 | */ |
| 188 | - for(i=j=0; i<n; i++, j++){ | |
| 189 | - int c = z[i]; | |
| 190 | - if( c==0 ) return 1; /* \000 byte in a file -> binary */ | |
| 188 | + if( n==0 ) return 1; /* Empty file -> text */ | |
| 189 | + c = *z; | |
| 190 | + if( c==0 ) return 0; /* \000 byte in a file -> binary */ | |
| 191 | + j = (c!='\n'); | |
| 192 | + while( --n>0 ){ | |
| 193 | + c = *++z; | |
| 194 | + if( c==0 ) return 0; /* \000 byte in a file -> binary */ | |
| 191 | 195 | if( c=='\n' ){ |
| 192 | - if( i>0 && z[i-1]=='\r' ){ | |
| 196 | + if( z[-1]=='\r' ){ | |
| 193 | 197 | result = -1; /* Contains CrLf, continue */ |
| 194 | 198 | } |
| 195 | 199 | if( j>LENGTH_MASK ){ |
| 196 | 200 | return 0; /* Very long line -> binary */ |
| 197 | 201 | } |
| 198 | 202 |
| --- src/diff.c | |
| +++ src/diff.c | |
| @@ -177,21 +177,25 @@ | |
| 177 | ** contains a line that is too long |
| 178 | ** Returns -1, if the file appears text, but it contains CrLf |
| 179 | */ |
| 180 | int looks_like_text(const Blob *pContent){ |
| 181 | const char *z = blob_buffer(pContent); |
| 182 | int n = blob_size(pContent); |
| 183 | int i, j; |
| 184 | int result = 1; |
| 185 | |
| 186 | /* Check individual lines. |
| 187 | */ |
| 188 | for(i=j=0; i<n; i++, j++){ |
| 189 | int c = z[i]; |
| 190 | if( c==0 ) return 1; /* \000 byte in a file -> binary */ |
| 191 | if( c=='\n' ){ |
| 192 | if( i>0 && z[i-1]=='\r' ){ |
| 193 | result = -1; /* Contains CrLf, continue */ |
| 194 | } |
| 195 | if( j>LENGTH_MASK ){ |
| 196 | return 0; /* Very long line -> binary */ |
| 197 | } |
| 198 |
| --- src/diff.c | |
| +++ src/diff.c | |
| @@ -177,21 +177,25 @@ | |
| 177 | ** contains a line that is too long |
| 178 | ** Returns -1, if the file appears text, but it contains CrLf |
| 179 | */ |
| 180 | int looks_like_text(const Blob *pContent){ |
| 181 | const char *z = blob_buffer(pContent); |
| 182 | unsigned int n = blob_size(pContent); |
| 183 | int j, c; |
| 184 | int result = 1; |
| 185 | |
| 186 | /* Check individual lines. |
| 187 | */ |
| 188 | if( n==0 ) return 1; /* Empty file -> text */ |
| 189 | c = *z; |
| 190 | if( c==0 ) return 0; /* \000 byte in a file -> binary */ |
| 191 | j = (c!='\n'); |
| 192 | while( --n>0 ){ |
| 193 | c = *++z; |
| 194 | if( c==0 ) return 0; /* \000 byte in a file -> binary */ |
| 195 | if( c=='\n' ){ |
| 196 | if( z[-1]=='\r' ){ |
| 197 | result = -1; /* Contains CrLf, continue */ |
| 198 | } |
| 199 | if( j>LENGTH_MASK ){ |
| 200 | return 0; /* Very long line -> binary */ |
| 201 | } |
| 202 |