Import new gnulib.
This commit is contained in:
parent
93cd84df63
commit
053cfcddf1
255 changed files with 12578 additions and 4948 deletions
|
@ -1,5 +1,5 @@
|
|||
/* Word-wrapping and line-truncating streams
|
||||
Copyright (C) 1997-1999, 2001-2003, 2005, 2009-2010 Free Software
|
||||
Copyright (C) 1997-1999, 2001-2003, 2005, 2009-2013 Free Software
|
||||
Foundation, Inc.
|
||||
This file is part of the GNU C Library.
|
||||
Written by Miles Bader <miles@gnu.ai.mit.edu>.
|
||||
|
@ -17,7 +17,7 @@
|
|||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/* This package emulates glibc `line_wrap_stream' semantics for systems that
|
||||
/* This package emulates glibc 'line_wrap_stream' semantics for systems that
|
||||
don't have that. */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
|
@ -33,6 +33,7 @@
|
|||
|
||||
#include "argp-fmtstream.h"
|
||||
#include "argp-namefrob.h"
|
||||
#include "mbswidth.h"
|
||||
|
||||
#ifndef ARGP_FMTSTREAM_USE_LINEWRAP
|
||||
|
||||
|
@ -118,37 +119,16 @@ weak_alias (__argp_fmtstream_free, argp_fmtstream_free)
|
|||
#endif
|
||||
|
||||
|
||||
size_t
|
||||
__argp_get_display_len (const char *beg, const char *end)
|
||||
/* Return the pointer to the first character that doesn't fit in l columns. */
|
||||
static inline const ptrdiff_t
|
||||
add_width (const char *ptr, const char *end, size_t l)
|
||||
{
|
||||
const char *ptr;
|
||||
size_t r = 0;
|
||||
mbstate_t ps;
|
||||
const char *ptr0 = ptr;
|
||||
|
||||
memset (&ps, 0, sizeof (ps));
|
||||
|
||||
for (ptr = beg; ptr < end && *ptr; )
|
||||
{
|
||||
wchar_t wc;
|
||||
size_t s;
|
||||
|
||||
s = mbrtowc (&wc, ptr, end - ptr, &ps);
|
||||
if (s == (size_t) -1)
|
||||
break;
|
||||
r += wcwidth (wc);
|
||||
ptr += s;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline char *
|
||||
add_length (char *ptr, char *end, size_t l)
|
||||
{
|
||||
mbstate_t ps;
|
||||
|
||||
memset (&ps, 0, sizeof (ps));
|
||||
|
||||
while (ptr < end && *ptr)
|
||||
while (ptr < end)
|
||||
{
|
||||
wchar_t wc;
|
||||
size_t s, k;
|
||||
|
@ -156,13 +136,31 @@ add_length (char *ptr, char *end, size_t l)
|
|||
s = mbrtowc (&wc, ptr, end - ptr, &ps);
|
||||
if (s == (size_t) -1)
|
||||
break;
|
||||
if (s == (size_t) -2)
|
||||
{
|
||||
if (1 >= l)
|
||||
break;
|
||||
l--;
|
||||
ptr++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (wc == '\e' && ptr + 3 < end
|
||||
&& ptr[1] == '[' && (ptr[2] == '0' || ptr[2] == '1')
|
||||
&& ptr[3] == 'm')
|
||||
{
|
||||
ptr += 4;
|
||||
continue;
|
||||
}
|
||||
|
||||
k = wcwidth (wc);
|
||||
|
||||
if (k >= l)
|
||||
break;
|
||||
l -= k;
|
||||
ptr += s;
|
||||
}
|
||||
return ptr;
|
||||
return ptr - ptr0;
|
||||
}
|
||||
|
||||
/* Process FS's buffer so that line wrapping is done from POINT_OFFS to the
|
||||
|
@ -217,15 +215,15 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
|
|||
|
||||
if (!nl)
|
||||
{
|
||||
size_t display_len = __argp_get_display_len (buf, fs->p);
|
||||
size_t display_width = mbsnwidth (buf, fs->p - buf, MBSW_STOP_AT_NUL);
|
||||
/* The buffer ends in a partial line. */
|
||||
|
||||
if (fs->point_col + display_len < fs->rmargin)
|
||||
if (fs->point_col + display_width < fs->rmargin)
|
||||
{
|
||||
/* The remaining buffer text is a partial line and fits
|
||||
within the maximum line width. Advance point for the
|
||||
characters to be written and stop scanning. */
|
||||
fs->point_col += display_len;
|
||||
fs->point_col += display_width;
|
||||
break;
|
||||
}
|
||||
else
|
||||
|
@ -235,8 +233,8 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
|
|||
}
|
||||
else
|
||||
{
|
||||
size_t display_len = __argp_get_display_len (buf, nl);
|
||||
if (display_len < (ssize_t) fs->rmargin)
|
||||
size_t display_width = mbsnwidth (buf, nl - buf, MBSW_STOP_AT_NUL);
|
||||
if (display_width < (ssize_t) fs->rmargin)
|
||||
{
|
||||
/* The buffer contains a full line that fits within the maximum
|
||||
line width. Reset point and scan the next line. */
|
||||
|
@ -280,7 +278,7 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
|
|||
char *p, *nextline;
|
||||
int i;
|
||||
|
||||
p = add_length (buf, fs->p, (r + 1 - fs->point_col));
|
||||
p = buf + add_width (buf, fs->p, (r + 1 - fs->point_col));
|
||||
while (p >= buf && !isblank ((unsigned char) *p))
|
||||
--p;
|
||||
nextline = p + 1; /* This will begin the next line. */
|
||||
|
@ -298,7 +296,7 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
|
|||
{
|
||||
/* A single word that is greater than the maximum line width.
|
||||
Oh well. Put it on an overlong line by itself. */
|
||||
p = add_length (buf, fs->p, (r + 1 - fs->point_col));
|
||||
p = buf + add_width (buf, fs->p, (r + 1 - fs->point_col));
|
||||
/* Find the end of the long word. */
|
||||
if (p < nl)
|
||||
do
|
||||
|
@ -332,7 +330,8 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
|
|||
&& fs->p > nextline)
|
||||
{
|
||||
/* The margin needs more blanks than we removed. */
|
||||
if (__argp_get_display_len (fs->p, fs->end) > fs->wmargin + 1)
|
||||
if (mbsnwidth (fs->p, fs->end - fs->p, MBSW_STOP_AT_NUL)
|
||||
> fs->wmargin + 1)
|
||||
/* Make some space for them. */
|
||||
{
|
||||
size_t mv = fs->p - nextline;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue