/// <summary> /// <see cref="IUnicodeReplacer"/> API /// </summary> public virtual string ToReplacerPattern(bool escapeUnprintable) { StringBuffer rule = new StringBuffer(); StringBuffer quoteBuf = new StringBuffer(); int cursor = cursorPos; // Handle a cursor preceding the output if (hasCursor && cursor < 0) { while (cursor++ < 0) { Utility.AppendToRule(rule, '@', true, escapeUnprintable, quoteBuf); } // Fall through and append '|' below } for (int i = 0; i < output.Length; ++i) { if (hasCursor && i == cursor) { Utility.AppendToRule(rule, '|', true, escapeUnprintable, quoteBuf); } char c = output[i]; // Ok to use 16-bits here IUnicodeReplacer r = data.LookupReplacer(c); if (r == null) { Utility.AppendToRule(rule, c, false, escapeUnprintable, quoteBuf); } else { StringBuffer buf = new StringBuffer(" "); buf.Append(r.ToReplacerPattern(escapeUnprintable)); buf.Append(' '); Utility.AppendToRule(rule, buf.ToString(), true, escapeUnprintable, quoteBuf); } } // Handle a cursor after the output. Use > rather than >= because // if cursor == output.length() it is at the end of the output, // which is the default position, so we need not emit it. if (hasCursor && cursor > output.Length) { cursor -= output.Length; while (cursor-- > 0) { Utility.AppendToRule(rule, '@', true, escapeUnprintable, quoteBuf); } Utility.AppendToRule(rule, '|', true, escapeUnprintable, quoteBuf); } // Flush quoteBuf out to result Utility.AppendToRule(rule, -1, true, escapeUnprintable, quoteBuf); return(rule.ToString()); }
/// <summary> /// Create a source string that represents this rule. Append it to the /// given string. /// </summary> public virtual string ToRule(bool escapeUnprintable) { // int i; StringBuffer rule = new StringBuffer(); // Accumulate special characters (and non-specials following them) // into quoteBuf. Append quoteBuf, within single quotes, when // a non-quoted element must be inserted. StringBuffer quoteBuf = new StringBuffer(); // Do not emit the braces '{' '}' around the pattern if there // is neither anteContext nor postContext. bool emitBraces = (anteContext != null) || (postContext != null); // Emit start anchor if ((flags & ANCHOR_START) != 0) { rule.Append('^'); } // Emit the input pattern Utility.AppendToRule(rule, anteContext, escapeUnprintable, quoteBuf); if (emitBraces) { Utility.AppendToRule(rule, '{', true, escapeUnprintable, quoteBuf); } Utility.AppendToRule(rule, key, escapeUnprintable, quoteBuf); if (emitBraces) { Utility.AppendToRule(rule, '}', true, escapeUnprintable, quoteBuf); } Utility.AppendToRule(rule, postContext, escapeUnprintable, quoteBuf); // Emit end anchor if ((flags & ANCHOR_END) != 0) { rule.Append('$'); } Utility.AppendToRule(rule, " > ", true, escapeUnprintable, quoteBuf); // Emit the output pattern Utility.AppendToRule(rule, output.ToReplacerPattern(escapeUnprintable), true, escapeUnprintable, quoteBuf); Utility.AppendToRule(rule, ';', true, escapeUnprintable, quoteBuf); return(rule.ToString()); }
/// <summary> /// <see cref="IUnicodeReplacer"/> API /// </summary> public virtual string ToReplacerPattern(bool escapeUnprintable) { StringBuilder rule = new StringBuilder("&"); rule.Append(translit.ID); rule.Append("( "); rule.Append(replacer.ToReplacerPattern(escapeUnprintable)); rule.Append(" )"); return(rule.ToString()); }