mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-04-02 22:06:10 +00:00
Rewrite extractVersionQuadFromString() to avoid copying the input string
This commit is contained in:
37
src/Misc.c
37
src/Misc.c
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user