utf8.widthindex
Client-side
Server-side
Shared
Returns the location, offset and width of the character at the given location in the UTF-8 string.
Syntax
int, int, int utf8.widthindex ( string input, int location, [ bool ambi_is_double = false, int default_width = 0 ] )Required Arguments
- input: A string character sequence.
- location: The target display width (in monospace columns). The function walks through the input string, accumulating each character's display width, until the total width reaches or exceeds this value. It then returns the index of the character at which this happens.
Optional Arguments
NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use.
- ambi_is_double (default: false): A boolean, if set to true , ambiguous character's width is 2 (see example).
- default_width (default: 0): An integer, if given, is used as width for unprintable characters.
Returns
- int: location
- int: offset
- int: width
Returns the given location, the offset in UTF-8 encoding (if cursor is in the middle of the wide char - offset will be 2) and the width of the character, otherwise only the location as integer will be returned.
Code Examples
shared
This example output (enhanced, not raw):
| Character | Location | Offset | Width |
|---|---|---|---|
| д | 1 | 1 | 2 |
| д | 1 | 2 | 2 |
| н | 2 | 1 | 2 |
| н | 2 | 2 | 2 |
| ё | 3 | 1 | 2 |
| ё | 3 | 2 | 2 |
| м | 4 | 1 | 2 |
| м | 4 | 2 | 2 |
local input = "днём"local raw_width = utf8.width(input, true)
for location = 1, raw_width do print(utf8.widthindex(input, location, true))end