︙ | | |
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
|
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 *distUTF8 /* [out] Distance to next space in UTF-8 sequences. */
int *sumWidth /* [out] Summated width of all characters to next space. */
){
int cchUTF8, utf32, wcwidth = 0;
int nextIndex = index + 1;
int nextIndex = index;
int fNonASCII=0;
for(;;){
char c = zLine[nextIndex];
if( (c&0x80)==0x80 ) fNonASCII=1;
if( c==0 || fossil_isspace(c) ){
char_info_utf8(&zLine[nextIndex],&cchUTF8,&utf32);
nextIndex += cchUTF8;
wcwidth += cli_wcwidth(utf32);
if( zLine[nextIndex]==0 || fossil_isspace(zLine[nextIndex]) ){
if( distUTF8 ){
if( fNonASCII!=0 ){
*distUTF8 = strlen_utf8(&zLine[index], nextIndex-index);
}else{
*distUTF8 = nextIndex-index;
*sumWidth = wcwidth;
}
}
return nextIndex;
}
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)
** Count the number of UTF-8 sequences in a string. Incomplete, ill-formed and
** overlong sequences are counted as one sequence. 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).
** 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
** Combining characters and East Asian Wide and Fullwidth characters are counted
** as one, so this function does not calculate the effective "display width".
** 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(
int strlen_utf8(const char *zString, int lengthBytes){
int i; /* Counted bytes. */
int lengthUTF8; /* Counted UTF-8 sequences. */
#if 0
assert( lengthBytes>=0 );
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
for(i=0, lengthUTF8=0; i<lengthBytes; i++, lengthUTF8++){
return;
char c = zString[i];
int cchUTF8=1; /* Code units consumed. */
int maxUTF8=1; /* Expected sequence length. */
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 &&
i<lengthBytes-1 &&
(zString[i+1]&0xc0)==0x80 ){ /* UTF-8 trail byte 10vvvvvv */
cchUTF8++;
i++;
}
}
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). */
);
return lengthUTF8;
#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
344
345
346
347
348
349
350
351
|
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. */
int cchUTF8, maxUTF8; /* Helper variables to count UTF-8 sequences. */
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
|
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
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 distUTF8;
int nextIndex = comment_next_space(zLine, index, &distUTF8);
if( nextIndex<=0 || distUTF8>maxChars ){
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 distUTF8;
int nextIndex = comment_next_space(zLine, index, &distUTF8);
if( nextIndex<=0 || distUTF8>=maxChars ){
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;
/* Skip over UTF-8 sequences, see comment on strlen_utf8() for details. */
cchUTF8=1; /* Code units consumed. */
char_info_utf8(&zLine[index-1],&cchUTF8,&utf32);
maxUTF8=1; /* Expected sequence length. */
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 &&
(zLine[index]&0xc0)==0x80 ){ /* UTF-8 trail byte 10vvvvvv */
cchUTF8++;
zBuf[iBuf++] = zLine[index++];
}
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++];
}
if( cchUTF8>1 ){
int utf32;
decodeUtf8((const unsigned char*)&zLine[index-cchUTF8],&utf32);
useChars += cli_wcwidth(utf32) - 1;
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
481
482
483
484
485
486
487
488
|
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;
int cchUTF8, maxUTF8; /* Helper variables to count UTF-8 sequences. */
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
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. */
/* Skip over UTF-8 sequences, see comment on strlen_utf8() for details. */
cchUTF8=1; /* Code units consumed. */
char_info_utf8(&zText[i],&cchUTF8,&utf32);
maxUTF8=1; /* Expected sequence length. */
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 */
if( maxUTF8>1 ){
zBuf[k++] = c;
if( cchUTF8>1 ){
int wcwidth;
wcwidth = cli_wcwidth(utf32);
while( cchUTF8<maxUTF8 &&
(zText[i+1]&0xc0)==0x80 ){ /* UTF-8 trail byte 10vvvvvv */
cchUTF8++;
if( kc+wcwidth-1>maxChars && maxChars>=wcwidth ){ /* rollback */
kc--;
break;
}
for( i--; cchUTF8>0; cchUTF8-- ){
zBuf[k++] = zText[++i];
}
}
if( cchUTF8>1 ){
int utf32;
decodeUtf8((const unsigned char*)&zText[k-cchUTF8],&utf32);
kc += cli_wcwidth(utf32) - 1;
kc += wcwidth - 1;
}
else if( fossil_isspace(c) ){
si = i;
sk = k;
if( k==0 || zBuf[k-1]!=' ' ){
zBuf[k++] = ' ';
}
|
︙ | | |