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

Update extensions #10

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
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
97 changes: 97 additions & 0 deletions Assets/BetterCommons/Runtime/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;

namespace Better.Commons.Runtime.Extensions
{
public static class DictionaryExtensions
{
public static bool TryGetKey<TKey, TValue>(this Dictionary<TKey, TValue> self, TValue value, out TKey key)
{
if (self == null)
{
throw new ArgumentNullException(nameof(self));
}

foreach (var keyValuePair in self)
{
if (keyValuePair.Value.Equals(value))
{
key = keyValuePair.Key;
return true;
}
}

key = default;
return false;
}

public static bool TryGetKeys<TKey, TValue>(this Dictionary<TKey, TValue> self, TValue value, out TKey[] keys)
{
if (self == null)
{
throw new ArgumentNullException(nameof(self));
}

var rawKeys = new List<TKey>();
foreach (var keyValuePair in self)
{
if (keyValuePair.Value.Equals(value))
{
rawKeys.Add(keyValuePair.Key);
}
}

keys = rawKeys.ToArray();
return !keys.IsEmpty();
}

public static bool Remove<TKey, TValue>(this Dictionary<TKey, TValue> self, IEnumerable<TKey> keys)
{
if (self == null)
{
throw new ArgumentNullException(nameof(self));
}

var removedAny = false;
foreach (var key in keys)
{
if (self.Remove(key))
{
removedAny = true;
}
}

return removedAny;
}

public static bool Remove<TKey, TValue>(this Dictionary<TKey, TValue> self, TValue value, out TKey key)
{
if (self == null)
{
throw new ArgumentNullException(nameof(self));
}

return self.TryGetKey(value, out key) && self.Remove(key);
}

public static bool Remove<TKey, TValue>(this Dictionary<TKey, TValue> self, TValue value)
{
return self.Remove(value, out _);
}

public static bool RemoveAll<TKey, TValue>(this Dictionary<TKey, TValue> self, TValue value, out TKey[] keys)
{
if (self == null)
{
throw new ArgumentNullException(nameof(self));
}

return self.TryGetKeys(value, out keys) && self.Remove(keys);
}

public static bool RemoveAll<TKey, TValue>(this Dictionary<TKey, TValue> self, TValue value)
{
return self.RemoveAll(value, out _);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

246 changes: 246 additions & 0 deletions Assets/BetterCommons/Runtime/Extensions/StringBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
using Better.Commons.Runtime.Utility;

namespace Better.Commons.Runtime.Extensions
{
public static class StringBuilderExtensions
{
private const string InfoFieldSeparator = ": ";

public static StringBuilder AppendLine(this StringBuilder self, object value)
{
if (self == null)
Expand All @@ -22,5 +25,248 @@ public static StringBuilder AppendLine(this StringBuilder self, object value)

return self.AppendLine(value.ToString());
}

public static StringBuilder AppendFormatLine(this StringBuilder self, IFormatProvider provider, string format, object arg0)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

if (provider == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendFormat(provider, format, arg0).AppendLine();
}

public static StringBuilder AppendFormatLine(this StringBuilder self, IFormatProvider provider, string format, object arg0, object arg1)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

if (provider == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendFormat(provider, format, arg0, arg1).AppendLine();
}

public static StringBuilder AppendFormatLine(this StringBuilder self, IFormatProvider provider, string format, object arg0, object arg1, object arg2)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

if (provider == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendFormat(provider, format, arg0, arg1, arg2).AppendLine();
}

public static StringBuilder AppendFormatLine(this StringBuilder self, IFormatProvider provider, string format, params object[] args)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

if (provider == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendFormat(provider, format, args).AppendLine();
}

public static StringBuilder AppendFormatLine(this StringBuilder self, string format, object arg0)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendFormat(format, arg0).AppendLine();
}

public static StringBuilder AppendFormatLine(this StringBuilder self, string format, object arg0, object arg1)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendFormat(format, arg0, arg1).AppendLine();
}

public static StringBuilder AppendFormatLine(this StringBuilder self, string format, object arg0, object arg1, object arg2)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendFormat(format, arg0, arg1, arg2).AppendLine();
}

public static StringBuilder AppendFormatLine(this StringBuilder self, string format, params object[] args)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

if (args == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendFormat(format, args).AppendLine();
}

public static StringBuilder AppendJoinLine(this StringBuilder self, char separator, params object[] values)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

if (values == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendJoin(separator, values).AppendLine();
}

public static StringBuilder AppendJoinLine(this StringBuilder self, char separator, params string[] values)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

if (values == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendJoin(separator, values).AppendLine();
}

public static StringBuilder AppendJoinLine(this StringBuilder self, string separator, params object[] values)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

if (values == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendJoin(separator, values).AppendLine();
}

public static StringBuilder AppendJoinLine(this StringBuilder self, string separator, params string[] values)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

if (values == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendJoin(separator, values).AppendLine();
}

public static StringBuilder AppendJoinLine<T>(this StringBuilder self, char separator, IEnumerable<T> values)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

if (values == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendJoin(separator, values).AppendLine();
}

public static StringBuilder AppendJoinLine<T>(this StringBuilder self, string separator, IEnumerable<T> values)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

if (values == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendJoin(separator, values).AppendLine();
}

public static StringBuilder AppendField(this StringBuilder self, string fieldName, object fieldValue)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

self.AppendJoin(InfoFieldSeparator, fieldName, fieldValue);
return self;
}

public static StringBuilder AppendFieldLine(this StringBuilder self, string fieldName, object fieldValue)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

return self.AppendField(fieldName, fieldValue).AppendLine();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CompletionAwaiter(CancellationToken cancellationToken)

protected void SetResult(TValue value)
{
if (!_completionSource.TrySetResult(value))
if (_completionSource.TrySetResult(value))
{
OnCompleted(value);
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/BetterCommons/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.tdw.better.commons",
"displayName": "Better Commons",
"version": "0.0.6",
"version": "0.0.7",
"unity": "2021.3",
"description": " ",
"dependencies": {
Expand Down
Loading