Utf8.widthindex: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Shared function}} Returns the location, offset and width of the character at the given location in the UTF-8 string. ==Syntax== <syntaxhighlight lang="lua">int, int, int utf8.wid...")
 
m (→‎Syntax: function is withindex)
 
(One intermediate revision by one other user not shown)
Line 5: Line 5:


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">int, int, int utf8.width ( string input, int location [, bool ambi_is_double = false, int default_width = 0 ] )</syntaxhighlight>
<syntaxhighlight lang="lua">int, int, int utf8.widthindex ( string input, int location [, bool ambi_is_double = false, int default_width = 0 ] )</syntaxhighlight>


===Required Arguments===
===Required Arguments===
Line 13: Line 13:
{{OptionalArg}}
{{OptionalArg}}
*'''ambi_is_double:''' A boolean, if set to ''true'', ambiguous character's width is 2 (see example).
*'''ambi_is_double:''' A boolean, if set to ''true'', ambiguous character's width is 2 (see example).
*'''default_width:''' A number, if given, is used as width for unprintable characters.
*'''default_width:''' An integer, if given, is used as width for unprintable characters.


===Returns===
===Returns===
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 ''number'' will be returned.
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.


==Example==
==Example==

Latest revision as of 14:27, 17 February 2021

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

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • ambi_is_double: A boolean, if set to true, ambiguous character's width is 2 (see example).
  • default_width: An integer, if given, is used as width for unprintable characters.

Returns

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.

Example

Click to collapse [-]
Server

This example

local input = "днём"
local raw_width = utf8.width( input, true )

for location = 1, raw_width do
    print( utf8.widthindex( input, location, true ) )
end

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

See Also