@@ -27,6 +27,9 @@ final _hex = <String>[
27
27
'e' ,
28
28
'f' ,
29
29
];
30
+ final _numericPattern = RegExp (r'^(\d*)(\.\d*)?$' );
31
+ final _patternLeadingZeros = RegExp (r'^0+' );
32
+ final _patternTrailingZeros = RegExp (r'0+$' );
30
33
31
34
class PostgresBinaryEncoder extends Converter <dynamic , Uint8List ?> {
32
35
final PostgreSQLDataType _dataType;
@@ -313,22 +316,18 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List?> {
313
316
} else if (value.startsWith ('+' )) {
314
317
value = value.substring (1 );
315
318
}
316
- final numericPattern = RegExp (r'^(\d*)(\.\d*)?$' );
317
- if (! numericPattern.hasMatch (value)) {
319
+ if (! _numericPattern.hasMatch (value)) {
318
320
throw FormatException ('Invalid format for parameter value. Expected: String which matches "/^(\\ d*)(\\ .\\ d*)?\$ /" Got: ${value }' );
319
321
}
320
322
final parts = value.split ('.' );
321
323
322
- final patternLeadingZeros = RegExp (r'^0+' );
323
- final patternTrailingZeros = RegExp (r'0+$' );
324
-
325
- var intPart = parts[0 ].replaceAll (patternLeadingZeros, '' );
324
+ var intPart = parts[0 ].replaceAll (_patternLeadingZeros, '' );
326
325
var intWeight = intPart.isEmpty ? - 1 : (intPart.length - 1 ) ~ / 4 ;
327
326
intPart = intPart.padLeft ((intWeight + 1 ) * 4 , '0' );
328
327
329
328
var fractPart = parts.length > 1 ? parts[1 ] : '' ;
330
329
final dScale = fractPart.length;
331
- fractPart = fractPart.replaceAll (patternTrailingZeros , '' );
330
+ fractPart = fractPart.replaceAll (_patternTrailingZeros , '' );
332
331
var fractWeight = fractPart.isEmpty ? - 1 : (fractPart.length - 1 ) ~ / 4 ;
333
332
fractPart = fractPart.padRight ((fractWeight + 1 ) * 4 , '0' );
334
333
@@ -339,7 +338,7 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List?> {
339
338
// Weight of value 0 or '' is 0;
340
339
weight = 0 ;
341
340
} else {
342
- final leadingZeros = patternLeadingZeros .firstMatch (fractPart)? .group (0 );
341
+ final leadingZeros = _patternLeadingZeros .firstMatch (fractPart)? .group (0 );
343
342
if (leadingZeros != null ) {
344
343
final leadingZerosWeight = leadingZeros.length ~ / 4 ; // Get count of leading zeros '0000'
345
344
fractPart = fractPart.substring (leadingZerosWeight * 4 ); // Remove leading zeros '0000'
@@ -349,7 +348,7 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List?> {
349
348
}
350
349
} else if (fractWeight < 0 ) {
351
350
// If int fract has no weight, handle trailing zeros in int part.
352
- final trailingZeros = patternTrailingZeros .firstMatch (intPart)? .group (0 );
351
+ final trailingZeros = _patternTrailingZeros .firstMatch (intPart)? .group (0 );
353
352
if (trailingZeros != null ) {
354
353
final trailingZerosWeight = trailingZeros.length ~ / 4 ; // Get count of trailing zeros '0000'
355
354
intPart = intPart.substring (0 , intPart.length - trailingZerosWeight * 4 ); // Remove leading zeros '0000'
@@ -370,8 +369,7 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List?> {
370
369
for (var i = 0 ; i <= fractWeight * 4 ; i += 4 ) {
371
370
writer.writeInt16 (int .parse (fractPart.substring (i, i + 4 )));
372
371
}
373
- final as = writer.toBytes ();
374
- return as ;
372
+ return writer.toBytes ();
375
373
}
376
374
}
377
375
@@ -574,7 +572,7 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
574
572
intPart += '0000' * (weight + 1 );
575
573
}
576
574
577
- var result = '$sign ${intPart .replaceAll (RegExp ( r'^0+' ) , '' )}' ;
575
+ var result = '$sign ${intPart .replaceAll (_patternLeadingZeros , '' )}' ;
578
576
if (result.isEmpty) result = '0' ; // Show at least 0, if no int value is given.
579
577
if (dScale > 0 ) {
580
578
// Only add fractional digits, if dScale allows
0 commit comments