Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions FileHelpers.Tests/Tests/Helpers/StringHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public void RemoveBlanksAfterSign()
public void RemoveTrailingBlanks()
{
StringHelper.RemoveBlanks(" + 41").AssertEqualTo("+41");
}

[Test (Description = "String IsNullOrWhiteSpace help method tests")]
public void IsNullOrWhiteSpace ()
{
StringHelper.IsNullOrWhiteSpace (" ").AssertEqualTo (true, "WhiteSpaces not detected");
StringHelper.IsNullOrWhiteSpace (null).AssertEqualTo (true, "null string not detected");
StringHelper.IsNullOrWhiteSpace (String.Empty).AssertEqualTo (true, "empty string not detected");
StringHelper.IsNullOrWhiteSpace (" test ").AssertEqualTo (false, "valid string not detected");
}
}
}
8 changes: 4 additions & 4 deletions FileHelpers/Converters/ConvertHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ protected override object ParseString(string from)
{
double res;
var blanksRemoved = StringHelper.RemoveBlanks(from);
if (blanksRemoved.EndsWith("%"))
if (blanksRemoved.EndsWith ("%", StringComparison.Ordinal))
{
if (!Double.TryParse(blanksRemoved, NumberStyles.Number | NumberStyles.AllowExponent, mCulture, out res))
throw new ConvertException(from, mType);
Expand Down Expand Up @@ -862,8 +862,8 @@ public BooleanConverter(string trueStr, string falseStr)
{
mTrueString = trueStr;
mFalseString = falseStr;
mTrueStringLower = trueStr.ToLower();
mFalseStringLower = falseStr.ToLower();
mTrueStringLower = trueStr.ToLowerInvariant ();
mFalseStringLower = falseStr.ToLowerInvariant ();
}

/// <summary>
Expand All @@ -874,7 +874,7 @@ public BooleanConverter(string trueStr, string falseStr)
public override object StringToField(string from)
{
object val;
string testTo = from.ToLower();
string testTo = from.ToLowerInvariant ();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember to get some problems with Invariant, maybe that don't convert Á É etc to lower, maybe we must use EqualsIgnoreCase instead of use ToLower


if (mTrueString == null)
{
Expand Down
8 changes: 4 additions & 4 deletions FileHelpers/Core/RecordInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ private static void CheckForOptionalAndArrayProblems(List<FieldBase> resFields)
/// <param name="resFields">List of fields to use</param>
private static void SortFieldsByOrder(List<FieldBase> resFields)
{
if (resFields.FindAll(x => x.FieldOrder.HasValue).Count > 0)
if (resFields.Exists(x => x.FieldOrder.HasValue))
resFields.Sort( (x,y) => x.FieldOrder.Value.CompareTo(y.FieldOrder.Value));
}

Expand Down Expand Up @@ -378,8 +378,8 @@ private static void CheckForOrderProblems(FieldBase currentField, List<FieldBase
}
else
{
var othersWithOrder = resFields.FindAll(x => x.FieldOrder.HasValue).Count;
if (othersWithOrder > 0)
var othersWithOrder = resFields.Exists (x => x.FieldOrder.HasValue);
if (othersWithOrder)
throw new BadUsageException(Messages.Errors.PartialFieldOrder
.FieldName(currentField.FieldInfo.Name)
.Text);
Expand All @@ -401,7 +401,7 @@ public int GetFieldIndex(string fieldName)
{
if (mMapFieldIndex == null)
{
mMapFieldIndex = new Dictionary<string, int>(FieldCount);
mMapFieldIndex = new Dictionary<string, int>(FieldCount, StringComparer.Ordinal);
for (int i = 0; i < FieldCount; i++)
{
mMapFieldIndex.Add(Fields[i].FieldInfo.Name, i);
Expand Down
39 changes: 27 additions & 12 deletions FileHelpers/Core/RecordOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ namespace FileHelpers
/// <summary>
/// Collection of operations that we perform on a type, cached for reuse
/// </summary>
internal sealed class RecordOperations
internal sealed class RecordOperations
//: IRecordOperations
{
// internal buffer for RecordToString and RecordValuesToString operations
// this buffer will be released when this instance goes out of scope
StringBuilder mBuffer = null;

/// <summary>
/// Record Info we use to parse the record and generate an object instance
/// </summary>
Expand All @@ -27,7 +31,7 @@ public RecordOperations(IRecordInfo recordInfo)
RecordInfo = recordInfo;
}

#region " StringToRecord "
#region " StringToRecord "

/// <summary>
/// Process a line and turn it into an object
Expand Down Expand Up @@ -128,13 +132,13 @@ public bool StringToRecord(object record, LineInfo line, object[] values)
private bool MustIgnoreLine(string line)
{
if (RecordInfo.IgnoreEmptyLines)
if ((RecordInfo.IgnoreEmptySpaces && line.TrimStart().Length == 0) ||
if ((RecordInfo.IgnoreEmptySpaces && StringHelper.IsNullOrWhiteSpace (line)) ||
line.Length == 0)
return true;

if (!String.IsNullOrEmpty(RecordInfo.CommentMarker))
if ((RecordInfo.CommentAnyPlace && line.TrimStart().StartsWith(RecordInfo.CommentMarker)) ||
line.StartsWith(RecordInfo.CommentMarker))
if ((RecordInfo.CommentAnyPlace && line.TrimStart ().StartsWith (RecordInfo.CommentMarker, StringComparison.Ordinal)) ||
line.StartsWith (RecordInfo.CommentMarker, StringComparison.Ordinal))
return true;

if (RecordInfo.RecordCondition != RecordCondition.None)
Expand Down Expand Up @@ -182,16 +186,21 @@ private bool MustIgnoreLine(string line)
/// <returns>String representing the object</returns>
public string RecordToString(object record)
{
var sb = new StringBuilder(RecordInfo.SizeHint);
// create or clear internal string buffer
if (mBuffer == null)
mBuffer = new StringBuilder (RecordInfo.SizeHint);
else
mBuffer.Length = 0;

// parse each field
var values = ObjectToValuesHandler(record);

var fields = RecordInfo.Fields;
for (int f = 0; f < RecordInfo.FieldCount; f++)
{
RecordInfo.Fields[f].AssignToString(sb, values[f]);
fields[f].AssignToString (mBuffer, values[f]);
}

return sb.ToString();
return mBuffer.ToString ();
}

/// <summary>
Expand All @@ -201,14 +210,20 @@ public string RecordToString(object record)
/// <returns>String representing values</returns>
public string RecordValuesToString(object[] recordValues)
{
var sb = new StringBuilder(RecordInfo.SizeHint);
// create or clear internal string buffer
if (mBuffer == null)
mBuffer = new StringBuilder (RecordInfo.SizeHint);
else
mBuffer.Length = 0;

// parse each field
var fields = RecordInfo.Fields;
for (int f = 0; f < RecordInfo.FieldCount; f++)
{
RecordInfo.Fields[f].AssignToString(sb, recordValues[f]);
fields[f].AssignToString (mBuffer, recordValues[f]);
}

return sb.ToString();
return mBuffer.ToString ();
}
#endregion

Expand Down
16 changes: 8 additions & 8 deletions FileHelpers/Dynamic/ClassBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ public static Type ClassFromString(string classStr, string className, NetLanguag

case NetLanguage.VbNet:

if (CultureInfo.CurrentCulture.CompareInfo.IndexOf(classStr, "Imports System", CompareOptions.IgnoreCase) == -1)
if (CultureInfo.CurrentCulture.CompareInfo.IndexOf(classStr, "Imports System", CompareOptions.OrdinalIgnoreCase) == -1)
code.Append("Imports System\n");

if (CultureInfo.CurrentCulture.CompareInfo.IndexOf(classStr, "Imports FileHelpers", CompareOptions.IgnoreCase) == -1)
if (CultureInfo.CurrentCulture.CompareInfo.IndexOf(classStr, "Imports FileHelpers", CompareOptions.OrdinalIgnoreCase) == -1)
code.Append("Imports FileHelpers\n");

if (mustAddSystemData && CultureInfo.CurrentCulture.CompareInfo.IndexOf(classStr, "Imports System.Data", CompareOptions.IgnoreCase) == -1)
if (mustAddSystemData && CultureInfo.CurrentCulture.CompareInfo.IndexOf(classStr, "Imports System.Data", CompareOptions.OrdinalIgnoreCase) == -1)
code.Append("Imports System.Data\n");

break;
Expand All @@ -123,7 +123,7 @@ public static Type ClassFromString(string classStr, string className, NetLanguag
code.Append(classStr);

CodeDomProvider prov = null;

switch (lang)
{
case NetLanguage.CSharp:
Expand Down Expand Up @@ -157,7 +157,7 @@ public static Type ClassFromString(string classStr, string className, NetLanguag
if (ts.Length > 0)
foreach (var t in ts)
{
if (t.FullName.StartsWith("My.My") == false && t.IsDefined(typeof(TypedRecordAttribute), false))
if (t.FullName.StartsWith ("My.My", StringComparison.Ordinal) == false && t.IsDefined (typeof (TypedRecordAttribute), false))
return t;
}

Expand Down Expand Up @@ -620,7 +620,7 @@ private void AddAttributesInternal(AttributesBuilder attbs)
attbs.AddAttribute("ConditionalRecord(RecordCondition." + mRecordConditionInfo.Condition.ToString() + ", \"" + mRecordConditionInfo.Selector + "\")");

if (!string.IsNullOrEmpty(mIgnoreCommentInfo.CommentMarker))
attbs.AddAttribute("IgnoreCommentedLines(\"" + mIgnoreCommentInfo.CommentMarker + "\", " + mIgnoreCommentInfo.InAnyPlace.ToString().ToLower() + ")");
attbs.AddAttribute("IgnoreCommentedLines(\"" + mIgnoreCommentInfo.CommentMarker + "\", " + mIgnoreCommentInfo.InAnyPlace.ToString().ToLowerInvariant() + ")");

}

Expand Down Expand Up @@ -897,7 +897,7 @@ public static ClassBuilder LoadFromXml(XmlDocument document)
if (node != null) res.IgnoreCommentedLines.CommentMarker = node.InnerText;

node = document.DocumentElement["CommentInAnyPlace"];
if (node != null) res.IgnoreCommentedLines.InAnyPlace = bool.Parse(node.InnerText.ToLower());
if (node != null) res.IgnoreCommentedLines.InAnyPlace = bool.Parse(node.InnerText.ToLowerInvariant());

node = document.DocumentElement["SealedClass"];
res.SealedClass = node != null;
Expand Down Expand Up @@ -1021,7 +1021,7 @@ public void SaveToXml(TextWriter writer)
xml.WriteElement("IgnoreLastLines", this.IgnoreLastLines.ToString(), "0");

xml.WriteElement("CommentMarker", this.IgnoreCommentedLines.CommentMarker, string.Empty);
xml.WriteElement("CommentInAnyPlace", this.IgnoreCommentedLines.InAnyPlace.ToString().ToLower(), true.ToString().ToLower());
xml.WriteElement("CommentInAnyPlace", this.IgnoreCommentedLines.InAnyPlace.ToString().ToLowerInvariant(), true.ToString().ToLowerInvariant());

xml.WriteElement("RecordCondition", this.RecordCondition.Condition.ToString(), "None");
xml.WriteElement("RecordConditionSelector", this.RecordCondition.Selector, string.Empty);
Expand Down
44 changes: 32 additions & 12 deletions FileHelpers/Engines/FileHelperAsyncEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ protected FileHelperAsyncEngine(Type recordType, Encoding encoding)
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
TextWriter mAsyncWriter;

public bool mEof = false;
/// <summary>
/// Indicates if End of File was reached.
/// </summary>
/// <value>The EOF flag.</value>
public bool Eof
{
get { return mEof; }
}

#endregion

#region " LastRecord "
Expand Down Expand Up @@ -357,6 +367,8 @@ private void ReadNextRecord()
}
else
{
// mark end of file
mEof = true;
mLastRecordValues = null;

mLastRecord = default(T);
Expand Down Expand Up @@ -400,9 +412,15 @@ public T[] ReadNexts(int numberOfRecords)
{
ReadNextRecord();
if (mLastRecord != null)
arr.Add(mLastRecord);
{
arr.Add(mLastRecord);
}
else
{
// mark end of file
mEof = true;
break;
}
}
return arr.ToArray();
}
Expand Down Expand Up @@ -447,17 +465,19 @@ public void Close()

try
{
var writer = mAsyncWriter;
if (writer != null)
using (var writer = mAsyncWriter)
{
if (!string.IsNullOrEmpty(mFooterText))
if (mFooterText.EndsWith(StringHelper.NewLine))
writer.Write(mFooterText);
else
writer.WriteLine(mFooterText);

writer.Close();
mAsyncWriter = null;
mAsyncWriter = null;
if (writer != null)
{
if (!string.IsNullOrEmpty (mFooterText))
if (mFooterText.EndsWith (StringHelper.NewLine, StringComparison.Ordinal))
writer.Write (mFooterText);
else
writer.WriteLine (mFooterText);

writer.Close ();
}
}
}
catch
Expand Down Expand Up @@ -499,7 +519,7 @@ public IDisposable BeginWriteStream(TextWriter writer)
private void WriteHeader()
{
if (!string.IsNullOrEmpty(mHeaderText))
if (mHeaderText.EndsWith(StringHelper.NewLine))
if (mHeaderText.EndsWith (StringHelper.NewLine, StringComparison.Ordinal))
mAsyncWriter.Write(mHeaderText);
else
mAsyncWriter.WriteLine(mHeaderText);
Expand Down
4 changes: 2 additions & 2 deletions FileHelpers/Engines/FileHelperEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ public void WriteStream(TextWriter writer, IEnumerable<T> records, int maxRecord
ResetFields();

if (!string.IsNullOrEmpty(mHeaderText))
if (mHeaderText.EndsWith(StringHelper.NewLine))
if (mHeaderText.EndsWith (StringHelper.NewLine, StringComparison.Ordinal))
writer.Write(mHeaderText);
else
writer.WriteLine(mHeaderText);
Expand Down Expand Up @@ -563,7 +563,7 @@ public void WriteStream(TextWriter writer, IEnumerable<T> records, int maxRecord
mTotalRecords = recIndex;

if (!string.IsNullOrEmpty(mFooterText))
if (mFooterText.EndsWith(StringHelper.NewLine))
if (mFooterText.EndsWith (StringHelper.NewLine, StringComparison.Ordinal))
writer.Write(mFooterText);
else
writer.WriteLine(mFooterText);
Expand Down
8 changes: 4 additions & 4 deletions FileHelpers/Engines/MultiRecordEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public void WriteStream(TextWriter writer, IEnumerable records, int maxRecords)
ResetFields();

if (!string.IsNullOrEmpty(mHeaderText))
if (mHeaderText.EndsWith(StringHelper.NewLine))
if (mHeaderText.EndsWith (StringHelper.NewLine, StringComparison.Ordinal))
writer.Write(mHeaderText);
else
writer.WriteLine(mHeaderText);
Expand Down Expand Up @@ -504,7 +504,7 @@ public void WriteStream(TextWriter writer, IEnumerable records, int maxRecords)
mTotalRecords = recIndex;

if (!string.IsNullOrEmpty(mFooterText))
if (mFooterText.EndsWith(StringHelper.NewLine))
if (mFooterText.EndsWith (StringHelper.NewLine, StringComparison.Ordinal))
writer.Write(mFooterText);
else
writer.WriteLine(mFooterText);
Expand Down Expand Up @@ -701,7 +701,7 @@ public void Close()
if (mAsyncWriter != null)
{
if (!string.IsNullOrEmpty(mFooterText))
if (mFooterText.EndsWith(StringHelper.NewLine))
if (mFooterText.EndsWith (StringHelper.NewLine, StringComparison.Ordinal))
mAsyncWriter.Write(mFooterText);
else
mAsyncWriter.WriteLine(mFooterText);
Expand Down Expand Up @@ -1036,7 +1036,7 @@ public void BeginWriteStream(TextWriter writer)
private void WriteHeader()
{
if (!string.IsNullOrEmpty(mHeaderText))
if (mHeaderText.EndsWith(StringHelper.NewLine))
if (mHeaderText.EndsWith (StringHelper.NewLine, StringComparison.Ordinal))
mAsyncWriter.Write(mHeaderText);
else
mAsyncWriter.WriteLine(mHeaderText);
Expand Down
2 changes: 1 addition & 1 deletion FileHelpers/ErrorHandling/ErrorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void SaveErrors(string fileName, string header)
{
var engine = new FileHelperEngine(typeof (ErrorInfo));

if (header.IndexOf(StringHelper.NewLine) == header.LastIndexOf(StringHelper.NewLine))
if (header.IndexOf (StringHelper.NewLine, StringComparison.Ordinal) == header.LastIndexOf (StringHelper.NewLine, StringComparison.Ordinal))
header += StringHelper.NewLine;

engine.HeaderText = header;
Expand Down
Loading