mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-04-19 03:58:05 -05:00
Make a few small helper functions inline.
Notably toint(), which ought to compile down to the identity function in any case so you don't really want to put in a pointless call overhead, and make_ptrlen() (and a couple of its wrappers) which is standing in for what ought to be a struct-literal syntax.
This commit is contained in:
parent
3214563d8e
commit
f9cb4eb568
41
misc.c
41
misc.c
@ -349,29 +349,6 @@ void burnstr(char *string) /* sfree(str), only clear it first */
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int toint(unsigned u)
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* Convert an unsigned to an int, without running into the
|
|
||||||
* undefined behaviour which happens by the strict C standard if
|
|
||||||
* the value overflows. You'd hope that sensible compilers would
|
|
||||||
* do the sensible thing in response to a cast, but actually I
|
|
||||||
* don't trust modern compilers not to do silly things like
|
|
||||||
* assuming that _obviously_ you wouldn't have caused an overflow
|
|
||||||
* and so they can elide an 'if (i < 0)' test immediately after
|
|
||||||
* the cast.
|
|
||||||
*
|
|
||||||
* Sensible compilers ought of course to optimise this entire
|
|
||||||
* function into 'just return the input value'!
|
|
||||||
*/
|
|
||||||
if (u <= (unsigned)INT_MAX)
|
|
||||||
return (int)u;
|
|
||||||
else if (u >= (unsigned)INT_MIN) /* wrap in cast _to_ unsigned is OK */
|
|
||||||
return INT_MIN + (int)(u - (unsigned)INT_MIN);
|
|
||||||
else
|
|
||||||
return INT_MIN; /* fallback; should never occur on binary machines */
|
|
||||||
}
|
|
||||||
|
|
||||||
int string_length_for_printf(size_t s)
|
int string_length_for_printf(size_t s)
|
||||||
{
|
{
|
||||||
/* Truncate absurdly long strings (should one show up) to fit
|
/* Truncate absurdly long strings (should one show up) to fit
|
||||||
@ -1235,24 +1212,6 @@ int nullstrcmp(const char *a, const char *b)
|
|||||||
return strcmp(a, b);
|
return strcmp(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
ptrlen make_ptrlen(const void *ptr, size_t len)
|
|
||||||
{
|
|
||||||
ptrlen pl;
|
|
||||||
pl.ptr = ptr;
|
|
||||||
pl.len = len;
|
|
||||||
return pl;
|
|
||||||
}
|
|
||||||
|
|
||||||
ptrlen ptrlen_from_asciz(const char *str)
|
|
||||||
{
|
|
||||||
return make_ptrlen(str, strlen(str));
|
|
||||||
}
|
|
||||||
|
|
||||||
ptrlen ptrlen_from_strbuf(strbuf *sb)
|
|
||||||
{
|
|
||||||
return make_ptrlen(sb->u, sb->len);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ptrlen_eq_string(ptrlen pl, const char *str)
|
bool ptrlen_eq_string(ptrlen pl, const char *str)
|
||||||
{
|
{
|
||||||
size_t len = strlen(str);
|
size_t len = strlen(str);
|
||||||
|
46
misc.h
46
misc.h
@ -12,6 +12,7 @@
|
|||||||
#include <stdio.h> /* for FILE * */
|
#include <stdio.h> /* for FILE * */
|
||||||
#include <stdarg.h> /* for va_list */
|
#include <stdarg.h> /* for va_list */
|
||||||
#include <time.h> /* for struct tm */
|
#include <time.h> /* for struct tm */
|
||||||
|
#include <limits.h> /* for INT_MAX/MIN */
|
||||||
|
|
||||||
unsigned long parse_blocksize(const char *bs);
|
unsigned long parse_blocksize(const char *bs);
|
||||||
char ctrlparse(char *s, char **next);
|
char ctrlparse(char *s, char **next);
|
||||||
@ -57,7 +58,29 @@ void strbuf_finalise_agent_query(strbuf *buf);
|
|||||||
wchar_t *dup_mb_to_wc_c(int codepage, int flags, const char *string, int len);
|
wchar_t *dup_mb_to_wc_c(int codepage, int flags, const char *string, int len);
|
||||||
wchar_t *dup_mb_to_wc(int codepage, int flags, const char *string);
|
wchar_t *dup_mb_to_wc(int codepage, int flags, const char *string);
|
||||||
|
|
||||||
int toint(unsigned);
|
static inline int toint(unsigned u)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Convert an unsigned to an int, without running into the
|
||||||
|
* undefined behaviour which happens by the strict C standard if
|
||||||
|
* the value overflows. You'd hope that sensible compilers would
|
||||||
|
* do the sensible thing in response to a cast, but actually I
|
||||||
|
* don't trust modern compilers not to do silly things like
|
||||||
|
* assuming that _obviously_ you wouldn't have caused an overflow
|
||||||
|
* and so they can elide an 'if (i < 0)' test immediately after
|
||||||
|
* the cast.
|
||||||
|
*
|
||||||
|
* Sensible compilers ought of course to optimise this entire
|
||||||
|
* function into 'just return the input value', and since it's
|
||||||
|
* also declared inline, elide it completely in their output.
|
||||||
|
*/
|
||||||
|
if (u <= (unsigned)INT_MAX)
|
||||||
|
return (int)u;
|
||||||
|
else if (u >= (unsigned)INT_MIN) /* wrap in cast _to_ unsigned is OK */
|
||||||
|
return INT_MIN + (int)(u - (unsigned)INT_MIN);
|
||||||
|
else
|
||||||
|
return INT_MIN; /* fallback; should never occur on binary machines */
|
||||||
|
}
|
||||||
|
|
||||||
char *fgetline(FILE *fp);
|
char *fgetline(FILE *fp);
|
||||||
char *chomp(char *str);
|
char *chomp(char *str);
|
||||||
@ -96,9 +119,24 @@ struct tm ltime(void);
|
|||||||
*/
|
*/
|
||||||
int nullstrcmp(const char *a, const char *b);
|
int nullstrcmp(const char *a, const char *b);
|
||||||
|
|
||||||
ptrlen make_ptrlen(const void *ptr, size_t len);
|
static inline ptrlen make_ptrlen(const void *ptr, size_t len)
|
||||||
ptrlen ptrlen_from_asciz(const char *str);
|
{
|
||||||
ptrlen ptrlen_from_strbuf(strbuf *sb);
|
ptrlen pl;
|
||||||
|
pl.ptr = ptr;
|
||||||
|
pl.len = len;
|
||||||
|
return pl;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline ptrlen ptrlen_from_asciz(const char *str)
|
||||||
|
{
|
||||||
|
return make_ptrlen(str, strlen(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline ptrlen ptrlen_from_strbuf(strbuf *sb)
|
||||||
|
{
|
||||||
|
return make_ptrlen(sb->u, sb->len);
|
||||||
|
}
|
||||||
|
|
||||||
bool ptrlen_eq_string(ptrlen pl, const char *str);
|
bool ptrlen_eq_string(ptrlen pl, const char *str);
|
||||||
bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2);
|
bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2);
|
||||||
bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail);
|
bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user