1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-09 22:41:56 +03:00

Refactor logic to check for ASCII-only characters in string

The same logic was present for collation commands, SASLprep and
pgcrypto, so this removes some code.

Author: Michael Paquier
Reviewed-by: Stephen Frost, Heikki Linnakangas
Discussion: https://postgr.es/m/X9womIn6rne6Gud2@paquier.xyz
This commit is contained in:
Michael Paquier
2020-12-21 09:37:11 +09:00
parent 4e1ee79e31
commit 93e8ff8701
5 changed files with 26 additions and 52 deletions

View File

@ -26,6 +26,7 @@
#endif
#include "common/saslprep.h"
#include "common/string.h"
#include "common/unicode_norm.h"
#include "mb/pg_wchar.h"
@ -47,7 +48,6 @@
static int codepoint_range_cmp(const void *a, const void *b);
static bool is_code_in_table(pg_wchar code, const pg_wchar *map, int mapsize);
static int pg_utf8_string_len(const char *source);
static bool pg_is_ascii_string(const char *p);
/*
* Stringprep Mapping Tables.
@ -1019,21 +1019,6 @@ pg_utf8_string_len(const char *source)
return num_chars;
}
/*
* Returns true if the input string is pure ASCII.
*/
static bool
pg_is_ascii_string(const char *p)
{
while (*p)
{
if (IS_HIGHBIT_SET(*p))
return false;
p++;
}
return true;
}
/*
* pg_saslprep - Normalize a password with SASLprep.
@ -1076,7 +1061,7 @@ pg_saslprep(const char *input, char **output)
* Quick check if the input is pure ASCII. An ASCII string requires no
* further processing.
*/
if (pg_is_ascii_string(input))
if (pg_is_ascii(input))
{
*output = STRDUP(input);
if (!(*output))

View File

@ -92,6 +92,22 @@ pg_clean_ascii(char *str)
}
/*
* pg_is_ascii -- Check if string is made only of ASCII characters
*/
bool
pg_is_ascii(const char *str)
{
while (*str)
{
if (IS_HIGHBIT_SET(*str))
return false;
str++;
}
return true;
}
/*
* pg_strip_crlf -- Remove any trailing newline and carriage return
*