@@ -128,6 +128,19 @@ void _TestAssert(int iCondition, const char* cstrFile, int iLine) {
128128#define MICROLAT_TO_FRACTIONS_FACTOR ((double) MAX_PRECISION_FACTOR)
129129#define MICROLON_TO_FRACTIONS_FACTOR (4.0 * MAX_PRECISION_FACTOR)
130130
131+ // Grid and encoding constants
132+ #define GRID_SIZE_31 31 // Base grid size for encoding
133+ #define GRID_SIZE_961 961 // 31 * 31 - large grid size
134+ #define GRID_SIZE_962 962 // 961 + 1
135+ #define GRID_SIZE_SQUARED (961 * 961) // Square of grid size
136+ #define MAX_NAMELESS_RECORDS 62 // Maximum number of nameless records
137+ #define Y_DIVIDER 90 // Standard Y coordinate divider
138+ #define SPECIAL_CODEX_21 21 // Special codex identifier
139+ #define SPECIAL_CODEX_22 22 // Special codex identifier
140+ #define SPECIAL_CODEX_13 13 // Special codex identifier
141+ #define SPECIAL_CODEX_14 14 // Special codex identifier
142+ #define GRID_MULTIPLIER_16 16 // Grid calculation multiplier
143+
131144#define FLAG_UTF8_STRING 0 // interpret pointer a utf8 characters
132145#define FLAG_UTF16_STRING 1 // interpret pointer a UWORD* to utf16 characters
133146
@@ -1054,6 +1067,44 @@ static void encodeGrid(char* result, const EncodeRec* enc, const int m, const in
10541067}
10551068
10561069
1070+ /**
1071+ * Helper function to calculate storage offset for nameless encoding.
1072+ * Determines the storage offset based on the number of nameless records (A),
1073+ * the current record index (X), and the codex value (codexm).
1074+ * This complex calculation was extracted to improve readability of encodeNameless.
1075+ */
1076+ static int calculateStorageOffset (int A , int X , int codexm ) {
1077+ const int p = GRID_SIZE_31 / A ;
1078+ const int r = GRID_SIZE_31 % A ; // the first r items are p+1
1079+
1080+ if (codexm != SPECIAL_CODEX_21 && A <= GRID_SIZE_31 ) {
1081+ return (X * p + (X < r ? X : r )) * GRID_SIZE_SQUARED ;
1082+ }
1083+ else if (codexm != SPECIAL_CODEX_21 && A < MAX_NAMELESS_RECORDS ) {
1084+ if (X < (MAX_NAMELESS_RECORDS - A )) {
1085+ return X * GRID_SIZE_SQUARED ;
1086+ }
1087+ else {
1088+ int storage_offset = (MAX_NAMELESS_RECORDS - A + ((X - MAX_NAMELESS_RECORDS + A ) / 2 )) * GRID_SIZE_SQUARED ;
1089+ if ((X + A ) & 1 ) {
1090+ storage_offset += (GRID_MULTIPLIER_16 * GRID_SIZE_961 * GRID_SIZE_31 );
1091+ }
1092+ return storage_offset ;
1093+ }
1094+ }
1095+ else {
1096+ const int BASEPOWER = (codexm == SPECIAL_CODEX_21 ) ? GRID_SIZE_SQUARED : GRID_SIZE_SQUARED * GRID_SIZE_31 ;
1097+ int BASEPOWERA = (BASEPOWER / A );
1098+ if (A == MAX_NAMELESS_RECORDS ) {
1099+ BASEPOWERA ++ ;
1100+ }
1101+ else {
1102+ BASEPOWERA = GRID_SIZE_961 * (BASEPOWERA / GRID_SIZE_961 );
1103+ }
1104+ return X * BASEPOWERA ;
1105+ }
1106+ }
1107+
10571108// *result==0 in case of error
10581109static void encodeNameless (char * result , const EncodeRec * enc , const enum Territory ccode ,
10591110 const int extraDigits , const int m ) {
@@ -1067,45 +1118,16 @@ static void encodeNameless(char* result, const EncodeRec* enc, const enum Territ
10671118 * result = 0 ;
10681119
10691120 {
1070- const int p = 31 / A ;
1071- const int r = 31 % A ; // the first r items are p+1
10721121 const int codexm = coDex (m );
10731122 const int codexlen = (codexm / 10 ) + (codexm % 10 );
10741123 // determine side of square around centre
10751124 int SIDE ;
10761125
1077- int storage_offset ;
1126+ const int storage_offset = calculateStorageOffset ( A , X , codexm ) ;
10781127 const TerritoryBoundary * b ;
10791128
10801129 int xSIDE , orgSIDE ;
10811130
1082- if (codexm != 21 && A <= 31 ) {
1083- storage_offset = (X * p + (X < r ? X : r )) * (961 * 961 );
1084- }
1085- else if (codexm != 21 && A < 62 ) {
1086- if (X < (62 - A )) {
1087- storage_offset = X * (961 * 961 );
1088- }
1089- else {
1090- storage_offset = (62 - A + ((X - 62 + A ) / 2 )) * (961 * 961 );
1091- if ((X + A ) & 1 ) {
1092- storage_offset += (16 * 961 * 31 );
1093- }
1094- }
1095- }
1096- else {
1097- const int BASEPOWER = (codexm == 21 ) ? 961 * 961 : 961 * 961 * 31 ;
1098- int BASEPOWERA = (BASEPOWER / A );
1099- if (A == 62 ) {
1100- BASEPOWERA ++ ;
1101- }
1102- else {
1103- BASEPOWERA = (961 ) * (BASEPOWERA / 961 );
1104- }
1105-
1106- storage_offset = X * BASEPOWERA ;
1107- }
1108-
11091131 SIDE = SMART_DIV (m );
11101132 ASSERT (SIDE > 0 );
11111133
@@ -1120,7 +1142,7 @@ static void encodeNameless(char* result, const EncodeRec* enc, const enum Territ
11201142 const int dx = (4 * (enc -> coord32 .lonMicroDeg - b -> minx ) + xFracture ) / dividerx4 ; // div with quarters
11211143 const int extrax4 = (enc -> coord32 .lonMicroDeg - b -> minx ) * 4 - (dx * dividerx4 ); // mod with quarters
11221144
1123- const int dividery = 90 ;
1145+ const int dividery = Y_DIVIDER ;
11241146 int dy = (b -> maxy - enc -> coord32 .latMicroDeg ) / dividery ;
11251147 int extray = (b -> maxy - enc -> coord32 .latMicroDeg ) % dividery ;
11261148
@@ -1130,7 +1152,7 @@ static void encodeNameless(char* result, const EncodeRec* enc, const enum Territ
11301152 }
11311153
11321154 if (IS_SPECIAL_SHAPE (m )) {
1133- SIDE = 1 + ((b -> maxy - b -> miny ) / 90 ); // new side, based purely on y-distance
1155+ SIDE = 1 + ((b -> maxy - b -> miny ) / Y_DIVIDER ); // new side, based purely on y-distance
11341156 xSIDE = (orgSIDE * orgSIDE ) / SIDE ;
11351157 v += encodeSixWide (dx , SIDE - 1 - dy , xSIDE , SIDE );
11361158 }
@@ -1141,15 +1163,15 @@ static void encodeNameless(char* result, const EncodeRec* enc, const enum Territ
11411163 encodeBase31 (result , v , codexlen + 1 ); // nameless
11421164 {
11431165 int dotp = codexlen ;
1144- if (codexm == 13 ) {
1166+ if (codexm == SPECIAL_CODEX_13 ) {
11451167 dotp -- ;
11461168 }
11471169 memmove (result + dotp , result + dotp - 1 , 4 );
11481170 result [dotp - 1 ] = '.' ;
11491171 }
11501172
11511173 if (!IS_SPECIAL_SHAPE (m )) {
1152- if (codexm == 22 && A < 62 && orgSIDE == 961 ) {
1174+ if (codexm == SPECIAL_CODEX_22 && A < MAX_NAMELESS_RECORDS && orgSIDE == GRID_SIZE_961 ) {
11531175 const char t = result [codexlen - 2 ];
11541176 result [codexlen - 2 ] = result [codexlen ];
11551177 result [codexlen ] = t ;
@@ -2416,7 +2438,12 @@ enum MapcodeError compareWithMapcodeFormatUtf16(const UWORD* Utf16String) {
24162438}
24172439
24182440
2419- // returns nonzero if error
2441+ /**
2442+ * Main decoder engine that converts a mapcode string to coordinates.
2443+ * Handles territory context, validates format, and iterates through
2444+ * possible territory boundaries to find the correct decoding.
2445+ * Returns ERR_OK on success, or appropriate error code on failure.
2446+ */
24202447static enum MapcodeError decoderEngine (DecodeRec * dec , int parseFlags ) {
24212448 enum Territory ccode ;
24222449 enum MapcodeError err ;
@@ -2428,9 +2455,10 @@ static enum MapcodeError decoderEngine(DecodeRec* dec, int parseFlags) {
24282455 int wasAllDigits = 0 ;
24292456 ASSERT (dec );
24302457
2458+ // Parse the mapcode string into its components (territory, proper mapcode, extension)
24312459 err = parseMapcodeString (& dec -> mapcodeElements , dec -> orginput , parseFlags , dec -> context );
24322460 if (err ) {
2433- // clear all parsed fields in case of error
2461+ // Clear all parsed fields in case of error to ensure clean state
24342462 dec -> mapcodeElements .territoryISO [0 ] = 0 ;
24352463 dec -> mapcodeElements .properMapcode [0 ] = 0 ;
24362464 dec -> mapcodeElements .precisionExtension [0 ] = 0 ;
0 commit comments