conterm/libc/fmtquote.c

250 lines
4.6 KiB
C
Raw Permalink Normal View History

2006-01-17 16:22:03 +03:00
#include <u.h>
#include <libc.h>
2005-08-08 16:50:13 +04:00
#include "fmtdef.h"
/*
* How many bytes of output UTF will be produced by quoting (if necessary) this string?
* How many runes? How much of the input will be consumed?
2006-01-17 15:37:52 +03:00
* The parameter q is filled in by __quotesetup.
2005-08-08 16:50:13 +04:00
* The string may be UTF or Runes (s or r).
* Return count does not include NUL.
* Terminate the scan at the first of:
* NUL in input
* count exceeded in input
* count exceeded on output
* *ninp is set to number of input bytes accepted.
* nin may be <0 initially, to avoid checking input by count.
*/
void
2006-01-17 15:37:52 +03:00
__quotesetup(char *s, Rune *r, int nin, int nout, Quoteinfo *q, int sharp, int runesout)
2005-08-08 16:50:13 +04:00
{
int w;
Rune c;
q->quoted = 0;
q->nbytesout = 0;
q->nrunesout = 0;
q->nbytesin = 0;
q->nrunesin = 0;
if(sharp || nin==0 || (s && *s=='\0') || (r && *r=='\0')){
if(nout < 2)
return;
q->quoted = 1;
q->nbytesout = 2;
q->nrunesout = 2;
}
2006-01-17 15:37:52 +03:00
for(; nin!=0; nin--){
2005-08-08 16:50:13 +04:00
if(s)
w = chartorune(&c, s);
else{
c = *r;
w = runelen(c);
}
if(c == '\0')
break;
if(runesout){
if(q->nrunesout+1 > nout)
break;
}else{
if(q->nbytesout+w > nout)
break;
}
2006-05-21 21:02:05 +04:00
if((c <= L' ') || (c == L'\'') || (fmtdoquote!=0 && fmtdoquote(c))){
2005-08-08 16:50:13 +04:00
if(!q->quoted){
if(runesout){
if(1+q->nrunesout+1+1 > nout) /* no room for quotes */
break;
}else{
if(1+q->nbytesout+w+1 > nout) /* no room for quotes */
break;
}
q->nrunesout += 2; /* include quotes */
q->nbytesout += 2; /* include quotes */
q->quoted = 1;
}
if(c == '\'') {
if(runesout){
if(1+q->nrunesout+1 > nout) /* no room for quotes */
break;
}else{
if(1+q->nbytesout+w > nout) /* no room for quotes */
break;
}
q->nbytesout++;
q->nrunesout++; /* quotes reproduce as two characters */
}
}
/* advance input */
if(s)
s += w;
else
r++;
q->nbytesin += w;
q->nrunesin++;
/* advance output */
q->nbytesout += w;
q->nrunesout++;
}
}
static int
qstrfmt(char *sin, Rune *rin, Quoteinfo *q, Fmt *f)
{
Rune r, *rm, *rme;
char *t, *s, *m, *me;
Rune *rt, *rs;
ulong fl;
int nc, w;
m = sin;
me = m + q->nbytesin;
rm = rin;
rme = rm + q->nrunesin;
w = f->width;
fl = f->flags;
if(f->runes){
2006-01-17 15:37:52 +03:00
if(!(fl & FmtLeft) && __rfmtpad(f, w - q->nrunesout) < 0)
2005-08-08 16:50:13 +04:00
return -1;
}else{
2006-01-17 15:37:52 +03:00
if(!(fl & FmtLeft) && __fmtpad(f, w - q->nbytesout) < 0)
2005-08-08 16:50:13 +04:00
return -1;
}
2006-01-17 15:37:52 +03:00
t = (char*)f->to;
s = (char*)f->stop;
rt = (Rune*)f->to;
rs = (Rune*)f->stop;
2005-08-08 16:50:13 +04:00
if(f->runes)
FMTRCHAR(f, rt, rs, '\'');
else
FMTRUNE(f, t, s, '\'');
for(nc = q->nrunesin; nc > 0; nc--){
if(sin){
r = *(uchar*)m;
if(r < Runeself)
m++;
else if((me - m) >= UTFmax || fullrune(m, me-m))
m += chartorune(&r, m);
else
break;
}else{
if(rm >= rme)
break;
r = *(uchar*)rm++;
}
if(f->runes){
FMTRCHAR(f, rt, rs, r);
if(r == '\'')
FMTRCHAR(f, rt, rs, r);
}else{
FMTRUNE(f, t, s, r);
if(r == '\'')
FMTRUNE(f, t, s, r);
}
}
if(f->runes){
FMTRCHAR(f, rt, rs, '\'');
USED(rs);
f->nfmt += rt - (Rune *)f->to;
f->to = rt;
2006-01-17 15:37:52 +03:00
if(fl & FmtLeft && __rfmtpad(f, w - q->nrunesout) < 0)
2005-08-08 16:50:13 +04:00
return -1;
}else{
FMTRUNE(f, t, s, '\'');
USED(s);
f->nfmt += t - (char *)f->to;
f->to = t;
2006-01-17 15:37:52 +03:00
if(fl & FmtLeft && __fmtpad(f, w - q->nbytesout) < 0)
2005-08-08 16:50:13 +04:00
return -1;
}
return 0;
}
int
2006-01-17 15:37:52 +03:00
__quotestrfmt(int runesin, Fmt *f)
2005-08-08 16:50:13 +04:00
{
2006-01-17 15:37:52 +03:00
int nin, outlen;
2005-08-08 16:50:13 +04:00
Rune *r;
char *s;
Quoteinfo q;
2006-01-17 15:37:52 +03:00
nin = -1;
if(f->flags&FmtPrec)
nin = f->prec;
2005-08-08 16:50:13 +04:00
if(runesin){
r = va_arg(f->args, Rune *);
s = nil;
}else{
s = va_arg(f->args, char *);
r = nil;
}
if(!s && !r)
2006-01-17 15:37:52 +03:00
return __fmtcpy(f, (void*)"<nil>", 5, 5);
2005-08-08 16:50:13 +04:00
if(f->flush)
outlen = 0x7FFFFFFF; /* if we can flush, no output limit */
else if(f->runes)
outlen = (Rune*)f->stop - (Rune*)f->to;
else
outlen = (char*)f->stop - (char*)f->to;
2006-01-17 15:37:52 +03:00
__quotesetup(s, r, nin, outlen, &q, f->flags&FmtSharp, f->runes);
2005-08-08 16:50:13 +04:00
//print("bytes in %d bytes out %d runes in %d runesout %d\n", q.nbytesin, q.nbytesout, q.nrunesin, q.nrunesout);
if(runesin){
if(!q.quoted)
2006-01-17 15:37:52 +03:00
return __fmtrcpy(f, r, q.nrunesin);
2005-08-08 16:50:13 +04:00
return qstrfmt(nil, r, &q, f);
}
if(!q.quoted)
2006-01-17 15:37:52 +03:00
return __fmtcpy(f, s, q.nrunesin, q.nbytesin);
2005-08-08 16:50:13 +04:00
return qstrfmt(s, nil, &q, f);
}
int
quotestrfmt(Fmt *f)
{
2006-01-17 15:37:52 +03:00
return __quotestrfmt(0, f);
2005-08-08 16:50:13 +04:00
}
int
quoterunestrfmt(Fmt *f)
{
2006-01-17 15:37:52 +03:00
return __quotestrfmt(1, f);
2005-08-08 16:50:13 +04:00
}
void
quotefmtinstall(void)
{
fmtinstall('q', quotestrfmt);
fmtinstall('Q', quoterunestrfmt);
}
int
2006-01-17 15:37:52 +03:00
__needsquotes(char *s, int *quotelenp)
2005-08-08 16:50:13 +04:00
{
Quoteinfo q;
2006-01-17 15:37:52 +03:00
__quotesetup(s, nil, -1, 0x7FFFFFFF, &q, 0, 0);
2005-08-08 16:50:13 +04:00
*quotelenp = q.nbytesout;
return q.quoted;
}
int
2006-01-17 15:37:52 +03:00
__runeneedsquotes(Rune *r, int *quotelenp)
2005-08-08 16:50:13 +04:00
{
Quoteinfo q;
2006-01-17 15:37:52 +03:00
__quotesetup(nil, r, -1, 0x7FFFFFFF, &q, 0, 0);
2005-08-08 16:50:13 +04:00
*quotelenp = q.nrunesout;
return q.quoted;
}