1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-10 01:18:15 +03:00

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>
This commit is contained in:
Andrew Bartlett 2011-04-08 13:04:26 +10:00 committed by Andrew Tridgell
parent b2e37d9ce1
commit 17ccff973a
3 changed files with 43 additions and 53 deletions

View File

@ -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++;
}
}

View File

@ -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;
}
}

View File

@ -233,49 +233,6 @@ bool strisnormal(const char *s, int case_default)
}
/**
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;
}
}
/**
* Skip past some strings in a buffer - old version - no checks.
* **/