public string Generate(RegexOperator rxop) { if (rxop == null) { return(""); } else { var sb = new StringBuilder(); GenerateStub(Root, rxop, sb); return(sb.ToString()); } }
public Migemo(Stream stream, RegexOperator regexOperator) { Dictionary = new CompactDictionary(stream); RegexOperator = regexOperator; }
static void GenerateStub(Node node, RegexOperator rxop, StringBuilder buf) { var escapeCharacters = "\\.[]{}()*+-?^$|".ToCharArray(); var brother = 1; var haschild = 0; for (var tmp = node; tmp != null; tmp = tmp.Next) { if (tmp.Next != null) { brother++; } if (tmp.Child != null) { haschild++; } } var nochild = brother - haschild; if (brother > 1 && haschild > 0) { buf.Append(rxop.BeginGroup); } if (nochild > 0) { if (nochild > 1) { buf.Append(rxop.BeginClass); } for (var tmp = node; tmp != null; tmp = tmp.Next) { if (tmp.Child != null) { continue; } if (Array.IndexOf(escapeCharacters, tmp.Code) != -1) { buf.Append("\\"); } buf.Append(tmp.Code); } if (nochild > 1) { buf.Append(rxop.EndClass); } } if (haschild > 0) { if (nochild > 0) { buf.Append(rxop.Or); } Node tmp; for (tmp = node; tmp.Child == null; tmp = tmp.Next) { } while (true) { if (Array.IndexOf(escapeCharacters, tmp.Code) != -1) { buf.Append("\\"); } buf.Append(tmp.Code); if (rxop.Newline != null) { buf.Append(rxop.Newline); } GenerateStub(tmp.Child, rxop, buf); for (tmp = tmp.Next; tmp != null && tmp.Child == null; tmp = tmp.Next) { } if (tmp == null) { break; } if (haschild > 1) { buf.Append(rxop.Or); } } } if (brother > 1 && haschild > 0) { buf.Append(rxop.EndGroup); } }
public Migemo(byte[] bytes, RegexOperator regexOperator) : this(new MemoryStream(bytes), regexOperator) { }