+
+ if (*skip) {
+ (*skip) = 0;
+ return(0);
+ } else
+ return((*p->width)(p, c));
+}
+
+size_t
+term_strlen(const struct termp *p, const char *cp)
+{
+ size_t sz, rsz, i;
+ int ssz, skip, uc;
+ const char *seq, *rhs;
+ enum mandoc_esc esc;
+ static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
+ ASCII_BREAK, '\0' };
+
+ /*
+ * Account for escaped sequences within string length
+ * calculations. This follows the logic in term_word() as we
+ * must calculate the width of produced strings.
+ */
+
+ sz = 0;
+ skip = 0;
+ while ('\0' != *cp) {
+ rsz = strcspn(cp, rej);
+ for (i = 0; i < rsz; i++)
+ sz += cond_width(p, *cp++, &skip);
+
+ switch (*cp) {
+ case '\\':
+ cp++;
+ esc = mandoc_escape(&cp, &seq, &ssz);
+ if (ESCAPE_ERROR == esc)
+ continue;
+
+ rhs = NULL;
+
+ switch (esc) {
+ case ESCAPE_UNICODE:
+ uc = mchars_num2uc(seq + 1, ssz - 1);
+ break;
+ case ESCAPE_NUMBERED:
+ uc = mchars_num2char(seq, ssz);
+ if (uc < 0)
+ continue;
+ break;
+ case ESCAPE_SPECIAL:
+ if (p->enc == TERMENC_ASCII) {
+ rhs = mchars_spec2str(p->symtab,
+ seq, ssz, &rsz);
+ if (rhs != NULL)
+ break;
+ } else {
+ uc = mchars_spec2cp(p->symtab,
+ seq, ssz);
+ if (uc > 0)
+ sz += cond_width(p, uc, &skip);
+ }
+ continue;
+ case ESCAPE_SKIPCHAR:
+ skip = 1;
+ continue;
+ case ESCAPE_OVERSTRIKE:
+ rsz = 0;
+ rhs = seq + ssz;
+ while (seq < rhs) {
+ if (*seq == '\\') {
+ mandoc_escape(&seq, NULL, NULL);
+ continue;
+ }
+ i = (*p->width)(p, *seq++);
+ if (rsz < i)
+ rsz = i;
+ }
+ sz += rsz;
+ continue;
+ default:
+ continue;
+ }
+
+ /*
+ * Common handling for Unicode and numbered
+ * character escape sequences.
+ */
+
+ if (rhs == NULL) {
+ if (p->enc == TERMENC_ASCII) {
+ rhs = ascii_uc2str(uc);
+ rsz = strlen(rhs);
+ } else {
+ if ((uc < 0x20 && uc != 0x09) ||
+ (uc > 0x7E && uc < 0xA0))
+ uc = 0xFFFD;
+ sz += cond_width(p, uc, &skip);
+ continue;
+ }
+ }
+
+ if (skip) {
+ skip = 0;
+ break;
+ }
+
+ /*
+ * Common handling for all escape sequences
+ * printing more than one character.
+ */
+
+ for (i = 0; i < rsz; i++)
+ sz += (*p->width)(p, *rhs++);
+ break;
+ case ASCII_NBRSP:
+ sz += cond_width(p, ' ', &skip);
+ cp++;
+ break;
+ case ASCII_HYPH:
+ sz += cond_width(p, '-', &skip);
+ cp++;
+ /* FALLTHROUGH */
+ case ASCII_BREAK:
+ break;
+ default:
+ break;