Changeset View
Changeset View
Standalone View
Standalone View
head/contrib/lua/src/lstrlib.c
Show First 20 Lines • Show All 1,128 Lines • ▼ Show 20 Lines | static const union { | ||||
int dummy; | int dummy; | ||||
char little; /* true iff machine is little endian */ | char little; /* true iff machine is little endian */ | ||||
} nativeendian = {1}; | } nativeendian = {1}; | ||||
/* dummy structure to get native alignment requirements */ | /* dummy structure to get native alignment requirements */ | ||||
struct cD { | struct cD { | ||||
char c; | char c; | ||||
union { double d; void *p; lua_Integer i; lua_Number n; } u; | union { | ||||
#if LUA_FLOAT_TYPE != LUA_FLOAT_INT64 | |||||
double d; | |||||
#endif | |||||
void *p; lua_Integer i; lua_Number n; } u; | |||||
}; | }; | ||||
#define MAXALIGN (offsetof(struct cD, u)) | #define MAXALIGN (offsetof(struct cD, u)) | ||||
/* | /* | ||||
** Union for serializing floats | ** Union for serializing floats | ||||
*/ | */ | ||||
typedef union Ftypes { | typedef union Ftypes { | ||||
#if LUA_FLOAT_TYPE != LUA_FLOAT_INT64 | |||||
float f; | float f; | ||||
double d; | double d; | ||||
#endif | |||||
lua_Number n; | lua_Number n; | ||||
char buff[5 * sizeof(lua_Number)]; /* enough for any float type */ | char buff[5 * sizeof(lua_Number)]; /* enough for any float type */ | ||||
} Ftypes; | } Ftypes; | ||||
/* | /* | ||||
** information to pack/unpack stuff | ** information to pack/unpack stuff | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | switch (opt) { | ||||
case 'B': *size = sizeof(char); return Kuint; | case 'B': *size = sizeof(char); return Kuint; | ||||
case 'h': *size = sizeof(short); return Kint; | case 'h': *size = sizeof(short); return Kint; | ||||
case 'H': *size = sizeof(short); return Kuint; | case 'H': *size = sizeof(short); return Kuint; | ||||
case 'l': *size = sizeof(long); return Kint; | case 'l': *size = sizeof(long); return Kint; | ||||
case 'L': *size = sizeof(long); return Kuint; | case 'L': *size = sizeof(long); return Kuint; | ||||
case 'j': *size = sizeof(lua_Integer); return Kint; | case 'j': *size = sizeof(lua_Integer); return Kint; | ||||
case 'J': *size = sizeof(lua_Integer); return Kuint; | case 'J': *size = sizeof(lua_Integer); return Kuint; | ||||
case 'T': *size = sizeof(size_t); return Kuint; | case 'T': *size = sizeof(size_t); return Kuint; | ||||
#if LUA_FLOAT_TYPE != LUA_FLOAT_INT64 | |||||
case 'f': *size = sizeof(float); return Kfloat; | case 'f': *size = sizeof(float); return Kfloat; | ||||
case 'd': *size = sizeof(double); return Kfloat; | case 'd': *size = sizeof(double); return Kfloat; | ||||
#endif | |||||
case 'n': *size = sizeof(lua_Number); return Kfloat; | case 'n': *size = sizeof(lua_Number); return Kfloat; | ||||
case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint; | case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint; | ||||
case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint; | case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint; | ||||
case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; | case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; | ||||
case 'c': | case 'c': | ||||
*size = getnum(fmt, -1); | *size = getnum(fmt, -1); | ||||
if (*size == -1) | if (*size == -1) | ||||
luaL_error(h->L, "missing size for format option 'c'"); | luaL_error(h->L, "missing size for format option 'c'"); | ||||
▲ Show 20 Lines • Show All 116 Lines • ▼ Show 20 Lines | switch (opt) { | ||||
arg, "unsigned overflow"); | arg, "unsigned overflow"); | ||||
packint(&b, (lua_Unsigned)n, h.islittle, size, 0); | packint(&b, (lua_Unsigned)n, h.islittle, size, 0); | ||||
break; | break; | ||||
} | } | ||||
case Kfloat: { /* floating-point options */ | case Kfloat: { /* floating-point options */ | ||||
volatile Ftypes u; | volatile Ftypes u; | ||||
char *buff = luaL_prepbuffsize(&b, size); | char *buff = luaL_prepbuffsize(&b, size); | ||||
lua_Number n = luaL_checknumber(L, arg); /* get argument */ | lua_Number n = luaL_checknumber(L, arg); /* get argument */ | ||||
#if LUA_FLOAT_TYPE != LUA_FLOAT_INT64 | |||||
if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */ | if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */ | ||||
else if (size == sizeof(u.d)) u.d = (double)n; | else if (size == sizeof(u.d)) u.d = (double)n; | ||||
else u.n = n; | else u.n = n; | ||||
#else | |||||
u.n = n; | |||||
#endif | |||||
/* move 'u' to final result, correcting endianness if needed */ | /* move 'u' to final result, correcting endianness if needed */ | ||||
copywithendian(buff, u.buff, size, h.islittle); | copywithendian(buff, u.buff, size, h.islittle); | ||||
luaL_addsize(&b, size); | luaL_addsize(&b, size); | ||||
break; | break; | ||||
} | } | ||||
case Kchar: { /* fixed-size string */ | case Kchar: { /* fixed-size string */ | ||||
size_t len; | size_t len; | ||||
const char *s = luaL_checklstring(L, arg, &len); | const char *s = luaL_checklstring(L, arg, &len); | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | switch (opt) { | ||||
(opt == Kint)); | (opt == Kint)); | ||||
lua_pushinteger(L, res); | lua_pushinteger(L, res); | ||||
break; | break; | ||||
} | } | ||||
case Kfloat: { | case Kfloat: { | ||||
volatile Ftypes u; | volatile Ftypes u; | ||||
lua_Number num; | lua_Number num; | ||||
copywithendian(u.buff, data + pos, size, h.islittle); | copywithendian(u.buff, data + pos, size, h.islittle); | ||||
#if LUA_FLOAT_TYPE != LUA_FLOAT_INT64 | |||||
if (size == sizeof(u.f)) num = (lua_Number)u.f; | if (size == sizeof(u.f)) num = (lua_Number)u.f; | ||||
else if (size == sizeof(u.d)) num = (lua_Number)u.d; | else if (size == sizeof(u.d)) num = (lua_Number)u.d; | ||||
else num = u.n; | else num = u.n; | ||||
#else | |||||
num = u.n; | |||||
#endif | |||||
lua_pushnumber(L, num); | lua_pushnumber(L, num); | ||||
break; | break; | ||||
} | } | ||||
case Kchar: { | case Kchar: { | ||||
lua_pushlstring(L, data + pos, size); | lua_pushlstring(L, data + pos, size); | ||||
break; | break; | ||||
} | } | ||||
case Kstring: { | case Kstring: { | ||||
▲ Show 20 Lines • Show All 68 Lines • Show Last 20 Lines |