Fossil SCM

speed-up lools_like_text(), by eliminating variable "i" and handle first character separately.

jan.nijtmans 2012-10-28 20:40 trunk
Commit 204680eedc0232952480fb932897ac5947f82a4b
1 file changed +10 -6
+10 -6
--- src/diff.c
+++ src/diff.c
@@ -177,21 +177,25 @@
177177
** contains a line that is too long
178178
** Returns -1, if the file appears text, but it contains CrLf
179179
*/
180180
int looks_like_text(const Blob *pContent){
181181
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;
184184
int result = 1;
185185
186186
/* Check individual lines.
187187
*/
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 */
191195
if( c=='\n' ){
192
- if( i>0 && z[i-1]=='\r' ){
196
+ if( z[-1]=='\r' ){
193197
result = -1; /* Contains CrLf, continue */
194198
}
195199
if( j>LENGTH_MASK ){
196200
return 0; /* Very long line -> binary */
197201
}
198202
--- 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

Keyboard Shortcuts

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