Skip to content

Commit

Permalink
Use char instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Dec 27, 2024
1 parent 7a465a2 commit b482316
Show file tree
Hide file tree
Showing 43 changed files with 4,933 additions and 2,627 deletions.
79 changes: 79 additions & 0 deletions src/iTextSharp.LGPLv2.Core/System/util/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#if NET40 || NETSTANDARD2_0
namespace System;

/// <summary>
/// Missing NET4_6_2 exts
/// </summary>
internal static class StringExtensions
{
/// <summary>
/// Returns a new string in which all occurrences of a specified string in the current instance are replaced with
/// another specified string, using the provided comparison type.
/// </summary>
public static bool Contains(this string commandText, string value, StringComparison comparisonType)
=> !string.IsNullOrWhiteSpace(commandText) && commandText.IndexOf(value, comparisonType) >= 0;

/// <summary>
/// Returns a new string in which all occurrences of a specified string in the current instance are replaced with
/// another specified string, using the provided comparison type.
/// </summary>
public static string Replace(this string str, string oldValue, string newValue, StringComparison comparisonType)
{
newValue ??= string.Empty;

if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(oldValue) || oldValue.Equals(newValue, comparisonType))
{
return str;
}

int foundAt;

while ((foundAt = str.IndexOf(oldValue, startIndex: 0, comparisonType)) != -1)
{
str = str.Remove(foundAt, oldValue.Length).Insert(foundAt, newValue);
}

return str;
}

/// <summary>
/// Returns the hash code for this string using the specified rules.
/// </summary>
public static int GetHashCode(this string str, StringComparison comparisonType)
=> comparisonType switch
{
StringComparison.CurrentCulture => StringComparer.CurrentCulture.GetHashCode(str),
StringComparison.CurrentCultureIgnoreCase => StringComparer.CurrentCultureIgnoreCase.GetHashCode(str),
StringComparison.InvariantCulture => StringComparer.InvariantCulture.GetHashCode(str),
StringComparison.InvariantCultureIgnoreCase => StringComparer.InvariantCultureIgnoreCase.GetHashCode(str),
StringComparison.Ordinal => StringComparer.Ordinal.GetHashCode(str),
StringComparison.OrdinalIgnoreCase => StringComparer.OrdinalIgnoreCase.GetHashCode(str),
_ => throw new NotSupportedException()
};

/// <summary>
/// Reports the zero-based index of the first occurrence of the specified string in the current String object. A
/// parameter specifies the type of search to use for the specified string.
/// </summary>
public static int IndexOf(this string text, char value, StringComparison comparisonType)
=> text.IndexOf(value.ToString(CultureInfo.InvariantCulture), comparisonType);

/// <summary>
/// Determines whether the end of this string instance matches the specified string when compared using the specified
/// comparison option.
/// </summary>
public static bool EndsWith(this string text,
char value,
StringComparison comparisonType = StringComparison.Ordinal)
=> text.EndsWith(value.ToString(CultureInfo.InvariantCulture), comparisonType);

/// <summary>
/// Determines whether the beginning of this string instance matches the specified string when compared using the
/// specified comparison option.
/// </summary>
public static bool StartsWith(this string text,
char value,
StringComparison comparisonType = StringComparison.Ordinal)
=> text.StartsWith(value.ToString(CultureInfo.InvariantCulture), comparisonType);
}
#endif
13 changes: 8 additions & 5 deletions src/iTextSharp.LGPLv2.Core/iTextSharp/text/Anchor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Anchor : Phrase
/// <overloads>
/// Has nine overloads.
/// </overloads>
public Anchor() : base(16)
public Anchor() : base(leading: 16)
{
}

Expand Down Expand Up @@ -104,6 +104,7 @@ public Anchor(float leading, string str, Font font) : base(leading, str, font)
public Anchor(Phrase phrase) : base(phrase)
{
var anchor = phrase as Anchor;

if (anchor != null)
{
var a = anchor;
Expand All @@ -124,8 +125,9 @@ public override IList<Chunk> Chunks
get
{
var tmp = new List<Chunk>();
var localDestination = reference != null && reference.StartsWith("#", StringComparison.Ordinal);
var localDestination = reference != null && reference.StartsWith(value: '#');
var notGotoOk = true;

foreach (Chunk chunk in this)
{
if (name != null && notGotoOk && !chunk.IsEmpty())
Expand All @@ -136,7 +138,7 @@ public override IList<Chunk> Chunks

if (localDestination)
{
chunk.SetLocalGoto(reference.Substring(1));
chunk.SetLocalGoto(reference.Substring(startIndex: 1));
}
else if (reference != null)
{
Expand Down Expand Up @@ -215,8 +217,9 @@ public override bool Process(IElementListener listener)

try
{
var localDestination = reference != null && reference.StartsWith("#", StringComparison.Ordinal);
var localDestination = reference != null && reference.StartsWith(value: '#');
var notGotoOk = true;

foreach (var chunk in Chunks)
{
if (name != null && notGotoOk && !chunk.IsEmpty())
Expand All @@ -227,7 +230,7 @@ public override bool Process(IElementListener listener)

if (localDestination)
{
chunk.SetLocalGoto(reference.Substring(1));
chunk.SetLocalGoto(reference.Substring(startIndex: 1));
}
else if (reference != null)
{
Expand Down
Loading

0 comments on commit b482316

Please sign in to comment.