/// <summary> /// Checks whether a new unit abbreviation can be defined (or may be redefined) in this context. /// </summary> /// <param name="a">The proposed abbreviation.</param> /// <param name="autoStandardPrefix"> /// Whether the new unit must support automatic metric and/or binary standard prefixes. /// </param> /// <returns>The <see cref="NewAbbreviationConflict"/> flag.</returns> public NewAbbreviationConflict CheckValidNewAbbreviation(string a, AutoStandardPrefix autoStandardPrefix) { if (_allUnits.ContainsKey(a)) { return(NewAbbreviationConflict.Exists); } if (String.IsNullOrEmpty(a) || !a.All(c => Char.IsLetter(c) || Char.IsSymbol(c) || c == '#' || c == '%' || c == '‰' || c == '‱' || c == '㏙')) { return(NewAbbreviationConflict.InvalidCharacters); } foreach (var withPrefix in MeasureStandardPrefix.GetPrefixes(autoStandardPrefix) .Select(p => p.Abbreviation + a)) { if (_allUnits.ContainsKey(withPrefix)) { return(NewAbbreviationConflict.AmbiguousAutoStandardPrefix); } } // Optimization: if the new abbreviation does not start with a // standard prefix, it is useless to challenge it against // existing units. var prefix = MeasureStandardPrefix.FindPrefix(a); if (prefix != MeasureStandardPrefix.None && _allUnits.Values .OfType <AtomicMeasureUnit>() .Where(u => u.AutoStandardPrefix != AutoStandardPrefix.None) .SelectMany(u => MeasureStandardPrefix.GetPrefixes(u.AutoStandardPrefix) .Where(p => p != MeasureStandardPrefix.None) .Select(p => p.Abbreviation + u.Abbreviation)) .Any(exists => exists == a)) { return(NewAbbreviationConflict.MatchPotentialAutoStandardPrefixedUnit); } return(NewAbbreviationConflict.None); }
/// <summary> /// Checks whether a new unit abbreviation can be defined in this context. /// </summary> /// <param name="a">The proposed abbreviation.</param> /// <param name="autoStandardPrefix"> /// Whether the new unit must support automatic metric and/or binary standard prefixes. /// </param> /// <returns>True if the abbreviation can be used, false otherwise.</returns> public bool IsValidNewAbbreviation(string a, AutoStandardPrefix autoStandardPrefix) { if (String.IsNullOrEmpty(a) || !a.All(c => Char.IsLetter(c) || Char.IsSymbol(c) || c == '#' || c == '%' || c == '‰' || c == '‱' || c == '㏙')) { return(false); } foreach (var withPrefix in MeasureStandardPrefix.GetPrefixes(autoStandardPrefix) .Select(p => p.Abbreviation + a)) { if (_allUnits.ContainsKey(withPrefix)) { return(false); } } var prefix = MeasureStandardPrefix.FindPrefix(a); // Optimization: if the new abbreviation does not start with a // standard prefix, it is useless to challenge it against // existing units. if (prefix != MeasureStandardPrefix.None) { return(!_allUnits.Values .OfType <AtomicMeasureUnit>() .Where(u => u.AutoStandardPrefix != AutoStandardPrefix.None) .SelectMany(u => MeasureStandardPrefix.GetPrefixes(u.AutoStandardPrefix) .Where(p => p != MeasureStandardPrefix.None) .Select(p => p.Abbreviation + u.Abbreviation)) .Any(exists => exists == a)); } return(true); }