Difference between revisions of "Utf8.offset"

From GiderosMobile
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
'''Available since:''' Gideros 2016.06<br/>
 
'''Available since:''' Gideros 2016.06<br/>
 +
'''Class:''' [[utf8]]<br/>
 +
 
=== Description ===
 
=== Description ===
<translate>Returns the position (in bytes) where the encoding of the n-th character of s (counting from position i) starts. A negative n gets characters before position i. The default for i is 1 when n is non-negative and #s + 1 otherwise, so that utf8.offset(s, -n) gets the offset of the n-th character from the end of the string. If the specified character is neither in the subject nor right after its end, the function returns nil.
+
Returns the position (in bytes) where the encoding of the n-th character of ''s'' (counting from position ''i'') starts.
As a special case, when n is 0 the function returns the start of the encoding of the character that contains the i-th byte of s.
+
<syntaxhighlight lang="lua">
This function assumes that s is a valid UTF-8 string.</translate>
 
<source lang="lua">
 
 
(number) = utf8.offset(s,n,i)
 
(number) = utf8.offset(s,n,i)
</source>
+
</syntaxhighlight>
 +
 
 +
A negative ''n'' gets characters before position ''i''.
 +
 
 +
The default for ''i'' is 1 when ''n'' is non-negative and #s + 1 otherwise, so that utf8.offset(s, -n) gets the offset of the n-th character from the end of the string.
 +
 
 +
If the specified character is neither in the subject nor right after its end, the function returns nil.
 +
 
 +
As a special case, when ''n'' is 0 the function returns the start of the encoding of the character that contains the i-th byte of s.
 +
 
 +
'''This function assumes that ''s'' is a valid UTF-8 string'''
 +
 
 
=== Parameters ===
 
=== Parameters ===
'''s''': (string) <translate></translate> <br/>
+
'''s''': (string) <br/>
'''n''': (number) <translate></translate> <br/>
+
'''n''': (number) <br/>
'''i''': (number) <translate></translate> '''optional'''<br/>
+
'''i''': (number) '''optional'''<br/>
 +
 
 
=== Return values ===
 
=== Return values ===
'''Returns''' (number) <translate>position (in bytes)</translate><br/>
+
'''Returns''' (number) position (in bytes)<br/>
 +
 
 +
{{Utf8}}

Latest revision as of 22:11, 1 December 2023

Available since: Gideros 2016.06
Class: utf8

Description

Returns the position (in bytes) where the encoding of the n-th character of s (counting from position i) starts.

(number) = utf8.offset(s,n,i)

A negative n gets characters before position i.

The default for i is 1 when n is non-negative and #s + 1 otherwise, so that utf8.offset(s, -n) gets the offset of the n-th character from the end of the string.

If the specified character is neither in the subject nor right after its end, the function returns nil.

As a special case, when n is 0 the function returns the start of the encoding of the character that contains the i-th byte of s.

This function assumes that s is a valid UTF-8 string

Parameters

s: (string)
n: (number)
i: (number) optional

Return values

Returns (number) position (in bytes)