Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| Comment: | Fix the off-by-one errors if a fullwidth character only fits partially, and take into account character widths when scanning forward to find the distance to the next space. |
|---|---|
| Downloads: | Tarball | ZIP archive |
| Timelines: | family | ancestors | descendants | both | comment-formatter-wcwidth |
| Files: | files | file ages | folders |
| SHA3-256: |
d5479ba7c66e74561ee43db687b3ce19 |
| User & Date: | florian 2024-10-02 14:43:00.000 |
Context
|
2024-10-02
| ||
| 14:45 | Have the `test-comment-format' command call `verify_all_options()' to report unknown command-line options. ... (check-in: fb0845d1aa user: florian tags: comment-formatter-wcwidth) | |
| 14:43 | Fix the off-by-one errors if a fullwidth character only fits partially, and take into account character widths when scanning forward to find the distance to the next space. ... (check-in: d5479ba7c6 user: florian tags: comment-formatter-wcwidth) | |
|
2024-09-30
| ||
| 18:21 | Fix the unicode code-point width estimating function to align with the SQLite CLI. ... (check-in: e483b3b15f user: drh tags: trunk) | |
Changes
Changes to src/comformat.c.
| ︙ | ︙ | |||
239 240 241 242 243 244 245 | ** initial index and returns the index of the next spacing character -OR- ** zero if such a character cannot be found. For the purposes of this ** algorithm, the NUL character is treated the same as a spacing character. */ static int comment_next_space( const char *zLine, /* [in] The comment line being printed. */ int index, /* [in] The current character index being handled. */ | | > | < | | > | < < < < | < < < > > | | | | | < < > > > | > > > | > > > > > > > > > > > > | > > | | | < < < < < < < < < < < | > > > > > > > > > > > > > > > > > > > > > > > > > > > | | 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
** initial index and returns the index of the next spacing character -OR-
** zero if such a character cannot be found. For the purposes of this
** algorithm, the NUL character is treated the same as a spacing character.
*/
static int comment_next_space(
const char *zLine, /* [in] The comment line being printed. */
int index, /* [in] The current character index being handled. */
int *sumWidth /* [out] Summated width of all characters to next space. */
){
int cchUTF8, utf32, wcwidth = 0;
int nextIndex = index;
for(;;){
char_info_utf8(&zLine[nextIndex],&cchUTF8,&utf32);
nextIndex += cchUTF8;
wcwidth += cli_wcwidth(utf32);
if( zLine[nextIndex]==0 || fossil_isspace(zLine[nextIndex]) ){
*sumWidth = wcwidth;
return nextIndex;
}
}
return 0; /* NOT REACHED */
}
/*
** Return information about the next (single- or multi-byte) character in the
** specified UTF-8 string: The number of UTF-8 code units (in this case: bytes)
** and the decoded UTF-32 code point. Incomplete, ill-formed and overlong
** sequences are consumed together as one invalid code point. The invalid lead
** bytes 0xC0 to 0xC1 and 0xF5 to 0xF7 are allowed to initiate (ill-formed) 2-
** and 4-byte sequences, respectively, the other invalid lead bytes 0xF8 to 0xFF
** are treated as invalid 1-byte sequences (as lone trail bytes), all resulting
** in one invalid code point. Invalid UTF-8 sequences encoding a non-scalar code
** point (UTF-16 surrogates U+D800 to U+DFFF) are allowed.
*/
void char_info_utf8(
const unsigned char *z,
int *pCchUTF8,
int *pUtf32
){
int i = 0; /* Counted bytes. */
int cchUTF8 = 1; /* Code units consumed. */
int maxUTF8 = 1; /* Expected sequence length. */
char c = z[i++];
if( (c&0xe0)==0xc0 ) maxUTF8 = 2; /* UTF-8 lead byte 110vvvvv */
else if( (c&0xf0)==0xe0 ) maxUTF8 = 3; /* UTF-8 lead byte 1110vvvv */
else if( (c&0xf8)==0xf0 ) maxUTF8 = 4; /* UTF-8 lead byte 11110vvv */
while( cchUTF8<maxUTF8 &&
(z[i]&0xc0)==0x80 ){ /* UTF-8 trail byte 10vvvvvv */
cchUTF8++;
i++;
}
*pCchUTF8 = cchUTF8;
if( cchUTF8!=maxUTF8 || /* Incomplete UTF-8 sequence. */
cchUTF8==1 && (c&0x80)==0x80 ){ /* Lone UTF-8 trail byte. */
*pUtf32 = 0xfffd; /* U+FFFD Replacement Character */
#ifdef FOSSIL_DEBUG
assert( *pUtf32!=0xfffd ); /* Invalid UTF-8 sequence. */
#endif
return;
}
switch( cchUTF8 ){
case 4:
*pUtf32 =
( (z[0] & 0x0f)<<18 ) |
( (z[1] & 0x3f)<<12 ) |
( (z[2] & 0x3f)<< 6 ) |
( (z[4] & 0x3f)<< 0 ) ;
break;
case 3:
*pUtf32 =
( (z[0] & 0x0f)<<12 ) |
( (z[1] & 0x3f)<< 6 ) |
( (z[2] & 0x3f)<< 0 ) ;
break;
case 2:
*pUtf32 =
( (z[0] & 0x1f)<< 6 ) |
( (z[1] & 0x3f)<< 0 ) ;
break;
case 1:
*pUtf32 = (int)z[0];
break;
}
#ifdef FOSSIL_DEBUG
assert(
*pUtf32>=0 && *pUtf32<=0x10ffff && /* Valid range U+0000 to U+10FFFF. */
*pUtf32<0xd800 && *pUtf32>0xdfff /* Non-scalar (UTF-16 surrogates). */
);
#endif
}
/*
** This function is called when printing a logical comment line to calculate
** the necessary indenting. The caller needs to emit the indenting spaces.
*/
static void comment_calc_indent(
|
| ︙ | ︙ | |||
337 338 339 340 341 342 343 |
int wordBreak, /* [in] Non-zero to try breaking on word boundaries. */
int origBreak, /* [in] Non-zero to break before original comment. */
int *pLineCnt, /* [in/out] Pointer to the total line count. */
const char **pzLine /* [out] Pointer to the end of the logical line. */
){
int index = 0, charCnt = 0, lineCnt = 0, maxChars, i;
char zBuf[400]; int iBuf=0; /* Output buffer and counter. */
| < | 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
int wordBreak, /* [in] Non-zero to try breaking on word boundaries. */
int origBreak, /* [in] Non-zero to break before original comment. */
int *pLineCnt, /* [in/out] Pointer to the total line count. */
const char **pzLine /* [out] Pointer to the end of the logical line. */
){
int index = 0, charCnt = 0, lineCnt = 0, maxChars, i;
char zBuf[400]; int iBuf=0; /* Output buffer and counter. */
if( !zLine ) return;
if( lineChars<=0 ) return;
#if 0
assert( indent<sizeof(zBuf)-5 ); /* See following comments to explain */
assert( origIndent<sizeof(zBuf)-5 ); /* these limits. */
#endif
if( indent>(int)sizeof(zBuf)-6 ){
|
| ︙ | ︙ | |||
360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
}
if( origIndent>(int)sizeof(zBuf)-6 ){
/* Limit line indent to fit output buffer. */
origIndent = sizeof(zBuf)-6;
}
maxChars = lineChars;
for(;;){
int useChars = 1;
char c = zLine[index];
/* Flush the output buffer if there's no space left for at least one more
** (potentially 4-byte) UTF-8 sequence, one level of indentation spaces,
** a new line, and a terminating NULL. */
if( iBuf>(int)sizeof(zBuf)-origIndent-6 ){
zBuf[iBuf]=0;
| > | 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
}
if( origIndent>(int)sizeof(zBuf)-6 ){
/* Limit line indent to fit output buffer. */
origIndent = sizeof(zBuf)-6;
}
maxChars = lineChars;
for(;;){
int cchUTF8, utf32;
int useChars = 1;
char c = zLine[index];
/* Flush the output buffer if there's no space left for at least one more
** (potentially 4-byte) UTF-8 sequence, one level of indentation spaces,
** a new line, and a terminating NULL. */
if( iBuf>(int)sizeof(zBuf)-origIndent-6 ){
zBuf[iBuf]=0;
|
| ︙ | ︙ | |||
391 392 393 394 395 396 397 |
index++;
}
if( c=='\n' ){
lineCnt++;
charCnt = 0;
useChars = 0;
}else if( c=='\t' ){
| | | | | | | < | < < < < | > > > | > > > > | | | < < < | | 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
index++;
}
if( c=='\n' ){
lineCnt++;
charCnt = 0;
useChars = 0;
}else if( c=='\t' ){
int sumWidth;
int nextIndex = comment_next_space(zLine, index, &sumWidth);
if( nextIndex<=0 || sumWidth>maxChars ){
break;
}
charCnt++;
useChars = COMMENT_TAB_WIDTH;
if( maxChars<useChars ){
zBuf[iBuf++] = ' ';
break;
}
}else if( wordBreak && fossil_isspace(c) ){
int sumWidth;
int nextIndex = comment_next_space(zLine, index, &sumWidth);
if( nextIndex<=0 || sumWidth>=maxChars ){
break;
}
charCnt++;
}else{
charCnt++;
}
assert( c!='\n' || charCnt==0 );
zBuf[iBuf++] = c;
char_info_utf8(&zLine[index-1],&cchUTF8,&utf32);
if( cchUTF8>1 ){
int wcwidth;
wcwidth = cli_wcwidth(utf32);
if( wcwidth>maxChars && lineChars>=wcwidth ){ /* rollback */
index--;
iBuf--;
zBuf[iBuf] = 0;
break;
}
for( ; cchUTF8>1; cchUTF8-- ){
zBuf[iBuf++] = zLine[index++];
}
useChars += wcwidth - 1;
}
maxChars -= useChars;
if( maxChars<=0 ) break;
if( c=='\n' ) break;
}
if( charCnt>0 ){
zBuf[iBuf++] = '\n';
|
| ︙ | ︙ | |||
474 475 476 477 478 479 480 |
){
int maxChars = width - indent;
int si, sk, i, k, kc;
int doIndent = 0;
char *zBuf;
char zBuffer[400];
int lineCnt = 0;
| < | 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
){
int maxChars = width - indent;
int si, sk, i, k, kc;
int doIndent = 0;
char *zBuf;
char zBuffer[400];
int lineCnt = 0;
if( width<0 ){
comment_set_maxchars(indent, &maxChars);
}
if( zText==0 ) zText = "(NULL)";
if( maxChars<=0 ){
maxChars = strlen(zText);
|
| ︙ | ︙ | |||
500 501 502 503 504 505 506 507 508 |
fossil_print("\n");
lineCnt = 1;
}
if( zBuf!=zBuffer) fossil_free(zBuf);
return lineCnt;
}
for(sk=si=i=k=kc=0; zText[i] && kc<maxChars; i++){
char c = zText[i];
kc++; /* Count complete UTF-8 sequences. */
| > < | < < < < | > | < < > > > > | < < < < | | 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
fossil_print("\n");
lineCnt = 1;
}
if( zBuf!=zBuffer) fossil_free(zBuf);
return lineCnt;
}
for(sk=si=i=k=kc=0; zText[i] && kc<maxChars; i++){
int cchUTF8, utf32;
char c = zText[i];
kc++; /* Count complete UTF-8 sequences. */
char_info_utf8(&zText[i],&cchUTF8,&utf32);
if( cchUTF8>1 ){
int wcwidth;
wcwidth = cli_wcwidth(utf32);
if( kc+wcwidth-1>maxChars && maxChars>=wcwidth ){ /* rollback */
kc--;
break;
}
for( i--; cchUTF8>0; cchUTF8-- ){
zBuf[k++] = zText[++i];
}
kc += wcwidth - 1;
}
else if( fossil_isspace(c) ){
si = i;
sk = k;
if( k==0 || zBuf[k-1]!=' ' ){
zBuf[k++] = ' ';
}
|
| ︙ | ︙ |