diff options
author | Michael Paquier <michael@paquier.xyz> | 2019-08-09 11:05:14 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2019-08-09 11:05:14 +0900 |
commit | b8f2da0ac5a24f669c8d320c58646759b8fc69a5 (patch) | |
tree | dc68a064354682b5916a87224ef325f1e83c1bbf /src/common/string.c | |
parent | 28b901f73a3924187988bfaac57d20e422a432c3 (diff) | |
download | postgresql-b8f2da0ac5a24f669c8d320c58646759b8fc69a5.tar.gz postgresql-b8f2da0ac5a24f669c8d320c58646759b8fc69a5.zip |
Refactor logic to remove trailing CR/LF characters from strings
b654714 has reworked the way trailing CR/LF characters are removed from
strings. This commit introduces a new routine in common/string.c and
refactors the code so as the logic is in a single place, mostly.
Author: Michael Paquier
Reviewed-by: Bruce Momjian
Discussion: https://postgr.es/m/20190801031820.GF29334@paquier.xyz
Diffstat (limited to 'src/common/string.c')
-rw-r--r-- | src/common/string.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/common/string.c b/src/common/string.c index b01a56ceaa5..c9b8482cb06 100644 --- a/src/common/string.c +++ b/src/common/string.c @@ -90,3 +90,25 @@ pg_clean_ascii(char *str) *p = '?'; } } + + +/* + * pg_strip_crlf -- Remove any trailing newline and carriage return + * + * Removes any trailing newline and carriage return characters (\r on + * Windows) in the input string, zero-terminating it. + * + * The passed in string must be zero-terminated. This function returns + * the new length of the string. + */ +int +pg_strip_crlf(char *str) +{ + int len = strlen(str); + + while (len > 0 && (str[len - 1] == '\n' || + str[len - 1] == '\r')) + str[--len] = '\0'; + + return len; +} |