using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MarketData.Utils; namespace MarketDataLib.Utility { public class Sections { private Sections() { } // returns the length of string containing the section. // Given: "kaskHSkahsKAHSLDFDLFG
......
ALaks;LKSA;ks;a;SKL;ks;aKS;" // will return
.....
public static String GetItemsInSection(String strInput,String sectionName,ref int searchIndex) { String startSection="<"+sectionName; String endSection=" GetAllItemsInSections(String strInput, String sectionName) { int searchIndex = 0; List sectionItems = new List(); while (true) { String itemsInSection = GetItemsInSection(strInput, sectionName, ref searchIndex); if(null==itemsInSection)break; sectionItems.Add(itemsInSection); searchIndex++; } return sectionItems; } public static List GetSections(String strInput) { try { List sections=new List(); int index=0; String strLiteral=null; if(null==strInput)return null; while(index') { strLiteral=ScanSection(strInput,ref index); if(null!=strLiteral&&!":??".Equals(strLiteral)) { if (strLiteral.Contains("&")) strLiteral = strLiteral.Replace("&", "&"); if(strLiteral.Contains(""")) strLiteral=strLiteral.Replace(""","'"); if(strLiteral.Contains("'")) strLiteral=strLiteral.Replace("'","'"); if(strLiteral.Contains("—")) strLiteral=strLiteral.Replace("—","-"); if(strLiteral.Contains(" ")) strLiteral=strLiteral.Replace(" "," "); sections.Add(strLiteral); } } index++; } return sections; } catch(Exception) { return null; } } public static bool FindInSections(List sections,String startsWith,int startingIndex,ref int indexOfItem,bool findExact=true) { for(int index=startingIndex;index sections,String startsWith,int startingIndex,ref int indexOfItem,bool findExact=true) { for(int index=startingIndex;index>=0;index--) { if(!findExact &§ions[index].StartsWith(startsWith)) { indexOfItem=index; return true; } else if(sections[index].Equals(startsWith)) { indexOfItem=index; return true; } } return false; } public static String ScanSectionsFindStartWithReturnIndexAfter(List sections, String startsWith, int indexAfter) { if (null == sections || 0 == sections.Count) return null; for (int index = 0; index < sections.Count; index++) { String sectionItem = sections[index]; if (null == sectionItem || "".Equals(sectionItem)||!sectionItem.StartsWith(startsWith)) continue; return sections[index + indexAfter]; } return null; } public static List LocateItems(String strInput,String item,int startIndex,int iterator) { List items=new List(); try { if(null==item||null==strInput)return null; int startPos=strInput.IndexOf(item); if(-1==startPos)return null; strInput=strInput.Substring(startPos-1); List sections=Sections.GetSections(strInput); if(0==sections.Count||sections.Count<=startIndex)return null; if(!item.Contains(sections[0])||"".Equals(sections[0]))return null; for(;startIndex GetItemSections(String strInput,String item) { int startIndex=0; List items=new List(); try { if(null==item||null==strInput)return null; int startPos=strInput.IndexOf(item); if(-1==startPos)return null; strInput=strInput.Substring(startPos-1); List sections=Sections.GetSections(strInput); if(0==sections.Count||sections.Count<=startIndex)return null; return sections; } catch(Exception) { return null; } } public static List LocateItems(String strInput,String item) { int startIndex=0; List items=new List(); try { if(null==item||null==strInput)return null; int startPos=strInput.IndexOf(item); if(-1==startPos)return null; strInput=strInput.Substring(startPos-1); List sections=Sections.GetSections(strInput); if(0==sections.Count||sections.Count<=startIndex)return null; if(!item.Contains(sections[0])||"".Equals(sections[0]))return null; for(;startIndex sections=Sections.GetSections(strInput); if(null==sections||0==sections.Count)return null; if(!item.Contains(sections[0])||"".Equals(sections[0]))return null; startIndex++; for(;startIndexmaxDepth)return null; continue; } strItem=sectionItem; break; } return strItem; } catch(Exception) { return null; } } public static String LocateItem(String strInput,String item) { int startIndex=0; String strItem=null; try { if(null==item||null==strInput)return null; int startPos=strInput.IndexOf(item); if(-1==startPos)return null; strInput=strInput.Substring(startPos-1); List sections=Sections.GetSections(strInput); if(null==sections||0==sections.Count)return null; if(!item.Contains(sections[0])||"".Equals(sections[0]))return null; for(;startIndex sections=Sections.GetSections(strInput); if(null==sections||0==sections.Count)return null; if(!item.Contains(sections[0])||"".Equals(sections[0]))return null; for(;startIndex sections=Sections.GetSections(strInput); if(null==sections||0==sections.Count)return null; if(!item.Contains(sections[0])||"".Equals(sections[0]))return null; for(;startIndex sections=Sections.GetSections(strInput); if(null==sections||0==sections.Count)return null; if(!item.Contains(sections[0])||"".Equals(sections[0]))return null; for(;startIndex sections=Sections.GetSections(strInput); strItem=Sections.ScanSectionsFindStartWithReturnIndexAfter(sections,item,depth); if(null==strItem||strItem.Equals("-"))return null; return strItem; } catch(Exception) { return null; } } public static String LocateFirstItem(String strInput,String item) { int startIndex=0; String strItem=null; try { if(null==item||null==strInput)return null; int startPos=strInput.IndexOf(item); if(-1==startPos)return null; strInput=strInput.Substring(startPos-1); List sections=Sections.GetSections(strInput); if(null==sections||0==sections.Count)return null; if(!item.Contains(sections[0])||"".Equals(sections[0]))return null; for(;startIndex", "<"); return strItem; } private static String ScanSection(String strInput, ref int index) { try { StringBuilder sb=new StringBuilder(); index++; while(index