parseLocalized function
ParseResult<ByteConverter>
parseLocalized(
- String input, {
- String? locale,
- ByteStandard standard = ByteStandard.si,
Parses a size string using locale-aware unit words and number formats when available. Uses resolveLocalizedUnitSymbol and number normalization to reconstruct a canonical parse input before delegating to the core parser.
Implementation
ParseResult<ByteConverter> parseLocalized(
String input, {
String? locale,
ByteStandard standard = ByteStandard.si,
}) {
final text = input.trim();
if (text.isEmpty) {
return ParseResult.failure(
originalInput: input,
error: const ParseError(message: 'Empty input'),
);
}
// Split numeric and unit parts heuristically
final m = RegExp(r'^\s*([+-]?[0-9\.,\u00A0_\s]+)\s*([\p{L}A-Za-z]+)?\s*$',
unicode: true)
.firstMatch(text);
if (m == null) {
return ParseResult.failure(
originalInput: input,
error: ParseError(message: 'Invalid format: $input'),
normalizedInput: text,
);
}
final numPart = (m.group(1) ?? '').trim();
final unitPartRaw = (m.group(2) ?? '').trim();
// Normalize number by inferring decimal separator (last of [.,]) and stripping grouping
String normalizedNumber = _normalizeLocalizedNumber(numPart);
String? resolvedSymbol;
if (unitPartRaw.isNotEmpty) {
resolvedSymbol = resolveLocalizedUnitSymbol(unitPartRaw, locale: locale);
}
final reconstructed = resolvedSymbol == null
? normalizedNumber
: '$normalizedNumber $resolvedSymbol';
try {
final r = parseSize<SizeUnit>(input: reconstructed, standard: standard);
final value = ByteConverter(r.valueInBytes);
return ParseResult.success(
originalInput: input,
value: value,
normalizedInput: r.normalizedInput,
detectedUnit: r.unitSymbol,
isBitInput: r.isBitInput,
parsedNumber: r.rawValue,
);
} on FormatException catch (e) {
return ParseResult.failure(
originalInput: input,
error: ParseError(
message: e.message,
position: e.offset,
exception: e,
),
normalizedInput: reconstructed,
);
}
}