Skip to content

Commit 3131cc6

Browse files
toncijukic_cptoncijukic_cp
authored andcommitted
Code clarity and performance improvements.
1 parent 8f70505 commit 3131cc6

19 files changed

Lines changed: 105 additions & 163 deletions

Class Library/ActiveUp.Net.Common/Address.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919

2020
namespace ActiveUp.Net.Mail
2121
{
22-
#region Address Object
22+
#region Address Object
2323

2424
/// <summary>
2525
/// Represent an Internet Email address with the owner's fullname.
2626
/// </summary>
2727
#if !PocketPC
2828
[System.Serializable]
2929
#endif
30-
public class Address
30+
public class Address
3131
{
3232
string _email, _name;
33-
/// <summary>
33+
/// <summary>
3434
/// The default constructor. Set all properties to string.Empty.
3535
/// </summary>
3636
public Address()
@@ -47,9 +47,9 @@ public Address(string email)
4747
{
4848
/*this.Email =
4949
this.Name = string.Empty;*/
50-
Address addr = Parser.ParseAddress(email);
51-
this.Email = addr.Email;
52-
this.Name = addr.Name;
50+
Address addr = Parser.ParseAddress(email);
51+
this.Email = addr.Email;
52+
this.Name = addr.Name;
5353
}
5454

5555
/// <summary>
@@ -88,7 +88,7 @@ public string Name
8888
//#if TRIAL
8989
// return ProductHelper.GetTrialString(this._name, TrialStringType.LongText);
9090
//#else
91-
return _name;
91+
return _name;
9292
//#endif
9393
}
9494
set
@@ -120,16 +120,16 @@ public string Merged
120120
}
121121
}
122122

123-
/// <summary>
124-
/// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
125-
/// </summary>
126-
/// <returns>
127-
/// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
128-
/// </returns>
129-
public override string ToString()
130-
{
131-
return this.Merged;
132-
}
123+
/// <summary>
124+
/// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
125+
/// </summary>
126+
/// <returns>
127+
/// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
128+
/// </returns>
129+
public override string ToString()
130+
{
131+
return this.Merged;
132+
}
133133

134134
///<summary>
135135
/// Gets an HTML formatted link to the address (mailto: form).
@@ -138,7 +138,7 @@ public string Link
138138
{
139139
get
140140
{
141-
string getString = string.Empty;
141+
string getString = string.Empty;
142142

143143
if (this.Name.Length > 0)
144144
{

Class Library/ActiveUp.Net.Common/AddressCollection.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1717

1818
using System.Collections.Generic;
19+
using System.Linq;
1920

2021
namespace ActiveUp.Net.Mail
2122
{
@@ -92,13 +93,9 @@ public string Merged
9293
{
9394
get
9495
{
95-
string _addresses="";
96-
97-
foreach(Address address in this)
98-
{
99-
_addresses += address.Merged+",";
100-
}
101-
return _addresses.TrimEnd(',');
96+
string _addresses= this.Aggregate("", (current, address) => current + (address.Merged + ","));
97+
98+
return _addresses.TrimEnd(',');
10299
}
103100
}
104101
/// <summary>
@@ -109,13 +106,22 @@ public string Links
109106
{
110107
get
111108
{
112-
string _addresses="";
113-
114-
foreach(Address address in this)
115-
{
116-
_addresses += address.Link+";";
117-
}
118-
return _addresses.TrimEnd(';');
109+
110+
//
111+
// Commented-out code is here just for clarification, since
112+
// some argued Aggregate function is not as clear at first as
113+
// foreach.
114+
//
115+
116+
//string _addresses = "";
117+
118+
//foreach (Address address in this)
119+
//{
120+
// _addresses += address.Link + ";";
121+
//}
122+
string _addresses= this.Aggregate("", (current, address) => current + (address.Link + ";"));
123+
124+
return _addresses.TrimEnd(';');
119125
}
120126
}
121127
}

Class Library/ActiveUp.Net.Common/Codec.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
using System.Collections.Specialized;
2121
using System.Collections.Generic;
22+
using System.Linq;
2223
using System.Text.RegularExpressions;
2324

2425
namespace ActiveUp.Net.Mail
@@ -291,7 +292,7 @@ internal static string Capitalize(string input)
291292
{
292293
string output = string.Empty;
293294
string[] parts = input.Split('-');
294-
foreach (string str in parts) output += str[0].ToString().ToUpper() + str.Substring(1) + "-";
295+
output = parts.Aggregate(output, (current, str) => current + (str[0].ToString().ToUpper() + str.Substring(1) + "-"));
295296
return output.TrimEnd('-');
296297
}
297298
/// <summary>

Class Library/ActiveUp.Net.Common/Condition.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace ActiveUp.Net.Mail
2525
#if !PocketPC
2626
[System.Serializable]
2727
#endif
28-
public class Condition
28+
public class Condition
2929
{
3030
private string _regionID, _field, _value, _nulltext;
3131
private OperatorType _operator;
@@ -283,24 +283,15 @@ public string Field
283283
_field = value;
284284
}
285285
}
286-
/// <summary>
287-
/// Determines whether the specified s is numeric.
288-
/// </summary>
289-
/// <param name="s">The s.</param>
290-
/// <returns>
291-
/// <c>true</c> if the specified s is numeric; otherwise, <c>false</c>.
292-
/// </returns>
293-
public bool IsNumeric(string s)
286+
/// <summary>
287+
/// Checks if supplied expression is a numeric value. Supports multiple languages.
288+
/// </summary>
289+
/// <param name="expression">An expression that needs to be checked.</param>
290+
/// <returns>A boolean response that is True if it is a numeric value.</returns>
291+
public bool IsNumeric(object expression)
294292
{
295-
try
296-
{
297-
double.Parse(s);
298-
}
299-
catch
300-
{
301-
return false;
302-
}
303-
return true;
293+
double retNum;
294+
return Double.TryParse(Convert.ToString(expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
304295
}
305296
}
306297
}

Class Library/ActiveUp.Net.Common/ContentDisposition.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
using System;
1919
using System.Collections.Generic;
20+
using System.Linq;
2021
using System.Text;
2122
using ActiveUp.Net.Mail;
2223
using System.Collections.Specialized;
@@ -83,8 +84,7 @@ public override string ToString()
8384
{
8485
string str = string.Empty;
8586
str += "Content-Disposition: " + this.Disposition;
86-
foreach (string key in this.Parameters.AllKeys) str += ";\r\n\t" + key + "=" + this.Parameters[key];
87-
return str;
87+
return this.Parameters.AllKeys.Aggregate(str, (current, key) => current + (";\r\n\t" + key + "=" + this.Parameters[key]));
8888
}
8989

9090
public static bool operator ==(ContentDisposition t1, int t2)

Class Library/ActiveUp.Net.Common/Header.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with SharpMap; if not, write to the Free Software
1616
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1717

18+
using System.Linq;
1819
using ActiveUp.Net.Mail;
1920
using ActiveUp.Net.Mail;
2021
using System.Collections.Generic;
@@ -1039,8 +1040,7 @@ public UsenetXrefList Xref
10391040
}
10401041
set
10411042
{
1042-
string temp = "";
1043-
foreach (string str in value.Groups.AllKeys) temp += " " + str + ":" + value.Groups[str];
1043+
string temp = value.Groups.AllKeys.Aggregate("", (current, str) => current + (" " + str + ":" + value.Groups[str]));
10441044
this.HeaderFields["xref"] = value.Host + temp;
10451045
}
10461046
}

