Skip to content

Commit

Permalink
Cleanup and (mostly) document util/string.h and (very) minor refactoring
Browse files Browse the repository at this point in the history
Updated: Incorporated feedback from 'kahrl'
Updated: Moved MinetestApp::boolToCStr() from game.cpp into string.h renaming it bool_to_cstr()
  • Loading branch information
Zeno- authored and kahrl committed Nov 2, 2014
1 parent 8040806 commit 43bf432
Show file tree
Hide file tree
Showing 3 changed files with 304 additions and 138 deletions.
15 changes: 3 additions & 12 deletions src/game.cpp
Expand Up @@ -1386,8 +1386,6 @@ class MinetestApp
void showOverlayMessage(const char *msg, float dtime, int percent,
bool draw_clouds = true);

inline const char *boolToCStr(bool v);

private:
InputHandler *input;

Expand Down Expand Up @@ -2472,7 +2470,7 @@ void MinetestApp::toggleFreeMove(float *statustext_time)
static const wchar_t *msg[] = { L"free_move disabled", L"free_move enabled" };

bool free_move = !g_settings->getBool("free_move");
g_settings->set("free_move", boolToCStr(free_move));
g_settings->set("free_move", bool_to_cstr(free_move));

*statustext_time = 0;
statustext = msg[free_move];
Expand All @@ -2494,7 +2492,7 @@ void MinetestApp::toggleFast(float *statustext_time)
{
static const wchar_t *msg[] = { L"fast_move disabled", L"fast_move enabled" };
bool fast_move = !g_settings->getBool("fast_move");
g_settings->set("fast_move", boolToCStr(fast_move));
g_settings->set("fast_move", bool_to_cstr(fast_move));

*statustext_time = 0;
statustext = msg[fast_move];
Expand All @@ -2508,7 +2506,7 @@ void MinetestApp::toggleNoClip(float *statustext_time)
{
static const wchar_t *msg[] = { L"noclip disabled", L"noclip enabled" };
bool noclip = !g_settings->getBool("noclip");
g_settings->set("noclip", boolToCStr(noclip));
g_settings->set("noclip", bool_to_cstr(noclip));

*statustext_time = 0;
statustext = msg[noclip];
Expand Down Expand Up @@ -3937,13 +3935,6 @@ void MinetestApp::showOverlayMessage(const char *msg, float dtime,
}


inline const char *MinetestApp::boolToCStr(bool v)
{
static const char *str[] = { "false", "true" };
return str[v];
}



/****************************************************************************
Shutdown / cleanup
Expand Down
26 changes: 25 additions & 1 deletion src/test.cpp
Expand Up @@ -166,7 +166,7 @@ struct TestUtilities: public TestBase
UASSERT(is_yes("0") == false);
UASSERT(is_yes("1") == true);
UASSERT(is_yes("2") == true);
const char *ends[] = {"abc", "c", "bc", NULL};
const char *ends[] = {"abc", "c", "bc", "", NULL};
UASSERT(removeStringEnd("abc", ends) == "");
UASSERT(removeStringEnd("bc", ends) == "b");
UASSERT(removeStringEnd("12c", ends) == "12");
Expand All @@ -175,6 +175,30 @@ struct TestUtilities: public TestBase
== "%22Aardvarks%20lurk%2C%20OK%3F%22");
UASSERT(urldecode("%22Aardvarks%20lurk%2C%20OK%3F%22")
== "\"Aardvarks lurk, OK?\"");
UASSERT(padStringRight("hello", 8) == "hello ");
UASSERT(str_equal(narrow_to_wide("abc"), narrow_to_wide("abc")));
UASSERT(str_equal(narrow_to_wide("ABC"), narrow_to_wide("abc"), true));
UASSERT(trim(" a") == "a");
UASSERT(trim(" a ") == "a");
UASSERT(trim("a ") == "a");
UASSERT(trim("") == "");
UASSERT(mystoi("123", 0, 1000) == 123);
UASSERT(mystoi("123", 0, 10) == 10);
std::string test_str;
test_str = "Hello there";
str_replace(test_str, "there", "world");
UASSERT(test_str == "Hello world");
test_str = "ThisAisAaAtest";
str_replace_char(test_str, 'A', ' ');
UASSERT(test_str == "This is a test");
UASSERT(string_allowed("hello", "abcdefghijklmno") == true);
UASSERT(string_allowed("123", "abcdefghijklmno") == false);
UASSERT(string_allowed_blacklist("hello", "123") == true);
UASSERT(string_allowed_blacklist("hello123", "123") == false);
UASSERT(wrap_rows("12345678",4) == "1234\n5678");
UASSERT(is_number("123") == true);
UASSERT(is_number("") == false);
UASSERT(is_number("123a") == false);
}
};

Expand Down

0 comments on commit 43bf432

Please sign in to comment.