fmtquote.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "fmtdef.h"
  4. /*
  5. * How many bytes of output UTF will be produced by quoting (if necessary) this string?
  6. * How many runes? How much of the input will be consumed?
  7. * The parameter q is filled in by _quotesetup.
  8. * The string may be UTF or Runes (s or r).
  9. * Return count does not include NUL.
  10. * Terminate the scan at the first of:
  11. * NUL in input
  12. * count exceeded in input
  13. * count exceeded on output
  14. * *ninp is set to number of input bytes accepted.
  15. * nin may be <0 initially, to avoid checking input by count.
  16. */
  17. void
  18. _quotesetup(char *s, Rune *r, int nin, int nout, Quoteinfo *q, int sharp, int runesout)
  19. {
  20. int w;
  21. Rune c;
  22. q->quoted = 0;
  23. q->nbytesout = 0;
  24. q->nrunesout = 0;
  25. q->nbytesin = 0;
  26. q->nrunesin = 0;
  27. if(sharp || nin==0 || (s && *s=='\0') || (r && *r=='\0')){
  28. if(nout < 2)
  29. return;
  30. q->quoted = 1;
  31. q->nbytesout = 2;
  32. q->nrunesout = 2;
  33. }
  34. for(; nin!=0; nin--){
  35. if(s)
  36. w = chartorune(&c, s);
  37. else{
  38. c = *r;
  39. w = runelen(c);
  40. }
  41. if(c == '\0')
  42. break;
  43. if(runesout){
  44. if(q->nrunesout+1 > nout)
  45. break;
  46. }else{
  47. if(q->nbytesout+w > nout)
  48. break;
  49. }
  50. if((c <= L' ') || (c == L'\'') || (doquote!=nil && doquote(c))){
  51. if(!q->quoted){
  52. if(runesout){
  53. if(1+q->nrunesout+1+1 > nout) /* no room for quotes */
  54. break;
  55. }else{
  56. if(1+q->nbytesout+w+1 > nout) /* no room for quotes */
  57. break;
  58. }
  59. q->nrunesout += 2; /* include quotes */
  60. q->nbytesout += 2; /* include quotes */
  61. q->quoted = 1;
  62. }
  63. if(c == '\'') {
  64. if(runesout){
  65. if(1+q->nrunesout+1 > nout) /* no room for quotes */
  66. break;
  67. }else{
  68. if(1+q->nbytesout+w > nout) /* no room for quotes */
  69. break;
  70. }
  71. q->nbytesout++;
  72. q->nrunesout++; /* quotes reproduce as two characters */
  73. }
  74. }
  75. /* advance input */
  76. if(s)
  77. s += w;
  78. else
  79. r++;
  80. q->nbytesin += w;
  81. q->nrunesin++;
  82. /* advance output */
  83. q->nbytesout += w;
  84. q->nrunesout++;
  85. }
  86. }
  87. static int
  88. qstrfmt(char *sin, Rune *rin, Quoteinfo *q, Fmt *f)
  89. {
  90. Rune r, *rm, *rme;
  91. char *t, *s, *m, *me;
  92. Rune *rt, *rs;
  93. ulong fl;
  94. int nc, w;
  95. m = sin;
  96. me = m + q->nbytesin;
  97. rm = rin;
  98. rme = rm + q->nrunesin;
  99. w = f->width;
  100. fl = f->flags;
  101. if(f->runes){
  102. if(!(fl & FmtLeft) && _rfmtpad(f, w - q->nrunesout) < 0)
  103. return -1;
  104. }else{
  105. if(!(fl & FmtLeft) && _fmtpad(f, w - q->nbytesout) < 0)
  106. return -1;
  107. }
  108. t = f->to;
  109. s = f->stop;
  110. rt = f->to;
  111. rs = f->stop;
  112. if(f->runes)
  113. FMTRCHAR(f, rt, rs, '\'');
  114. else
  115. FMTRUNE(f, t, s, '\'');
  116. for(nc = q->nrunesin; nc > 0; nc--){
  117. if(sin){
  118. r = *(uchar*)m;
  119. if(r < Runeself)
  120. m++;
  121. else if((me - m) >= UTFmax || fullrune(m, me-m))
  122. m += chartorune(&r, m);
  123. else
  124. break;
  125. }else{
  126. if(rm >= rme)
  127. break;
  128. r = *(uchar*)rm++;
  129. }
  130. if(f->runes){
  131. FMTRCHAR(f, rt, rs, r);
  132. if(r == '\'')
  133. FMTRCHAR(f, rt, rs, r);
  134. }else{
  135. FMTRUNE(f, t, s, r);
  136. if(r == '\'')
  137. FMTRUNE(f, t, s, r);
  138. }
  139. }
  140. if(f->runes){
  141. FMTRCHAR(f, rt, rs, '\'');
  142. USED(rs);
  143. f->nfmt += rt - (Rune *)f->to;
  144. f->to = rt;
  145. if(fl & FmtLeft && _rfmtpad(f, w - q->nrunesout) < 0)
  146. return -1;
  147. }else{
  148. FMTRUNE(f, t, s, '\'');
  149. USED(s);
  150. f->nfmt += t - (char *)f->to;
  151. f->to = t;
  152. if(fl & FmtLeft && _fmtpad(f, w - q->nbytesout) < 0)
  153. return -1;
  154. }
  155. return 0;
  156. }
  157. int
  158. _quotestrfmt(int runesin, Fmt *f)
  159. {
  160. int nin, outlen;
  161. Rune *r;
  162. char *s;
  163. Quoteinfo q;
  164. nin = -1;
  165. if(f->flags&FmtPrec)
  166. nin = f->prec;
  167. if(runesin){
  168. r = va_arg(f->args, Rune *);
  169. s = nil;
  170. }else{
  171. s = va_arg(f->args, char *);
  172. r = nil;
  173. }
  174. if(!s && !r)
  175. return _fmtcpy(f, "<nil>", 5, 5);
  176. if(f->flush)
  177. outlen = 0x7FFFFFFF; /* if we can flush, no output limit */
  178. else if(f->runes)
  179. outlen = (Rune*)f->stop - (Rune*)f->to;
  180. else
  181. outlen = (char*)f->stop - (char*)f->to;
  182. _quotesetup(s, r, nin, outlen, &q, f->flags&FmtSharp, f->runes);
  183. //print("bytes in %d bytes out %d runes in %d runesout %d\n", q.nbytesin, q.nbytesout, q.nrunesin, q.nrunesout);
  184. if(runesin){
  185. if(!q.quoted)
  186. return _fmtrcpy(f, r, q.nrunesin);
  187. return qstrfmt(nil, r, &q, f);
  188. }
  189. if(!q.quoted)
  190. return _fmtcpy(f, s, q.nrunesin, q.nbytesin);
  191. return qstrfmt(s, nil, &q, f);
  192. }
  193. int
  194. quotestrfmt(Fmt *f)
  195. {
  196. return _quotestrfmt(0, f);
  197. }
  198. int
  199. quoterunestrfmt(Fmt *f)
  200. {
  201. return _quotestrfmt(1, f);
  202. }
  203. void
  204. quotefmtinstall(void)
  205. {
  206. fmtinstall('q', quotestrfmt);
  207. fmtinstall('Q', quoterunestrfmt);
  208. }
  209. int
  210. _needsquotes(char *s, int *quotelenp)
  211. {
  212. Quoteinfo q;
  213. _quotesetup(s, nil, -1, 0x7FFFFFFF, &q, 0, 0);
  214. *quotelenp = q.nbytesout;
  215. return q.quoted;
  216. }
  217. int
  218. _runeneedsquotes(Rune *r, int *quotelenp)
  219. {
  220. Quoteinfo q;
  221. _quotesetup(nil, r, -1, 0x7FFFFFFF, &q, 0, 0);
  222. *quotelenp = q.nrunesout;
  223. return q.quoted;
  224. }