Skip to content

Commit

Permalink
[CBRD-25043] refactor functions related to isspace, tolower, and toup…
Browse files Browse the repository at this point in the history
…per (#4932)

http://jira.cubrid.org/browse/CBRD-25043

* Refactor the functions in the following list to inline.
  - char_islower, char_isupper, char_isalpha, char_isdigit, char_isalnum,
  - char_isspace, char_iseol, char_isxdigit, char_tolower, char_toupper,
  - char_isupper_iso8859, char_islower_iso8859, char_tolower_iso8859, char_toupper_iso8859
* Each function can immediately check and obtain values from a pre-prepared array.
Conflicts:
	src/base/hide_password.cpp
	src/base/system_parameter.h
	win/cubridcs/cubridcs.def
  • Loading branch information
ctshim authored and mhoh3963 committed Aug 6, 2024
1 parent 5d130f0 commit c491e69
Show file tree
Hide file tree
Showing 23 changed files with 355 additions and 328 deletions.
374 changes: 216 additions & 158 deletions src/base/chartype.c

Large diffs are not rendered by default.

120 changes: 103 additions & 17 deletions src/base/chartype.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,119 @@

#ident "$Id$"

#include <assert.h>

#ifdef __cplusplus
extern "C"
{
#endif

extern int char_islower (int c);
extern int char_isupper (int c);
extern int char_isalpha (int c);
extern int char_isdigit (int c);
extern int char_isalnum (int c);
extern int char_isspace (int c);
extern int char_iseol (int c);
extern int char_isxdigit (int c);
#if defined (ENABLE_UNUSED_FUNCTION)
extern int char_isascii (int c);
#define CHAR_PROP_NONE (0x00)
#define CHAR_PROP_UPPER (0x01) /* uppercase. */
#define CHAR_PROP_LOWER (0x02) /* lowercase. */
#define CHAR_PROP_DIGIT (0x04) /* Numeric. */
#define CHAR_PROP_SPACE (0x08) /* space, \t \n \r \f \v */
#define CHAR_PROP_HEXNUM (0x10) /* 0~9, a~f, A~F */
#define CHAR_PROP_EOL (0x20) /* \r \n */
#define CHAR_PROP_ISO8859_UPPER (0x40)
#define CHAR_PROP_ISO8859_LOWER (0x80)

#define CHAR_PROP_ALPHA (CHAR_PROP_UPPER | CHAR_PROP_LOWER) /* Alphabetic. */
#define CHAR_PROP_ALPHA_NUM (CHAR_PROP_ALPHA | CHAR_PROP_DIGIT) /* Alpha-Numeric */

typedef unsigned char char_type_prop;
extern const char_type_prop *char_properties_ptr;
extern const unsigned char *char_lower_mapper_ptr;
extern const unsigned char *char_upper_mapper_ptr;
extern const unsigned char *iso8859_lower_mapper_ptr;
extern const unsigned char *iso8859_upper_mapper_ptr;

#ifndef NDEBUG
#define CHECK_OVER_CODE_VALUE(c) assert((unsigned char)(c) < 256)
#else
#define CHECK_OVER_CODE_VALUE(c)
#endif

extern int char_tolower (int c);
extern int char_toupper (int c);
inline int char_isspace (int c)
{
CHECK_OVER_CODE_VALUE (c);
return (char_properties_ptr[(unsigned char) c] & CHAR_PROP_SPACE);
}
inline int char_isupper (int c)
{
CHECK_OVER_CODE_VALUE (c);
return (char_properties_ptr[(unsigned char) c] & CHAR_PROP_UPPER);
}
inline int char_tolower (int c)
{
CHECK_OVER_CODE_VALUE (c);
return ((int) char_lower_mapper_ptr[(unsigned char) c]);
}
inline int char_islower (int c)
{
CHECK_OVER_CODE_VALUE (c);
return (char_properties_ptr[(unsigned char) c] & CHAR_PROP_LOWER);
}
inline int char_isalpha (int c)
{
CHECK_OVER_CODE_VALUE (c);
return (char_properties_ptr[(unsigned char) c] & CHAR_PROP_ALPHA);
}
inline int char_isdigit (int c)
{
CHECK_OVER_CODE_VALUE (c);
return (char_properties_ptr[(unsigned char) c] & CHAR_PROP_DIGIT);
}
inline int char_isalnum (int c)
{
CHECK_OVER_CODE_VALUE (c);
return (char_properties_ptr[(unsigned char) c] & CHAR_PROP_ALPHA_NUM);
}
inline int char_iseol (int c)
{
CHECK_OVER_CODE_VALUE (c);
return (char_properties_ptr[(unsigned char) c] & CHAR_PROP_EOL);
}
inline int char_isxdigit (int c)
{
CHECK_OVER_CODE_VALUE (c);
return (char_properties_ptr[(unsigned char) c] & CHAR_PROP_HEXNUM);
}
inline int char_toupper (int c)
{
CHECK_OVER_CODE_VALUE (c);
return ((int) char_upper_mapper_ptr[(unsigned char) c]);
}

inline int char_tolower_iso8859 (int c)
{
CHECK_OVER_CODE_VALUE (c);
return ((int) iso8859_lower_mapper_ptr[(unsigned char) c]);
}
inline int char_toupper_iso8859 (int c)
{
CHECK_OVER_CODE_VALUE (c);
return ((int) iso8859_upper_mapper_ptr[(unsigned char) c]);
}
inline int char_islower_iso8859 (int c)
{
CHECK_OVER_CODE_VALUE (c);
return (char_properties_ptr[(unsigned char) c] & (CHAR_PROP_LOWER | CHAR_PROP_ISO8859_LOWER));
}
inline int char_isupper_iso8859 (int c)
{
CHECK_OVER_CODE_VALUE (c);
return (char_properties_ptr[(unsigned char) c] & (CHAR_PROP_UPPER | CHAR_PROP_ISO8859_UPPER));
}

/* In some codes, only four items (' ', '\t', '\n', '\r') were checked.
* In order to maintain its original form, it was given a different name. */
#define char_isspace2 char_isspace

extern int char_isupper_iso8859 (int c);
extern int char_islower_iso8859 (int c);
extern int char_tolower_iso8859 (int c);
extern int char_toupper_iso8859 (int c);
extern char *trim (char *str);

#ifdef __cplusplus
}
#endif

#endif /* _CHARTYPE_H_ */
#endif /* _CHARTYPE_H_ */
23 changes: 3 additions & 20 deletions src/base/intl_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@
&& ((unsigned char) ch <= (unsigned char) 0xfe) )
#endif

