FFstrbuf: adds new ffStrbufDecodeHexEscapeSequences

This commit is contained in:
李通洲
2026-03-25 14:13:03 +08:00
parent 06ecd5a1ff
commit fe0f292cec
5 changed files with 85 additions and 15 deletions
+42
View File
@@ -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;
}