1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-23 17:34:34 +03:00

More tuning from cachegrind. Change most trim_string() calls to trim_char(0,

as that's what they do. Fix string_replace() to fast-path ascii.
Jeremy.
(This used to be commit f35e9a8b90)
This commit is contained in:
Jeremy Allison 2003-09-05 19:59:55 +00:00
parent 7544b0c773
commit 94f59f5492
26 changed files with 130 additions and 119 deletions

View File

@ -373,10 +373,10 @@ BOOL load_auth_module(struct auth_context *auth_context,
if (p) {
*p = 0;
module_params = p+1;
trim_string(module_params, " ", " ");
trim_char(module_params, ' ', ' ');
}
trim_string(module_name, " ", " ");
trim_char(module_name, ' ', ' ');
entry = auth_find_backend_entry(module_name);

View File

@ -40,7 +40,7 @@ static BOOL check_user_equiv(const char *user, const char *remote, const char *e
if (! lines) return False;
for (i=0; lines[i]; i++) {
char *buf = lines[i];
trim_string(buf," "," ");
trim_char(buf,' ',' ');
if (buf[0] != '#' && buf[0] != '\n')
{

View File

@ -230,7 +230,7 @@ static struct chat_struct *make_pw_chat(char *p)
special_char_sub(prompt);
fstrcpy(t->prompt, prompt);
strlower_m(t->prompt);
trim_string(t->prompt, " ", " ");
trim_char(t->prompt, ' ', ' ');
if (!next_token(&p, reply, NULL, sizeof(fstring)))
break;
@ -241,7 +241,7 @@ static struct chat_struct *make_pw_chat(char *p)
special_char_sub(reply);
fstrcpy(t->reply, reply);
strlower_m(t->reply);
trim_string(t->reply, " ", " ");
trim_char(t->reply, ' ', ' ');
}
return list;
@ -304,7 +304,7 @@ static int smb_pam_passchange_conv(int num_msg,
case PAM_PROMPT_ECHO_ON:
DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_ON: PAM said: %s\n", msg[replies]->msg));
fstrcpy(current_prompt, msg[replies]->msg);
trim_string(current_prompt, " ", " ");
trim_char(current_prompt, ' ', ' ');
for (t=pw_chat; t; t=t->next) {
DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_ON: trying to match |%s| to |%s|\n",
@ -335,7 +335,7 @@ static int smb_pam_passchange_conv(int num_msg,
case PAM_PROMPT_ECHO_OFF:
DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_OFF: PAM said: %s\n", msg[replies]->msg));
fstrcpy(current_prompt, msg[replies]->msg);
trim_string(current_prompt, " ", " ");
trim_char(current_prompt, ' ', ' ');
for (t=pw_chat; t; t=t->next) {
DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_OFF: trying to match |%s| to |%s|\n",

View File

@ -1006,7 +1006,7 @@ static int cmd_mkdir(void)
*ddir2 = 0;
pstrcpy(ddir,mask);
trim_string(ddir,".",NULL);
trim_char(ddir,'.','\0');
p = strtok(ddir,"/\\");
while (p) {
pstrcat(ddir2,p);

View File

@ -53,8 +53,8 @@ static BOOL load_msg(const char *msg_file)
}
if (msgid && strncmp(lines[i], "msgstr \"", 8) == 0) {
msgstr = lines[i] + 8;
trim_string(msgid, NULL, "\"");
trim_string(msgstr, NULL, "\"");
trim_char(msgid, '\0', '\"');
trim_char(msgstr, '\0', '\"');
if (*msgstr == 0) {
msgstr = msgid;
}

View File

@ -879,9 +879,8 @@ size_t pull_ucs2(const void *base_ptr, char *dest, const void *src, size_t dest_
}
if (flags & STR_TERMINATE) {
if (src_len == (size_t)-1) {
src_len = strlen_w(src)*2 + 2;
} else {
/* src_len -1 is the default for null terminated strings. */
if (src_len != (size_t)-1) {
size_t len = strnlen_w(src, src_len/2);
if (len < src_len/2)
len++;
@ -1051,4 +1050,3 @@ size_t align_string(const void *base_ptr, const char *p, int flags)
}
return 0;
}

View File

@ -57,7 +57,7 @@ void set_local_machine_name(const char* local_name, BOOL perm)
already_perm = perm;
fstrcpy(tmp_local_machine,local_name);
trim_string(tmp_local_machine," "," ");
trim_char(tmp_local_machine,' ',' ');
alpha_strcpy(local_machine,tmp_local_machine,SAFE_NETBIOS_CHARS,sizeof(local_machine)-1);
strlower_m(local_machine);
}
@ -79,7 +79,7 @@ void set_remote_machine_name(const char* remote_name, BOOL perm)
already_perm = perm;
fstrcpy(tmp_remote_machine,remote_name);
trim_string(tmp_remote_machine," "," ");
trim_char(tmp_remote_machine,' ',' ');
alpha_strcpy(remote_machine,tmp_remote_machine,SAFE_NETBIOS_CHARS,sizeof(remote_machine)-1);
strlower_m(remote_machine);
}
@ -111,7 +111,7 @@ void sub_set_smb_name(const char *name)
return;
fstrcpy(tmp,name);
trim_string(tmp," "," ");
trim_char(tmp,' ',' ');
strlower_m(tmp);
alpha_strcpy(smb_user_name,tmp,SAFE_NETBIOS_CHARS,sizeof(smb_user_name)-1);
}

View File

@ -261,7 +261,7 @@ BOOL init_names(void)
}
fstrcpy( local_machine, global_myname() );
trim_string( local_machine, " ", " " );
trim_char( local_machine, ' ', ' ' );
p = strchr( local_machine, ' ' );
if (p)
*p = 0;
@ -605,68 +605,6 @@ void unix_clean_name(char *s)
trim_string(s,NULL,"/..");
}
/*******************************************************************
Convert '\' to '/'.
Reduce a file name, removing or reducing /../ , /./ , // elements.
Remove also any trailing . and /
Return a new allocated string.
********************************************************************/
smb_ucs2_t *unix_clean_path(const smb_ucs2_t *s)
{
smb_ucs2_t *ns;
smb_ucs2_t *p, *r, *t;
DEBUG(3, ("unix_clean_path\n")); /* [%unicode]\n")); */
if(!s)
return NULL;
/* convert '\' to '/' */
ns = strdup_w(s);
if (!ns)
return NULL;
unix_format_w(ns);
/* remove all double slashes */
p = ns;
ns = all_string_sub_wa(p, "//", "/");
SAFE_FREE(p);
if (!ns)
return NULL;
/* remove any /./ */
p = ns;
ns = all_string_sub_wa(p, "/./", "/");
SAFE_FREE(p);
if (!ns)
return NULL;
/* reduce any /../ */
t = ns;
while (*t && (r = strstr_wa(t, "/.."))) {
t = &(r[3]);
if (*t == UCS2_CHAR('/') || *t == 0) {
*r = 0;
p = strrchr_w(ns, UCS2_CHAR('/'));
if (!p)
p = ns;
if (*t == 0)
*p = 0;
else
memmove(p, t, (strlen_w(t) + 1) * sizeof(smb_ucs2_t));
t = p;
}
}
/* remove any leading ./ trailing /. */
trim_string_wa(ns, "./", "/.");
/* remove any leading and trailing / */
trim_string_wa(ns, "/", "/");
return ns;
}
/****************************************************************************
Make a dir struct.
****************************************************************************/
@ -2205,7 +2143,7 @@ char *lock_path(const char *name)
static pstring fname;
pstrcpy(fname,lp_lockdir());
trim_string(fname,"","/");
trim_char(fname,'\0','/');
if (!directory_exist(fname,NULL))
mkdir(fname,0755);
@ -2225,7 +2163,7 @@ char *pid_path(const char *name)
static pstring fname;
pstrcpy(fname,lp_piddir());
trim_string(fname,"","/");
trim_char(fname,'\0','/');
if (!directory_exist(fname,NULL))
mkdir(fname,0755);

View File

@ -364,9 +364,27 @@ BOOL strisnormal(const char *s)
void string_replace(pstring s,char oldc,char newc)
{
push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
unsigned 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 = (unsigned char *)s; *p; p++) {
if (*p & 0x80) /* mb string - slow path. */
break;
if (*p == oldc)
*p = newc;
}
if (!*p)
return;
/* Slow (mb) path. */
push_ucs2(NULL, tmpbuf, p, sizeof(tmpbuf), STR_TERMINATE);
string_replace_w(tmpbuf, UCS2_CHAR(oldc), UCS2_CHAR(newc));
pull_ucs2(NULL, s, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE);
pull_ucs2(NULL, p, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE);
}
/**
@ -406,6 +424,59 @@ size_t str_ascii_charnum(const char *s)
return strlen(tmpbuf2);
}
BOOL trim_char(char *s,char cfront,char cback)
{
BOOL ret = False;
char *ep;
char *fp = s;
/* Ignore null or empty strings. */
if (!s || (s[0] == '\0'))
return False;
if (cfront) {
while (*fp && *fp == cfront)
fp++;
if (!*fp) {
/* We ate the string. */
s[0] = '\0';
return True;
}
if (fp != s)
ret = True;
}
ep = fp + strlen(fp) - 1;
if (cback) {
/* Attempt ascii only. Bail for mb strings. */
while ((ep >= fp) && (*ep == cback)) {
ret = True;
if ((ep > fp) && (((unsigned char)ep[-1]) & 0x80)) {
/* Could be mb... bail back to tim_string. */
char fs[2], bs[2];
if (cfront) {
fs[0] = cfront;
fs[1] = '\0';
}
bs[0] = cback;
bs[1] = '\0';
return trim_string(s, cfront ? fs : NULL, bs);
} else {
ep--;
}
}
if (ep < fp) {
/* We ate the string. */
s[0] = '\0';
return True;
}
}
ep[1] = '\0';
memmove(s, fp, ep-fp+2);
return ret;
}
/**
Trim the specified elements off the front and back of a string.
**/

View File

@ -951,8 +951,9 @@ BOOL cli_chkpath(struct cli_state *cli, const char *path)
char *p;
pstrcpy(path2,path);
trim_string(path2,NULL,"\\");
if (!*path2) *path2 = '\\';
trim_char(path2,'\0','\\');
if (!*path2)
*path2 = '\\';
memset(cli->outbuf,'\0',smb_size);
set_message(cli->outbuf,0,0,True);

View File

@ -61,7 +61,7 @@ static struct node_status *parse_node_status(char *p, int *num_names)
p++;
for (i=0;i< *num_names;i++) {
StrnCpy(ret[i].name,p,15);
trim_string(ret[i].name,NULL," ");
trim_char(ret[i].name,'\0',' ');
ret[i].type = CVAL(p,15);
ret[i].flags = p[16];
p += 18;

View File

@ -336,7 +336,7 @@ static int recycle_unlink(vfs_handle_struct *handle, connection_struct *conn, co
repository = alloc_sub_conn(conn, recycle_repository(handle));
ALLOC_CHECK(repository, done);
/* shouldn't we allow absolute path names here? --metze */
trim_string(repository, "/", "/");
trim_char(repository, '/', '/');
if(!repository || *(repository) == '\0') {
DEBUG(3, ("recycle: repository path not set, purging %s...\n", file_name));

View File

@ -40,7 +40,7 @@ static BOOL parse_dfs_path(char* pathname, struct dfs_path* pdp)
ZERO_STRUCTP(pdp);
trim_string(temp,"\\","\\");
trim_char(temp,'\\','\\');
DEBUG(10,("temp in parse_dfs_path: .%s. after trimming \\'s\n",temp));
/* now tokenize */
@ -275,7 +275,7 @@ static BOOL resolve_dfs_path(char* dfspath, struct dfs_path* dp,
char *q;
pstring buf;
pstrcpy(buf, dfspath);
trim_string(buf, NULL, "\\");
trim_char(buf, '\0', '\\');
for (; consumed_level; consumed_level--) {
q = strrchr(buf, '\\');
if (q) *q = 0;
@ -780,7 +780,7 @@ BOOL create_msdfs_link(struct junction_map* jn, BOOL exists)
for(i=0; i<jn->referral_count; i++) {
char* refpath = jn->referral_list[i].alternate_path;
trim_string(refpath, "\\", "\\");
trim_char(refpath, '\\', '\\');
if(*refpath == '\0') {
if (i == 0)
insert_comma = False;

View File

@ -204,7 +204,7 @@ static void domain_master_node_status_success(struct subnet_record *subrec,
pull_ascii_nstring(qname, p);
name_type = CVAL(p,15);
nb_flags = get_nb_flags(&p[16]);
trim_string(qname,NULL," ");
trim_char(qname,'\0',' ');
p += 18;
@ -427,7 +427,7 @@ static void get_domain_master_name_node_status_success(struct subnet_record *sub
pull_ascii_nstring(qname, p);
name_type = CVAL(p,15);
nb_flags = get_nb_flags(&p[16]);
trim_string(qname,NULL," ");
trim_char(qname,'\0',' ');
p += 18;

View File

@ -328,7 +328,7 @@ void write_browse_list(time_t t, BOOL force_write)
updatecount++;
pstrcpy(fname,lp_lockdir());
trim_string(fname,NULL,"/");
trim_char(fname,'\0' ,'/');
pstrcat(fname,"/");
pstrcat(fname,SERVER_LIST);
pstrcpy(fnamenew,fname);

View File

@ -1558,8 +1558,8 @@ static char *lp_string(const char *s)
lp_talloc = talloc_init("lp_talloc");
tmpstr = alloc_sub_basic(current_user_info.smb_name, s);
if (trim_string(tmpstr, "\"", "\"")) {
if (strchr(tmpstr,'"') != NULL) {
if (trim_char(tmpstr, '\"', '\"')) {
if (strchr(tmpstr,'\"') != NULL) {
SAFE_FREE(tmpstr);
tmpstr = alloc_sub_basic(current_user_info.smb_name,s);
}
@ -3112,7 +3112,7 @@ BOOL lp_do_parameter(int snum, const char *pszParmName, const char *pszParmValue
pstr_sprintf(param_key, "%s:", pszParmName);
slen = strlen(param_key);
pstrcat(param_key, sep+1);
trim_string(param_key+slen, " ", " ");
trim_char(param_key+slen, ' ', ' ');
not_added = True;
data = (snum < 0) ? Globals.param_opt :
ServicePtrs[snum]->param_opt;

View File

@ -422,10 +422,10 @@ static NTSTATUS make_pdb_methods_name(struct pdb_methods **methods, struct pdb_c
if (p) {
*p = 0;
module_location = p+1;
trim_string(module_location, " ", " ");
trim_char(module_location, ' ', ' ');
}
trim_string(module_name, " ", " ");
trim_char(module_name, ' ', ' ');
DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));

View File

@ -41,9 +41,11 @@ NTSTATUS pdb_init_plugin(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, con
if (p) {
*p = 0;
plugin_location = p+1;
trim_string(plugin_location, " ", " ");
} else plugin_location = NULL;
trim_string(plugin_name, " ", " ");
trim_char(plugin_location, ' ', ' ');
} else {
plugin_location = NULL;
}
trim_char(plugin_name, ' ', ' ');
DEBUG(5, ("Trying to load sam plugin %s\n", plugin_name));
dl_handle = sys_dlopen(plugin_name, RTLD_NOW );

View File

@ -766,14 +766,14 @@ static BOOL parse_lpq_nt(char *line,print_queue_struct *buf,BOOL first)
/* Make sure the status is valid */
parse_line.space2 = '\0';
trim_string(parse_line.status, NULL, " ");
trim_char(parse_line.status, '\0', ' ');
if (!strequal(parse_line.status, LPRNT_PRINTING) &&
!strequal(parse_line.status, LPRNT_PAUSED) &&
!strequal(parse_line.status, LPRNT_WAITING))
return(False);
parse_line.space3 = '\0';
trim_string(parse_line.jobname, NULL, " ");
trim_char(parse_line.jobname, '\0', ' ');
buf->job = atoi(parse_line.jobid);
buf->priority = 0;
@ -837,7 +837,7 @@ static BOOL parse_lpq_os2(char *line,print_queue_struct *buf,BOOL first)
/* Get the job name */
parse_line.space2[0] = '\0';
trim_string(parse_line.jobname, NULL, " ");
trim_char(parse_line.jobname, '\0', ' ');
fstrcpy(buf->fs_file, parse_line.jobname);
buf->priority = 0;
@ -850,7 +850,7 @@ static BOOL parse_lpq_os2(char *line,print_queue_struct *buf,BOOL first)
/* Make sure we have a valid status */
parse_line.space4[0] = '\0';
trim_string(parse_line.status, NULL, " ");
trim_char(parse_line.status, '\0', ' ');
if (!strequal(parse_line.status, LPROS2_PRINTING) &&
!strequal(parse_line.status, LPROS2_PAUSED) &&
!strequal(parse_line.status, LPROS2_WAITING))

View File

@ -2026,7 +2026,7 @@ static WERROR update_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info)
if (info->servername[0]!='\0') {
trim_string(info->printername, info->servername, NULL);
trim_string(info->printername, "\\", NULL);
trim_char(info->printername, '\\', '\0');
info->servername[0]='\0';
}

View File

@ -159,7 +159,7 @@ WERROR _dfs_remove(pipes_struct *p, DFS_Q_DFS_REMOVE *q_u,
{
pstring refpath;
pstrcpy(refpath,jn.referral_list[i].alternate_path);
trim_string(refpath, "\\", "\\");
trim_char(refpath, '\\', '\\');
DEBUG(10,("_dfs_remove: refpath: .%s.\n", refpath));
if(strequal(refpath, altpath))
{
@ -257,7 +257,7 @@ static BOOL init_reply_dfs_info_3(TALLOC_CTX *ctx, struct junction_map* j, DFS_I
struct referral* ref = &(j[i].referral_list[ii]);
pstrcpy(path, ref->alternate_path);
trim_string(path,"\\","");
trim_char(path,'\\','\0');
p = strrchr_m(path,'\\');
if(p==NULL)
{

View File

@ -259,7 +259,7 @@ static int expect(int master, char *issue, char *expected)
/* Eat leading/trailing whitespace before match. */
pstring str;
pstrcpy( str, buffer);
trim_string( str, " ", " ");
trim_char( str, ' ', ' ');
if ((match = (unix_wild_match(expected, str) == 0)))
timeout = 200;

View File

@ -125,7 +125,7 @@ BOOL unix_convert(pstring name,connection_struct *conn,char *saved_last_componen
* also trim trailing /'s.
*/
trim_string(name,"/","/");
trim_char(name,'/','/');
/*
* If we trimmed down to a single '\0' character
@ -164,7 +164,7 @@ BOOL unix_convert(pstring name,connection_struct *conn,char *saved_last_componen
return(True);
start = name;
while (strncmp(start,"./",2) == 0)
while (start[0] == '.' && start[1] == '/')
start += 2;
pstrcpy(orig_path, name);

View File

@ -66,11 +66,12 @@ void stat_cache_add( const char *full_orig_name, const char *orig_translated_pat
return;
/*
* Don't cache trivial valid directory entries.
* Don't cache trivial valid directory entries such as . and ..
*/
if((*full_orig_name == '\0') || (strcmp(full_orig_name, ".") == 0) ||
(strcmp(full_orig_name, "..") == 0))
if((*full_orig_name == '\0') || (full_orig_name[0] == '.' &&
((full_orig_name[1] == '\0') ||
(full_orig_name[1] == '.' && full_orig_name[1] == '\0'))))
return;
/*

View File

@ -217,13 +217,13 @@ static void uw_pathname(pstring fname, const char *uw_name, const char *uw_defau
/* For w-files, first look for explicit "wtmp dir" */
if (uw_name[0] == 'w') {
pstrcpy(dirname,lp_wtmpdir());
trim_string(dirname,"","/");
trim_char(dirname,'\0','/');
}
/* For u-files and non-explicit w-dir, look for "utmp dir" */
if (dirname == 0 || strlen(dirname) == 0) {
pstrcpy(dirname,lp_utmpdir());
trim_string(dirname,"","/");
trim_char(dirname,'\0','/');
}
/* If explicit directory above, use it */

View File

@ -233,10 +233,10 @@ BOOL vfs_init_custom(connection_struct *conn, const char *vfs_object)
if (p) {
*p = 0;
module_param = p+1;
trim_string(module_param, " ", " ");
trim_char(module_param, ' ', ' ');
}
trim_string(module_name, " ", " ");
trim_char(module_name, ' ', ' ');
/* First, try to load the module with the new module system */
if((entry = vfs_find_backend_entry(module_name)) ||