Skip to content

Commit c0e26a7

Browse files
authored
Fixes: handle empty segments and relax UnitOfMeasure validation for HBIC #39
2 parents bc30f12 + 351fd90 commit c0e26a7

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

BarcodeParserBuilder.UnitTests/Barcodes/HIBC/HibcBarcodeParserBuilderTestFixture.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,21 @@ public void CanBuildBarcodeString(string expectedBarcode, HibcBarcode barcode)
211211
UnitOfMeasure = 9,
212212
Quantity = 100
213213
}
214-
},
214+
},
215+
// HIBC 2.3.2.4 has note about Q, saying that that Q should (not shall!) be used with UOM of 9. So Q with other UOMs is also valid.
216+
{
217+
"]d1+EFOR78712011/$26485266/16D20240626/Q10S",
218+
new HibcBarcode(AimSymbologyIdentifier.ParseString("]d1"))
219+
{
220+
LabelerIdentificationCode = "EFOR",
221+
ProductCode = TestProductCode.CreateProductCode<HibcProductCode>("7871201"),
222+
UnitOfMeasure = 1, // Valid UOM other than 9 with Q
223+
Quantity = 10,
224+
BatchNumber = "26485266",
225+
ProductionDate = new TestBarcodeDateTime(new DateTime(2024, 06, 26), "20240626", "yyyyMMdd")
226+
227+
}
228+
}
215229
};
216230

217231
public static TheoryData<string, HibcBarcode> ValidHibcBuildingBarcodes() => new()
@@ -480,11 +494,6 @@ public void InvalidBarcodeStringThrowsException(string barcode, string expectedM
480494
"+A123BJC5D6E71G+$$52001510X3+ ",
481495
$"Failed to parse HIBC Barcode :{Environment.NewLine}Link Character did not match: expected 'G' but got '+'."
482496
},
483-
484-
// Using Quantity /Q requires UnitOfMeasure of 9!
485-
{
486-
"+A99912341/$10X3/16D20111231/14D20200131/Q500R",
487-
$"Failed to parse HIBC Barcode :{Environment.NewLine}Using Quantity /Q requires UnitOfMeasure of 9!"
488-
},
497+
489498
};
490499
}

BarcodeParserBuilder/Barcodes/HIBC/HibcBarcodeParserBuilder.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ protected static IList<string> BuildSegments(HibcBarcode? barcode)
191191
if (string.IsNullOrWhiteSpace(barcodeString))
192192
return null;
193193

194-
barcodeString = symbologyIdentifier?.StripSymbologyIdentifier(barcodeString!) ?? barcodeString!;
194+
barcodeString = symbologyIdentifier?.StripSymbologyIdentifier(barcodeString!) ?? barcodeString!;
195+
196+
if (string.IsNullOrWhiteSpace(barcodeString))
197+
return null;
198+
195199
if (!Regex.IsMatch(barcodeString, HibcCheckCharacterCalculator.AllowedCharacterRegex))
196200
throw new HIBCParseException("Invalid HIBC Character detected.");
197201

@@ -288,8 +292,7 @@ protected static IList<string> BuildSegments(HibcBarcode? barcode)
288292

289293
break;
290294
case 'Q': // Quantity /Q as of Spec 2.6 version
291-
if (barcode.UnitOfMeasure != 9)
292-
throw new HIBCParseException($"Using Quantity /Q requires UnitOfMeasure of 9!");
295+
// HIBC 2.3.2.4 states that quantity should be used with UoM 9 but it is not strictly enforced
293296
segmentData = segmentData[1..];
294297
barcode.Quantity = int.Parse(segmentData);
295298
break;
@@ -305,7 +308,9 @@ protected static IList<string> BuildSegments(HibcBarcode? barcode)
305308
continue;
306309

307310
var isMultiDataLine = segmentData.StartsWith("$$", StringComparison.Ordinal);
308-
segmentData = segmentData[(isMultiDataLine ? 2 : 1)..];
311+
segmentData = segmentData[(isMultiDataLine ? 2 : 1)..];
312+
if (segmentData.Length == 0)
313+
continue;
309314
var isSerialLine = segmentData.First() == '+';
310315
if (isSerialLine)
311316
segmentData = segmentData[1..];

0 commit comments

Comments
 (0)