Skip to content

Commit

Permalink
Apply CA1859: Change type of field from INullValueDictionary to NullV…
Browse files Browse the repository at this point in the history
…alueDictionary for improved performance
  • Loading branch information
VahidN committed Nov 24, 2024
1 parent 558c830 commit 47560bc
Show file tree
Hide file tree
Showing 18 changed files with 1,482 additions and 1,306 deletions.
38 changes: 25 additions & 13 deletions src/iTextSharp.LGPLv2.Core/System/util/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ public virtual string this[string key]
set => _col[key] = value;
}

public virtual void Add(string key, string value)
{
_col[key] = value;
}
public virtual void Add(string key, string value) => _col[key] = value;

public void AddAll(Properties col)
{
Expand All @@ -44,22 +41,21 @@ public void AddAll(Properties col)
}
}

public void Clear()
{
_col.Clear();
}
public void Clear() => _col.Clear();

public bool ContainsKey(string key) => _col.ContainsKey(key);

public IEnumerator<KeyValuePair<string, string>> GetEnumerator() => _col.GetEnumerator();

public void Load(Stream inStream)
{
using var inp = new StreamReader(inStream, EncodingsRegistry.GetEncoding(1252));
using var inp = new StreamReader(inStream, EncodingsRegistry.GetEncoding(codepage: 1252));

while (true)
{
// Get next line
var line = inp.ReadLine();

if (line == null)
{
return;
Expand All @@ -70,6 +66,7 @@ public void Load(Stream inStream)
// Find start of key
var len = line.Length;
int keyStart;

for (keyStart = 0; keyStart < len; keyStart++)
{
if (WhiteSpaceChars.IndexOf(line[keyStart].ToString(), StringComparison.Ordinal) == -1)
Expand All @@ -86,19 +83,23 @@ public void Load(Stream inStream)

// Continue lines that end in slashes if they are not comments
var firstChar = line[keyStart];

if (firstChar != '#' && firstChar != '!')
{
while (continueLine(line))
{
var nextLine = inp.ReadLine();

if (nextLine == null)
{
nextLine = "";
}

var loppedLine = line.Substring(0, len - 1);
var loppedLine = line.Substring(startIndex: 0, len - 1);

// Advance beyond whitespace on new line
int startIndex;

for (startIndex = 0; startIndex < nextLine.Length; startIndex++)
{
if (WhiteSpaceChars.IndexOf(nextLine[startIndex].ToString(), StringComparison.Ordinal) ==
Expand All @@ -108,16 +109,18 @@ public void Load(Stream inStream)
}
}

nextLine = nextLine.Substring(startIndex, nextLine.Length - startIndex);
nextLine = nextLine.Substring(startIndex);
line = loppedLine + nextLine;
len = line.Length;
}

// Find separation between key and value
int separatorIndex;

for (separatorIndex = keyStart; separatorIndex < len; separatorIndex++)
{
var currentChar = line[separatorIndex];

if (currentChar == '\\')
{
separatorIndex++;
Expand All @@ -130,6 +133,7 @@ public void Load(Stream inStream)

// Skip over whitespace after key if any
int valueIndex;

for (valueIndex = separatorIndex; valueIndex < len; valueIndex++)
{
if (WhiteSpaceChars.IndexOf(line[valueIndex].ToString(), StringComparison.Ordinal) == -1)
Expand Down Expand Up @@ -175,13 +179,15 @@ public string Remove(string key)
{
var retval = _col[key];
_col.Remove(key);

return retval;
}

private static bool continueLine(string line)
{
var slashCount = 0;
var index = line.Length - 1;

while (index >= 0 && line[index--] == '\\')
{
slashCount++;
Expand All @@ -203,16 +209,20 @@ private static string loadConvert(string theString)
for (var x = 0; x < len;)
{
aChar = theString[x++];

if (aChar == '\\')
{
aChar = theString[x++];

if (aChar == 'u')
{
// Read the xxxx
var value = 0;

for (var i = 0; i < 4; i++)
{
aChar = theString[x++];

switch (aChar)
{
case '0':
Expand All @@ -226,6 +236,7 @@ private static string loadConvert(string theString)
case '8':
case '9':
value = (value << 4) + aChar - '0';

break;
case 'a':
case 'b':
Expand All @@ -234,6 +245,7 @@ private static string loadConvert(string theString)
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';

break;
case 'A':
case 'B':
Expand All @@ -242,10 +254,10 @@ private static string loadConvert(string theString)
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';

break;
default:
throw new ArgumentException(
"Malformed \\uxxxx encoding.");
throw new ArgumentException(message: "Malformed \\uxxxx encoding.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace iTextSharp.text.html.simpleparser;
/// </summary>
public class IncTable : IElement
{
private readonly INullValueDictionary<string, string> _props = new NullValueDictionary<string, string>();
private readonly NullValueDictionary<string, string> _props = new();
private List<PdfPCell> _cols;

/// <summary>
Expand Down Expand Up @@ -65,23 +65,26 @@ public PdfPTable BuildTable()
{
if (Rows.Count == 0)
{
return new PdfPTable(1);
return new PdfPTable(numColumns: 1);
}

var ncol = 0;

var c0 = Rows[0];
var c0 = Rows[index: 0];

for (var k = 0; k < c0.Count; ++k)
{
ncol += c0[k].Colspan;
}

var table = new PdfPTable(ncol);

var widths = _props["widths"];
var widths = _props[key: "widths"];

if (widths != null)
{
var intWidths = new List<int>();

foreach (var widthElement in widths.Split(','))
{
intWidths.Add(int.Parse(widthElement, CultureInfo.InvariantCulture));
Expand All @@ -90,17 +93,18 @@ public PdfPTable BuildTable()
table.SetWidths(intWidths.ToArray());
}

var width = _props["width"];
var width = _props[key: "width"];

if (width == null)
{
table.WidthPercentage = 100;
}
else
{
if (width.EndsWith("%", StringComparison.OrdinalIgnoreCase))
if (width.EndsWith(value: "%", StringComparison.OrdinalIgnoreCase))
{
table.WidthPercentage =
float.Parse(width.Substring(0, width.Length - 1), NumberFormatInfo.InvariantInfo);
table.WidthPercentage = float.Parse(width.Substring(startIndex: 0, width.Length - 1),
NumberFormatInfo.InvariantInfo);
}
else
{
Expand All @@ -112,6 +116,7 @@ public PdfPTable BuildTable()
for (var row = 0; row < Rows.Count; ++row)
{
var col = Rows[row];

for (var k = 0; k < col.Count; ++k)
{
table.AddCell(col[k]);
Expand Down
Loading

0 comments on commit 47560bc

Please sign in to comment.