#define CHAR_BYTE_TO_LOWER(c) ((c) + ('a' - 'A'))

#define CHAR_BYTE_TO_UPPER(c) ((c) - ('a' - 'A'))

/* conversion from turkish ISO 8859-9 to UTF-8 */
#define ISO_8859_9_FIRST_CP 0x11e
#define ISO_8859_9_LAST_CP 0x15f
Expand Down Expand Up @@ -727,10 +723,7 @@ intl_tolower_iso8859 (unsigned char *s, int length)

for (end = s + length; s < end; s++)
{
if (char_isupper_iso8859 (*s))
{
*s = CHAR_BYTE_TO_LOWER (*s);
}
*s = char_tolower_iso8859 (*s);
}

return char_count;
Expand All @@ -753,10 +746,7 @@ intl_toupper_iso8859 (unsigned char *s, int length)

for (end = s + length; s < end; s++)
{
if (char_islower_iso8859 (*s))
{
*s = CHAR_BYTE_TO_UPPER (*s);
}
*s = char_toupper_iso8859 (*s);
}

return char_count;
Expand Down Expand Up @@ -3257,14 +3247,7 @@ intl_identifier_mht_1strlowerhash (const void *key, const unsigned int ht_size)
case INTL_CODESET_ISO88591:
for (hash = 0; *byte_p; byte_p++)
{
if (char_isupper_iso8859 (*byte_p))
{
ch = char_tolower_iso8859 (*byte_p);
}
else
{
ch = char_tolower (*byte_p);
}
ch = char_tolower_iso8859 (*byte_p);
hash = (hash << 5) - hash + ch;
}
break;
Expand Down
30 changes: 0 additions & 30 deletions src/base/porting.c
Original file line number Diff line number Diff line change
Expand Up @@ -2257,36 +2257,6 @@ port_close_memstream (FILE * fp, char **ptr, size_t * sizeloc)
}
}

