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.
Commit
b029ed2222b0b803a5b3ccffb3b02b3433bfc017
Parent
16fde3ff666cf07…
1 file changed
+29
-21
+29
-21
| --- src/comformat.c | ||
| +++ src/comformat.c | ||
| @@ -95,21 +95,20 @@ | ||
| 95 | 95 | #endif |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /* |
| 99 | 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. | |
| 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 | 102 | */ |
| 103 | 103 | static int comment_check_orig( |
| 104 | 104 | const char *zOrigText, /* [in] Original comment text ONLY, may be NULL. */ |
| 105 | 105 | const char *zLine, /* [in] The comment line to print. */ |
| 106 | 106 | int *pCharCnt, /* [in/out] Pointer to the line character count. */ |
| 107 | 107 | int *pLineCnt /* [in/out] Pointer to the total line count. */ |
| 108 | 108 | ){ |
| 109 | 109 | if( zOrigText && fossil_strcmp(zLine, zOrigText)==0 ){ |
| 110 | - fossil_print("\n"); | |
| 111 | 110 | if( pCharCnt ) *pCharCnt = 0; |
| 112 | 111 | if( pLineCnt ) (*pLineCnt)++; |
| 113 | 112 | return 1; |
| 114 | 113 | } |
| 115 | 114 | return 0; |
| @@ -135,23 +134,20 @@ | ||
| 135 | 134 | } |
| 136 | 135 | return 0; /* NOT REACHED */ |
| 137 | 136 | } |
| 138 | 137 | |
| 139 | 138 | /* |
| 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. | |
| 142 | 141 | */ |
| 143 | -static void comment_print_indent( | |
| 142 | +static void comment_calc_indent( | |
| 144 | 143 | const char *zLine, /* [in] The comment line being printed. */ |
| 145 | 144 | int indent, /* [in] Number of spaces to indent, zero for none. */ |
| 146 | 145 | int trimCrLf, /* [in] Non-zero to trim leading/trailing CR/LF. */ |
| 147 | 146 | int trimSpace, /* [in] Non-zero to trim leading/trailing spaces. */ |
| 148 | 147 | int *piIndex /* [in/out] Pointer to first non-space character. */ |
| 149 | 148 | ){ |
| 150 | - if( indent>0 ){ | |
| 151 | - fossil_print("%*s", indent, ""); | |
| 152 | - } | |
| 153 | 149 | if( zLine && piIndex ){ |
| 154 | 150 | int index = *piIndex; |
| 155 | 151 | if( trimCrLf ){ |
| 156 | 152 | while( zLine[index]=='\r' || zLine[index]=='\n' ){ index++; } |
| 157 | 153 | } |
| @@ -179,22 +175,36 @@ | ||
| 179 | 175 | int wordBreak, /* [in] Non-zero to try breaking on word boundaries. */ |
| 180 | 176 | int origBreak, /* [in] Non-zero to break before original comment. */ |
| 181 | 177 | int *pLineCnt, /* [in/out] Pointer to the total line count. */ |
| 182 | 178 | const char **pzLine /* [out] Pointer to the end of the logical line. */ |
| 183 | 179 | ){ |
| 184 | - int index = 0, charCnt = 0, lineCnt = 0, maxChars; | |
| 180 | + int index = 0, charCnt = 0, lineCnt = 0, maxChars, i; | |
| 185 | 181 | char zBuf[400]; int iBuf=0; /* Output buffer and counter. */ |
| 186 | 182 | if( !zLine ) return; |
| 187 | 183 | 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; | |
| 189 | 198 | maxChars = lineChars; |
| 190 | 199 | for(;;){ |
| 191 | 200 | int useChars = 1; |
| 192 | 201 | char c = zLine[index]; |
| 193 | 202 | /* 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 ){ | |
| 196 | 206 | zBuf[iBuf]=0; |
| 197 | 207 | iBuf=0; |
| 198 | 208 | fossil_print("%s", zBuf); |
| 199 | 209 | } |
| 200 | 210 | if( c==0 ){ |
| @@ -201,18 +211,16 @@ | ||
| 201 | 211 | break; |
| 202 | 212 | }else{ |
| 203 | 213 | if( origBreak && index>0 ){ |
| 204 | 214 | const char *zCurrent = &zLine[index]; |
| 205 | 215 | 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 | + } | |
| 214 | 222 | maxChars = lineChars; |
| 215 | 223 | } |
| 216 | 224 | } |
| 217 | 225 | index++; |
| 218 | 226 | } |
| 219 | 227 |
| --- 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 |