summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjussi2024-01-28 18:29:06 +0200
committerjussi2024-01-28 18:29:06 +0200
commit143453af9ea9be82b2d58d665bed62abb716e21f (patch)
tree53ead99a8db6da542c868f48fb3115a3da0d5df6
parent71cc89c3033365746e5dcdb933c460c8e0da7fb0 (diff)
downloadreilua-enhanced-143453af9ea9be82b2d58d665bed62abb716e21f.tar.gz
reilua-enhanced-143453af9ea9be82b2d58d665bed62abb716e21f.tar.bz2
reilua-enhanced-143453af9ea9be82b2d58d665bed62abb716e21f.zip
TextInsert and TextSplit.
-rw-r--r--API.md20
-rw-r--r--ReiLua_API.lua17
-rw-r--r--changelog1
-rw-r--r--devnotes3
-rw-r--r--include/text.h3
-rw-r--r--src/lua_core.c3
-rw-r--r--src/text.c61
7 files changed, 105 insertions, 3 deletions
diff --git a/API.md b/API.md
index d003c37..70a75f2 100644
--- a/API.md
+++ b/API.md
@@ -6668,6 +6668,26 @@ Encode one codepoint into UTF-8 byte array
---
+## Text - Text strings management functions (no UTF-8 strings, only byte chars)
+
+---
+
+> text = RL.TextInsert( string text, string insert, int position )
+
+Insert text in a specific position, moves all text forward
+
+- Success return string
+
+---
+
+> splits = RL.TextSplit( string text, char delimiter )
+
+Split text into multiple strings
+
+- Success return string{}
+
+---
+
## Models - Basic geometric 3D shapes drawing functions
---
diff --git a/ReiLua_API.lua b/ReiLua_API.lua
index fb11af6..eb3f1b5 100644
--- a/ReiLua_API.lua
+++ b/ReiLua_API.lua
@@ -3999,6 +3999,23 @@ function RL.GetCodepointPrevious( text ) end
---@return any utf8Size
function RL.CodepointToUTF8( codepoint ) end
+-- Text - Text strings management functions (no UTF-8 strings, only byte chars)
+
+---Insert text in a specific position, moves all text forward
+---- Success return string
+---@param text string
+---@param insert string
+---@param position integer
+---@return any text
+function RL.TextInsert( text, insert, position ) end
+
+---Split text into multiple strings
+---- Success return string{}
+---@param text string
+---@param delimiter any
+---@return any splits
+function RL.TextSplit( text, delimiter ) end
+
-- Models - Basic geometric 3D shapes drawing functions
---Draw a line in 3D world space
diff --git a/changelog b/changelog
index f94e786..2a72fc7 100644
--- a/changelog
+++ b/changelog
@@ -65,6 +65,7 @@ DETAILED CHANGES:
- CHANGE: Raygui lib changed term element to control to correspond raylib naming.
- ADDED: LoadBufferFromString, LoadWaveFromMemory and LoadMusicStreamFromMemory.
- CHANGE: GetGlyphInfo and GetGlyphInfoByIndex return glyphInfo as lightuserdata.
+ - ADDED: TextInsert and TextSplit.
------------------------------------------------------------------------
Release: ReiLua version 0.6.0 Using Raylib 4.5
diff --git a/devnotes b/devnotes
index d6fc514..bf24d14 100644
--- a/devnotes
+++ b/devnotes
@@ -9,9 +9,6 @@ Backlog {
* Platform desktop SDL
* Text input not working on gui. Could this be Raylib issue?
* Haptic functions.
- * Text
- * Text codepoints management functions (unicode characters)? Could be usefull for luajit.
- * Some of the Text strings management functions could be easier to use than the Lua ones.
* Audio
* AudioStream.
* Core
diff --git a/include/text.h b/include/text.h
index bbfe1ba..ff516d2 100644
--- a/include/text.h
+++ b/include/text.h
@@ -53,3 +53,6 @@ int ltextGetCodepoint( lua_State *L );
int ltextGetCodepointNext( lua_State *L );
int ltextGetCodepointPrevious( lua_State *L );
int ltextCodepointToUTF8( lua_State *L );
+/* Text strings management functions (no UTF-8 strings, only byte chars) */
+int ltextTextInsert( lua_State *L );
+int ltextTextSplit( lua_State *L );
diff --git a/src/lua_core.c b/src/lua_core.c
index a7c9f57..970a2ba 100644
--- a/src/lua_core.c
+++ b/src/lua_core.c
@@ -1765,6 +1765,9 @@ void luaRegister() {
assingGlobalFunction( "GetCodepointNext", ltextGetCodepointNext );
assingGlobalFunction( "GetCodepointPrevious", ltextGetCodepointPrevious );
assingGlobalFunction( "CodepointToUTF8", ltextCodepointToUTF8 );
+ /* Text strings management functions (no UTF-8 strings, only byte chars) */
+ assingGlobalFunction( "TextInsert", ltextTextInsert );
+ assingGlobalFunction( "TextSplit", ltextTextSplit );
/* Audio. */
/* Audio device management functions. */
diff --git a/src/text.c b/src/text.c
index b09eb96..e214c70 100644
--- a/src/text.c
+++ b/src/text.c
@@ -1042,6 +1042,8 @@ int ltextLoadCodepoints( lua_State *L ) {
int count = 0;
int* codepoints = LoadCodepoints( text, &count );
+ lua_createtable( L, count, 0 );
+
for ( int i = 0; i < count; i++ ) {
lua_pushinteger( L, codepoints[i] );
lua_rawseti( L, -2, i + 1 );
@@ -1133,3 +1135,62 @@ int ltextCodepointToUTF8( lua_State *L ) {
return 2;
}
+
+/*
+## Text - Text strings management functions (no UTF-8 strings, only byte chars)
+*/
+
+/*
+> text = RL.TextInsert( string text, string insert, int position )
+
+Insert text in a specific position, moves all text forward
+
+- Success return string
+*/
+int ltextTextInsert( lua_State *L ) {
+ const char* text = luaL_checkstring( L, 1 );
+ const char* insert = luaL_checkstring( L, 2 );
+ int position = luaL_checkinteger( L, 3 );
+
+ // char* result = TextInsert( text, insert, position ); // Bug in the raylib implementation.
+
+ int textLen = TextLength( text );
+ int insertLen = TextLength( insert );
+ char* result = RL_MALLOC( textLen + insertLen + 1 );
+
+ memcpy( result, text, position );
+ memcpy( result + position, insert, insertLen );
+ memcpy( result + position + insertLen, text + position, textLen - position );
+
+ result[ textLen + insertLen ] = '\0'; // Make sure text string is valid!
+
+ lua_pushstring( L, result );
+ free( result );
+
+ return 1;
+}
+
+/*
+> splits = RL.TextSplit( string text, char delimiter )
+
+Split text into multiple strings
+
+- Success return string{}
+*/
+int ltextTextSplit( lua_State *L ) {
+ const char* text = luaL_checkstring( L, 1 );
+ const char* delimiter = luaL_checkstring( L, 2 );
+
+ int count = 0;
+ const char** splits = TextSplit( text, delimiter[0], &count );
+
+ lua_createtable( L, count, 0 );
+
+ for ( int i = 0; i < count; i++ ) {
+ lua_pushstring( L, splits[i] );
+ lua_rawseti( L, -2, i + 1 );
+ }
+ /* Note! No need to free. Uses static memory. */
+
+ return 1;
+}