Rewrite extractVersionQuadFromString() to avoid copying the input string

This commit is contained in:
Cameron Gutman
2023-10-06 17:43:56 -05:00
parent 8b84d17c8d
commit b2497a3918

View File

@@ -80,32 +80,19 @@ int gracefullyDisconnectEnetPeer(ENetHost* host, ENetPeer* peer, enet_uint32 lin
}
int extractVersionQuadFromString(const char* string, int* quad) {
char versionString[128];
char* nextDot;
char* nextNumber;
int i;
strcpy(versionString, string);
nextNumber = versionString;
for (i = 0; i < 4; i++) {
if (i == 3) {
nextDot = strchr(nextNumber, '\0');
const char* nextNumber = string;
for (int i = 0; i < 4; i++) {
// Parse the next component
quad[i] = (int)strtol(nextNumber, (char**)&nextNumber, 10);
// Skip the dot if we still have version components left.
//
// We continue looping even when we're at the end of the
// input string to ensure all subsequent version components
// are zeroed.
if (*nextNumber != 0) {
nextNumber++;
}
else {
nextDot = strchr(nextNumber, '.');
}
if (nextDot == NULL) {
return -1;
}
// Cut the string off at the next dot
*nextDot = '\0';
quad[i] = atoi(nextNumber);
// Move on to the next segment
nextNumber = nextDot + 1;
}
return 0;