Skip to contents

The three primitives listing_wrap() uses to lay text out, exported so that a rule of your own – see listing_wrap_code() – shares them rather than carrying a copy. They are useful on their own too: a listing's arithmetic is done in DISPLAY widths, where a full-width (CJK) glyph counts as two, and nchar() alone would let a Japanese column ask for 20 columns and take up to 40.

Usage

listing_disp_width(x)

listing_take(x, width)

listing_split_after(text, sep)

Arguments

x, text

Text to measure, cut or split. listing_disp_width() is vectorised; the other two take one string.

width

Maximum display width.

sep

The separator to break after. "" or NULL returns text unsplit.

Value

listing_disp_width() an integer vector; listing_take() a single string; listing_split_after() a character vector of pieces.

Details

listing_disp_width() measures. listing_take() cuts: the longest prefix that still fits, never "" for non-empty text, so a caller looping on the remainder always makes progress even where one glyph is wider than the whole column. listing_split_after() breaks after each separator and keeps it at the end of the piece it closes, which is the look a stacked listing column is expected to have.

See also

listing_wrap(), the rule these build; listing_wrap_code() to fork that rule.

Examples

listing_disp_width(c("ABC", "\u3042\u3044\u3046"))   # 3 and 6 (three wide characters)
#> [1] 3 6
listing_take("HYPERGLYCAEMIA", 6)
#> [1] "HYPERG"
listing_split_after("COMPLETED/UPPER", "/")
#> [1] "COMPLETED/" "UPPER"