<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

using Dependencies.ClrPh;

namespace Dependencies
{
    namespace Test
    {
        public class Demangler :  PhSymbolProvider
        {
            public Demangler(String demangler = "Default")
            {
                _demangler = demangler;
            }

            public override string UndecorateName(string DecoratedName)
            {

                switch (_demangler)
                {
                    case "Demumble":
                        return base.UndecorateNameDemumble(DecoratedName);
                    case "LLVMItanium":
                        return base.UndecorateNameLLVMItanium(DecoratedName);
                    case "LLVMMicrosoft":
                        return base.UndecorateNameLLVMMicrosoft(DecoratedName);
                    case "Microsoft":
                        return base.UndecorateNamePh(DecoratedName);

                    default:
                    case "Default":
                        return base.UndecorateName(DecoratedName);
                }

            }

            private string _demangler;
        }

        class Program
        {
            static bool TestKnownInputs(Demangler SymPrv)
            {
                Debug.Assert(SymPrv.UndecorateName("??1type_info@@UEAA@XZ") == "public: virtual __cdecl type_info::~type_info(void) __ptr64");
                Debug.Assert(SymPrv.UndecorateName("?setbuf@strstreambuf@@UEAAPEAVstreambuf@@PEADH@Z") == "public: virtual class streambuf * __ptr64 __cdecl strstreambuf::setbuf(char * __ptr64,int) __ptr64");
                Debug.Assert(SymPrv.UndecorateName("?CreateXBaby@XProvider@DirectUI@@UEAAJPEAVIXElementCP@2@PEAUHWND__@@PEAVElement@2@PEAKPEAPEAUIXBaby@2@@Z") == "public: virtual long __cdecl DirectUI::XProvider::CreateXBaby(class DirectUI::IXElementCP * __ptr64,struct HWND__ * __ptr64,class DirectUI::Element * __ptr64,unsigned long * __ptr64,struct DirectUI::IXBaby * __ptr64 * __ptr64) __ptr64");
                Debug.Assert(SymPrv.UndecorateName("??0exception@@QEAA@AEBQEBDH@Z") == "public: __cdecl exception::exception(char const * __ptr64 const & __ptr64,int) __ptr64");
                Debug.Assert(SymPrv.UndecorateName("?what@exception@@UEBAPEBDXZ") == "public: virtual char const * __ptr64 __cdecl exception::what(void)const __ptr64");
                Debug.Assert(SymPrv.UndecorateName("?_Execute_once@std@@YAHAEAUonce_flag@1@P6AHPEAX1PEAPEAX@Z1@Z") == "int __cdecl std::_Execute_once(struct std::once_flag & __ptr64,int (__cdecl*)(void * __ptr64,void * __ptr64,void * __ptr64 * __ptr64),void * __ptr64)");
                Debug.Assert(SymPrv.UndecorateName("?swap@?$basic_streambuf@_WU?$char_traits@_W@std@@@std@@IEAAXAEAV12@@Z") == "protected: void __cdecl std::basic_streambuf<wchar_t,struct std::char_traits<wchar_t> >::swap(class std::basic_streambuf<wchar_t,struct std::char_traits<wchar_t> > & __ptr64) __ptr64");

                Console.WriteLine("demangler-test : all known inputs OK");
                return true;
            }

            static bool TestFilepath(string Filepath, Demangler SymPrv)
            {

                PE Pe = new PE(Filepath);
                if (!Pe.Load())
                {
                    Console.Error.WriteLine("[x] Could not load file {0:s} as a PE", Filepath);
                    return false;
                }

                foreach (PeExport Export in Pe.GetExports())
                {
                    if (Export.Name.Length > 0)
                    {
                        Console.Write("\t Export : {0:s} -> ", Export.Name);
                        Console.Out.Flush();
                        Console.WriteLine("{0:s}", SymPrv.UndecorateName(Export.Name));
                    }
                        
                }

                foreach (PeImportDll DllImport in Pe.GetImports())
                {
                    foreach (PeImport Import in DllImport.ImportList)
                    {
                        if (!Import.ImportByOrdinal)
                        {
                            Console.Write("\t Import from {0:s} : {1:s} -> ", DllImport.Name, Import.Name);
                            Console.Out.Flush();
                            Console.WriteLine("{0:s}", SymPrv.UndecorateName(Import.Name));
                        }

                    }
                }

                return true;
            }

            static void Main(string[] args)
            {
                // always the first call to make
                Phlib.InitializePhLib();

                Demangler demangler;

                switch(args.Length)
                {
                    case 0:
                        demangler = new Demangler("Microsoft");
                        TestKnownInputs(demangler);
                        break;
                    case 1:
                        demangler = new Demangler();
                        TestFilepath(args[0], demangler);
                        
                        break;

                    default:
                    case 2:
                        string demanglerName = args[0].TrimStart(new char[] { '-' });
                        string Filepath = args[1];

                        demangler = new Demangler(demanglerName);
                        if (NativeFile.Exists(Filepath))
                        {
                            TestFilepath(Filepath, demangler);
                        }
                        else
                        {
                            Console.WriteLine(demangler.UndecorateName(args[1]));
                        }

                        break;
                }
            }
        }
    }
}<commit_msg>[demangler] add debug traces to console output<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

using Dependencies.ClrPh;

namespace Dependencies
{
    namespace Test
    {
        public class Demangler :  PhSymbolProvider
        {
            public Demangler(String demangler = "Default")
            {
                _demangler = demangler;
            }

