public bool SetNumberVal(int size, SrcSizeType expectedSizeType, char sizeType) { var type = CharForSrcSetType(sizeType); if (type != expectedSizeType) { return(false); } Size = size; return(true); }
public static string Name(this SrcSizeType val) { switch (val) { case SrcSizeType.Width: return("width"); case SrcSizeType.PixelDensity: return("pixel"); default: case SrcSizeType.None: return("none"); } }
public static List <SrcSetImg> ParseSrcSets(string srcSetStr, out SrcSizeType sizeType) { sizeType = SrcSizeType.None; if (srcSetStr.IsNulle()) { return(null); } if (srcSetStr.Last() != ',') { srcSetStr += ','; // makes regex much easier / better, and allows consistent } var matches = _rxSrcSets .Matches(srcSetStr) .OfType <Match>() .Where(m => m.Groups.Count > 0) .ToArray(); List <SrcSetImg> srcs = new List <SrcSetImg>(); if (matches.Length > 0) { for (int i = 0; i < matches.Length; i++) { var m = matches[i]; var grps = m.Groups; if ((grps?.Count ?? 0).InRange(5, 6)) // final comma can be missing { string url = grps[1].Value.NullIfEmptyTrimmed(); int size = grps[3].Value.ToInt(-1); string sizeTypStr = grps[4].Value; if (url == null || size < 1 || sizeTypStr == null || sizeTypStr.Length != 1) { continue; } char sType = sizeTypStr[0]; if (i == 0) { sizeType = SrcSetImg.CharForSrcSetType(sType); if (sizeType == SrcSizeType.None) { return(null); } } SrcSetImg sImg = new SrcSetImg() { Url = url, SizeType = sizeType, Size = size }; if (!sImg.SetNumberVal(size, sizeType, sizeTypStr[0])) { continue; } srcs.Add(sImg); } } } if (srcs.IsNulle()) { return(null); } return(srcs); }