diff --git a/src/Microsoft.ML.Data/Data/Conversion.cs b/src/Microsoft.ML.Data/Data/Conversion.cs index 2ea1717cb4..1921d3f852 100644 --- a/src/Microsoft.ML.Data/Data/Conversion.cs +++ b/src/Microsoft.ML.Data/Data/Conversion.cs @@ -1585,6 +1585,17 @@ public void Convert(in TX span, ref I4 value) { value = ParseI4(in span); } + public bool TryConvert(in TX span, ref I4 value) + { + TryParseSigned(I4.MaxValue, in span, out long? res); + if (res.HasValue) + { + value = (I4)res.GetValueOrDefault(); + return true; + } + + return false; + } public void Convert(in TX span, ref U4 value) { value = ParseU4(in span); diff --git a/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoaderParser.cs b/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoaderParser.cs index 7c7305c723..3d316cf641 100644 --- a/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoaderParser.cs +++ b/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoaderParser.cs @@ -1016,18 +1016,8 @@ public int GatherFields(ReadOnlyMemory lineSpan, ReadOnlySpan span, } var spanT = Fields.Spans[Fields.Count - 1]; - // Note that Convert throws exception the text is unparsable. - int csrc = default; - try - { - Conversions.Instance.Convert(in spanT, ref csrc); - } - catch - { - Contracts.Assert(csrc == default); - } - - if (csrc <= 0) + int csrc = 0; + if (!Conversions.Instance.TryConvert(in spanT, ref csrc) || csrc <= 0) { _stats.LogBadFmt(ref scan, "Bad dimensionality or ambiguous sparse item. Use sparse=- for non-sparse file, and/or quote the value."); break;