            public override string UndecorateName(string DecoratedName)
            {

                switch (_demangler)
                {
                    case "Demumble":
                        return base.UndecorateNameDemumble(DecoratedName);
                    case "LLVMItanium":
                        return base.UndecorateNameLLVMItanium(DecoratedName);
                    case "LLVMMicrosoft":
                        return base.UndecorateNameLLVMMicrosoft(DecoratedName);
                    case "Microsoft":
                        return base.UndecorateNamePh(DecoratedName);

                    default:
                    case "Default":
                        return base.UndecorateName(DecoratedName);
                }

            }

            private string _demangler;
        }

        class Program
        {
            static bool TestKnownInputs(Demangler SymPrv)
            {
                Debug.Assert(SymPrv.UndecorateName("??1type_info@@UEAA@XZ") == "public: virtual __cdecl type_info::~type_info(void) __ptr64");
                Debug.Assert(SymPrv.UndecorateName("?setbuf@strstreambuf@@UEAAPEAVstreambuf@@PEADH@Z") == "public: virtual class streambuf * __ptr64 __cdecl strstreambuf::setbuf(char * __ptr64,int) __ptr64");
                Debug.Assert(SymPrv.UndecorateName("?CreateXBaby@XProvider@DirectUI@@UEAAJPEAVIXElementCP@2@PEAUHWND__@@PEAVElement@2@PEAKPEAPEAUIXBaby@2@@Z") == "public: virtual long __cdecl DirectUI::XProvider::CreateXBaby(class DirectUI::IXElementCP * __ptr64,struct HWND__ * __ptr64,class DirectUI::Element * __ptr64,unsigned long * __ptr64,struct DirectUI::IXBaby * __ptr64 * __ptr64) __ptr64");
                Debug.Assert(SymPrv.UndecorateName("??0exception@@QEAA@AEBQEBDH@Z") == "public: __cdecl exception::exception(char const * __ptr64 const & __ptr64,int) __ptr64");
                Debug.Assert(SymPrv.UndecorateName("?what@exception@@UEBAPEBDXZ") == "public: virtual char const * __ptr64 __cdecl exception::what(void)const __ptr64");
                Debug.Assert(SymPrv.UndecorateName("?_Execute_once@std@@YAHAEAUonce_flag@1@P6AHPEAX1PEAPEAX@Z1@Z") == "int __cdecl std::_Execute_once(struct std::once_flag & __ptr64,int (__cdecl*)(void * __ptr64,void * __ptr64,void * __ptr64 * __ptr64),void * __ptr64)");
                Debug.Assert(SymPrv.UndecorateName("?swap@?$basic_streambuf@_WU?$char_traits@_W@std@@@std@@IEAAXAEAV12@@Z") == "protected: void __cdecl std::basic_streambuf<wchar_t,struct std::char_traits<wchar_t> >::swap(class std::basic_streambuf<wchar_t,struct std::char_traits<wchar_t> > & __ptr64) __ptr64");

                Console.WriteLine("demangler-test : all known inputs OK");
                return true;
            }

            static bool TestFilepath(string Filepath, Demangler SymPrv)
            {

                PE Pe = new PE(Filepath);
                if (!Pe.Load())
                {
                    Console.Error.WriteLine("[x] Could not load file {0:s} as a PE", Filepath);
                    return false;
                }

                foreach (PeExport Export in Pe.GetExports())
                {
                    if (Export.Name.Length > 0)
                    {
                        Console.Write("\t Export : {0:s} -> ", Export.Name);
                        Console.Out.Flush();
                        Console.WriteLine("{0:s}", SymPrv.UndecorateName(Export.Name));
                    }
                        
                }

                foreach (PeImportDll DllImport in Pe.GetImports())
                {
                    foreach (PeImport Import in DllImport.ImportList)
                    {
                        if (!Import.ImportByOrdinal)
                        {
                            Console.Write("\t Import from {0:s} : {1:s} -> ", DllImport.Name, Import.Name);
                            Console.Out.Flush();
                            Console.WriteLine("{0:s}", SymPrv.UndecorateName(Import.Name));
                        }

                    }
                }

                return true;
            }

            static void Main(string[] args)
            {
                // always the first call to make
                Phlib.InitializePhLib();

                // Redirect debug log to the console
                Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
                Debug.AutoFlush = true;

                Demangler demangler;

                switch(args.Length)
                {
                    case 0:
                        demangler = new Demangler("Microsoft");
                        TestKnownInputs(demangler);
                        break;
                    case 1:
                        demangler = new Demangler();
                        TestFilepath(args[0], demangler);
                        
                        break;

                    default:
                    case 2:
                        string demanglerName = args[0].TrimStart(new char[] { '-' });
                        string Filepath = args[1];

                        demangler = new Demangler(demanglerName);
                        if (NativeFile.Exists(Filepath))
                        {
                            TestFilepath(Filepath, demangler);
                        }
                        else
                        {
                            string undecoratedName = demangler.UndecorateName(args[1]);
                            Console.WriteLine(undecoratedName);
                        }
                        break;
                }

                // Force flushing out buffer
                Console.Out.Flush();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09639759839893262, 'fraction_numerical': 0.012174783188792528, 'mean_word_length': 1.4636400986031224, 'pattern_counts': {'":': 10, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1531508', 'n_tokens_mistral': 3410, 'n_tokens_neox': 3110, 'n_words': 542}