Added String Concatenation Feature

This commit is contained in:
Aaron Helton
2019-12-12 17:37:22 -05:00
parent 4d05a3096f
commit 1aaf42dc2b
3 changed files with 126 additions and 0 deletions

View File

@@ -52,6 +52,19 @@ EmberString *strip_estring(EmberString *str);
*/
int compare_estring(const EmberString *str, const EmberString *other);
/**
* Concatenates two EmberStrings into a new EmberString. If one or both strings
* are NULL, ember_string_error will be set to EMBER_STRING_NULL_ARG. In the
* case of null arguments, if only one string is null, it will return a new
* EmberString that is essentially just the non-null string. If both strings are
* NULL, it will return a valid, but empty, EmberString.
*
* @param str The first string to concatenate.
* @param other The second string to concatenate.
* @return A string that is the concatenation of both input strings.
*/
EmberString *estring_concat(EmberString *str, EmberString *other);
/*
* Error Handling Stuff
*/

View File

@@ -181,4 +181,38 @@ int compare_estring(const EmberString *str, const EmberString *other)
{
return 0;
}
}
#include <stdio.h>
EmberString *estring_concat(EmberString *str, EmberString *other)
{
if(str == NULL)
{
error_val = EMBER_STRING_NULL_ARG;
if(!other)
{
return create_estring_from_cstr("", 0);
}
return create_estring_from_cstr(other->c_str, other->len);
}
if(other == NULL)
{
error_val = EMBER_STRING_NULL_ARG;
return create_estring_from_cstr(str->c_str, str->len);
}
EmberString *result = malloc(sizeof(EmberString));
result->len = str->len + other->len;
result->c_str = malloc(sizeof(char) * (result->len + 1));
for(size_t i = 0; i < str->len; i++)
{
result->c_str[i] = str->c_str[i];
}
for(size_t i = str->len; i < other->len+str->len; i++)
{
result->c_str[i] = other->c_str[i-str->len];
}
result->c_str[result->len] = '\0';
return result;
}

View File

@@ -82,6 +82,84 @@ START_TEST(test_ember_string_from_cstr)
}
END_TEST
START_TEST(test_ember_string_concat)
{
EmberString *str1 = create_estring_from_cstr("String1", 7);
EmberString *str2 = create_estring_from_cstr("String2", 7);
EmberString *empty = create_estring_from_cstr("", 0);
EmberString *null_str = NULL;
EmberString *result = estring_concat(str1, str2);
ck_assert_ptr_nonnull(result);
ck_assert_ptr_nonnull(result->c_str);
ck_assert_ptr_ne(result->c_str, str1->c_str);
ck_assert_ptr_ne(result->c_str, str2->c_str);
ck_assert_int_eq(result->len, str1->len+str2->len);
ck_assert_str_eq(result->c_str, "String1String2");
ck_assert_int_eq(ember_string_error(), EMBER_STRING_NO_ERR);
result = estring_concat(str2, str1);
ck_assert_ptr_nonnull(result);
ck_assert_ptr_nonnull(result->c_str);
ck_assert_ptr_ne(result->c_str, str1->c_str);
ck_assert_ptr_ne(result->c_str, str2->c_str);
ck_assert_int_eq(result->len, str1->len+str2->len);
ck_assert_str_eq(result->c_str, "String2String1");
ck_assert_int_eq(ember_string_error(), EMBER_STRING_NO_ERR);
result = estring_concat(str1, empty);
ck_assert_ptr_nonnull(result);
ck_assert_ptr_nonnull(result->c_str);
ck_assert_ptr_ne(result->c_str, str1->c_str);
ck_assert_ptr_ne(result->c_str, empty->c_str);
ck_assert_int_eq(result->len, str1->len);
ck_assert_str_eq(result->c_str, "String1");
ck_assert_int_eq(ember_string_error(), EMBER_STRING_NO_ERR);
result = estring_concat(str1, null_str);
ck_assert_ptr_nonnull(result);
ck_assert_ptr_nonnull(result->c_str);
ck_assert_ptr_ne(result->c_str, str1->c_str);
ck_assert_ptr_ne(result->c_str, empty->c_str);
ck_assert_int_eq(result->len, str1->len);
ck_assert_str_eq(result->c_str, "String1");
ck_assert_int_eq(ember_string_error(), EMBER_STRING_NULL_ARG);
clear_ember_string_error();
result = estring_concat(null_str, str2);
ck_assert_ptr_nonnull(result);
ck_assert_ptr_nonnull(result->c_str);
ck_assert_ptr_ne(result->c_str, str2->c_str);
ck_assert_ptr_ne(result->c_str, empty->c_str);
ck_assert_int_eq(result->len, str1->len);
ck_assert_str_eq(result->c_str, "String2");
ck_assert_int_eq(ember_string_error(), EMBER_STRING_NULL_ARG);
clear_ember_string_error();
result = estring_concat(null_str, null_str);
ck_assert_ptr_nonnull(result);
ck_assert_ptr_nonnull(result->c_str);
ck_assert_int_eq(result->len, empty->len);
ck_assert_str_eq(result->c_str, empty->c_str);
ck_assert_int_eq(ember_string_error(), EMBER_STRING_NULL_ARG);
clear_ember_string_error();
result = estring_concat(empty, empty);
ck_assert_ptr_nonnull(result);
ck_assert_ptr_nonnull(result->c_str);
ck_assert_ptr_ne(result->c_str, empty->c_str);
ck_assert_int_eq(result->len, empty->len);
ck_assert_str_eq(result->c_str, empty->c_str);
ck_assert_int_eq(ember_string_error(), EMBER_STRING_NO_ERR);
clear_ember_string_error();
destroy_estring(str1);
destroy_estring(str2);
destroy_estring(empty);
destroy_estring(result);
}
END_TEST
Suite *ember_string_suite(void)
{
Suite *s;
@@ -93,6 +171,7 @@ Suite *ember_string_suite(void)
tc_core = tcase_create("Core");
tcase_add_test(tc_core, test_ember_string_from_cstr);
tcase_add_test(tc_core, test_ember_string_concat);
suite_add_tcase(s, tc_core);
return s;