@@ -22,6 +22,12 @@ public class ZplCode128Symbology
22
22
private static readonly Regex startCRegex = new Regex ( @"^\d{4}" , RegexOptions . Compiled ) ;
23
23
private static readonly Regex digitPairRegex = new Regex ( @"^\d\d" , RegexOptions . Compiled ) ;
24
24
25
+ // GS1 specific
26
+ private static readonly Regex gs1SwitchCRegex = new Regex ( @"^\d\d(?:\d\d|>8)+(?!\d)" , RegexOptions . Compiled ) ;
27
+ private static readonly Regex gs1StartCRegex = new Regex ( @"^(?:\d\d|>8){2}" , RegexOptions . Compiled ) ;
28
+ private static readonly Regex gs1DigitPairRegex = new Regex ( @"^(?:\d\d|>8)" , RegexOptions . Compiled ) ;
29
+ private static readonly Regex fnc1Regex = new Regex ( @"^>8" , RegexOptions . Compiled ) ;
30
+
25
31
private static readonly Regex startCodeRegex = new Regex ( @"^(>[9:;])(.+)$" , RegexOptions . Compiled ) ;
26
32
27
33
private static readonly Dictionary < string , int > invocationMap = new Dictionary < string , int > ( ) {
@@ -44,6 +50,12 @@ public class ZplCode128Symbology
44
50
{ ">;" , Code128CodeSet . Code128C }
45
51
} ;
46
52
53
+ private static readonly Dictionary < Code128CodeSet , string > codeSetCodeMap = new Dictionary < Code128CodeSet , string > ( ) {
54
+ { Code128CodeSet . Code128A , CODE_A } ,
55
+ { Code128CodeSet . Code128B , CODE_B } ,
56
+ { Code128CodeSet . Code128C , CODE_C }
57
+ } ;
58
+
47
59
private const string FNC_1 = "FNC_1" ;
48
60
private const string FNC_2 = "FNC_2" ;
49
61
private const string FNC_3 = "FNC_3" ;
@@ -208,7 +220,20 @@ static ZplCode128Symbology() {
208
220
public static ( List < bool > , string ) Encode ( string content , Code128CodeSet initialCodeSet , bool gs1 )
209
221
{
210
222
List < bool > result = new List < bool > ( ) ;
211
- var ( data , interpretation ) = Analyze ( content , initialCodeSet ) ;
223
+ List < int > data ;
224
+ string interpretation ;
225
+ if ( gs1 )
226
+ {
227
+ ( data , interpretation ) = AnalyzeGS1 ( content ) ;
228
+ }
229
+ else if ( initialCodeSet == Code128CodeSet . Code128 )
230
+ {
231
+ ( data , interpretation ) = AnalyzeAuto ( content ) ;
232
+ }
233
+ else
234
+ {
235
+ ( data , interpretation ) = Analyze ( content , initialCodeSet ) ;
236
+ }
212
237
213
238
// TODO: magic constant FNC_1
214
239
if ( gs1 && data [ 1 ] != 102 )
@@ -265,11 +290,6 @@ private static (List<int>, string) Analyze(string content, Code128CodeSet initia
265
290
startCodeMatch = startCodeRegex . Match ( content ) ;
266
291
}
267
292
268
- if ( codeSet == Code128CodeSet . Code128 )
269
- {
270
- return AnalyzeAuto ( content ) ;
271
- }
272
-
273
293
( var codeChars , var codeMap ) = codeMaps [ codeSet ] ;
274
294
275
295
data . Add ( ( int ) codeSet ) ;
@@ -303,8 +323,14 @@ private static (List<int>, string) Analyze(string content, Code128CodeSet initia
303
323
}
304
324
else if ( startCodeMap . ContainsKey ( symbol ) )
305
325
{
306
- codeSet = startCodeMap [ symbol ] ;
307
- ( codeChars , codeMap ) = codeMaps [ codeSet ] ;
326
+ Code128CodeSet newCodeSet = startCodeMap [ symbol ] ;
327
+ if ( newCodeSet != codeSet )
328
+ {
329
+ int value = codeMap [ codeSetCodeMap [ newCodeSet ] ] ;
330
+ data . Add ( value ) ;
331
+ codeSet = newCodeSet ;
332
+ ( codeChars , codeMap ) = codeMaps [ codeSet ] ;
333
+ }
308
334
}
309
335
else
310
336
{
@@ -322,6 +348,8 @@ private static (List<int>, string) Analyze(string content, Code128CodeSet initia
322
348
}
323
349
else
324
350
{
351
+ int value = codeMap [ CODE_B ] ;
352
+ data . Add ( value ) ;
325
353
codeSet = Code128CodeSet . Code128B ;
326
354
( codeChars , codeMap ) = codeMaps [ codeSet ] ;
327
355
}
@@ -387,5 +415,58 @@ private static (List<int>, string) AnalyzeAuto(string content)
387
415
return ( data , interpretation ) ;
388
416
}
389
417
418
+ private static ( List < int > , string ) AnalyzeGS1 ( string content )
419
+ {
420
+ List < int > data = new List < int > ( ) ;
421
+ string interpretation = "" ;
422
+ Code128CodeSet codeSet = Code128CodeSet . Code128B ;
423
+ var codeMap = codeBMap ;
424
+ if ( gs1StartCRegex . IsMatch ( content ) )
425
+ {
426
+ codeSet = Code128CodeSet . Code128C ;
427
+ codeMap = codeCMap ;
428
+ }
429
+ data . Add ( ( int ) codeSet ) ;
430
+
431
+ while ( content . Length > 0 )
432
+ {
433
+ if ( codeSet != Code128CodeSet . Code128C && gs1SwitchCRegex . IsMatch ( content ) )
434
+ {
435
+ data . Add ( codeMap [ CODE_C ] ) ;
436
+ codeSet = Code128CodeSet . Code128C ;
437
+ codeMap = codeCMap ;
438
+ }
439
+ else if ( codeSet == Code128CodeSet . Code128C && ! gs1DigitPairRegex . IsMatch ( content ) )
440
+ {
441
+ data . Add ( codeMap [ CODE_B ] ) ;
442
+ codeSet = Code128CodeSet . Code128B ;
443
+ codeMap = codeBMap ;
444
+ }
445
+ else if ( fnc1Regex . IsMatch ( content ) )
446
+ {
447
+ content = content . Substring ( 2 ) ;
448
+ data . Add ( codeMap [ FNC_1 ] ) ;
449
+ }
450
+ else
451
+ {
452
+ string symbol = content [ 0 ] . ToString ( ) ;
453
+ if ( codeSet == Code128CodeSet . Code128C )
454
+ {
455
+ symbol += content [ 1 ] ;
456
+ content = content . Substring ( 2 ) ;
457
+ }
458
+ else
459
+ {
460
+ content = content . Substring ( 1 ) ;
461
+ }
462
+
463
+ data . Add ( codeMap [ symbol ] ) ;
464
+ interpretation += symbol ;
465
+ }
466
+ }
467
+
468
+ return ( data , interpretation ) ;
469
+ }
470
+
390
471
}
391
472
}
0 commit comments