Fossil SCM
Re-add the legacy comment printing algorithm. Currently, it is being retained primarily for testing and comparison purposes.
Commit
0463c7bfb1a9980bb08681b9fef37a7d3a9b15c1
Parent
265a48d3ec96cdb…
1 file changed
+118
-8
+118
-8
| --- src/comformat.c | ||
| +++ src/comformat.c | ||
| @@ -26,13 +26,14 @@ | ||
| 26 | 26 | #else |
| 27 | 27 | # include <termios.h> |
| 28 | 28 | #endif |
| 29 | 29 | |
| 30 | 30 | #if INTERFACE |
| 31 | -#define COMMENT_PRINT_NONE ((u32)0x00000000) /* No flags. */ | |
| 32 | -#define COMMENT_PRINT_TRIM_SPACE ((u32)0x00000001) /* Trim leading/trailing. */ | |
| 33 | -#define COMMENT_PRINT_WORD_BREAK ((u32)0x00000002) /* Break lines on words. */ | |
| 31 | +#define COMMENT_PRINT_NONE ((u32)0x00000000) /* No flags. */ | |
| 32 | +#define COMMENT_PRINT_LEGACY ((u32)0x00000001) /* Use legacy algorithm. */ | |
| 33 | +#define COMMENT_PRINT_TRIM_SPACE ((u32)0x00000002) /* Trim leading/trailing. */ | |
| 34 | +#define COMMENT_PRINT_WORD_BREAK ((u32)0x00000004) /* Break lines on words. */ | |
| 34 | 35 | #define COMMENT_PRINT_DEFAULT (COMMENT_PRINT_TRIM_SPACE) /* Defaults. */ |
| 35 | 36 | #endif |
| 36 | 37 | |
| 37 | 38 | /* |
| 38 | 39 | ** This is the previous value used by most external callers when they |
| @@ -156,29 +157,134 @@ | ||
| 156 | 157 | *pzLine = zLine + index; |
| 157 | 158 | } |
| 158 | 159 | } |
| 159 | 160 | |
| 160 | 161 | /* |
| 161 | -** Given a comment string zText, format that string for printing | |
| 162 | -** on a TTY. Assume that the output cursors is indent spaces from | |
| 163 | -** the left margin and that a single line can contain no more than | |
| 164 | -** width characters. Indent all subsequent lines by indent. | |
| 162 | +** This is the legacy comment printing algorithm. Currently, it is being | |
| 163 | +** retained primarily for testing and comparison purposes. | |
| 164 | +** | |
| 165 | +** Given a comment string, format that string for printing on a TTY. | |
| 166 | +** Assume that the output cursors is indent spaces from the left margin | |
| 167 | +** and that a single line can contain no more than width characters. | |
| 168 | +** Indent all subsequent lines by indent. | |
| 169 | +** | |
| 170 | +** Returns the number of new lines emitted. | |
| 171 | +*/ | |
| 172 | +static int comment_print_legacy( | |
| 173 | + const char *zText, /* The comment text to be printed. */ | |
| 174 | + int indent, /* Number of spaces to indent each non-initial line. */ | |
| 175 | + int width /* Maximum number of characters per line. */ | |
| 176 | +){ | |
| 177 | + int tlen = width - indent; | |
| 178 | + int si, sk, i, k; | |
| 179 | + int doIndent = 0; | |
| 180 | + char *zBuf; | |
| 181 | + char zBuffer[400]; | |
| 182 | + int lineCnt = 0; | |
| 183 | + | |
| 184 | +#if defined(_WIN32) | |
| 185 | + if( width<0 ){ | |
| 186 | + CONSOLE_SCREEN_BUFFER_INFO csbi; | |
| 187 | + memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO)); | |
| 188 | + if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){ | |
| 189 | + tlen = csbi.srWindow.Right - csbi.srWindow.Left - indent; | |
| 190 | + } | |
| 191 | + } | |
| 192 | +#elif defined(TIOCGWINSZ) | |
| 193 | + if( width<0 ){ | |
| 194 | + struct winsize w; | |
| 195 | + memset(&w, 0, sizeof(struct winsize)); | |
| 196 | + if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){ | |
| 197 | + tlen = w.ws_col - indent; | |
| 198 | + } | |
| 199 | + } | |
| 200 | +#else | |
| 201 | + if( width<0 ){ | |
| 202 | + /* | |
| 203 | + ** Fallback to using more-or-less the "legacy semantics" of hard-coding | |
| 204 | + ** the maximum line length to a value reasonable for the vast majority | |
| 205 | + ** of supported systems. | |
| 206 | + */ | |
| 207 | + tlen = COMMENT_LEGACY_LINE_LENGTH - indent; | |
| 208 | + } | |
| 209 | +#endif | |
| 210 | + if( zText==0 ) zText = "(NULL)"; | |
| 211 | + if( tlen<=0 ){ | |
| 212 | + tlen = strlen(zText); | |
| 213 | + } | |
| 214 | + if( tlen >= (sizeof(zBuffer)) ){ | |
| 215 | + zBuf = fossil_malloc(tlen+1); | |
| 216 | + }else{ | |
| 217 | + zBuf = zBuffer; | |
| 218 | + } | |
| 219 | + for(;;){ | |
| 220 | + while( fossil_isspace(zText[0]) ){ zText++; } | |
| 221 | + if( zText[0]==0 ){ | |
| 222 | + if( doIndent==0 ){ | |
| 223 | + fossil_print("\n"); | |
| 224 | + lineCnt = 1; | |
| 225 | + } | |
| 226 | + if( zBuf!=zBuffer) fossil_free(zBuf); | |
| 227 | + return lineCnt; | |
| 228 | + } | |
| 229 | + for(sk=si=i=k=0; zText[i] && k<tlen; i++){ | |
| 230 | + char c = zText[i]; | |
| 231 | + if( fossil_isspace(c) ){ | |
| 232 | + si = i; | |
| 233 | + sk = k; | |
| 234 | + if( k==0 || zBuf[k-1]!=' ' ){ | |
| 235 | + zBuf[k++] = ' '; | |
| 236 | + } | |
| 237 | + }else{ | |
| 238 | + zBuf[k] = c; | |
| 239 | + if( c=='-' && k>0 && fossil_isalpha(zBuf[k-1]) ){ | |
| 240 | + si = i+1; | |
| 241 | + sk = k+1; | |
| 242 | + } | |
| 243 | + k++; | |
| 244 | + } | |
| 245 | + } | |
| 246 | + if( doIndent ){ | |
| 247 | + fossil_print("%*s", indent, ""); | |
| 248 | + } | |
| 249 | + doIndent = 1; | |
| 250 | + if( sk>0 && zText[i] ){ | |
| 251 | + zText += si; | |
| 252 | + zBuf[sk] = 0; | |
| 253 | + }else{ | |
| 254 | + zText += i; | |
| 255 | + zBuf[k] = 0; | |
| 256 | + } | |
| 257 | + fossil_print("%s\n", zBuf); | |
| 258 | + lineCnt++; | |
| 259 | + } | |
| 260 | +} | |
| 261 | + | |
| 262 | +/* | |
| 263 | +** Given a comment string, format that string for printing on a TTY. | |
| 264 | +** Assume that the output cursors is indent spaces from the left margin | |
| 265 | +** and that a single line can contain no more than width characters. | |
| 266 | +** Indent all subsequent lines by indent. | |
| 165 | 267 | ** |
| 166 | -** Return the number of newlines that are output. | |
| 268 | +** Returns the number of new lines emitted. | |
| 167 | 269 | */ |
| 168 | 270 | int comment_print( |
| 169 | 271 | const char *zText, /* The comment text to be printed. */ |
| 170 | 272 | int indent, /* Number of spaces to indent each non-initial line. */ |
| 171 | 273 | int width, /* Maximum number of characters per line. */ |
| 172 | 274 | int flags /* Zero or more "COMMENT_PRINT_*" flags, see above. */ |
| 173 | 275 | ){ |
| 174 | 276 | int maxChars = width - indent; |
| 277 | + int legacy = flags & COMMENT_PRINT_LEGACY; | |
| 175 | 278 | int trimSpace = flags & COMMENT_PRINT_TRIM_SPACE; |
| 176 | 279 | int wordBreak = flags & COMMENT_PRINT_WORD_BREAK; |
| 177 | 280 | int lineCnt = 0; |
| 178 | 281 | const char *zLine; |
| 179 | 282 | |
| 283 | + if( legacy ){ | |
| 284 | + return comment_print_legacy(zText, indent, width); | |
| 285 | + } | |
| 180 | 286 | #if defined(_WIN32) |
| 181 | 287 | if( width<0 ){ |
| 182 | 288 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 183 | 289 | memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO)); |
| 184 | 290 | if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){ |
| @@ -233,18 +339,22 @@ | ||
| 233 | 339 | ** Test comment formatting and printing. Use for testing only. |
| 234 | 340 | ** |
| 235 | 341 | ** Options: |
| 236 | 342 | ** --decode Decode the text using the same method used when |
| 237 | 343 | ** handling the value of a C-card from a manifest. |
| 344 | +** --legacy Use the legacy comment printing algorithm. | |
| 238 | 345 | ** --wordbreak Attempt to break lines on word boundaries. |
| 239 | 346 | */ |
| 240 | 347 | void test_comment_format(void){ |
| 241 | 348 | const char *zPrefix; |
| 242 | 349 | char *zText; |
| 243 | 350 | int indent, width; |
| 244 | 351 | int decode = find_option("decode", 0, 0)!=0; |
| 245 | 352 | int flags = COMMENT_PRINT_DEFAULT; |
| 353 | + if( find_option("legacy", 0, 0) ){ | |
| 354 | + flags |= COMMENT_PRINT_LEGACY; | |
| 355 | + } | |
| 246 | 356 | if( find_option("wordbreak", 0, 0) ){ |
| 247 | 357 | flags |= COMMENT_PRINT_WORD_BREAK; |
| 248 | 358 | } |
| 249 | 359 | if( g.argc!=4 && g.argc!=5 ){ |
| 250 | 360 | usage("PREFIX TEXT ?WIDTH?"); |
| 251 | 361 |
| --- src/comformat.c | |
| +++ src/comformat.c | |
| @@ -26,13 +26,14 @@ | |
| 26 | #else |
| 27 | # include <termios.h> |
| 28 | #endif |
| 29 | |
| 30 | #if INTERFACE |
| 31 | #define COMMENT_PRINT_NONE ((u32)0x00000000) /* No flags. */ |
| 32 | #define COMMENT_PRINT_TRIM_SPACE ((u32)0x00000001) /* Trim leading/trailing. */ |
| 33 | #define COMMENT_PRINT_WORD_BREAK ((u32)0x00000002) /* Break lines on words. */ |
| 34 | #define COMMENT_PRINT_DEFAULT (COMMENT_PRINT_TRIM_SPACE) /* Defaults. */ |
| 35 | #endif |
| 36 | |
| 37 | /* |
| 38 | ** This is the previous value used by most external callers when they |
| @@ -156,29 +157,134 @@ | |
| 156 | *pzLine = zLine + index; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | ** Given a comment string zText, format that string for printing |
| 162 | ** on a TTY. Assume that the output cursors is indent spaces from |
| 163 | ** the left margin and that a single line can contain no more than |
| 164 | ** width characters. Indent all subsequent lines by indent. |
| 165 | ** |
| 166 | ** Return the number of newlines that are output. |
| 167 | */ |
| 168 | int comment_print( |
| 169 | const char *zText, /* The comment text to be printed. */ |
| 170 | int indent, /* Number of spaces to indent each non-initial line. */ |
| 171 | int width, /* Maximum number of characters per line. */ |
| 172 | int flags /* Zero or more "COMMENT_PRINT_*" flags, see above. */ |
| 173 | ){ |
| 174 | int maxChars = width - indent; |
| 175 | int trimSpace = flags & COMMENT_PRINT_TRIM_SPACE; |
| 176 | int wordBreak = flags & COMMENT_PRINT_WORD_BREAK; |
| 177 | int lineCnt = 0; |
| 178 | const char *zLine; |
| 179 | |
| 180 | #if defined(_WIN32) |
| 181 | if( width<0 ){ |
| 182 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 183 | memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO)); |
| 184 | if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){ |
| @@ -233,18 +339,22 @@ | |
| 233 | ** Test comment formatting and printing. Use for testing only. |
| 234 | ** |
| 235 | ** Options: |
| 236 | ** --decode Decode the text using the same method used when |
| 237 | ** handling the value of a C-card from a manifest. |
| 238 | ** --wordbreak Attempt to break lines on word boundaries. |
| 239 | */ |
| 240 | void test_comment_format(void){ |
| 241 | const char *zPrefix; |
| 242 | char *zText; |
| 243 | int indent, width; |
| 244 | int decode = find_option("decode", 0, 0)!=0; |
| 245 | int flags = COMMENT_PRINT_DEFAULT; |
| 246 | if( find_option("wordbreak", 0, 0) ){ |
| 247 | flags |= COMMENT_PRINT_WORD_BREAK; |
| 248 | } |
| 249 | if( g.argc!=4 && g.argc!=5 ){ |
| 250 | usage("PREFIX TEXT ?WIDTH?"); |
| 251 |
| --- src/comformat.c | |
| +++ src/comformat.c | |
| @@ -26,13 +26,14 @@ | |
| 26 | #else |
| 27 | # include <termios.h> |
| 28 | #endif |
| 29 | |
| 30 | #if INTERFACE |
| 31 | #define COMMENT_PRINT_NONE ((u32)0x00000000) /* No flags. */ |
| 32 | #define COMMENT_PRINT_LEGACY ((u32)0x00000001) /* Use legacy algorithm. */ |
| 33 | #define COMMENT_PRINT_TRIM_SPACE ((u32)0x00000002) /* Trim leading/trailing. */ |
| 34 | #define COMMENT_PRINT_WORD_BREAK ((u32)0x00000004) /* Break lines on words. */ |
| 35 | #define COMMENT_PRINT_DEFAULT (COMMENT_PRINT_TRIM_SPACE) /* Defaults. */ |
| 36 | #endif |
| 37 | |
| 38 | /* |
| 39 | ** This is the previous value used by most external callers when they |
| @@ -156,29 +157,134 @@ | |
| 157 | *pzLine = zLine + index; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | ** This is the legacy comment printing algorithm. Currently, it is being |
| 163 | ** retained primarily for testing and comparison purposes. |
| 164 | ** |
| 165 | ** Given a comment string, format that string for printing on a TTY. |
| 166 | ** Assume that the output cursors is indent spaces from the left margin |
| 167 | ** and that a single line can contain no more than width characters. |
| 168 | ** Indent all subsequent lines by indent. |
| 169 | ** |
| 170 | ** Returns the number of new lines emitted. |
| 171 | */ |
| 172 | static int comment_print_legacy( |
| 173 | const char *zText, /* The comment text to be printed. */ |
| 174 | int indent, /* Number of spaces to indent each non-initial line. */ |
| 175 | int width /* Maximum number of characters per line. */ |
| 176 | ){ |
| 177 | int tlen = width - indent; |
| 178 | int si, sk, i, k; |
| 179 | int doIndent = 0; |
| 180 | char *zBuf; |
| 181 | char zBuffer[400]; |
| 182 | int lineCnt = 0; |
| 183 | |
| 184 | #if defined(_WIN32) |
| 185 | if( width<0 ){ |
| 186 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 187 | memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO)); |
| 188 | if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){ |
| 189 | tlen = csbi.srWindow.Right - csbi.srWindow.Left - indent; |
| 190 | } |
| 191 | } |
| 192 | #elif defined(TIOCGWINSZ) |
| 193 | if( width<0 ){ |
| 194 | struct winsize w; |
| 195 | memset(&w, 0, sizeof(struct winsize)); |
| 196 | if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){ |
| 197 | tlen = w.ws_col - indent; |
| 198 | } |
| 199 | } |
| 200 | #else |
| 201 | if( width<0 ){ |
| 202 | /* |
| 203 | ** Fallback to using more-or-less the "legacy semantics" of hard-coding |
| 204 | ** the maximum line length to a value reasonable for the vast majority |
| 205 | ** of supported systems. |
| 206 | */ |
| 207 | tlen = COMMENT_LEGACY_LINE_LENGTH - indent; |
| 208 | } |
| 209 | #endif |
| 210 | if( zText==0 ) zText = "(NULL)"; |
| 211 | if( tlen<=0 ){ |
| 212 | tlen = strlen(zText); |
| 213 | } |
| 214 | if( tlen >= (sizeof(zBuffer)) ){ |
| 215 | zBuf = fossil_malloc(tlen+1); |
| 216 | }else{ |
| 217 | zBuf = zBuffer; |
| 218 | } |
| 219 | for(;;){ |
| 220 | while( fossil_isspace(zText[0]) ){ zText++; } |
| 221 | if( zText[0]==0 ){ |
| 222 | if( doIndent==0 ){ |
| 223 | fossil_print("\n"); |
| 224 | lineCnt = 1; |
| 225 | } |
| 226 | if( zBuf!=zBuffer) fossil_free(zBuf); |
| 227 | return lineCnt; |
| 228 | } |
| 229 | for(sk=si=i=k=0; zText[i] && k<tlen; i++){ |
| 230 | char c = zText[i]; |
| 231 | if( fossil_isspace(c) ){ |
| 232 | si = i; |
| 233 | sk = k; |
| 234 | if( k==0 || zBuf[k-1]!=' ' ){ |
| 235 | zBuf[k++] = ' '; |
| 236 | } |
| 237 | }else{ |
| 238 | zBuf[k] = c; |
| 239 | if( c=='-' && k>0 && fossil_isalpha(zBuf[k-1]) ){ |
| 240 | si = i+1; |
| 241 | sk = k+1; |
| 242 | } |
| 243 | k++; |
| 244 | } |
| 245 | } |
| 246 | if( doIndent ){ |
| 247 | fossil_print("%*s", indent, ""); |
| 248 | } |
| 249 | doIndent = 1; |
| 250 | if( sk>0 && zText[i] ){ |
| 251 | zText += si; |
| 252 | zBuf[sk] = 0; |
| 253 | }else{ |
| 254 | zText += i; |
| 255 | zBuf[k] = 0; |
| 256 | } |
| 257 | fossil_print("%s\n", zBuf); |
| 258 | lineCnt++; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | ** Given a comment string, format that string for printing on a TTY. |
| 264 | ** Assume that the output cursors is indent spaces from the left margin |
| 265 | ** and that a single line can contain no more than width characters. |
| 266 | ** Indent all subsequent lines by indent. |
| 267 | ** |
| 268 | ** Returns the number of new lines emitted. |
| 269 | */ |
| 270 | int comment_print( |
| 271 | const char *zText, /* The comment text to be printed. */ |
| 272 | int indent, /* Number of spaces to indent each non-initial line. */ |
| 273 | int width, /* Maximum number of characters per line. */ |
| 274 | int flags /* Zero or more "COMMENT_PRINT_*" flags, see above. */ |
| 275 | ){ |
| 276 | int maxChars = width - indent; |
| 277 | int legacy = flags & COMMENT_PRINT_LEGACY; |
| 278 | int trimSpace = flags & COMMENT_PRINT_TRIM_SPACE; |
| 279 | int wordBreak = flags & COMMENT_PRINT_WORD_BREAK; |
| 280 | int lineCnt = 0; |
| 281 | const char *zLine; |
| 282 | |
| 283 | if( legacy ){ |
| 284 | return comment_print_legacy(zText, indent, width); |
| 285 | } |
| 286 | #if defined(_WIN32) |
| 287 | if( width<0 ){ |
| 288 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 289 | memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO)); |
| 290 | if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){ |
| @@ -233,18 +339,22 @@ | |
| 339 | ** Test comment formatting and printing. Use for testing only. |
| 340 | ** |
| 341 | ** Options: |
| 342 | ** --decode Decode the text using the same method used when |
| 343 | ** handling the value of a C-card from a manifest. |
| 344 | ** --legacy Use the legacy comment printing algorithm. |
| 345 | ** --wordbreak Attempt to break lines on word boundaries. |
| 346 | */ |
| 347 | void test_comment_format(void){ |
| 348 | const char *zPrefix; |
| 349 | char *zText; |
| 350 | int indent, width; |
| 351 | int decode = find_option("decode", 0, 0)!=0; |
| 352 | int flags = COMMENT_PRINT_DEFAULT; |
| 353 | if( find_option("legacy", 0, 0) ){ |
| 354 | flags |= COMMENT_PRINT_LEGACY; |
| 355 | } |
| 356 | if( find_option("wordbreak", 0, 0) ){ |
| 357 | flags |= COMMENT_PRINT_WORD_BREAK; |
| 358 | } |
| 359 | if( g.argc!=4 && g.argc!=5 ){ |
| 360 | usage("PREFIX TEXT ?WIDTH?"); |
| 361 |