From 7a596739d8563e20bfdf75cb9ab43ed4973ba494 Mon Sep 17 00:00:00 2001 From: cfif Date: Tue, 30 Sep 2025 15:52:33 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5=D0=B9?= =?UTF-8?q?=D1=81=D1=8B=20=D0=B8=20=D0=B4=D1=80=D0=B0=D0=B9=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Inc/AsciiStringParsingUtils.h | 33 +++++++++ Src/AsciiStringParsingUtils.c | 123 ++++++++++++++++++++++++++++++++++ Tst/CMakeLists.txt | 8 +++ Tst/Src/test.c | 17 +++++ Tst/modular.json | 16 +++++ modular.json | 12 ++++ 6 files changed, 209 insertions(+) create mode 100644 Inc/AsciiStringParsingUtils.h create mode 100644 Src/AsciiStringParsingUtils.c create mode 100644 Tst/CMakeLists.txt create mode 100644 Tst/Src/test.c create mode 100644 Tst/modular.json create mode 100644 modular.json diff --git a/Inc/AsciiStringParsingUtils.h b/Inc/AsciiStringParsingUtils.h new file mode 100644 index 0000000..c3460f7 --- /dev/null +++ b/Inc/AsciiStringParsingUtils.h @@ -0,0 +1,33 @@ +// +// Created by xemon on 10.09.22. +// + +#ifndef ASCIISTRINGPARSINGUTILS_ASCIISTRINGPARSINGUTILS_H +#define ASCIISTRINGPARSINGUTILS_ASCIISTRINGPARSINGUTILS_H + +#include +#include "stdint.h" + +int64_t iAsciiStringParseSignedLongDecimalNumber(char *str, const char *strEnd); + +uint64_t iAsciiStringParseUnsignedLongDecimalNumber(char *str, const char *strEnd); + +double dAsciiStringParseDouble(char *str, const char *strEnd); + +char *xAsciiStringSkipChar(char *str, char const *strEnd, char symbol); + +#define xAsciiStringSkipSpace(STR, STR_END) xAsciiStringSkipChar(STR,STR_END,' ') + +char *xAsciiStringSeekChar(char *str, char const *strEnd, char symbol); + +uint8_t iAsciiStringMoveToNextParsingBlock(char **blockBeginPos, char **blockDividerPos, char *strEnd, char divider); + +uint8_t iAsciiStringParseHexByte(const char *str); + +size_t iAsciiStringParseHexBytes(uint8_t *bytesData, char *str, size_t stringLen); + +#define BIN_FROM_HEX(DATA,LEN,HEX_STRING) \ +uint8_t DATA[(sizeof(HEX_STRING)-1)/2]; \ +size_t LEN = iAsciiStringParseHexBytes(DATA, HEX_STRING, sizeof(HEX_STRING) - 1); + +#endif //ASCIISTRINGPARSINGUTILS_ASCIISTRINGPARSINGUTILS_H diff --git a/Src/AsciiStringParsingUtils.c b/Src/AsciiStringParsingUtils.c new file mode 100644 index 0000000..9391393 --- /dev/null +++ b/Src/AsciiStringParsingUtils.c @@ -0,0 +1,123 @@ +// +// Created by xemon on 10.09.22. +// + +#include "AsciiStringParsingUtils.h" +#include +#include + +uint8_t iAsciiStringParseCharToDecimalInt(char symbol) { + return (symbol - '0'); // return overvalue if not a decimal number +} + +uint8_t iAsciiStringParseHexCharToInt(char symbol) { + if (symbol >= '0' && symbol <= '9') { + return iAsciiStringParseCharToDecimalInt(symbol); + } else if (symbol >= 'a' && symbol <= 'f') { + return 0xA + symbol - 'a'; + } else if (symbol >= 'A' && symbol <= 'F') { + return 0xA + (symbol - 'A'); + } else { + return 0x10; //not a hex number, return overvalue + } +} + + +uint8_t iAsciiStringParseHexByte(const char *str) { + return iAsciiStringParseHexCharToInt(*str) * 0x10 + iAsciiStringParseHexCharToInt(*(str + 1)); +} + +size_t iAsciiStringParseHexBytes(uint8_t *bytesData, char *str, size_t stringLen) { + char *end = str + stringLen; + + size_t dataLen = stringLen / 2; + + while (end > str) { + + *bytesData = iAsciiStringParseHexByte(str); + + str += 2; + ++bytesData; + } + + return dataLen; +} + +uint64_t iAsciiStringParseUIntDecimalNumberWhitEnd(char **str, const char *strEnd) { + + + uint32_t value = 0; + uint8_t digit; + + while ((*str) < strEnd) { + digit = iAsciiStringParseCharToDecimalInt(**str); + if (digit > 9) { + return value; + } else { + value = (value * 10) + digit; + } + ++(*str); + } + + return value; +} + +int64_t iAsciiStringParseSignedLongDecimalNumber(char *str, const char *strEnd) { + int8_t sign = 1; + if (*str == '-') { + sign = -1; + ++str; + } + + return iAsciiStringParseUIntDecimalNumberWhitEnd(&str, strEnd) * sign; +} + +uint64_t iAsciiStringParseUnsignedLongDecimalNumber(char *str, const char *strEnd) { + return iAsciiStringParseUIntDecimalNumberWhitEnd(&str, strEnd); +} + + +char *xAsciiStringSkipChar(char *utcString, char const *utcStringEnd, char symbol) { + while ((utcString < utcStringEnd) && (*utcString == symbol)) { + ++utcString; + } + return utcString; +} + +char *xAsciiStringSeekChar(char *utcString, char const *utcStringEnd, char symbol) { + while ((utcString < utcStringEnd) && (*utcString != symbol)) { + ++utcString; + } + return utcString; +} + +double dAsciiStringParseDouble(char *str, const char *strEnd) { + double sign = 1; + if (str[0] == '-') { + sign = -1; + ++str; + } + char *dividerSymbol = xAsciiStringSeekChar(str, strEnd, '.'); + + double result = iAsciiStringParseUnsignedLongDecimalNumber(str, dividerSymbol); + + if (dividerSymbol < (strEnd - 1)) { + ++dividerSymbol; + char *fractionalStr = dividerSymbol; + double fractional = iAsciiStringParseUIntDecimalNumberWhitEnd(&fractionalStr, strEnd); + + for (; dividerSymbol < fractionalStr; ++dividerSymbol) { + fractional /= 10; + } + result += fractional; + } + + + return result * sign; +} + +uint8_t iAsciiStringMoveToNextParsingBlock(char **blockBeginPos, char **blockDividerPos, char *strEnd, char divider) { + *blockBeginPos = xAsciiStringSkipSpace((*blockDividerPos + 1), strEnd); + *blockDividerPos = xAsciiStringSeekChar(*blockBeginPos, strEnd, divider); + return *blockDividerPos - *blockBeginPos; //return block length +} \ No newline at end of file diff --git a/Tst/CMakeLists.txt b/Tst/CMakeLists.txt new file mode 100644 index 0000000..72c5078 --- /dev/null +++ b/Tst/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.17) +PROJECT(test) + +set(CMAKE_CXX_STANDARD 17) + +include(modular.cmake) + +add_executable(test ${SOURCES}) \ No newline at end of file diff --git a/Tst/Src/test.c b/Tst/Src/test.c new file mode 100644 index 0000000..f43e370 --- /dev/null +++ b/Tst/Src/test.c @@ -0,0 +1,17 @@ +// +// Created by xemon on 10.09.22. +// +#include "AsciiStringParsingUtils.h" +#include "printf.h" + +int main() { + char srt[] = "22341.233adfa"; + + + int32_t si = iAsciiStringParseSignedLongDecimalNumber(srt, srt + 10); + double dbl = dAsciiStringParseDouble(srt, srt + 10); + + printf("short unsigned %i %f\n", si, dbl); + + return 0; +} \ No newline at end of file diff --git a/Tst/modular.json b/Tst/modular.json new file mode 100644 index 0000000..236f353 --- /dev/null +++ b/Tst/modular.json @@ -0,0 +1,16 @@ +{ + "dep": [ + { + "type": "local", + "dir": "../" + } + ], + "cmake": { + "inc_dirs": [ + "Inc" + ], + "srcs": [ + "Src/**.c" + ] + } +} \ No newline at end of file diff --git a/modular.json b/modular.json new file mode 100644 index 0000000..d4d574e --- /dev/null +++ b/modular.json @@ -0,0 +1,12 @@ +{ + "dep": [ + ], + "cmake": { + "inc_dirs": [ + "Inc" + ], + "srcs": [ + "Src/**.c" + ] + } +} \ No newline at end of file