Example #1
0
        public static int StringToSize(this string text, SizeFormatEnum sizeFormat)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(0);
            }
            text = text.ToLower().TrimEnd();
            switch (sizeFormat)
            {
            case SizeFormatEnum.Last2AlphaNumeric:
                text = text.Substring(text.Length - 2, 2);
                break;

            case SizeFormatEnum.Rxx:
                var lastR = text.LastIndexOf('r');
                text = lastR >= 0 && text.Length - lastR > 1 ? text.Substring(text.LastIndexOf('r') + 1, 2) : "0";
                break;

            case SizeFormatEnum.Simple:
                break;

            case SizeFormatEnum.Vredestein:
                var firstRSpace = text.IndexOf("r ", StringComparison.OrdinalIgnoreCase);
                text = firstRSpace >= 0 && text.Length - firstRSpace > 1 ? text.Substring(firstRSpace + 2, 2) : "0";
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(sizeFormat), sizeFormat, null);
            }

            int.TryParse(text, out var size);
            return(size);
        }
        public void StringToSize_ShouldReturnSize_WhenInputIsOk(string text, SizeFormatEnum sizeFormat, int resultExpected)
        {
            var result = text.StringToSize(sizeFormat);

            Assert.AreEqual(resultExpected, result);
        }