Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lower and upper string operations to stringify header. #4933

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/stringify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
#include <sstream>
#include <string>

#include "absl/strings/ascii.h"
#include "big_int.h"
#include "cstring.h"
#include "exceptions.h"
Expand Down Expand Up @@ -128,4 +129,12 @@ cstring toString(cstring value) {

cstring toString(std::string_view value) { return cstring(value); }

void lowerInPlace(std::string &s) { absl::AsciiStrToLower(&s); }

std::string toLower(std::string_view s) { return absl::AsciiStrToLower(s); }

void upperInPlace(std::string &s) { absl::AsciiStrToUpper(&s); }

std::string toUpper(std::string_view s) { return absl::AsciiStrToUpper(s); }

} // namespace P4::Util
15 changes: 15 additions & 0 deletions lib/stringify.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ cstring toString(const void *value);

char DigitToChar(int digit);

/// Convert a string to lower case. Using non-ASCII strings might lead to unexpected results..
/// Performs the lowering operation in place.
void lowerInPlace(std::string &s);

/// Convert a string to lower case. Using non-ASCII strings might lead to unexpected results..
/// Returns a copy of the string.
std::string toLower(std::string_view s);

/// Convert a string to upper case. Using non-ASCII strings might lead to unexpected results..
/// Performs the up operation in place.
void upperInPlace(std::string &s);

/// Convert a string to upper case. Using non-ASCII strings might lead to unexpected results..
std::string toUpper(std::string_view s);

} // namespace Util
} // namespace P4

Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ set (GTEST_UNITTEST_SOURCES
gtest/source_file_test.cpp
gtest/strength_reduction.cpp
gtest/string_map.cpp
gtest/string_test.cpp
gtest/transforms.cpp
gtest/rtti_test.cpp
gtest/nethash.cpp
Expand Down
165 changes: 165 additions & 0 deletions test/gtest/string_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include <gtest/gtest.h>

#include <string>

#include "lib/stringify.h"

namespace P4::Test {

TEST(StringCaseConversionTests, MixedCase) {
std::string input = "HeLLo WoRLd!";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "hello world!");

EXPECT_EQ(Util::toLower(input), "hello world!");

std::string upperInput = input;
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, "HELLO WORLD!");

EXPECT_EQ(Util::toUpper(input), "HELLO WORLD!");
}

TEST(StringCaseConversionTests, DigitsAndSpecialChars) {
std::string input = "123ABC$%^";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "123abc$%^");

EXPECT_EQ(Util::toLower(input), "123abc$%^");

std::string upperInput = input;
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, "123ABC$%^");

EXPECT_EQ(Util::toUpper(input), "123ABC$%^");
}

TEST(StringCaseConversionTests, AlreadyLowercase) {
std::string input = "already lowercase";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "already lowercase");

EXPECT_EQ(Util::toLower(input), "already lowercase");

Util::upperInPlace(input);
EXPECT_EQ(input, "ALREADY LOWERCASE");

EXPECT_EQ(Util::toUpper("already lowercase"), "ALREADY LOWERCASE");
}

TEST(StringCaseConversionTests, AlreadyUppercase) {
std::string input = "ALREADY UPPERCASE";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "already uppercase");

EXPECT_EQ(Util::toLower(input), "already uppercase");

Util::upperInPlace(input);
EXPECT_EQ(input, "ALREADY UPPERCASE");

EXPECT_EQ(Util::toUpper(input), "ALREADY UPPERCASE");
}

TEST(StringCaseConversionTests, EmptyString) {
std::string input;

Util::lowerInPlace(input);
EXPECT_EQ(input, "");

EXPECT_EQ(Util::toLower(""), "");

Util::upperInPlace(input);
EXPECT_EQ(input, "");

EXPECT_EQ(Util::toUpper(""), "");
}

TEST(StringCaseConversionTests, SingleCharacter) {
std::string input = "A";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "a");

EXPECT_EQ(Util::toLower(input), "a");

std::string upperInput = "b";
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, "B");

EXPECT_EQ(Util::toUpper("b"), "B");
}

TEST(StringCaseConversionTests, WhitespaceString) {
std::string input = " HeLlO wOrLd ";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, " hello world ");

EXPECT_EQ(Util::toLower(input), " hello world ");

std::string upperInput = input;
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, " HELLO WORLD ");

EXPECT_EQ(Util::toUpper(input), " HELLO WORLD ");
}

TEST(StringCaseConversionTests, UnicodeCharacters) {
std::string input = "áÉíÓú";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "áéíóú");

EXPECT_EQ(Util::toLower(input), "áéíóú");

std::string upperInput = input;
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, "ÁÉÍÓÚ");

EXPECT_EQ(Util::toUpper(input), "ÁÉÍÓÚ");
}

TEST(StringCaseConversionTests, SymbolsOnly) {
std::string input = "@#$%^&*";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "@#$%^&*");

EXPECT_EQ(Util::toLower(input), "@#$%^&*");

std::string upperInput = input;
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, "@#$%^&*");

EXPECT_EQ(Util::toUpper(input), "@#$%^&*");
}

TEST(StringCaseConversionTests, LongString) {
std::string input = "ThisIsAVeryLongStringToTestTheEfficiencyOfTheFunctions";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "thisisaverylongstringtotesttheefficiencyofthefunctions");

EXPECT_EQ(Util::toLower(input), "thisisaverylongstringtotesttheefficiencyofthefunctions");

std::string upperInput = input;
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, "THISISAVERYLONGSTRINGTOTESTTHEEFFICIENCYOFTHEFUNCTIONS");

EXPECT_EQ(Util::toUpper(input), "THISISAVERYLONGSTRINGTOTESTTHEEFFICIENCYOFTHEFUNCTIONS");
}

} // namespace P4::Test
Loading