Skip to content

Commit

Permalink
Introduced new EscapeDecimalMarkInText. It is used by the EdiTextWr…
Browse files Browse the repository at this point in the history
…iter to escape the decimal mark character inside text values #160.
  • Loading branch information
cleftheris committed Jul 6, 2020
1 parent 034e9e8 commit 69f1c91
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 55 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.9.10.{build}
version: 1.9.11.{build}
image: Visual Studio 2019
configuration: Release
platform: Any CPU
Expand Down
18 changes: 0 additions & 18 deletions src/indice.Edi/EdiGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,6 @@ private char[] Separators {
/// <value>An array of possible characters</value>
public char[] Reserved { get; protected set; }

/// <summary>
/// <para>
/// These characters will be escaped by the serializer. This list should only include non default (additional) characters to be escaped.
/// </para>
/// </summary>
/// <value>An array of additioal escape characters.</value>
public string EscapeCharacters { get; protected set; }

/// <summary>
/// Segment terminator indicates the end of a message segment.
/// </summary>
Expand Down Expand Up @@ -208,16 +200,6 @@ public void SetAdvice(char segmentNameDelimiter,
_separators = null;
}

/// <summary>
/// Adds an character to the list of escape chars.
/// </summary>
/// <param name="characterToEscape">The character to escape</param>
public void AddEscapeCharacter(char characterToEscape) {
if (EscapeCharacters == null)
EscapeCharacters = string.Empty;
EscapeCharacters += characterToEscape;
}

/// <summary>
/// Factory for creating an <see cref="IEdiGrammar"/> with the EdiFact defaults.
/// </summary>
Expand Down
14 changes: 6 additions & 8 deletions src/indice.Edi/EdiTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ public EdiTextWriter(TextWriter textWriter, IEdiGrammar grammar)
_charEscapeFlags[Grammar.SegmentNameDelimiter] =
_charEscapeFlags[Grammar.SegmentTerminator] =
_charEscapeFlags[Grammar.ReleaseCharacter.Value] = true;
if (Grammar.EscapeCharacters != null)
foreach (var c in Grammar.EscapeCharacters) {
_charEscapeFlags[c] = true;
}
if (EscapeDecimalMarkInText && Grammar.DecimalMark.HasValue)
_charEscapeFlags[Grammar.DecimalMark.Value] = true;
}
}

Expand Down Expand Up @@ -305,7 +303,7 @@ public override void WriteValue(ulong value, Picture? picture = null) {
/// <param name="picture"></param>
public override void WriteValue(float value, Picture? picture) {
InternalWriteValue(EdiToken.Float);
WriteEscapedString(value.ToEdiString(picture, Grammar.DecimalMark));
_writer.Write(value.ToEdiString(picture, Grammar.DecimalMark));
}

/// <summary>
Expand All @@ -318,7 +316,7 @@ public override void WriteValue(float? value, Picture? picture = null) {
WriteNull();
} else {
InternalWriteValue(EdiToken.Float);
WriteEscapedString(value.ToEdiString(picture, Grammar.DecimalMark));
_writer.Write(value.ToEdiString(picture, Grammar.DecimalMark));
}
}

