Azure IoT common library
Fork of azure_c_shared_utility by
Diff: strings.c
- Revision:
- 30:ce3813c5a692
- Parent:
- 25:8507bf644fdf
--- a/strings.c Fri Jun 30 10:42:00 2017 -0700 +++ b/strings.c Fri Jul 14 16:38:40 2017 -0700 @@ -24,7 +24,7 @@ typedef struct STRING_TAG { char* s; -}STRING; +} STRING; /*this function will allocate a new string with just '\0' in it*/ /*return NULL if it fails*/ @@ -137,7 +137,7 @@ if (format != NULL) { va_list arg_list; - int length; + int length; va_start(arg_list, format); /* Codes_SRS_STRING_07_041: [STRING_construct_sprintf shall determine the size of the resulting string and allocate the necessary memory.] */ @@ -534,7 +534,7 @@ else { va_list arg_list; - int s2Length; + int s2Length; va_start(arg_list, format); s2Length = vsnprintf(buf, maxBufSize, format, arg_list); @@ -553,7 +553,7 @@ else { STRING* s1 = (STRING*)handle; - char* temp; + char* temp; size_t s1Length = strlen(s1->s); temp = (char*)realloc(s1->s, s1Length + s2Length + 1); if (temp != NULL) @@ -807,3 +807,36 @@ } return (STRING_HANDLE)result; } + +int STRING_replace(STRING_HANDLE handle, char target, char replace) +{ + int result; + if (handle == NULL) + { + /* Codes_SRS_STRING_07_046: [ If handle is NULL STRING_replace shall return a non-zero value. ] */ + result = __FAILURE__; + } + else if (target == replace) + { + /* Codes_SRS_STRING_07_048: [ If target and replace are equal STRING_replace, shall do nothing shall return zero. ] */ + result = 0; + } + else + { + size_t length; + size_t index; + /* Codes_SRS_STRING_07_047: [ STRING_replace shall replace all instances of target with replace. ] */ + STRING* str_value = (STRING*)handle; + length = strlen(str_value->s); + for (index = 0; index < length; index++) + { + if (str_value->s[index] == target) + { + str_value->s[index] = replace; + } + } + /* Codes_SRS_STRING_07_049: [ On success STRING_replace shall return zero. ] */ + result = 0; + } + return result; +}