Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lex UTF-8 text as a byte stream #38

Merged
merged 1 commit into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 22 additions & 6 deletions CSVLintNppPlugin/PluginInfrastructure/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Kbg.NppPluginNET;
using Kbg.NppPluginNET.PluginInfrastructure;

Expand Down Expand Up @@ -466,8 +467,9 @@ public static void Lex(IntPtr instance, UIntPtr start_pos, IntPtr length_doc, in
// convert the buffer into a managed string
string content = Marshal.PtrToStringAnsi(buffer_ptr, length);

// TODO: fix this; this is just a quick & dirty way to prevent index overflow when Windows = code page 65001
length = content.Length;
// iterate string as a byte array; prevents index overflow when Windows = code page 65001
byte[] contentBytes = ByteStream.GetBytes(content);
length = contentBytes.Length;

// column color index
int idx = 1;
Expand Down Expand Up @@ -523,7 +525,7 @@ public static void Lex(IntPtr instance, UIntPtr start_pos, IntPtr length_doc, in
while (i < length)
{
// next character
char cur = content[i];
byte cur = contentBytes[i];
i++;

// new line
Expand Down Expand Up @@ -578,8 +580,8 @@ public static void Lex(IntPtr instance, UIntPtr start_pos, IntPtr length_doc, in

for (i = 0; i < length - 1; i++)
{
char cur = content[i];
char next = content[i + 1];
byte cur = contentBytes[i];
byte next = contentBytes[i + 1];

if (!quote)
{
Expand Down Expand Up @@ -640,7 +642,7 @@ public static void Lex(IntPtr instance, UIntPtr start_pos, IntPtr length_doc, in
vtable.StartStyling(p_access, (IntPtr)(start + start_col));
vtable.SetStyleFor(p_access, (IntPtr)(length - start_col), (char)idx);
// exception when csv AND separator character not colored AND file ends with separator so the very last value is empty
if ( (separatorChar != '\0') && (!sepcol) && (content[length-1] == separatorChar) )
if ( (separatorChar != '\0') && (!sepcol) && (contentBytes[length-1] == separatorChar) )
{
// style empty value between columns
vtable.StartStyling(p_access, (IntPtr)(start + i));
Expand Down Expand Up @@ -943,4 +945,18 @@ public static IntPtr PropertyGet(IntPtr instance, IntPtr key)
return Marshal.StringToHGlobalAnsi($"{value}\0");
}
}

internal static class ByteStream
{
/// <summary>
/// Converts a CLR String to a byte array.
/// Properly handles the UTF-8 code page, available since Windows version 1903.
/// </summary>
public static byte[] GetBytes(string clrString)
{
var byteBuf = new char[clrString.Length];
clrString.CopyTo(0, byteBuf, 0, clrString.Length);
return (Win32.GetACP() == 65001U) ? Encoding.UTF8.GetBytes(byteBuf) : Encoding.Default.GetBytes(byteBuf);
}
}
}
3 changes: 3 additions & 0 deletions CSVLintNppPlugin/PluginInfrastructure/Win32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ public static IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, ref LangType

public const int MAX_PATH = 260;

[DllImport("kernel32")]
public static extern uint GetACP();

[DllImport("kernel32")]
public static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);

Expand Down