diff --git a/FractionStringConverter.vb b/FractionStringConverter.vb index ed4ccfa..c3ff96d 100644 --- a/FractionStringConverter.vb +++ b/FractionStringConverter.vb @@ -89,7 +89,7 @@ Public Module FractionStringConverter End Function ' riceve la stringa sorgente e restituisce la nuova strunga in formato decimale (senza eseguire conversioni di unità) - Public Function StringFractionToStringDecimal(sVal As String, ByRef sValConverted As String) As Boolean + Public Function OldStringFractionToStringDecimal(sVal As String, ByRef sValConverted As String) As Boolean Dim dVal As Double = 0 Dim bNegativeValue As Boolean = False ' dato in ingresso: sVal = 2'3"23/32 @@ -182,31 +182,82 @@ Public Module FractionStringConverter End If End Function - Public Function MyStringFractionToStringDecimal(sVal As String, ByRef sValConverted As String) As Boolean - Dim sPattern As String = "(-?) *(\d+ */? *\d*')? *(\d* */? *\d+-)? *(\d+ */? *\d* *"")?(\d+ */? *\d*)?" + Public Function StringFractionToStringDecimal(sVal As String, ByRef sValConverted As String) As Boolean + sValConverted = sVal + ' (-?) *(\d */? *\d*')? *(\d* */? *\d+-)? *(\d */? *\d* *""?)? *(\d */? *\d*)? + Dim sPattern As String = "(-?) *(\d */? *\d*')?( *(?(?=\d+ */ *\d+)\d+ */ *\d+|\d+)""?)*" Dim MyMatchCollection As MatchCollection = Regex.Matches(sVal, sPattern) For Each MyMatch As Match In MyMatchCollection - ' Loop over captures. + Dim bValueExists As Boolean = False + Dim bNegative As Boolean = False + Dim dFeet As Double = 0 + Dim bFeet As Boolean = True + Dim dInch As Double = 0 + Dim bInch As Boolean = True + Dim dFraction As Double = 0 + Dim bFraction As Boolean = True + Dim sCurrValue As String = String.Empty + Dim sCurrValueConverted As String = String.Empty + Dim dCurrValueConverted As Double = 0 - For Each MyGroup As Capture In MyMatch.Captures + For GroupIndex As Integer = 0 To MyMatch.Groups.Count - 1 + Dim MyGroup As Group = MyMatch.Groups(GroupIndex) Dim sGroupVal As String = MyGroup.Value - Select Case MyGroup.Index - Case 1 - ' riconosco il segno "-" ad inizio stringa - Case 2 - ' riconosco il valore di Feet " ' " - Case 3 - ' ...?... - Case 4 - ' riconosco il valore di Inch " "" " - Case 5 - ' riconosco il valore di frazione - End Select - + ' verifico che il gruppo non sia vuoto + If Not String.IsNullOrEmpty(sGroupVal) Or Not String.IsNullOrWhiteSpace(sGroupVal) Then + Select Case GroupIndex + Case 0 + ' se il valore è vuoto allora passo a prossimo Match + If String.IsNullOrEmpty(sGroupVal.Trim) Or String.IsNullOrWhiteSpace(sGroupVal.Trim) Then + Exit For + End If + ' salvo il valore del membro dell'espressione che deve essere valutato + sCurrValue = sGroupVal + bValueExists = True + Case 1 + ' riconosco il segno "-" ad inizio stringa + bNegative = True + Case 2 + ' riconosco il valore di Feet " ' " + sGroupVal = sGroupVal.Replace("'"c, "") + bFeet = StringToDouble(sGroupVal, dFeet) + Case 3 + For CaptureIndex As Integer = 0 To MyGroup.Captures.Count - 1 + Dim MyCapture As Capture = MyGroup.Captures(CaptureIndex) + Dim sCapture As String = MyCapture.Value + Select Case CaptureIndex + Case 0 + ' riconosco il valore in pollici + sCapture = sCapture.Replace(""""c, "") + bInch = StringToDouble(sCapture, dInch) + Case 1 + ' riconosco il valore di frazione + bFraction = StringToDouble(sCapture, dFraction) + End Select + Next + End Select + End If Next - Next + If bValueExists And bFeet And bInch And bFraction Then + ' ricavo il valore decimale + dCurrValueConverted = dFeet * 12 + dInch + dFraction + If bNegative Then dCurrValueConverted = -dCurrValueConverted + sCurrValueConverted = DoubleToString(dCurrValueConverted, 4) + ' sostituisco il valore calcolato nell'espressione iniziale + Dim nStartIndex As Integer = sValConverted.IndexOf(sCurrValue) + sValConverted = sValConverted.Remove(nStartIndex, sCurrValue.Count).Insert(nStartIndex, sCurrValueConverted) + End If + ' passo allavalutazione del prossimo membro dell'espressione + Next + Dim dValueConverted As Double = 0 + If StringToDouble(sValConverted, dValueConverted) Then + sValConverted = DoubleToString(dValueConverted, 4) + Else + ' Errore lettura stringa: sCurrVal non è convertibile + Return False + End If Return True End Function