public static bool TryParseCsv <T>( this IPContext pContext, string input, out IEnumerable <ISortParticle <T> > particles ) where T : class { var particlesRaw = new List <ISortParticle <T> >(); var generators = pContext.GetGenerators <T, ISortParticle <T> >(); var splitInput = input .Split(','); bool wasAbleToParse = true; foreach (var sort in splitInput) { ISortParticle <T> particle = null; wasAbleToParse &= generators.Any(x => x.TryGenerate(pContext, sort, out particle)); if (!(particle is null)) { particlesRaw.Add(particle); } } particles = wasAbleToParse ? particlesRaw : Array.Empty <ISortParticle <T> >(); return(wasAbleToParse); }
public static bool TryParseCsv <T>( this IPContext pContext, string input, out IEnumerable <IFilterParticle <T> > particles ) where T : class { var particlesRaw = new List <IFilterParticle <T> >(); var generators = new List <IParticleGenerator <IFilterParticle <T> > >() { // Default generators new ComplexLogicGenerator <T>() }; generators.AddRange(pContext.GetGenerators <T, IFilterParticle <T> >()); // Matching commas outside of quotes and parentheses Regex regx = new Regex("[,](?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))(?=(((?!\\)).)*\\()|[^\\(\\)]*$)"); var splitInput = regx.Split(input); bool wasAbleToParse = true; foreach (var filter in splitInput) { IFilterParticle <T> particle = null; wasAbleToParse &= generators.Any(x => x.TryGenerate(pContext, filter, out particle)); if (!(particle is null)) { particlesRaw.Add(particle); } } particles = wasAbleToParse ? particlesRaw : Array.Empty <IFilterParticle <T> >(); return(wasAbleToParse); }