{"id":2740,"date":"2019-10-28T11:56:58","date_gmt":"2019-10-28T10:56:58","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=2740"},"modified":"2024-02-01T15:44:31","modified_gmt":"2024-02-01T14:44:31","slug":"encode-decode-javascript-c-strings","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/encode-decode-javascript-c-strings\/","title":{"rendered":"Encode\/Decode javascript\/C# strings"},"content":{"rendered":"\n<style>  \n.my-grid {\n    display: grid;\n    grid-auto-flow: column;    \n    column-gap: 5px;\n    margin:10px\n}\n\n.auto-columns {\n\tgrid-auto-columns: max-content;\n}\n<\/style>\n<div class=\"my-grid\">\n<textarea id=\"input\" style=\"width:100%;height:700px;display:inline-block;font-family:monospace;\"><\/textarea>\n<textarea id=\"output\" style=\"width:100%;height:700px;display:inline-block;font-family:monospace;\"><\/textarea>\n<\/div>\n<div class=\"my-grid auto-columns\">\n<button id=\"btnEncode\" type=\"button\">Encode<\/button>\n<button id=\"btnDecode\" type=\"button\">Decode<\/button>\n<button id=\"btnBuildString\" type=\"button\">BuildString<\/button>\n<button id=\"btnReverseBuildString\" type=\"button\">Reverse BuildString<\/button>\n<label>\n<input type=\"checkbox\" id=\"chkSingleQuote\"\/>\nUse Single Quotes\n<label>\n<\/div>\n\n\n\n<p><br>Code:<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"javascript\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">\/*\n    Backspace is replaced with \\b\n    Newline is replaced with \\n\n    Tab is replaced with \\t\n    Carriage return is replaced with \\r\n    Form feed is replaced with \\f\n    Double quote is replaced with \\\"\n    Backslash is replaced with \\\\\n*\/\nconst $ = document.querySelector.bind(document);\nconst on = (e, evt, fn) => e.addEventListener(evt, fn);\nlet quote = '\"';\n\nconst encodeR = {\n\t\"\\b\": \"\\\\b\",\n\t\"\\n\": \"\\\\n\",\n\t\"\\t\": \"\\\\t\",\n\t\"\\r\": \"\\\\r\",\n\t\"\\f\": \"\\\\f\",\n\t\"\\\"\": \"\\\\\\\"\",\n\t\"\\\\\": \"\\\\\\\\\",\n};\nconst decodeR = {\n\t\"\\\\b\": \"\\b\",\n\t\"\\\\n\": \"\\n\",\n\t\"\\\\t\": \"\\t\",\n\t\"\\\\r\": \"\\r\",\n\t\"\\\\f\": \"\\f\",\n\t\"\\\\\\\"\": \"\\\"\",\n\t\"\\\\\\\\\": \"\\\\\" \n};\n\nconst switchQuote = () => {\n    const useSingleQuote = $(\"#chkSingleQuote\").checked;\n    quote = useSingleQuote ? \"'\" : '\"';\n\n    if (quote === \"'\") {\n        encodeR[\"\\'\"] = \"\\\\\\'\";\n        delete encodeR[\"\\\"\"];\n        decodeR[\"\\\\\\'\"] = \"\\'\";\n        delete decodeR[\"\\\\\\\"\"];\n    } else {\n        encodeR[\"\\\"\"] = \"\\\\\\\"\";\n        delete encodeR[\"\\'\"];\n        decodeR[\"\\\\\\\"\"] = \"\\\"\";\n        delete decodeR[\"\\\\\\'\"];\n    }\n}\n\nconst encode = (text) => {\n\tlet res = \"\";\n\tfor (const c of String(text)) {\n\t\tif (c in encodeR) {\n\t\t\tres += encodeR[c];\n\t\t} else {\n\t\t\tres += c;\n\t\t}\n\t}\n\treturn res;\n};\n\nconst decode = (text) => {\n\treturn String(text).replace(\/(\\\\.)\/gm, (group) => {\n\t\tif (group in decodeR) {\n\t\t\treturn decodeR[group];\n\t\t}\n\t\treturn group;\n\t});\n};\n\nconst buildString = (text) => {\n\tvar lines = text.split('\\n');\n\treturn lines\n\t.map(l => '\\t' + quote + encode(l) + quote)\n\t.join(\",\\n\");\n};\n\n\/\/ reverse buildstring\nconst stringPart = \/\"(.*?[^\\\\])\"\/gm;\nconst stringPartSq = \/'(.*?[^\\\\])'\/gm;\nconst getMatches = (re, str) => {\n    let match, matches = [];\n    while (match = re.exec(str)) {\n        matches.push(match);\n    }\n    return matches;\n};\n\nconst reverseBuildString = (text) => {\n    const regEx = quote === \"'\" ? stringPartSq : stringPart;\n    const lines = text.split('\\n');\n    const resultLines = [];\n    for(const line of lines) \n    {\n        const str = getMatches(regEx, line).map(m => decode(m[1])).join(\"\").trim();\n        if(str.length > 0) resultLines.push(str);\n    }\n    return resultLines.join(\"\\n\");\n};\n\nfunction copyStringToClipboard(string) {\n    function handler(event) {\n        event.clipboardData.setData('text\/plain', string);\n        event.preventDefault();\n        document.removeEventListener('copy', handler, true);\n    }\n    document.addEventListener('copy', handler, true);\n    document.execCommand('copy');\n}\n\non(window, \"DOMContentLoaded\", () => {\n\tconst input = $(\"#input\");\n\tconst output = $(\"#output\");\n\t\n\tfunction run(fn) {\n\t\treturn () => {\n\t\t\tswitchQuote();\n\t\t\toutput.value = fn();\n\t\t\tcopyStringToClipboard(output.value);\n\t\t};\n\t}\n\t\n\t$(\"#btnEncode\").addEventListener(\"click\", run(() => encode(input.value)));\n\t$(\"#btnDecode\").addEventListener(\"click\", run(() => decode(input.value)));\n\tconst prefix = \"var sql = string.Join(Environment.NewLine, new string[] {\\n\";\n\tconst postfix = \"\\n});\";\n    $(\"#btnBuildString\").addEventListener(\"click\", run(() => prefix + buildString(input.value) + postfix));\n    $(\"#btnReverseBuildString\").addEventListener(\"click\", run(() => reverseBuildString(input.value)));\n});<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Encode Decode BuildString Reverse BuildString Use Single Quotes Code:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,5,4,1],"tags":[],"class_list":["post-2740","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-javascript","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=2740"}],"version-history":[{"count":17,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2740\/revisions"}],"predecessor-version":[{"id":8251,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2740\/revisions\/8251"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=2740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=2740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=2740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}