Skip to content

Commit

Permalink
Merge pull request #5 from boston-engineering/db/Make_cmake_V4_bug
Browse files Browse the repository at this point in the history
Fix for bug in CanOpenNodeExporter_V4.Make_cname
  • Loading branch information
trojanobelix committed Sep 15, 2022
2 parents a88fd00 + f701092 commit fdb0a09
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 17 deletions.
35 changes: 35 additions & 0 deletions Tests/ExporterTestsV4.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Xunit;
using libEDSsharp;

namespace Tests
{
public class ExporterTestsV4 : CanOpenNodeExporter_V4
{
[Fact]
public void Test_Make_cname_conversion()
{
if (Make_cname("axle 0 wheel right controlword") != "axle0WheelRightControlword")
throw (new Exception("Make_cname Conversion error"));

if (Make_cname("mapped object 4") != "mappedObject4")
throw (new Exception("Make_cname Conversion error"));

if (Make_cname("COB ID used by RPDO") != "COB_IDUsedByRPDO")
throw (new Exception("Make_cname Conversion error"));

if (Make_cname("A/D unit offset value (filtered)") != "A_DUnitOffsetValueFiltered")
throw (new Exception("Make_cname Conversion error"));

if (Make_cname("80 test string") != "_80TestString")
throw (new Exception("Make_cname Conversion error"));

if (Make_cname("Eighty test string") != "eightyTestString")
throw (new Exception("Make_cname Conversion error"));

if (Make_cname("A") != "a")
throw (new Exception("Make_cname Conversion error"));

}
}
}
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="ExporterTests.cs" />
<Compile Include="PDOHelperTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ExporterTestsV4.cs" />
<Compile Include="XDDImportExportTest.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
45 changes: 28 additions & 17 deletions libEDSsharp/CanOpenNodeExporter_V4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Linq;

namespace libEDSsharp
{
Expand Down Expand Up @@ -664,47 +665,57 @@ Object dictionary
/// </summary>
/// <param name="name">string, name to convert</param>
/// <returns>string</returns>
private static string Make_cname(string name)
protected static string Make_cname(string name)
{
if (name == null || name == "")
return "";

// split string to tokens, separated by non-word characters
string[] tokens = Regex.Split(name.Replace('-', '_'), @"[\W]+");
// split string to tokens, separated by non-word characters. Remove any empty strings
var tokens = Regex.Split(name.Replace('-', '_'), @"[\W]+").Where(s => s != String.Empty);

string output = "";
char prev = ' ';
foreach (string tok in tokens)
{
if (tok.Length == 0)
continue;

char first = tok[0];

if (Char.IsDigit(first) || Char.IsDigit(prev) || (Char.IsUpper(prev) && Char.IsUpper(first)))
if (Char.IsUpper(prev) && Char.IsUpper(first))
{
// add underscore, if tok starts with digit or we have two upper-case words
output += "_" + tok;
// add underscore, if we have two upper-case words
output += "_";
}
else if (output.Length > 0)

if (tok.Length > 1 && Char.IsLetter(first))
{
// all tokens except the first start with uppercse letter
// all tokens except the first start with uppercase letter
output += Char.ToUpper(first) + tok.Substring(1);
}
else if (Char.IsLower(tok[1]))
{
// first token start with lower-case letter, except whole word is uppercase
output += Char.ToLower(first) + tok.Substring(1);
}
else
{
// use token as is
// use token as is and handle what the start of the output looks like outside of the loop
output += tok;
}

prev = tok[tok.Length - 1];
}

if (Char.IsDigit(output[0]))
{
// output that starts with a digit needs a starting underscore
output = "_" + output;
}
else if (output.Length > 1)
{
// output that doesnt start with all-cap-words should have word start with a lower case character
if (Char.IsLetter(output[0]) && Char.IsLower(output[1]))
output = Char.ToLower(output[0]) + output.Substring(1);
}
else
{
// single character output
output = output.ToLower();
}

return output;
}

Expand Down

0 comments on commit fdb0a09

Please sign in to comment.