** z[] is a line of text that is to be displayed the .mode box or table or ** similar tabular formats. z[] might contain control characters such ** as \n, \t, \f, or \r. ** ** Compute characters to display on the first line of z[]. Stop at the ** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained ** from malloc()) of that first line, which caller should free sometime. ** Write
| 18637 | ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.) |
| 18638 | */ |
| 18639 | static char *translateForDisplayAndDup( |
| 18640 | const unsigned char *z, /* Input text to be transformed */ |
| 18641 | const unsigned char **pzTail, /* OUT: Tail of the input for next line */ |
| 18642 | int mxWidth, /* Max width. 0 means no limit */ |
| 18643 | u8 bWordWrap /* If true, avoid breaking mid-word */ |
| 18644 | ){ |
| 18645 | int i; /* Input bytes consumed */ |
| 18646 | int j; /* Output bytes generated */ |
| 18647 | int k; /* Input bytes to be displayed */ |
| 18648 | int n; /* Output column number */ |
| 18649 | unsigned char *zOut; /* Output text */ |
| 18650 | |
| 18651 | if( z==0 ){ |
| 18652 | *pzTail = 0; |
| 18653 | return 0; |
| 18654 | } |
| 18655 | if( mxWidth<0 ) mxWidth = -mxWidth; |
| 18656 | if( mxWidth==0 ) mxWidth = 1000000; |
| 18657 | i = j = n = 0; |
| 18658 | while( n<mxWidth ){ |
| 18659 | if( z[i]>=' ' ){ |
| 18660 | n++; |
| 18661 | do{ i++; j++; }while( (z[i]&0xc0)==0x80 ); |
| 18662 | continue; |
| 18663 | } |
| 18664 | if( z[i]=='\t' ){ |
| 18665 | do{ |
| 18666 | n++; |
| 18667 | j++; |
| 18668 | }while( (n&7)!=0 && n<mxWidth ); |
| 18669 | i++; |
| 18670 | continue; |
| 18671 | } |
| 18672 | break; |
| 18673 | } |
| 18674 | if( n>=mxWidth && bWordWrap ){ |
| 18675 | /* Perhaps try to back up to a better place to break the line */ |
| 18676 | for(k=i; k>i/2; k--){ |
| 18677 | if( isspace(z[k-1]) ) break; |
| 18678 | } |
| 18679 | if( k<=i/2 ){ |
| 18680 | for(k=i; k>i/2; k--){ |
| 18681 | if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break; |
| 18682 | } |
| 18683 | } |
| 18684 | if( k<=i/2 ){ |
| 18685 | k = i; |
| 18686 | }else{ |
| 18687 | i = k; |
| 18688 | while( z[i]==' ' ) i++; |
| 18689 | } |
| 18690 | }else{ |
| 18691 | k = i; |
| 18692 | } |
| 18693 | if( n>=mxWidth && z[i]>=' ' ){ |
| 18694 | *pzTail = &z[i]; |
| 18695 | }else if( z[i]=='\r' && z[i+1]=='\n' ){ |
| 18696 | *pzTail = z[i+2] ? &z[i+2] : 0; |
no test coverage detected