本文整理汇总了C#中Stack.FirstOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.FirstOrDefault方法的具体用法?C# Stack.FirstOrDefault怎么用?C# Stack.FirstOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.FirstOrDefault方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Parse
public static ParsedData Parse(string data)
{
if (data != null && data.Length > 0)
{
var curr = Start (data[0]);
var stk = new Stack<Token> ();
stk.Push (curr);
for (var i = 1; i < data.Length; i++)
{
curr = curr.Parse (data[i]);
if (stk.Peek () != curr)
stk.Push (curr);
}
if (stk.Any (x => x is Error))
return new ParsedData ();
else
{
var title = stk.FirstOrDefault (x => x is Title);
var id = stk.FirstOrDefault (x => x is ID);
var path = stk.FirstOrDefault (x => x is Path);
return new ParsedData (title != null ? title.ToString () : null, path != null ? path.ToString () : null, id != null ? id.ToString () : null);
}
}
else
return new ParsedData ();
}
示例2: ParagraphHandler
private string ParagraphHandler(string paragraph, DiaryMode mode)
{
var pbuilder = new StringBuilder();
var paragraphPlugins = new Stack<IDiaryParagraphPlugin>(Plugins.OfType<IDiaryParagraphPlugin>());
var linePlugins = new Stack<IDiaryLinePlugin>(Plugins.OfType<IDiaryLinePlugin>());
var linkPlugins = new Stack<IDiaryLinkPlugin>(Plugins.OfType<IDiaryLinkPlugin>());
Action<IList<string>> prevYield = lines => {
// do line plugins
while (linePlugins.Count > 0) {
var plugin = linePlugins.Pop();
lines = lines.SelectMany(x => plugin.Handle(x, mode)).ToArray();
}
var linkRegex = new Regex(@"\[([^\]]+)\]");
var result = lines.Select(l => linkRegex.Replace(l, me => {
var content = me.Groups[1].Value;
var plugin = linkPlugins.FirstOrDefault(lp => lp.CanHandle(content, mode));
return plugin == null ? content : me.Result(plugin.Handle(content, mode));
}));
// join final result into one string
pbuilder.Append(string.Join("\r\n", result));
};
while (paragraphPlugins.Count > 0) {
var plugin = paragraphPlugins.Pop();
var wrappedYield = prevYield;
prevYield = l => plugin.Handle(l, mode, pbuilder, wrappedYield);
}
prevYield(paragraph.Split(new[] { "\r\n" }, StringSplitOptions.None));
return pbuilder.ToString();
}
示例3: Evaluate
//.........这里部分代码省略.........
break;
case PartType.Parenthesis:
if (CurrentPart.Value[0] == '(')
if (!needValue)
throw new Exception("Неуместная открывающая скобка: позиция " + CurrentPart.Position.ToString());
else
depth++;
else
if (depth == 0 || needValue)
throw new Exception("Неуместная закрывающая скобка: позиция " + CurrentPart.Position.ToString());
else
depth--;
break;
case PartType.Sign:
Operation CurrentOperation;
if (needValue)
if (CurrentPart.Value[0] == '-')
{
CurrentOperation.Sign = '~';
CurrentOperation.Precedence = depth * 10 + 1;
OpStack.Push(CurrentOperation);
}
else
throw new Exception("Неуместный знак: (" + CurrentPart.Value.ToString() + "): позиция "
+ CurrentPart.Position.ToString());
else
{
CurrentOperation.Sign = CurrentPart.Value[0];
CurrentOperation.Precedence = 0;
switch (CurrentPart.Value[0])
{
case '+':
CurrentOperation.Precedence = depth * 10 + 1;
break;
case '-':
CurrentOperation.Precedence = depth * 10 + 1;
break;
case '*':
CurrentOperation.Precedence = depth * 10 + 2;
break;
case '/':
CurrentOperation.Precedence = depth * 10 + 2;
break;
case '^':
CurrentOperation.Precedence = depth * 10 + 3;
break;
}
Reduce(CurrentOperation.Precedence);
OpStack.Push(CurrentOperation);
needValue = true;
}
break;
case PartType.Identifier:
if (!needValue)
throw new Exception("Неуместный идиентификатор: (" + CurrentPart.Value.ToString() +
"): позиция " + CurrentPart.Position.ToString());
foreach (Identifier CurrentIdentifier in Identifiers)
{
if ((CurrentPart.Value).ToLower() == (CurrentIdentifier.IdentifierString).ToLower())
{
ArgStack.Push(CurrentIdentifier.Value);
IsNew = false;
}
else
IsNew = true;
if (IsNew == false)
break;
}
if (IsNew == true)
{
Console.WriteLine("Введите значение идиентификатора {0:s}:", CurrentPart.Value);
ID.IdentifierString = CurrentPart.Value;
bool Check = double.TryParse(Console.ReadLine(), out ID.Value);
if (Check == false)
throw new Exception("Неверное значение идиентификатора (" + CurrentPart.Value + ')');
ArgStack.Push(ID.Value);
Identifiers.Add(ID);
}
needValue = false;
break;
}
// Вывод списка лексем с текущими состояниями стеков аргументов и операций
Console.WriteLine("__________________");
Console.WriteLine("Обработка лексемы № {0:d}", n);
Console.WriteLine("Стек аргументов: ");
foreach (double Arg in ArgStack)
Console.WriteLine("{0:g}", Arg.ToString());
Console.WriteLine("Стек операций:");
foreach (Operation Op in OpStack)
Console.WriteLine("{0:g} ({1:d})", Op.Sign.ToString(), Op.Precedence.ToString());
}
Reduce(0);
if (depth > 0)
{
throw new Exception("Не закрыты скобки в конце строки");
}
return ArgStack.FirstOrDefault();
}
示例4: ParseChunk
public static IList<ParsedChunk> ParseChunk(string input)
{
#region init
// init
var toReturn = new List<ParsedChunk>();
var openTags = new Stack<BbTag>();
var tags = new Queue<BbTag>();
var processedQueue = new Queue<BbTag>();
var finder = new BbFinder(input);
// find all tags
while (!finder.HasReachedEnd)
{
var next = finder.Next();
if (next != null)
tags.Enqueue(next);
}
// return original input if we've no valid bbcode tags
if (tags.All(x => x.Type == BbCodeType.None))
return new[] {AsChunk(input)};
#endregion
while (tags.Count > 0)
{
// get the next tag to process
var tag = tags.Dequeue();
var addToQueue = true;
#region add as child of last tag
// check if we're in the context of another open tag
if (openTags.Count > 0)
{
var lastOpen = openTags.Peek();
var lastMatching = openTags.FirstOrDefault(x => tag.IsClosing && x.Type == tag.Type);
// check if we're closing any previous tags
if (lastMatching != null)
{
lastMatching.ClosingTag = tag;
// keep going through our opened tag stack until we find the one
// we're closing
do
{
lastOpen = openTags.Pop();
// if we end up with a tag that isn't the one we're closing,
// it must not have been closed correctly, e.g
// [i] [b] [/i]
// we'll treat that '[b]' as text
if (lastOpen != lastMatching) lastOpen.Type = BbCodeType.None;
} while (lastOpen != lastMatching);
#region handle noparse
if (lastMatching.Type == BbCodeType.NoParse)
{
lastMatching.Children = lastMatching.Children ?? new List<BbTag>();
lastMatching.Children.Add(new BbTag
{
Type = BbCodeType.None,
End = tag.Start,
Start = lastMatching.End
});
}
#endregion
}
else
{
if (openTags.All(x => x.Type != BbCodeType.NoParse))
{
// if not, we have to be a child of it
lastOpen.Children = lastOpen.Children ?? new List<BbTag>();
lastOpen.Children.Add(tag);
}
// any matching closing tags would be caught in the if part of this
// branch, this is an invalid tag, treat as text
if (tag.IsClosing) tag.Type = BbCodeType.None;
addToQueue = false;
}
}
#endregion
// we don't need to continue processing closing tags
if (tag.IsClosing) continue;
// tell the system we're in the context of this tag now
// though ignore children of 'text' and 'hr'
if (tag.Type != BbCodeType.None && tag.Type != BbCodeType.HorizontalRule)
//.........这里部分代码省略.........