Skip to content

Commit cce4792

Browse files
authored
Merge pull request #159 from RobLoach/subtext
text: Fix TextSubtext() address bounding
2 parents ba1583f + 667f543 commit cce4792

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

include/Functions.hpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -291,63 +291,68 @@ RLCPPAPI inline unsigned int TextLength(const std::string& text) {
291291
* Get text length, checks for '\0' ending
292292
*/
293293
RLAPI inline std::string TextSubtext(const std::string& text, int position, int length) {
294-
return TextSubtext(text.c_str(), position, length);
294+
return ::TextSubtext(text.c_str(), position, length);
295295
}
296296

297297
/**
298298
* Replace text string (WARNING: memory must be freed!)
299299
*/
300300
RLAPI inline std::string TextReplace(const std::string& text, const std::string& replace, const std::string& by) {
301-
return TextReplace(text.c_str(), replace.c_str(), by.c_str());
301+
char* output = ::TextReplace((char*)text.c_str(), replace.c_str(), by.c_str());
302+
if (output != NULL) {
303+
std::string stringOutput(output);
304+
free(output);
305+
return stringOutput;
306+
}
307+
return "";
302308
}
303309

304310
/**
305311
* Insert text in a position (WARNING: memory must be freed!)
306312
*/
307313
RLAPI inline std::string TextInsert(const std::string& text, const std::string& insert, int position) {
308-
return TextInsert(text.c_str(), insert.c_str(), position);
309-
}
310-
311-
/**
312-
* Append text at specific position and move cursor!
313-
*/
314-
RLAPI inline void TextAppend(const std::string& text, const std::string& append, int *position) {
315-
return TextAppend(text.c_str(), append.c_str(), position);
314+
char* output = ::TextInsert(text.c_str(), insert.c_str(), position);
315+
if (output != NULL) {
316+
std::string stringOutput(output);
317+
free(output);
318+
return stringOutput;
319+
}
320+
return "";
316321
}
317322

318323
/**
319324
* Find first text occurrence within a string
320325
*/
321326
RLAPI inline int TextFindIndex(const std::string& text, const std::string& find) {
322-
return TextFindIndex(text.c_str(), find.c_str());
327+
return ::TextFindIndex(text.c_str(), find.c_str());
323328
}
324329

325330
/**
326331
* Get upper case version of provided string
327332
*/
328333
RLAPI inline std::string TextToUpper(const std::string& text) {
329-
return TextToUpper(text.c_str());
334+
return ::TextToUpper(text.c_str());
330335
}
331336

332337
/**
333338
* Get lower case version of provided string
334339
*/
335340
RLAPI inline std::string TextToLower(const std::string& text) {
336-
return TextToLower(text.c_str());
341+
return ::TextToLower(text.c_str());
337342
}
338343

339344
/**
340345
* Get Pascal case notation version of provided string
341346
*/
342347
RLAPI inline std::string TextToPascal(const std::string& text) {
343-
return TextToPascal(text.c_str());
348+
return ::TextToPascal(text.c_str());
344349
}
345350

346351
/**
347352
* Get integer value from text (negative values not supported)
348353
*/
349354
RLAPI inline int TextToInteger(const std::string& text) {
350-
return TextToInteger(text.c_str());
355+
return ::TextToInteger(text.c_str());
351356
}
352357

353358
} // namespace raylib

tests/raylib_test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ int main(int argc, char *argv[]) {
5858
assert(files.size() > 3);
5959
}
6060

61+
// raylib::TextReplace()
62+
{
63+
std::string input = "Hello World!";
64+
std::string output = raylib::TextReplace(input, "World", "Moon");
65+
assert(output == "Hello Moon!");
66+
}
67+
68+
// raylib::TextInsert()
69+
{
70+
std::string input = "Hello World!";
71+
std::string output = raylib::TextInsert(input, "Good!", 0);
72+
assert(output == "Good! World!");
73+
}
74+
75+
// raylib::TextSubtext()
76+
{
77+
std::string input = "Hello World!";
78+
std::string output = raylib::TextSubtext(input, 6, 5);
79+
assert(output == "World");
80+
}
81+
6182
// Sound
6283
{
6384
raylib::Wave wave(path + "/resources/weird.wav");

0 commit comments

Comments
 (0)