Class Library/ActiveUp.Net.Common/Parser.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ public static string Fold(string input)
357357
System.Text.StringBuilder sb = new System.Text.StringBuilder();
358358
string[] separated = input.Split(' ');
359359
string templine = string.Empty;
360-
for (int i = 0; i < separated.Length; i++)
360+
foreach (string t in separated)
361361
{
362-
if (templine.Length + separated[i].Length < 77) templine += separated[i] + " ";
362+
if (templine.Length + t.Length < 77) templine += t + " ";
363363
else
364364
{
365365
sb.Append(templine + "\r\n ");
@@ -923,8 +923,9 @@ public static AddressCollection ParseAddresses(string input)
923923
if (comma_separated[i].IndexOf("@") == -1 && comma_separated.Length > (i + 1))
924924
comma_separated[i + 1] = comma_separated[i] + comma_separated[i + 1];
925925

926-
for (int i = 0; i < comma_separated.Length; i++) if(comma_separated[i].IndexOf("@")!=-1)
927-
addresses.Add(Parser.ParseAddress((comma_separated[i].IndexOf("<") != -1 && comma_separated[i].IndexOf(":") != -1 && comma_separated[i].IndexOf(":") < comma_separated[i].IndexOf("<")) ? ((comma_separated[i].Split(':')[0].IndexOf("\"") == -1) ? comma_separated[i].Split(':')[1] : comma_separated[i]) : comma_separated[i]));
926+
foreach (string t in comma_separated)
927+
if(t.IndexOf("@")!=-1)
928+
addresses.Add(Parser.ParseAddress((t.IndexOf("<") != -1 && t.IndexOf(":") != -1 && t.IndexOf(":") < t.IndexOf("<")) ? ((t.Split(':')[0].IndexOf("\"") == -1) ? t.Split(':')[1] : t) : t));
928929

929930
//MatchCollection matches = Regex.Matches(input, "(\"(?<name>.+?)\")*\\s*<?(?<email>[^<>,\"\\s]+)>?");
930931

Class Library/ActiveUp.Net.Common/Validator.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//using System.Management;
1919
using System;
2020
using System.Collections;
21+
using System.Linq;
2122
using Microsoft.Win32;
2223
using ActiveUp.Net.Mail;
2324
#if !PocketPC
@@ -389,17 +390,8 @@ public static ArrayList GetListNameServers()
389390

390391
private static bool IsPresent(ArrayList list, string valueToTest)
391392
{
392-
foreach(string valueList in list)
393-
{
394-
if (valueToTest == valueList)
395-
{
396-
return true;
397-
}
398-
}
399-
400-
return false;
393+
return list.Cast<string>().Any(valueList => valueToTest == valueList);
401394
}
402-
403395
}
404396
}
405397

0 commit comments

Comments
 (0)