Expand All @@ -329,7 +327,7 @@ public override void WriteValue(float? value, Picture? picture = null) {
/// <param name="picture"></param>
public override void WriteValue(double value, Picture? picture = null) {
InternalWriteValue(EdiToken.Float);
WriteEscapedString(value.ToEdiString(picture, Grammar.DecimalMark));
_writer.Write(value.ToEdiString(picture, Grammar.DecimalMark));
}

/// <summary>
Expand All @@ -342,7 +340,7 @@ public override void WriteValue(double? value, Picture? picture = null) {
WriteNull();
} else {
InternalWriteValue(EdiToken.Float);
WriteEscapedString(value.ToEdiString(picture, Grammar.DecimalMark));
_writer.Write(value.ToEdiString(picture, Grammar.DecimalMark));
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/indice.Edi/EdiWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ public CultureInfo Culture {
/// </summary>
public bool EnableCompression { get; set; }

/// <summary>
/// Escape decimal mark when encountered in text.
/// </summary>
public bool EscapeDecimalMarkInText { get; set; }

internal bool LastWriteNull { get; set; }
internal int UnwrittenComponents { get; set; }
internal int UnwrittenElements { get; set; }
Expand All @@ -205,7 +210,7 @@ protected EdiWriter(IEdiGrammar grammar) {
_grammar = grammar;
_currentState = State.Start;
_formatting = Formatting.LinePerSegment;

EscapeDecimalMarkInText = true;
CloseOutput = true;
}

Expand Down
14 changes: 0 additions & 14 deletions src/indice.Edi/IEdiGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ public interface IEdiGrammar
/// <value>An array of possible characters</value>
char[] Reserved { get; }

/// <summary>
/// <para>
/// These characters will be escaped by the serializer. This list should only include non default (additional) characters to be escaped.
/// </para>
/// </summary>
/// <value>An array of additioal escape characters.</value>
string EscapeCharacters { get; }

/// <summary>
/// Segment terminator indicates the end of a message segment.
/// </summary>
Expand Down Expand Up @@ -124,11 +116,5 @@ void SetAdvice(char segmentNameDelimiter,
char? releaseCharacter,
char? reserved,
char? decimalMark);

/// <summary>
/// Adds an character to the list of escape chars.
/// </summary>
/// <param name="characterToEscape">The character to escape</param>
void AddEscapeCharacter(char characterToEscape);
}
}
21 changes: 11 additions & 10 deletions src/indice.Edi/indice.Edi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Description>Edi.Net Class Library</Description>
<Copyright>Copyright (c) 2015 Indice</Copyright>
<AssemblyTitle>Edi.Net</AssemblyTitle>
<VersionPrefix>1.9.10</VersionPrefix>
<VersionPrefix>1.9.11</VersionPrefix>
<!--<VersionSuffix>beta1</VersionSuffix>-->
<Authors>c.leftheris</Authors>
<TargetFrameworks>net40;net45;netstandard1.0;netstandard1.3;netstandard2.0</TargetFrameworks>
Expand All @@ -15,15 +15,16 @@
<PackageId>indice.Edi</PackageId>
<PackageTags>EDI;EDIFact;Tradacoms;X12;Serializer;Parser</PackageTags>
<PackageReleaseNotes>
- Support deserializing message fragment without interchange #137
- deserialize Functional group headers and trailers into a separate class #138
- Revisited Element List deserialization and serialization #121
- Fixed paths with wildcard fragments (segment and element) now serialize fine.
- Fixed writer serializing boolean values #141.
- Fixed EdiSerializer bug when using Serialize overload that passes a plain `TextWriter` the internal EdiTextWriter was never closed thus not autocompleteing/terminating the current active structure #142.
- Fixed EdiReader when empty segment was found without segment name delimiter #152.
- Introduced new `SuppressBadEscapeSequenceErrors` option on the serializer. it is used to suppress the exception error thrown when a malformed escape sequence is encountered #157.
- Introduced new `EscapeCharacters` and AddEscapeCharacter() options on the edi grammar. It is used by the EdiTextWriter to escape additional characters inside values #160.
- Support deserializing message fragment without interchange #137
- deserialize Functional group headers and trailers into a separate class #138
- Revisited Element List deserialization and serialization #121
- Fixed paths with wildcard fragments (segment and element) now serialize fine.
- Fixed writer serializing boolean values #141.
- Fixed EdiSerializer bug when using Serialize overload that passes a plain `TextWriter` the internal EdiTextWriter was never closed thus not autocompleteing/terminating the current active structure #142.
- Fixed EdiReader when empty segment was found without segment name delimiter #152.
- Introduced new `SuppressBadEscapeSequenceErrors` option on the serializer. it is used to suppress the exception error thrown when a malformed escape sequence is encountered #157.
- Rolledback v1.9.10 `EscapeCharacters` change #160.
- Introduced new `EscapeDecimalMarkInText`. It is used by the EdiTextWriter to escape the decimal mark character inside text values #160.
</PackageReleaseNotes>
<PackageIcon>icon-256.png</PackageIcon>
<PackageProjectUrl>https://github.com/indice-co/EDI.Net</PackageProjectUrl>
Expand Down
6 changes: 3 additions & 3 deletions test/indice.Edi.Tests/EdiTextWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ public void WriterWrites_Boolean_Correctly() {
public void WriterWrites_Precision_Correctly() {
var grammar = EdiGrammar.NewEdiFact();
grammar.SetAdvice('+', '+', ':', '\'', '?', null, ',');
grammar.AddEscapeCharacter(',');
var expected = new StringBuilder().Append($"AAA+10?,42+:234?,46'{Environment.NewLine}");
var expected = new StringBuilder().Append($"AAA+10,42+:234,46:234?,45563'{Environment.NewLine}");
var output = new StringBuilder();
using (var writer = new EdiTextWriter(new StringWriter(output), grammar)) {
using (var writer = new EdiTextWriter(new StringWriter(output), grammar) { EscapeDecimalMarkInText = true }) {
writer.WriteToken(EdiToken.SegmentName, "AAA"); Assert.Equal("AAA", writer.Path);
writer.WriteValue(10.42345, (Picture)"9(1)V9(2)"); Assert.Equal("AAA[0][0]", writer.Path);
writer.WriteToken(EdiToken.ElementStart); Assert.Equal("AAA[1]", writer.Path);
writer.WriteToken(EdiToken.Null); Assert.Equal("AAA[1][0]", writer.Path);
writer.WriteValue(234.45563, (Picture)"9(1)V9(2)"); Assert.Equal("AAA[1][1]", writer.Path);
writer.WriteValue("234,45563"); Assert.Equal("AAA[1][2]", writer.Path);
}
Assert.Equal(expected.ToString(), output.ToString());
}
Expand Down

3 comments on commit 69f1c91

@tnhung0101
Copy link

Choose a reason for hiding this comment

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

can not set EscapeDecimalMarkInText as false. it not working

@cleftheris
Copy link
Contributor Author

Choose a reason for hiding this comment

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

will look into it asap

@cleftheris
Copy link
Contributor Author

@cleftheris cleftheris commented on 69f1c91 Jul 20, 2020

Choose a reason for hiding this comment

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

new version up with fix (checkout #162). Also no need to explicitly set to false since it is the default.

Please sign in to comment.