Skip to content

Commit

Permalink
#6131: Move string algorithm to string/predicate.h, add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Oct 26, 2022
1 parent 62ed8fb commit 2762372
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
18 changes: 18 additions & 0 deletions libs/string/predicate.h
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <cstring>
#include <cctype>

namespace string
{
Expand Down Expand Up @@ -233,4 +234,21 @@ inline bool iends_with(const std::string& input, const TestStringType& test)
return ends_with(input, test, detail::isEqualNoCase);
}

/**
* Returns true if the given inpit string is consisting of alphanumeric characters only.
* Returns false if the input string is an empty string.
*/
inline bool isAlphaNumeric(const std::string& input)
{
for (const auto c : input)
{
if (!::isalpha(c) && !::isdigit(c))
{
return false;
}
}

return !input.empty();
}

}
9 changes: 3 additions & 6 deletions radiant/ui/UserInterfaceModule.cpp
Expand Up @@ -26,7 +26,7 @@
#include "wxutil/dialog/MessageBox.h"
#include "messages/TextureChanged.h"
#include "messages/ClearConsole.h"
#include "string/string.h"
#include "string/predicate.h"
#include "scene/Group.h"
#include "command/ExecutionNotPossible.h"

Expand Down Expand Up @@ -329,12 +329,9 @@ void UserInterfaceModule::registerControl(const IUserControl::Ptr& control)
}

// Check the name for validity
for (const auto c : control->getControlName())
if (!string::isAlphaNumeric(control->getControlName()))
{
if (!::isalpha(c) && !::isdigit(c))
{
throw std::invalid_argument("Control name " + control->getControlName() + " contains invalid characters, only alphanumerics are allowed");
}
throw std::invalid_argument("Control name " + control->getControlName() + " contains invalid characters, only alphanumerics are allowed");
}

// Add a command shortcut toggling this control
Expand Down
20 changes: 20 additions & 0 deletions test/Basic.cpp
Expand Up @@ -33,6 +33,26 @@ TEST(BasicTest, StringILessFunctor)
EXPECT_TRUE(!less("BLEH", "blah"));
}

TEST(BasicTest, StringIsAlphaNumeric)
{
EXPECT_FALSE(string::isAlphaNumeric(""));

EXPECT_TRUE(string::isAlphaNumeric("abc"));
EXPECT_TRUE(string::isAlphaNumeric("ABC"));
EXPECT_TRUE(string::isAlphaNumeric("12"));
EXPECT_TRUE(string::isAlphaNumeric("0"));
EXPECT_TRUE(string::isAlphaNumeric("abc12"));
EXPECT_TRUE(string::isAlphaNumeric("abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"));

EXPECT_FALSE(string::isAlphaNumeric("abc test"));
EXPECT_FALSE(string::isAlphaNumeric("abc\ntest"));
EXPECT_FALSE(string::isAlphaNumeric("abc\ttest"));
EXPECT_FALSE(string::isAlphaNumeric("abc\rtest"));
EXPECT_FALSE(string::isAlphaNumeric("$abc"));
EXPECT_FALSE(string::isAlphaNumeric("/abc"));
EXPECT_FALSE(string::isAlphaNumeric("test&afff"));
}

TEST(PathTests, GetFileExtension)
{
EXPECT_EQ(os::getExtension(""), "");
Expand Down

0 comments on commit 2762372

Please sign in to comment.