mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 10:52:08 +02:00
FFstrbuf: adds new ffStrbufDecodeHexEscapeSequences
This commit is contained in:
@@ -628,4 +628,6 @@ static inline bool ffStrbufSeparatedContainIgnCase(const FFstrbuf* strbuf, const
|
||||
return ffStrbufSeparatedContainIgnCaseNS(strbuf, comp->length, comp->chars, separator);
|
||||
}
|
||||
|
||||
bool ffStrbufDecodeHexEscapeSequences(FFstrbuf* strbuf);
|
||||
|
||||
#define FF_STRBUF_AUTO_DESTROY FFstrbuf __attribute__((__cleanup__(ffStrbufDestroy)))
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "common/FFstrbuf.h"
|
||||
#include "common/mallocHelper.h"
|
||||
#include "common/stringUtils.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
@@ -915,3 +916,44 @@ bool ffStrbufSeparatedContainIgnCaseNS(const FFstrbuf* strbuf, uint32_t compLeng
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ffStrbufDecodeHexEscapeSequences(FFstrbuf* strbuf)
|
||||
{
|
||||
assert(strbuf);
|
||||
|
||||
if (strbuf->length < 4)
|
||||
return false;
|
||||
|
||||
// Static string must be converted first.
|
||||
assert(strbuf->allocated > 0);
|
||||
|
||||
bool changed = false;
|
||||
uint32_t read = 0;
|
||||
uint32_t write = 0;
|
||||
|
||||
while (read < strbuf->length)
|
||||
{
|
||||
if (
|
||||
read + 3 < strbuf->length &&
|
||||
strbuf->chars[read] == '\\' &&
|
||||
strbuf->chars[read + 1] == 'x'
|
||||
)
|
||||
{
|
||||
int8_t hi = ffHexCharToInt(strbuf->chars[read + 2]);
|
||||
int8_t lo = ffHexCharToInt(strbuf->chars[read + 3]);
|
||||
if (hi >= 0 && lo >= 0)
|
||||
{
|
||||
strbuf->chars[write++] = (char) ((hi << 4) | lo);
|
||||
read += 4;
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
strbuf->chars[write++] = strbuf->chars[read++];
|
||||
}
|
||||
|
||||
strbuf->length = write;
|
||||
strbuf->chars[write] = '\0';
|
||||
return changed;
|
||||
}
|
||||
|
||||
@@ -80,6 +80,23 @@ static inline bool ffCharIsDigit(char c)
|
||||
return '0' <= c && c <= '9';
|
||||
}
|
||||
|
||||
static inline bool ffCharIsHexDigit(char c)
|
||||
{
|
||||
return ffCharIsDigit(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
|
||||
}
|
||||
|
||||
static inline int8_t ffHexCharToInt(char c)
|
||||
{
|
||||
if (ffCharIsDigit(c))
|
||||
return (int8_t) (c - '0');
|
||||
else if ('a' <= c && c <= 'f')
|
||||
return (int8_t) (c - 'a' + 10);
|
||||
else if ('A' <= c && c <= 'F')
|
||||
return (int8_t) (c - 'A' + 10);
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Copies at most (dstBufSiz - 1) bytes from src to dst; dst is always null-terminated
|
||||
static inline char* ffStrCopy(char* __restrict__ dst, const char* __restrict__ src, size_t dstBufSiz)
|
||||
{
|
||||
|
||||
@@ -140,21 +140,7 @@ static void detectName(FFDisk* disk)
|
||||
|
||||
if (disk->name.length == 0) return;
|
||||
|
||||
// Basic\x20data\x20partition
|
||||
for (uint32_t i = ffStrbufFirstIndexS(&disk->name, "\\x");
|
||||
i != disk->name.length;
|
||||
i = ffStrbufNextIndexS(&disk->name, i + 1, "\\x"))
|
||||
{
|
||||
uint32_t len = (uint32_t) strlen("\\x20");
|
||||
if (disk->name.length >= len)
|
||||
{
|
||||
char bak = disk->name.chars[i + len];
|
||||
disk->name.chars[i + len] = '\0';
|
||||
disk->name.chars[i] = (char) strtoul(&disk->name.chars[i + 2], NULL, 16);
|
||||
ffStrbufRemoveSubstr(&disk->name, i + 1, i + len);
|
||||
disk->name.chars[i + 1] = bak;
|
||||
}
|
||||
}
|
||||
ffStrbufDecodeHexEscapeSequences(&disk->name);
|
||||
}
|
||||
|
||||
#ifdef __ANDROID__
|
||||
|
||||
Reference in New Issue
Block a user