summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndrew Bartlett <abartlet@samba.org>2011-04-08 13:04:26 +1000
committerAndrew Tridgell <tridge@samba.org>2011-04-13 14:47:07 +1000
commit17ccff973a4136c6bffa2fce68eeb1be53add447 (patch)
tree17bedd41f68431afc7a12397766b19fe5033a5f1 /lib
parentb2e37d9ce12627883ff18ab22ed9d3b6233f6baf (diff)
downloadsamba-17ccff973a4136c6bffa2fce68eeb1be53add447.tar.gz
samba-17ccff973a4136c6bffa2fce68eeb1be53add447.tar.bz2
samba-17ccff973a4136c6bffa2fce68eeb1be53add447.zip
lib/util: Make string_replace from s3 common
The s4 implementation didn't do multibyte strings, so was only good for '/' which is known to be safe in all multibyte charsets. Andrew Bartlett Signed-off-by: Andrew Tridgell <tridge@samba.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/util/util_str.c10
-rw-r--r--lib/util/util_str_common.c43
2 files changed, 43 insertions, 10 deletions
diff --git a/lib/util/util_str.c b/lib/util/util_str.c
index 7fa531f0e4..cf1b07ff0f 100644
--- a/lib/util/util_str.c
+++ b/lib/util/util_str.c
@@ -249,13 +249,3 @@ _PUBLIC_ bool strequal(const char *s1, const char *s2)
return strcasecmp(s1,s2) == 0;
}
-/**
- String replace.
-**/
-_PUBLIC_ void string_replace(char *s, char oldc, char newc)
-{
- while (*s) {
- if (*s == oldc) *s = newc;
- s++;
- }
-}
diff --git a/lib/util/util_str_common.c b/lib/util/util_str_common.c
index 9999ee440e..e6671be8ad 100644
--- a/lib/util/util_str_common.c
+++ b/lib/util/util_str_common.c
@@ -59,3 +59,46 @@ _PUBLIC_ size_t ucs2_align(const void *base_ptr, const void *p, int flags)
return 0;
return PTR_DIFF(p, base_ptr) & 1;
}
+
+/**
+ String replace.
+ NOTE: oldc and newc must be 7 bit characters
+**/
+void string_replace( char *s, char oldc, char newc )
+{
+ char *p;
+
+ /* this is quite a common operation, so we want it to be
+ fast. We optimise for the ascii case, knowing that all our
+ supported multi-byte character sets are ascii-compatible
+ (ie. they match for the first 128 chars) */
+
+ for (p = s; *p; p++) {
+ if (*p & 0x80) /* mb string - slow path. */
+ break;
+ if (*p == oldc) {
+ *p = newc;
+ }
+ }
+
+ if (!*p)
+ return;
+
+ /* Slow (mb) path. */
+#ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
+ /* With compose characters we must restart from the beginning. JRA. */
+ p = s;
+#endif
+
+ while (*p) {
+ size_t c_size;
+ next_codepoint(p, &c_size);
+
+ if (c_size == 1) {
+ if (*p == oldc) {
+ *p = newc;
+ }
+ }
+ p += c_size;
+ }
+}