Skip to content

Commit

Permalink
Count unique values fixed width always empty
Browse files Browse the repository at this point in the history
Bugfix, Count unique values for fixed width results in empty list
  • Loading branch information
BdR76 committed Mar 30, 2024
1 parent ebc64e5 commit 8ab259e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 10 additions & 6 deletions CSVLintNppPlugin/CsvLint/CsvAnalyze.cs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,10 @@ public static void CountUniqueValues(CsvDefinition csvdef, List<int> colidx, boo
// if first line is header column names, then consume line and ignore
if (csvdef.ColNameHeader) csvdef.ParseNextLine(strdata, out iscomm);

// New separator same as old
// Exception for fixed width, because code for fixed width output would be too complicated
var newsep = (csvdef.Separator == '\0' ? ',' : csvdef.Separator);

// read all data lines
while (!strdata.EndOfStream)
{
Expand All @@ -701,14 +705,14 @@ public static void CountUniqueValues(CsvDefinition csvdef, List<int> colidx, boo

// if value contains separator character then put value in quotes
var val = col < values.Count ? values[col] : "";
if (val.IndexOf(csvdef.Separator) >= 0)
if (val.IndexOf(newsep) >= 0)
{
val = val.Replace("\"", "\"\"");
val = string.Format("\"{0}\"", val);
}

// concatenate selected column values as csv string, use same separator as source file
uniq += (i > 0 ? csvdef.Separator.ToString() : "") + val;
uniq += (i > 0 ? newsep.ToString() : "") + val;
}

// count unique value(s)
Expand All @@ -724,7 +728,7 @@ public static void CountUniqueValues(CsvDefinition csvdef, List<int> colidx, boo
StringBuilder sb = new StringBuilder();

// new output unique values and count to new file
CsvDefinition csvnew = new CsvDefinition(csvdef.Separator);
CsvDefinition csvnew = new CsvDefinition(newsep);

// get access to Notepad++
INotepadPPGateway notepad = new NotepadPPGateway();
Expand All @@ -735,10 +739,10 @@ public static void CountUniqueValues(CsvDefinition csvdef, List<int> colidx, boo
{
// if column name contains separator character then put column name in quotes
var colname = colidx[i] < csvdef.Fields.Count ? csvdef.Fields[colidx[i]].Name : "";
if (colname.IndexOf(csvdef.Separator) >= 0) colname = string.Format("\"{0}\"", colname);
if (colname.IndexOf(newsep) >= 0) colname = string.Format("\"{0}\"", colname);

// new header
sb.Append(string.Format("{0}{1}", colname, csvdef.Separator));
sb.Append(string.Format("{0}{1}", colname, newsep));

// new csv defintion
csvnew.AddColumn(i, colname, csvdef.Fields[colidx[i]].MaxWidth, csvdef.Fields[colidx[i]].DataType, csvdef.Fields[colidx[i]].Mask);
Expand All @@ -759,7 +763,7 @@ public static void CountUniqueValues(CsvDefinition csvdef, List<int> colidx, boo
var maxwidth = 0;
foreach (KeyValuePair<string, int> unqcnt in uniquecount)
{
sb.Append(string.Format("{0}{1}{2}\r\n", unqcnt.Key, csvdef.Separator, unqcnt.Value));
sb.Append(string.Format("{0}{1}{2}\r\n", unqcnt.Key, newsep, unqcnt.Value));
if (maxwidth < unqcnt.Value) maxwidth = unqcnt.Value;
}
csvnew.AddColumn("count_unique", maxwidth.ToString().Length, ColumnType.Integer);
Expand Down
4 changes: 4 additions & 0 deletions CSVLintNppPlugin/Forms/DetectColumnsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ namespace CSVLintNppPlugin.Forms
{
public partial class DetectColumnsForm : CSVLintNppPlugin.Forms.CsvEditFormBase
{
private readonly ToolTip helperTip = new ToolTip();
public DetectColumnsForm()
{
InitializeComponent();

// tooltip initialization
helperTip.SetToolTip(btnFixedWidthPos, "Column end positions based on current column widths");
}

public char Separator { get; set; }
Expand Down

0 comments on commit 8ab259e

Please sign in to comment.