-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce a bit of LINQ in M.E.AI (#5663)
- Loading branch information
1 parent
930af05
commit 5982f6a
Showing
5 changed files
with
73 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/AIContentExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
#if !NET | ||
using System.Linq; | ||
#else | ||
using System.Runtime.CompilerServices; | ||
#endif | ||
|
||
namespace Microsoft.Extensions.AI; | ||
|
||
/// <summary>Internal extensions for working with <see cref="AIContent"/>.</summary> | ||
internal static class AIContentExtensions | ||
{ | ||
/// <summary>Finds the first occurrence of a <typeparamref name="T"/> in the list.</summary> | ||
public static T? FindFirst<T>(this IList<AIContent> contents) | ||
where T : AIContent | ||
{ | ||
int count = contents.Count; | ||
for (int i = 0; i < count; i++) | ||
{ | ||
if (contents[i] is T t) | ||
{ | ||
return t; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// <summary>Concatenates the text of all <see cref="TextContent"/> instances in the list.</summary> | ||
public static string ConcatText(this IList<AIContent> contents) | ||
{ | ||
int count = contents.Count; | ||
switch (count) | ||
{ | ||
case 0: | ||
break; | ||
|
||
case 1: | ||
return contents[0] is TextContent tc ? tc.Text : string.Empty; | ||
|
||
default: | ||
#if NET | ||
DefaultInterpolatedStringHandler builder = new(0, 0, null, stackalloc char[512]); | ||
for (int i = 0; i < count; i++) | ||
{ | ||
if (contents[i] is TextContent text) | ||
{ | ||
builder.AppendLiteral(text.Text); | ||
} | ||
} | ||
|
||
return builder.ToStringAndClear(); | ||
#else | ||
return string.Concat(contents.OfType<TextContent>()); | ||
#endif | ||
} | ||
|
||
return string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters