Skip to content

Commit 49a4bb1

Browse files
committed
Add String Replace Function to Lua Scripting API
This change exposes a new `str_replace_all` function to the Lua scripting environment, allowing Lua scripts to perform substring replacements on strings. The implementation provides an efficient string replacement utility that finds and replaces all occurrences of a substring within a given string. The function includes optimizations such as pre-allocation to reduce memory reallocations during the replacement process, and handles edge cases like empty search strings by returning the original string unchanged. The new function is registered with LuaBridge in the `codelite` namespace, making it accessible to all Lua scripts running within the CodeLite environment. Comprehensive documentation has been added to the header file, including usage examples and implementation notes. * Scripting/CodeLiteLUA.cpp - Register new function and add implementation * Scripting/CodeLiteLUA.hpp - Add function declaration with documentation ** Generated by CodeLite. ** Signed-off-by: Eran Ifrah <eran@codelite.org>
1 parent 612edca commit 49a4bb1

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

Plugin/Scripting/CodeLiteLUA.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void CodeLiteLUA::Initialise()
4040
auto& self = Get();
4141
self.m_state = luaL_newstate();
4242
luaL_openlibs(self.m_state);
43-
43+
4444
try {
4545
clDEBUG() << "Registering codelite with LUA" << endl;
4646
luabridge::getGlobalNamespace(self.m_state)
@@ -57,6 +57,7 @@ void CodeLiteLUA::Initialise()
5757
.addFunction("log_system", &CodeLiteLUA::log_system)
5858
.addFunction("log_debug", &CodeLiteLUA::log_debug)
5959
.addFunction("log_trace", &CodeLiteLUA::log_trace)
60+
.addFunction("str_replace_all", &CodeLiteLUA::str_replace_all)
6061
.endNamespace();
6162

6263
} catch (const std::exception& e) {
@@ -196,6 +197,29 @@ void CodeLiteLUA::log_message(const std::string& msg, FileLogger::LogLevel level
196197
}
197198
}
198199

200+
std::string
201+
CodeLiteLUA::str_replace_all(const std::string& str, const std::string& find_what, const std::string& replace_with)
202+
{
203+
if (find_what.empty()) {
204+
return str;
205+
}
206+
207+
std::string result;
208+
result.reserve(str.size()); // Pre-allocate to reduce reallocations
209+
210+
size_t pos = 0;
211+
size_t last_pos = 0;
212+
213+
while ((pos = str.find(find_what, last_pos)) != std::string::npos) {
214+
result.append(str, last_pos, pos - last_pos);
215+
result.append(replace_with);
216+
last_pos = pos + find_what.length();
217+
}
218+
219+
result.append(str, last_pos, std::string::npos);
220+
return result;
221+
}
222+
199223
void CodeLiteLUA::chat(const std::string& prompt)
200224
{
201225
// send an event requesting to initiate a chat

Plugin/Scripting/CodeLiteLUA.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,41 @@ class WXDLLIMPEXP_SDK CodeLiteLUA : public wxEvtHandler
249249
*/
250250
static inline void log_trace(const std::string& msg) { log_message(msg, FileLogger::LogLevel::Developer); }
251251

252+
/**
253+
* @brief Replaces all occurrences of a substring within a string with another substring.
254+
*
255+
* This function searches for all instances of `find_what` in `str` and replaces them with
256+
* `replace_with`. The function pre-allocates memory to minimize reallocations during the
257+
* replacement process. If `find_what` is empty, the original string is returned unchanged.
258+
*
259+
* @param str The input string to search within.
260+
* @param find_what The substring to search for and replace.
261+
* @param replace_with The substring to replace each occurrence of `find_what` with.
262+
*
263+
* @return std::string A new string with all occurrences of `find_what` replaced by `replace_with`.
264+
*
265+
* @note If `find_what` is an empty string, the function returns a copy of the original string
266+
* to avoid infinite loops.
267+
*
268+
* @code
269+
* CodeLiteLUA lua_handler;
270+
* std::string input = "Hello world, world!";
271+
* std::string result = lua_handler.str_replace_all(input, "world", "universe");
272+
* // result is "Hello universe, universe!"
273+
*
274+
* std::string no_match = lua_handler.str_replace_all(input, "xyz", "abc");
275+
* // no_match is "Hello world, world!" (unchanged)
276+
*
277+
* std::string empty_find = lua_handler.str_replace_all(input, "", "test");
278+
* // empty_find is "Hello world, world!" (unchanged)
279+
* @endcode
280+
*
281+
* @see std::string::find
282+
* @see std::string::replace
283+
*/
284+
static std::string
285+
str_replace_all(const std::string& str, const std::string& find_what, const std::string& replace_with);
286+
252287
private:
253288
CodeLiteLUA();
254289
~CodeLiteLUA();

0 commit comments

Comments
 (0)