public string[] Split() { if (string.IsNullOrEmpty(this.Text) || this.Splitters == null || this.Splitters.Length == 0) { return(new string[0]); } this.Tokenizer.ResetPosition(); List <string> splitted = new List <string>(); while (!this.Tokenizer.Finish) { StringTokenResult tokenResult = this.Tokenizer.Tokenize(); if (this.SplitOptions.HasFlag(StringSplitOption.CrossEmptyValue) && string.IsNullOrEmpty(tokenResult.TokenText)) { continue; } if (this.OnSplit != null) { var handler = new StringSplitHandler(tokenResult.TokenText, tokenResult.TokenKey, splitted.Count); this.OnSplitEvent(handler); if (!handler.Cancel) { splitted.Add(handler.Text); } if (handler.Stop) { break; } } if (this.Count > 0 && splitted.Count >= this.Count) { break; } } return(splitted.ToArray()); }
public StringTokenResult Tokenize(params string[] token) { StringTokenResult tokenResult = new StringTokenResult(); tokenResult.TokenIndex = -1; if (token != null && token.Length > 0) { this.Tokens = token; } if (string.IsNullOrEmpty(this.Text) || this.Tokens == null || this.Tokens.Length == 0) { return(tokenResult); } if (this.CurrentPosition >= this.Text.Length) { return(tokenResult); } StringBuilder currentKey = new StringBuilder(); StringBuilder currentValue = new StringBuilder(); bool specialchar = false; bool isquote = false; bool quoted = false; char quotchar = '\0'; for (int i = this.CurrentPosition; i < this.Text.Length; i++) { char current = this.Text[i]; if (this.AllowSpecialChar && current == '\\' && !specialchar) { if (this.PrintSpecialCharacter) { currentValue.Append(current); } specialchar = true; continue; } bool continueNext = false; if (this.StringQuoteOption != StringQuoteOption.None) { if ((this.StringQuoteOption == StringQuoteOption.SingleQuote && current == '\'') || (this.StringQuoteOption == StringQuoteOption.DoubleQuote && current == '"')) { continueNext = true; } else if (this.StringQuoteOption == StringQuoteOption.All) { if (isquote) { if (current == quotchar) { continueNext = true; } } else { continueNext = true; quotchar = current; } } } if (continueNext && !specialchar) { if (isquote) { quoted = true; } isquote = !isquote; if (this.PrintSpecialCharacter) { currentValue.Append(current); } continue; } else { if (!isquote) { currentKey.Append(current); } if (currentKey.Length > this.Maxlen) { currentKey.Remove(0, 1); } if (currentKey.Length >= this.Maxlen && !isquote && !specialchar) { bool next = false; for (int j = 0; j < this.Tokens.Length; j++) { if (currentKey.ToString(0, this.Tokens[j].Length) == this.Tokens[j]) { currentKey.Remove(0, this.Tokens[j].Length); var value = ""; if (quoted && !this.AddToAll) { value = currentValue.ToString(); } else { value = currentValue.ToString(0, currentValue.Length - (this.Maxlen - 1)); } if (this.AutoTrim) { value = value.Trim(); } currentValue.Clear(); currentValue.Append(currentKey.ToString()); this.CurrentPosition = i + 1 - (this.Maxlen - this.Tokens[j].Length); tokenResult.TokenFound = true; tokenResult.TokenText = value; tokenResult.TokenKey = this.Tokens[j]; tokenResult.TokenIndex = this.CurrentPosition - (tokenResult.TokenKey.Length); return(tokenResult); } } if (next) { continue; } } if ((!quoted || AddToAll)) { currentValue.Append(current); } } if (specialchar) { specialchar = false; continue; } } if (currentValue.Length > 0) { string text = this.AutoTrim ? currentValue.ToString().Trim() : currentValue.ToString(); tokenResult.TokenText = text; } this.CurrentPosition = this.text.Length; this.Finish = true; return(tokenResult); }