Skip to content

Commit 3a1f048

Browse files
committed
PhysicalDisk: unifying white space trimming
1 parent 64910d0 commit 3a1f048

File tree

8 files changed

+61
-7
lines changed

8 files changed

+61
-7
lines changed

src/detection/physicaldisk/physicaldisk_apple.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options)
113113
if (deviceCharacteristics)
114114
{
115115
ffCfDictGetString(deviceCharacteristics, CFSTR(kIOPropertyProductSerialNumberKey), &device->serial);
116-
ffStrbufTrim(&device->serial, ' ');
116+
ffStrbufTrimSpace(&device->serial);
117117
ffCfDictGetString(deviceCharacteristics, CFSTR(kIOPropertyProductRevisionLevelKey), &device->revision);
118-
ffStrbufTrim(&device->revision, ' ');
118+
ffStrbufTrimRightSpace(&device->revision);
119119

120120
CFStringRef mediumType = (CFStringRef) CFDictionaryGetValue(deviceCharacteristics, CFSTR(kIOPropertyMediumTypeKey));
121121
if (mediumType)

src/detection/physicaldisk/physicaldisk_bsd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options)
6565
FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result);
6666
ffStrbufInitF(&device->devPath, "/dev/%s", provider->lg_name);
6767
ffStrbufInitMove(&device->serial, &identifier);
68-
ffStrbufTrim(&device->serial, ' ');
68+
ffStrbufTrimSpace(&device->serial);
6969
ffStrbufInit(&device->revision);
7070
ffStrbufInit(&device->interconnect);
7171
switch (snapIter->device_type & DEVSTAT_TYPE_IF_MASK)

src/detection/physicaldisk/physicaldisk_linux.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOption
153153
{
154154
ffStrbufInit(&device->serial);
155155
if (ffReadFileBufferRelative(devfd, "serial", &device->serial))
156-
ffStrbufTrim(&device->serial, ' ');
156+
ffStrbufTrimSpace(&device->serial);
157157
}
158158

159159
{

src/detection/physicaldisk/physicaldisk_sunos.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ static int walkDevTree(di_node_t node, di_minor_t minor, struct FFWalkTreeBundle
3636

3737
char* buf;
3838
if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "inquiry-serial-no", &buf) > 0)
39+
{
3940
ffStrbufSetS(&device->serial, buf);
41+
ffStrbufTrimSpace(&device->serial);
42+
}
4043
if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "inquiry-revision-id", &buf) > 0)
44+
{
4145
ffStrbufSetS(&device->revision, buf);
46+
ffStrbufTrimRightSpace(&device->revision);
47+
}
4248
if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "class", &buf) > 0)
4349
ffStrbufSetS(&device->interconnect, buf);
4450

src/detection/physicaldisk/physicaldisk_windows.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFPhysic
6262
if (sdd->SerialNumberOffset != 0)
6363
{
6464
ffStrbufSetS(&device->serial, (const char*) sddBuffer + sdd->SerialNumberOffset);
65-
ffStrbufTrim(&device->serial, ' ');
65+
ffStrbufTrimSpace(&device->serial);
6666
}
6767

6868
ffStrbufInit(&device->revision);
6969
if (sdd->ProductRevisionOffset != 0)
7070
{
7171
ffStrbufSetS(&device->revision, (const char*) sddBuffer + sdd->ProductRevisionOffset);
72-
ffStrbufTrim(&device->revision, ' ');
72+
ffStrbufTrimRightSpace(&device->revision);
7373
}
7474

7575
device->type |= sdd->RemovableMedia ? FF_PHYSICALDISK_TYPE_REMOVABLE : FF_PHYSICALDISK_TYPE_FIXED;

src/util/FFstrbuf.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,31 @@ void ffStrbufTrimRight(FFstrbuf* strbuf, char c)
272272
strbuf->chars[strbuf->length] = '\0';
273273
}
274274

