< Summary

Information
Class: Utility.Components.HCF.HCF
Assembly: Utility
File(s): /home/runner/work/Utility-Blazor/Utility-Blazor/src/Utility/Components/HCF/HCF.razor
Tag: 231_14069517506
Line coverage
0%
Covered lines: 0
Uncovered lines: 133
Coverable lines: 133
Total lines: 267
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 34
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
OnInitializedAsync()100%210%
Encode()100%210%
Decode()100%210%
parseWhitespace(...)0%7280%
isRegularUnicodeCharacter(...)100%210%
htmlChar(...)0%7280%
text2html(...)0%342180%

File(s)

/home/runner/work/Utility-Blazor/Utility-Blazor/src/Utility/Components/HCF/HCF.razor

#LineLine coverage
 1@using System.IO
 2@using System.Text
 3@using System.Text.RegularExpressions
 4
 5<div class="container">
 6    <div class="row">
 7        <div class="col">
 8            <p><a href="https://www.soscisurvey.de/tools/view-chars.php" target="_blank">View non-printable unicode char
 9            <p><a href="https://github.com/BurninLeo/see-non-printable-characters/blob/main/view-chars.php"target="_blan
 10        </div>
 11    </div>
 12
 13    <div class="row">
 14        <div class="col">
 15            <h2>View non-printable unicode characters</h2>
 16            <p>Online tool to display non-printable characters that may be hidden in copy&pasted strings.</p>
 17        </div>
 18    </div>
 19    <div class="row">
 20        <div class="col">
 21            <label for="s">Please paste the string here:</label>
 22            <textarea id="s" name="s" class="form-control" rows="8" cols="40"
 23                @bind="s" style="width: 100%; box-sizing: border-box;"></textarea>
 24        </div>
 25    </div>
 26    <div class="row">
 27        <div class="col">
 28            <button id="btnEncode" name="btnEncode" class="btn btn-success float-right" @onclick="Encode"> Show me the c
 29        </div>
 30    </div>
 31
 32    <div class="row">
 33        <div class="col">
 34            @* <div class="output">
 35                @((MarkupString)output)
 36            </div> *@
 37
 38            @((MarkupString)output)
 39
 40            <p># characters, # bytes</p>
 41        </div>
 42    </div>
 43
 44    <div class="row">
 45        <div class="col">
 46            <div class="output2">
 47                <span class="S2Tooltip anchor">S</span>
 48                <span class="S2Tooltip container">
 49                    <span class="S2Tooltip tiptext rounded shadow">83<br>0x53</span>
 50                </span>
 51                <span class="hex S2Tooltip anchor">U+A0</span>
 52                <span class="S2Tooltip container">
 53                    <span class="S2Tooltip tiptext rounded shadow">&amp;#160;<br>\u00A0</span>
 54                </span>
 55            </div>
 56        </div>
 57    </div>
 58
 59    <div class="row">
 60        <div class="col">
 61
 62            <h2>Helpful Sites for Details on UTF Characters</h2>
 63            <ul>
 64                <li><a href="https://www.branah.com/unicode-converter" target="_blank">Branah.com Unicode Converter</a><
 65                <li><a href="http://www.fileformat.info/info/unicode/char/search.htm" target="_blank">FileFormat.Info</a
 66                <li><a href="http://utf8-chartable.de/unicode-utf8-table.pl" target="_blank">utf8-chartable.de</a></li>
 67            </ul>
 68
 69        </div>
 70    </div>
 71
 72</div>
 73
 74@code {
 75    string s;
 76    string output;
 77
 78    protected override async Task OnInitializedAsync()
 079    {
 80        @* s = "See what's hidden in your string…  or be​hind"; *@
 81        //s = "See\u00A0what\'s hidden in your string\u2026\tor be\\u200Bhind\uFEFF";
 082        s = "a …  ​⟶b";
 083        output = string.Empty;
 84        @* var conversion = htmlChar("S"); *@
 85        //var conversion = htmlChar("⟶");
 086        var conversion = htmlChar("a …  ​⟶b");
 087        Console.WriteLine($"Conversion: {conversion}");
 088        output = conversion;
 089    }
 90
 91    private void Encode()
 092    {
 093        var writer = new StringWriter();
 94        @* output = "abc"; // writer.ToString(); *@
 95
 096        var html = text2html(s);
 097        output = html;
 098    }
 99
 100    private void Decode()
 0101    {
 0102        s = "";
 0103    }
 104
 105    private string parseWhitespace(string c)
 0106    {
 0107        var symbol = "";
 0108        if (c == "\r")
 0109        {
 0110            symbol = "<span class='symbol S2Tooltip anchor'>CR</span>";
 0111        }
 0112        else if (c == "\n")
 0113        {
 0114            symbol = "<span class='symbol S2Tooltip anchor'>LF</span>";
 0115        }
 0116        else if (c == "\t")
 0117        {
 0118            symbol = "<span class='symbol S2Tooltip anchor'>⟶</span>&#8203;";
 0119        }
 0120        else if (c == " ")
 0121        {
 0122            symbol = "<span class='white S2Tooltip anchor'>·</span>&#8203;";
 0123        }
 0124        return symbol;
 0125    }
 126
 127    // Checks that the character is not in other or seperator groups.
 128    private bool isRegularUnicodeCharacter(string c)
 0129    {
 130
 0131        Console.WriteLine($"isRegularUnicodeCharacter Character: {c}");
 132
 133        //\p{M}\p{N}\p{P}\p{S}
 0134        var pattern = @"[\p{L}\p{M}\p{N}\p{P}\p{S}]";
 0135        Regex rx = new Regex(pattern);
 0136        MatchCollection matches = rx.Matches(c);
 137
 0138        Console.WriteLine($"isRegularUnicodeCharacter Character: {matches.Count}");
 139
 0140        return matches.Count > 0;
 0141    }
 142
 143    private string htmlChar(string c)
 0144    {
 0145        var desc = "";
 0146        var hex = "";
 147
 148        //var theSize = System.Runtime.InteropServices.Marshal.SizeOf(c);
 149        //Console.WriteLine($"Size: {theSize}")
 150
 0151        Console.WriteLine($"Length: {c.Length}");
 0152        var bytes = Encoding.UTF8.GetBytes(c);
 0153        Console.WriteLine($"Byte Length: {bytes.Length}");
 154
 0155        if (bytes.Length == 1)
 0156        {
 0157            var c1 = char.Parse(c);
 0158            desc = Convert.ToByte(c1) + "<br>\r\n" + String.Format("0x{0,2:X2}", (Convert.ToByte(c1)));
 0159            hex = String.Format("{0,2:X2}", (Convert.ToByte(c1)));
 0160        }
 161        else
 0162        {
 163            //@* $n = unpack('V', iconv('UTF-8', 'UCS-4LE', $c))[1]; *@
 164            // unassigned long
 165
 0166            bytes = Encoding.UTF8.GetBytes(c);
 167            //var bytes = BitConverter.GetBytes(c);
 0168            var isLittleEndian = BitConverter.IsLittleEndian;
 0169            if (isLittleEndian) {
 0170                Array.Reverse(bytes);
 0171            }
 172
 0173            desc = $"&amp;#{bytes.Select(x => (int)x).Sum()};<br>";
 0174            hex = "\\u" + BitConverter.ToString(bytes).Replace("-", String.Empty); // \u00A0
 0175        }
 176
 0177        Console.WriteLine($"CHAR: {c}");
 0178        Console.WriteLine($"DESC: {desc}");
 0179        Console.WriteLine($"HEX: {hex}");
 180
 0181        var symbol = parseWhitespace(c);
 182        // If symbol is not a whitespace char.
 0183        if (symbol == String.Empty)
 0184        {
 0185            var isRegularCharacter = isRegularUnicodeCharacter(c);
 186
 0187            if (isRegularCharacter)
 0188            {
 0189                symbol = "<span class='S2Tooltip anchor'>" + c + "</span>";
 0190            }
 191            else
 0192            {
 0193                symbol = "<span class='hex S2Tooltip anchor'>" + hex + "</span>";
 0194            }
 0195        }
 196
 0197        return symbol +
 0198            "<span class='S2Tooltip container'>" +
 0199            "<span class='S2Tooltip tiptext rounded shadow'>" + desc + "</span>" +
 0200            "</span>";
 0201    }
 202
 203    private string text2html(string s)
 0204    {
 205        // UTF-8 global?
 0206        var html = "<div class='output'>" + "\r\n";
 207
 0208        var sl = s.Length;
 0209        var nlc = 0;
 210
 211        // \n = LF (Line Feed) // Used as a new line character on Unix
 212        // \r = CR (Carriage Return) // Used as a new line character on Mac
 213        // \r\n = CR + LF // Used as a new line character on Windows
 214        // (char)13 = \r = CR
 215        // Environment.NewLine = any of the above code based on the operating system
 216
 0217        for (var i=0; i < sl; i++)
 0218        {
 0219            var c = s.Substring(i, 1); // Get Character from string at position.
 220
 221            //if and else is to deal with newline characters.
 0222            if (c == "\r")
 0223            {
 0224                if (nlc == 0)
 0225                {
 0226                    nlc = 1;
 0227                  html += htmlChar(c);
 0228                }
 0229                else if (nlc == 1)
 0230                {
 0231                    html += "<br>" + "\r\n" + htmlChar(c);
 0232                  nlc = 1;
 0233                }
 0234                else if (nlc == 2) {
 0235                  html += htmlChar(c) + "<br>" + "\r\n";
 0236                  nlc = 0;
 0237              }
 0238            }
 0239            else if (c == "\n")
 0240            {
 0241                var sym = htmlChar(c);
 242
 0243                if (nlc == 0)
 0244                {
 0245                  nlc = 2;
 0246                  html += sym;
 0247              }
 0248                else if (nlc == 2)
 0249                {
 0250                  html += "<br>" + "\r\n" + htmlChar(c);
 0251                  nlc = 2;
 0252              }
 0253                else if (nlc == 1)
 0254                {
 0255                  html += htmlChar(c) + "<br>" + "\r\n";
 0256                  nlc = 0;
 0257              }
 0258            }
 259            else
 0260            {
 0261              html += htmlChar(c);
 0262          }
 0263        }
 264
 0265        return html + "</div>" + "\r\n";
 0266    }
 267}