Skip to content

Commit

Permalink
Bump version to 2.0.1
Browse files Browse the repository at this point in the history
- Fixed JID equality comparations.

- Removed redudant code.

- Update CHANGELOG
  • Loading branch information
nathan130200 committed Apr 12, 2024
1 parent 801c18b commit 4a3d435
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 35 deletions.
18 changes: 12 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ A manipulation library and utility for XMPP inspired by the classic agsXMPP libr

### 2.0.0

- v2.0 is here! I was finally able to migrate to an improved system using `XElement`.
- 2.0 is here! I was finally able to migrate to an improved system using `XElement`.
- First of all, removed ALL AI-generated docstrings. I think it was one of the most bizarre ideas I've ever had and it didn't work out very well, it generated a lot of redundant things that weren't well explained. Then soon I will calmly document everything.
- Some things **_may_** break! For example, how to create/declare your elements. But I did everything I could to maintain the compatibility layer between old `Element` class and `XElement` class!
- By the way, most of the xmpp elements remained the same, the way to access/define data also remained almost the same.
- `CDATA` and `Comment` nodes are supported now when parsing (Even though it is not recommended to use these types of nodes in the XMPP protocol standards, this depends on each developer and the needs of each project.).
- Added extra helper methods around XElement class.
- Some things **_may_** break! For example, how to create/declare your elements. But I did everything I could to maintain the compatibility layer between old `Element` class and `XElement` class!
- By the way, most of the xmpp elements remained the same, the way to access/define data also remained almost the same.
- `CDATA` and `Comment` nodes are supported now when parsing (Even though it is not recommended to use these types of nodes in the XMPP protocol standards, this depends on each developer and the needs of each project.).
- Added extra helper methods around XElement class.
- All XMPP elements are now around `XElement` class and all `System.Xml.Linq` related.

### 2.0.1

**Don't forget to report bugs on github! Even though I personally use it for my own gaming XMPP server, there may be some bug that pass unnoticed.**
- Minor bug fixes around `XmppSharp.Jid` equality comparer.


### Notes
I ask users to report any bugs that may exist. Even though I carry out internal tests on my XMPP server, there may be some bug or other that goes unnoticed during the tests.
54 changes: 31 additions & 23 deletions XmppSharp/Jid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace XmppSharp;
/// </summary>
[DebuggerDisplay("{ToString(),nq}")]
public sealed record Jid : IEquatable<Jid>

#if NET7_0_OR_GREATER
, IParsable<Jid>
#endif
Expand Down Expand Up @@ -70,15 +71,15 @@ public string? Resource
set => _resource = EnsureByteSize(value);
}

internal static string EnsureByteSize(string? s, [CallerMemberName] string param = null)
static string EnsureByteSize(string? s, [CallerMemberName] string memberName = null)
{
if (string.IsNullOrEmpty(s))
return s;

int len;

if ((len = Encoding.UTF8.GetByteCount(s)) > 1023)
throw new ArgumentOutOfRangeException(param, len, $"{param} part exceeds the maximum bytes allowed.");
throw new ArgumentOutOfRangeException(memberName, len, $"{memberName} part exceeds the maximum bytes allowed.");

return s;
}
Expand Down Expand Up @@ -198,18 +199,19 @@ public static bool TryParse(string input, out Jid result)
/// </summary>
public override string ToString()
{
var sb = StringBuilderPool.Rent();

if (_local != null)
sb.Append(_local).Append('@');
using (StringBuilderPool.Rent(out var sb))
{
if (_local != null)
sb.Append(_local).Append('@');

if (_domain != null)
sb.Append(_domain);
if (_domain != null)
sb.Append(_domain);

if (_resource != null)
sb.Append('/').Append(_resource);
if (_resource != null)
sb.Append('/').Append(_resource);

return StringBuilderPool.Return(sb);
return sb.ToString();
}
}

/// <summary>
Expand All @@ -233,16 +235,28 @@ public override int GetHashCode() => HashCode.Combine(
_domain?.GetHashCode() ?? 0,
_resource?.GetHashCode() ?? 0);

public bool Equals(Jid other)
{
if (ReferenceEquals(other, null))
return false;

if (ReferenceEquals(other, this))
return true;

return IsBare ? IsBareEquals(this, other)
: IsFullEqual(this, other);
}

/// <summary>
/// Determines whether both JIDs are the same "Bare" (i.e. they have no resource and the other components are the same)
/// Determines whether both JIDs are the same "Bare" (i.e. they have no resource part)
/// </summary>
/// <param name="lhs">First JID to compare.</param>
/// <param name="rhs">Second JID to compare.</param>
/// <returns><see langword="true"/> if both JIDs are equals, otherwise <see langword="false" />.</returns>
public static bool IsBareEquals(Jid lhs, Jid rhs)
{
if (lhs is null)
return rhs is null;
if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null))
return ReferenceEquals(lhs, rhs);

if (!lhs.IsBare || !rhs.IsBare)
return false;
Expand All @@ -252,27 +266,21 @@ public static bool IsBareEquals(Jid lhs, Jid rhs)
}

/// <summary>
/// Determines whether both JIDs are the same "Full" (i.e. both have resources and all components are the same)
/// Determines whether both JIDs are the same "Full" (i.e. both have resource part)
/// </summary>
/// <param name="lhs">First JID to compare.</param>
/// <param name="rhs">Second JID to compare.</param>
/// <returns><see langword="true"/> if both JIDs are equals, otherwise <see langword="false" />.</returns>
public static bool IsFullEqual(Jid lhs, Jid rhs)
{
if (lhs is null)
return rhs is null;

if (lhs.IsBare || rhs.IsBare)
return false;
if (lhs is null || rhs is null)
return lhs is null && rhs is null;

return string.Equals(lhs._local, rhs._local, StringComparison.OrdinalIgnoreCase)
&& string.Equals(lhs._domain, rhs._domain, StringComparison.OrdinalIgnoreCase)
&& string.Equals(lhs._resource, rhs.Resource, StringComparison.Ordinal);
}

public bool Equals(Jid other)
=> IsFullEqual(this, other);

/// <summary>
/// Implicit convert string to JID.
/// </summary>
Expand Down
5 changes: 1 addition & 4 deletions XmppSharp/Protocol/DataForms/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ public IEnumerable<Option> Option
this.Elements<Option>().Remove();

foreach (var item in value)
{
if (item != null)
Add(item);
}
Add(item);
}
}
}
11 changes: 10 additions & 1 deletion XmppSharp/StringBuilderPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@ public static string Return(StringBuilder sb)

return result;
}

public static void Return(StringBuilder sb, out string result)
=> result = Return(sb);

static void ReturnInplace(StringBuilder sb)
{
sb.Clear();

if (!s_Pool.Contains(sb))
s_Pool.Add(sb);
}

readonly struct Entry : IDisposable
{
readonly StringBuilder _builder;
Expand All @@ -38,6 +47,6 @@ public Entry(StringBuilder builder)
=> _builder = builder;

public void Dispose()
=> Return(_builder);
=> ReturnInplace(_builder);
}
}
2 changes: 1 addition & 1 deletion XmppSharp/XmppSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>2.0.0</Version>
<Version>2.0.1</Version>
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
<Copyright>nathan130200</Copyright>
Expand Down

0 comments on commit 4a3d435

Please sign in to comment.