Skip to content

Commit

Permalink
Correctly deduces and manages decimal separator
Browse files Browse the repository at this point in the history
  • Loading branch information
Baltasarq committed Aug 27, 2015
1 parent 39e2ded commit a7393d4
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 73 deletions.
20 changes: 15 additions & 5 deletions Colorado.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,21 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
<Reference Include="gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
<Reference Include="glib-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
<Reference Include="pango-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
<Reference Include="atk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
<Reference Include="gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<Package>gtk-sharp-2.0</Package>
</Reference>
<Reference Include="gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<Package>gtk-sharp-2.0</Package>
</Reference>
<Reference Include="glib-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<Package>glib-sharp-2.0</Package>
</Reference>
<Reference Include="pango-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<Package>gtk-sharp-2.0</Package>
</Reference>
<Reference Include="atk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<Package>gtk-sharp-2.0</Package>
</Reference>
<Reference Include="GtkUtil">
<HintPath>..\CsMyLib\GtkUtil\bin\Release\GtkUtil.dll</HintPath>
</Reference>
Expand Down
6 changes: 6 additions & 0 deletions Colorado.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Colorado", "Colorado.csproj", "{12F5964C-2BDE-4379-915B-E55322B5DE75}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ColoradoTests", "ColoradoTests\ColoradoTests.csproj", "{64A2D360-A3A2-4F87-8983-EC4A18E8F053}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|AnyCPU = Debug|AnyCPU
Expand All @@ -13,6 +15,10 @@ Global
{12F5964C-2BDE-4379-915B-E55322B5DE75}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{12F5964C-2BDE-4379-915B-E55322B5DE75}.Release|AnyCPU.ActiveCfg = Release|Any CPU
{12F5964C-2BDE-4379-915B-E55322B5DE75}.Release|AnyCPU.Build.0 = Release|Any CPU
{64A2D360-A3A2-4F87-8983-EC4A18E8F053}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU
{64A2D360-A3A2-4F87-8983-EC4A18E8F053}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{64A2D360-A3A2-4F87-8983-EC4A18E8F053}.Release|AnyCPU.ActiveCfg = Release|Any CPU
{64A2D360-A3A2-4F87-8983-EC4A18E8F053}.Release|AnyCPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
Policies = $0
Expand Down
51 changes: 51 additions & 0 deletions ColoradoTests/ColoradoTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{64A2D360-A3A2-4F87-8983-EC4A18E8F053}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>ColoradoTests</RootNamespace>
<AssemblyName>ColoradoTests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Test.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Colorado.csproj">
<Project>{12F5964C-2BDE-4379-915B-E55322B5DE75}</Project>
<Name>Colorado</Name>
</ProjectReference>
</ItemGroup>
</Project>
105 changes: 105 additions & 0 deletions ColoradoTests/Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using NUnit.Framework;
using System;

using Colorado.Core;

