Fossil SCM

Fix a problem with initial indent introduced by the previous check-in, so that all regression tests from test/comment.test now succeed. Also eliminate three more calls to fossil_print(). Regarding performance, the legacy comment printing algorithm is outnumbered by factor 2-3, with these changes.

florian 2018-11-15 15:15 UTC comment-formatter-utf8
Commit b029ed2222b0b803a5b3ccffb3b02b3433bfc017
1 file changed +29 -21
+29 -21
--- src/comformat.c
+++ src/comformat.c
@@ -95,21 +95,20 @@
9595
#endif
9696
}
9797
9898
/*
9999
** This function checks the current line being printed against the original
100
-** comment text. Upon matching, it emits a new line and updates the provided
101
-** character and line counts, if applicable.
100
+** comment text. Upon matching, it updates the provided character and line
101
+** counts, if applicable. The caller needs to emit a new line, if desired.
102102
*/
103103
static int comment_check_orig(
104104
const char *zOrigText, /* [in] Original comment text ONLY, may be NULL. */
105105
const char *zLine, /* [in] The comment line to print. */
106106
int *pCharCnt, /* [in/out] Pointer to the line character count. */
107107
int *pLineCnt /* [in/out] Pointer to the total line count. */
108108
){
109109
if( zOrigText && fossil_strcmp(zLine, zOrigText)==0 ){
110
- fossil_print("\n");
111110
if( pCharCnt ) *pCharCnt = 0;
112111
if( pLineCnt ) (*pLineCnt)++;
113112
return 1;
114113
}
115114
return 0;
@@ -135,23 +134,20 @@
135134
}
136135
return 0; /* NOT REACHED */
137136
}
138137
139138
/*
140
-** This function is called when printing a logical comment line to perform
141
-** the necessary indenting.
139
+** This function is called when printing a logical comment line to calculate
140
+** the necessary indenting. The caller needs to emit the indenting spaces.
142141
*/
143
-static void comment_print_indent(
142
+static void comment_calc_indent(
144143
const char *zLine, /* [in] The comment line being printed. */
145144
int indent, /* [in] Number of spaces to indent, zero for none. */
146145
int trimCrLf, /* [in] Non-zero to trim leading/trailing CR/LF. */
147146
int trimSpace, /* [in] Non-zero to trim leading/trailing spaces. */
148147
int *piIndex /* [in/out] Pointer to first non-space character. */
149148
){
150
- if( indent>0 ){
151
- fossil_print("%*s", indent, "");
152
- }
153149
if( zLine && piIndex ){
154150
int index = *piIndex;
155151
if( trimCrLf ){
156152
while( zLine[index]=='\r' || zLine[index]=='\n' ){ index++; }
157153
}
@@ -179,22 +175,36 @@
179175
int wordBreak, /* [in] Non-zero to try breaking on word boundaries. */
180176
int origBreak, /* [in] Non-zero to break before original comment. */
181177
int *pLineCnt, /* [in/out] Pointer to the total line count. */
182178
const char **pzLine /* [out] Pointer to the end of the logical line. */
183179
){
184
- int index = 0, charCnt = 0, lineCnt = 0, maxChars;
180
+ int index = 0, charCnt = 0, lineCnt = 0, maxChars, i;
185181
char zBuf[400]; int iBuf=0; /* Output buffer and counter. */
186182
if( !zLine ) return;
187183
if( lineChars<=0 ) return;
188
- comment_print_indent(zLine, indent, trimCrLf, trimSpace, &index);
184
+#if 0
185
+ assert( indent<sizeof(zBuf)-5 ); /* See following comments to explain */
186
+ assert( origIndent<sizeof(zBuf)-5 ); /* these limits. */
187
+#endif
188
+ if ( indent>sizeof(zBuf)-6 ) /* Limit initial indent to fit output buffer. */
189
+ indent = sizeof(zBuf)-6;
190
+ comment_calc_indent(zLine, indent, trimCrLf, trimSpace, &index);
191
+ if ( indent>0 ){
192
+ for ( i=0; i<indent; i++ ){
193
+ zBuf[iBuf++] = ' ';
194
+ }
195
+ }
196
+ if ( origIndent>sizeof(zBuf)-6 ) /* Limit line indent to fit output buffer. */
197
+ origIndent = sizeof(zBuf)-6;
189198
maxChars = lineChars;
190199
for(;;){
191200
int useChars = 1;
192201
char c = zLine[index];
193202
/* Flush the output buffer if there's no space left for at least one more
194
- ** (potentially 4-byte) UTF-8 sequence and a terminating NULL. */
195
- if ( iBuf>sizeof(zBuf)-5 ){
203
+ ** (potentially 4-byte) UTF-8 sequence, one level of indentation spaces,
204
+ ** a new line, and a terminating NULL. */
205
+ if ( iBuf>sizeof(zBuf)-origIndent-6 ){
196206
zBuf[iBuf]=0;
197207
iBuf=0;
198208
fossil_print("%s", zBuf);
199209
}
200210
if( c==0 ){
@@ -201,18 +211,16 @@
201211
break;
202212
}else{
203213
if( origBreak && index>0 ){
204214
const char *zCurrent = &zLine[index];
205215
if( comment_check_orig(zOrigText, zCurrent, &charCnt, &lineCnt) ){
206
- /* Flush the output buffer before printing the indentation. */
207
- if ( iBuf>0 ){
208
- zBuf[iBuf]=0;
209
- iBuf=0;
210
- fossil_print("%s", zBuf);
211
- }
212
- comment_print_indent(zCurrent, origIndent, trimCrLf, trimSpace,
213
- &index);
216
+ zBuf[iBuf++] = '\n';
217
+ comment_calc_indent(zCurrent, origIndent, trimCrLf, trimSpace,
218
+ &index);
219
+ for ( i=0; i<origIndent; i++ ){
220
+ zBuf[iBuf++] = ' ';
221
+ }
214222
maxChars = lineChars;
215223
}
216224
}
217225
index++;
218226
}
219227
--- src/comformat.c
+++ src/comformat.c
@@ -95,21 +95,20 @@
95 #endif
96 }
97
98 /*
99 ** This function checks the current line being printed against the original
100 ** comment text. Upon matching, it emits a new line and updates the provided
101 ** character and line counts, if applicable.
102 */
103 static int comment_check_orig(
104 const char *zOrigText, /* [in] Original comment text ONLY, may be NULL. */
105 const char *zLine, /* [in] The comment line to print. */
106 int *pCharCnt, /* [in/out] Pointer to the line character count. */
107 int *pLineCnt /* [in/out] Pointer to the total line count. */
108 ){
109 if( zOrigText && fossil_strcmp(zLine, zOrigText)==0 ){
110 fossil_print("\n");
111 if( pCharCnt ) *pCharCnt = 0;
112 if( pLineCnt ) (*pLineCnt)++;
113 return 1;
114 }
115 return 0;
@@ -135,23 +134,20 @@
135 }
136 return 0; /* NOT REACHED */
137 }
138
139 /*
140 ** This function is called when printing a logical comment line to perform
141 ** the necessary indenting.
142 */
143 static void comment_print_indent(
144 const char *zLine, /* [in] The comment line being printed. */
145 int indent, /* [in] Number of spaces to indent, zero for none. */
146 int trimCrLf, /* [in] Non-zero to trim leading/trailing CR/LF. */
147 int trimSpace, /* [in] Non-zero to trim leading/trailing spaces. */
148 int *piIndex /* [in/out] Pointer to first non-space character. */
149 ){
150 if( indent>0 ){
151 fossil_print("%*s", indent, "");
152 }
153 if( zLine && piIndex ){
154 int index = *piIndex;
155 if( trimCrLf ){
156 while( zLine[index]=='\r' || zLine[index]=='\n' ){ index++; }
157 }
@@ -179,22 +175,36 @@
179 int wordBreak, /* [in] Non-zero to try breaking on word boundaries. */
180 int origBreak, /* [in] Non-zero to break before original comment. */
181 int *pLineCnt, /* [in/out] Pointer to the total line count. */
182 const char **pzLine /* [out] Pointer to the end of the logical line. */
183 ){
184 int index = 0, charCnt = 0, lineCnt = 0, maxChars;
185 char zBuf[400]; int iBuf=0; /* Output buffer and counter. */
186 if( !zLine ) return;
187 if( lineChars<=0 ) return;
188 comment_print_indent(zLine, indent, trimCrLf, trimSpace, &index);
 
 
 
 
 
 
 
 
 
 
 
 
 
189 maxChars = lineChars;
190 for(;;){
191 int useChars = 1;
192 char c = zLine[index];
193 /* Flush the output buffer if there's no space left for at least one more
194 ** (potentially 4-byte) UTF-8 sequence and a terminating NULL. */
195 if ( iBuf>sizeof(zBuf)-5 ){
 
196 zBuf[iBuf]=0;
197 iBuf=0;
198 fossil_print("%s", zBuf);
199 }
200 if( c==0 ){
@@ -201,18 +211,16 @@
201 break;
202 }else{
203 if( origBreak && index>0 ){
204 const char *zCurrent = &zLine[index];
205 if( comment_check_orig(zOrigText, zCurrent, &charCnt, &lineCnt) ){
206 /* Flush the output buffer before printing the indentation. */
207 if ( iBuf>0 ){
208 zBuf[iBuf]=0;
209 iBuf=0;
210 fossil_print("%s", zBuf);
211 }
212 comment_print_indent(zCurrent, origIndent, trimCrLf, trimSpace,
213 &index);
214 maxChars = lineChars;
215 }
216 }
217 index++;
218 }
219
--- src/comformat.c
+++ src/comformat.c
@@ -95,21 +95,20 @@
95 #endif
96 }
97
98 /*
99 ** This function checks the current line being printed against the original
100 ** comment text. Upon matching, it updates the provided character and line
101 ** counts, if applicable. The caller needs to emit a new line, if desired.
102 */
103 static int comment_check_orig(
104 const char *zOrigText, /* [in] Original comment text ONLY, may be NULL. */
105 const char *zLine, /* [in] The comment line to print. */
106 int *pCharCnt, /* [in/out] Pointer to the line character count. */
107 int *pLineCnt /* [in/out] Pointer to the total line count. */
108 ){
109 if( zOrigText && fossil_strcmp(zLine, zOrigText)==0 ){
 
110 if( pCharCnt ) *pCharCnt = 0;
111 if( pLineCnt ) (*pLineCnt)++;
112 return 1;
113 }
114 return 0;
@@ -135,23 +134,20 @@
134 }
135 return 0; /* NOT REACHED */
136 }
137
138 /*
139 ** This function is called when printing a logical comment line to calculate
140 ** the necessary indenting. The caller needs to emit the indenting spaces.
141 */
142 static void comment_calc_indent(
143 const char *zLine, /* [in] The comment line being printed. */
144 int indent, /* [in] Number of spaces to indent, zero for none. */
145 int trimCrLf, /* [in] Non-zero to trim leading/trailing CR/LF. */
146 int trimSpace, /* [in] Non-zero to trim leading/trailing spaces. */
147 int *piIndex /* [in/out] Pointer to first non-space character. */
148 ){
 
 
 
149 if( zLine && piIndex ){
150 int index = *piIndex;
151 if( trimCrLf ){
152 while( zLine[index]=='\r' || zLine[index]=='\n' ){ index++; }
153 }
@@ -179,22 +175,36 @@
175 int wordBreak, /* [in] Non-zero to try breaking on word boundaries. */
176 int origBreak, /* [in] Non-zero to break before original comment. */
177 int *pLineCnt, /* [in/out] Pointer to the total line count. */
178 const char **pzLine /* [out] Pointer to the end of the logical line. */
179 ){
180 int index = 0, charCnt = 0, lineCnt = 0, maxChars, i;
181 char zBuf[400]; int iBuf=0; /* Output buffer and counter. */
182 if( !zLine ) return;
183 if( lineChars<=0 ) return;
184 #if 0
185 assert( indent<sizeof(zBuf)-5 ); /* See following comments to explain */
186 assert( origIndent<sizeof(zBuf)-5 ); /* these limits. */
187 #endif
188 if ( indent>sizeof(zBuf)-6 ) /* Limit initial indent to fit output buffer. */
189 indent = sizeof(zBuf)-6;
190 comment_calc_indent(zLine, indent, trimCrLf, trimSpace, &index);
191 if ( indent>0 ){
192 for ( i=0; i<indent; i++ ){
193 zBuf[iBuf++] = ' ';
194 }
195 }
196 if ( origIndent>sizeof(zBuf)-6 ) /* Limit line indent to fit output buffer. */
197 origIndent = sizeof(zBuf)-6;
198 maxChars = lineChars;
199 for(;;){
200 int useChars = 1;
201 char c = zLine[index];
202 /* Flush the output buffer if there's no space left for at least one more
203 ** (potentially 4-byte) UTF-8 sequence, one level of indentation spaces,
204 ** a new line, and a terminating NULL. */
205 if ( iBuf>sizeof(zBuf)-origIndent-6 ){
206 zBuf[iBuf]=0;
207 iBuf=0;
208 fossil_print("%s", zBuf);
209 }
210 if( c==0 ){
@@ -201,18 +211,16 @@
211 break;
212 }else{
213 if( origBreak && index>0 ){
214 const char *zCurrent = &zLine[index];
215 if( comment_check_orig(zOrigText, zCurrent, &charCnt, &lineCnt) ){
216 zBuf[iBuf++] = '\n';
217 comment_calc_indent(zCurrent, origIndent, trimCrLf, trimSpace,
218 &index);
219 for ( i=0; i<origIndent; i++ ){
220 zBuf[iBuf++] = ' ';
221 }
 
 
222 maxChars = lineChars;
223 }
224 }
225 index++;
226 }
227

Keyboard Shortcuts

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