char *
trim (char *str)
{
char *p;
char *s;

if (str == NULL)
return (str);

for (s = str; *s != '\0' && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'); s++)
;
if (*s == '\0')
{
*str = '\0';
return (str);
}

/* *s must be a non-white char */
for (p = s; *p != '\0'; p++)
;
for (p--; *p == ' ' || *p == '\t' || *p == '\n' || *p == '\r'; p--)
;
*++p = '\0';

if (s != str)
memmove (str, s, strlen (s) + 1);

return (str);
}

int
parse_int (int *ret_p, const char *str_p, int base)
{
Expand Down
2 changes: 0 additions & 2 deletions src/base/porting.h
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,6 @@ extern FILE *port_open_memstream (char **ptr, size_t * sizeloc);

extern void port_close_memstream (FILE * fp, char **ptr, size_t * sizeloc);

extern char *trim (char *str);

extern int parse_bigint (INT64 * ret_p, const char *str_p, int base);

extern int str_to_int32 (int *ret_p, char **end_p, const char *str_p, int base);
Expand Down
2 changes: 2 additions & 0 deletions src/base/system_parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "error_manager.h"
#include "error_code.h"
#include "porting.h"
#include "porting_inline.hpp"
#include "chartype.h"

typedef enum
{
Expand Down
2 changes: 1 addition & 1 deletion src/broker/broker_log_sql_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ sql_change_comp_form (char *src, char *dest)

for (p = src, q = dest; *p; p++)
{
if (*p == '\r' || *p == '\n' || *p == '\t' || *p == ' ')
if (char_isspace2 (*p))
{
if (write_space_flag)
{
Expand Down
33 changes: 1 addition & 32 deletions src/broker/broker_log_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,6 @@

static bool is_bind_with_size (char *buf, int *tot_val_size, int *info_size);

char *
ut_trim (char *str)
{
char *p;
char *s;

if (str == NULL)
return (str);

for (s = str; *s != '\0' && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'); s++)
;
if (*s == '\0')
{
*str = '\0';
return (str);
}

/* *s must be a non-white char */
for (p = s; *p != '\0'; p++)
;
for (p--; *p == ' ' || *p == '\t' || *p == '\n' || *p == '\r'; p--)
;
*++p = '\0';

if (s != str)
memmove (str, s, strlen (s) + 1);

return (str);
}

void
ut_tolower (char *str)
{
Expand All @@ -79,8 +49,7 @@ ut_tolower (char *str)

for (p = str; *p; p++)
{
if (*p >= 'A' && *p <= 'Z')
*p = *p - 'A' + 'a';
*p = char_tolower (*p);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/broker/broker_log_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
BUF[DATE_STR_LEN] = '\0'; \
} while (0)

extern char *ut_trim (char *);
#define ut_trim trim
extern void ut_tolower (char *str);
extern int ut_get_line (FILE * fp, T_STRING * t_str, char **out_str, int *lineno);
extern int is_cas_log (char *str);
Expand Down
2 changes: 1 addition & 1 deletion src/broker/broker_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ ut_access_log (int as_index, struct timeval *start_time, char error_flag, int er
clt_appl = (char *) "-";
for (p = clt_appl; *p; p++)
{
if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
if (char_isspace2 (*p))
*p = '_';
}

Expand Down
1 change: 1 addition & 0 deletions src/broker/cas_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#endif

#include "porting.h"
#include "chartype.h"
#define makestring1(x) #x
#define makestring(x) makestring1(x)

Expand Down
34 changes: 1 addition & 33 deletions src/broker/cas_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,6 @@ ut_uchar2ipstr (unsigned char *ip_addr)
return (ip_str);
}

char *
ut_trim (char *str)
{
char *p;
char *s;

if (str == NULL)
return (str);

for (s = str; *s != '\0' && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'); s++)
;
if (*s == '\0')
{
*str = '\0';
return (str);
}

/* *s must be a non-white char */
for (p = s; *p != '\0'; p++)
;
for (p--; *p == ' ' || *p == '\t' || *p == '\n' || *p == '\r'; p--)
;
*++p = '\0';

if (s != str)
memmove (str, s, strlen (s) + 1);

return (str);
}


void
ut_tolower (char *str)
{
Expand All @@ -92,8 +61,7 @@ ut_tolower (char *str)

for (p = str; *p; p++)
{
if (*p >= 'A' && *p <= 'Z')
*p = *p - 'A' + 'a';
*p = char_tolower (*p);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/broker/cas_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
#include <windows.h>
#endif

#define ut_trim trim

extern char *ut_uchar2ipstr (unsigned char *ip_addr);
extern char *ut_trim (char *);
extern void ut_tolower (char *str);
extern void ut_timeval_diff (struct timeval *start, struct timeval *end, int *res_sec, int *res_msec);
extern int ut_check_timeout (struct timeval *start_time, struct timeval *end_time, int timeout_msec, int *res_sec,
Expand Down
2 changes: 1 addition & 1 deletion src/broker/shard_proxy_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ proxy_access_log (struct timeval *start_time, int client_ip_addr, const char *db

for (p = clt_appl; *p; p++)
{
if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
if (char_isspace2 (*p))
*p = '_';
}

Expand Down
Loading

0 comments on commit c491e69

Please sign in to comment.