Skip to content
Snippets Groups Projects
Commit 0700f514 authored by Mergen Imeev's avatar Mergen Imeev Committed by Vladimir Davydov
Browse files

trivia: add string conversion to upper case

In some cases we need to convert a string to upper case (e.g., when
normalizing a lower-case field name for SQL): add helper functions
that do this in-place or by creating a copy of the original string.

Needed for #8098

NO_DOC=internal
NO_CHANGELOG=internal
parent 2f1ae906
No related branches found
No related tags found
No related merge requests found
......@@ -510,3 +510,22 @@ strtolowerdup(const char *s)
lowercase[len] = '\0';
return lowercase;
}
char *
strtoupper(char *s)
{
for (size_t i = 0; s[i] != '\0'; ++i)
s[i] = (char)toupper(s[i]);
return s;
}
char *
strtoupperdup(const char *s)
{
size_t len = strlen(s);
char *uppercase = xmalloc(len + 1);
for (size_t i = 0; i < len; ++i)
uppercase[i] = (char)toupper(s[i]);
uppercase[len] = '\0';
return uppercase;
}
......@@ -748,6 +748,17 @@ strtolower(char *s);
char *
strtolowerdup(const char *s);
/** Returns the null-terminated string converted to upper case in-place. */
char *
strtoupper(char *s);
/**
* Returns a copy of the null-terminated string converted to upper case. The
* result is dynamically allocated using `xmalloc`.
*/
char *
strtoupperdup(const char *s);
#if !defined(__cplusplus) && !defined(static_assert)
# define static_assert _Static_assert
#endif
......
......@@ -5,7 +5,8 @@
#include "unit.h"
static const char *const test_lower_case_conv_expected = "str";
static const char *const test_lower_case_conv_input[] = {
static const char *const test_upper_case_conv_expected = "STR";
static const char *const test_case_conv_input[] = {
"str", "Str", "sTr", "stR", "STr", "sTR", "StR", "STR"
};
......@@ -50,15 +51,15 @@ static void
test_strtolowerdup(void)
{
header();
plan(lengthof(test_lower_case_conv_input) * 2);
plan(lengthof(test_case_conv_input) * 2);
for (size_t i = 0; i < lengthof(test_lower_case_conv_input); ++i) {
char *test = strtolowerdup(test_lower_case_conv_input[i]);
isnt(test, test_lower_case_conv_input[i],
"a copy of %s is returned", test_lower_case_conv_input[i]);
for (size_t i = 0; i < lengthof(test_case_conv_input); ++i) {
char *test = strtolowerdup(test_case_conv_input[i]);
isnt(test, test_case_conv_input[i],
"a copy of %s is returned", test_case_conv_input[i]);
is(strcmp(test_lower_case_conv_expected, test), 0,
"%s is converted to lower case correctly",
test_lower_case_conv_input[i]);
test_case_conv_input[i]);
free(test);
}
......@@ -70,15 +71,55 @@ static void
test_strtolower(void)
{
header();
plan(lengthof(test_lower_case_conv_input) * 2);
plan(lengthof(test_case_conv_input) * 2);
for (size_t i = 0; i < lengthof(test_lower_case_conv_input); ++i) {
char *cp = xstrdup(test_lower_case_conv_input[i]);
for (size_t i = 0; i < lengthof(test_case_conv_input); ++i) {
char *cp = xstrdup(test_case_conv_input[i]);
char *test = strtolower(cp);
is(test, cp, "%s is converted in-place", cp);
is(strcmp(test_lower_case_conv_expected, test), 0,
"%s is converted to lower case correctly",
test_lower_case_conv_input[i]);
test_case_conv_input[i]);
free(cp);
}
footer();
check_plan();
}
static void
test_strtoupperdup(void)
{
header();
plan(lengthof(test_case_conv_input) * 2);
for (size_t i = 0; i < lengthof(test_case_conv_input); ++i) {
char *test = strtoupperdup(test_case_conv_input[i]);
isnt(test, test_case_conv_input[i],
"a copy of %s is returned", test_case_conv_input[i]);
is(strcmp(test_upper_case_conv_expected, test), 0,
"%s is converted to upper case correctly",
test_case_conv_input[i]);
free(test);
}
footer();
check_plan();
}
static void
test_strtoupper(void)
{
header();
plan(lengthof(test_case_conv_input) * 2);
for (size_t i = 0; i < lengthof(test_case_conv_input); ++i) {
char *cp = xstrdup(test_case_conv_input[i]);
char *test = strtoupper(cp);
is(test, cp, "%s is converted in-place", cp);
is(strcmp(test_upper_case_conv_expected, test), 0,
"%s is converted to upper case correctly",
test_case_conv_input[i]);
free(cp);
}
......@@ -89,12 +130,14 @@ test_strtolower(void)
int
main(void)
{
plan(3);
plan(5);
header();
test_strlcat();
test_strtolowerdup();
test_strtolower();
test_strtoupperdup();
test_strtoupper();
footer();
return check_plan();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment