mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-10 21:47:21 +00:00
Moved all extension methods into extension blocks (#1155)
This commit is contained in:
1 parent
acc7d09fa2
commit
c0a75f39ac
138 files changed
+5884
-4954
No files matched your search
@@ -2,37 +2,40 @@
|
||||
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static string RemoveThinkTags(this string input)
|
||||
{
|
||||
const string OPEN_TAG = "<think>";
|
||||
const string CLOSE_TAG = "</think>";
|
||||
if (string.IsNullOrWhiteSpace(input) || !input.StartsWith(OPEN_TAG, StringComparison.Ordinal))
|
||||
return input;
|
||||
|
||||
var endIndex = input.IndexOf(CLOSE_TAG, StringComparison.Ordinal);
|
||||
if (endIndex == -1)
|
||||
return string.Empty;
|
||||
|
||||
return input[(endIndex + CLOSE_TAG.Length)..];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shortens a text to the given number of characters, and marks the cut with three dots.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Never cuts between the two halves of a surrogate pair, which no JSON writer takes.<br/><br/>
|
||||
/// Takes and returns a string rather than a span on purpose: most texts are short enough, and
|
||||
/// those come back as the very same instance. Only a text which is cut costs one new string.
|
||||
/// </remarks>
|
||||
/// <param name="text">The text.</param>
|
||||
/// <param name="maxCharacters">How many characters of the text to keep at most, the dots not counted.</param>
|
||||
/// <returns>The text as it was when it is short enough, otherwise its beginning followed by three dots.</returns>
|
||||
public static string Shorten(this string text, int maxCharacters)
|
||||
extension(string text)
|
||||
{
|
||||
if (text.Length <= maxCharacters)
|
||||
return text;
|
||||
public string RemoveThinkTags()
|
||||
{
|
||||
const string OPEN_TAG = "<think>";
|
||||
const string CLOSE_TAG = "</think>";
|
||||
if (string.IsNullOrWhiteSpace(text) || !text.StartsWith(OPEN_TAG, StringComparison.Ordinal))
|
||||
return text;
|
||||
|
||||
var endIndex = text.IndexOf(CLOSE_TAG, StringComparison.Ordinal);
|
||||
if (endIndex == -1)
|
||||
return string.Empty;
|
||||
|
||||
return text[(endIndex + CLOSE_TAG.Length)..];
|
||||
}
|
||||
|
||||
var end = char.IsHighSurrogate(text[maxCharacters - 1]) ? maxCharacters - 1 : maxCharacters;
|
||||
return string.Concat(text.AsSpan(0, end).TrimEnd(), "...");
|
||||
/// <summary>
|
||||
/// Shortens a text to the given number of characters, and marks the cut with three dots.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Never cuts between the two halves of a surrogate pair, which no JSON writer takes.<br/><br/>
|
||||
/// Takes and returns a string rather than a span on purpose: most texts are short enough, and
|
||||
/// those come back as the very same instance. Only a text which is cut costs one new string.
|
||||
/// </remarks>
|
||||
/// <param name="maxCharacters">How many characters of the text to keep at most, the dots not counted.</param>
|
||||
/// <returns>The text as it was when it is short enough, otherwise its beginning followed by three dots.</returns>
|
||||
public string Shorten(int maxCharacters)
|
||||
{
|
||||
if (text.Length <= maxCharacters)
|
||||
return text;
|
||||
|
||||
var end = char.IsHighSurrogate(text[maxCharacters - 1]) ? maxCharacters - 1 : maxCharacters;
|
||||
return string.Concat(text.AsSpan(0, end).TrimEnd(), "...");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user