summaryrefslogtreecommitdiffstats
path: root/src/util/string_utils.c
diff options
context:
space:
mode:
authorJakub Hrozek <jhrozek@redhat.com>2014-08-05 10:12:34 +0200
committerJakub Hrozek <jhrozek@redhat.com>2014-08-13 15:10:22 +0200
commit1f3127e88a87953f059c9a70d3582ae1719594b1 (patch)
treeb4638413e09809da334d7dfa6ef0b75f51e2f61c /src/util/string_utils.c
parentd3c6fca0f0d3b1c5d3dda3dcf3de0ae3ae4c0c38 (diff)
downloadsssd-1f3127e88a87953f059c9a70d3582ae1719594b1.tar.gz
sssd-1f3127e88a87953f059c9a70d3582ae1719594b1.tar.xz
sssd-1f3127e88a87953f059c9a70d3582ae1719594b1.zip
Only replace space with the specified substitution
https://fedorahosted.org/sssd/ticket/2397 - make sss_replace_whitespaces only replace space (' ') not any whitespace - make sss_replace_whitespaces only replace a single char, not the whole string - rename CONFDB_NSS_OVERRIDE_DEFAULT_WHITESPACE to CONFDB_NSS_OVERRIDE_DEFAULT_SPACE - rename the override_default_whitespace option to override_space - rename sss_replace_whitespaces() to sss_replace_space() - rename sss_reverse_replace_whitespaces() to sss_reverse_replace_space() - rename nctx->override_default_wsp_str to nctx->override_space - make the return value of sss_replace_space non-const to avoid freeing the result without compilation warnings Reviewed-by: Pavel Březina <pbrezina@redhat.com> Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
Diffstat (limited to 'src/util/string_utils.c')
-rw-r--r--src/util/string_utils.c137
1 files changed, 26 insertions, 111 deletions
diff --git a/src/util/string_utils.c b/src/util/string_utils.c
index 8d878923f..ec4cc687e 100644
--- a/src/util/string_utils.c
+++ b/src/util/string_utils.c
@@ -22,129 +22,44 @@
#include "util/util.h"
-const char * sss_replace_whitespaces(TALLOC_CTX *mem_ctx,
- const char *orig_name,
- const char *replace_string)
+static char *replace_char(TALLOC_CTX *mem_ctx,
+ const char *in,
+ const char match,
+ const char sub)
{
- char *new_name;
- const char *ptr;
- size_t replace_string_len;
- TALLOC_CTX *tmp_ctx;
+ char *p;
+ char *out;
- if (replace_string == NULL || replace_string[0] == '\0') {
- return orig_name;
- }
-
- replace_string_len = strlen(replace_string);
- /* faster implementations without multiple allocations */
- if (replace_string_len == 1) {
- char *p;
- new_name = talloc_strdup(mem_ctx, orig_name);
- if (new_name == NULL) {
- return NULL;
- }
-
- for (p = new_name; *p != '\0'; ++p) {
- if (isspace(*p)) {
- *p = replace_string[0];
- }
- }
-
- return new_name;
- }
-
- tmp_ctx = talloc_new(NULL);
- if (tmp_ctx == NULL) {
+ out = talloc_strdup(mem_ctx, in);
+ if (out == NULL) {
return NULL;
}
- new_name = talloc_strdup(tmp_ctx, "");
- if (new_name == NULL) {
- goto done;
- }
-
- ptr = orig_name;
- while (*ptr != '\0') {
- if (isspace(*ptr)) {
- new_name = talloc_asprintf_append(new_name, "%s", replace_string);
- } else {
- new_name = talloc_asprintf_append(new_name, "%c", *ptr);
- }
- if (new_name == NULL) {
- goto done;;
+ for (p = out; *p != '\0'; ++p) {
+ if (*p == match) {
+ *p = sub;
}
-
- ++ptr;
}
- new_name = talloc_steal(mem_ctx, new_name);
-done:
- talloc_free(tmp_ctx);
- return new_name;
+ return out;
}
-char * sss_reverse_replace_whitespaces(TALLOC_CTX *mem_ctx,
- char *orig_name,
- const char *replace_string)
+char * sss_replace_space(TALLOC_CTX *mem_ctx,
+ const char *orig_name,
+ const char subst)
{
- TALLOC_CTX *tmp_ctx;
- char *substr;
- char *new_name;
- const char *ptr = orig_name;
- size_t replace_string_len;
-
- if (replace_string == NULL || replace_string[0] == '\0') {
- return orig_name;
- }
-
- replace_string_len = strlen(replace_string);
- /* faster implementations without multiple allocations */
- if (replace_string_len == 1) {
- char *p;
- new_name = talloc_strdup(mem_ctx, orig_name);
- if (new_name == NULL) {
- return NULL;
- }
-
- for (p = new_name; *p != '\0'; ++p) {
- if (*p == replace_string[0] ) {
- *p = ' ';
- }
- }
-
- return new_name;
- }
-
- tmp_ctx = talloc_new(NULL);
- if (tmp_ctx == NULL) {
- return NULL;
+ if (subst == '\0') {
+ return talloc_strdup(mem_ctx, orig_name);
}
+ return replace_char(mem_ctx, orig_name, ' ', subst);
+}
- new_name = talloc_strdup(tmp_ctx, "");
- if (new_name == NULL) {
- goto done;
+char * sss_reverse_replace_space(TALLOC_CTX *mem_ctx,
+ char *orig_name,
+ const char subst)
+{
+ if (subst == '\0') {
+ return talloc_strdup(mem_ctx, orig_name);
}
-
- do {
- substr = strstr(ptr, replace_string);
- if (substr != NULL) {
- new_name = talloc_asprintf_append(new_name, "%.*s ",
- (int)(substr - ptr), ptr);
- if (new_name == NULL) {
- goto done;
- }
- ptr += substr - ptr;
- ptr += replace_string_len;
- } else {
- new_name = talloc_asprintf_append(new_name, "%s", ptr);
- if (new_name == NULL) {
- goto done;
- }
- }
- } while (substr != NULL);
-
- new_name = talloc_steal(mem_ctx, new_name);
-done:
- talloc_free(tmp_ctx);
- return new_name;
+ return replace_char(mem_ctx, orig_name, subst, ' ');
}