public static Symbol Load(BinaryReader br) { SymbolType type = (SymbolType)br.ReadByte(); Symbol sym; switch (type) { case PunctuationSymbol.SymType: sym = new PunctuationSymbol((PunctuationType)br.ReadByte()); break; case WordSymbol.SymType: sym = new WordSymbol(br.ReadString()); break; case TerminateSymbol.SymType: sym = new TerminateSymbol(); break; case UrlSymbol.SymType: sym = new UrlSymbol(br.ReadString()); break; default: throw new InvalidOperationException(); } return(sym); }
public static IEnumerable <Symbol> Parse(string input) { if (input == null) { yield break; } var buf = new StringBuilder(); for (int i = 0; i < input.Length; i++) { char ch = input[i]; if (!WordSymbol.IsWordChar(ch) && buf.Length > 0) { if (ch == ':' && (buf.ToString() == "http" || buf.ToString() == "https")) { for (; i < input.Length && !char.IsWhiteSpace(input[i]); i++) { buf.Append(input[i]); } string url = buf.ToString(); buf.Length = 0; yield return(new UrlSymbol(url)); continue; } string word = buf.ToString().ToUpper(); buf.Length = 0; yield return(new WordSymbol(word)); } if (WordSymbol.IsWordChar(ch)) { buf.Append(ch); continue; } if (PunctuationSymbol.IsPunctuationChar(ch)) { yield return(new PunctuationSymbol(ch)); } } if (buf.Length > 0) { yield return(new WordSymbol(buf.ToString().ToUpper())); } yield return(new TerminateSymbol()); }
public static Symbol Load(BinaryReader br) { SymbolType type = (SymbolType)br.ReadByte(); Symbol sym; switch (type) { case PunctuationSymbol.SymType: sym = new PunctuationSymbol((PunctuationType)br.ReadByte()); break; case WordSymbol.SymType: sym = new WordSymbol(br.ReadString()); break; case TerminateSymbol.SymType: sym = new TerminateSymbol(); break; case UrlSymbol.SymType: sym = new UrlSymbol(br.ReadString()); break; default: throw new InvalidOperationException(); } return sym; }