Difference between revisions of "utf8"
m |
m |
||
(10 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
{{newin|[[0.9.2]]|092|type=module}} | {{newin|[[0.9.2]]|092|type=module}} | ||
− | This library provides basic support for UTF-8 | + | This library provides basic support for handling UTF-8 encoded strings. |
− | + | It provides all its functions inside the table returned by <code>require("utf8")</code>. This library does not provide any support for Unicode other than handling UTF-8 encoding. Any operation that needs the meaning of a character, such as character classification, is outside its scope. | |
+ | |||
+ | For detailed usage, see the [https://www.lua.org/manual/5.3/manual.html#6.5 reference manual]. | ||
+ | |||
+ | {{notice|The utf8.char function does not work correctly in [[0.9.2]]; However, it is not an issue since [[0.10.0]]}} | ||
+ | |||
+ | |||
+ | == Examples == | ||
+ | Print text the user writes, and erase it using the UTF-8 module. | ||
<source lang="lua"> | <source lang="lua"> | ||
local utf8 = require("utf8") | local utf8 = require("utf8") | ||
+ | |||
+ | function love.load() | ||
+ | text = "Type away! -- " | ||
+ | |||
+ | -- enable key repeat so backspace can be held down to trigger love.keypressed multiple times. | ||
+ | love.keyboard.setKeyRepeat(true) | ||
+ | end | ||
+ | |||
+ | function love.textinput(t) | ||
+ | text = text .. t | ||
+ | end | ||
+ | |||
+ | function love.keypressed(key) | ||
+ | if key == "backspace" then | ||
+ | -- get the byte offset to the last UTF-8 character in the string. | ||
+ | local byteoffset = utf8.offset(text, -1) | ||
+ | |||
+ | if byteoffset then | ||
+ | -- remove the last UTF-8 character. | ||
+ | -- string.sub operates on bytes rather than UTF-8 characters, so we couldn't do string.sub(text, 1, -2). | ||
+ | text = string.sub(text, 1, byteoffset - 1) | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function love.draw() | ||
+ | love.graphics.printf(text, 0, 0, love.graphics.getWidth()) | ||
+ | end | ||
</source> | </source> | ||
− | == | + | == See Also == |
− | + | * [[parent::love]] | |
+ | * [[love.textinput]] | ||
[[Category:Libraries]] | [[Category:Libraries]] | ||
− | {{#set:Description=Provides basic support for UTF-8 | + | [[Category:ThirdParty]] |
+ | {{#set:Description=Provides basic support for manipulating UTF-8 strings.}} | ||
{{#set:LOVE Version=0.9.2}} | {{#set:LOVE Version=0.9.2}} | ||
+ | {{#set:Keyword=String}} | ||
== Other Languages == | == Other Languages == | ||
{{i18n|utf8}} | {{i18n|utf8}} |
Latest revision as of 03:04, 5 July 2023
Available since LÖVE 0.9.2 |
This module is not supported in earlier versions. |
This library provides basic support for handling UTF-8 encoded strings.
It provides all its functions inside the table returned by require("utf8")
. This library does not provide any support for Unicode other than handling UTF-8 encoding. Any operation that needs the meaning of a character, such as character classification, is outside its scope.
For detailed usage, see the reference manual.
The utf8.char function does not work correctly in 0.9.2; However, it is not an issue since 0.10.0 |
Examples
Print text the user writes, and erase it using the UTF-8 module.
local utf8 = require("utf8")
function love.load()
text = "Type away! -- "
-- enable key repeat so backspace can be held down to trigger love.keypressed multiple times.
love.keyboard.setKeyRepeat(true)
end
function love.textinput(t)
text = text .. t
end
function love.keypressed(key)
if key == "backspace" then
-- get the byte offset to the last UTF-8 character in the string.
local byteoffset = utf8.offset(text, -1)
if byteoffset then
-- remove the last UTF-8 character.
-- string.sub operates on bytes rather than UTF-8 characters, so we couldn't do string.sub(text, 1, -2).
text = string.sub(text, 1, byteoffset - 1)
end
end
end
function love.draw()
love.graphics.printf(text, 0, 0, love.graphics.getWidth())
end
See Also
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info