private void LoadSaveFile(string strFile) { StreamReader sr = new StreamReader(strFile); string textFile = LZString.DecompressFromBase64(sr.ReadToEnd()); textBoxSaveFileContent.Text = textFile; sr.Close(); }
private static bool TryPrepareSharplabPreview(string url, int markdownLength, out string?preview) { if (!url.Contains("#v2:")) { preview = null; return(false); } try { // Decode the compressed code from the URL payload var base64Text = url.Substring(url.IndexOf("#v2:") + "#v2:".Length); var plainText = LZString.DecompressFromBase64(base64Text); // Extract the option and get the target language var textParts = Regex.Match(plainText, @"([^|]*)\|([\s\S]*)$"); var languageOption = Regex.Match(textParts.Groups[1].Value, @"l:(\w+)"); var language = languageOption.Success ? languageOption.Groups[1].Value : "cs"; var sourceCode = textParts.Groups[2].Value; // Replace the compression tokens if (language is "cs") { sourceCode = ReplaceTokens(sourceCode, _sharplabCSTokens); // Strip using directives sourceCode = Regex.Replace(sourceCode, @"using \w+(?:\.\w+)*;", string.Empty); } else if (language is "il") { sourceCode = ReplaceTokens(sourceCode, _sharplabILTokens); } else { sourceCode = sourceCode.Replace("@@", "@"); } var maxPreviewLength = EmbedBuilder.MaxDescriptionLength - (markdownLength + language.Length + "```\n\n```".Length); preview = FormatUtilities.FormatCodeForEmbed(language, sourceCode, maxPreviewLength); return(!string.IsNullOrWhiteSpace(preview)); } catch { preview = null; return(false); } }
public void CompatibilityDecompressBase64FromNode(LZStringTestCase test) { var compress = RunNodeLzString("compressToBase64", test.Uncompressed); var uncompress = ""; try { uncompress = LZString.DecompressFromBase64(compress); } catch (FormatException exc) { Assert.Fail($"Invalid Base64 string: '{compress}'{Environment.NewLine}{exc.Message}"); } Console.WriteLine("lz-string compression result:"); Console.WriteLine(compress); Assert.That(uncompress, Is.EqualTo(test.Uncompressed)); }
public void DecompressFromBase64Performance() { var value = new string('x', 65536); value = LZString.CompressToBase64(value); LZString.DecompressFromBase64(value); // Warmup var timer = new Stopwatch(); timer.Start(); const int times = 1024; for (var i = 0; i < times; i++) { LZString.DecompressFromBase64(value); } timer.Stop(); Assert.Pass($"Did {times} decompressions in {timer.Elapsed.TotalSeconds}s. Average: {timer.ElapsedMilliseconds / times}ms"); }
public void DecompressFromBase64(LZStringTestCase test) { Assert.That(LZString.DecompressFromBase64(test.CompressedBase64), Is.EqualTo(test.Uncompressed)); }
private static bool TryPrepareSharplabPreview(string url, out string preview) { if (!url.Contains("#v2:")) { preview = null; return(false); } try { // Decode the compressed code from the URL payload string base64Text = url.Substring(url.IndexOf("#v2:") + "#v2:".Length); string plainText = LZString.DecompressFromBase64(base64Text); // Extract the option and get the target language var textParts = Regex.Match(plainText, @"([^|]*)\|([\s\S]*)$"); var languageOption = Regex.Match(textParts.Groups[1].Value, @"l:(\w+)"); string language = languageOption.Success ? languageOption.Groups[1].Value : "cs"; string sourceCode = textParts.Groups[2].Value; // Replace the compression tokens if (language is "cs") { sourceCode = Regex.Replace(sourceCode, @"@(\d+|@)", match => { if (match.Value is "@@") { return("@"); } return(_sharplabCSTokens[int.Parse(match.Groups[1].Value)]); }); // Strip using directives sourceCode = Regex.Replace(sourceCode, @"using \w+(?:\.\w+)*;", string.Empty); } else { sourceCode = sourceCode.Replace("@@", "@"); } // Trim, for good measure sourceCode = sourceCode.Trim(); // Clip to avoid Discord errors. // - 2048 is the maximum length // - 20 is the approximate length of the markdown URL and message // - 10 is the approximate length of the code highlight markdown int maximumLength = 2048 - (url.Length + 20 + 10); if (sourceCode.Length > maximumLength) { // Clip at the maximum length sourceCode = sourceCode.Substring(0, maximumLength); int lastCarriageIndex = sourceCode.LastIndexOf('\r'); // Remove the last line to avoid having code cut mid-statements if (lastCarriageIndex > 0) { sourceCode = sourceCode.Substring(0, lastCarriageIndex).Trim(); } } preview = $"```{language}\r{sourceCode}\r```"; return(true); } catch { preview = null; return(false); } }