Skip to content

Commit 32e8c69

Browse files
authored
Refactor STUN code for clarity and consistency (#1653) (#1656)
Replaced manual string comparisons with StringComparison.OrdinalIgnoreCase and placed constants first in comparisons. Refactored switch statements to use switch expressions with spans. Updated header concatenation to use string interpolation. Used string.IsNullOrWhiteSpace for null/whitespace checks. Changed method overrides to use override instead of new where appropriate.
1 parent b37e7a3 commit 32e8c69

8 files changed

Lines changed: 11 additions & 11 deletions

src/SIPSorcery/net/STUN/STUNAppState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static string PrintBuffer(byte[] buffer)
4040

4141
if (byteStr.Length == 1)
4242
{
43-
bufferStr += "0" + byteStr;
43+
bufferStr += $"0{byteStr}";
4444
}
4545
else
4646
{

src/SIPSorcery/net/STUN/STUNAttributes/STUNAddressAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public override int ToByteBuffer(byte[] buffer, int startIndex)
9595

9696
public override string ToString()
9797
{
98-
string attrDescrStr = "STUN Attribute: " + base.AttributeType + ", address=" + Address.ToString() + ", port=" + Port + ".";
98+
string attrDescrStr = $"STUN Attribute: {base.AttributeType}, address={Address.ToString()}, port={Port}.";
9999

100100
return attrDescrStr;
101101
}

src/SIPSorcery/net/STUN/STUNAttributes/STUNAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public static List<STUNAttribute> ParseMessageAttributes(byte[] buffer, int star
220220

221221
attributes.Add(attribute);
222222

223-
// Attributes start on 32 bit word boundaries so where an attribute length is not a multiple of 4 it gets padded.
223+
// Attributes start on 32 bit word boundaries so where an attribute length is not a multiple of 4 it gets padded.
224224
int padding = (stunAttributeLength % 4 != 0) ? 4 - (stunAttributeLength % 4) : 0;
225225

226226
startAttIndex = startAttIndex + 4 + stunAttributeLength + padding;
@@ -258,7 +258,7 @@ public virtual int ToByteBuffer(byte[] buffer, int startIndex)
258258

259259
public new virtual string ToString()
260260
{
261-
string attrDescrString = "STUN Attribute: " + AttributeType.ToString() + ", length=" + PaddedLength + ".";
261+
string attrDescrString = $"STUN Attribute: {AttributeType.ToString()}, length={PaddedLength}.";
262262

263263
return attrDescrString;
264264
}

src/SIPSorcery/net/STUN/STUNAttributes/STUNChangeRequestAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public STUNChangeRequestAttribute(byte[] attributeValue)
5353

5454
public override string ToString()
5555
{
56-
string attrDescrStr = "STUN Attribute: " + STUNAttributeTypesEnum.ChangeRequest.ToString() + ", key byte=" + m_changeRequestByte.ToString("X") + ", change address=" + ChangeAddress + ", change port=" + ChangePort + ".";
56+
string attrDescrStr = $"STUN Attribute: {STUNAttributeTypesEnum.ChangeRequest.ToString()}, key byte={m_changeRequestByte.ToString("X")}, change address={ChangeAddress}, change port={ChangePort}.";
5757

5858
return attrDescrStr;
5959
}

src/SIPSorcery/net/STUN/STUNAttributes/STUNConnectionIdAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public STUNConnectionIdAttribute(uint connectionId)
4444

4545
public override string ToString()
4646
{
47-
string attrDescrStr = "STUN CONNECTION_ID Attribute: value=" + ConnectionId + ".";
47+
string attrDescrStr = $"STUN CONNECTION_ID Attribute: value={ConnectionId}.";
4848

4949
return attrDescrStr;
5050
}

src/SIPSorcery/net/STUN/STUNAttributes/STUNErrorCodeAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private static byte[] BuildValue(int errorCode, string reasonPhrase)
6060

6161
public override string ToString()
6262
{
63-
string attrDescrStr = "STUN ERROR_CODE_ADDRESS Attribute: error code=" + ErrorCode + ", reason phrase=" + ReasonPhrase + ".";
63+
string attrDescrStr = $"STUN ERROR_CODE_ADDRESS Attribute: error code={ErrorCode}, reason phrase={ReasonPhrase}.";
6464

6565
return attrDescrStr;
6666
}

src/SIPSorcery/net/STUN/STUNAttributes/STUNXORAddressAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public override int ToByteBuffer(byte[] buffer, int startIndex)
133133

134134
public override string ToString()
135135
{
136-
string attrDescrStr = "STUN XOR_MAPPED_ADDRESS Attribute: " + base.AttributeType + ", address=" + Address.ToString() + ", port=" + Port + ".";
136+
string attrDescrStr = $"STUN XOR_MAPPED_ADDRESS Attribute: {base.AttributeType}, address={Address.ToString()}, port={Port}.";
137137

138138
return attrDescrStr;
139139
}

src/SIPSorcery/net/STUN/STUNMessage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@ public byte[] ToByteBuffer(byte[] messageIntegrityKey, bool addFingerprint)
198198
return buffer;
199199
}
200200

201-
public new string ToString()
201+
public override string ToString()
202202
{
203-
string messageDescr = "STUN Message: " + Header.MessageType.ToString() + ", length=" + Header.MessageLength;
203+
string messageDescr = $"STUN Message: {Header.MessageType.ToString()}, length={Header.MessageLength}";
204204

205205
foreach (STUNAttribute attribute in Attributes)
206206
{
207-
messageDescr += "\n " + attribute.ToString();
207+
messageDescr += $"\n {attribute.ToString()}";
208208
}
209209

210210
return messageDescr;

0 commit comments

Comments
 (0)