forked from github/server
inline optimizations for string functions
a little more speed for locale_string (less strcmp)
This commit is contained in:
parent
9a343df8fc
commit
e119a29142
6 changed files with 65 additions and 60 deletions
|
@ -2,11 +2,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "bsdstring.h"
|
#include "bsdstring.h"
|
||||||
|
|
||||||
#ifndef HAVE_INLINE
|
|
||||||
# undef INLINE_FUNCTION
|
|
||||||
# define INLINE_FUNCTION
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(HAVE_STRLCPY)
|
#if !defined(HAVE_STRLCPY)
|
||||||
INLINE_FUNCTION size_t
|
INLINE_FUNCTION size_t
|
||||||
strlcpy(char *dst, const char *src, size_t siz) /* copied from OpenBSD source code */
|
strlcpy(char *dst, const char *src, size_t siz) /* copied from OpenBSD source code */
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
#ifndef UTIL_BSDSTRING_H
|
#ifndef UTIL_BSDSTRING_H
|
||||||
#define UTIL_BSDSTRING_H
|
#define UTIL_BSDSTRING_H
|
||||||
|
|
||||||
#ifndef NDEBUG
|
|
||||||
# undef HAVE_INLINE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(HAVE_STRLCPY)
|
#if !defined(HAVE_STRLCPY)
|
||||||
# ifdef HAVE_INLINE
|
# ifdef HAVE_INLINE
|
||||||
# include "bsdstring.c"
|
# include "bsdstring.c"
|
||||||
|
|
|
@ -57,38 +57,6 @@ intlist_find(int *i_p, int fi)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
|
||||||
hashstring(const char* s)
|
|
||||||
{
|
|
||||||
unsigned int key = 0;
|
|
||||||
while (*s) {
|
|
||||||
key = key*37 + *s++;
|
|
||||||
}
|
|
||||||
return key % 0x7FFFFFFF;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *
|
|
||||||
escape_string(const char * str, char * buffer, unsigned int len)
|
|
||||||
{
|
|
||||||
static char s_buffer[4096];
|
|
||||||
const char * p = str;
|
|
||||||
char * o;
|
|
||||||
if (buffer==NULL) {
|
|
||||||
buffer = s_buffer;
|
|
||||||
len = sizeof(s_buffer);
|
|
||||||
}
|
|
||||||
o = buffer;
|
|
||||||
do {
|
|
||||||
switch (*p) {
|
|
||||||
case '\"':
|
|
||||||
case '\\':
|
|
||||||
(*o++) = '\\';
|
|
||||||
}
|
|
||||||
(*o++) = (*p);
|
|
||||||
} while (*p++);
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
char *
|
||||||
set_string (char **s, const char *neu)
|
set_string (char **s, const char *neu)
|
||||||
{
|
{
|
||||||
|
|
|
@ -21,9 +21,14 @@ extern "C" {
|
||||||
extern int *intlist_init(void);
|
extern int *intlist_init(void);
|
||||||
extern int *intlist_add(int *i_p, int i);
|
extern int *intlist_add(int *i_p, int i);
|
||||||
extern int *intlist_find(int *i_p, int i);
|
extern int *intlist_find(int *i_p, int i);
|
||||||
extern unsigned int hashstring(const char* s);
|
|
||||||
|
|
||||||
|
#ifdef HAVE_INLINE
|
||||||
|
# include "strings.c"
|
||||||
|
#else
|
||||||
|
extern unsigned int hashstring(const char* s);
|
||||||
extern const char *escape_string(const char * str, char * buffer, unsigned int len);
|
extern const char *escape_string(const char * str, char * buffer, unsigned int len);
|
||||||
|
#endif
|
||||||
|
|
||||||
extern boolean locale_check(void);
|
extern boolean locale_check(void);
|
||||||
|
|
||||||
extern int set_email(char** pemail, const char *newmail);
|
extern int set_email(char** pemail, const char *newmail);
|
||||||
|
|
|
@ -91,8 +91,7 @@ locale_getstring(const locale * lang, const char * key)
|
||||||
const char *
|
const char *
|
||||||
locale_string(const locale * lang, const char * key)
|
locale_string(const locale * lang, const char * key)
|
||||||
{
|
{
|
||||||
if (key==NULL) return NULL;
|
if (key!=NULL) {
|
||||||
else {
|
|
||||||
unsigned int hkey = hashstring(key);
|
unsigned int hkey = hashstring(key);
|
||||||
unsigned int id = hkey & (SMAXHASH-1);
|
unsigned int id = hkey & (SMAXHASH-1);
|
||||||
struct locale_str * find;
|
struct locale_str * find;
|
||||||
|
@ -101,7 +100,14 @@ locale_string(const locale * lang, const char * key)
|
||||||
if (lang == NULL) return key;
|
if (lang == NULL) return key;
|
||||||
find = lang->strings[id];
|
find = lang->strings[id];
|
||||||
while (find) {
|
while (find) {
|
||||||
if (find->hashkey == hkey && !strcmp(key, find->key)) break;
|
if (find->hashkey == hkey) {
|
||||||
|
if (find->nexthash==NULL) {
|
||||||
|
/* if this is the only entry with this hash, fine. */
|
||||||
|
assert(strcmp(key, find->key)==0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (strcmp(key, find->key)==0) break;
|
||||||
|
}
|
||||||
find = find->nexthash;
|
find = find->nexthash;
|
||||||
}
|
}
|
||||||
if (!find) {
|
if (!find) {
|
||||||
|
@ -122,30 +128,22 @@ locale_string(const locale * lang, const char * key)
|
||||||
}
|
}
|
||||||
return find->str;
|
return find->str;
|
||||||
}
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
locale_setstring(locale * lang, const char * key, const char * value)
|
locale_setstring(locale * lang, const char * key, const char * value)
|
||||||
{
|
{
|
||||||
int nval = atoi(key);
|
unsigned int hkey = hashstring(key);
|
||||||
unsigned int hkey = nval?nval:hashstring(key);
|
|
||||||
unsigned int id = hkey & (SMAXHASH-1);
|
unsigned int id = hkey & (SMAXHASH-1);
|
||||||
struct locale_str * find;
|
struct locale_str * find;
|
||||||
static int maxs = 0, totals = 0;
|
|
||||||
int si=0;
|
|
||||||
if (lang==NULL) lang=default_locale;
|
if (lang==NULL) lang=default_locale;
|
||||||
find = lang->strings[id];
|
find = lang->strings[id];
|
||||||
while (find) {
|
while (find) {
|
||||||
++si;
|
if (find->hashkey==hkey && strcmp(key, find->key)==0) break;
|
||||||
if (find->hashkey==hkey && !strcmp(key, find->key)) break;
|
find = find->nexthash;
|
||||||
find=find->nexthash;
|
|
||||||
}
|
}
|
||||||
if (!find) {
|
if (!find) {
|
||||||
++totals;
|
|
||||||
if (si>=maxs) {
|
|
||||||
maxs=si+1;
|
|
||||||
printf("max hash conflicts: %d/%d\n", maxs, totals);
|
|
||||||
}
|
|
||||||
find = calloc(1, sizeof(struct locale_str));
|
find = calloc(1, sizeof(struct locale_str));
|
||||||
find->nexthash = lang->strings[id];
|
find->nexthash = lang->strings[id];
|
||||||
lang->strings[id] = find;
|
lang->strings[id] = find;
|
||||||
|
|
|
@ -251,15 +251,58 @@
|
||||||
RelativePath=".\xml.h">
|
RelativePath=".\xml.h">
|
||||||
</File>
|
</File>
|
||||||
</Filter>
|
</Filter>
|
||||||
|
<Filter
|
||||||
|
Name="Inline"
|
||||||
|
Filter="">
|
||||||
|
<File
|
||||||
|
RelativePath=".\bsdstring.c">
|
||||||
|
<FileConfiguration
|
||||||
|
Name="Release|Win32"
|
||||||
|
ExcludedFromBuild="TRUE">
|
||||||
|
<Tool
|
||||||
|
Name="VCCLCompilerTool"/>
|
||||||
|
</FileConfiguration>
|
||||||
|
<FileConfiguration
|
||||||
|
Name="Profile|Win32"
|
||||||
|
ExcludedFromBuild="TRUE">
|
||||||
|
<Tool
|
||||||
|
Name="VCCLCompilerTool"/>
|
||||||
|
</FileConfiguration>
|
||||||
|
<FileConfiguration
|
||||||
|
Name="Debug|Win32"
|
||||||
|
ExcludedFromBuild="TRUE">
|
||||||
|
<Tool
|
||||||
|
Name="VCCLCompilerTool"/>
|
||||||
|
</FileConfiguration>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\strings.c">
|
||||||
|
<FileConfiguration
|
||||||
|
Name="Release|Win32"
|
||||||
|
ExcludedFromBuild="TRUE">
|
||||||
|
<Tool
|
||||||
|
Name="VCCLCompilerTool"/>
|
||||||
|
</FileConfiguration>
|
||||||
|
<FileConfiguration
|
||||||
|
Name="Profile|Win32"
|
||||||
|
ExcludedFromBuild="TRUE">
|
||||||
|
<Tool
|
||||||
|
Name="VCCLCompilerTool"/>
|
||||||
|
</FileConfiguration>
|
||||||
|
<FileConfiguration
|
||||||
|
Name="Debug|Win32"
|
||||||
|
ExcludedFromBuild="TRUE">
|
||||||
|
<Tool
|
||||||
|
Name="VCCLCompilerTool"/>
|
||||||
|
</FileConfiguration>
|
||||||
|
</File>
|
||||||
|
</Filter>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\attrib.c">
|
RelativePath=".\attrib.c">
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\base36.c">
|
RelativePath=".\base36.c">
|
||||||
</File>
|
</File>
|
||||||
<File
|
|
||||||
RelativePath=".\bsdstring.c">
|
|
||||||
</File>
|
|
||||||
<File
|
<File
|
||||||
RelativePath=".\command.c">
|
RelativePath=".\command.c">
|
||||||
</File>
|
</File>
|
||||||
|
|
Loading…
Reference in a new issue