Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cached/compiled Regex in InternalChatCompletionRequestMessageContentPartImageImageUrl #32

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ namespace OpenAI.Chat;
[CodeGenSuppress("InternalChatCompletionRequestMessageContentPartImageImageUrl", typeof(string))]
internal partial class InternalChatCompletionRequestMessageContentPartImageImageUrl
{
#if NET8_0_OR_GREATER
[GeneratedRegex(@"^data:(?<type>.+?);base64,(?<data>.+)$")]
private static partial Regex ParseDataUriRegex();
#else
private static Regex ParseDataUriRegex() => s_parseDataUriRegex;
private static readonly Regex s_parseDataUriRegex = new(@"^data:(?<type>.+?);base64,(?<data>.+)$", RegexOptions.Compiled);
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
#endif

private readonly Uri _imageUri = default;
private readonly BinaryData _imageBytes = default;
private readonly string _imageBytesMediaType = default;
Expand Down Expand Up @@ -47,7 +55,7 @@ public InternalChatCompletionRequestMessageContentPartImageImageUrl(BinaryData i
/// <param name="serializedAdditionalRawData"> Keeps track of any properties unknown to the library. </param>
internal InternalChatCompletionRequestMessageContentPartImageImageUrl(string url, ImageChatMessageContentPartDetail? detail, IDictionary<string, BinaryData> serializedAdditionalRawData)
{
Match parsedDataUri = Regex.Match(url, @"^data:(?<type>.+?);base64,(?<data>.+)$");
Match parsedDataUri = ParseDataUriRegex().Match(url);

if (parsedDataUri.Success)
{
Expand Down
Loading