namespace ColoradoTests {
[TestFixture]
public class TestDecimalMark {
[Test]
public void TestDecimalSeparator() {
for (int i = 0; i < DecimalMark.DecimalSeparatorChar.Count; ++i) {
Assert.AreEqual(
DecimalMark.DecimalSeparatorChar[ i ],
DecimalMark.AsChar( (DecimalMark.DecimalSeparator) i )
);
}
}

[Test]
public void TestIsDecimalMark() {
Assert.AreEqual( false, DecimalMark.IsDecimalMark( 'a' ) );
Assert.AreEqual( true, DecimalMark.IsDecimalMark( '.' ) );
Assert.AreEqual( true, DecimalMark.IsDecimalMark( ',' ) );
}

[Test]
public void TestWhichDecimalMark() {
Assert.AreEqual( DecimalMark.DecimalSeparator.Point, DecimalMark.WhichDecimalMark( "4" ) );
Assert.AreEqual( DecimalMark.DecimalSeparator.Point, DecimalMark.WhichDecimalMark( "4.5" ) );
Assert.AreEqual( DecimalMark.DecimalSeparator.Comma, DecimalMark.WhichDecimalMark( "4,5" ) );

Assert.AreEqual( 0, DecimalMark.WhichDecimalMark( '.' ) );
Assert.AreEqual( 1, DecimalMark.WhichDecimalMark( ',' ) );
Assert.AreEqual( -1, DecimalMark.WhichDecimalMark( 'a' ) );
}

[Test]
public void TestIsNumber()
{
string[] testNumbers = {
"", " ", ".0",
"-.1", "-.", "0.4e-5",
"5.4", "0", "+",
"+.0", "0,6e-6", "5,6",
"hello4", "4d", "3",
"0.4e5", "0,1E45", "0,0001",
",1", "1,", "1.",
};

bool[] testResults = {
false, false, true,
true, false, true,
true, true, false,
true, true, true,
false, false, true,
true, true, true,
true, true, true,
};

for(int i = 0; i < testResults.Length; ++i) {
double num;
bool actualResult = double.TryParse( testNumbers[ i ], out num );
bool result = DecimalMark.IsNumber( testNumbers[ i ] );

Console.WriteLine( "[\"{0}\"] -> {1} == {2} == {3} == {4}?",
testNumbers[ i ], testResults[ i ], result, actualResult, num );

Assert.AreEqual( testResults[ i ], result );
Assert.AreEqual( testResults[ i ], actualResult );
}
}

[Test]
public void TestIsRealNumber()
{
string[] testNumbers = {
"", " ", ".0",
"4e+5", "-.", "4",
"5.4", "0", "+",
"+.0", "0,6e-6", "5,6",
"hello4", "4d", "0,",
"0.4e5", "0,1E45", "0,0001",
};

bool[] testResults = {
false, false, true,
false, false, false,
true, false, false,
true, true, true,
false, false, true,
true, true, true,
};

for(int i = 0; i < testResults.Length; ++i) {
bool result = DecimalMark.IsRealNumber( testNumbers[ i ] );

Console.WriteLine( "[\"{0}\"] -> {1} == {2}?",
testNumbers[ i ], testResults[ i ], result );

Assert.AreEqual( testResults[ i ], result );
}
}
}
}

4 changes: 4 additions & 0 deletions ColoradoTests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="2.6.3" targetFramework="net45" />
</packages>
53 changes: 29 additions & 24 deletions Core/CsvDocumentPersistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,7 @@ public static void PrepareFileName(ref string fileName)

return;
}

public static string PrepareValue(string cell)
{
// Prepare
string toret = cell.Trim();

if ( toret.Length > 0 ) {
if ( toret[ 0 ] == '"' ) {
// Remove double quotes
toret = toret.Substring( 1 );

if ( toret[ toret.Length -1 ] == '"' ) {
toret = toret.Substring( 0, toret.Length -1 );
}
} else {
toret = cell;
}
}

return toret;
}


/// <summary>
/// Strips spaces from a text line, trimming it.
/// Note this is needed because the TAB delimiter is part of the spaces
Expand Down Expand Up @@ -214,6 +193,32 @@ protected void DetermineDelimiter(string line)
return;
}

private string FormatLoadedCell(string cell)
{
string toret = cell.Trim();

if ( toret.Length > 0 ) {
// Strip contents
if ( toret[ 0 ] == '"' ) {
// Remove double quotes
toret = toret.Substring( 1 );

if ( toret[ toret.Length -1 ] == '"' ) {
toret = toret.Substring( 0, toret.Length -1 );
}
} else {
toret = cell;
}

// Deduce decimal separator
if ( DecimalMark.IsRealNumber( toret ) ) {
this.Document.DecimalSeparator = DecimalMark.WhichDecimalMark( toret );
}
}

return toret;
}

private string[] SplitLine(string line)
{
var row = new List<string>();
Expand Down Expand Up @@ -242,7 +247,7 @@ private string[] SplitLine(string line)
if ( !inQuoted
&& line[ i ] == Document.DelimiterValue[ 0 ] )
{
row.Add( PrepareValue( line.Substring( pos, i - pos ) ) );
row.Add( FormatLoadedCell( line.Substring( pos, i - pos ) ) );
pos = i + 1;
}
else
Expand All @@ -255,7 +260,7 @@ private string[] SplitLine(string line)

// Add last column
if ( pos < line.Length ) {
row.Add( PrepareValue( line.Substring( pos, line.Length - pos ) ) );
row.Add( FormatLoadedCell( line.Substring( pos, line.Length - pos ) ) );
}
else
if ( line[ line.Length -1 ] == Document.DelimiterValue[ 0 ] ) {
Expand Down
Loading

0 comments on commit a7393d4

Please sign in to comment.