275+
void ffStrbufTrimLeftSpace(FFstrbuf* strbuf)
276+
{
277+
if(strbuf->length == 0)
278+
return;
279+
280+
uint32_t index = 0;
281+
while(index < strbuf->length && isspace(strbuf->chars[index]))
282+
++index;
283+
284+
if(index == 0)
285+
return;
286+
287+
if(strbuf->allocated == 0)
288+
{
289+
//static string
290+
strbuf->length -= index;
291+
strbuf->chars += index;
292+
return;
293+
}
294+
295+
memmove(strbuf->chars, strbuf->chars + index, strbuf->length - index);
296+
strbuf->length -= index;
297+
strbuf->chars[strbuf->length] = '\0';
298+
}
299+
275300
void ffStrbufTrimRightSpace(FFstrbuf* strbuf)
276301
{
277302
if (strbuf->length == 0)

src/util/FFstrbuf.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ FF_C_PRINTF(2, 3) void ffStrbufSetF(FFstrbuf* strbuf, const char* format, ...);
5353

5454
void ffStrbufTrimLeft(FFstrbuf* strbuf, char c);
5555
void ffStrbufTrimRight(FFstrbuf* strbuf, char c);
56+
void ffStrbufTrimLeftSpace(FFstrbuf* strbuf);
5657
void ffStrbufTrimRightSpace(FFstrbuf* strbuf);
5758

5859
bool ffStrbufRemoveSubstr(FFstrbuf* strbuf, uint32_t startIndex, uint32_t endIndex);
@@ -473,7 +474,7 @@ static inline FF_C_NODISCARD bool ffStrbufEndsWithS(const FFstrbuf* strbuf, cons
473474
return ffStrbufEndsWithNS(strbuf, (uint32_t) strlen(end), end);
474475
}
475476

476-
static inline FF_C_NODISCARD bool ffStrbufEndsWithFn(const FFstrbuf* strbuf, int (*fn)(int))
477+
static inline FF_C_NODISCARD bool ffStrbufEndsWithFn(const FFstrbuf* strbuf, int (*const fn)(int))
477478
{
478479
return strbuf->length == 0 ? false :
479480
fn(strbuf->chars[strbuf->length - 1]);
@@ -507,6 +508,12 @@ static inline void ffStrbufTrim(FFstrbuf* strbuf, char c)
507508
ffStrbufTrimLeft(strbuf, c);
508509
}
509510

511+
static inline void ffStrbufTrimSpace(FFstrbuf* strbuf)
512+
{
513+
ffStrbufTrimRightSpace(strbuf);
514+
ffStrbufTrimLeftSpace(strbuf);
515+
}
516+
510517
static inline bool ffStrbufMatchSeparatedS(const FFstrbuf* strbuf, const char* comp, char separator)
511518
{
512519
return ffStrbufMatchSeparatedNS(strbuf, (uint32_t) strlen(comp), comp, separator);

tests/strbuf.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,22 @@ int main(void)
371371
VERIFY(strbuf.allocated > 0);
372372
ffStrbufDestroy(&strbuf);
373373

374+
//ffStrbufCreateStatic / TrimSpace
375+
ffStrbufInitStatic(&strbuf, "\n TEST\n ");
376+
ffStrbufTrimSpace(&strbuf);
377+
VERIFY(strbuf.length == 4);
378+
VERIFY(strbuf.allocated > 0);
379+
VERIFY(ffStrbufEqualS(&strbuf, "TEST"));
380+
ffStrbufDestroy(&strbuf);
381+
382+
//ffStrbufCreate / TrimSpace
383+
ffStrbufInitS(&strbuf, "\n TEST\n ");
384+
ffStrbufTrimSpace(&strbuf);
385+
VERIFY(strbuf.length == 4);
386+
VERIFY(strbuf.allocated > 0);
387+
VERIFY(ffStrbufEqualS(&strbuf, "TEST"));
388+
ffStrbufDestroy(&strbuf);
389+
374390
//ffStrbufEnsureFixedLengthFree / empty buffer
375391
ffStrbufInit(&strbuf);
376392
ffStrbufEnsureFixedLengthFree(&strbuf, 10);

0 commit comments

Comments
 (0)