Compute the display widths of the elements of a character object.
utf8_width(x, ..., encode = TRUE, quote = FALSE, utf8 = NULL)
character object.
These dots are for future extensions and must be empty.
whether to encode the object before measuring its width.
whether to quote the object before measuring its width.
logical scalar indicating whether to determine widths assuming a
UTF-8 capable display (ASCII-only otherwise), or NULL
to format for
output capabilities as determined by output_utf8()
.
An integer object, with the same names
, dim
, and
dimnames
as x
.
utf8_width()
returns the printed widths of the elements of a character
object on a UTF-8 device (or on an ASCII device when output_utf8()
is
FALSE
), when printed with utf8_print()
. If the string is not
printable on the device, for example if it contains a control code like
"\n"
, then the result is NA
. If encode = TRUE
, the
default, then the function returns the widths of the encoded elements via
utf8_encode()
); otherwise, the function returns the widths of the
original elements.
# the second element is encoded in latin-1, but declared as UTF-8
x <- c("fa\u00E7ile", "fa\xE7ile", "fa\xC3\xA7ile")
Encoding(x) <- c("UTF-8", "UTF-8", "bytes")
# get widths
utf8_width(x)
#> [1] 6 9 13
utf8_width(x, encode = FALSE)
#> [1] 6 NA NA
utf8_width('"')
#> [1] 1
utf8_width('"', quote = TRUE)
#> [1] 4