mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-17 11:00:59 -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:
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)
|
||||
{
|
||||
/* 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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
size_t len = strlen(str);
|
||||
|
Reference in New Issue
Block a user