1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-25 13:14:19 +01:00

Codechange: Replace SaveLoad var length arrays with switch block and sizeof. (#12570)

SlCalcConvMemLen(), SlCalcConfFileLen() and CalcOldVarLen() follow a pattern of looking up part of a value in an array.

These function returns the size of bytes of a variable type, but is not very clear. Replace with a switch block instead.

Removes lengthof, array indices, and magic numbers.
This commit is contained in:
Peter Nelson
2024-04-24 21:33:29 +01:00
committed by GitHub
parent 1dc94d0670
commit 26bb87ebf1
2 changed files with 42 additions and 15 deletions

View File

@@ -33,12 +33,24 @@ static inline OldChunkType GetOldChunkType(OldChunkType type) {return (OldCh
static inline OldChunkType GetOldChunkVarType(OldChunkType type) {return (OldChunkType)(GB(type, 8, 8) << 8);}
static inline OldChunkType GetOldChunkFileType(OldChunkType type) {return (OldChunkType)(GB(type, 16, 8) << 16);}
/**
* Return expected size in bytes of a OldChunkType
* @param type OldChunkType to get size of.
* @return size of type in bytes.
*/
static inline uint8_t CalcOldVarLen(OldChunkType type)
{
static const uint8_t type_mem_size[] = {0, 1, 1, 2, 2, 4, 4, 8};
uint8_t length = GB(type, 8, 8);
assert(length != 0 && length < lengthof(type_mem_size));
return type_mem_size[length];
switch (GetOldChunkVarType(type)) {
case OC_VAR_I8: return sizeof(int8_t);
case OC_VAR_U8: return sizeof(uint8_t);
case OC_VAR_I16: return sizeof(int16_t);
case OC_VAR_U16: return sizeof(uint16_t);
case OC_VAR_I32: return sizeof(int32_t);
case OC_VAR_U32: return sizeof(uint32_t);
case OC_VAR_I64: return sizeof(int64_t);
case OC_VAR_U64: return sizeof(uint64_t);
default: NOT_REACHED();
}
}
/**