diff --git a/src/common/type/stringId.c b/src/common/type/stringId.c index 538370110..1f0a36d16 100644 --- a/src/common/type/stringId.c +++ b/src/common/type/stringId.c @@ -20,7 +20,6 @@ typedef enum Constants used to extract information from the header ***********************************************************************************************************************************/ #define STRING_ID_HEADER_SIZE 4 -#define STRING_ID_PREFIX 4 /**********************************************************************************************************************************/ // Helper to do encoding for specified number of bits @@ -65,12 +64,13 @@ strIdBitFromZN(const StringIdBit bit, const char *const buffer, size_t size) }; // {uncrustify_on} + // If size is greater than can be encoded then error + if (size > STRID5_MAX) + FUNCTION_TEST_RETURN(STRING_ID, 0); + // Make sure the string is valid for this encoding for (size_t bufferIdx = 0; bufferIdx < size; bufferIdx++) { - if (bufferIdx == STRID5_MAX - 1) - break; - if (map[(uint8_t)buffer[bufferIdx]] == 0) FUNCTION_TEST_RETURN(STRING_ID, 0); } @@ -78,13 +78,6 @@ strIdBitFromZN(const StringIdBit bit, const char *const buffer, size_t size) // Set encoding in header uint64_t result = stringIdBit5; - // If size is greater than can be encoded then add prefix bit and adjust size - if (size >= STRID5_MAX) - { - result |= STRING_ID_PREFIX; - size = STRID5_MAX - 1; - } - // Encode based on the number of characters that need to be encoded switch (size) { @@ -132,7 +125,7 @@ strIdBitFromZN(const StringIdBit bit, const char *const buffer, size_t size) result |= (uint64_t)map[(uint8_t)buffer[1]] << 9; __attribute__((fallthrough)); - case 1: + default: result |= (uint64_t)map[(uint8_t)buffer[0]] << 4; } @@ -167,12 +160,13 @@ strIdBitFromZN(const StringIdBit bit, const char *const buffer, size_t size) }; // {uncrustify_on} + // If size is greater than can be encoded then error + if (size > STRID6_MAX) + FUNCTION_TEST_RETURN(STRING_ID, 0); + // Make sure the string is valid for this encoding for (size_t bufferIdx = 0; bufferIdx < size; bufferIdx++) { - if (bufferIdx == STRID6_MAX - 1) - break; - if (map[(uint8_t)buffer[bufferIdx]] == 0) FUNCTION_TEST_RETURN(STRING_ID, 0); } @@ -180,13 +174,6 @@ strIdBitFromZN(const StringIdBit bit, const char *const buffer, size_t size) // Set encoding in header uint64_t result = stringIdBit6; - // If size is greater than can be encoded then add prefix bit and adjust size - if (size >= STRID6_MAX) - { - result |= STRING_ID_PREFIX; - size = STRID6_MAX - 1; - } - // Encode based on the number of characters that need to be encoded switch (size) { @@ -226,7 +213,7 @@ strIdBitFromZN(const StringIdBit bit, const char *const buffer, size_t size) result |= (uint64_t)map[(uint8_t)buffer[1]] << 10; __attribute__((fallthrough)); - case 1: + default: result |= (uint64_t)map[(uint8_t)buffer[0]] << 4; } @@ -271,9 +258,6 @@ strIdToZN(StringId strId, char *const buffer) ASSERT(strId != 0); ASSERT(buffer != NULL); - // Is the StringId a prefix of a longer string? - bool prefix = strId & STRING_ID_PREFIX; - // Extract bits used to encode the characters StringIdBit bit = (StringIdBit)(strId & STRING_ID_BIT_MASK); @@ -314,13 +298,6 @@ strIdToZN(StringId strId, char *const buffer) buffer[11] = map[strId & 0x1F]; ASSERT(strId >> 5 == 0); - // If prefix flag is set then append + - if (prefix) - { - buffer[12] = '+'; - FUNCTION_TEST_RETURN(SIZE, 13); - } - FUNCTION_TEST_RETURN(SIZE, 12); } @@ -355,13 +332,6 @@ strIdToZN(StringId strId, char *const buffer) buffer[9] = map[strId & 0x3F]; ASSERT(strId >> 6 == 0); - // If prefix flag is set then append + - if (prefix) - { - buffer[10] = '+'; - FUNCTION_TEST_RETURN(SIZE, 11); - } - FUNCTION_TEST_RETURN(SIZE, 10); } } diff --git a/src/common/type/stringId.h b/src/common/type/stringId.h index 15e7d130a..f733c76fb 100644 --- a/src/common/type/stringId.h +++ b/src/common/type/stringId.h @@ -9,8 +9,8 @@ they cannot be directly used in switch statements leading to less efficient if-e A StringId encodes a short string into an integer so it can be used in switch statements but may also be readily converted back into a string for debugging purposes. StringIds may also be suitable for matching user input providing the strings are short enough. -strIdFromZ("mytest0123a") will return the StringId 0x7de75c51315464d5. Using the value, the string representation can be retrieved -with strIdToZ(0x7de75c51315464d5, ...) which returns "mytest0123+" where the plus at the end signals that the original string was +strIdFromZ("mytest0123") will return the StringId 0x7de75c51315464d1. Using the value, the string representation can be retrieved +with strIdToZ(0x7de75c51315464d1, ...) which returns "mytest0123+" where the plus at the end signals that the original string was equal to or longer than the maximum allowed. When assigning a StringId to an enum, it will be necessary to cast the StringId to the enum type if the enum contains all 32-bit @@ -33,8 +33,8 @@ the MAX, then a plus sign + will be assigned as the last (MAX) character in the when calling strIdToZN. If the buffer needs to be zero-terminated then an extra byte should be added. ***********************************************************************************************************************************/ // Maximum for specific encodings (e.g. 5-bit, 6-bit) -#define STRID5_MAX 13 -#define STRID6_MAX 11 +#define STRID5_MAX 12 +#define STRID6_MAX 10 // Maximum for any encoding #define STRID_MAX STRID5_MAX diff --git a/test/src/module/common/typeStringTest.c b/test/src/module/common/typeStringTest.c index f75edf100..9238d85fc 100644 --- a/test/src/module/common/typeStringTest.c +++ b/test/src/module/common/typeStringTest.c @@ -590,13 +590,20 @@ testRun(void) #define TEST_STR5ID10 (TEST_STR5ID9 | (uint64_t)('y' - 96) << 49) #define TEST_STR5ID11 (TEST_STR5ID10 | (uint64_t)('5' - 24) << 54) #define TEST_STR5ID12 (TEST_STR5ID11 | (uint64_t)('6' - 24) << 59) - #define TEST_STR5ID13 (TEST_STR5ID12 | STRING_ID_PREFIX) TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "a", 1), TEST_STR5ID1, "5 bits 1 char"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "ab", 2), TEST_STR5ID2, "5 bits 2 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "abc", 3), TEST_STR5ID3, "5 bits 3 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "abc-", 4), TEST_STR5ID4, "5 bits 4 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "abc-z", 5), TEST_STR5ID5, "5 bits 5 chars"); TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "abc-zk", 6), TEST_STR5ID6, "5 bits 6 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "abc-zkz", 7), TEST_STR5ID7, "5 bits 7 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "abc-zkz2", 8), TEST_STR5ID8, "5 bits 8 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "abc-zkz2-", 9), TEST_STR5ID9, "5 bits 9 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "abc-zkz2-y", 10), TEST_STR5ID10, "5 bits 10 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "abc-zkz2-y5", 11), TEST_STR5ID11, "5 bits 11 chars"); TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "abc-zkz2-y56", 12), TEST_STR5ID12, "5 bits 12 chars"); - TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "abc-zkz2-y56?", 13), TEST_STR5ID13, "5 bits 13 chars"); - TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "abc-zkz2-y56??", 14), TEST_STR5ID13, "5 bits 14 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "abc-zkz2-y56X", 13), 0, "error on too many chars"); TEST_RESULT_UINT(strIdBitFromZN(stringIdBit5, "AB", 2), 0, "'A' is invalid for 5-bit encoding in 'AB'"); @@ -610,13 +617,18 @@ testRun(void) #define TEST_STR6ID8 (TEST_STR6ID7 | (uint64_t)('z' - 96) << 46) #define TEST_STR6ID9 (TEST_STR6ID8 | (uint64_t)('Z' - 27) << 52) #define TEST_STR6ID10 (TEST_STR6ID9 | (uint64_t)('9' - 20) << 58) - #define TEST_STR6ID11 (TEST_STR6ID10 | STRING_ID_PREFIX) TEST_RESULT_UINT(strIdBitFromZN(stringIdBit6, "a", 1), TEST_STR6ID1, "6 bits 1 char"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit6, "ab", 2), TEST_STR6ID2, "6 bits 2 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit6, "abC", 3), TEST_STR6ID3, "6 bits 3 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit6, "abC-", 4), TEST_STR6ID4, "6 bits 4 chars"); TEST_RESULT_UINT(strIdBitFromZN(stringIdBit6, "abC-4", 5), TEST_STR6ID5, "6 bits 5 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit6, "abC-40", 6), TEST_STR6ID6, "6 bits 6 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit6, "abC-40M", 7), TEST_STR6ID7, "6 bits 7 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit6, "abC-40Mz", 8), TEST_STR6ID8, "6 bits 8 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit6, "abC-40MzZ", 9), TEST_STR6ID9, "6 bits 9 chars"); TEST_RESULT_UINT(strIdBitFromZN(stringIdBit6, "abC-40MzZ9", 10), TEST_STR6ID10, "6 bits 10 chars"); - TEST_RESULT_UINT(strIdBitFromZN(stringIdBit6, "abC-40MzZ9?", 11), TEST_STR6ID11, "6 bits 11 chars"); - TEST_RESULT_UINT(strIdBitFromZN(stringIdBit6, "abC-40MzZ9??", 12), TEST_STR6ID11, "6 bits 12 chars"); + TEST_RESULT_UINT(strIdBitFromZN(stringIdBit6, "abC-40MzZ9Z", 11), 0, "error on too many chars"); TEST_RESULT_UINT(strIdFromZN("|B", 2, false), 0, "'|' is invalid for 6-bit encoding in '|B'"); @@ -670,8 +682,6 @@ testRun(void) TEST_RESULT_Z(buffer5, "abc-zkz2-y5XX", " check"); TEST_RESULT_UINT(strIdToZN(TEST_STR5ID12, buffer5), 12, "5 bits 12 chars"); TEST_RESULT_Z(buffer5, "abc-zkz2-y56X", " check"); - TEST_RESULT_UINT(strIdToZN(TEST_STR5ID13, buffer5), 13, "5 bits 13 chars"); - TEST_RESULT_Z(buffer5, "abc-zkz2-y56+", " check"); char buffer6[] = "XXXXXXXXXXX"; @@ -695,8 +705,6 @@ testRun(void) TEST_RESULT_Z(buffer6, "abC-40MzZXX", " check"); TEST_RESULT_UINT(strIdToZN(TEST_STR6ID10, buffer6), 10, "6 bits 10 chars"); TEST_RESULT_Z(buffer6, "abC-40MzZ9X", " check"); - TEST_RESULT_UINT(strIdToZN(TEST_STR6ID11, buffer6), 11, "6 bits 11 chars"); - TEST_RESULT_Z(buffer6, "abC-40MzZ9+", " check"); // ------------------------------------------------------------------------------------------------------------------------- TEST_TITLE("strNewStrId()");